Type-check tokenizer/types.js (#493)

This commit is contained in:
Andy 2017-04-27 07:53:17 -07:00 committed by Henry Zhu
parent cd5bfb786a
commit 8288f7d9e4
3 changed files with 34 additions and 5 deletions

View File

@ -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);

View File

@ -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

View File

@ -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 }),