edu.cornell.cs.cs4120.xi.lexer.cup
Class LexerAdapter

java.lang.Object
  extended by edu.cornell.cs.cs4120.xi.lexer.cup.LexerAdapter
All Implemented Interfaces:
java_cup.runtime.Scanner

public class LexerAdapter
extends Object
implements java_cup.runtime.Scanner

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).

See Also:
CUP manual section on symbol management

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

LexerAdapter

public LexerAdapter(Lexer lexer,
                    Class<?> cupSymClass)
Constructs an adapter for the given lexer and CUP-generated symbol class.

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.

Parameters:
lexer - a Xi lexer
cupSymClass - a CUP-generated symbol class satisfying the above constraints
Method Detail

next_token

public final java_cup.runtime.ComplexSymbolFactory.ComplexSymbol next_token()
Returns a ComplexSymbolFactory.ComplexSymbol corresponding to the lexer's next token. Returns an EOF token if the lexer has no more tokens.

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.

Specified by:
next_token in interface java_cup.runtime.Scanner
Returns:
a Symbol instance corresponding to the lexer's next token
Throws:
CompilationException - if there is a lexical error

valueOf

protected Object valueOf(Token token)
Returns the object that will serve as the Symbol.value for symbols returned by this Scanner. This value is what CUP provides to actions when you refer to labeled terminals in a CUP specification. For instance, in the following production specification, the value of 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).

Parameters:
token - the token generated by the Lexer
Returns:
the value to assign to Symbol.value of the outputted [Symbol