Class: Vernacular::ASTModifier
- Inherits:
-
Object
- Object
- Vernacular::ASTModifier
- 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.
Direct Known Subclasses
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| ... } ⇒ ASTModifier
constructor
A new instance of ASTModifier.
- #modify(source) ⇒ Object
Constructor Details
#initialize {|_self| ... } ⇒ ASTModifier
Returns a new instance of ASTModifier.
24 25 26 27 28 |
# File 'lib/vernacular/ast_modifier.rb', line 24 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.
12 13 14 |
# File 'lib/vernacular/ast_modifier.rb', line 12 def builder_extensions @builder_extensions end |
#parser_extensions ⇒ Object (readonly)
Returns the value of attribute parser_extensions.
20 21 22 |
# File 'lib/vernacular/ast_modifier.rb', line 20 def parser_extensions @parser_extensions end |
#rewriter_block ⇒ Object (readonly)
Returns the value of attribute rewriter_block.
22 23 24 |
# File 'lib/vernacular/ast_modifier.rb', line 22 def rewriter_block @rewriter_block end |
Instance Method Details
#build_rewriter(&block) ⇒ Object
30 31 32 |
# File 'lib/vernacular/ast_modifier.rb', line 30 def build_rewriter(&block) @rewriter_block = block end |
#components ⇒ Object
55 56 57 58 |
# File 'lib/vernacular/ast_modifier.rb', line 55 def components (builder_extensions + parser_extensions).flat_map(&:components) + rewriter_block.source_location end |
#extend_builder(method, &block) ⇒ Object
34 35 36 |
# File 'lib/vernacular/ast_modifier.rb', line 34 def extend_builder(method, &block) builder_extensions << BuilderExtension.new(method, block) end |
#extend_parser(symbol, pattern, code) ⇒ Object
38 39 40 |
# File 'lib/vernacular/ast_modifier.rb', line 38 def extend_parser(symbol, pattern, code) parser_extensions << ParserExtension.new(symbol, pattern, code) end |
#modify(source) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/vernacular/ast_modifier.rb', line 42 def modify(source) raise 'You must first configure a rewriter!' unless rewriter_block rewriter = Class.new(Parser::Rewriter, &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 |