tarsec 0.0.3 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -0
- package/dist/combinators.js +188 -178
- package/dist/index.js +15 -5
- package/dist/parsers.js +102 -92
- package/dist/trace.js +39 -29
- package/dist/types.js +12 -2
- package/dist/utils.js +42 -32
- package/package.json +6 -5
package/README.md
CHANGED
package/dist/combinators.js
CHANGED
|
@@ -1,189 +1,199 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
function
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
1
|
+
(function (factory) {
|
|
2
|
+
if (typeof module === "object" && typeof module.exports === "object") {
|
|
3
|
+
var v = factory(require, exports);
|
|
4
|
+
if (v !== undefined) module.exports = v;
|
|
5
|
+
}
|
|
6
|
+
else if (typeof define === "function" && define.amd) {
|
|
7
|
+
define(["require", "exports", "./trace", "./utils"], factory);
|
|
8
|
+
}
|
|
9
|
+
})(function (require, exports) {
|
|
10
|
+
"use strict";
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.transform = exports.shapeCaptures = exports.captureCaptures = exports.capture = exports.seq = exports.sepBy = exports.between = exports.not = exports.optional = exports.or = exports.many1WithJoin = exports.manyWithJoin = exports.many1 = exports.many = void 0;
|
|
13
|
+
const trace_1 = require("./trace");
|
|
14
|
+
const utils_1 = require("./utils");
|
|
15
|
+
function many(parser) {
|
|
16
|
+
return (0, trace_1.trace)("many", (input) => {
|
|
17
|
+
let match = [];
|
|
18
|
+
let rest = input;
|
|
19
|
+
while (true) {
|
|
20
|
+
let result = parser(rest);
|
|
21
|
+
if (!result.success) {
|
|
22
|
+
return { success: true, match, rest };
|
|
23
|
+
}
|
|
24
|
+
match.push(result.match);
|
|
25
|
+
rest = result.rest;
|
|
14
26
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
let result = many(parser)(input);
|
|
24
|
-
// this logic doesn't work with optional and not
|
|
25
|
-
if (result.rest !== input) {
|
|
26
|
-
return result;
|
|
27
|
-
}
|
|
28
|
-
return {
|
|
29
|
-
success: false,
|
|
30
|
-
rest: input,
|
|
31
|
-
message: "expected at least one match",
|
|
32
|
-
};
|
|
33
|
-
});
|
|
34
|
-
}
|
|
35
|
-
exports.many1 = many1;
|
|
36
|
-
function manyWithJoin(parser) {
|
|
37
|
-
return transform(many(parser), (x) => x.join(""));
|
|
38
|
-
}
|
|
39
|
-
exports.manyWithJoin = manyWithJoin;
|
|
40
|
-
function many1WithJoin(parser) {
|
|
41
|
-
return transform(many1(parser), (x) => x.join(""));
|
|
42
|
-
}
|
|
43
|
-
exports.many1WithJoin = many1WithJoin;
|
|
44
|
-
function or(parsers, name = "") {
|
|
45
|
-
return (0, trace_1.trace)(`or(${name})`, (input) => {
|
|
46
|
-
for (let parser of parsers) {
|
|
47
|
-
let result = parser(input);
|
|
48
|
-
if (result.success) {
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
exports.many = many;
|
|
30
|
+
function many1(parser) {
|
|
31
|
+
return (0, trace_1.trace)(`many1`, (input) => {
|
|
32
|
+
let result = many(parser)(input);
|
|
33
|
+
// this logic doesn't work with optional and not
|
|
34
|
+
if (result.rest !== input) {
|
|
49
35
|
return result;
|
|
50
36
|
}
|
|
51
|
-
}
|
|
52
|
-
return {
|
|
53
|
-
success: false,
|
|
54
|
-
rest: input,
|
|
55
|
-
message: "all parsers failed",
|
|
56
|
-
};
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
|
-
exports.or = or;
|
|
60
|
-
function optional(parser) {
|
|
61
|
-
return (0, trace_1.trace)("optional", (input) => {
|
|
62
|
-
let result = parser(input);
|
|
63
|
-
if (result.success) {
|
|
64
|
-
return result;
|
|
65
|
-
}
|
|
66
|
-
return { success: true, match: null, rest: input };
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
exports.optional = optional;
|
|
70
|
-
function not(parser) {
|
|
71
|
-
return (0, trace_1.trace)("not", (input) => {
|
|
72
|
-
let result = parser(input);
|
|
73
|
-
if (result.success) {
|
|
74
37
|
return {
|
|
75
38
|
success: false,
|
|
76
39
|
rest: input,
|
|
77
|
-
message: "
|
|
40
|
+
message: "expected at least one match",
|
|
78
41
|
};
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
}
|
|
98
|
-
return { success: true, match: parserResult.match, rest: result2.rest };
|
|
99
|
-
};
|
|
100
|
-
}
|
|
101
|
-
exports.between = between;
|
|
102
|
-
function sepBy(separator, parser) {
|
|
103
|
-
return (input) => {
|
|
104
|
-
let match = [];
|
|
105
|
-
let rest = input;
|
|
106
|
-
while (true) {
|
|
107
|
-
const result = parser(rest);
|
|
108
|
-
if (!result.success) {
|
|
109
|
-
return { success: true, match, rest };
|
|
110
|
-
}
|
|
111
|
-
match.push(result.match);
|
|
112
|
-
rest = result.rest;
|
|
113
|
-
const sepResult = separator(rest);
|
|
114
|
-
if (!sepResult.success) {
|
|
115
|
-
return { success: true, match, rest };
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
exports.many1 = many1;
|
|
45
|
+
function manyWithJoin(parser) {
|
|
46
|
+
return transform(many(parser), (x) => x.join(""));
|
|
47
|
+
}
|
|
48
|
+
exports.manyWithJoin = manyWithJoin;
|
|
49
|
+
function many1WithJoin(parser) {
|
|
50
|
+
return transform(many1(parser), (x) => x.join(""));
|
|
51
|
+
}
|
|
52
|
+
exports.many1WithJoin = many1WithJoin;
|
|
53
|
+
function or(parsers, name = "") {
|
|
54
|
+
return (0, trace_1.trace)(`or(${name})`, (input) => {
|
|
55
|
+
for (let parser of parsers) {
|
|
56
|
+
let result = parser(input);
|
|
57
|
+
if (result.success) {
|
|
58
|
+
return result;
|
|
59
|
+
}
|
|
116
60
|
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
let result = parser(rest);
|
|
130
|
-
if (!result.success) {
|
|
61
|
+
return {
|
|
62
|
+
success: false,
|
|
63
|
+
rest: input,
|
|
64
|
+
message: "all parsers failed",
|
|
65
|
+
};
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
exports.or = or;
|
|
69
|
+
function optional(parser) {
|
|
70
|
+
return (0, trace_1.trace)("optional", (input) => {
|
|
71
|
+
let result = parser(input);
|
|
72
|
+
if (result.success) {
|
|
131
73
|
return result;
|
|
132
74
|
}
|
|
133
|
-
match
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
75
|
+
return { success: true, match: null, rest: input };
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
exports.optional = optional;
|
|
79
|
+
function not(parser) {
|
|
80
|
+
return (0, trace_1.trace)("not", (input) => {
|
|
81
|
+
let result = parser(input);
|
|
82
|
+
if (result.success) {
|
|
83
|
+
return {
|
|
84
|
+
success: false,
|
|
85
|
+
rest: input,
|
|
86
|
+
message: "unexpected match",
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
return { success: true, match: null, rest: input };
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
exports.not = not;
|
|
93
|
+
function between(open, close, parser) {
|
|
94
|
+
return (input) => {
|
|
95
|
+
const result1 = open(input);
|
|
96
|
+
if (!result1.success) {
|
|
97
|
+
return result1;
|
|
137
98
|
}
|
|
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
|
-
|
|
99
|
+
const parserResult = parser(result1.rest);
|
|
100
|
+
if (!parserResult.success) {
|
|
101
|
+
return parserResult;
|
|
102
|
+
}
|
|
103
|
+
const result2 = close(parserResult.rest);
|
|
104
|
+
if (!result2.success) {
|
|
105
|
+
return result2;
|
|
106
|
+
}
|
|
107
|
+
return { success: true, match: parserResult.match, rest: result2.rest };
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
exports.between = between;
|
|
111
|
+
function sepBy(separator, parser) {
|
|
112
|
+
return (input) => {
|
|
113
|
+
let match = [];
|
|
114
|
+
let rest = input;
|
|
115
|
+
while (true) {
|
|
116
|
+
const result = parser(rest);
|
|
117
|
+
if (!result.success) {
|
|
118
|
+
return { success: true, match, rest };
|
|
119
|
+
}
|
|
120
|
+
match.push(result.match);
|
|
121
|
+
rest = result.rest;
|
|
122
|
+
const sepResult = separator(rest);
|
|
123
|
+
if (!sepResult.success) {
|
|
124
|
+
return { success: true, match, rest };
|
|
125
|
+
}
|
|
126
|
+
rest = sepResult.rest;
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
exports.sepBy = sepBy;
|
|
131
|
+
function seq(parsers, name = "") {
|
|
132
|
+
return (0, trace_1.trace)(`seq(${name})`, (input) => {
|
|
133
|
+
let match = [];
|
|
134
|
+
let rest = input;
|
|
135
|
+
// @ts-ignore
|
|
136
|
+
let captures = {};
|
|
137
|
+
for (let parser of parsers) {
|
|
138
|
+
let result = parser(rest);
|
|
139
|
+
if (!result.success) {
|
|
140
|
+
return result;
|
|
141
|
+
}
|
|
142
|
+
match.push(result.match);
|
|
143
|
+
rest = result.rest;
|
|
144
|
+
if (result.captures) {
|
|
145
|
+
captures = (0, utils_1.mergeCaptures)(captures, result.captures);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return { success: true, match, rest, captures };
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
exports.seq = seq;
|
|
152
|
+
function capture(parser, name) {
|
|
153
|
+
return (0, trace_1.trace)(`captures(${(0, utils_1.escape)(name)})`, (input) => {
|
|
154
|
+
let result = parser(input);
|
|
155
|
+
if (result.success) {
|
|
156
|
+
const captures = {
|
|
157
|
+
[name]: result.match,
|
|
158
|
+
};
|
|
159
|
+
return Object.assign(Object.assign({}, result), { captures: (0, utils_1.mergeCaptures)(result.captures || {}, captures) });
|
|
160
|
+
}
|
|
161
|
+
return result;
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
exports.capture = capture;
|
|
165
|
+
function captureCaptures(parser, name) {
|
|
166
|
+
return (0, trace_1.trace)(`captures(${(0, utils_1.escape)(name)})`, (input) => {
|
|
167
|
+
let result = parser(input);
|
|
168
|
+
if (result.success) {
|
|
169
|
+
const captures = {
|
|
170
|
+
[name]: result.captures,
|
|
171
|
+
};
|
|
172
|
+
return Object.assign(Object.assign({}, result), { captures: (0, utils_1.mergeCaptures)(result.captures || {}, captures) });
|
|
173
|
+
}
|
|
174
|
+
return result;
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
exports.captureCaptures = captureCaptures;
|
|
178
|
+
function shapeCaptures(parser, func, name) {
|
|
179
|
+
return (0, trace_1.trace)(`captures(${(0, utils_1.escape)(name)})`, (input) => {
|
|
180
|
+
let result = parser(input);
|
|
181
|
+
if (result.success) {
|
|
182
|
+
const captures = result.captures || {};
|
|
183
|
+
return Object.assign(Object.assign({}, result), { captures: func(captures) });
|
|
184
|
+
}
|
|
185
|
+
return result;
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
exports.shapeCaptures = shapeCaptures;
|
|
189
|
+
function transform(parser, transformerFunc) {
|
|
190
|
+
return (0, trace_1.trace)(`transform(${transformerFunc})`, (input) => {
|
|
191
|
+
let result = parser(input);
|
|
192
|
+
if (result.success) {
|
|
193
|
+
return Object.assign(Object.assign({}, result), { match: transformerFunc(result.match) });
|
|
194
|
+
}
|
|
195
|
+
return result;
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
exports.transform = transform;
|
|
199
|
+
});
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
2
|
if (k2 === undefined) k2 = k;
|
|
4
3
|
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
@@ -13,7 +12,18 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
|
|
|
13
12
|
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
13
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
14
|
};
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
15
|
+
(function (factory) {
|
|
16
|
+
if (typeof module === "object" && typeof module.exports === "object") {
|
|
17
|
+
var v = factory(require, exports);
|
|
18
|
+
if (v !== undefined) module.exports = v;
|
|
19
|
+
}
|
|
20
|
+
else if (typeof define === "function" && define.amd) {
|
|
21
|
+
define(["require", "exports", "./parsers", "./combinators", "./trace"], factory);
|
|
22
|
+
}
|
|
23
|
+
})(function (require, exports) {
|
|
24
|
+
"use strict";
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
__exportStar(require("./parsers"), exports);
|
|
27
|
+
__exportStar(require("./combinators"), exports);
|
|
28
|
+
__exportStar(require("./trace"), exports);
|
|
29
|
+
});
|
package/dist/parsers.js
CHANGED
|
@@ -1,104 +1,114 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
(function (factory) {
|
|
2
|
+
if (typeof module === "object" && typeof module.exports === "object") {
|
|
3
|
+
var v = factory(require, exports);
|
|
4
|
+
if (v !== undefined) module.exports = v;
|
|
5
|
+
}
|
|
6
|
+
else if (typeof define === "function" && define.amd) {
|
|
7
|
+
define(["require", "exports", "./combinators", "./trace", "./utils"], factory);
|
|
8
|
+
}
|
|
9
|
+
})(function (require, exports) {
|
|
10
|
+
"use strict";
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.quotedString = exports.newline = exports.tab = exports.quote = exports.num = exports.word = exports.alphanum = exports.letter = exports.digit = exports.spaces = exports.space = exports.anyChar = exports.noneOf = exports.oneOf = exports.str = exports.char = void 0;
|
|
13
|
+
const combinators_1 = require("./combinators");
|
|
14
|
+
const trace_1 = require("./trace");
|
|
15
|
+
const utils_1 = require("./utils");
|
|
16
|
+
function char(c) {
|
|
17
|
+
return (0, trace_1.trace)(`char(${(0, utils_1.escape)(c)})`, (input) => {
|
|
18
|
+
if (input.length === 0) {
|
|
19
|
+
return {
|
|
20
|
+
success: false,
|
|
21
|
+
rest: input,
|
|
22
|
+
message: "unexpected end of input",
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
if (input[0] === c) {
|
|
26
|
+
return { success: true, match: c, rest: input.slice(1) };
|
|
27
|
+
}
|
|
10
28
|
return {
|
|
11
29
|
success: false,
|
|
12
30
|
rest: input,
|
|
13
|
-
message:
|
|
31
|
+
message: `expected ${c}, got ${input[0]}`,
|
|
14
32
|
};
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
return {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
};
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
exports.char = char;
|
|
27
|
-
function str(s) {
|
|
28
|
-
return (0, trace_1.trace)(`str(${(0, utils_1.escape)(s)})`, (input) => {
|
|
29
|
-
if (input.substring(0, s.length) === s) {
|
|
30
|
-
return { success: true, match: s, rest: input.slice(s.length) };
|
|
31
|
-
}
|
|
32
|
-
return {
|
|
33
|
-
success: false,
|
|
34
|
-
rest: input,
|
|
35
|
-
message: `expected ${s}, got ${input.substring(0, s.length)}`,
|
|
36
|
-
};
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
exports.str = str;
|
|
40
|
-
function oneOf(chars) {
|
|
41
|
-
return (0, trace_1.trace)(`oneOf(${(0, utils_1.escape)(chars)})`, (input) => {
|
|
42
|
-
if (input.length === 0) {
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
exports.char = char;
|
|
36
|
+
function str(s) {
|
|
37
|
+
return (0, trace_1.trace)(`str(${(0, utils_1.escape)(s)})`, (input) => {
|
|
38
|
+
if (input.substring(0, s.length) === s) {
|
|
39
|
+
return { success: true, match: s, rest: input.slice(s.length) };
|
|
40
|
+
}
|
|
43
41
|
return {
|
|
44
42
|
success: false,
|
|
45
43
|
rest: input,
|
|
46
|
-
message:
|
|
44
|
+
message: `expected ${s}, got ${input.substring(0, s.length)}`,
|
|
47
45
|
};
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
exports.str = str;
|
|
49
|
+
function oneOf(chars) {
|
|
50
|
+
return (0, trace_1.trace)(`oneOf(${(0, utils_1.escape)(chars)})`, (input) => {
|
|
51
|
+
if (input.length === 0) {
|
|
52
|
+
return {
|
|
53
|
+
success: false,
|
|
54
|
+
rest: input,
|
|
55
|
+
message: "unexpected end of input",
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
const c = input[0];
|
|
59
|
+
if (chars.includes(c)) {
|
|
60
|
+
return char(c)(input);
|
|
61
|
+
}
|
|
64
62
|
return {
|
|
65
63
|
success: false,
|
|
66
64
|
rest: input,
|
|
67
|
-
message:
|
|
65
|
+
message: `expected one of ${(0, utils_1.escape)(chars)}, got ${c}`,
|
|
68
66
|
};
|
|
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
|
-
exports.
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
exports.oneOf = oneOf;
|
|
70
|
+
function noneOf(chars) {
|
|
71
|
+
return (0, trace_1.trace)(`noneOf(${(0, utils_1.escape)(chars)})`, (input) => {
|
|
72
|
+
if (input.length === 0) {
|
|
73
|
+
return {
|
|
74
|
+
success: false,
|
|
75
|
+
rest: input,
|
|
76
|
+
message: "unexpected end of input",
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
if (chars.includes(input[0])) {
|
|
80
|
+
return {
|
|
81
|
+
success: false,
|
|
82
|
+
rest: input,
|
|
83
|
+
message: `expected none of ${chars}`,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
return char(input[0])(input);
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
exports.noneOf = noneOf;
|
|
90
|
+
function anyChar(input) {
|
|
91
|
+
return (0, trace_1.trace)("anyChar", (input) => {
|
|
92
|
+
if (input.length === 0) {
|
|
93
|
+
return {
|
|
94
|
+
success: false,
|
|
95
|
+
rest: input,
|
|
96
|
+
message: "unexpected end of input",
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
return { success: true, match: input[0], rest: input.slice(1) };
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
exports.anyChar = anyChar;
|
|
103
|
+
exports.space = oneOf(" \t\n\r");
|
|
104
|
+
exports.spaces = (0, combinators_1.many1WithJoin)(exports.space);
|
|
105
|
+
exports.digit = oneOf("0123456789");
|
|
106
|
+
exports.letter = oneOf("abcdefghijklmnopqrstuvwxyz");
|
|
107
|
+
exports.alphanum = oneOf("abcdefghijklmnopqrstuvwxyz0123456789");
|
|
108
|
+
exports.word = (0, combinators_1.many1WithJoin)(exports.letter);
|
|
109
|
+
exports.num = (0, combinators_1.many1WithJoin)(exports.digit);
|
|
110
|
+
exports.quote = oneOf(`'"`);
|
|
111
|
+
exports.tab = char("\t");
|
|
112
|
+
exports.newline = char("\n");
|
|
113
|
+
exports.quotedString = (0, combinators_1.transform)((0, combinators_1.seq)([exports.quote, exports.word, exports.quote], "quotedString"), (x) => x.join(""));
|
|
114
|
+
});
|
package/dist/trace.js
CHANGED
|
@@ -1,32 +1,42 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const STEP = 2;
|
|
6
|
-
function resultToString(name, result) {
|
|
7
|
-
if (result.success) {
|
|
8
|
-
return `✅ ${name} -- match: ${(0, utils_1.escape)(result.match)}, rest: ${(0, utils_1.escape)(result.rest)}`;
|
|
1
|
+
(function (factory) {
|
|
2
|
+
if (typeof module === "object" && typeof module.exports === "object") {
|
|
3
|
+
var v = factory(require, exports);
|
|
4
|
+
if (v !== undefined) module.exports = v;
|
|
9
5
|
}
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
6
|
+
else if (typeof define === "function" && define.amd) {
|
|
7
|
+
define(["require", "exports", "./utils"], factory);
|
|
8
|
+
}
|
|
9
|
+
})(function (require, exports) {
|
|
10
|
+
"use strict";
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.trace = exports.resultToString = void 0;
|
|
13
|
+
const utils_1 = require("./utils");
|
|
14
|
+
const STEP = 2;
|
|
15
|
+
function resultToString(name, result) {
|
|
16
|
+
if (result.success) {
|
|
17
|
+
return `✅ ${name} -- match: ${(0, utils_1.escape)(result.match)}, rest: ${(0, utils_1.escape)(result.rest)}`;
|
|
18
18
|
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
19
|
+
return `❌ ${name} -- message: ${(0, utils_1.escape)(result.message)}, rest: ${(0, utils_1.escape)(result.rest)}`;
|
|
20
|
+
}
|
|
21
|
+
exports.resultToString = resultToString;
|
|
22
|
+
let level = 0;
|
|
23
|
+
function trace(name, parser) {
|
|
24
|
+
return (input) => {
|
|
25
|
+
if (process.env.DEBUG) {
|
|
26
|
+
console.log(" ".repeat(level) + `🔍 ${name} -- input: ${(0, utils_1.escape)(input)}`);
|
|
27
27
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
28
|
+
level += STEP;
|
|
29
|
+
const result = parser(input);
|
|
30
|
+
level -= STEP;
|
|
31
|
+
if (process.env.DEBUG) {
|
|
32
|
+
console.log(" ".repeat(level) + resultToString(name, result));
|
|
33
|
+
if (result.success && result.captures) {
|
|
34
|
+
console.log(" ".repeat(level) +
|
|
35
|
+
`⭐ ${name} -- captures: ${JSON.stringify(result.captures)}`);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return result;
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
exports.trace = trace;
|
|
42
|
+
});
|
package/dist/types.js
CHANGED
|
@@ -1,2 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
(function (factory) {
|
|
2
|
+
if (typeof module === "object" && typeof module.exports === "object") {
|
|
3
|
+
var v = factory(require, exports);
|
|
4
|
+
if (v !== undefined) module.exports = v;
|
|
5
|
+
}
|
|
6
|
+
else if (typeof define === "function" && define.amd) {
|
|
7
|
+
define(["require", "exports"], factory);
|
|
8
|
+
}
|
|
9
|
+
})(function (require, exports) {
|
|
10
|
+
"use strict";
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
});
|
package/dist/utils.js
CHANGED
|
@@ -1,38 +1,48 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
return JSON.stringify(str);
|
|
6
|
-
}
|
|
7
|
-
exports.escape = escape;
|
|
8
|
-
function merge(a, b) {
|
|
9
|
-
if (Array.isArray(a) && Array.isArray(b)) {
|
|
10
|
-
return [...a, ...b];
|
|
1
|
+
(function (factory) {
|
|
2
|
+
if (typeof module === "object" && typeof module.exports === "object") {
|
|
3
|
+
var v = factory(require, exports);
|
|
4
|
+
if (v !== undefined) module.exports = v;
|
|
11
5
|
}
|
|
12
|
-
else if (
|
|
13
|
-
|
|
6
|
+
else if (typeof define === "function" && define.amd) {
|
|
7
|
+
define(["require", "exports"], factory);
|
|
14
8
|
}
|
|
15
|
-
|
|
16
|
-
|
|
9
|
+
})(function (require, exports) {
|
|
10
|
+
"use strict";
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.mergeCaptures = exports.merge = exports.escape = void 0;
|
|
13
|
+
function escape(str) {
|
|
14
|
+
return JSON.stringify(str);
|
|
17
15
|
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
Object.keys(b).forEach((key) => {
|
|
29
|
-
if (result[key]) {
|
|
30
|
-
result[key] = merge(result[key], b[key]);
|
|
16
|
+
exports.escape = escape;
|
|
17
|
+
function merge(a, b) {
|
|
18
|
+
if (Array.isArray(a) && Array.isArray(b)) {
|
|
19
|
+
return [...a, ...b];
|
|
20
|
+
}
|
|
21
|
+
else if (Array.isArray(a)) {
|
|
22
|
+
return [...a, b];
|
|
23
|
+
}
|
|
24
|
+
else if (Array.isArray(b)) {
|
|
25
|
+
return [a, ...b];
|
|
31
26
|
}
|
|
32
27
|
else {
|
|
33
|
-
|
|
28
|
+
return [a, b];
|
|
34
29
|
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
30
|
+
}
|
|
31
|
+
exports.merge = merge;
|
|
32
|
+
function mergeCaptures(a, b) {
|
|
33
|
+
const result = {};
|
|
34
|
+
Object.keys(a).forEach((key) => {
|
|
35
|
+
result[key] = a[key];
|
|
36
|
+
});
|
|
37
|
+
Object.keys(b).forEach((key) => {
|
|
38
|
+
if (result[key]) {
|
|
39
|
+
result[key] = merge(result[key], b[key]);
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
result[key] = b[key];
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
return result;
|
|
46
|
+
}
|
|
47
|
+
exports.mergeCaptures = mergeCaptures;
|
|
48
|
+
});
|
package/package.json
CHANGED
|
@@ -1,20 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tarsec",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.5",
|
|
4
4
|
"description": "A parser combinator library for TypeScript, inspired by Parsec.",
|
|
5
5
|
"homepage": "https://github.com/egonSchiele/tarsec",
|
|
6
|
+
|
|
6
7
|
"scripts": {
|
|
7
8
|
"test": "vitest",
|
|
8
9
|
"build": "tsc",
|
|
9
10
|
"start": "cd dist && node index.js"
|
|
10
11
|
},
|
|
11
|
-
"files": [
|
|
12
|
+
"files": [
|
|
13
|
+
"./dist"
|
|
14
|
+
],
|
|
12
15
|
"keywords": [],
|
|
13
16
|
"author": "",
|
|
14
17
|
"license": "ISC",
|
|
15
|
-
"
|
|
16
|
-
"*": "./dist/*"
|
|
17
|
-
},
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
18
19
|
"devDependencies": {
|
|
19
20
|
"@types/node": "^20.11.28",
|
|
20
21
|
"vitest": "^1.4.0"
|