class ANTLR3::Parser
Parser
¶ ↑
Parser
is the default base class of ANTLR-generated parser classes. The class tailors the functionality provided by Recognizer
to the task of parsing.
About Parsing¶ ↑
This is just a lose overview of parsing. For considerably more in-depth coverage of the topic, read the ANTLR documentation or check out the ANTLR website (www.antlr.org).
A grammar defines the vocabulary and the sentence structure of a language. While a lexer concerns the basic vocabulary symbols of the language, a parser’s primary task is to implement the sentence structure.
Parsers are set up by providing a stream of tokens, which is usually created by a corresponding lexer. Then, the user requests a specific sentence-structure within the grammar, such as “class_definition” or “xml_node”, from the parser. It iterates through the tokens, verifying the syntax of the sentence and performing actions specified by the grammar. It stops when it encounters an error or when it has matched the full sentence according to its defined structure.
ANTLR Parsers and the Parser
API¶ ↑
Plain ANTLR-generated parsers directly subclass this class, unless specified otherwise within the grammar options. The generated code will provide a method for each parser rule defined in the ANTLR grammar, as well as any other customized member attributes and methods specified in the source grammar.
This class does not override much of the functionality in Recognizer
, and thus the API closely mirrors Recognizer
.
Public Class Methods
# File lib/antlr3/recognizers.rb, line 1263 def self.associated_lexer @associated_lexer ||= begin @grammar_home and @grammar_home::Lexer rescue NameError grammar_name = @grammar_home.name.split( "::" ).last begin require "#{ grammar_name }Lexer" @grammar_home::Lexer rescue LoadError, NameError end end end
# File lib/antlr3/recognizers.rb, line 1257 def self.main( argv = ARGV, options = {} ) if argv.is_a?( ::Hash ) then argv, options = ARGV, argv end main = ANTLR3::Main::ParserMain.new( self, options ) block_given? ? yield( main ) : main.execute( argv ) end
ANTLR3::Recognizer::new
# File lib/antlr3/recognizers.rb, line 1277 def initialize( input, options = {} ) super( options ) @input = nil reset @input = cast_input( input, options ) end
Public Instance Methods
# File lib/antlr3/recognizers.rb, line 1284 def missing_symbol( error, expected_type, follow ) current = @input.look current = @input.look( -1 ) if current == ANTLR3::EOF_TOKEN t = case when current && current != ANTLR3::EOF_TOKEN then current.clone when @input.token_class then @input.token_class.new else ( create_token rescue CommonToken.new ) end t.type = expected_type name = t.name.gsub( /(^<)|(>$)/,'' ) t.text = "<missing #{ name }>" t.channel = DEFAULT_CHANNEL return( t ) end
# File lib/antlr3/recognizers.rb, line 1308 def source_name @input.source_name end
# File lib/antlr3/recognizers.rb, line 1301 def token_stream=( input ) @input = nil reset @input = input end
Private Instance Methods
# File lib/antlr3/recognizers.rb, line 1323 def cast_input( input, options ) case input when TokenStream then input when TokenSource then CommonTokenStream.new( input, options ) when IO, String, CharacterStream if lexer_class = self.class.associated_lexer CommonTokenStream.new( lexer_class.new( input, options ), options ) else raise ArgumentError, Util.tidy( <<-END, true ) | unable to automatically convert input #{ input.inspect } | to a ANTLR3::TokenStream object as #{ self.class } | does not appear to have an associated lexer class END end else # assume it's a stream if it at least implements peek and consume unless input.respond_to?( :peek ) and input.respond_to?( :consume ) raise ArgumentError, Util.tidy( <<-END, true ) | #{ self.class } requires a token stream as input, but | #{ input.inspect } was provided END end input end end
ANTLR3::Recognizer#trace_in
# File lib/antlr3/recognizers.rb, line 1315 def trace_in( rule_name, rule_index ) super( rule_name, rule_index, @input.look.inspect ) end
ANTLR3::Recognizer#trace_out
# File lib/antlr3/recognizers.rb, line 1319 def trace_out( rule_name, rule_index ) super( rule_name, rule_index, @input.look.inspect ) end