Module: CodeTree::Rewriting::Rewriter::InstanceMethods
- Included in:
- CodeTree::Rewriting::Rewriter
- Defined in:
- lib/sbyc/codetree/rewriting/instance_methods.rb
Instance Attribute Summary collapse
-
#rules ⇒ Object
readonly
Installed rules.
-
#scope ⇒ Object
readonly
The scope.
Instance Method Summary collapse
- #ANY ⇒ Object
-
#apply(*args) ⇒ Object
Applies rules on a node.
-
#apply_args_conventions(*args) ⇒ Object
Applies conventions announced by the apply method.
-
#apply_on_node(node) ⇒ Object
Applies on a single node.
- #BRANCH ⇒ Object
-
#context_node ⇒ Object
Returns the current context node, being the top node on the stack.
-
#initialize {|_self| ... } ⇒ Object
Creates a Rewriter instance.
- #LEAF ⇒ Object
-
#node(function, *children) ⇒ Object
Produces a node by copying another one.
-
#rewrite(code = nil, scope = nil, &block) ⇒ Object
Rewrites some code.
-
#rule(match, &block) ⇒ Object
Adds a rule to the engine.
Instance Attribute Details
#rules ⇒ Object (readonly)
Installed rules
7 8 9 |
# File 'lib/sbyc/codetree/rewriting/instance_methods.rb', line 7 def rules @rules end |
#scope ⇒ Object (readonly)
The scope
10 11 12 |
# File 'lib/sbyc/codetree/rewriting/instance_methods.rb', line 10 def scope @scope end |
Instance Method Details
#ANY ⇒ Object
18 |
# File 'lib/sbyc/codetree/rewriting/instance_methods.rb', line 18 def ANY() Rewriter::Match::ANY; end |
#apply(*args) ⇒ Object
Applies rules on a node
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/sbyc/codetree/rewriting/instance_methods.rb', line 43 def apply(*args) case node = apply_args_conventions(*args) when CodeTree::AstNode apply_on_node(node) when Array node.collect{|c| c.kind_of?(CodeTree::AstNode) ? apply_on_node(c) : c} else node end end |
#apply_args_conventions(*args) ⇒ Object
Applies conventions announced by the apply method.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/sbyc/codetree/rewriting/instance_methods.rb', line 72 def apply_args_conventions(*args) if args.size == 1 and args[0].kind_of?(CodeTree::AstNode) args[0] elsif args.size > 1 and args[0].kind_of?(Symbol) function = args.shift children = args.collect{|c| apply_args_conventions(c)}.flatten CodeTree::AstNode.coerce([function, children]) elsif args.all?{|a| a.kind_of?(CodeTree::AstNode)} args elsif args.size == 1 args[0] else raise ArgumentError, "Unable to apply on #{args.inspect} (#{args.size})", caller end end |
#apply_on_node(node) ⇒ Object
Applies on a single node
55 56 57 58 59 60 61 62 |
# File 'lib/sbyc/codetree/rewriting/instance_methods.rb', line 55 def apply_on_node(node) raise ArgumentError, "Node expected, #{node.inspect} received" unless node.kind_of?(CodeTree::AstNode) @stack.push(node) rule = @rules.find{|r| r === node} result = (rule ? rule.apply(self, node) : nil) @stack.pop result end |
#BRANCH ⇒ Object
19 |
# File 'lib/sbyc/codetree/rewriting/instance_methods.rb', line 19 def BRANCH() Rewriter::Match::BRANCH; end |
#context_node ⇒ Object
Returns the current context node, being the top node on the stack
38 39 40 |
# File 'lib/sbyc/codetree/rewriting/instance_methods.rb', line 38 def context_node @stack.last end |
#initialize {|_self| ... } ⇒ Object
Creates a Rewriter instance
13 14 15 16 |
# File 'lib/sbyc/codetree/rewriting/instance_methods.rb', line 13 def initialize @rules = [] yield(self) if block_given? end |
#LEAF ⇒ Object
20 |
# File 'lib/sbyc/codetree/rewriting/instance_methods.rb', line 20 def LEAF() Rewriter::Match::LEAF; end |
#node(function, *children) ⇒ Object
Produces a node by copying another one
65 66 67 |
# File 'lib/sbyc/codetree/rewriting/instance_methods.rb', line 65 def node(function, *children) CodeTree::AstNode.coerce([function, children.flatten]) end |
#rewrite(code = nil, scope = nil, &block) ⇒ Object
Rewrites some code
28 29 30 31 32 33 34 35 |
# File 'lib/sbyc/codetree/rewriting/instance_methods.rb', line 28 def rewrite(code = nil, scope = nil, &block) @stack = [] @scope = block ? code : scope apply(CodeTree.coerce(block || code)) ensure @stack = nil @scope = nil end |