FileDocCategorySizeDatePackage
Parser.javaAPI DocGlassfish v2 API12914Wed Aug 30 15:34:10 BST 2006persistence.antlr

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
persistence.antlr.Token
see
persistence.antlr.TokenBuffer
see
persistence.antlr.Tokenizer
see
persistence.antlr.LL1Parser
see
persistence.antlr.LLkParser

Fields Summary
protected ParserSharedInputState
inputState
protected String[]
tokenNames
Table of token type to token names
protected AST
returnAST
AST return value for a rule is squirreled away here
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.
protected Hashtable
tokenTypeToASTClassMap
Constructed if any AST types specified in tokens{..}. Maps an Integer->Class object.
private boolean
ignoreInvalidDebugCalls
protected int
traceDepth
Used to keep track of indentdepth for traceIn/Out
Constructors Summary
public Parser()


      
        this(new ParserSharedInputState());
    
public Parser(ParserSharedInputState state)

        inputState = state;
    
Methods Summary
public 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).

public abstract persistence.antlr.TokenLT(int i)
Return the ith token of lookahead

public voidaddMessageListener(persistence.antlr.debug.MessageListener l)

        if (!ignoreInvalidDebugCalls)
            throw new IllegalArgumentException("addMessageListener() is only valid if parser built for debugging");
    
public voidaddParserListener(persistence.antlr.debug.ParserListener l)

        if (!ignoreInvalidDebugCalls)
            throw new IllegalArgumentException("addParserListener() is only valid if parser built for debugging");
    
public voidaddParserMatchListener(persistence.antlr.debug.ParserMatchListener l)

        if (!ignoreInvalidDebugCalls)
            throw new IllegalArgumentException("addParserMatchListener() is only valid if parser built for debugging");
    
public voidaddParserTokenListener(persistence.antlr.debug.ParserTokenListener l)

        if (!ignoreInvalidDebugCalls)
            throw new IllegalArgumentException("addParserTokenListener() is only valid if parser built for debugging");
    
public voidaddSemanticPredicateListener(persistence.antlr.debug.SemanticPredicateListener l)

        if (!ignoreInvalidDebugCalls)
            throw new IllegalArgumentException("addSemanticPredicateListener() is only valid if parser built for debugging");
    
public voidaddSyntacticPredicateListener(persistence.antlr.debug.SyntacticPredicateListener l)

        if (!ignoreInvalidDebugCalls)
            throw new IllegalArgumentException("addSyntacticPredicateListener() is only valid if parser built for debugging");
    
public voidaddTraceListener(persistence.antlr.debug.TraceListener l)

        if (!ignoreInvalidDebugCalls)
            throw new IllegalArgumentException("addTraceListener() is only valid if parser built for debugging");
    
public abstract voidconsume()
Get another token object from the token stream

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

        while (LA(1) != Token.EOF_TYPE && LA(1) != tokenType) {
            consume();
        }
    
public voidconsumeUntil(persistence.antlr.collections.impl.BitSet set)
Consume tokens until one matches the given token set

        while (LA(1) != Token.EOF_TYPE && !set.member(LA(1))) {
            consume();
        }
    
protected voiddefaultDebuggingSetup(persistence.antlr.TokenStream lexer, persistence.antlr.TokenBuffer tokBuf)

        // by default, do nothing -- we're not debugging
    
public persistence.antlr.collections.ASTgetAST()
Get the AST return value squirreled away in the parser

        return returnAST;
    
public persistence.antlr.ASTFactorygetASTFactory()

        return astFactory;
    
public java.lang.StringgetFilename()

        return inputState.filename;
    
public persistence.antlr.ParserSharedInputStategetInputState()

        return inputState;
    
public java.lang.StringgetTokenName(int num)

        return tokenNames[num];
    
public java.lang.String[]getTokenNames()

        return tokenNames;
    
public java.util.HashtablegetTokenTypeToASTClassMap()
If the user specifies a tokens{} section with heterogeneous AST node types, then ANTLR generates code to fill this mapping.

		return tokenTypeToASTClassMap;
	
public booleanisDebugMode()

        return false;
    
public intmark()

        return inputState.input.mark();
    
public voidmatch(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.

        if (LA(1) != t)
            throw new MismatchedTokenException(tokenNames, LT(1), t, false, getFilename());
        else
        // mark token as consumed -- fetch next token deferred until LA/LT
            consume();
    
public voidmatch(persistence.antlr.collections.impl.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.

        if (!b.member(LA(1)))
            throw new MismatchedTokenException(tokenNames, LT(1), b, false, getFilename());
        else
        // mark token as consumed -- fetch next token deferred until LA/LT
            consume();
    
public voidmatchNot(int t)

        if (LA(1) == t)
        // Throws inverted-sense exception
            throw new MismatchedTokenException(tokenNames, LT(1), t, true, getFilename());
        else
        // mark token as consumed -- fetch next token deferred until LA/LT
            consume();
    
public static voidpanic()

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.

        System.err.println("Parser: panic");
        System.exit(1);
    
public voidremoveMessageListener(persistence.antlr.debug.MessageListener l)

        if (!ignoreInvalidDebugCalls)
            throw new RuntimeException("removeMessageListener() is only valid if parser built for debugging");
    
public voidremoveParserListener(persistence.antlr.debug.ParserListener l)

        if (!ignoreInvalidDebugCalls)
            throw new RuntimeException("removeParserListener() is only valid if parser built for debugging");
    
public voidremoveParserMatchListener(persistence.antlr.debug.ParserMatchListener l)

        if (!ignoreInvalidDebugCalls)
            throw new RuntimeException("removeParserMatchListener() is only valid if parser built for debugging");
    
public voidremoveParserTokenListener(persistence.antlr.debug.ParserTokenListener l)

        if (!ignoreInvalidDebugCalls)
            throw new RuntimeException("removeParserTokenListener() is only valid if parser built for debugging");
    
public voidremoveSemanticPredicateListener(persistence.antlr.debug.SemanticPredicateListener l)

        if (!ignoreInvalidDebugCalls)
            throw new IllegalArgumentException("removeSemanticPredicateListener() is only valid if parser built for debugging");
    
public voidremoveSyntacticPredicateListener(persistence.antlr.debug.SyntacticPredicateListener l)

        if (!ignoreInvalidDebugCalls)
            throw new IllegalArgumentException("removeSyntacticPredicateListener() is only valid if parser built for debugging");
    
public voidremoveTraceListener(persistence.antlr.debug.TraceListener l)

        if (!ignoreInvalidDebugCalls)
            throw new RuntimeException("removeTraceListener() is only valid if parser built for debugging");
    
public voidreportError(persistence.antlr.RecognitionException ex)
Parser error-reporting function can be overridden in subclass

        System.err.println(ex);
    
public voidreportError(java.lang.String s)
Parser error-reporting function can be overridden in subclass

        if (getFilename() == null) {
            System.err.println("error: " + s);
        }
        else {
            System.err.println(getFilename() + ": error: " + s);
        }
    
public voidreportWarning(java.lang.String s)
Parser warning-reporting function can be overridden in subclass

        if (getFilename() == null) {
            System.err.println("warning: " + s);
        }
        else {
            System.err.println(getFilename() + ": warning: " + s);
        }
    
public voidrewind(int pos)

        inputState.input.rewind(pos);
    
public voidsetASTFactory(persistence.antlr.ASTFactory f)
Specify an object with support code (shared by Parser and TreeParser. Normally, the programmer does not play with this, using setASTNodeType instead.

        astFactory = f;
    
public voidsetASTNodeClass(java.lang.String cl)

        astFactory.setASTNodeType(cl);
    
public voidsetASTNodeType(java.lang.String nodeType)
Specify the type of node to create during tree building; use setASTNodeClass now to be consistent with Token Object Type accessor.

deprecated
since 2.7.1

        setASTNodeClass(nodeType);
    
public voidsetDebugMode(boolean debugMode)

        if (!ignoreInvalidDebugCalls)
            throw new RuntimeException("setDebugMode() only valid if parser built for debugging");
    
public voidsetFilename(java.lang.String f)

        inputState.filename = f;
    
public voidsetIgnoreInvalidDebugCalls(boolean value)

        ignoreInvalidDebugCalls = value;
    
public voidsetInputState(persistence.antlr.ParserSharedInputState state)

        inputState = state;
    
public voidsetTokenBuffer(persistence.antlr.TokenBuffer t)
Set or change the input token buffer

        inputState.input = t;
    
public voidtraceIn(java.lang.String rname)

        traceDepth += 1;
        traceIndent();
        System.out.println("> " + rname + "; LA(1)==" + LT(1).getText() +
                           ((inputState.guessing > 0)?" [guessing]":""));
    
public voidtraceIndent()

        for (int i = 0; i < traceDepth; i++)
            System.out.print(" ");
    
public voidtraceOut(java.lang.String rname)

        traceIndent();
        System.out.println("< " + rname + "; LA(1)==" + LT(1).getText() +
                           ((inputState.guessing > 0)?" [guessing]":""));
        traceDepth -= 1;