Class Parser

java.lang.Object
org.mozilla.javascript.Parser
Direct Known Subclasses:
IRFactory

public class Parser extends Object
This class implements the JavaScript parser.

It is based on the SpiderMonkey C source files jsparse.c and jsparse.h in the jsref package.

The parser generates an AstRoot parse tree representing the source code. No tree rewriting is permitted at this stage, so that the parse tree is a faithful representation of the source for frontend processing tools and IDEs.

This parser implementation is not intended to be reused after a parse finishes, and will throw an IllegalStateException() if invoked again.

See Also:
  • Field Details

    • ARGC_LIMIT

      public static final int ARGC_LIMIT
      Maximum number of allowed function or constructor arguments, to follow SpiderMonkey.
      See Also:
    • CLEAR_TI_MASK

      static final int CLEAR_TI_MASK
      See Also:
    • TI_AFTER_EOL

      static final int TI_AFTER_EOL
      See Also:
    • TI_CHECK_LABEL

      static final int TI_CHECK_LABEL
      See Also:
    • compilerEnv

      CompilerEnvirons compilerEnv
    • errorReporter

      private ErrorReporter errorReporter
    • errorCollector

      private IdeErrorReporter errorCollector
    • sourceURI

      private String sourceURI
    • sourceChars

      private char[] sourceChars
    • calledByCompileFunction

      boolean calledByCompileFunction
    • parseFinished

      private boolean parseFinished
    • ts

      private TokenStream ts
    • currentFlaggedToken

      private int currentFlaggedToken
    • currentToken

      private int currentToken
    • syntaxErrorCount

      private int syntaxErrorCount
    • scannedComments

      private List<Comment> scannedComments
    • currentJsDocComment

      private Comment currentJsDocComment
    • nestingOfFunction

      protected int nestingOfFunction
    • currentLabel

      private LabeledStatement currentLabel
    • inDestructuringAssignment

      private boolean inDestructuringAssignment
    • inUseStrictDirective

      protected boolean inUseStrictDirective
    • currentScriptOrFn

      ScriptNode currentScriptOrFn
    • currentScope

      Scope currentScope
    • endFlags

      private int endFlags
    • inForInit

      private boolean inForInit
    • labelSet

      private Map<String, LabeledStatement> labelSet
    • loopSet

      private List<Loop> loopSet
    • loopAndSwitchSet

      private List<Jump> loopAndSwitchSet
    • prevNameTokenStart

      private int prevNameTokenStart
    • prevNameTokenString

      private String prevNameTokenString
    • prevNameTokenLineno

      private int prevNameTokenLineno
    • defaultUseStrictDirective

      private boolean defaultUseStrictDirective
    • PROP_ENTRY

      private static final int PROP_ENTRY
      See Also:
    • GET_ENTRY

      private static final int GET_ENTRY
      See Also:
    • SET_ENTRY

      private static final int SET_ENTRY
      See Also:
    • METHOD_ENTRY

      private static final int METHOD_ENTRY
      See Also:
  • Constructor Details

  • Method Details

    • addStrictWarning

      void addStrictWarning(String messageId, String messageArg)
    • addStrictWarning

      void addStrictWarning(String messageId, String messageArg, int position, int length)
    • addWarning

      void addWarning(String messageId, String messageArg)
    • addWarning

      void addWarning(String messageId, int position, int length)
    • addWarning

      void addWarning(String messageId, String messageArg, int position, int length)
    • addError

      void addError(String messageId)
    • addError

      void addError(String messageId, int position, int length)
    • addError

      void addError(String messageId, String messageArg)
    • addError

      void addError(String messageId, int c)
    • addError

      void addError(String messageId, String messageArg, int position, int length)
    • addStrictWarning

      private void addStrictWarning(String messageId, String messageArg, int position, int length, int line, String lineSource, int lineOffset)
    • addWarning

      private void addWarning(String messageId, String messageArg, int position, int length, int line, String lineSource, int lineOffset)
    • addError

      private void addError(String messageId, String messageArg, int position, int length, int line, String lineSource, int lineOffset)
    • lookupMessage

      String lookupMessage(String messageId)
    • lookupMessage

      String lookupMessage(String messageId, String messageArg)
    • reportError

      void reportError(String messageId)
    • reportError

      void reportError(String messageId, String messageArg)
    • reportError

      void reportError(String messageId, int position, int length)
    • reportError

      void reportError(String messageId, String messageArg, int position, int length)
    • getNodeEnd

      private static int getNodeEnd(AstNode n)
    • recordComment

      private void recordComment(int lineno, String comment)
    • getAndResetJsDoc

      private Comment getAndResetJsDoc()
    • peekToken

      private int peekToken() throws IOException
      Throws:
      IOException
    • peekFlaggedToken

      private int peekFlaggedToken() throws IOException
      Throws:
      IOException
    • consumeToken

      private void consumeToken()
    • nextToken

      private int nextToken() throws IOException
      Throws:
      IOException
    • matchToken

      private boolean matchToken(int toMatch, boolean ignoreComment) throws IOException
      Throws:
      IOException
    • peekTokenOrEOL

      private int peekTokenOrEOL() throws IOException
      Throws:
      IOException
    • mustMatchToken

      private boolean mustMatchToken(int toMatch, String messageId, boolean ignoreComment) throws IOException
      Throws:
      IOException
    • mustMatchToken

      private boolean mustMatchToken(int toMatch, String msgId, int pos, int len, boolean ignoreComment) throws IOException
      Throws:
      IOException
    • mustHaveXML

      private void mustHaveXML()
    • eof

      public boolean eof()
    • insideFunction

      boolean insideFunction()
    • pushScope

      void pushScope(Scope scope)
    • popScope

      void popScope()
    • enterLoop

      private void enterLoop(Loop loop)
    • exitLoop

      private void exitLoop()
    • restoreRelativeLoopPosition

      private void restoreRelativeLoopPosition(Loop loop)
    • enterSwitch

      private void enterSwitch(SwitchStatement node)
    • exitSwitch

      private void exitSwitch()
    • parse

      public AstRoot parse(String sourceString, String sourceURI, int lineno)
      Builds a parse tree from the given source string.
      Returns:
      an AstRoot object representing the parsed program. If the parse fails, null will be returned. (The parse failure will result in a call to the ErrorReporter from CompilerEnvirons.)
    • parse

      @Deprecated public AstRoot parse(Reader sourceReader, String sourceURI, int lineno) throws IOException
      Deprecated.
      use parse(String, String, int) instead
      Builds a parse tree from the given sourcereader.
      Throws:
      IOException - if the Reader encounters an error
      See Also:
    • parse

      private AstRoot parse() throws IOException
      Throws:
      IOException
    • parseFunctionBody

      private AstNode parseFunctionBody(int type, FunctionNode fnNode) throws IOException
      Throws:
      IOException
    • getDirective

      private static String getDirective(AstNode n)
    • parseFunctionParams

      private void parseFunctionParams(FunctionNode fnNode) throws IOException
      Throws:
      IOException
    • function

      private FunctionNode function(int type) throws IOException
      Throws:
      IOException
    • function

      private FunctionNode function(int type, boolean isGenerator) throws IOException
      Throws:
      IOException
    • arrowFunction

      private AstNode arrowFunction(AstNode params) throws IOException
      Throws:
      IOException
    • arrowFunctionParams

      private void arrowFunctionParams(FunctionNode fnNode, AstNode params, Map<String,Node> destructuring, Set<String> paramNames)
    • statements

      private AstNode statements(AstNode parent) throws IOException
      Throws:
      IOException
    • statements

      private AstNode statements() throws IOException
      Throws:
      IOException
    • condition

      private Parser.ConditionData condition() throws IOException
      Throws:
      IOException
    • statement

      private AstNode statement() throws IOException
      Throws:
      IOException
    • statementHelper

      private AstNode statementHelper() throws IOException
      Throws:
      IOException
    • autoInsertSemicolon

      private void autoInsertSemicolon(AstNode pn) throws IOException
      Throws:
      IOException
    • ifStatement

      private IfStatement ifStatement() throws IOException
      Throws:
      IOException
    • switchStatement

      private SwitchStatement switchStatement() throws IOException
      Throws:
      IOException
    • whileLoop

      private WhileLoop whileLoop() throws IOException
      Throws:
      IOException
    • doLoop

      private DoLoop doLoop() throws IOException
      Throws:
      IOException
    • peekUntilNonComment

      private int peekUntilNonComment(int tt) throws IOException
      Throws:
      IOException
    • getNextStatementAfterInlineComments

      private AstNode getNextStatementAfterInlineComments(AstNode pn) throws IOException
      Throws:
      IOException
    • forLoop

      private Loop forLoop() throws IOException
      Throws:
      IOException
    • forLoopInit

      private AstNode forLoopInit(int tt) throws IOException
      Throws:
      IOException
    • tryStatement

      private TryStatement tryStatement() throws IOException
      Throws:
      IOException
    • throwStatement

      private ThrowStatement throwStatement() throws IOException
      Throws:
      IOException
    • matchJumpLabelName

      private LabeledStatement matchJumpLabelName() throws IOException
      Throws:
      IOException
    • breakStatement

      private BreakStatement breakStatement() throws IOException
      Throws:
      IOException
    • continueStatement

      private ContinueStatement continueStatement() throws IOException
      Throws:
      IOException
    • withStatement

      private WithStatement withStatement() throws IOException
      Throws:
      IOException
    • letStatement

      private AstNode letStatement() throws IOException
      Throws:
      IOException
    • nowAllSet

      private static final boolean nowAllSet(int before, int after, int mask)
      Returns whether or not the bits in the mask have changed to all set.
      Parameters:
      before - bits before change
      after - bits after change
      mask - mask for bits
      Returns:
      true if all the bits in the mask are set in "after" but not in "before"
    • returnOrYield

      private AstNode returnOrYield(int tt, boolean exprContext) throws IOException
      Throws:
      IOException
    • block

      private AstNode block() throws IOException
      Throws:
      IOException
    • defaultXmlNamespace

      private AstNode defaultXmlNamespace() throws IOException
      Throws:
      IOException
    • recordLabel

      private void recordLabel(Label label, LabeledStatement bundle) throws IOException
      Throws:
      IOException
    • nameOrLabel

      private AstNode nameOrLabel() throws IOException
      Found a name in a statement context. If it's a label, we gather up any following labels and the next non-label statement into a LabeledStatement "bundle" and return that. Otherwise we parse an expression and return it wrapped in an ExpressionStatement.
      Throws:
      IOException
    • variables

      private VariableDeclaration variables(int declType, int pos, boolean isStatement) throws IOException
      Parse a 'var' or 'const' statement, or a 'var' init list in a for statement.
      Parameters:
      declType - A token value: either VAR, CONST, or LET depending on context.
      pos - the position where the node should start. It's sometimes the var/const/let keyword, and other times the beginning of the first token in the first variable declaration.
      Returns:
      the parsed variable list
      Throws:
      IOException
    • let

      private AstNode let(boolean isStatement, int pos) throws IOException
      Throws:
      IOException
    • defineSymbol

      void defineSymbol(int declType, String name)
    • defineSymbol

      void defineSymbol(int declType, String name, boolean ignoreNotInBlock)
    • expr

      private AstNode expr() throws IOException
      Throws:
      IOException
    • assignExpr

      private AstNode assignExpr() throws IOException
      Throws:
      IOException
    • condExpr

      private AstNode condExpr() throws IOException
      Throws:
      IOException
    • orExpr

      private AstNode orExpr() throws IOException
      Throws:
      IOException
    • andExpr

      private AstNode andExpr() throws IOException
      Throws:
      IOException
    • bitOrExpr

      private AstNode bitOrExpr() throws IOException
      Throws:
      IOException
    • bitXorExpr

      private AstNode bitXorExpr() throws IOException
      Throws:
      IOException
    • bitAndExpr

      private AstNode bitAndExpr() throws IOException
      Throws:
      IOException
    • eqExpr

      private AstNode eqExpr() throws IOException
      Throws:
      IOException
    • relExpr

      private AstNode relExpr() throws IOException
      Throws:
      IOException
    • shiftExpr

      private AstNode shiftExpr() throws IOException
      Throws:
      IOException
    • addExpr

      private AstNode addExpr() throws IOException
      Throws:
      IOException
    • mulExpr

      private AstNode mulExpr() throws IOException
      Throws:
      IOException
    • expExpr

      private AstNode expExpr() throws IOException
      Throws:
      IOException
    • unaryExpr

      private AstNode unaryExpr() throws IOException
      Throws:
      IOException
    • xmlInitializer

      private AstNode xmlInitializer() throws IOException
      Throws:
      IOException
    • argumentList

      private List<AstNode> argumentList() throws IOException
      Throws:
      IOException
    • memberExpr

      private AstNode memberExpr(boolean allowCallSyntax) throws IOException
      Parse a new-expression, or if next token isn't Token.NEW, a primary expression.
      Parameters:
      allowCallSyntax - passed down to memberExprTail(boolean, AstNode)
      Throws:
      IOException
    • memberExprTail

      private AstNode memberExprTail(boolean allowCallSyntax, AstNode pn) throws IOException
      Parse any number of "(expr)", "[expr]" ".expr", "..expr", or ".(expr)" constructs trailing the passed expression.
      Parameters:
      pn - the non-null parent node
      Returns:
      the outermost (lexically last occurring) expression, which will have the passed parent node as a descendant
      Throws:
      IOException
    • taggedTemplateLiteral

      private AstNode taggedTemplateLiteral(AstNode pn) throws IOException
      Throws:
      IOException
    • propertyAccess

      private AstNode propertyAccess(int tt, AstNode pn) throws IOException
      Handles any construct following a "." or ".." operator.
      Parameters:
      pn - the left-hand side (target) of the operator. Never null.
      Returns:
      a PropertyGet, XmlMemberGet, or ErrorNode
      Throws:
      IOException
    • attributeAccess

      private AstNode attributeAccess() throws IOException
      Xml attribute expression:

      @attr, @ns::attr, @ns::*, @ns::*, @*, @*::attr, @*::*, @ns::[expr], @*::[expr], @[expr]

      Called if we peeked an '@' token.

      Throws:
      IOException
    • propertyName

      private AstNode propertyName(int atPos, int memberTypeFlags) throws IOException
      Check if :: follows name in which case it becomes a qualified name.
      Parameters:
      atPos - a natural number if we just read an '@' token, else -1
      memberTypeFlags - flags tracking whether we're a '.' or '..' child
      s - the name or string that was matched (an identifier, "throw" or "*").
      Returns:
      an XmlRef node if it's an attribute access, a child of a '..' operator, or the name is followed by ::. For a plain name, returns a Name node. Returns an ErrorNode for malformed XML expressions. (For now - might change to return a partial XmlRef.)
      Throws:
      IOException
    • xmlElemRef

      private XmlElemRef xmlElemRef(int atPos, Name namespace, int colonPos) throws IOException
      Parse the [expr] portion of an xml element reference, e.g. @[expr], @*::[expr], or ns::[expr].
      Throws:
      IOException
    • destructuringPrimaryExpr

      private AstNode destructuringPrimaryExpr() throws IOException, Parser.ParserException
      Throws:
      IOException
      Parser.ParserException
    • primaryExpr

      private AstNode primaryExpr() throws IOException
      Throws:
      IOException
    • parenExpr

      private AstNode parenExpr() throws IOException
      Throws:
      IOException
    • name

      private AstNode name(int ttFlagged, int tt) throws IOException
      Throws:
      IOException
    • arrayLiteral

      private AstNode arrayLiteral() throws IOException
      Throws:
      IOException
    • arrayComprehension

      private AstNode arrayComprehension(AstNode result, int pos) throws IOException
      Parse a JavaScript 1.7 Array comprehension.
      Parameters:
      result - the first expression after the opening left-bracket
      pos - start of LB token that begins the array comprehension
      Returns:
      the array comprehension or an error node
      Throws:
      IOException
    • arrayComprehensionLoop

      private ArrayComprehensionLoop arrayComprehensionLoop() throws IOException
      Throws:
      IOException
    • generatorExpression

      private AstNode generatorExpression(AstNode result, int pos) throws IOException
      Throws:
      IOException
    • generatorExpression

      private AstNode generatorExpression(AstNode result, int pos, boolean inFunctionParams) throws IOException
      Throws:
      IOException
    • generatorExpressionLoop

      private GeneratorExpressionLoop generatorExpressionLoop() throws IOException
      Throws:
      IOException
    • objectLiteral

      private ObjectLiteral objectLiteral() throws IOException
      Throws:
      IOException
    • objliteralProperty

      private AstNode objliteralProperty() throws IOException
      Throws:
      IOException
    • plainProperty

      private ObjectProperty plainProperty(AstNode property, int ptt) throws IOException
      Throws:
      IOException
    • methodDefinition

      private ObjectProperty methodDefinition(int pos, AstNode propName, int entryKind) throws IOException
      Throws:
      IOException
    • createNameNode

      private Name createNameNode()
    • createNameNode

      private Name createNameNode(boolean checkActivation, int token)
      Create a Name node using the token info from the last scanned name. In some cases we need to either synthesize a name node, or we lost the name token information by peeking. If the token parameter is not Token.NAME, then we use token info saved in instance vars.
    • createStringLiteral

      private StringLiteral createStringLiteral()
    • templateLiteral

      private AstNode templateLiteral(boolean isTaggedLiteral) throws IOException
      Throws:
      IOException
    • createTemplateLiteralCharacters

      private TemplateCharacters createTemplateLiteralCharacters(int pos)
    • createNumericLiteral

      private AstNode createNumericLiteral(int tt, boolean isProperty)
    • checkActivationName

      protected void checkActivationName(String name, int token)
    • setRequiresActivation

      protected void setRequiresActivation()
    • checkCallRequiresActivation

      private void checkCallRequiresActivation(AstNode pn)
    • setIsGenerator

      protected void setIsGenerator()
    • checkBadIncDec

      private void checkBadIncDec(UpdateExpression expr)
    • makeErrorNode

      private ErrorNode makeErrorNode()
    • nodeEnd

      private static int nodeEnd(AstNode node)
    • saveNameTokenData

      private void saveNameTokenData(int pos, String name, int lineno)
    • lineBeginningFor

      private int lineBeginningFor(int pos)
      Return the file offset of the beginning of the input source line containing the passed position.
      Parameters:
      pos - an offset into the input source stream. If the offset is negative, it's converted to 0, and if it's beyond the end of the source buffer, the last source position is used.
      Returns:
      the offset of the beginning of the line containing pos (i.e. 1+ the offset of the first preceding newline). Returns -1 if the CompilerEnvirons is not set to ide-mode, and parse(java.io.Reader,String,int) was used.
    • warnMissingSemi

      private void warnMissingSemi(int pos, int end)
    • warnTrailingComma

      private void warnTrailingComma(int pos, List<?> elems, int commaPos)
    • createDestructuringAssignment

      Node createDestructuringAssignment(int type, Node left, Node right)
      Given a destructuring assignment with a left hand side parsed as an array or object literal and a right hand side expression, rewrite as a series of assignments to the variables defined in left from property accesses to the expression on the right.
      Parameters:
      type - declaration type: Token.VAR or Token.LET or -1
      left - array or object literal containing NAME nodes for variables to assign
      right - expression to assign from
      Returns:
      expression that performs a series of assignments to the variables defined in left
    • destructuringAssignmentHelper

      Node destructuringAssignmentHelper(int variableType, Node left, Node right, String tempName)
    • destructuringArray

      boolean destructuringArray(ArrayLiteral array, int variableType, String tempName, Node parent, List<String> destructuringNames)
    • destructuringObject

      boolean destructuringObject(ObjectLiteral node, int variableType, String tempName, Node parent, List<String> destructuringNames)
    • createName

      protected Node createName(String name)
    • createName

      protected Node createName(int type, String name, Node child)
    • createNumber

      protected Node createNumber(double number)
    • createScopeNode

      protected Scope createScopeNode(int token, int lineno)
      Create a node that can be used to hold lexically scoped variable definitions (via let declarations).
      Parameters:
      token - the token of the node to create
      lineno - line number of source
      Returns:
      the created node
    • simpleAssignment

      protected Node simpleAssignment(Node left, Node right)
    • checkMutableReference

      protected void checkMutableReference(Node n)
    • removeParens

      protected AstNode removeParens(AstNode node)
    • markDestructuring

      void markDestructuring(AstNode node)
    • codeBug

      private RuntimeException codeBug() throws RuntimeException
      Throws:
      RuntimeException
    • setDefaultUseStrictDirective

      public void setDefaultUseStrictDirective(boolean useStrict)
    • inUseStrictDirective

      public boolean inUseStrictDirective()