-
Notifications
You must be signed in to change notification settings - Fork 46
/
match.ts
140 lines (118 loc) · 3.27 KB
/
match.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import * as _ from 'lodash';
import { IToken } from '../lexer/token';
import { chain } from './chain';
import { IElements } from './define';
import { Scanner } from './scanner';
export interface IMatch {
token?: IToken;
match: boolean;
}
function equalWordOrIncludeWords(str: string, word: string | string[] | null) {
if (typeof word === 'string') {
return judgeMatch(str, word);
} else {
return word.some(eachWord => judgeMatch(str, eachWord));
}
}
function judgeMatch(source: string, target: string) {
if (source === null) {
return false;
} else {
return (source && source.toLowerCase()) === (target && target.toLowerCase());
}
}
function matchToken(scanner: Scanner, compare: (token: IToken) => boolean, isCostToken?: boolean): IMatch {
const token = scanner.read();
if (!token) {
return {
token: null,
match: false
};
}
if (compare(token)) {
if (isCostToken) {
scanner.next();
}
return {
token,
match: true
};
} else {
return {
token,
match: false
};
}
}
function createMatch<T>(fn: (scanner: Scanner, arg?: T, isCostToken?: boolean) => IMatch, specialName?: string) {
return (arg?: T) => {
function foo() {
return (scanner: Scanner, isCostToken?: boolean) => fn(scanner, arg, isCostToken);
}
foo.parserName = 'match';
foo.displayName = specialName;
return foo;
};
}
export const match = createMatch((scanner, word: string | string[], isCostToken) =>
matchToken(scanner, token => equalWordOrIncludeWords(token.value, word), isCostToken)
);
interface IMatchTokenTypeOption {
includes?: string[];
excludes?: string[];
}
export const matchTokenType = (tokenType: string, opts: IMatchTokenTypeOption = {}) => {
const options: IMatchTokenTypeOption = { includes: [], excludes: [], ...opts };
return createMatch((scanner, word, isCostToken) => {
return matchToken(
scanner,
token => {
if (options.includes.some(includeValue => judgeMatch(includeValue, token.value))) {
return true;
}
if (options.excludes.some(includeValue => judgeMatch(includeValue, token.value))) {
return false;
}
if (token.type !== tokenType) {
return false;
}
return true;
},
isCostToken
);
}, tokenType)();
};
export const matchTrue = (): IMatch => ({
token: null,
match: true
});
export const matchFalse = (): IMatch => ({
token: null,
match: true
});
export const optional = (...elements: IElements) => {
if (elements.length === 0) {
throw Error('Must have arguments!');
}
return chain([chain(...elements)(ast => (elements.length === 1 ? ast[0] : ast)), true])(ast => ast[0]);
};
export const plus = (...elements: IElements) => {
if (elements.length === 0) {
throw Error('Must have arguments!');
}
const plusFunction = () =>
chain(chain(...elements)(ast => (elements.length === 1 ? ast[0] : ast)), optional(plusFunction))(ast => {
if (ast[1]) {
return [ast[0]].concat(ast[1]);
} else {
return [ast[0]];
}
});
return plusFunction;
};
export const many = (...elements: IElements) => {
if (elements.length === 0) {
throw Error('Must have arguments!');
}
return optional(plus(...elements));
};