Class: ANTLR3::Parser

Inherits:
Recognizer show all
Defined in:
lib/antlr3/recognizers.rb

Overview

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.

Direct Known Subclasses

Template::GroupFile::Parser

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_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

#input, #state

Attributes included from TokenFactory

#token_class

Class Method Summary collapse

Instance Method Summary collapse

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, #error_header, #error_message, generic_return_scope, #grammar_file_name, imported_grammars, master, master_grammars, #match, #match_any, #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

#create_token

Methods included from Error

EarlyExit, FailedPredicate, MismatchedNotSet, MismatchedRange, MismatchedSet, MismatchedToken, MismatchedTreeNode, MissingToken, NoViableAlternative, RewriteCardinalityError, RewriteEarlyExit, RewriteEmptyStream, UnwantedToken

Constructor Details

#initialize(input, options = {}) ⇒ Parser

Returns a new instance of Parser


1277
1278
1279
1280
1281
1282
# File 'lib/antlr3/recognizers.rb', line 1277

def initialize( input, options = {} )
  super( options )
  @input = nil
  reset
  @input = cast_input( input, options )
end

Class Method Details

.associated_lexerObject


1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
# 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

.main(argv = ARGV, options = {}) ⇒ Object


1257
1258
1259
1260
1261
# 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

Instance Method Details

#missing_symbol(error, expected_type, follow) ⇒ Object


1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
# 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

#source_nameObject


1308
1309
1310
# File 'lib/antlr3/recognizers.rb', line 1308

def source_name
  @input.source_name
end

#token_stream=(input) ⇒ Object


1301
1302
1303
1304
1305
# File 'lib/antlr3/recognizers.rb', line 1301

def token_stream=( input )
  @input = nil
  reset
  @input = input
end