class ANTLR3::AST::TreeParser

TreeParser

TreeParser is the default base class of ANTLR-generated tree parsers. The class tailors the functionality provided by Recognizer to the task of tree-pattern recognition.

About Tree Parsers

ANTLR generates three basic types of recognizers:

Furthermore, it is capable of generating several different flavors of parser, including parsers that take token input and use it to build Abstract Syntax Trees (ASTs), tree structures that reflect the high-level syntactic and semantic structures defined by the language.

You can take the information encapsulated by the AST and process it directly in a program. However, ANTLR also provides a means to create a recognizer which is capable of walking through the AST, verifying its structure and performing custom actions along the way – tree parsers.

Tree parsers are created from tree grammars. ANTLR-generated tree parsers closely mirror the general structure of regular parsers and lexers.

For more in-depth coverage of the topic, check out the ANTLR documentation (www.antlr.org).

The Tree Parser API

Like Parser, the class does not stray too far from the Recognizer API. Mainly, it customizes a few methods specifically to deal with tree nodes (instead of basic tokens), and adds some helper methods for working with trees.

Like all ANTLR recognizers, tree parsers contained a shared state structure and an input stream, which should be a TreeNodeStream. ANTLR intends to keep its tree features flexible and customizable, and thus it does not make any assumptions about the class of the actual nodes it processes. One consequence of this flexibility is that tree parsers also require an extra tree adaptor object, the purpose of which is to provide a homogeneous interface for handling tree construction and analysis of your tree nodes.

See Tree and TreeAdaptor for more information.

Public Class Methods

main( argv = ARGV, options = {} ) { |main| ... } click to toggle source
# File lib/antlr3/tree.rb, line 105
def self.main( argv = ARGV, options = {} )
  if ::Hash === argv then argv, options = ARGV, argv end
  main = ANTLR3::Main::WalkerMain.new( self, options )
  block_given? ? yield( main ) : main.execute( argv )
end
new( input, options = {} ) click to toggle source
Calls superclass method ANTLR3::Recognizer::new
# File lib/antlr3/tree.rb, line 111
def initialize( input, options = {} )
  super( options )
  @input = input
end

Public Instance Methods

error_header( e ) click to toggle source
# File lib/antlr3/tree.rb, line 159
  def error_header( e )
    <<-END.strip!
    #{ grammar_file_name }: node from #{ 
      e.approximate_line_info? ? 'after ' : ''
    } line #{ e.line }:#{ e.column }
    END
  end
error_message( e ) click to toggle source
Calls superclass method ANTLR3::Recognizer#error_message
# File lib/antlr3/tree.rb, line 167
def error_message( e )
  adaptor = e.input.adaptor
  e.token = adaptor.token( e.node )
  e.token ||= create_token do | tok |
    tok.type = adaptor.type_of( e.node )
    tok.text = adaptor.text_of( e.node )
  end
  return super( e )
end
match_any( ignore = nil ) click to toggle source
# File lib/antlr3/tree.rb, line 133
def match_any( ignore = nil )
  @state.error_recovery = false
  
  look, adaptor = @input.look, @input.tree_adaptor
  if adaptor.child_count( look ) == 0
    @input.consume
    return
  end
  
  level = 0
  while type = @input.peek and type != EOF
    #token_type == EOF or ( token_type == UP && level == 0 )
    @input.consume
    case type
    when DOWN then level += 1
    when UP
      level -= 1
      level.zero? and break
    end
  end
end
mismatch( input, type, follow = nil ) click to toggle source
# File lib/antlr3/tree.rb, line 155
def mismatch( input, type, follow = nil )
  raise MismatchedTreeNode.new( type, input )
end
missing_symbol( error, expected_token_type, follow ) click to toggle source
# File lib/antlr3/tree.rb, line 123
def missing_symbol( error, expected_token_type, follow )
  name = token_name( expected_token_type ).to_s
  text = "<missing " << name << '>'
  tk = create_token do |t|
    t.text = text
    t.type = expected_token_type
  end
  return( CommonTree.new( tk ) )
end
source_name() click to toggle source
# File lib/antlr3/tree.rb, line 119
def source_name
  @input.source_name
end
trace_in( rule_name, rule_index ) click to toggle source
Calls superclass method ANTLR3::Recognizer#trace_in
# File lib/antlr3/tree.rb, line 177
def trace_in( rule_name, rule_index )
  super( rule_name, rule_index, @input.look )
end
trace_out( rule_name, rule_index ) click to toggle source
Calls superclass method ANTLR3::Recognizer#trace_out
# File lib/antlr3/tree.rb, line 181
def trace_out( rule_name, rule_index )
  super( rule_name, rule_index, @input.look )
end