Class: ANTLR3::AST::Wizard::PatternParser

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/antlr3/tree/wizard.rb

Overview

A class that is used internally by AST::Wizard to construct AST tree objects from a tokenized tree pattern

Constant Summary

CONTINUE_TYPES =
[ :open, :identifier, :percent, :dot ]

Constants included from Constants

Constants::BUILT_IN_TOKEN_NAMES, Constants::DEFAULT, Constants::DOWN, Constants::EOF, Constants::EOF_TOKEN, Constants::EOR_TOKEN_TYPE, Constants::HIDDEN, Constants::INVALID_TOKEN, Constants::INVALID_TOKEN_TYPE, Constants::MEMO_RULE_FAILED, Constants::MEMO_RULE_UNKNOWN, Constants::MIN_TOKEN_TYPE, Constants::SKIP_TOKEN, Constants::UP

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (PatternParser) initialize(tokenizer, token_scheme, adaptor)

Returns a new instance of PatternParser



193
194
195
196
197
198
# File 'lib/antlr3/tree/wizard.rb', line 193

def initialize( tokenizer, token_scheme, adaptor )
  @tokenizer = tokenizer
  @token_scheme = token_scheme
  @adaptor   = adaptor
  @token_type = tokenizer.next_token
end

Class Method Details

+ (Object) parse(pattern, token_scheme, adaptor)



186
187
188
189
# File 'lib/antlr3/tree/wizard.rb', line 186

def self.parse( pattern, token_scheme, adaptor )
  lexer = PatternLexer.new( pattern )
  new( lexer, token_scheme, adaptor ).pattern
end

Instance Method Details

- (Object) parse_node



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/antlr3/tree/wizard.rb', line 234

def parse_node
  label = nil
  if @token_type == :percent
    ( @token_type = @tokenizer.next_token ) == :identifier or return nil
    label = @tokenizer.text
    ( @token_type = @tokenizer.next_token ) == :colon or return nil
    @token_type = @tokenizer.next_token
  end
  
  if @token_type == :dot
    @token_type = @tokenizer.next_token
    wildcard_payload = CommonToken.create( :type => 0, :text => '.' )
    node = WildcardPattern.new( wildcard_payload )
    label and node.label = label
    return node
  end
  
  @token_type == :identifier or return nil
  token_name = @tokenizer.text
  @token_type = @tokenizer.next_token
  token_name == 'nil' and return @adaptor.create_flat_list
  
  text = token_name
  arg = nil
  if @token_type == :argument
    arg = @tokenizer.text
    text = arg
    @token_type = @tokenizer.next_token
  end
  
  node_type = @token_scheme[ token_name ] || INVALID_TOKEN_TYPE
  node = @adaptor.create_from_type( node_type, text )
  
  if Pattern === node
    node.label, node.has_text_arg = label, arg
  end
  return node
end

- (Object) parse_tree



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/antlr3/tree/wizard.rb', line 213

def parse_tree
  @token_type != :open and return nil
  @token_type = @tokenizer.next_token
  root = parse_node or return nil
  
  loop do
    case @token_type
    when :open
      subtree = parse_tree
      @adaptor.add_child( root, subtree )
    when :identifier, :percent, :dot
      child = parse_node or return nil
      @adaptor.add_child( root, child )
    else break
    end
  end
  @token_type == :close or return nil
  @token_type = @tokenizer.next_token
  return root
end

- (Object) pattern



200
201
202
203
204
205
206
207
208
209
# File 'lib/antlr3/tree/wizard.rb', line 200

def pattern
  case @token_type
  when :open then return parse_tree
  when :identifier
    node = parse_node
    @token_type == EOF and return node
    return nil
  end
  return nil
end