Class: ANTLR3::AST::TreeParser
- Inherits:
-
Recognizer
- Object
- Recognizer
- ANTLR3::AST::TreeParser
- Defined in:
- lib/antlr3/tree.rb
Overview
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:
-
lexers
-
parsers
-
tree parsers
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.
Constant Summary
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, Constants::INVALID_NODE, Constants::INVALID_TOKEN, Constants::MEMO_RULE_FAILED, Constants::MEMO_RULE_UNKNOWN, Constants::MIN_TOKEN_TYPE, Constants::SKIP_TOKEN, Constants::UP
Instance Attribute Summary
Attributes inherited from Recognizer
Attributes included from TokenFactory
Class Method Summary collapse
Instance Method Summary collapse
- #error_header(e) ⇒ Object
- #error_message(e) ⇒ Object
-
#initialize(input, options = {}) ⇒ TreeParser
constructor
A new instance of TreeParser.
- #match_any(ignore = nil) ⇒ Object
- #mismatch(input, type, follow = nil) ⇒ Object
- #missing_symbol(error, expected_token_type, follow) ⇒ Object
- #source_name ⇒ Object
- #trace_in(rule_name, rule_index) ⇒ Object
- #trace_out(rule_name, rule_index) ⇒ Object
Methods inherited from Recognizer
Scope, #already_parsed_rule?, #antlr_version, #antlr_version_string, #backtrack, #backtracking?, #backtracking_level, #backtracking_level=, #begin_resync, #combine_follows, #compute_context_sensitive_rule_follow, #compute_error_recovery_set, #consume_until, #current_symbol, debug?, define_return_scope, #display_recognition_error, #each_delegate, #emit_error_message, #end_resync, generated_using, generic_return_scope, #grammar_file_name, imported_grammars, imports, master, master_grammars, masters, #match, #memoize, #mismatch_is_missing_token?, #mismatch_is_unwanted_token?, #number_of_syntax_errors, profile?, #recover, #recover_from_mismatched_element, #recover_from_mismatched_set, #recover_from_mismatched_token, #report_error, #reset, #resync, return_scope_members, #rule_memoization, rules, #syntactic_predicate?, #syntax_errors?, token_class, #token_error_display
Methods included from TokenFactory
Methods included from Error
EarlyExit, FailedPredicate, MismatchedNotSet, MismatchedRange, MismatchedSet, MismatchedToken, MismatchedTreeNode, MissingToken, NoViableAlternative, RewriteCardinalityError, RewriteEarlyExit, RewriteEmptyStream, UnwantedToken
Constructor Details
#initialize(input, options = {}) ⇒ TreeParser
Returns a new instance of TreeParser.
111 112 113 114 |
# File 'lib/antlr3/tree.rb', line 111 def initialize( input, = {} ) super( ) @input = input end |
Class Method Details
.main(argv = ARGV, options = {}) ⇒ Object
105 106 107 108 109 |
# File 'lib/antlr3/tree.rb', line 105 def self.main( argv = ARGV, = {} ) if ::Hash === argv then argv, = ARGV, argv end main = ANTLR3::Main::WalkerMain.new( self, ) block_given? ? yield( main ) : main.execute( argv ) end |
Instance Method Details
#error_header(e) ⇒ Object
159 160 161 162 163 164 165 |
# 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) ⇒ Object
167 168 169 170 171 172 173 174 175 |
# File 'lib/antlr3/tree.rb', line 167 def ( 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) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# 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) ⇒ Object
155 156 157 |
# 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) ⇒ Object
123 124 125 126 127 128 129 130 131 |
# 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 ⇒ Object
119 120 121 |
# File 'lib/antlr3/tree.rb', line 119 def source_name @input.source_name end |
#trace_in(rule_name, rule_index) ⇒ Object
177 178 179 |
# 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) ⇒ Object
181 182 183 |
# File 'lib/antlr3/tree.rb', line 181 def trace_out( rule_name, rule_index ) super( rule_name, rule_index, @input.look ) end |