/
nodes.ts
173 lines (146 loc) · 3.21 KB
/
nodes.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
import type {
AstRegExp,
Alternative,
Expression,
Char,
SimpleChar,
SpecialChar,
ClassRange,
CharacterClass,
Quantifier,
SimpleQuantifier,
RangeQuantifier,
CapturingGroup,
Repetition,
Disjunction
} from 'regexp-tree/ast'
import { concat } from '../utils/index.js'
export function AstRegExpNode(body: Expression): AstRegExp {
return {
type: 'RegExp',
body,
flags: ''
}
}
export function AlternativeNode(
...expressions: (Expression | Expression[])[]
): Alternative | Expression {
const exps = concat(expressions).filter(Boolean)
if (exps.length === 1) {
return exps[0]
}
return {
type: 'Alternative',
expressions: exps
}
}
export function SimpleCharNode(value: string | number): SimpleChar {
return {
type: 'Char',
kind: 'simple',
value: String(value),
codePoint: NaN
}
}
export function MetaCharNode(value: string): SpecialChar {
return {
type: 'Char',
kind: 'meta',
value,
codePoint: NaN
}
}
export function ClassRangeNode(from: Char, to: Char): ClassRange {
return {
type: 'ClassRange',
from,
to
}
}
export function CharacterClassNode(
...expressions: (Char | ClassRange | (Char | ClassRange)[])[]
): CharacterClass {
return {
type: 'CharacterClass',
expressions: concat(expressions).filter(Boolean)
}
}
export function SimpleQuantifierNode(kind: SimpleQuantifier['kind']): SimpleQuantifier {
return {
type: 'Quantifier',
kind,
greedy: true
}
}
export function RangeQuantifierNode(from: number, to?: number): RangeQuantifier {
return {
type: 'Quantifier',
kind: 'Range',
from,
to,
greedy: true
}
}
export function CapturingGroupNode(expression: Expression): CapturingGroup {
return {
type: 'Group',
capturing: true,
expression,
number: null
}
}
export function RepetitionNode(expression: Expression, quantifier: Quantifier): Repetition {
return {
type: 'Repetition',
expression,
quantifier
}
}
export function DisjunctionNode(...expressions: (Expression | Expression[])[]): Disjunction | Expression {
const exprs = concat(expressions).filter(Boolean)
if (exprs.length === 1) {
return exprs[0]
}
const disjunction: Disjunction = {
type: 'Disjunction',
left: null,
right: exprs.pop()
}
exprs.reduceRight<Disjunction>((disjunction, expr, i) => {
if (i === 0) {
disjunction.left = expr
return disjunction
}
disjunction.left = {
type: 'Disjunction',
left: null,
right: expr
}
return disjunction.left
}, disjunction)
return disjunction
}
export function DisjunctionCapturingGroupNode(...expressions: (Expression | Expression[])[]) {
const expr = DisjunctionNode(...expressions)
if (expr.type === 'Disjunction') {
return CapturingGroupNode(expr)
}
return expr
}
export function DigitPatternNode() {
return MetaCharNode('\\d')
}
export function NumberPatternNode(
quantifier: Quantifier = SimpleQuantifierNode('+')
) {
const numberPattern = RepetitionNode(
DigitPatternNode(),
quantifier
)
return numberPattern
}
export function NumberCharsNode(value: number) {
return AlternativeNode(
Array.from(String(value), SimpleCharNode)
)
}