fr.umlv.tatoo.runtime.parser
Class Parser<T,N,P,V>

java.lang.Object
  extended by fr.umlv.tatoo.runtime.parser.Parser<T,N,P,V>
Type Parameters:
T - type of terminal.
N - type of non-terminal.
P - type of production.
V - type of version.
All Implemented Interfaces:
SimpleParser<T>

public class Parser<T,N,P,V>
extends Object
implements SimpleParser<T>

Tatoo parser class.

Author:
Julien Cervelle

Method Summary
 ActionReturn branchOnError(T terminal, String message)
          Signals to the parser an external error, (by example a lexer error) has occurred allowing the parser to try to branch (enter or exit from the current grammar).
 void close()
          Indicates to the parser that there is no more terminals.
static
<T,N,P,V> Parser<T,N,P,V>
createParser(ParserTable<T,N,P,V> table, ParserListener<? super T,? super N,? super P> listener, ParserErrorRecoveryPolicy<T,N,P,V> policy, N start, V version, LookaheadMap<? extends T,? super V> lookaheadMap)
          Creates a parser.
static
<T> String
formatMessage(SimpleParser<T> parser, String message, T terminal)
          Default way to create a message from arguments
 int getBranchingLevel()
          Returns current the number of recursive call to this parser.
 BranchingParserListener<? super T> getBranchingParserListener()
          Returns the branching parser listener associated with the current parser.
 Set<? extends T> getLookahead()
          Returns the set of terminals which don't lead to an error for the current state with the current version.
 LookaheadMap<? extends T,? super V> getLookaheadMap()
          Returns the lookahead map.
 ParserListener<? super T,? super N,? super P> getParserListener()
          Returns the parser listener associated with the current parser.
 N getStartNonTerminal()
          Returns the start non-terminal
 ReadOnlyIntStack getStateStack()
          Returns a view on the current parser state stack.
 ParserTable<T,N,P,V> getTable()
          Returns the table associated with the parser.
 V getVersion()
           
 boolean isBranchingParser()
          Returns true if the current parser is a branching parser
 void push(N start, V version)
          Push a new context into the state stack
 void reset()
          Resets the parser and clears the state stack.
 void reset(N start)
          Resets the parser, clears the state stack and changes a new start non-terminal and reset the error recovery policy.
 void setBranchingParserListener(BranchingParserListener<? super T> branchingParserListener)
          Set the branching parser listener associated with the current parser.
 void setNeedRelex()
          Informs the parser that it needs to ask the lexer for relexing on error (this method should only be called by error recovery policy).
 void setVersion(V version)
          Change the version.
 SmartStepReturn smartStep(T next)
          Performs the actions induced by a particular terminal.
 void step(T next)
          Performs the actions induced by a particular terminal.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

createParser

public static <T,N,P,V> Parser<T,N,P,V> createParser(ParserTable<T,N,P,V> table,
                                                     ParserListener<? super T,? super N,? super P> listener,
                                                     ParserErrorRecoveryPolicy<T,N,P,V> policy,
                                                     N start,
                                                     V version,
                                                     LookaheadMap<? extends T,? super V> lookaheadMap)
Creates a parser.

Type Parameters:
T - type of terminal.
N - type of non-terminal.
P - type of production.
V - type of version.
Parameters:
table - the table produced by Tatoo.
lookaheadMap - a map of valid terminals in a particular state.
listener - the listener of parser actions.
policy - the error recovery policy.
start - the start non-terminal.
version -
Returns:
the parser
Throws:
IllegalStateException - if the initial state corresponding to the non-terminal is incompatible with the version.
See Also:
reset(Object), setVersion(Object)

reset

public void reset()
Resets the parser and clears the state stack. A call to reset() is roughly equivalent to a call to reset(Object) with the current start non-terminal.

Specified by:
reset in interface SimpleParser<T>

reset

public void reset(N start)
Resets the parser, clears the state stack and changes a new start non-terminal and reset the error recovery policy.

Parameters:
start - the new start non-terminal.
Throws:
IllegalStateException - if the initial state corresponding to the non-terminal is incompatible with the current version.
See Also:
reset(), push(Object,Object), ParserErrorRecoveryPolicy.reset()

setVersion

public void setVersion(V version)
Change the version. The parser stack must be only contains compatible.

Parameters:
version - version of the grammar
Throws:
IllegalStateException - if the state stack contains incompatible state with the version.
See Also:
reset(Object)

getStartNonTerminal

public N getStartNonTerminal()
Returns the start non-terminal

Returns:
the start non terminal
See Also:
reset(Object)

push

public void push(N start,
                 V version)
Push a new context into the state stack

Parameters:
start - the new start non-terminal.
version - new version
See Also:
close(), reset(Object)

getLookahead

public Set<? extends T> getLookahead()
Returns the set of terminals which don't lead to an error for the current state with the current version.

Specified by:
getLookahead in interface SimpleParser<T>
Returns:
a set of terminals or null if no lookahead states was provided at construction of the parser.

getVersion

public V getVersion()
Returns:
the version used by the parser according to the grammar.

getLookaheadMap

public LookaheadMap<? extends T,? super V> getLookaheadMap()
Returns the lookahead map.

Specified by:
getLookaheadMap in interface SimpleParser<T>
Returns:
the lookahead map or null if no lookahead map was provided at construction of the parser or by reset(Object).

getParserListener

public ParserListener<? super T,? super N,? super P> getParserListener()
Returns the parser listener associated with the current parser.

Returns:
the parser listener.

getBranchingParserListener

public BranchingParserListener<? super T> getBranchingParserListener()
Returns the branching parser listener associated with the current parser.

Returns:
the branching parser listener.

setBranchingParserListener

public void setBranchingParserListener(BranchingParserListener<? super T> branchingParserListener)
Set the branching parser listener associated with the current parser.

Parameters:
branchingParserListener - the new sub-parser listener or null.

isBranchingParser

public boolean isBranchingParser()
Returns true if the current parser is a branching parser The parser is a branching parser if a branching parser listener is set.

Specified by:
isBranchingParser in interface SimpleParser<T>
Returns:
true if the current parser is a branching parser
See Also:
setBranchingParserListener(BranchingParserListener)

getBranchingLevel

public int getBranchingLevel()
Returns current the number of recursive call to this parser. If the parser is not a branching parser the branching level is always zero. If the parser is a branching parser the branching level is the number of recursive calls to push.

Returns:
the number of recursive call to this parser.

formatMessage

public static <T> String formatMessage(SimpleParser<T> parser,
                                       String message,
                                       T terminal)
Default way to create a message from arguments

Type Parameters:
T - terminals type
Parameters:
parser - the parser
message - the message to include in the output
terminal - the terminal that caused this message
Returns:
the full formatted message

getTable

public ParserTable<T,N,P,V> getTable()
Returns the table associated with the parser.

Specified by:
getTable in interface SimpleParser<T>
Returns:
the parser table.

getStateStack

public ReadOnlyIntStack getStateStack()
Returns a view on the current parser state stack. The view is attached to the current parser states, if the parser stack changes by a call to step(Object) or close() then returned view is modified.

Specified by:
getStateStack in interface SimpleParser<T>
Returns:
an unmodifiable list of states.
See Also:
ReadOnlyIntStack.duplicate()

branchOnError

public ActionReturn branchOnError(T terminal,
                                  String message)
Signals to the parser an external error, (by example a lexer error) has occurred allowing the parser to try to branch (enter or exit from the current grammar).

Specified by:
branchOnError in interface SimpleParser<T>
See Also:
SimpleParser.isBranchingParser(), ParserErrorRecoveryPolicy.recoverOnError(Parser, IntArrayList, Object, String)

step

public void step(T next)
Performs the actions induced by a particular terminal.

Specified by:
step in interface SimpleParser<T>
Parameters:
next - the new terminal

smartStep

public SmartStepReturn smartStep(T next)
Performs the actions induced by a particular terminal. Return RELEX if the expected lookahead has changed during a reduce and that the lexer should reconsider its active rule set and NEXT otherwise. If this feature is used lexical semantic actions must not have already been performed. if NEXT token is required

Specified by:
smartStep in interface SimpleParser<T>
Parameters:
next - the new terminal
Returns:
RELEX if the expected lookahead has changed during a reduce and that the lexer should reconsider its active rule set and NEXT if NEXT token is required

setNeedRelex

public void setNeedRelex()
Informs the parser that it needs to ask the lexer for relexing on error (this method should only be called by error recovery policy).


close

public void close()
Indicates to the parser that there is no more terminals. This method clean the stack.

Specified by:
close in interface SimpleParser<T>