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 CHANGED
@@ -19,6 +19,8 @@ npm install tarsec
19
19
  ## Hello world
20
20
 
21
21
  ```ts
22
+ import { str, char } from "tarsec";
23
+
22
24
  // define a parser
23
25
  const parser = seq([
24
26
  str("hello"),
@@ -1,189 +1,199 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- 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;
4
- const trace_1 = require("./trace");
5
- const utils_1 = require("./utils");
6
- function many(parser) {
7
- return (0, trace_1.trace)("many", (input) => {
8
- let match = [];
9
- let rest = input;
10
- while (true) {
11
- let result = parser(rest);
12
- if (!result.success) {
13
- return { success: true, match, 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;
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
- match.push(result.match);
16
- rest = result.rest;
17
- }
18
- });
19
- }
20
- exports.many = many;
21
- function many1(parser) {
22
- return (0, trace_1.trace)(`many1`, (input) => {
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: "unexpected match",
40
+ message: "expected at least one match",
78
41
  };
79
- }
80
- return { success: true, match: null, rest: input };
81
- });
82
- }
83
- exports.not = not;
84
- function between(open, close, parser) {
85
- return (input) => {
86
- const result1 = open(input);
87
- if (!result1.success) {
88
- return result1;
89
- }
90
- const parserResult = parser(result1.rest);
91
- if (!parserResult.success) {
92
- return parserResult;
93
- }
94
- const result2 = close(parserResult.rest);
95
- if (!result2.success) {
96
- return result2;
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
- rest = sepResult.rest;
118
- }
119
- };
120
- }
121
- exports.sepBy = sepBy;
122
- function seq(parsers, name = "") {
123
- return (0, trace_1.trace)(`seq(${name})`, (input) => {
124
- let match = [];
125
- let rest = input;
126
- // @ts-ignore
127
- let captures = {};
128
- for (let parser of parsers) {
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.push(result.match);
134
- rest = result.rest;
135
- if (result.captures) {
136
- captures = (0, utils_1.mergeCaptures)(captures, result.captures);
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
- return { success: true, match, rest, captures };
140
- });
141
- }
142
- exports.seq = seq;
143
- function capture(parser, name) {
144
- return (0, trace_1.trace)(`captures(${(0, utils_1.escape)(name)})`, (input) => {
145
- let result = parser(input);
146
- if (result.success) {
147
- const captures = {
148
- [name]: result.match,
149
- };
150
- return Object.assign(Object.assign({}, result), { captures: (0, utils_1.mergeCaptures)(result.captures || {}, captures) });
151
- }
152
- return result;
153
- });
154
- }
155
- exports.capture = capture;
156
- function captureCaptures(parser, name) {
157
- return (0, trace_1.trace)(`captures(${(0, utils_1.escape)(name)})`, (input) => {
158
- let result = parser(input);
159
- if (result.success) {
160
- const captures = {
161
- [name]: result.captures,
162
- };
163
- return Object.assign(Object.assign({}, result), { captures: (0, utils_1.mergeCaptures)(result.captures || {}, captures) });
164
- }
165
- return result;
166
- });
167
- }
168
- exports.captureCaptures = captureCaptures;
169
- function shapeCaptures(parser, func, name) {
170
- return (0, trace_1.trace)(`captures(${(0, utils_1.escape)(name)})`, (input) => {
171
- let result = parser(input);
172
- if (result.success) {
173
- const captures = result.captures || {};
174
- return Object.assign(Object.assign({}, result), { captures: func(captures) });
175
- }
176
- return result;
177
- });
178
- }
179
- exports.shapeCaptures = shapeCaptures;
180
- function transform(parser, transformerFunc) {
181
- return (0, trace_1.trace)(`transform(${transformerFunc})`, (input) => {
182
- let result = parser(input);
183
- if (result.success) {
184
- return Object.assign(Object.assign({}, result), { match: transformerFunc(result.match) });
185
- }
186
- return result;
187
- });
188
- }
189
- exports.transform = transform;
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
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./parsers"), exports);
18
- __exportStar(require("./combinators"), exports);
19
- __exportStar(require("./trace"), exports);
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
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- 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;
4
- const combinators_1 = require("./combinators");
5
- const trace_1 = require("./trace");
6
- const utils_1 = require("./utils");
7
- function char(c) {
8
- return (0, trace_1.trace)(`char(${(0, utils_1.escape)(c)})`, (input) => {
9
- if (input.length === 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", "./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: "unexpected end of input",
31
+ message: `expected ${c}, got ${input[0]}`,
14
32
  };
15
- }
16
- if (input[0] === c) {
17
- return { success: true, match: c, rest: input.slice(1) };
18
- }
19
- return {
20
- success: false,
21
- rest: input,
22
- message: `expected ${c}, got ${input[0]}`,
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: "unexpected end of input",
44
+ message: `expected ${s}, got ${input.substring(0, s.length)}`,
47
45
  };
48
- }
49
- const c = input[0];
50
- if (chars.includes(c)) {
51
- return char(c)(input);
52
- }
53
- return {
54
- success: false,
55
- rest: input,
56
- message: `expected one of ${(0, utils_1.escape)(chars)}, got ${c}`,
57
- };
58
- });
59
- }
60
- exports.oneOf = oneOf;
61
- function noneOf(chars) {
62
- return (0, trace_1.trace)(`noneOf(${(0, utils_1.escape)(chars)})`, (input) => {
63
- if (input.length === 0) {
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: "unexpected end of input",
65
+ message: `expected one of ${(0, utils_1.escape)(chars)}, got ${c}`,
68
66
  };
69
- }
70
- if (chars.includes(input[0])) {
71
- return {
72
- success: false,
73
- rest: input,
74
- message: `expected none of ${chars}`,
75
- };
76
- }
77
- return char(input[0])(input);
78
- });
79
- }
80
- exports.noneOf = noneOf;
81
- function anyChar(input) {
82
- return (0, trace_1.trace)("anyChar", (input) => {
83
- if (input.length === 0) {
84
- return {
85
- success: false,
86
- rest: input,
87
- message: "unexpected end of input",
88
- };
89
- }
90
- return { success: true, match: input[0], rest: input.slice(1) };
91
- });
92
- }
93
- exports.anyChar = anyChar;
94
- exports.space = oneOf(" \t\n\r");
95
- exports.spaces = (0, combinators_1.many1WithJoin)(exports.space);
96
- exports.digit = oneOf("0123456789");
97
- exports.letter = oneOf("abcdefghijklmnopqrstuvwxyz");
98
- exports.alphanum = oneOf("abcdefghijklmnopqrstuvwxyz0123456789");
99
- exports.word = (0, combinators_1.many1WithJoin)(exports.letter);
100
- exports.num = (0, combinators_1.many1WithJoin)(exports.digit);
101
- exports.quote = oneOf(`'"`);
102
- exports.tab = char("\t");
103
- exports.newline = char("\n");
104
- exports.quotedString = (0, combinators_1.transform)((0, combinators_1.seq)([exports.quote, exports.word, exports.quote], "quotedString"), (x) => x.join(""));
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
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.trace = exports.resultToString = void 0;
4
- const utils_1 = require("./utils");
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
- return `❌ ${name} -- message: ${(0, utils_1.escape)(result.message)}, rest: ${(0, utils_1.escape)(result.rest)}`;
11
- }
12
- exports.resultToString = resultToString;
13
- let level = 0;
14
- function trace(name, parser) {
15
- return (input) => {
16
- if (process.env.DEBUG) {
17
- console.log(" ".repeat(level) + `🔍 ${name} -- input: ${(0, utils_1.escape)(input)}`);
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
- level += STEP;
20
- const result = parser(input);
21
- level -= STEP;
22
- if (process.env.DEBUG) {
23
- console.log(" ".repeat(level) + resultToString(name, result));
24
- if (result.success && result.captures) {
25
- console.log(" ".repeat(level) +
26
- `⭐ ${name} -- captures: ${JSON.stringify(result.captures)}`);
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
- return result;
30
- };
31
- }
32
- exports.trace = trace;
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
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
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
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.mergeCaptures = exports.merge = exports.escape = void 0;
4
- function escape(str) {
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 (Array.isArray(a)) {
13
- return [...a, b];
6
+ else if (typeof define === "function" && define.amd) {
7
+ define(["require", "exports"], factory);
14
8
  }
15
- else if (Array.isArray(b)) {
16
- return [a, ...b];
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
- else {
19
- return [a, b];
20
- }
21
- }
22
- exports.merge = merge;
23
- function mergeCaptures(a, b) {
24
- const result = {};
25
- Object.keys(a).forEach((key) => {
26
- result[key] = a[key];
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
- result[key] = b[key];
28
+ return [a, b];
34
29
  }
35
- });
36
- return result;
37
- }
38
- exports.mergeCaptures = mergeCaptures;
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",
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": ["./dist"],
12
+ "files": [
13
+ "./dist"
14
+ ],
12
15
  "keywords": [],
13
16
  "author": "",
14
17
  "license": "ISC",
15
- "exports": {
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"