Class: Parser

lib/core/expression/parser~Parser(source)

A recursive-descent parser over a token stream.

Constructor

new Parser(source)

Parameters:
Name Type Description
source string

The expression source.

Source:

Members

index :number

Type:
  • number
Source:

source :string

Type:
  • string
Source:

tokens :Array:.<object:>

Type:
  • Array:.<object:>
Source:

Methods

atName(value) → {boolean}

Whether the current token is a given keyword.

Parameters:
Name Type Description
value string

The keyword.

Source:
Returns:

True when it matches.

Type
boolean

atPunct(value) → {boolean}

Whether the current token is a given punctuator.

Parameters:
Name Type Description
value string

The punctuator.

Source:
Returns:

True when it matches.

Type
boolean

eat(value) → {boolean}

Consumes a punctuator if present.

Parameters:
Name Type Description
value string

The punctuator.

Source:
Returns:

Whether it was consumed.

Type
boolean

expect(value)

Consumes a punctuator, or fails.

Parameters:
Name Type Description
value string

The punctuator.

Source:
Throws:

When the token does not match.

Type
ExpressionParseError

expectEnd()

Asserts the token stream is exhausted.

Source:
Throws:

When tokens remain.

Type
ExpressionParseError

next() → {object}

Advances past the current token.

Source:
Returns:

The token consumed.

Type
object

parseArguments() → {Array:.<object:>}

Parses a parenthesised argument list.

Source:
Returns:

The argument nodes.

Type
Array:.<object:>

parseArrowBody() → {object}

Parses an arrow function's body.

A braced body is supported only when it is a single return; anything more is a statement list, which belongs to the statement evaluator rather than here.

Source:
Returns:

The body expression.

Type
object

parseAssignment() → {object}

Parses an assignment, arrow function or conditional.

Source:
Returns:

The AST node.

Type
object

parseBinary(minPrecedence) → {object}

Parses a binary expression using precedence climbing.

Parameters:
Name Type Description
minPrecedence number

The lowest precedence to accept.

Source:
Returns:

The AST node.

Type
object

parseCallMember() → {object}

Parses member access and calls, left to right.

Source:
Returns:

The AST node.

Type
object

parseConditional() → {object}

Parses a conditional expression.

Source:
Returns:

The AST node.

Type
object

parseExpressionStatement() → {object}

Parses a full expression, including the comma operator.

Source:
Returns:

The AST node.

Type
object

parseNewCallee() → {object}

Parses the constructor of a new expression: member access only, no calls.

Source:
Returns:

The AST node.

Type
object

parseObjectKey() → {Object}

Parses an object literal key.

Source:
Returns:

The key description.

Type
Object

parseParameterList() → {Array:.<string:>}

Parses a parenthesised parameter name list.

Source:
Returns:

The parameter names.

Type
Array:.<string:>

parsePostfix() → {object}

Parses a postfix update.

Source:
Returns:

The AST node.

Type
object

parsePrimary() → {object}

Parses a primary expression.

Source:
Returns:

The AST node.

Type
object

parseTemplateParts(raw) → {object}

Splits a template literal into its static and interpolated parts.

Parameters:
Name Type Description
raw string

The text between the backticks.

Source:
Returns:

A TemplateLiteral node.

Type
object

parseUnary() → {object}

Parses a prefix expression.

Source:
Returns:

The AST node.

Type
object

peek() → {object}

The token at the cursor.

Source:
Returns:

The current token.

Type
object

tryParseArrow() → {object|null}

Attempts to parse an arrow function at the cursor.

Speculative because (a, b) is ambiguous until => is or is not found. The cursor is restored when the guess is wrong, which is cheap: the token stream is already materialised.

Source:
Returns:

The arrow node, or null when this is not one.

Type
object | null