Class Lexer
java.lang.Object
com.oracle.js.parser.Scanner
com.oracle.js.parser.Lexer
- All Implemented Interfaces:
StringPool
Responsible for converting source content into a stream of tokens.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classHelper class for Lexer tokens, e.g XML or RegExp tokens.protected static interfaceinterface to receive line information for multi-line literals.static classTemporary container for regular expressions. -
Field Summary
-
Constructor Summary
ConstructorsConstructorDescriptionLexer(Source source, int start, int len, TokenStream stream, boolean scripting, int ecmaScriptVersion, boolean shebang, boolean isModule, boolean pauseOnFunctionBody, boolean allowBigInt, boolean annexB) ConstructorLexer(Source source, TokenStream stream, boolean scripting, int ecmaScriptVersion, boolean shebang, boolean isModule, boolean allowBigInt, boolean annexB) Constructor -
Method Summary
Modifier and TypeMethodDescriptionprotected voidAdd a new token to the stream.protected voidAdd a new token to the stream.booleancanStartLiteral(TokenType token) Return true if the given token can be the beginning of a literal.booleancheckIdentForKeyword(long token, String keyword) protected static intconvertDigit(char ch, int base) Convert a digit to a integer.protected voidGenerate a runtime exceptionstatic booleanisEOL(char ch) Test whether a char is valid JavaScript end of lineprotected static booleanisEscapeCharacter(char ch) Is the given character a valid escape char after "\" ?protected static booleanisStringDelimiter(char ch) Test if char is a string delimiter, e.g.static booleanisStringLineTerminator(char ch) Test whether a char is valid JavaScript end of string.static booleanisWhitespace(char ch) Test whether a char is valid JavaScript whitespacevoidlexify()Breaks source content down into lex units, adding tokens to the token stream.protected static StringGet the correctly localized error message for a given message id format argumentsprotected booleanscanLiteral(long token, TokenType startTokenType, Lexer.LineInfoReceiver lir) Check whether the given token represents the beginning of a literal.protected voidScan a number.protected voidscanString(boolean add) Scan over a string literal.protected final voidContinue scanning a template literal after an expression.com.oracle.truffle.api.strings.TruffleStringstringIntern(com.oracle.truffle.api.strings.TruffleString candidate) com.oracle.truffle.api.strings.TruffleStringstringIntern(String candidate) valueOfPattern(int start, int length) Convert a regex token to a token object.com.oracle.truffle.api.strings.TruffleStringvalueOfRawString(long token) Get the raw string value of a template literal string part.
-
Constructor Details
-
Lexer
public Lexer(Source source, TokenStream stream, boolean scripting, int ecmaScriptVersion, boolean shebang, boolean isModule, boolean allowBigInt, boolean annexB) Constructor- Parameters:
source- the sourcestream- the token stream to lexscripting- are we in scripting modeecmaScriptVersion- ECMAScript language versionshebang- do we support shebangisModule- are we in module
-
Lexer
public Lexer(Source source, int start, int len, TokenStream stream, boolean scripting, int ecmaScriptVersion, boolean shebang, boolean isModule, boolean pauseOnFunctionBody, boolean allowBigInt, boolean annexB) Constructor- Parameters:
source- the sourcestart- start position in source from which to start lexinglen- length of source segment to lexstream- token stream to lexscripting- are we in scripting modeecmaScriptVersion- ECMAScript language versionshebang- do we support shebangisModule- are we in modulepauseOnFunctionBody- if true, lexer will return fromlexify()when it encounters a function body. This is used with the feature where the parser is skipping nested function bodies to avoid reading ahead unnecessarily when we skip the function bodies.
-
-
Method Details
-
add
Add a new token to the stream.- Parameters:
type- Token type.start- Start position.end- End position.
-
add
Add a new token to the stream.- Parameters:
type- Token type.start- Start position.
-
isWhitespace
public static boolean isWhitespace(char ch) Test whether a char is valid JavaScript whitespace- Parameters:
ch- a char- Returns:
- true if valid JavaScript whitespace
-
isEOL
public static boolean isEOL(char ch) Test whether a char is valid JavaScript end of line- Parameters:
ch- a char- Returns:
- true if valid JavaScript end of line
-
isStringLineTerminator
public static boolean isStringLineTerminator(char ch) Test whether a char is valid JavaScript end of string. Line separators and paragraph separators can appear in JavaScript string literals.- Parameters:
ch- a char- Returns:
- true if valid JavaScript end of string
-
isStringDelimiter
protected static boolean isStringDelimiter(char ch) Test if char is a string delimiter, e.g. '\' or '"'.- Parameters:
ch- a char- Returns:
- true if string delimiter
-
valueOfPattern
Convert a regex token to a token object.- Parameters:
start- Position in source content.length- Length of regex token.- Returns:
- Regex token object.
-
canStartLiteral
Return true if the given token can be the beginning of a literal.- Parameters:
token- a token- Returns:
- true if token can start a literal.
-
scanLiteral
Check whether the given token represents the beginning of a literal. If so scan the literal and return true, otherwise return false.- Parameters:
token- the token.startTokenType- the token type.lir- LineInfoReceiver that receives line info for multi-line string literals.- Returns:
- True if a literal beginning with startToken was found and scanned.
-
convertDigit
protected static int convertDigit(char ch, int base) Convert a digit to a integer. Can't use Character.digit since we are restricted to ASCII by the spec.- Parameters:
ch- Character to convert.base- Numeric base.- Returns:
- The converted digit or -1 if invalid.
-
checkIdentForKeyword
-
scanString
protected void scanString(boolean add) Scan over a string literal.- Parameters:
add- true if we are not just scanning but should actually modify the token stream
-
scanTemplateSpan
protected final void scanTemplateSpan()Continue scanning a template literal after an expression. -
isEscapeCharacter
protected static boolean isEscapeCharacter(char ch) Is the given character a valid escape char after "\" ?- Parameters:
ch- character to be checked- Returns:
- if the given character is valid after "\"
-
scanNumber
protected void scanNumber()Scan a number. -
lexify
public void lexify()Breaks source content down into lex units, adding tokens to the token stream. The routine scans until the stream buffer is full. Can be called repeatedly until EOF is detected. -
valueOfRawString
public com.oracle.truffle.api.strings.TruffleString valueOfRawString(long token) Get the raw string value of a template literal string part.- Parameters:
token- template string token- Returns:
- raw string
-
stringIntern
public com.oracle.truffle.api.strings.TruffleString stringIntern(com.oracle.truffle.api.strings.TruffleString candidate) - Specified by:
stringInternin interfaceStringPool
-
stringIntern
-
message
-
error
Generate a runtime exception- Parameters:
message- error messagetype- token typestart- start position of lexed errorlength- length of lexed error- Throws:
ParserException- unconditionally
-