-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
serialize.ts
114 lines (87 loc) · 2.79 KB
/
serialize.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
import type {
ExpressionToken,
LiqeQuery,
} from './types';
const quote = (value: string, quotes: 'double' | 'single') => {
if (quotes === 'double') {
return `"${value}"`;
}
if (quotes === 'single') {
return `'${value}'`;
}
return value;
};
const serializeExpression = (expression: ExpressionToken) => {
if (expression.type === 'LiteralExpression') {
if (expression.quoted && typeof expression.value === 'string') {
return quote(expression.value, expression.quotes);
}
return String(expression.value);
}
if (expression.type === 'RegexExpression') {
return String(expression.value);
}
if (expression.type === 'RangeExpression') {
const {
min,
max,
minInclusive,
maxInclusive,
} = expression.range;
return `${minInclusive ? '[' : '{'}${min} TO ${max}${maxInclusive ? ']' : '}'}`;
}
if (expression.type === 'EmptyExpression') {
return '';
}
throw new Error('Unexpected AST type.');
};
const serializeTag = (ast: LiqeQuery) => {
if (ast.type !== 'Tag') {
throw new Error('Expected a tag expression.');
}
const {
field,
expression,
operator,
} = ast;
if (field.type === 'ImplicitField') {
return serializeExpression(expression);
}
const left = field.quoted ? quote(field.name, field.quotes) : field.name;
const patEnd = ' '.repeat(expression.location.start - operator.location.end);
return left + operator.operator + patEnd + serializeExpression(expression);
};
export const serialize = (ast: LiqeQuery): string => {
if (ast.type === 'ParenthesizedExpression') {
if (!('location' in ast.expression)) {
throw new Error('Expected location in expression.');
}
if (!ast.location.end) {
throw new Error('Expected location end.');
}
const patStart = ' '.repeat(ast.expression.location.start - (ast.location.start + 1));
const patEnd = ' '.repeat(ast.location.end - ast.expression.location.end - 1);
return `(${patStart}${serialize(ast.expression)}${patEnd})`;
}
if (ast.type === 'Tag') {
return serializeTag(ast);
}
if (ast.type === 'LogicalExpression') {
let operator = '';
if (ast.operator.type === 'BooleanOperator') {
operator += ' '.repeat(ast.operator.location.start - ast.left.location.end);
operator += ast.operator.operator;
operator += ' '.repeat(ast.right.location.start - ast.operator.location.end);
} else {
operator = ' '.repeat(ast.right.location.start - ast.left.location.end);
}
return `${serialize(ast.left)}${operator}${serialize(ast.right)}`;
}
if (ast.type === 'UnaryOperator') {
return (ast.operator === 'NOT' ? 'NOT ' : ast.operator) + serialize(ast.operand);
}
if (ast.type === 'EmptyExpression') {
return '';
}
throw new Error('Unexpected AST type.');
};