pjs/js/js2/parser.h

844 строки
31 KiB
C
Исходник Обычный вид История

2000-01-11 00:22:43 +03:00
// -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
//
// The contents of this file are subject to the Netscape Public
// License Version 1.1 (the "License"); you may not use this file
// except in compliance with the License. You may obtain a copy of
// the License at http://www.mozilla.org/NPL/
//
// Software distributed under the License is distributed on an "AS
// IS" basis, WITHOUT WARRANTY OF ANY KIND, either express oqr
// implied. See the License for the specific language governing
// rights and limitations under the License.
//
// The Original Code is the JavaScript 2 Prototype.
//
// The Initial Developer of the Original Code is Netscape
// Communications Corporation. Portions created by Netscape are
// Copyright (C) 1998 Netscape Communications Corporation. All
// Rights Reserved.
#ifndef parser_h
#define parser_h
#include "utilities.h"
#include <vector>
2000-01-11 00:22:43 +03:00
namespace JavaScript {
2000-01-26 01:57:32 +03:00
class StringAtom;
class World;
2000-01-11 00:22:43 +03:00
//
// Reader
//
// A Reader reads Unicode characters from a source string.
// Calling get() yields all of the characters in sequence. One character may be read
// past the end of the source; that character appears as a null.
2000-01-11 00:22:43 +03:00
class Reader {
const char16 *begin; // Beginning of source text
const char16 *p; // Position in source text
const char16 *end; // End of source text; *end is a null character
2000-02-23 04:54:30 +03:00
public:
const String source; // Source text
const String sourceLocation; // Description of location from which the source text came
2000-02-23 04:54:30 +03:00
private:
const uint32 initialLineNum; // One-based number of current line
std::vector<const char16 *> linePositions; // Array of line starts recorded by beginLine()
2000-01-11 00:22:43 +03:00
String *recordString; // String, if any, into which recordChar() records characters; not owned by the Reader
2000-01-26 01:57:32 +03:00
const char16 *recordBase; // Position of last beginRecording() call
const char16 *recordPos; // Position of last recordChar() call; nil if a discrepancy occurred
2000-01-11 00:22:43 +03:00
public:
Reader(const String &source, const String &sourceLocation, uint32 initialLineNum = 1);
2000-01-11 00:22:43 +03:00
private:
Reader(const Reader&); // No copy constructor
void operator=(const Reader&); // No assignment operator
public:
char16 get() {ASSERT(p <= end); return *p++;}
char16 peek() {ASSERT(p <= end); return *p;}
void unget(uint32 n = 1) {ASSERT(p >= begin + n); p -= n;}
uint32 getPos() const {return static_cast<uint32>(p - begin);}
void setPos(uint32 pos) {ASSERT(pos <= getPos()); p = begin + pos;}
bool eof() const {ASSERT(p <= end); return p == end;}
bool peekEof(char16 ch) const {ASSERT(p <= end && *p == ch); return !ch && p == end;} // Faster version. ch is the result of a peek
bool pastEof() const {return p == end+1;}
bool getEof(char16 ch) const {ASSERT(p[-1] == ch); return !ch && p == end+1;} // Faster version. ch is the result of a get
void beginLine();
uint32 posToLineNum(uint32 pos) const;
uint32 getLine(uint32 lineNum, const char16 *&lineBegin, const char16 *&lineEnd) const;
2000-01-26 01:57:32 +03:00
void beginRecording(String &recordString);
void recordChar(char16 ch);
String &endRecording();
void error(Exception::Kind kind, const String &message, uint32 pos);
2000-01-11 00:22:43 +03:00
};
//
// Lexer
//
class Token {
static const char *const kindNames[];
2000-01-11 00:22:43 +03:00
public:
enum Kind { // Keep synchronized with kindNames table
// Special
end, // End of token stream
2000-01-11 00:22:43 +03:00
number, // Number
string, // String
unit, // Unit after number
regExp, // Regular expression
2000-01-11 00:22:43 +03:00
// Punctuators
openParenthesis, // (
closeParenthesis, // )
openBracket, // [
closeBracket, // ]
openBrace, // {
closeBrace, // }
comma, // ,
semicolon, // ;
dot, // .
doubleDot, // ..
tripleDot, // ...
arrow, // ->
colon, // :
doubleColon, // ::
pound, // #
at, // @
increment, // ++
decrement, // --
complement, // ~
logicalNot, // !
times, // *
divide, // /
modulo, // %
plus, // +
minus, // -
leftShift, // <<
rightShift, // >>
logicalRightShift, // >>>
logicalAnd, // &&
logicalXor, // ^^
logicalOr, // ||
bitwiseAnd, // & // These must be at constant offsets from logicalAnd ... logicalOr
bitwiseXor, // ^
bitwiseOr, // |
assignment, // =
timesEquals, // *= // These must be at constant offsets from times ... bitwiseOr
divideEquals, // /=
moduloEquals, // %=
plusEquals, // +=
minusEquals, // -=
leftShiftEquals, // <<=
rightShiftEquals, // >>=
logicalRightShiftEquals, // >>>=
logicalAndEquals, // &&=
logicalXorEquals, // ^^=
logicalOrEquals, // ||=
bitwiseAndEquals, // &=
bitwiseXorEquals, // ^=
bitwiseOrEquals, // |=
equal, // ==
notEqual, // !=
lessThan, // <
lessThanOrEqual, // <=
greaterThan, // > // >, >= must be at constant offsets from <, <=
greaterThanOrEqual, // >=
identical, // ===
notIdentical, // !==
question, // ?
2000-01-11 00:22:43 +03:00
// Reserved words
Abstract, // abstract
Break, // break
Case, // case
Catch, // catch
Class, // class
Const, // const
Continue, // continue
Debugger, // debugger
Default, // default
Delete, // delete
Do, // do
Else, // else
Enum, // enum
Eval, // eval
Export, // export
Extends, // extends
False, // false
Final, // final
Finally, // finally
For, // for
Function, // function
Goto, // goto
If, // if
Implements, // implements
Import, // import
In, // in
Instanceof, // instanceof
Native, // native
New, // new
Null, // null
Package, // package
Private, // private
Protected, // protected
Public, // public
Return, // return
Static, // static
Super, // super
Switch, // switch
Synchronized, // synchronized
This, // this
Throw, // throw
Throws, // throws
Transient, // transient
True, // true
Try, // try
Typeof, // typeof
Var, // var
Volatile, // volatile
While, // while
With, // with
// Non-reserved words
Box, // box
Constructor, // constructor
Field, // field
Get, // get
Language, // language
Local, // local
Method, // method
Override, // override
Set, // set
Version, // version
identifier, // Non-keyword identifier (may be same as a keyword if it contains an escape code)
KindsEnd, // End of token kinds
KeywordsBegin = Abstract, // Beginning of range of special identifier tokens
KeywordsEnd = identifier, // End of range of special identifier tokens
NonReservedBegin = Box, // Beginning of range of non-reserved words
NonReservedEnd = identifier,// End of range of non-reserved words
KindsWithCharsBegin = number,// Beginning of range of tokens for which the chars field (below) is valid
KindsWithCharsEnd = regExp+1// End of range of tokens for which the chars field (below) is valid
2000-01-11 00:22:43 +03:00
};
#define CASE_TOKEN_NONRESERVED \
Token::Box: \
case Token::Constructor: \
case Token::Field: \
case Token::Get: \
case Token::Language: \
case Token::Local: \
case Token::Method: \
case Token::Override: \
case Token::Set: \
case Token::Version: \
case Token::identifier
private:
#ifdef DEBUG
bool valid; // True if this token has been initialized
#endif
2000-01-11 00:22:43 +03:00
Kind kind; // The token's kind
bool lineBreak; // True if line break precedes this token
uint32 pos; // Source position of this token
const StringAtom *id; // The token's characters; non-nil for identifiers, keywords, and regular expressions only
String chars; // The token's characters; valid for strings, units, numbers, and regular expression flags only
2000-01-11 00:22:43 +03:00
float64 value; // The token's value (numbers only)
2000-01-26 01:57:32 +03:00
#ifdef DEBUG
Token(): valid(false) {}
#endif
2000-01-11 00:22:43 +03:00
public:
static void initKeywords(World &world);
static bool isIdentifierKind(Kind kind) {ASSERT(NonReservedEnd == identifier && KindsEnd == identifier+1); return kind >= NonReservedBegin;}
static bool isSpecialKind(Kind kind) {return kind <= regExp || kind == identifier;}
static const char *kindName(Kind kind) {ASSERT(uint(kind) < KindsEnd); return kindNames[kind];}
Kind getKind() const {ASSERT(valid); return kind;}
bool hasKind(Kind k) const {ASSERT(valid); return kind == k;}
bool getLineBreak() const {ASSERT(valid); return lineBreak;}
uint32 getPos() const {ASSERT(valid); return pos;}
const StringAtom &getIdentifier() const {ASSERT(valid && id); return *id;}
const String &getChars() const {ASSERT(valid && kind >= KindsWithCharsBegin && kind < KindsWithCharsEnd); return chars;}
float64 getValue() const {ASSERT(valid && kind == number); return value;}
friend Formatter &operator<<(Formatter &f, Kind k) {f << kindName(k); return f;}
void print(Formatter &f, bool debug = false) const;
friend class Lexer;
};
2000-01-11 00:22:43 +03:00
2000-01-26 01:57:32 +03:00
2000-01-11 00:22:43 +03:00
class Lexer {
enum {tokenLookahead = 2}; // Number of tokens that can be simultaneously live
#ifdef DEBUG
enum {tokenGuard = 10}; // Number of invalid tokens added to circular token buffer to catch references to old tokens
#else
enum {tokenGuard = 0}; // Number of invalid tokens added to circular token buffer to catch references to old tokens
#endif
enum {tokenBufferSize = tokenLookahead + tokenGuard}; // Token lookahead buffer size
2000-01-11 00:22:43 +03:00
Token tokens[tokenBufferSize]; // Circular buffer of recently read or lookahead tokens
Token *nextToken; // Address of next Token in the circular buffer to be returned by get()
int nTokensFwd; // Net number of Tokens on which unget() has been called; these Tokens are ahead of nextToken
#ifdef DEBUG
int nTokensBack; // Number of Tokens on which unget() can be called; these Tokens are beind nextToken
bool savedPreferRegExp[tokenBufferSize]; // Circular buffer of saved values of preferRegExp to get() calls
#endif
2000-01-26 01:57:32 +03:00
bool lexingUnit; // True if lexing a unit identifier immediately following a number
2000-01-11 00:22:43 +03:00
public:
World &world;
Reader reader;
Lexer(World &world, const String &source, const String &sourceLocation, uint32 initialLineNum = 1);
2000-01-11 00:22:43 +03:00
const Token &get(bool preferRegExp);
const Token *eat(bool preferRegExp, Token::Kind kind);
2000-01-11 00:22:43 +03:00
const Token &peek(bool preferRegExp);
void redesignate(bool preferRegExp);
2000-01-11 00:22:43 +03:00
void unget();
uint32 getPos() const;
2000-01-11 00:22:43 +03:00
private:
2000-01-26 01:57:32 +03:00
void syntaxError(const char *message, uint backUp = 1);
char16 getChar();
char16 internalGetChar(char16 ch);
char16 peekChar();
char16 internalPeekChar(char16 ch);
2000-01-26 01:57:32 +03:00
bool testChar(char16 ch);
char16 lexEscape(bool unicodeOnly);
bool lexIdentifier(String &s, bool allowLeadingDigit);
bool lexNumeral();
void lexString(String &s, char16 separator);
2000-01-26 01:57:32 +03:00
void lexRegExp();
2000-01-11 00:22:43 +03:00
void lexToken(bool preferRegExp);
};
2000-02-23 04:54:30 +03:00
#ifndef DEBUG
inline void Lexer::redesignate(bool) {}
#endif
// Return the position of the first character of the next token, which must have been peeked.
inline uint32 Lexer::getPos() const
{
ASSERT(nTokensFwd);
return nextToken->getPos();
}
2000-02-23 04:54:30 +03:00
2000-03-01 10:00:31 +03:00
//
// Language Selectors
//
2000-02-23 04:54:30 +03:00
2000-03-01 10:00:31 +03:00
class Language {
enum {
minorVersion = 0, // Single BCD digit representing the minor JavaScript version
minorVersionBits = 4,
majorVersion = 4, // Single BCD digit representing the major JavaScript version
majorVersionBits = 4,
strictSemantics = 8, // True when strict semantics are in use
strictSyntax = 9, // True when strict syntax is in use
unknown = 31 // True when language is unknown
};
uint32 flags; // Bitmap of flags at locations as above
};
2000-02-23 04:54:30 +03:00
2000-03-01 10:00:31 +03:00
//
// Parser
//
2000-02-23 04:54:30 +03:00
2000-03-01 10:00:31 +03:00
// The structures below are generally allocated inside an arena. The structures' destructors
// may never be called, so these structures should not hold onto any data that needs to be
// destroyed explicitly. Strings are allocated via newArenaString.
2000-02-23 04:54:30 +03:00
2000-03-01 10:00:31 +03:00
struct ParseNode: ArenaObject {
uint32 pos; // Position of this statement or expression
2000-03-01 10:00:31 +03:00
explicit ParseNode(uint32 pos): pos(pos) {}
2000-03-01 10:00:31 +03:00
};
2000-02-23 04:54:30 +03:00
// A helper template for creating linked lists of ParseNode subtypes. N should be derived
// from a ParseNode and should have an instance variable called <next> of type N* and that
// is initialized to nil when an N instance is created.
template<class N>
class NodeQueue {
public:
N *first; // Head of queue
private:
N **last; // Next link of last element of queue
public:
NodeQueue(): first(0), last(&first) {}
private:
NodeQueue(const NodeQueue&); // No copy constructor
void operator=(const NodeQueue&); // No assignment operator
public:
void operator+=(N *elt) {ASSERT(elt && !elt->next); *last = elt; last = &elt->next;}
};
2000-03-01 10:00:31 +03:00
struct FunctionName {
enum Prefix {
normal, // No prefix
2000-03-01 10:00:31 +03:00
Get, // get
Set, // set
New // new
};
Prefix prefix; // The name's prefix, if any
const StringAtom *name; // The name; nil if omitted
2000-03-01 10:00:31 +03:00
FunctionName(): prefix(normal), name(0) {}
2000-03-01 10:00:31 +03:00
};
struct ExprNode;
struct StmtNode;
struct VariableBinding: ParseNode {
VariableBinding *next; // Next binding in a linked list of variable or parameter bindings
const StringAtom *name; // The variable's name; nil if omitted, which currently can only happen for ... parameters
2000-03-01 10:00:31 +03:00
ExprNode *type; // Type expression or nil if not provided
ExprNode *initializer; // Initial value expression or nil if not provided
VariableBinding(uint32 pos): ParseNode(pos), next(0), name(0), type(0), initializer(0) {}
2000-03-01 10:00:31 +03:00
};
2000-02-23 04:54:30 +03:00
2000-03-01 10:00:31 +03:00
struct FunctionDefinition: FunctionName {
VariableBinding *parameters; // Linked list of all parameters, including optional and rest parameters, if any
VariableBinding *optParameters; // Pointer to first non-required parameter inside parameters list; nil if none
VariableBinding *restParameter; // Pointer to rest parameter inside parameters list; nil if none
ExprNode *resultType; // Result type expression or nil if not provided
StmtNode *body; // Body; nil if none
};
2000-02-23 04:54:30 +03:00
2000-03-01 10:00:31 +03:00
struct ExprNode: ParseNode {
enum Kind { // Actual class Operands
identifier, // IdentifierExprNode <name>
number, // NumberExprNode <value>
string, // StringExprNode <str>
regExp, // RegExpExprNode /<regExp>/<flags>
2000-03-01 10:00:31 +03:00
Null, // ExprNode null
True, // ExprNode true
False, // ExprNode false
This, // ExprNode this
Super, // ExprNode super
parentheses, // UnaryExprNode (<op>)
numUnit, // NumUnitExprNode <num> "<str>" or <num><str>
exprUnit, // ExprUnitExprNode (<op>) "<str>"
qualifiedIdentifier, // OpIdentifierExprNode <op> :: <name>
2000-03-01 10:00:31 +03:00
objectLiteral, // PairListExprNode {<field>:<value>, <field>:<value>, ..., <field>:<value>}
arrayLiteral, // PairListExprNode [<value>, <value>, ..., <value>]
functionLiteral, // FunctionExprNode function <function>
2000-03-01 10:00:31 +03:00
call, // InvokeExprNode <op>(<field>:<value>, <field>:<value>, ..., <field>:<value>)
2000-03-01 10:00:31 +03:00
New, // InvokeExprNode new <op>(<field>:<value>, <field>:<value>, ..., <field>:<value>)
index, // InvokeExprNode <op>[<field>:<value>, <field>:<value>, ..., <field>:<value>]
2000-03-01 10:00:31 +03:00
dot, // BinaryExprNode <op1> . <op2> // <op2> must be identifier or qualifiedIdentifier
dotParen, // BinaryExprNode <op1> .( <op2> )
at, // BinaryExprNode <op1> @ <op2> or <op1> @( <op2> )
2000-03-01 10:00:31 +03:00
Delete, // UnaryExprNode delete <op>
Typeof, // UnaryExprNode typeof <op>
Eval, // UnaryExprNode eval <op>
preIncrement, // UnaryExprNode ++ <op>
preDecrement, // UnaryExprNode -- <op>
postIncrement, // UnaryExprNode <op> ++
postDecrement, // UnaryExprNode <op> --
plus, // UnaryExprNode + <op>
minus, // UnaryExprNode - <op>
complement, // UnaryExprNode ~ <op>
logicalNot, // UnaryExprNode ! <op>
add, // BinaryExprNode <op1> + <op2>
subtract, // BinaryExprNode <op1> - <op2>
multiply, // BinaryExprNode <op1> * <op2>
divide, // BinaryExprNode <op1> / <op2>
modulo, // BinaryExprNode <op1> % <op2>
leftShift, // BinaryExprNode <op1> << <op2>
rightShift, // BinaryExprNode <op1> >> <op2>
logicalRightShift, // BinaryExprNode <op1> >>> <op2>
bitwiseAnd, // BinaryExprNode <op1> & <op2>
bitwiseXor, // BinaryExprNode <op1> ^ <op2>
bitwiseOr, // BinaryExprNode <op1> | <op2>
logicalAnd, // BinaryExprNode <op1> && <op2>
logicalXor, // BinaryExprNode <op1> ^^ <op2>
logicalOr, // BinaryExprNode <op1> || <op2>
equal, // BinaryExprNode <op1> == <op2>
notEqual, // BinaryExprNode <op1> != <op2>
lessThan, // BinaryExprNode <op1> < <op2>
lessThanOrEqual, // BinaryExprNode <op1> <= <op2>
greaterThan, // BinaryExprNode <op1> > <op2>
greaterThanOrEqual, // BinaryExprNode <op1> >= <op2>
identical, // BinaryExprNode <op1> === <op2>
notIdentical, // BinaryExprNode <op1> !== <op2>
2000-03-01 10:00:31 +03:00
In, // BinaryExprNode <op1> in <op2>
Instanceof, // BinaryExprNode <op1> instanceof <op2>
assignment, // BinaryExprNode <op1> = <op2>
addEquals, // BinaryExprNode <op1> += <op2>
subtractEquals, // BinaryExprNode <op1> -= <op2>
multiplyEquals, // BinaryExprNode <op1> *= <op2>
divideEquals, // BinaryExprNode <op1> /= <op2>
moduloEquals, // BinaryExprNode <op1> %= <op2>
leftShiftEquals, // BinaryExprNode <op1> <<= <op2>
rightShiftEquals, // BinaryExprNode <op1> >>= <op2>
logicalRightShiftEquals, // BinaryExprNode <op1> >>>= <op2>
bitwiseAndEquals, // BinaryExprNode <op1> &= <op2>
bitwiseXorEquals, // BinaryExprNode <op1> ^= <op2>
bitwiseOrEquals, // BinaryExprNode <op1> |= <op2>
logicalAndEquals, // BinaryExprNode <op1> &&= <op2>
logicalXorEquals, // BinaryExprNode <op1> ^^= <op2>
logicalOrEquals, // BinaryExprNode <op1> ||= <op2>
conditional, // TernaryExprNode <op1> ? <op2> : <op3>
comma // BinaryExprNode <op1> , <op2> // Comma expressions only
2000-03-01 10:00:31 +03:00
};
private:
2000-03-01 10:00:31 +03:00
Kind kind; // The node's kind
public:
ExprNode(uint32 pos, Kind kind): ParseNode(pos), kind(kind) {}
2000-02-23 04:54:30 +03:00
Kind getKind() const {return kind;}
bool hasKind(Kind k) const {return kind == k;}
static bool isFieldKind(Kind kind) {return kind == identifier || kind == number || kind == string || kind == qualifiedIdentifier;}
2000-03-01 10:00:31 +03:00
};
struct IdentifierExprNode: ExprNode {
const StringAtom &name; // The identifier
2000-03-01 10:00:31 +03:00
IdentifierExprNode(uint32 pos, Kind kind, const StringAtom &name):
ExprNode(pos, kind), name(name) {}
2000-03-01 10:00:31 +03:00
};
struct OpIdentifierExprNode: IdentifierExprNode {
ExprNode *op; // The namespace expression or indexed expression; non-nil only
2000-03-01 10:00:31 +03:00
OpIdentifierExprNode(uint32 pos, Kind kind, const StringAtom &name, ExprNode *op):
IdentifierExprNode(pos, kind, name), op(op) {ASSERT(op);}
2000-03-01 10:00:31 +03:00
};
struct NumberExprNode: ExprNode {
float64 value; // The number's value
NumberExprNode(uint32 pos, float64 value): ExprNode(pos, number), value(value) {}
2000-03-01 10:00:31 +03:00
};
struct StringExprNode: ExprNode {
String &str; // The string
StringExprNode(uint32 pos, Kind kind, String &str): ExprNode(pos, kind), str(str) {}
2000-03-01 10:00:31 +03:00
};
struct RegExpExprNode: ExprNode {
const StringAtom &regExp; // The regular expression's contents
String &flags; // The regular expression's flags
RegExpExprNode(uint32 pos, Kind kind, const StringAtom &regExp, String &flags):
2000-03-01 10:00:31 +03:00
ExprNode(pos, kind), regExp(regExp), flags(flags) {}
};
struct NumUnitExprNode: StringExprNode { // str is the unit string
String &numStr; // The number's source string
float64 num; // The number's value
NumUnitExprNode(uint32 pos, Kind kind, String &numStr, float64 num, String &unitStr):
2000-03-01 10:00:31 +03:00
StringExprNode(pos, kind, unitStr), numStr(numStr), num(num) {}
};
struct ExprUnitExprNode: StringExprNode { // str is the unit string
ExprNode *op; // The expression to which the unit is applied; non-nil only
2000-03-01 10:00:31 +03:00
ExprUnitExprNode(uint32 pos, Kind kind, ExprNode *op, String &unitStr):
StringExprNode(pos, kind, unitStr), op(op) {ASSERT(op);}
2000-03-01 10:00:31 +03:00
};
struct FunctionExprNode: ExprNode {
FunctionDefinition function; // Function definition
FunctionExprNode(uint32 pos, Kind kind): ExprNode(pos, kind) {}
2000-03-01 10:00:31 +03:00
};
struct ExprList: ArenaObject {
ExprList *next; // Next expression in linked list
ExprNode *expr; // Attribute expression; non-nil only
2000-03-01 10:00:31 +03:00
explicit ExprList(ExprNode *expr): next(0), expr(expr) {ASSERT(expr);}
2000-03-01 10:00:31 +03:00
};
struct ExprPairList: ArenaObject {
ExprPairList *next; // Next pair in linked list
ExprNode *field; // Field expression or nil if not provided
ExprNode *value; // Value expression or nil if not provided
explicit ExprPairList(ExprNode *field = 0, ExprNode *value = 0): next(0), field(field), value(value) {}
};
struct PairListExprNode: ExprNode {
ExprPairList *pairs; // Linked list of pairs
PairListExprNode(uint32 pos, Kind kind, ExprPairList *pairs): ExprNode(pos, kind), pairs(pairs) {}
2000-03-01 10:00:31 +03:00
};
struct InvokeExprNode: PairListExprNode {
ExprNode *op; // The called function, called constructor, or indexed object; non-nil only
2000-03-01 10:00:31 +03:00
InvokeExprNode(uint32 pos, Kind kind, ExprNode *op, ExprPairList *pairs):
PairListExprNode(pos, kind, pairs), op(op) {ASSERT(op);}
2000-03-01 10:00:31 +03:00
};
struct UnaryExprNode: ExprNode {
ExprNode *op; // The unary operator's operand; non-nil only
2000-03-01 10:00:31 +03:00
UnaryExprNode(uint32 pos, Kind kind, ExprNode *op): ExprNode(pos, kind), op(op) {ASSERT(op);}
2000-03-01 10:00:31 +03:00
};
struct BinaryExprNode: ExprNode {
ExprNode *op1; // The binary operator's first operand; non-nil only
ExprNode *op2; // The binary operator's second operand; non-nil only
2000-03-01 10:00:31 +03:00
BinaryExprNode(uint32 pos, Kind kind, ExprNode *op1, ExprNode *op2):
ExprNode(pos, kind), op1(op1), op2(op2) {ASSERT(op1 && op2);}
2000-03-01 10:00:31 +03:00
};
struct TernaryExprNode: ExprNode {
ExprNode *op1; // The ternary operator's first operand; non-nil only
ExprNode *op2; // The ternary operator's second operand; non-nil only
ExprNode *op3; // The ternary operator's third operand; non-nil only
2000-03-01 10:00:31 +03:00
TernaryExprNode(uint32 pos, Kind kind, ExprNode *op1, ExprNode *op2, ExprNode *op3):
ExprNode(pos, kind), op1(op1), op2(op2), op3(op3) {ASSERT(op1 && op2 && op3);}
2000-03-01 10:00:31 +03:00
};
struct StmtNode: ParseNode {
enum Kind { // Actual class Operands
empty, // StmtNode ;
expression, // ExprStmtNode <expr> ;
block, // BlockStmtNode <attributes> { <statements> }
label, // LabelStmtNode <label> : <stmt>
2000-03-01 10:00:31 +03:00
If, // UnaryStmtNode if ( <expr> ) <stmt>
IfElse, // BinaryStmtNode if ( <expr> ) <stmt> else <stmt2>
Switch, // SwitchStmtNode switch ( <expr> ) <statements>
While, // UnaryStmtNode while ( <expr> ) <stmt>
DoWhile, // UnaryStmtNode do <stmt> while ( <expr> )
With, // UnaryStmtNode with ( <expr> ) <stmt>
For, // ForStmtNode for ( <initializer> ; <expr2> ; <expr3> ) <stmt>
ForExprIn, // ForExprInStmtNode for ( <varExpr> in <container> ) <stmt>
ForConstIn, // ForVarInStmtNode for ( const <binding> in <container> ) <stmt>
ForVarIn, // ForVarInStmtNode for ( var <binding> in <container> ) <stmt>
Case, // ExprStmtNode case <expr> : or default : // Only occurs directly inside a Switch
Break, // GoStmtNode break ; or break <label> ;
Continue, // GoStmtNode continue ; or continue <label> ;
Return, // ExprStmtNode return ; or return <expr> ;
Throw, // ExprStmtNode throw <expr> ;
Try, // TryStmtNode try <stmt> <catches> <finally>
Const, // VariableStmtNode <attributes> const <bindings> ;
Var, // VariableStmtNode <attributes> var <bindings> ;
Function, // FunctionStmtNode <attributes> function <function>
Class, // ClassStmtNode <attributes> class <name> extends <superclasses> <body>
2000-03-01 10:00:31 +03:00
Language // LanguageStmtNode language <language> ;
2000-02-23 04:54:30 +03:00
};
2000-03-01 10:00:31 +03:00
Kind kind; // The node's kind
StmtNode *next; // Next statement in a linked list of statements in this block
StmtNode(uint32 pos, Kind kind): ParseNode(pos), kind(kind), next(0) {}
2000-03-01 10:00:31 +03:00
};
struct ExprStmtNode: StmtNode {
ExprNode *expr; // The expression statement's expression. May be nil for default: or return-with-no-expression statements.
2000-03-01 10:00:31 +03:00
ExprStmtNode(uint32 pos, Kind kind, ExprNode *expr): StmtNode(pos, kind), expr(expr) {}
2000-03-01 10:00:31 +03:00
};
struct AttributeStmtNode: StmtNode {
ExprList *attributes; // Linked list of block or definition's attributes
AttributeStmtNode(uint32 pos, Kind kind, ExprList *attributes): StmtNode(pos, kind), attributes(attributes) {}
2000-03-01 10:00:31 +03:00
};
struct BlockStmtNode: AttributeStmtNode {
StmtNode *statements; // Linked list of block's statements
BlockStmtNode(uint32 pos, Kind kind, ExprList *attributes): AttributeStmtNode(pos, kind, attributes) {}
2000-03-01 10:00:31 +03:00
};
struct LabelStmtNode: StmtNode {
const StringAtom &label; // The label
StmtNode *stmt; // Labeled statement; non-nil only
2000-03-01 10:00:31 +03:00
LabelStmtNode(uint32 pos, Kind kind, const StringAtom &label, StmtNode *stmt):
StmtNode(pos, kind), label(label), stmt(stmt) {ASSERT(stmt);}
2000-03-01 10:00:31 +03:00
};
struct UnaryStmtNode: ExprStmtNode {
StmtNode *stmt; // First substatement; non-nil only
2000-03-01 10:00:31 +03:00
UnaryStmtNode(uint32 pos, Kind kind, ExprNode *expr, StmtNode *stmt):
ExprStmtNode(pos, kind, expr), stmt(stmt) {ASSERT(stmt);}
2000-02-23 04:54:30 +03:00
};
2000-03-01 10:00:31 +03:00
struct BinaryStmtNode: UnaryStmtNode {
StmtNode *stmt2; // Second substatement; non-nil only
2000-03-01 10:00:31 +03:00
BinaryStmtNode(uint32 pos, Kind kind, ExprNode *expr, StmtNode *stmt1, StmtNode *stmt2):
UnaryStmtNode(pos, kind, expr, stmt1), stmt2(stmt2) {ASSERT(stmt2);}
2000-03-01 10:00:31 +03:00
};
struct ForStmtNode: StmtNode {
StmtNode *initializer; // First item in parentheses; either nil (if not provided), an expression, or a Var, or a Const.
2000-03-01 10:00:31 +03:00
ExprNode *expr2; // Second item in parentheses; nil if not provided
ExprNode *expr3; // Third item in parentheses; nil if not provided
StmtNode *stmt; // Substatement; non-nil only
2000-03-01 10:00:31 +03:00
ForStmtNode(uint32 pos, Kind kind, StmtNode *stmt):
StmtNode(pos, kind), stmt(stmt) {ASSERT(stmt);}
2000-03-01 10:00:31 +03:00
};
struct ForInStmtNode: StmtNode {
ExprNode *container; // Subexpression after 'in'; non-nil only
StmtNode *stmt; // Substatement; non-nil only
2000-03-01 10:00:31 +03:00
ForInStmtNode(uint32 pos, Kind kind, ExprNode *container, StmtNode *stmt):
StmtNode(pos, kind), container(container), stmt(stmt) {ASSERT(container && stmt);}
2000-03-01 10:00:31 +03:00
};
struct ForExprInStmtNode: ForInStmtNode {
ExprNode *varExpr; // Subexpression before 'in'; non-nil only
2000-03-01 10:00:31 +03:00
ForExprInStmtNode(uint32 pos, Kind kind, ExprNode *container, StmtNode *stmt, ExprNode *varExpr):
ForInStmtNode(pos, kind, container, stmt), varExpr(varExpr) {ASSERT(varExpr);}
2000-03-01 10:00:31 +03:00
};
struct ForVarInStmtNode: ForInStmtNode {
VariableBinding *binding; // Var or const binding before 'in'; non-nil only
2000-03-01 10:00:31 +03:00
ForVarInStmtNode(uint32 pos, Kind kind, ExprNode *container, StmtNode *stmt, VariableBinding *binding):
ForInStmtNode(pos, kind, container, stmt), binding(binding) {ASSERT(binding);}
2000-03-01 10:00:31 +03:00
};
struct SwitchStmtNode: ExprStmtNode {
StmtNode *statements; // Linked list of switch block's statements, which may include Case and Default statements
SwitchStmtNode(uint32 pos, Kind kind, ExprNode *expr):
2000-03-01 10:00:31 +03:00
ExprStmtNode(pos, kind, expr) {}
};
struct GoStmtNode: StmtNode {
const StringAtom *label; // The label; nil if none
GoStmtNode(uint32 pos, Kind kind, const StringAtom *label): StmtNode(pos, kind), label(label) {}
2000-03-01 10:00:31 +03:00
};
struct CatchClause: ParseNode {
CatchClause *next; // Next catch clause in a linked list of catch clauses
const StringAtom &name; // The name of the variable that will hold the exception
2000-03-01 10:00:31 +03:00
ExprNode *type; // Type expression or nil if not provided
StmtNode *stmt; // The catch clause's body; non-nil only
2000-03-01 10:00:31 +03:00
CatchClause(uint32 pos, const StringAtom &name, ExprNode *type, StmtNode *stmt):
ParseNode(pos), next(0), name(name), type(type), stmt(stmt) {ASSERT(stmt);}
2000-03-01 10:00:31 +03:00
};
struct TryStmtNode: StmtNode {
StmtNode *stmt; // Substatement being tried; usually a block; non-nil only
2000-03-01 10:00:31 +03:00
CatchClause *catches; // Linked list of catch blocks; may be nil
StmtNode *finally; // Finally block or nil if none
TryStmtNode(uint32 pos, Kind kind, StmtNode *stmt, CatchClause *catches, StmtNode *finally):
StmtNode(pos, kind), stmt(stmt), catches(catches), finally(finally) {ASSERT(stmt);}
2000-03-01 10:00:31 +03:00
};
struct VariableStmtNode: AttributeStmtNode {
VariableBinding *bindings; // Linked list of variable bindings
VariableStmtNode(uint32 pos, Kind kind, ExprList *attributes): AttributeStmtNode(pos, kind, attributes) {}
2000-03-01 10:00:31 +03:00
};
struct FunctionStmtNode: AttributeStmtNode {
FunctionDefinition function; // Function definition
FunctionStmtNode(uint32 pos, Kind kind, ExprList *attributes): AttributeStmtNode(pos, kind, attributes) {}
2000-03-01 10:00:31 +03:00
};
struct ClassStmtNode: AttributeStmtNode {
const StringAtom &name; // The class's name
2000-03-01 10:00:31 +03:00
ExprList *superclasses; // Linked list of superclass expressions
StmtNode *body; // The class's body; non-nil only
2000-03-01 10:00:31 +03:00
ClassStmtNode(uint32 pos, Kind kind, ExprList *attributes, const StringAtom &name, ExprList *superclasses,
StmtNode *body):
AttributeStmtNode(pos, kind, attributes), name(name), superclasses(superclasses), body(body) {ASSERT(body);}
2000-03-01 10:00:31 +03:00
};
struct LanguageStmtNode: StmtNode {
JavaScript::Language language; // The selected language
LanguageStmtNode(uint32 pos, Kind kind, JavaScript::Language language):
2000-03-01 10:00:31 +03:00
StmtNode(pos, kind), language(language) {}
};
2000-02-23 04:54:30 +03:00
class Parser {
public:
Lexer lexer;
Arena &arena;
Parser(World &world, Arena &arena, const String &source, const String &sourceLocation, uint32 initialLineNum = 1);
private:
Reader &getReader() {return lexer.reader;}
World &getWorld() {return lexer.world;}
void syntaxError(const char *message, uint backUp = 1);
void syntaxError(const String &message, uint backUp = 1);
const Token &require(bool preferRegExp, Token::Kind kind);
String &copyTokenChars(const Token &t);
ExprNode *parseIdentifierQualifiers(ExprNode *e, bool &foundQualifiers);
ExprNode *parseParenthesesAndIdentifierQualifiers(const Token &tParen, bool &foundQualifiers);
IdentifierExprNode *parseQualifiedIdentifier(const Token &t);
PairListExprNode *parseArrayLiteral(const Token &initialToken);
PairListExprNode *parseObjectLiteral(const Token &initialToken);
ExprNode *parsePrimaryExpression();
BinaryExprNode *parseMember(ExprNode *target, const Token &tOperator, ExprNode::Kind kind, ExprNode::Kind parenKind);
InvokeExprNode *parseInvoke(ExprNode *target, uint32 pos, Token::Kind closingTokenKind, ExprNode::Kind invokeKind);
public:
ExprNode *parsePostfixExpression(bool newExpression = false);
ExprNode *parseNonAssignmentExpression(bool noIn);
ExprNode *parseAssignmentExpression(bool noIn);
ExprNode *parseExpression(bool noIn);
};
2000-01-11 00:22:43 +03:00
}
#endif