Skip to content
This repository has been archived by the owner on May 19, 2018. It is now read-only.

Type-check tokenizer/types.js #493

Merged
merged 1 commit into from Apr 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/parser/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/tokenizer/context.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down
35 changes: 31 additions & 4 deletions src/tokenizer/types.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @flow

// ## Token types

// The assignment of fine-grained, information-carrying type objects
Expand All @@ -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;
Expand All @@ -40,20 +67,20 @@ export class TokenType {
}

class KeywordTokenType extends TokenType {
constructor(name, options = {}) {
constructor(name: string, options: TokenOptions = {}) {
options.keyword = name;

super(name, options);
}
}

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 }),
Expand Down