-
Notifications
You must be signed in to change notification settings - Fork 1
/
condition-lexer.ts
134 lines (111 loc) · 4 KB
/
condition-lexer.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
import { ConditionError } from '../../error/';
import { LexerToken } from '../';
/**
* Class that lexicographically parses a condition object into tokens.
*/
export class ConditionLexer {
/**
* Parse the sentence into tokens.
* @param condStr - The condition string to parse. If condStr is an object
* it is converted to a string using JSON.stringify.
* @return An array of tokens. Each token object has the following
* properties: terminal, which is a boolean indicating if the token is a
* terminal or not; type, as described by the condition BNF; and value.
*/
parse(condStr: string|object): LexerToken[] {
// Make sure condStr is actually a string. If it's an object, stringify it.
if (typeof condStr === 'object')
condStr = JSON.stringify(condStr);
const tokens: LexerToken[] = [];
const len = condStr.length;
const boolOps = ['$and', '$or'];
const compOps = ['$eq', '$neq', '$lt', '$lte', '$gt', '$gte', '$like', '$notLike'];
const inOps = ['$in', '$notIn'];
const nullOps = ['$is', '$isnt'];
let curChar = '';
let str, nextQuote, nonNum;
// Helper to add a token to the tokens array.
function addToken(terminal: boolean, type: string, value: any): void {
tokens.push(new LexerToken(terminal, type, value));
}
for (let i = 0; i < len; ++i) {
curChar = condStr[i];
switch (curChar) {
// Basic terminals.
case '{':
case '}':
case '[':
case ']':
case ':':
case ',':
addToken(true, 'char', curChar);
break;
case '"':
// Move past the quote.
if (++i >= condStr.length)
throw new ConditionError('Expected character but found EOL.');
// The string immediatly after the quote, and the index of the next quote.
str = condStr.substring(i);
nextQuote = str.indexOf('"');
// No next quote - hit EOL.
if (nextQuote === -1)
throw new ConditionError('Expected quote but found EOL.');
// Looks good - store the actual string and advance the token pointer.
str = str.substring(0, nextQuote);
i += nextQuote;
// Store the appropriate token.
if (boolOps.indexOf(str) !== -1)
addToken(false, 'boolean-operator', str);
else if (compOps.indexOf(str) !== -1)
addToken(false, 'comparison-operator', str);
else if (inOps.indexOf(str) !== -1)
addToken(false, 'in-comparison-operator', str);
else if (nullOps.indexOf(str) !== -1)
addToken(false, 'null-comparison-operator', str);
else if (str[0] === ':')
addToken(true, 'parameter', str);
else
addToken(true, 'column', str);
break;
// Number.
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '.':
case '-':
str = condStr.substring(i);
// The first character that's not part of a number.
nonNum = str.search(/[^0-9\.\-\+]+/);
// Number continues to the end of the string.
if (nonNum === -1)
nonNum = str.length;
// Pull the number, and advance the token pointer.
str = str.substring(0, nonNum);
i += nonNum - 1;
if (isNaN(Number(str)))
throw new ConditionError(`Expected number but found ${str}.`);
addToken(true, 'number', Number(str));
break;
// Null.
case 'n':
str = condStr.substr(i, 4);
if (str !== 'null')
throw new ConditionError(`Expected null but found ${str}.`);
addToken(true, 'null', null);
i += 3;
break;
// Anything else is invalid.
default:
throw new ConditionError(`Unexpected character. Found ${curChar}.`);
}
}
return tokens;
}
}