-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser-combinator.js
201 lines (140 loc) · 4.36 KB
/
parser-combinator.js
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
const updateParserState = (state, index, result) => {
return { ...state, index, result };
}
const updateParserResult = (state, result) => {
return { ...state, result };
}
const updateParserError = (state, errorMsg) => {
return { ...state, isError: true, error: errorMsg}
}
class Parser {
constructor(stateTransformFn){
this.stateTransformFn = stateTransformFn;
}
run(target) {
const initialState = {
target,
result: null,
index: 0,
error: null,
isError: false
}
return this.stateTransformFn(initialState);
}
map(resultTransformFn) {
return new Parser(parserState => {
const nextState = this.stateTransformFn(parserState);
if(nextState.isError) return parserState;
return updateParserResult(parserState, resultTransformFn(nextState.result));
})
}
}
const str = s=>new Parser((parserState) => {
let { target, index, isError } = parserState;
if (isError) {
return parserState;
}
target = target.slice(index);
if (target.length === 0) {
return updateParserError(parserState, `str: tried to match ${s} but got end of input error.`)
}
if (target.startsWith(s)) {
return updateParserState(parserState, index + s.length, s );
}
return updateParserError(parserState, `str: could not find string ${s} at the index ${parserState.index}`);
})
const lettersRegex = /^[a-zA-Z]+/i
const letters = new Parser((parserState) => {
let { target, index, isError } = parserState;
if (isError) {
return parserState;
}
target = target.slice(index);
if (target.length === 0) {
return updateParserError(parserState, `letters: got end of input error.`)
}
const match = target.match(lettersRegex);
if(match){
return updateParserState(parserState, index + match[0].length, match[0])
}
return updateParserError(parserState, `letters: could not match letters at index ${index}`)
})
const digitRegex = /[0-9]/i
const digit = new Parser((parserState) => {
let { target, index, isError} = parserState;
if (isError) {
return parserState;
}
target = target.slice(index);
if (target.length === 0) {
return updateParserError(parserState, `digit: got end of input error.`)
}
let match = target.match(digitRegex);
if (match) {
return {...parserState, result: Number(match[0]), index: index + 1 }
}
return updateParserError(parserState, `digit: could not parse digit at index ${index}`);
})
const digitsRegex = /[0-9]+/i
const digits = new Parser((parserState) => {
let { target, index, isError} = parserState;
if (isError) {
return parserState;
}
target = target.slice(index);
if (target.length === 0) {
return updateParserError(parserState, `digits: got end of input error.`)
}
let match = target.match(digitsRegex);
if (match) {
return {...parserState, result: Number(match[0]), index: index + 1 }
}
return updateParserError(parserState, `digit: could not parse digits at index ${index}`);
})
const sequenceOf = (parsers) => new Parser((parserState) => {
if (parserState.isError) {
return parserState;
}
const results = [];
let nextState = parserState;
for ( let p of parsers) {
let testState = p.stateTransformFn(nextState);
if (testState.isError) {
throw Error(`Could not parse at position ${testState.index}`)
} else {
results.push(testState.result);
nextState = testState
}
}
return updateParserResult(parserState, results);
})
const choice = (parsers) => new Parser((parserState) => {
if (parserState.isError) {
return parserState;
}
for ( let p of parsers) {
const nextState = p.stateTransformFn(parserState);
if (!nextState.isError) {
return nextState;
}
}
return updateParserError(
parserState,
`choice: unable to match with any parser at index ${parserState.index}`
)
})
console.log(
sequenceOf(
[
str('a'),
str('n'),
str('i'),
]
)
.map((result=>{
return result.map((i)=>{
return `* - ${i}`
})
}))
.run('ani')
);