|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object edu.cornell.cs.cs4120.xi.lexer.cup.LexerAdapter
public class LexerAdapter
An adapter that allows Lexer
instances to interface as CUP
Scanners.
This class adapts a Lexer to produce
ComplexSymbolFactory.ComplexSymbol instances for CUP. As such, this
Scanner works with CUP's provided ComplexSymbolFactory
symbol management tool, which is used to construct nonterminal symbols every
time it carries out reductions. The ComplexSymbolFactory handles
position tracking for your parsed entities. See the CUP manual for an example
of how to take advantage of this.
The Symbol.value of the returned Symbol
instances—the value of the terminals in your CUP specification—is
configurable. By default, these values are the Token
instances produced by the given lexer. This behavior may be changed by
extending this class and overriding valueOf(Token)
.
Constructor Summary | |
---|---|
LexerAdapter(Lexer lexer,
Class<?> cupSymClass)
Constructs an adapter for the given lexer and CUP-generated symbol class. |
Method Summary | |
---|---|
java_cup.runtime.ComplexSymbolFactory.ComplexSymbol |
next_token()
Returns a ComplexSymbolFactory.ComplexSymbol corresponding to the lexer's next token. |
protected Object |
valueOf(Token token)
Returns the object that will serve as the Symbol.value for symbols returned by this Scanner. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public LexerAdapter(Lexer lexer, Class<?> cupSymClass)
Call it like this:
Scanner cupScanner = new LexerAdapter(myLexer, sym.class);
The given class (named sym
by default) must be generated by CUP
and include a terminal declaration for every TokenType.
lexer
- a Xi lexercupSymClass
- a CUP-generated symbol class satisfying the above constraintsMethod Detail |
---|
public final java_cup.runtime.ComplexSymbolFactory.ComplexSymbol next_token()
This implementation sets the value of Symbol.value
for each
returned Symbol (except the EOF
symbol) to the result
of invoking valueOf(Token)
on the token returned by the lexer.
next_token
in interface java_cup.runtime.Scanner
CompilationException
- if there is a lexical errorprotected Object valueOf(Token token)
n
is the value returned by this method:
value ::= INTEGER_LITERAL:n {: RESULT = new IntegerNode(n); :}
Note that in the above example, the static type of the Java expression
n
in the generated code is determined by the type specified in the
TERMINAL
declaration for INTEGER_LITERAL
(CUP generates
code that performs the appropriate cast).
This implementation is the identity: the returned value is the input token. This method may be overridden to provide more convenient functionality (such as returning the literal values for integer, character, and string literal tokens).
token
- the token generated by the Lexer
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |