forked from thomaspoignant/scim2-parse-filter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.ts
175 lines (166 loc) · 4.83 KB
/
parser.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import { Filter, Compare, NotFilter, Suffix, ValuePath } from "./index";
type TokenType = "Number" | "Quoted" | "Bracket" | "Word" | "EOT";
const EOT = { type: "EOT" as TokenType, literal: "" };
export type Token = {
type: TokenType;
literal: string;
};
export function tokenizer(f: string): Token[] {
const ret: Token[] = [];
let rest = f;
const patterns = /^(?:(\s+)|(-?\d+(?:\.\d+)?(?:[eE][-+]?\d+)?)|("(?:[^"]|\\.|\n)*")|([[()]|]\.?)|(\w[-\w._:\/%]*))/;
let n;
while ((n = patterns.exec(rest))) {
if (n[1] || n[0].length === 0) {
//
} else if (n[2]) {
ret.push({ literal: n[2], type: "Number" });
} else if (n[3]) {
const literal = n[3].replace(/\\/g, '\\\\');
ret.push({ literal, type: "Quoted" });
} else if (n[4]) {
ret.push({ literal: n[4], type: "Bracket" });
} else if (n[5]) {
ret.push({ literal: n[5], type: "Word" });
}
rest = rest.substring(n.index + n[0].length);
}
if (rest.length !== 0) {
throw new Error(`unexpected token ${rest}`);
}
ret.push(EOT);
return ret;
}
export class Tokens implements TokenList {
i: number;
private current: Token | undefined;
getList() {
return this.list.map((a, i) =>
i == this.i ? `[${a.literal}]` : a.literal
);
}
peek(): Token {
return this.current || EOT;
}
constructor(private list: Token[]) {
this.i = 0;
this.current = this.list[this.i];
}
forward(): TokenList {
this.current = this.list[++this.i];
return this;
}
shift(): Token {
const c = this.peek();
this.forward();
return c;
}
}
interface TokenList {
peek(): Token;
forward(): TokenList;
shift(): Token;
}
const cops = new Set(["eq", "ne", "co", "sw", "ew", "gt", "lt", "ge", "le"]);
const sops = new Set(["pr"]);
export function parseFilter(list: TokenList): Filter {
return parseInxif(parseExpression(list), list, Precedence.LOWEST);
}
export function parseExpression(list: TokenList): Filter {
const t = list.shift();
if (t.literal == "(") {
const filter = parseFilter(list);
const close = list.shift();
if (close.literal !== ")") {
throw new Error(
`Unexpected token [${close.literal}(${close.type})] expected ')'`
);
}
return filter;
} else if (t.literal.toLowerCase() == "not") {
const notFilter: NotFilter = { op: "not", filter: parseExpression(list) };
return parseInxif(notFilter, list, Precedence.NOT);
} else if (t.type == "Word") {
return readValFilter(t, list);
} else {
throw new Error(`Unexpected token ${t.literal} (${t.type})`);
}
}
enum Precedence {
LOWEST = 1,
OR = 2,
AND = 3,
NOT = 4
}
const PRECEDENCE: { [key: string]: Precedence } = {
'or': Precedence.OR,
'and': Precedence.AND,
'not': Precedence.NOT
}
function parseInxif(left: Filter, list: TokenList, precede: Precedence): Filter {
const op = list.peek().literal.toLowerCase();
const p = PRECEDENCE[op];
if (!p || precede >= p) {
return left;
}
const filters = [left];
while (list.peek().literal.toLowerCase() === op) {
let r = parseExpression(list.forward());
const rr = list.peek().literal.toLowerCase();
if (PRECEDENCE[rr] > p) {
r = parseInxif(r, list, p);
}
filters.push(r);
}
return parseInxif({ op, filters } as Filter, list, precede);
}
function readValFilter(left: Token, list: TokenList): Filter {
if (left.type !== "Word") {
throw new Error(`Unexpected token ${left.literal} expected Word`);
}
const attrPath = left.literal;
const t = list.shift();
const op = t.literal.toLowerCase();
if (cops.has(op)) {
const compValue = parseCompValue(list);
return { op, attrPath, compValue } as Compare;
} else if (sops.has(op)) {
return { op, attrPath } as Suffix;
} else if (op === "[") {
const valFilter = parseFilter(list);
const close = list.shift();
if (close.literal[0] !== "]") {
throw new Error(`Unexpected token ${close.literal} expected ']'`);
}
const valPath: ValuePath = { op: "[]", attrPath, valFilter };
if (close.literal[1] !== "." || list.peek().type !== "Word") {
return valPath
}
// convert a sub-attribute after a value-path to an 'and' op
const next = list.shift()
next.literal = `${attrPath}.${next.literal}`
return { op: 'and', filters: [valPath, readValFilter(next, list)] }
} else {
throw new Error(
`Unexpected token ${attrPath} ${t.literal} as valFilter operator`
);
}
}
function parseCompValue(list: TokenList): Compare["compValue"] {
const t = list.shift();
try {
const v = JSON.parse(t.literal);
if (
v === null ||
typeof v == "string" ||
typeof v == "number" ||
typeof v == "boolean"
) {
return v;
} else {
throw new Error(`${t.literal} is ${typeof v} (un supported value)`);
}
} catch (e) {
throw new Error(`[${t.literal}(${t.type})] is not json`);
}
}