Class: Ast::Ast

Inherits:
Object
  • Object
show all
Defined in:
lib/ast_ast/ast.rb

Defined Under Namespace

Classes: BlockDesc, Group, TokenDesc

Instance Attribute Summary collapse

For #token block collapse

Class Method Summary collapse

Instance Attribute Details

#block_descsObject

Returns the value of attribute block_descs.



3
4
5
# File 'lib/ast_ast/ast.rb', line 3

def block_descs
  @block_descs
end

#groupsObject

Returns the value of attribute groups.



3
4
5
# File 'lib/ast_ast/ast.rb', line 3

def groups
  @groups
end

#token_descsObject

Returns the value of attribute token_descs.



3
4
5
# File 'lib/ast_ast/ast.rb', line 3

def token_descs
  @token_descs
end

#tokensObject

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.

Examples:


block :begin => :end do |r|
  ...
end


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

See Also:



151
152
153
# File 'lib/ast_ast/ast.rb', line 151

def self.check(type=nil)
  @curr_tree.check(type)
end

.curr_treeAst::Tree

Returns current tree being read.

Returns:



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.

Examples:


group :names, [:john, :dave, :josh]


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

See Also:



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

See Also:



156
157
158
# File 'lib/ast_ast/ast.rb', line 156

def self.scan_until(type)
  @curr_tree.scan_until(type)
end

.token(name, &block) ⇒ Object

Creates a new token within the subclass. The block is executed when the token is found during the execution of #astify.

Examples:


class TestAst < Ast::Ast
  token :test do
    p 'test'
  end
end


51
52
53
54
# File 'lib/ast_ast/ast.rb', line 51

def self.token(name, &block)
  @token_descs ||= []
  @token_descs << TokenDesc.new(name, &block)
end