Skip to content

Commit

Permalink
fix(parser): fixed negative bitmask values
Browse files Browse the repository at this point in the history
  • Loading branch information
KFlash committed Aug 6, 2019
1 parent a184f67 commit 972a6f0
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 48 deletions.
17 changes: 10 additions & 7 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2148,7 +2148,8 @@ export function parseForStatement(
let update: ESTree.Expression | null = null;
let destructible: AssignmentKind | DestructuringKind = 0;
let init = null;
let isVarDecl: number = parser.token & Token.VarDecl;
let isVarDecl =
parser.token === Token.VarKeyword || parser.token === Token.LetKeyword || parser.token === Token.ConstKeyword;
let right;

const { token, tokenPos, linePos, colPos } = parser;
Expand Down Expand Up @@ -2177,7 +2178,7 @@ export function parseForStatement(
} else if (context & Context.Strict) {
report(parser, Errors.DisallowedLetInStrict);
} else {
isVarDecl = 0;
isVarDecl = false;
parser.assignable = AssignmentKind.Assignable;
init = parseMemberOrUpdateExpression(parser, context, init, 0, 0, 0, tokenPos, linePos, colPos);

Expand Down Expand Up @@ -3189,15 +3190,14 @@ export function parseAssignmentExpression(
column: number,
left: ESTree.ArgumentExpression | ESTree.Expression
): ESTree.ArgumentExpression | ESTree.Expression {
/** AssignmentExpression
*
* https://tc39.github.io/ecma262/#prod-AssignmentExpression
*
/**
* AssignmentExpression ::
* ConditionalExpression
* ArrowFunction
* AsyncArrowFunction
* YieldExpression
* LeftHandSideExpression AssignmentOperator AssignmentExpression
*
*/

const { token } = parser;
Expand Down Expand Up @@ -3854,7 +3854,10 @@ export function parseOptionalExpression(
context: Context,
expr: ESTree.Expression
): ESTree.OptionalChain {
// OptionalExpression[?Yield, ?Await]
// OptionalExpression ::
// MemberExpression
// CallExpression
// OptionalExpression[
const { tokenPos, linePos, colPos } = parser;
return finishNode(parser, context, tokenPos, linePos, colPos, {
type: 'OptionalExpression',
Expand Down
85 changes: 44 additions & 41 deletions src/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,28 @@ export const enum Token {
Precedence = 15 << PrecStart, // 8-11

/* Attribute names */
Keyword = 1 << 12,
Contextual = 1 << 13 | Keyword,
Reserved = 1 << 14 | Keyword,
FutureReserved = 1 << 15 | Keyword,

IsExpressionStart = 1 << 16,
IsIdentifier = 1 << 17 | Contextual,
IsInOrOf = 1 << 18, // 'in' or 'of'
IsLogical = 1 << 19,
IsAutoSemicolon = 1 << 20,
IsPatternStart = 1 << 21, // Start of pattern, '[' or '{'
IsAssignOp = 1 << 22,
IsBinaryOp = 1 << 23 | IsExpressionStart,
IsUnaryOp = 1 << 24 | IsExpressionStart,
IsUpdateOp = 1 << 25 | IsExpressionStart,
Keyword = 1 << 12,
Contextual = 1 << 13 | Keyword,
Reserved = 1 << 14 | Keyword,
FutureReserved = 1 << 15 | Keyword,

IsExpressionStart = 1 << 16,
IsIdentifier = 1 << 17 | Contextual,
IsInOrOf = 1 << 18, // 'in' or 'of'
IsLogical = 1 << 19,
IsAutoSemicolon = 1 << 20,
IsPatternStart = 1 << 21, // Start of pattern, '[' or '{'
IsAssignOp = 1 << 22,
IsBinaryOp = 1 << 23 | IsExpressionStart,
IsUnaryOp = 1 << 24 | IsExpressionStart,
IsUpdateOp = 1 << 25 | IsExpressionStart,
IsMemberOrCallExpression = 1 << 26,
IsStringOrNumber = 1 << 27,
VarDecl = 1 << 28,
IsEvalOrArguments = 1 << 29 | IsExpressionStart | IsIdentifier,
IsClassField = 1 << 30,
IsCoalesc = 1 << 31,
IsStringOrNumber = 1 << 27,
IsCoalesc = 1 << 28,
IsEvalOrArguments = 1 << 29 | IsExpressionStart | IsIdentifier,
IsClassField = 1 << 30,

// Note: 1 << 31... turns to negative

/* Node types */
EOF = 0 | IsAutoSemicolon,
Expand Down Expand Up @@ -114,9 +115,9 @@ export const enum Token {
BitwiseXor = 70 | IsBinaryOp | 5 << PrecStart, // ^

/* Variable declaration kinds */
VarKeyword = 71 | IsExpressionStart | Reserved | VarDecl,
LetKeyword = 72 | IsExpressionStart | FutureReserved | VarDecl | IsIdentifier,
ConstKeyword = 73 | IsExpressionStart | Reserved | VarDecl,
VarKeyword = 71 | IsExpressionStart | Reserved,
LetKeyword = 72 | IsExpressionStart | FutureReserved | IsIdentifier,
ConstKeyword = 73 | IsExpressionStart | Reserved,

/* Other reserved words */
BreakKeyword = 74 | Reserved,
Expand Down Expand Up @@ -169,27 +170,27 @@ export const enum Token {
Eval = 116 | IsEvalOrArguments,
Arguments = 117 | IsEvalOrArguments,

EscapedReserved = 118 | IsIdentifier,
EscapedReserved = 118 | IsIdentifier,
EscapedFutureReserved = 119 | IsIdentifier,
ReservedIfStrict = 120 | IsIdentifier,

PrivateName = 121,
BigIntLiteral = 122,
WhiteSpace = 124,
Illegal = 129,
CarriageReturn = 130,
PrivateField = 131,
Template = 132,
Decorator = 133,
Target = 134 | IsIdentifier,
LineFeed = 135,
EscapedIdentifier = 136,
JSXText = 137,

// Stage #3 proposals
Coalesce = 123 | IsBinaryOp | IsCoalesc | 1 << PrecStart, // ??,
QuestionMarkPeriod = 125 | IsMemberOrCallExpression, // ?.,

PrivateName = 121,
BigIntLiteral = 122,
Coalesce = 123 | IsBinaryOp | IsCoalesc | 1 << PrecStart, // ??,
QuestionMarkPeriod = 124 | IsMemberOrCallExpression, // ?.,

// Others
WhiteSpace = 125,
Illegal = 126,
CarriageReturn = 127,
PrivateField = 128,
Template = 129,
Decorator = 130,
Target = 131 | IsIdentifier,
LineFeed = 132,
EscapedIdentifier = 133,
JSXText = 134,
}

export const KeywordDescTable = [
Expand Down Expand Up @@ -233,7 +234,9 @@ export const KeywordDescTable = [
/* Others */
'enum', 'eval', 'arguments', 'escaped reserved', 'escaped future reserved', 'reserved if strict', '#',

'BigIntLiteral', '??', 'WhiteSpace', '?.', 'Illegal', 'LineTerminator', 'PrivateField', 'Template', '@', 'target', 'LineFeed', 'Escaped', 'JSXText', 'JSXText'
'BigIntLiteral', '??', '?.', 'WhiteSpace', 'Illegal', 'CarriageReturn', 'PrivateField', 'Template',

'@', 'target', 'LineFeed', 'EscapedIdentifier', 'JSXText'
];

// Normal object is much faster than Object.create(null), even with typeof check to avoid Object.prototype interference
Expand Down

0 comments on commit 972a6f0

Please sign in to comment.