Top Description Inners Fields Constructors Methods
org.stringtemplate.v4.compiler

public Class STLexer

extends Object
implements TokenSource
Class Inheritance
All Implemented Interfaces
org.antlr.runtime.TokenSource
Imports
org.antlr.runtime.CharStream, .CommonToken, .MismatchedTokenException, .NoViableAltException, .RecognitionException, .Token, .TokenSource, org.stringtemplate.v4.STGroup, org.stringtemplate.v4.misc.ErrorManager, .Misc, java.util.ArrayList, .List

This class represents the tokenizer for templates. It operates in two modes: inside and outside of expressions. It implements the TokenSource interface so it can be used with ANTLR parsers. Outside of expressions, we can return these token types: TEXT, INDENT, LDELIM (start of expression), RCURLY (end of subtemplate), and NEWLINE. Inside of an expression, this lexer returns all of the tokens needed by STParser. From the parser's point of view, it can treat a template as a simple stream of elements.

This class defines the token types and communicates these values to STParser.g via STLexer.tokens file (which must remain consistent).

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
STLexer.STToken

We build STToken tokens instead of relying on CommonToken so we can override toString().

Field Summary

Modifier and TypeField and Description
public static final int
public static final int
public static final int
pack-priv char
c

current character

public static final int
public static final int
public static final int
pack-priv char
delimiterStartChar

The char which delimits the start of an expression.

pack-priv char
delimiterStopChar

The char which delimits the end of an expression.

public static final int
public static final int
public static final int
public static final int
public static final int
public static final char
public static final int
public static final int
pack-priv ErrorManager
public static final int
public static final int
public static final int
public static final int
pack-priv CharStream
public static final int
public static final int
public static final int
public static final int
public static final int
public static final int
public static final int
public static final int
public static final int
public static final int
public static final int
public static final int
pack-priv boolean
scanningInsideExpr

This keeps track of the current mode of the lexer.

public static final int
public static final Token
public static final int
pack-priv int
startCharIndex

When we started token, track initial coordinates so we can properly build token objects.

pack-priv int
pack-priv int
public static final int
public int
subtemplateDepth

To be able to properly track the inside/outside mode, we need to track how deeply nested we are in some templates.

public static final int
pack-priv Token
templateToken

template embedded in a group file?

public static final int
pack-priv List<Token>
tokens

Our lexer routines might have to emit more than a single token.

public static final int

Constructor Summary

AccessConstructor and Description
public
public
STLexer(ErrorManager errMgr, CharStream input, Token templateToken)

public
STLexer(ErrorManager errMgr, CharStream input, Token templateToken, char delimiterStartChar, char delimiterStopChar)

Method Summary

Modifier and TypeMethod and Description
public Token
pack-priv Token
protected void
public void
emit(Token token)

pack-priv Token
public String
getSourceName()

Implements org.antlr.runtime.TokenSource.getSourceName.

Where are you getting tokens from?
protected Token
public static boolean
isIDLetter(char c)

public static boolean
public static boolean
public static boolean
isWS(char c)

pack-priv void
public void
match(char x)

Consume if x is next character on the input stream.

pack-priv Token
mID()

ID  : ('a'..'z'|'A'..'Z'|'_'|'/')
      ('a'..'z'|'A'..'Z'|'0'..'9'|'_'|'/')*
    ;
pack-priv Token
mSTRING()

 STRING : '"'
          (   '\\' '"'
          |   '\\' ~'"'
          |   ~('\\'|'"')
          )*
          '"'
        ;
pack-priv Token
public Token
newToken(int ttype)

public Token
newToken(int ttype, String text, int pos)

public Token
newToken(int ttype, String text)

public Token
public Token
nextToken()

Implements org.antlr.runtime.TokenSource.nextToken.

Return a Token object from your input stream (usually a CharStream).
protected Token
public static String
str(int c)

pack-priv Token
pack-priv Token
pack-priv void
WS()

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

ANDback to summary
public static final int AND
ATback to summary
public static final int AT
BANGback to summary
public static final int BANG
cback to summary
pack-priv char c

current character

COLONback to summary
public static final int COLON
COMMAback to summary
public static final int COMMA
COMMENTback to summary
public static final int COMMENT
delimiterStartCharback to summary
pack-priv char delimiterStartChar

The char which delimits the start of an expression.

delimiterStopCharback to summary
pack-priv char delimiterStopChar

The char which delimits the end of an expression.

DOTback to summary
public static final int DOT
ELLIPSISback to summary
public static final int ELLIPSIS
ELSEback to summary
public static final int ELSE
ELSEIFback to summary
public static final int ELSEIF
ENDIFback to summary
public static final int ENDIF
EOFback to summary
public static final char EOF
EOF_TYPEback to summary
public static final int EOF_TYPE
EQUALSback to summary
public static final int EQUALS
errMgrback to summary
pack-priv ErrorManager errMgr
FALSEback to summary
public static final int FALSE
IDback to summary
public static final int ID
IFback to summary
public static final int IF
INDENTback to summary
public static final int INDENT
inputback to summary
pack-priv CharStream input
LBRACKback to summary
public static final int LBRACK
LCURLYback to summary
public static final int LCURLY
LDELIMback to summary
public static final int LDELIM
LPARENback to summary
public static final int LPAREN
NEWLINEback to summary
public static final int NEWLINE
ORback to summary
public static final int OR
PIPEback to summary
public static final int PIPE
RBRACKback to summary
public static final int RBRACK
RCURLYback to summary
public static final int RCURLY
RDELIMback to summary
public static final int RDELIM
REGION_ENDback to summary
public static final int REGION_END
RPARENback to summary
public static final int RPAREN
scanningInsideExprback to summary
pack-priv boolean scanningInsideExpr

This keeps track of the current mode of the lexer. Are we inside or outside an ST expression?

SEMIback to summary
public static final int SEMI
SKIPback to summary
public static final Token SKIP
SLASHback to summary
public static final int SLASH
startCharIndexback to summary
pack-priv int startCharIndex

When we started token, track initial coordinates so we can properly build token objects.

startCharPositionInLineback to summary
pack-priv int startCharPositionInLine
startLineback to summary
pack-priv int startLine
STRINGback to summary
public static final int STRING
subtemplateDepthback to summary
public int subtemplateDepth

To be able to properly track the inside/outside mode, we need to track how deeply nested we are in some templates. Otherwise, we know whether a '}' and the outermost subtemplate to send this back to outside mode.

SUPERback to summary
public static final int SUPER
templateTokenback to summary
pack-priv Token templateToken

template embedded in a group file? this is the template

TEXTback to summary
public static final int TEXT
tokensback to summary
pack-priv List<Token> tokens

Our lexer routines might have to emit more than a single token. We buffer everything through this list.

TRUEback to summary
public static final int TRUE

Constructor Detail

STLexerback to summary
public STLexer(CharStream input)
STLexerback to summary
public STLexer(ErrorManager errMgr, CharStream input, Token templateToken)
STLexerback to summary
public STLexer(ErrorManager errMgr, CharStream input, Token templateToken, char delimiterStartChar, char delimiterStopChar)

Method Detail

_nextTokenback to summary
public Token _nextToken()
COMMENTback to summary
pack-priv Token COMMENT()
consumeback to summary
protected void consume()
emitback to summary
public void emit(Token token)
ESCAPEback to summary
pack-priv Token ESCAPE()
getSourceNameback to summary
public String getSourceName()

Implements org.antlr.runtime.TokenSource.getSourceName.

Doc from org.antlr.runtime.TokenSource.getSourceName.

Where are you getting tokens from? normally the implication will simply ask lexers input stream.

Annotations
@Override
insideback to summary
protected Token inside()
isIDLetterback to summary
public static boolean isIDLetter(char c)
isIDStartLetterback to summary
public static boolean isIDStartLetter(char c)
isUnicodeLetterback to summary
public static boolean isUnicodeLetter(char c)
isWSback to summary
public static boolean isWS(char c)
LINEBREAKback to summary
pack-priv void LINEBREAK()
matchback to summary
public void match(char x)

Consume if x is next character on the input stream.

mIDback to summary
pack-priv Token mID()
ID  : ('a'..'z'|'A'..'Z'|'_'|'/')
      ('a'..'z'|'A'..'Z'|'0'..'9'|'_'|'/')*
    ;
mSTRINGback to summary
pack-priv Token mSTRING()
 STRING : '"'
          (   '\\' '"'
          |   '\\' ~'"'
          |   ~('\\'|'"')
          )*
          '"'
        ;
mTEXTback to summary
pack-priv Token mTEXT()
newTokenback to summary
public Token newToken(int ttype)
newTokenback to summary
public Token newToken(int ttype, String text, int pos)
newTokenback to summary
public Token newToken(int ttype, String text)
newTokenFromPreviousCharback to summary
public Token newTokenFromPreviousChar(int ttype)
nextTokenback to summary
public Token nextToken()

Implements org.antlr.runtime.TokenSource.nextToken.

Doc from org.antlr.runtime.TokenSource.nextToken.

Return a Token object from your input stream (usually a CharStream). Do not fail/return upon lexing error; keep chewing on the characters until you get a good one; errors are not passed through to the parser.

Annotations
@Override
outsideback to summary
protected Token outside()
strback to summary
public static String str(int c)
subTemplateback to summary
pack-priv Token subTemplate()
UNICODEback to summary
pack-priv Token UNICODE()
WSback to summary
pack-priv void WS()
org.stringtemplate.v4.compiler back to summary

public Class STLexer.STToken

extends CommonToken
Class Inheritance

We build STToken tokens instead of relying on CommonToken so we can override toString(). It just converts token types to token names like 23 to "LDELIM".

Field Summary

Inherited from org.antlr.runtime.CommonToken:
channelcharPositionInLineindexinputlinestartstoptexttype

Constructor Summary

AccessConstructor and Description
public
STToken(CharStream input, int type, int start, int stop)

public
STToken(int type, String text)

Method Summary

Modifier and TypeMethod and Description
public String
toString()

Overrides org.antlr.runtime.CommonToken.toString.

Returns a string representation of the object.
Inherited from org.antlr.runtime.CommonToken:
getChannelgetCharPositionInLinegetInputStreamgetLinegetStartIndexgetStopIndexgetTextgetTokenIndexgetTypesetChannelsetCharPositionInLinesetInputStreamsetLinesetStartIndexsetStopIndexsetTextsetTokenIndexsetType

Constructor Detail

STTokenback to summary
public STToken(CharStream input, int type, int start, int stop)
STTokenback to summary
public STToken(int type, String text)

Method Detail

toStringback to summary
public String toString()

Overrides org.antlr.runtime.CommonToken.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object

Annotations
@Override