parser-combinators 1.2.3 → 1.2.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/dist/parser.js CHANGED
@@ -12,7 +12,7 @@ const types_1 = require("./types");
12
12
  function ParseText(text, parser, path = '') {
13
13
  const res = parser({ text, path, index: 0 });
14
14
  if ((0, types_1.isFailure)(res)) {
15
- throw new types_1.ParseError(`Parse error, expected ${[...res.history].pop()} at char ${res.ctx.index}`, text, res.ctx.index, res.history);
15
+ throw new types_1.ParseError(`Parse error, expected ${res.history[res.history.length - 1]} at char ${res.ctx.index}`, text, res.ctx.index, res.history);
16
16
  }
17
17
  if (res.ctx.index !== text.length) {
18
18
  throw new types_1.ParseError(`Parse error, expected end of text at char ${res.ctx.index}`, text, res.ctx.index, []);
@@ -22,8 +22,9 @@ function any(...parsers) {
22
22
  for (const parser of parsers) {
23
23
  const res = parser(ctx);
24
24
  if ((0, types_1.isFailure)(res)) {
25
- if (res.history.includes('surely')) {
26
- return (0, types_1.failure)(res.ctx, res.expected, res.history.filter(h => h !== 'surely'));
25
+ const surelyIndex = res.history.findIndex(h => h === 'surely');
26
+ if (surelyIndex >= 0) {
27
+ return (0, types_1.failure)(res.ctx, res.expected, ['any', ...res.history.slice(0, surelyIndex), ...res.history.slice(surelyIndex + 1)]);
27
28
  }
28
29
  expected.push(res);
29
30
  }
@@ -14,6 +14,10 @@ function many(parser) {
14
14
  while (true) {
15
15
  const res = parser(ctx);
16
16
  if ((0, types_1.isFailure)(res)) {
17
+ const surelyIndex = res.history.findIndex(h => h === 'surely');
18
+ if (surelyIndex >= 0) {
19
+ return (0, types_1.failure)(res.ctx, res.expected, ['many', ...res.history.slice(0, surelyIndex), ...res.history.slice(surelyIndex + 1)]);
20
+ }
17
21
  return (0, types_1.success)(ctx, results);
18
22
  }
19
23
  ctx = res.ctx;
@@ -30,6 +34,10 @@ function zeroOrMany(item, separator = undefined) {
30
34
  const results = [];
31
35
  const res = item(ctx);
32
36
  if ((0, types_1.isFailure)(res)) {
37
+ const surelyIndex = res.history.findIndex(h => h === 'surely');
38
+ if (surelyIndex >= 0) {
39
+ return (0, types_1.failure)(res.ctx, res.expected, ['zeroOrMany', ...res.history.slice(0, surelyIndex), ...res.history.slice(surelyIndex + 1)]);
40
+ }
33
41
  return (0, types_1.success)(ctx, results);
34
42
  }
35
43
  ctx = res.ctx;
@@ -37,10 +45,18 @@ function zeroOrMany(item, separator = undefined) {
37
45
  while (true) {
38
46
  const resSep = separator(ctx);
39
47
  if ((0, types_1.isFailure)(resSep)) {
48
+ const surelyIndex = resSep.history.findIndex(h => h === 'surely');
49
+ if (surelyIndex >= 0) {
50
+ return (0, types_1.failure)(resSep.ctx, resSep.expected, ['zeroOrMany', ...resSep.history.slice(0, surelyIndex), ...resSep.history.slice(surelyIndex + 1)]);
51
+ }
40
52
  return (0, types_1.success)(ctx, results);
41
53
  }
42
54
  const res = item(resSep.ctx);
43
55
  if ((0, types_1.isFailure)(res)) {
56
+ const surelyIndex = res.history.findIndex(h => h === 'surely');
57
+ if (surelyIndex >= 0) {
58
+ return (0, types_1.failure)(res.ctx, res.expected, ['zeroOrMany', ...res.history.slice(0, surelyIndex), ...res.history.slice(surelyIndex + 1)]);
59
+ }
44
60
  return (0, types_1.success)(ctx, results);
45
61
  }
46
62
  ctx = res.ctx;
@@ -66,10 +82,18 @@ function oneOrMany(item, separator = undefined) {
66
82
  while (true) {
67
83
  const resSep = separator(ctx);
68
84
  if ((0, types_1.isFailure)(resSep)) {
85
+ const surelyIndex = resSep.history.findIndex(h => h === 'surely');
86
+ if (surelyIndex >= 0) {
87
+ return (0, types_1.failure)(resSep.ctx, resSep.expected, ['oneOrMany', ...resSep.history.slice(0, surelyIndex), ...resSep.history.slice(surelyIndex + 1)]);
88
+ }
69
89
  return (0, types_1.success)(ctx, results);
70
90
  }
71
91
  const res = item(resSep.ctx);
72
92
  if ((0, types_1.isFailure)(res)) {
93
+ const surelyIndex = res.history.findIndex(h => h === 'surely');
94
+ if (surelyIndex >= 0) {
95
+ return (0, types_1.failure)(res.ctx, res.expected, ['oneOrMany', ...res.history.slice(0, surelyIndex), ...res.history.slice(surelyIndex + 1)]);
96
+ }
73
97
  return (0, types_1.success)(ctx, results);
74
98
  }
75
99
  ctx = res.ctx;
@@ -88,6 +112,10 @@ function oneOrMany(item, separator = undefined) {
88
112
  while (true) {
89
113
  const res = item(ctx);
90
114
  if ((0, types_1.isFailure)(res)) {
115
+ const surelyIndex = res.history.findIndex(h => h === 'surely');
116
+ if (surelyIndex >= 0) {
117
+ return (0, types_1.failure)(res.ctx, res.expected, ['oneOrMany', ...res.history.slice(0, surelyIndex), ...res.history.slice(surelyIndex + 1)]);
118
+ }
91
119
  return (0, types_1.success)(ctx, results);
92
120
  }
93
121
  ctx = res.ctx;
@@ -109,10 +137,18 @@ function oneOrManyRed(item, separator, reducer) {
109
137
  while (true) {
110
138
  const resSep = separator(ctx);
111
139
  if ((0, types_1.isFailure)(resSep)) {
140
+ const surelyIndex = resSep.history.findIndex(h => h === 'surely');
141
+ if (surelyIndex >= 0) {
142
+ return (0, types_1.failure)(resSep.ctx, resSep.expected, ['oneOrManyRed', ...resSep.history.slice(0, surelyIndex), ...resSep.history.slice(surelyIndex + 1)]);
143
+ }
112
144
  return (0, types_1.success)(ctx, result);
113
145
  }
114
146
  const res = item(resSep.ctx);
115
147
  if ((0, types_1.isFailure)(res)) {
148
+ const surelyIndex = res.history.findIndex(h => h === 'surely');
149
+ if (surelyIndex >= 0) {
150
+ return (0, types_1.failure)(res.ctx, res.expected, ['oneOrManyRed', ...res.history.slice(0, surelyIndex), ...res.history.slice(surelyIndex + 1)]);
151
+ }
116
152
  return (0, types_1.success)(ctx, result);
117
153
  }
118
154
  ctx = res.ctx;
@@ -9,7 +9,7 @@ import { isFailure, ParseError } from './types';
9
9
  export function ParseText(text, parser, path = '') {
10
10
  const res = parser({ text, path, index: 0 });
11
11
  if (isFailure(res)) {
12
- throw new ParseError(`Parse error, expected ${[...res.history].pop()} at char ${res.ctx.index}`, text, res.ctx.index, res.history);
12
+ throw new ParseError(`Parse error, expected ${res.history[res.history.length - 1]} at char ${res.ctx.index}`, text, res.ctx.index, res.history);
13
13
  }
14
14
  if (res.ctx.index !== text.length) {
15
15
  throw new ParseError(`Parse error, expected end of text at char ${res.ctx.index}`, text, res.ctx.index, []);
@@ -19,8 +19,9 @@ export function any(...parsers) {
19
19
  for (const parser of parsers) {
20
20
  const res = parser(ctx);
21
21
  if (isFailure(res)) {
22
- if (res.history.includes('surely')) {
23
- return failure(res.ctx, res.expected, res.history.filter(h => h !== 'surely'));
22
+ const surelyIndex = res.history.findIndex(h => h === 'surely');
23
+ if (surelyIndex >= 0) {
24
+ return failure(res.ctx, res.expected, ['any', ...res.history.slice(0, surelyIndex), ...res.history.slice(surelyIndex + 1)]);
24
25
  }
25
26
  expected.push(res);
26
27
  }
@@ -8,6 +8,10 @@ export function many(parser) {
8
8
  while (true) {
9
9
  const res = parser(ctx);
10
10
  if (isFailure(res)) {
11
+ const surelyIndex = res.history.findIndex(h => h === 'surely');
12
+ if (surelyIndex >= 0) {
13
+ return failure(res.ctx, res.expected, ['many', ...res.history.slice(0, surelyIndex), ...res.history.slice(surelyIndex + 1)]);
14
+ }
11
15
  return success(ctx, results);
12
16
  }
13
17
  ctx = res.ctx;
@@ -24,6 +28,10 @@ export function zeroOrMany(item, separator = undefined) {
24
28
  const results = [];
25
29
  const res = item(ctx);
26
30
  if (isFailure(res)) {
31
+ const surelyIndex = res.history.findIndex(h => h === 'surely');
32
+ if (surelyIndex >= 0) {
33
+ return failure(res.ctx, res.expected, ['zeroOrMany', ...res.history.slice(0, surelyIndex), ...res.history.slice(surelyIndex + 1)]);
34
+ }
27
35
  return success(ctx, results);
28
36
  }
29
37
  ctx = res.ctx;
@@ -31,10 +39,18 @@ export function zeroOrMany(item, separator = undefined) {
31
39
  while (true) {
32
40
  const resSep = separator(ctx);
33
41
  if (isFailure(resSep)) {
42
+ const surelyIndex = resSep.history.findIndex(h => h === 'surely');
43
+ if (surelyIndex >= 0) {
44
+ return failure(resSep.ctx, resSep.expected, ['zeroOrMany', ...resSep.history.slice(0, surelyIndex), ...resSep.history.slice(surelyIndex + 1)]);
45
+ }
34
46
  return success(ctx, results);
35
47
  }
36
48
  const res = item(resSep.ctx);
37
49
  if (isFailure(res)) {
50
+ const surelyIndex = res.history.findIndex(h => h === 'surely');
51
+ if (surelyIndex >= 0) {
52
+ return failure(res.ctx, res.expected, ['zeroOrMany', ...res.history.slice(0, surelyIndex), ...res.history.slice(surelyIndex + 1)]);
53
+ }
38
54
  return success(ctx, results);
39
55
  }
40
56
  ctx = res.ctx;
@@ -60,10 +76,18 @@ export function oneOrMany(item, separator = undefined) {
60
76
  while (true) {
61
77
  const resSep = separator(ctx);
62
78
  if (isFailure(resSep)) {
79
+ const surelyIndex = resSep.history.findIndex(h => h === 'surely');
80
+ if (surelyIndex >= 0) {
81
+ return failure(resSep.ctx, resSep.expected, ['oneOrMany', ...resSep.history.slice(0, surelyIndex), ...resSep.history.slice(surelyIndex + 1)]);
82
+ }
63
83
  return success(ctx, results);
64
84
  }
65
85
  const res = item(resSep.ctx);
66
86
  if (isFailure(res)) {
87
+ const surelyIndex = res.history.findIndex(h => h === 'surely');
88
+ if (surelyIndex >= 0) {
89
+ return failure(res.ctx, res.expected, ['oneOrMany', ...res.history.slice(0, surelyIndex), ...res.history.slice(surelyIndex + 1)]);
90
+ }
67
91
  return success(ctx, results);
68
92
  }
69
93
  ctx = res.ctx;
@@ -82,6 +106,10 @@ export function oneOrMany(item, separator = undefined) {
82
106
  while (true) {
83
107
  const res = item(ctx);
84
108
  if (isFailure(res)) {
109
+ const surelyIndex = res.history.findIndex(h => h === 'surely');
110
+ if (surelyIndex >= 0) {
111
+ return failure(res.ctx, res.expected, ['oneOrMany', ...res.history.slice(0, surelyIndex), ...res.history.slice(surelyIndex + 1)]);
112
+ }
85
113
  return success(ctx, results);
86
114
  }
87
115
  ctx = res.ctx;
@@ -103,10 +131,18 @@ export function oneOrManyRed(item, separator, reducer) {
103
131
  while (true) {
104
132
  const resSep = separator(ctx);
105
133
  if (isFailure(resSep)) {
134
+ const surelyIndex = resSep.history.findIndex(h => h === 'surely');
135
+ if (surelyIndex >= 0) {
136
+ return failure(resSep.ctx, resSep.expected, ['oneOrManyRed', ...resSep.history.slice(0, surelyIndex), ...resSep.history.slice(surelyIndex + 1)]);
137
+ }
106
138
  return success(ctx, result);
107
139
  }
108
140
  const res = item(resSep.ctx);
109
141
  if (isFailure(res)) {
142
+ const surelyIndex = res.history.findIndex(h => h === 'surely');
143
+ if (surelyIndex >= 0) {
144
+ return failure(res.ctx, res.expected, ['oneOrManyRed', ...res.history.slice(0, surelyIndex), ...res.history.slice(surelyIndex + 1)]);
145
+ }
110
146
  return success(ctx, result);
111
147
  }
112
148
  ctx = res.ctx;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "parser-combinators",
3
- "version": "1.2.3",
3
+ "version": "1.2.5",
4
4
  "license": "ISC",
5
5
  "maintainers": [
6
6
  "Micha_i"