antlr

Class Parser

public abstract class Parser extends Object

A generic ANTLR parser (LL(k) for k>=1) containing a bunch of utility routines useful at any lookahead depth. We distinguish between the LL(1) and LL(k) parsers because of efficiency. This may not be necessary in the near future. Each parser object contains the state of the parse including a lookahead cache (the form of which is determined by the subclass), whether or not the parser is in guess mode, where tokens come from, etc...

During guess mode, the current lookahead token(s) and token type(s) cache must be saved because the token stream may not have been informed to save the token (via mark) before the try block. Guessing is started by:

  1. saving the lookahead cache.
  2. marking the current position in the TokenBuffer.
  3. increasing the guessing level.
After guessing, the parser state is restored by:
  1. restoring the lookahead cache.
  2. rewinding the TokenBuffer.
  3. decreasing the guessing level.

See Also: Token TokenBuffer LLkParser

Field Summary
protected ASTFactoryastFactory
AST support code; parser delegates to this object.
protected ParserSharedInputStateinputState
protected ASTreturnAST
AST return value for a rule is squirreled away here
protected String[]tokenNames
Table of token type to token names
protected HashtabletokenTypeToASTClassMap
Constructed if any AST types specified in tokens{..}.
protected inttraceDepth
Used to keep track of indentdepth for traceIn/Out
Constructor Summary
Parser()
Parser(ParserSharedInputState state)
Method Summary
voidaddMessageListener(MessageListener l)
voidaddParserListener(ParserListener l)
voidaddParserMatchListener(ParserMatchListener l)
voidaddParserTokenListener(ParserTokenListener l)
voidaddSemanticPredicateListener(SemanticPredicateListener l)
voidaddSyntacticPredicateListener(SyntacticPredicateListener l)
voidaddTraceListener(TraceListener l)
abstract voidconsume()
Get another token object from the token stream
voidconsumeUntil(int tokenType)
Consume tokens until one matches the given token
voidconsumeUntil(BitSet set)
Consume tokens until one matches the given token set
protected voiddefaultDebuggingSetup(TokenStream lexer, TokenBuffer tokBuf)
ASTgetAST()
Get the AST return value squirreled away in the parser
ASTFactorygetASTFactory()
StringgetFilename()
ParserSharedInputStategetInputState()
StringgetTokenName(int num)
String[]getTokenNames()
HashtablegetTokenTypeToASTClassMap()
If the user specifies a tokens{} section with heterogeneous AST node types, then ANTLR generates code to fill this mapping.
booleanisDebugMode()
abstract intLA(int i)
Return the token type of the ith token of lookahead where i=1 is the current token being examined by the parser (i.e., it has not been matched yet).
abstract TokenLT(int i)
Return the ith token of lookahead
intmark()
voidmatch(int t)
Make sure current lookahead symbol matches token type t.
voidmatch(BitSet b)
Make sure current lookahead symbol matches the given set Throw an exception upon mismatch, which is catch by either the error handler or by the syntactic predicate.
voidmatchNot(int t)
static voidpanic()
voidrecover(RecognitionException ex, BitSet tokenSet)
voidremoveMessageListener(MessageListener l)
voidremoveParserListener(ParserListener l)
voidremoveParserMatchListener(ParserMatchListener l)
voidremoveParserTokenListener(ParserTokenListener l)
voidremoveSemanticPredicateListener(SemanticPredicateListener l)
voidremoveSyntacticPredicateListener(SyntacticPredicateListener l)
voidremoveTraceListener(TraceListener l)
voidreportError(RecognitionException ex)
Parser error-reporting function can be overridden in subclass
voidreportError(String s)
Parser error-reporting function can be overridden in subclass
voidreportWarning(String s)
Parser warning-reporting function can be overridden in subclass
voidrewind(int pos)
voidsetASTFactory(ASTFactory f)
Specify an object with support code (shared by Parser and TreeParser.
voidsetASTNodeClass(String cl)
voidsetASTNodeType(String nodeType)
Specify the type of node to create during tree building; use setASTNodeClass now to be consistent with Token Object Type accessor.
voidsetDebugMode(boolean debugMode)
voidsetFilename(String f)
voidsetIgnoreInvalidDebugCalls(boolean value)
voidsetInputState(ParserSharedInputState state)
voidsetTokenBuffer(TokenBuffer t)
Set or change the input token buffer
voidtraceIn(String rname)
voidtraceIndent()
voidtraceOut(String rname)

Field Detail

astFactory

protected ASTFactory astFactory
AST support code; parser delegates to this object. This is set during parser construction by default to either "new ASTFactory()" or a ctor that has a token type to class map for hetero nodes.

inputState

protected ParserSharedInputState inputState

returnAST

protected AST returnAST
AST return value for a rule is squirreled away here

tokenNames

protected String[] tokenNames
Table of token type to token names

tokenTypeToASTClassMap

protected Hashtable tokenTypeToASTClassMap
Constructed if any AST types specified in tokens{..}. Maps an Integer->Class object.

traceDepth

protected int traceDepth
Used to keep track of indentdepth for traceIn/Out

Constructor Detail

Parser

public Parser()

Parser

public Parser(ParserSharedInputState state)

Method Detail

addMessageListener

public void addMessageListener(MessageListener l)

addParserListener

public void addParserListener(ParserListener l)

addParserMatchListener

public void addParserMatchListener(ParserMatchListener l)

addParserTokenListener

public void addParserTokenListener(ParserTokenListener l)

addSemanticPredicateListener

public void addSemanticPredicateListener(SemanticPredicateListener l)

addSyntacticPredicateListener

public void addSyntacticPredicateListener(SyntacticPredicateListener l)

addTraceListener

public void addTraceListener(TraceListener l)

consume

public abstract void consume()
Get another token object from the token stream

consumeUntil

public void consumeUntil(int tokenType)
Consume tokens until one matches the given token

consumeUntil

public void consumeUntil(BitSet set)
Consume tokens until one matches the given token set

defaultDebuggingSetup

protected void defaultDebuggingSetup(TokenStream lexer, TokenBuffer tokBuf)

getAST

public AST getAST()
Get the AST return value squirreled away in the parser

getASTFactory

public ASTFactory getASTFactory()

getFilename

public String getFilename()

getInputState

public ParserSharedInputState getInputState()

getTokenName

public String getTokenName(int num)

getTokenNames

public String[] getTokenNames()

getTokenTypeToASTClassMap

public Hashtable getTokenTypeToASTClassMap()
If the user specifies a tokens{} section with heterogeneous AST node types, then ANTLR generates code to fill this mapping.

isDebugMode

public boolean isDebugMode()

LA

public abstract int LA(int i)
Return the token type of the ith token of lookahead where i=1 is the current token being examined by the parser (i.e., it has not been matched yet).

LT

public abstract Token LT(int i)
Return the ith token of lookahead

mark

public int mark()

match

public void match(int t)
Make sure current lookahead symbol matches token type t. Throw an exception upon mismatch, which is catch by either the error handler or by the syntactic predicate.

match

public void match(BitSet b)
Make sure current lookahead symbol matches the given set Throw an exception upon mismatch, which is catch by either the error handler or by the syntactic predicate.

matchNot

public void matchNot(int t)

panic

public static void panic()

Deprecated: as of 2.7.2. This method calls System.exit() and writes directly to stderr, which is usually not appropriate when a parser is embedded into a larger application. Since the method is static, it cannot be overridden to avoid these problems. ANTLR no longer uses this method internally or in generated code.

recover

public void recover(RecognitionException ex, BitSet tokenSet)

removeMessageListener

public void removeMessageListener(MessageListener l)

removeParserListener

public void removeParserListener(ParserListener l)

removeParserMatchListener

public void removeParserMatchListener(ParserMatchListener l)

removeParserTokenListener

public void removeParserTokenListener(ParserTokenListener l)

removeSemanticPredicateListener

public void removeSemanticPredicateListener(SemanticPredicateListener l)

removeSyntacticPredicateListener

public void removeSyntacticPredicateListener(SyntacticPredicateListener l)

removeTraceListener

public void removeTraceListener(TraceListener l)

reportError

public void reportError(RecognitionException ex)
Parser error-reporting function can be overridden in subclass

reportError

public void reportError(String s)
Parser error-reporting function can be overridden in subclass

reportWarning

public void reportWarning(String s)
Parser warning-reporting function can be overridden in subclass

rewind

public void rewind(int pos)

setASTFactory

public void setASTFactory(ASTFactory f)
Specify an object with support code (shared by Parser and TreeParser. Normally, the programmer does not play with this, using setASTNodeType instead.

setASTNodeClass

public void setASTNodeClass(String cl)

setASTNodeType

public void setASTNodeType(String nodeType)

Deprecated: since 2.7.1

Specify the type of node to create during tree building; use setASTNodeClass now to be consistent with Token Object Type accessor.

setDebugMode

public void setDebugMode(boolean debugMode)

setFilename

public void setFilename(String f)

setIgnoreInvalidDebugCalls

public void setIgnoreInvalidDebugCalls(boolean value)

setInputState

public void setInputState(ParserSharedInputState state)

setTokenBuffer

public void setTokenBuffer(TokenBuffer t)
Set or change the input token buffer

traceIn

public void traceIn(String rname)

traceIndent

public void traceIndent()

traceOut

public void traceOut(String rname)