diff --git a/src/parser/util.js b/src/parser/util.js index 891a193c25..89ec41154e 100644 --- a/src/parser/util.js +++ b/src/parser/util.js @@ -83,7 +83,7 @@ export default class UtilParser extends Tokenizer { // instead of a message string. unexpected(pos: ?number, messageOrType: string | TokenType = "Unexpected token"): empty { - if (messageOrType && typeof messageOrType === "object" && messageOrType.label) { + if (typeof messageOrType !== "string") { messageOrType = `Unexpected token, expected ${messageOrType.label}`; } throw this.raise(pos != null ? pos : this.state.start, messageOrType); diff --git a/src/tokenizer/context.js b/src/tokenizer/context.js index 81db0499c3..7b7b97b6d7 100644 --- a/src/tokenizer/context.js +++ b/src/tokenizer/context.js @@ -1,3 +1,5 @@ +// @flow + // The algorithm used to determine whether a regexp can appear at a // given point in the program is loosely based on sweet.js' approach. // See https://github.com/mozilla/sweet.js/wiki/design diff --git a/src/tokenizer/types.js b/src/tokenizer/types.js index 1d78aabb40..e6552249e6 100644 --- a/src/tokenizer/types.js +++ b/src/tokenizer/types.js @@ -1,3 +1,5 @@ +// @flow + // ## Token types // The assignment of fine-grained, information-carrying type objects @@ -23,8 +25,33 @@ const isAssign = true; const prefix = true; const postfix = true; +type TokenOptions = { + keyword?: string; + + beforeExpr?: boolean; + startsExpr?: boolean; + rightAssociative?: boolean; + isLoop?: boolean; + isAssign?: boolean; + prefix?: boolean; + postfix?: boolean; + binop?: ?number; +}; + export class TokenType { - constructor(label, conf = {}) { + label: string; + keyword: ?string; + beforeExpr: boolean; + startsExpr: boolean; + rightAssociative: boolean; + isLoop: boolean; + isAssign: boolean; + prefix: boolean; + postfix: boolean; + binop: ?number; + updateContext: ?((prevType: TokenType) => void); + + constructor(label: string, conf: TokenOptions = {}) { this.label = label; this.keyword = conf.keyword; this.beforeExpr = !!conf.beforeExpr; @@ -40,7 +67,7 @@ export class TokenType { } class KeywordTokenType extends TokenType { - constructor(name, options = {}) { + constructor(name: string, options: TokenOptions = {}) { options.keyword = name; super(name, options); @@ -48,12 +75,12 @@ class KeywordTokenType extends TokenType { } export class BinopTokenType extends TokenType { - constructor(name, prec) { + constructor(name: string, prec: number) { super(name, { beforeExpr, binop: prec }); } } -export const types = { +export const types: { [name: string]: TokenType } = { num: new TokenType("num", { startsExpr }), regexp: new TokenType("regexp", { startsExpr }), string: new TokenType("string", { startsExpr }),