Class: Vernacular::AST::Modifier
- Inherits:
-
Object
- Object
- Vernacular::AST::Modifier
- Defined in:
- lib/vernacular/ast/modifier.rb
Overview
Represents a modification that will be performed against the AST between the time that the source code is read and the time that it is compiled down to YARV instruction sequences.
Defined Under Namespace
Classes: BuilderExtension, ParserExtension
Instance Attribute Summary collapse
-
#builder_extensions ⇒ Object
readonly
Returns the value of attribute builder_extensions.
-
#parser_extensions ⇒ Object
readonly
Returns the value of attribute parser_extensions.
-
#rewriter_block ⇒ Object
readonly
Returns the value of attribute rewriter_block.
Instance Method Summary collapse
- #build_rewriter(&block) ⇒ Object
- #components ⇒ Object
- #extend_builder(method, &block) ⇒ Object
- #extend_parser(symbol, pattern, code) ⇒ Object
-
#initialize {|_self| ... } ⇒ Modifier
constructor
A new instance of Modifier.
- #modify(source) ⇒ Object
Constructor Details
#initialize {|_self| ... } ⇒ Modifier
Returns a new instance of Modifier.
27 28 29 30 31 |
# File 'lib/vernacular/ast/modifier.rb', line 27 def initialize @builder_extensions = [] @parser_extensions = [] yield self if block_given? end |
Instance Attribute Details
#builder_extensions ⇒ Object (readonly)
Returns the value of attribute builder_extensions.
15 16 17 |
# File 'lib/vernacular/ast/modifier.rb', line 15 def builder_extensions @builder_extensions end |
#parser_extensions ⇒ Object (readonly)
Returns the value of attribute parser_extensions.
23 24 25 |
# File 'lib/vernacular/ast/modifier.rb', line 23 def parser_extensions @parser_extensions end |
#rewriter_block ⇒ Object (readonly)
Returns the value of attribute rewriter_block.
25 26 27 |
# File 'lib/vernacular/ast/modifier.rb', line 25 def rewriter_block @rewriter_block end |
Instance Method Details
#build_rewriter(&block) ⇒ Object
33 34 35 |
# File 'lib/vernacular/ast/modifier.rb', line 33 def build_rewriter(&block) @rewriter_block = block end |
#components ⇒ Object
58 59 60 61 |
# File 'lib/vernacular/ast/modifier.rb', line 58 def components (builder_extensions + parser_extensions).flat_map(&:components) + rewriter_block.source_location end |
#extend_builder(method, &block) ⇒ Object
37 38 39 |
# File 'lib/vernacular/ast/modifier.rb', line 37 def extend_builder(method, &block) builder_extensions << BuilderExtension.new(method, block) end |
#extend_parser(symbol, pattern, code) ⇒ Object
41 42 43 |
# File 'lib/vernacular/ast/modifier.rb', line 41 def extend_parser(symbol, pattern, code) parser_extensions << ParserExtension.new(symbol, pattern, code) end |
#modify(source) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/vernacular/ast/modifier.rb', line 45 def modify(source) raise 'You must first configure a rewriter!' unless rewriter_block rewriter = Class.new(Parser::TreeRewriter, &rewriter_block).new rewriter.instance_variable_set(:@parser, ASTParser.parser) buffer = Parser::Source::Buffer.new('<dynamic>') buffer.source = source ast = ASTParser.parse(source) rewriter.rewrite(buffer, ast) end |