Class AbstractParser

java.lang.Object
com.oracle.js.parser.AbstractParser
Direct Known Subclasses:
Parser

public abstract class AbstractParser extends Object
Base class for parsers.
  • Field Details

    • source

      protected final Source source
      Source to parse.
    • errors

      protected final ErrorManager errors
      Error manager to report errors.
    • stream

      protected TokenStream stream
      Stream of lex tokens to parse.
    • k

      protected int k
      Index of current token.
    • previousToken

      protected long previousToken
      Previous token - accessible to sub classes
    • token

      protected long token
      Descriptor of current token.
    • type

      protected TokenType type
      Type of current token.
    • last

      protected TokenType last
      Type of last token.
    • start

      protected int start
      Start position of current token.
    • finish

      protected int finish
      Finish position of previous token.
    • line

      protected int line
      Current line number.
    • linePosition

      protected int linePosition
      Position of last EOL + 1.
    • lexer

      protected Lexer lexer
      Lexer used to scan source content.
    • isStrictMode

      protected boolean isStrictMode
      Is this parser running under strict mode?
    • lineOffset

      protected final int lineOffset
      What should line numbers be counted from?
  • Constructor Details

    • AbstractParser

      protected AbstractParser(Source source, ErrorManager errors, boolean strict, int lineOffset)
      Construct a parser.
      Parameters:
      source - Source to parse.
      errors - Error reporting manager.
      strict - True if we are in strict mode
      lineOffset - Offset from which lines should be counted
  • Method Details

    • getToken

      protected final long getToken(int i)
      Get the ith token.
      Parameters:
      i - Index of token.
      Returns:
      the token
    • T

      protected final TokenType T(int i)
      Return the tokenType of the ith token.
      Parameters:
      i - Index of token
      Returns:
      the token type
    • next

      protected final TokenType next()
      Seek next token that is not an EOL or comment.
      Returns:
      tokenType of next token.
    • nextOrEOL

      protected final TokenType nextOrEOL()
      Seek next token or EOL (skipping comments.)
      Returns:
      tokenType of next token.
    • message

      protected static String message(String msgId, String... args)
      Get the message string for a message ID and arguments
      Parameters:
      msgId - The Message ID
      args - The arguments
      Returns:
      The message string
    • message

      protected static String message(String msgId, IdentNode ident)
    • error

      protected final ParserException error(String message, long errorToken)
      Report an error.
      Parameters:
      message - Error message.
      errorToken - Offending token.
      Returns:
      ParserException upon failure. Caller should throw and not ignore
    • error

      protected final ParserException error(JSErrorType errorType, String message, long errorToken)
      Report an error.
      Parameters:
      errorType - The error type
      message - Error message.
      errorToken - Offending token.
      Returns:
      ParserException upon failure. Caller should throw and not ignore
    • error

      protected final ParserException error(String message)
      Report an error.
      Parameters:
      message - Error message.
      Returns:
      ParserException upon failure. Caller should throw and not ignore
    • error

      protected final ParserException error(JSErrorType errorType, String message)
      Report an error.
      Parameters:
      errorType - The error type
      message - Error message.
      Returns:
      ParserException upon failure. Caller should throw and not ignore
    • warning

      protected final void warning(JSErrorType errorType, String message, long errorToken)
      Report a warning to the error manager.
      Parameters:
      errorType - The error type of the warning
      message - Warning message.
      errorToken - error token
    • expectMessage

      protected final String expectMessage(TokenType expected)
      Generate 'expected' message.
      Parameters:
      expected - Expected tokenType.
      Returns:
      the message string
    • expectMessage

      protected final String expectMessage(TokenType expected, long errorToken)
    • expect

      protected final void expect(TokenType expected) throws ParserException
      Check current token and advance to the next token.
      Parameters:
      expected - Expected tokenType.
      Throws:
      ParserException - on unexpected token type
    • expectDontAdvance

      protected final void expectDontAdvance(TokenType expected) throws ParserException
      Check current token, but don't advance to the next token.
      Parameters:
      expected - Expected tokenType.
      Throws:
      ParserException - on unexpected token type
    • getValueNoEscape

      protected final Object getValueNoEscape()
      Get the value of the current token. If the current token contains an escape sequence, the method does not attempt to convert it.
      Returns:
      JavaScript value of the token.
    • getValue

      protected final Object getValue()
      Get the value of the current token.
      Returns:
      JavaScript value of the token.
    • getValue

      protected final Object getValue(long valueToken)
      Get the value of a specific token
      Parameters:
      valueToken - the token
      Returns:
      JavaScript value of the token
    • isNonStrictModeIdent

      protected final boolean isNonStrictModeIdent()
      Certain future reserved words can be used as identifiers in non-strict mode. Check if the current token is one such.
      Returns:
      true if non strict mode identifier
    • getIdent

      protected final IdentNode getIdent()
      Get ident.
      Returns:
      Ident node.
    • createIdentNode

      protected IdentNode createIdentNode(long identToken, int identFinish, com.oracle.truffle.api.strings.TruffleString name)
      Creates a new IdentNode as if invoked with a constructor but making sure that the name is deduplicated within this parse job.
      Parameters:
      identToken - the token for the new IdentNode
      identFinish - the finish for the new IdentNode
      name - the name for the new IdentNode. It will be de-duplicated.
      Returns:
      a newly constructed IdentNode with the specified token, finish, and name; the name will be deduplicated.
    • isIdentifierName

      protected final boolean isIdentifierName()
      Check if current token is in identifier name
      Returns:
      true if current token is an identifier name
    • isIdentifierName

      protected final boolean isIdentifierName(long currentToken)
      Check if token is an identifier name
      Returns:
      true if token is an identifier name
    • getIdentifierName

      protected final IdentNode getIdentifierName()
      Create an IdentNode from the current token
      Returns:
      an IdentNode representing the current token
    • getLiteral

      protected final LiteralNode<?> getLiteral() throws ParserException
      Create a LiteralNode from the current token
      Returns:
      LiteralNode representing the current token
      Throws:
      ParserException - if any literals fails to parse
    • validateLexerToken

      protected void validateLexerToken(Lexer.LexerToken lexerToken)
      Lexer token validation hook for subclasses.
      Parameters:
      lexerToken - the lexer token to validate
    • getNumberToStringConverter

      protected Function<Number,com.oracle.truffle.api.strings.TruffleString> getNumberToStringConverter()
      Custom number-to-string converter used to convert numeric property names to strings.
      Returns:
      custom number-to-string converter or null to use the default converter