Class: Ast::Ast
- Inherits:
-
Object
- Object
- Ast::Ast
- Defined in:
- lib/ast_ast/ast.rb
Defined Under Namespace
Classes: BlockDesc, Group, TokenDesc
Instance Attribute Summary collapse
-
#block_descs ⇒ Object
Returns the value of attribute block_descs.
-
#groups ⇒ Object
Returns the value of attribute groups.
-
#token_descs ⇒ Object
Returns the value of attribute token_descs.
-
#tokens ⇒ Object
Returns the value of attribute tokens.
For #token block collapse
- .check(type = nil) ⇒ Object
-
.curr_tree ⇒ Ast::Tree
Current tree being read.
- .scan(type = nil) ⇒ Object
- .scan_until(type) ⇒ Object
Class Method Summary collapse
- .astify(tokens) ⇒ Object
-
.block(t, &block) ⇒ Object
Creates a block which begins with a certain token and ends with different token.
- .find_block(curr_desc = nil) ⇒ Object
-
.group(name, items) ⇒ Object
Creates a new group of token types, this allows you to refer to multiple tokens easily.
- .run_tokens(tok, descs) ⇒ Object
-
.token(name, &block) ⇒ Object
Creates a new token within the subclass.
Instance Attribute Details
#block_descs ⇒ Object
Returns the value of attribute block_descs.
3 4 5 |
# File 'lib/ast_ast/ast.rb', line 3 def block_descs @block_descs end |
#groups ⇒ Object
Returns the value of attribute groups.
3 4 5 |
# File 'lib/ast_ast/ast.rb', line 3 def groups @groups end |
#token_descs ⇒ Object
Returns the value of attribute token_descs.
3 4 5 |
# File 'lib/ast_ast/ast.rb', line 3 def token_descs @token_descs end |
#tokens ⇒ Object
Returns the value of attribute tokens.
3 4 5 |
# File 'lib/ast_ast/ast.rb', line 3 def tokens @tokens end |
Class Method Details
.astify(tokens) ⇒ Object
83 84 85 86 87 |
# File 'lib/ast_ast/ast.rb', line 83 def self.astify(tokens) @tokens = tokens t = find_block t = run_tokens(t, @token_descs) end |
.block(t, &block) ⇒ Object
Creates a block which begins with a certain token and ends with different token.
77 78 79 80 |
# File 'lib/ast_ast/ast.rb', line 77 def self.block(t, &block) @block_descs ||= [] @block_descs << BlockDesc.new(t.keys[0], t.values[0], &block) end |
.check(type = nil) ⇒ Object
151 152 153 |
# File 'lib/ast_ast/ast.rb', line 151 def self.check(type=nil) @curr_tree.check(type) end |
.curr_tree ⇒ Ast::Tree
Returns current tree being read.
161 162 163 |
# File 'lib/ast_ast/ast.rb', line 161 def self.curr_tree @curr_tree end |
.find_block(curr_desc = nil) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/ast_ast/ast.rb', line 113 def self.find_block(curr_desc=nil) body = Tree.new until @tokens.eot? # Check if closes current search if curr_desc && curr_desc.close == @tokens.curr_item.type @tokens.inc return body # Check if close token in wrong place elsif @block_descs.map(&:close).include?(@tokens.curr_item.type) raise "close found before open: #{@tokens.curr_item}" # Check if open token elsif @block_descs.map(&:open).include?(@tokens.curr_item.type) _desc = @block_descs.find_all {|i| i.open == @tokens.curr_item.type }[0] @tokens.inc found = find_block(_desc) body << Tree.new(_desc.block.call(found)) # Otherwise add to body, and start with next token else body << @tokens.curr_item @tokens.inc end end body end |
.group(name, items) ⇒ Object
Creates a new group of token types, this allows you to refer to multiple tokens easily.
63 64 65 66 |
# File 'lib/ast_ast/ast.rb', line 63 def self.group(name, items) @groups ||= [] @groups << Group.new(name, items) end |
.run_tokens(tok, descs) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/ast_ast/ast.rb', line 89 def self.run_tokens(tok, descs) r = [] @curr_tree = tok until tok.eot? i = tok.scan case i when Token # run the token _desc = descs.find_all{|j| j.name == i.type}[0] if _desc r << _desc.block.call(i) else r << i end when Tree # run the whole branch r << run_tokens(i, descs) end end r end |
.scan(type = nil) ⇒ Object
146 147 148 |
# File 'lib/ast_ast/ast.rb', line 146 def self.scan(type=nil) @curr_tree.scan(type) end |
.scan_until(type) ⇒ Object
156 157 158 |
# File 'lib/ast_ast/ast.rb', line 156 def self.scan_until(type) @curr_tree.scan_until(type) end |