tarsec 0.0.7 → 0.0.9

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.
Files changed (44) hide show
  1. package/README.md +6 -6
  2. package/dist/combinators.d.ts +8 -6
  3. package/dist/combinators.js +130 -57
  4. package/dist/lib/combinators.d.ts +8 -6
  5. package/dist/lib/combinators.js +130 -57
  6. package/dist/lib/parsers.d.ts +1 -1
  7. package/dist/lib/parsers.js +12 -39
  8. package/dist/lib/trace.d.ts +3 -3
  9. package/dist/lib/trace.js +1 -1
  10. package/dist/lib/types.d.ts +25 -9
  11. package/dist/lib/types.js +37 -0
  12. package/dist/parsers.d.ts +1 -1
  13. package/dist/parsers.js +12 -39
  14. package/dist/trace.d.ts +3 -3
  15. package/dist/trace.js +1 -1
  16. package/dist/types.d.ts +25 -9
  17. package/dist/types.js +37 -0
  18. package/package.json +3 -3
  19. package/dist/lib/parsers.test.d.ts +0 -1
  20. package/dist/lib/parsers.test.js +0 -235
  21. package/dist/tests/combinators/many.test.d.ts +0 -1
  22. package/dist/tests/combinators/many.test.js +0 -27
  23. package/dist/tests/combinators/many1.test.d.ts +0 -1
  24. package/dist/tests/combinators/many1.test.js +0 -27
  25. package/dist/tests/combinators/or.test.d.ts +0 -1
  26. package/dist/tests/combinators/or.test.js +0 -31
  27. package/dist/tests/combinators/seq.test.d.ts +0 -1
  28. package/dist/tests/combinators/seq.test.js +0 -41
  29. package/dist/tests/integration/hello_capture.test.d.ts +0 -1
  30. package/dist/tests/integration/hello_capture.test.js +0 -30
  31. package/dist/tests/integration/hello_world.test.d.ts +0 -1
  32. package/dist/tests/integration/hello_world.test.js +0 -23
  33. package/dist/tests/parsers/char.test.d.ts +0 -1
  34. package/dist/tests/parsers/char.test.js +0 -35
  35. package/dist/tests/parsers/noneOf.test.d.ts +0 -1
  36. package/dist/tests/parsers/noneOf.test.js +0 -26
  37. package/dist/tests/parsers/oneOf.test.d.ts +0 -1
  38. package/dist/tests/parsers/oneOf.test.js +0 -26
  39. package/dist/tests/parsers/str.test.d.ts +0 -1
  40. package/dist/tests/parsers/str.test.js +0 -38
  41. package/dist/vitest.config.d.ts +0 -3
  42. package/dist/vitest.config.js +0 -34
  43. package/dist/vitest.globals.d.ts +0 -18
  44. package/dist/vitest.globals.js +0 -25
package/dist/types.d.ts CHANGED
@@ -1,17 +1,33 @@
1
- export type Object = Record<string, string>;
2
- export type ParserSuccess<M, C extends string> = {
1
+ export type PlainObject = Record<string, unknown>;
2
+ export type ParserSuccess<T> = {
3
3
  success: true;
4
- match: M;
5
- captures?: Record<C, any>;
4
+ result: T;
6
5
  rest: string;
7
6
  };
7
+ export type CaptureParserSuccess<T, C extends PlainObject> = ParserSuccess<T> & {
8
+ captures: C;
9
+ };
8
10
  export type ParserFailure = {
9
11
  success: false;
10
12
  rest: string;
11
13
  message: string;
12
14
  };
13
- export type ParserOptions = {
14
- capture: string;
15
- };
16
- export type ParserResult<M, C extends string> = ParserSuccess<M, C> | ParserFailure;
17
- export type Parser<M, C extends string = string> = (input: string) => ParserResult<M, C>;
15
+ export type ParserResult<T> = ParserSuccess<T> | ParserFailure;
16
+ export type CaptureParserResult<T, C extends PlainObject> = CaptureParserSuccess<T, C> | ParserFailure;
17
+ export type Parser<T> = (input: string) => ParserResult<T>;
18
+ export type CaptureParser<T, C extends PlainObject> = (input: string) => CaptureParserResult<T, C>;
19
+ export type GeneralParser<T, C extends PlainObject> = Parser<T> | CaptureParser<T, C>;
20
+ export declare function isCaptureResult<T, C extends PlainObject>(result: ParserResult<T>): result is CaptureParserSuccess<T, C>;
21
+ export declare function success<T>(result: T, rest: string): ParserSuccess<T>;
22
+ export declare function captureSuccess<T, C extends PlainObject>(result: T, rest: string, captures: C): CaptureParserSuccess<T, C>;
23
+ export declare function failure(message: string, rest: string): ParserFailure;
24
+ export type Prettify<T> = {
25
+ [K in keyof T]: T[K];
26
+ } & {};
27
+ export type UnionToIntersection<U> = (U extends any ? (x: U) => void : never) extends (x: infer I) => void ? I : never;
28
+ type ExtractResults<T> = T extends Parser<infer U> ? U : never;
29
+ type ExtractCaptures<T> = T extends CaptureParser<any, infer U> ? U : never;
30
+ type ExtractCaptureParsers<T extends readonly GeneralParser<any, any>[]> = Extract<T[number], CaptureParser<any, any>>;
31
+ export type MergedCaptures<T extends readonly GeneralParser<any, any>[]> = Prettify<UnionToIntersection<ExtractCaptures<ExtractCaptureParsers<T>>>>;
32
+ export type MergedResults<T extends readonly GeneralParser<any, any>[]> = ExtractResults<T[number]>;
33
+ export {};
package/dist/types.js CHANGED
@@ -9,4 +9,41 @@
9
9
  })(function (require, exports) {
10
10
  "use strict";
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.failure = exports.captureSuccess = exports.success = exports.isCaptureResult = void 0;
13
+ function isCaptureResult(result) {
14
+ return "captures" in result;
15
+ }
16
+ exports.isCaptureResult = isCaptureResult;
17
+ function success(result, rest) {
18
+ return { success: true, result, rest };
19
+ }
20
+ exports.success = success;
21
+ function captureSuccess(result, rest, captures) {
22
+ return { success: true, result, rest, captures };
23
+ }
24
+ exports.captureSuccess = captureSuccess;
25
+ function failure(message, rest) {
26
+ return { success: false, message, rest };
27
+ }
28
+ exports.failure = failure;
12
29
  });
30
+ /* export type Merge2<O extends Array<T>, T = any> = Prettify<
31
+ UnionToIntersection<O[number]>
32
+ >;
33
+
34
+ export type NonNullObject<T> = {
35
+ [K in keyof T]: T[K] extends null | undefined ? never : T[K];
36
+ }; */
37
+ /* export type NonNullableUnionOfObjects<T> = T extends object
38
+ ? RemoveNeverKeys<DeepNonNullable<T>>
39
+ : T;
40
+
41
+ export type DeepNonNullable<T> = {
42
+ [P in keyof T]-?: NonNullable<T[P]>;
43
+ }; */
44
+ /* export type FilterNeverKeys<T> = {
45
+ [K in keyof T]: T[K] extends never ? never : K;
46
+ };
47
+ */
48
+ /* type ValueOf<T> = T[keyof T]; */
49
+ /* type RemoveNeverKeys<T> = Pick<T, ValueOf<FilterNeverKeys<T>>>; */
package/package.json CHANGED
@@ -1,9 +1,8 @@
1
1
  {
2
2
  "name": "tarsec",
3
- "version": "0.0.7",
3
+ "version": "0.0.9",
4
4
  "description": "A parser combinator library for TypeScript, inspired by Parsec.",
5
5
  "homepage": "https://github.com/egonSchiele/tarsec",
6
-
7
6
  "scripts": {
8
7
  "test": "vitest",
9
8
  "build": "tsc",
@@ -11,7 +10,7 @@
11
10
  },
12
11
  "files": [
13
12
  "./dist"
14
- ],
13
+ ],
15
14
  "exports": {
16
15
  ".": {
17
16
  "import": "./dist/index.js",
@@ -23,6 +22,7 @@
23
22
  "license": "ISC",
24
23
  "devDependencies": {
25
24
  "@types/node": "^20.11.28",
25
+ "typescript": "^5.4.2",
26
26
  "vitest": "^1.4.0"
27
27
  }
28
28
  }
@@ -1 +0,0 @@
1
- export {};
@@ -1,235 +0,0 @@
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", "vitest", "./parsers", "../vitest.globals.js", "./combinators"], factory);
8
- }
9
- })(function (require, exports) {
10
- "use strict";
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- const vitest_1 = require("vitest");
13
- const parsers_1 = require("./parsers");
14
- const vitest_globals_js_1 = require("../vitest.globals.js");
15
- const combinators_1 = require("./combinators");
16
- vitest_1.test.skip("hello", () => {
17
- (0, vitest_1.describe)("Parser Tests", () => {
18
- (0, vitest_1.describe)("optional parser", () => {
19
- const parser = (0, combinators_1.optional)((0, parsers_1.char)("a"));
20
- (0, vitest_1.it)("should parse the character if it exists", () => {
21
- const result = parser("a");
22
- (0, vitest_1.expect)(result).toEqual((0, vitest_globals_js_1.success)({ rest: "", match: "a" }));
23
- });
24
- (0, vitest_1.it)("should return an empty string if the character is missing", () => {
25
- const result = parser("b");
26
- (0, vitest_1.expect)(result).toEqual((0, vitest_globals_js_1.success)({ rest: "b", match: "" }));
27
- });
28
- (0, vitest_1.it)("should not consume any input if it fails", () => {
29
- // @ts-ignore
30
- const parser2 = (0, combinators_1.optional)((0, combinators_1.seq)((0, combinators_1.many1WithJoin)(parsers_1.letter), (0, parsers_1.char)("!")));
31
- const result1 = parser2("hello!");
32
- (0, vitest_1.expect)(result1).toEqual((0, vitest_globals_js_1.success)({ rest: "", match: "hello!", captures: {} }));
33
- const result2 = parser2("hello");
34
- (0, vitest_1.expect)(result2).toEqual((0, vitest_globals_js_1.success)({ rest: "hello", match: "" }));
35
- });
36
- });
37
- (0, vitest_1.describe)("not parser", () => {
38
- const parser = (0, combinators_1.not)((0, parsers_1.char)("a"));
39
- (0, vitest_1.it)("should fail if the character is present", () => {
40
- const result = parser("a");
41
- (0, vitest_1.expect)(result).toEqual((0, vitest_globals_js_1.failure)({ rest: "a", message: "unexpected match" }));
42
- });
43
- (0, vitest_1.it)("should return an empty string if the character is missing", () => {
44
- const result = parser("b");
45
- (0, vitest_1.expect)(result).toEqual((0, vitest_globals_js_1.success)({ rest: "b", match: "" }));
46
- });
47
- });
48
- (0, vitest_1.describe)("space parser", () => {
49
- (0, vitest_1.it)("should parse a space character", () => {
50
- const result = (0, parsers_1.space)(" ");
51
- (0, vitest_1.expect)(result).toEqual((0, vitest_globals_js_1.success)({ rest: "", match: " " }));
52
- });
53
- (0, vitest_1.it)("should fail if the character is not a space", () => {
54
- const result = (0, parsers_1.space)("a");
55
- (0, vitest_1.expect)(result).toEqual((0, vitest_globals_js_1.failure)({ rest: "a", message: "expected , got a" }));
56
- });
57
- });
58
- (0, vitest_1.describe)("spaces parser", () => {
59
- (0, vitest_1.it)("should parse multiple space characters", () => {
60
- const result = (0, parsers_1.spaces)(" ");
61
- (0, vitest_1.expect)(result).toEqual((0, vitest_globals_js_1.success)({ rest: "", match: " " }));
62
- });
63
- (0, vitest_1.it)("should fail if no space characters found", () => {
64
- const result = (0, parsers_1.spaces)("abc");
65
- (0, vitest_1.expect)(result).toEqual((0, vitest_globals_js_1.failure)({ rest: "abc", message: "expected at least one match" }));
66
- });
67
- });
68
- (0, vitest_1.describe)("digit parser", () => {
69
- (0, vitest_1.it)("should parse a single digit", () => {
70
- const result = (0, parsers_1.digit)("1");
71
- (0, vitest_1.expect)(result).toEqual((0, vitest_globals_js_1.success)({ rest: "", match: "1" }));
72
- });
73
- (0, vitest_1.it)("should fail if the character is not a digit", () => {
74
- const result = (0, parsers_1.digit)("a");
75
- (0, vitest_1.expect)(result).toEqual((0, vitest_globals_js_1.failure)({ rest: "a", message: "expected one of 0123456789" }));
76
- });
77
- });
78
- (0, vitest_1.describe)("letter parser", () => {
79
- (0, vitest_1.it)("should parse a single letter", () => {
80
- const result = (0, parsers_1.letter)("a");
81
- (0, vitest_1.expect)(result).toEqual((0, vitest_globals_js_1.success)({ rest: "", match: "a" }));
82
- });
83
- (0, vitest_1.it)("should fail if the character is not a letter", () => {
84
- const result = (0, parsers_1.letter)("1");
85
- (0, vitest_1.expect)(result).toEqual((0, vitest_globals_js_1.failure)({
86
- rest: "1",
87
- message: "expected one of abcdefghijklmnopqrstuvwxyz",
88
- }));
89
- });
90
- });
91
- (0, vitest_1.describe)("alphanum parser", () => {
92
- (0, vitest_1.it)("should parse a single alphanumeric character", () => {
93
- const result = (0, parsers_1.alphanum)("1");
94
- (0, vitest_1.expect)(result).toEqual((0, vitest_globals_js_1.success)({ rest: "", match: "1" }));
95
- });
96
- (0, vitest_1.it)("should fail if the character is not alphanumeric", () => {
97
- const result = (0, parsers_1.alphanum)("_");
98
- (0, vitest_1.expect)(result).toEqual((0, vitest_globals_js_1.failure)({
99
- rest: "_",
100
- message: "expected one of abcdefghijklmnopqrstuvwxyz0123456789",
101
- }));
102
- });
103
- });
104
- (0, vitest_1.describe)("word parser", () => {
105
- const parser = parsers_1.word;
106
- (0, vitest_1.it)("should parse a single word", () => {
107
- const result = parser("hello");
108
- (0, vitest_1.expect)(result).toEqual((0, vitest_globals_js_1.success)({ rest: "", match: "hello" }));
109
- });
110
- (0, vitest_1.it)("should fail if no word characters found", () => {
111
- const result = parser("123");
112
- (0, vitest_1.expect)(result).toEqual((0, vitest_globals_js_1.failure)({ rest: "123", message: "expected at least one match" }));
113
- });
114
- });
115
- (0, vitest_1.describe)("number parser", () => {
116
- const parser = parsers_1.num;
117
- (0, vitest_1.it)("should parse a single number", () => {
118
- const result = parser("123");
119
- (0, vitest_1.expect)(result).toEqual((0, vitest_globals_js_1.success)({ rest: "", match: "123" }));
120
- });
121
- (0, vitest_1.it)("should fail if no number characters found", () => {
122
- const result = parser("abc");
123
- (0, vitest_1.expect)(result).toEqual((0, vitest_globals_js_1.failure)({ rest: "abc", message: "expected at least one match" }));
124
- });
125
- });
126
- });
127
- (0, vitest_1.describe)("seq parser", () => {
128
- const parser = (0, combinators_1.seq)([(0, parsers_1.char)("a"), (0, parsers_1.char)("b")]);
129
- (0, vitest_1.it)("should parse both characters in sequence", () => {
130
- const result = parser("ab");
131
- (0, vitest_1.expect)(result).toEqual((0, vitest_globals_js_1.success)({ rest: "", match: "ab", captures: {} }));
132
- });
133
- (0, vitest_1.it)("should fail if any of the parsers fail", () => {
134
- const result = parser("ac");
135
- (0, vitest_1.expect)(result).toEqual((0, vitest_globals_js_1.failure)({ rest: "c", message: "expected b, got c" }));
136
- });
137
- });
138
- (0, vitest_1.describe)("seq parser - hello world", () => {
139
- (0, vitest_1.it)("multiple char parsers", () => {
140
- const parser = (0, combinators_1.seq)([
141
- (0, parsers_1.char)("h"),
142
- (0, parsers_1.char)("e"),
143
- (0, parsers_1.char)("l"),
144
- (0, parsers_1.char)("l"),
145
- (0, parsers_1.char)("o"),
146
- ]);
147
- const result = parser("hello world");
148
- (0, vitest_1.expect)(result).toEqual((0, vitest_globals_js_1.success)({ match: "hello", rest: " world", captures: {} }));
149
- });
150
- (0, vitest_1.it)("multiple str parsers", () => {
151
- const parser = (0, combinators_1.seq)([(0, parsers_1.str)("hello"), parsers_1.space, (0, parsers_1.str)("world")]);
152
- const result = parser("hello world");
153
- (0, vitest_1.expect)(result).toEqual((0, vitest_globals_js_1.success)({ match: "hello world", rest: "", captures: {} }));
154
- });
155
- (0, vitest_1.it)("multiple str parsers + capture", () => {
156
- const parser = (0, combinators_1.seq)([(0, parsers_1.str)("hello"), parsers_1.space, (0, combinators_1.capture)((0, parsers_1.str)("world"), "name")]);
157
- const result = parser("hello world");
158
- (0, vitest_1.expect)(result).toEqual((0, vitest_globals_js_1.success)({ match: "hello world", rest: "", captures: { name: "world" } }));
159
- });
160
- });
161
- /* test("quote parser - single quote", () => {
162
- const input = "'";
163
- const result = quote(input);
164
- expect(result).toEqual(success({ rest: "", match: "'", captures: {} }));
165
- });
166
-
167
- test("quote parser - double quote", () => {
168
- const input = '"';
169
- const result = quote(input);
170
- expect(result).toEqual(success({ rest: "", match: '"', captures: {} }));
171
- });
172
-
173
- test("quote parser - invalid quote", () => {
174
- const input = "`";
175
- const result = quote(input);
176
- expect(result).toEqual(
177
- failure({ rest: "`", message: "unexpected end of input" })
178
- );
179
- });
180
-
181
- // Test for anyChar parser
182
- test("anyChar parser - non-empty input", () => {
183
- const input = "abc";
184
- const result = anyChar(input);
185
- expect(result).toEqual(success({ rest: "bc", match: "a", captures: {} }));
186
- });
187
-
188
- test("anyChar parser - empty input", () => {
189
- const input = "";
190
- const result = anyChar(input);
191
- expect(result).toEqual(
192
- failure({ rest: "", message: "unexpected end of input" })
193
- );
194
- });
195
-
196
- // Test for between parser
197
- test("between parser - valid input", () => {
198
- const open = quote;
199
- const close = quote;
200
- const parser = anyChar;
201
- const input = "'abc'";
202
- const result = between(open, close, parser)(input);
203
- expect(result).toEqual(success({ rest: "", match: "a", captures: {} }));
204
- });
205
-
206
- test("between parser - invalid input", () => {
207
- const open = quote;
208
- const close = quote;
209
- const parser = anyChar;
210
- const input = "\"abc'";
211
- const result = between(open, close, parser)(input);
212
- expect(result).toEqual(
213
- failure({ rest: "abc'", message: "unexpected end of input" })
214
- );
215
- });
216
-
217
- // Test for sepBy parser
218
- test("sepBy parser - valid input", () => {
219
- const separator = anyChar;
220
- const parser = anyChar;
221
- const input = "a,b,c";
222
- const result = sepBy(separator, parser)(input);
223
- expect(result).toEqual(success({ rest: "", match: "abc", captures: {} }));
224
- });
225
-
226
- test("sepBy parser - invalid input", () => {
227
- const separator = quote;
228
- const parser = anyChar;
229
- const input = '"a"bc';
230
- const result = sepBy(separator, parser)(input);
231
- expect(result).toEqual(success({ rest: "bc", match: "a", captures: {} }));
232
- });
233
- */
234
- });
235
- });
@@ -1 +0,0 @@
1
- export {};
@@ -1,27 +0,0 @@
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", "@/lib/combinators", "@/lib/parsers", "vitest", "vitest.globals"], factory);
8
- }
9
- })(function (require, exports) {
10
- "use strict";
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- const combinators_1 = require("@/lib/combinators");
13
- const parsers_1 = require("@/lib/parsers");
14
- const vitest_1 = require("vitest");
15
- const vitest_globals_1 = require("vitest.globals");
16
- (0, vitest_1.describe)("many combinator", () => {
17
- const parser = (0, combinators_1.many)(parsers_1.digit);
18
- (0, vitest_1.it)("should parse multiple digits", () => {
19
- const result = parser("1234");
20
- (0, vitest_1.expect)(result).toEqual((0, vitest_globals_1.success)({ rest: "", match: ["1", "2", "3", "4"] }));
21
- });
22
- (0, vitest_1.it)("should return an empty string if no matches found", () => {
23
- const result = parser("abc");
24
- (0, vitest_1.expect)(result).toEqual((0, vitest_globals_1.success)({ rest: "abc", match: [] }));
25
- });
26
- });
27
- });
@@ -1 +0,0 @@
1
- export {};
@@ -1,27 +0,0 @@
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", "@/lib/combinators", "@/lib/parsers", "vitest", "vitest.globals"], factory);
8
- }
9
- })(function (require, exports) {
10
- "use strict";
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- const combinators_1 = require("@/lib/combinators");
13
- const parsers_1 = require("@/lib/parsers");
14
- const vitest_1 = require("vitest");
15
- const vitest_globals_1 = require("vitest.globals");
16
- (0, vitest_1.describe)("many1 combinator", () => {
17
- const parser = (0, combinators_1.many1)(parsers_1.digit);
18
- (0, vitest_1.it)("should parse multiple digits", () => {
19
- const result = parser("1234");
20
- (0, vitest_1.expect)(result).toEqual((0, vitest_globals_1.success)({ rest: "", match: ["1", "2", "3", "4"] }));
21
- });
22
- (0, vitest_1.it)("should fail if no matches found", () => {
23
- const result = parser("abc");
24
- (0, vitest_1.expect)(result).toEqual((0, vitest_globals_1.failure)({ rest: "abc", message: "expected at least one match" }));
25
- });
26
- });
27
- });
@@ -1 +0,0 @@
1
- export {};
@@ -1,31 +0,0 @@
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", "@/lib/combinators", "@/lib/parsers", "vitest", "vitest.globals"], factory);
8
- }
9
- })(function (require, exports) {
10
- "use strict";
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- const combinators_1 = require("@/lib/combinators");
13
- const parsers_1 = require("@/lib/parsers");
14
- const vitest_1 = require("vitest");
15
- const vitest_globals_1 = require("vitest.globals");
16
- (0, vitest_1.describe)("or parser", () => {
17
- const parser = (0, combinators_1.or)([(0, parsers_1.char)("a"), (0, parsers_1.char)("b")]);
18
- (0, vitest_1.it)("should parse the first parser if it succeeds", () => {
19
- const result = parser("a");
20
- (0, vitest_1.expect)(result).toEqual((0, vitest_globals_1.success)({ rest: "", match: "a" }));
21
- });
22
- (0, vitest_1.it)("should parse the second parser if the first one fails", () => {
23
- const result = parser("b");
24
- (0, vitest_1.expect)(result).toEqual((0, vitest_globals_1.success)({ rest: "", match: "b" }));
25
- });
26
- (0, vitest_1.it)("should fail if all parsers fail", () => {
27
- const result = parser("c");
28
- (0, vitest_1.expect)(result).toEqual((0, vitest_globals_1.failure)({ rest: "c", message: "all parsers failed" }));
29
- });
30
- });
31
- });
@@ -1 +0,0 @@
1
- export {};
@@ -1,41 +0,0 @@
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", "@/lib/combinators", "@/lib/parsers", "vitest", "vitest.globals"], factory);
8
- }
9
- })(function (require, exports) {
10
- "use strict";
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- const combinators_1 = require("@/lib/combinators");
13
- const parsers_1 = require("@/lib/parsers");
14
- const vitest_1 = require("vitest");
15
- const vitest_globals_1 = require("vitest.globals");
16
- (0, vitest_1.describe)("seq parser - hello world", () => {
17
- (0, vitest_1.it)("multiple char parsers", () => {
18
- const parser = (0, combinators_1.seq)([(0, parsers_1.char)("h"), (0, parsers_1.char)("e"), (0, parsers_1.char)("l"), (0, parsers_1.char)("l"), (0, parsers_1.char)("o")]);
19
- const result = parser("hello world");
20
- (0, vitest_1.expect)(result).toEqual((0, vitest_globals_1.success)({
21
- match: ["h", "e", "l", "l", "o"],
22
- rest: " world",
23
- captures: {},
24
- }));
25
- });
26
- (0, vitest_1.it)("multiple str parsers", () => {
27
- const parser = (0, combinators_1.seq)([(0, parsers_1.str)("hello"), parsers_1.space, (0, parsers_1.str)("world")]);
28
- const result = parser("hello world");
29
- (0, vitest_1.expect)(result).toEqual((0, vitest_globals_1.success)({ match: ["hello", " ", "world"], rest: "", captures: {} }));
30
- });
31
- (0, vitest_1.it)("multiple str parsers + capture", () => {
32
- const parser = (0, combinators_1.seq)([(0, parsers_1.str)("hello"), parsers_1.space, (0, combinators_1.capture)((0, parsers_1.str)("world"), "name")]);
33
- const result = parser("hello world");
34
- (0, vitest_1.expect)(result).toEqual((0, vitest_globals_1.success)({
35
- match: ["hello", " ", "world"],
36
- rest: "",
37
- captures: { name: "world" },
38
- }));
39
- });
40
- });
41
- });
@@ -1 +0,0 @@
1
- export {};
@@ -1,30 +0,0 @@
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", "@/lib/combinators", "@/lib/parsers", "vitest"], factory);
8
- }
9
- })(function (require, exports) {
10
- "use strict";
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- const combinators_1 = require("@/lib/combinators");
13
- const parsers_1 = require("@/lib/parsers");
14
- const vitest_1 = require("vitest");
15
- (0, vitest_1.describe)("hello world", () => {
16
- (0, vitest_1.it)("parses + captures name", () => {
17
- const parser = (0, combinators_1.seq)([
18
- (0, parsers_1.str)("hello"),
19
- parsers_1.space,
20
- (0, combinators_1.capture)((0, combinators_1.many1WithJoin)((0, parsers_1.noneOf)("!")), "name"),
21
- (0, parsers_1.char)("!"),
22
- ]);
23
- const result = parser("hello adit!");
24
- (0, vitest_1.expect)(result.success).toEqual(true);
25
- (0, vitest_1.expect)(result.captures).toEqual({
26
- name: "adit",
27
- });
28
- });
29
- });
30
- });
@@ -1 +0,0 @@
1
- export {};
@@ -1,23 +0,0 @@
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", "@/lib/combinators", "@/lib/parsers", "vitest", "vitest.globals"], factory);
8
- }
9
- })(function (require, exports) {
10
- "use strict";
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- const combinators_1 = require("@/lib/combinators");
13
- const parsers_1 = require("@/lib/parsers");
14
- const vitest_1 = require("vitest");
15
- const vitest_globals_1 = require("vitest.globals");
16
- (0, vitest_1.describe)("hello world", () => {
17
- (0, vitest_1.it)("parses", () => {
18
- const parser = (0, combinators_1.seq)([(0, parsers_1.str)("hello"), parsers_1.space, (0, parsers_1.str)("world")]);
19
- const result = parser("hello world");
20
- (0, vitest_1.expect)(result).toEqual((0, vitest_globals_1.success)({ match: ["hello", " ", "world"], rest: "", captures: {} }));
21
- });
22
- });
23
- });
@@ -1 +0,0 @@
1
- export {};
@@ -1,35 +0,0 @@
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", "@/lib/parsers.js", "vitest", "vitest.globals"], factory);
8
- }
9
- })(function (require, exports) {
10
- "use strict";
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- const parsers_js_1 = require("@/lib/parsers.js");
13
- const vitest_1 = require("vitest");
14
- const vitest_globals_1 = require("vitest.globals");
15
- (0, vitest_1.describe)("char parser", () => {
16
- (0, vitest_1.it)("should parse correct character", () => {
17
- const parser = (0, parsers_js_1.char)("a");
18
- (0, vitest_1.expect)(parser("abc")).toEqual((0, vitest_globals_1.success)({ match: "a", rest: "bc" }));
19
- });
20
- (0, vitest_1.it)("should handle unexpected end of input", () => {
21
- const parser = (0, parsers_js_1.char)("a");
22
- (0, vitest_1.expect)(parser("")).toEqual((0, vitest_globals_1.failure)({
23
- rest: "",
24
- message: "unexpected end of input",
25
- }));
26
- });
27
- (0, vitest_1.it)("should handle incorrect character", () => {
28
- const parser = (0, parsers_js_1.char)("a");
29
- (0, vitest_1.expect)(parser("bcd")).toEqual((0, vitest_globals_1.failure)({
30
- rest: "bcd",
31
- message: "expected a, got b",
32
- }));
33
- });
34
- });
35
- });
@@ -1 +0,0 @@
1
- export {};
@@ -1,26 +0,0 @@
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", "@/lib/parsers", "vitest", "vitest.globals"], factory);
8
- }
9
- })(function (require, exports) {
10
- "use strict";
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- const parsers_1 = require("@/lib/parsers");
13
- const vitest_1 = require("vitest");
14
- const vitest_globals_1 = require("vitest.globals");
15
- (0, vitest_1.describe)("noneOf parser", () => {
16
- const parser = (0, parsers_1.noneOf)("xyz");
17
- (0, vitest_1.it)("should parse any character that is not one of the given characters", () => {
18
- const result = parser("a");
19
- (0, vitest_1.expect)(result).toEqual((0, vitest_globals_1.success)({ rest: "", match: "a" }));
20
- });
21
- (0, vitest_1.it)("should fail if any of the given characters match", () => {
22
- const result = parser("x");
23
- (0, vitest_1.expect)(result).toEqual((0, vitest_globals_1.failure)({ rest: "x", message: "expected none of xyz" }));
24
- });
25
- });
26
- });
@@ -1 +0,0 @@
1
- export {};