@bablr/helpers 0.1.3 → 0.1.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/lib/grammar.js CHANGED
@@ -101,7 +101,7 @@ export class Coroutine {
101
101
  finalize() {
102
102
  // ensures failures can be logged!
103
103
  if (!this.done) {
104
- this.throw('failure');
104
+ this.return();
105
105
  }
106
106
  }
107
107
  }
@@ -1,30 +1,37 @@
1
1
  import { interpolateString as _interpolateString } from "@bablr/boot-helpers/template";
2
2
  import { interpolateArray as _interpolateArray } from "@bablr/boot-helpers/template";
3
3
  import * as _t from "@bablr/boot-helpers/types";
4
- export function* List({
5
- props: {
4
+ export function* List(props, s, ctx) {
5
+ const {
6
6
  element,
7
7
  separator,
8
8
  allowHoles = false,
9
9
  allowTrailingSeparator = true
10
- }
11
- }) {
10
+ } = ctx.unbox(props);
12
11
  let sep, it;
13
12
  for (;;) {
14
13
  it = yield _t.node("Instruction", "Call", [_t.ref`verb`, _t.ref`arguments`], {
15
14
  verb: _t.node("Instruction", "Identifier", [_t.lit`eatMatch`], {}, {}),
16
- arguments: _t.node("Instruction", "Tuple", [_t.ref`open`, _t.ref`values[]`, _t.ref`close`], {
15
+ arguments: _t.node("Instruction", "Tuple", [_t.ref`open`, _t.ref`values[]`, _t.trivia` `, _t.ref`values[]`, _t.ref`close`], {
17
16
  open: _t.node("Instruction", "Punctuator", [_t.lit`(`], {}, {}),
18
- values: [..._interpolateArray(element)],
17
+ values: [..._interpolateArray(element), _t.node("String", "String", [_t.ref`open`, _t.ref`content`, _t.ref`close`], {
18
+ open: _t.node("String", "Punctuator", [_t.lit`'`], {}, {}),
19
+ content: _t.node("String", "Content", [_t.lit`elements[]`], {}, {}),
20
+ close: _t.node("String", "Punctuator", [_t.lit`'`], {}, {})
21
+ }, {})],
19
22
  close: _t.node("Instruction", "Punctuator", [_t.lit`)`], {}, {})
20
23
  }, {})
21
24
  }, {});
22
25
  if (it || allowTrailingSeparator) {
23
26
  sep = yield _t.node("Instruction", "Call", [_t.ref`verb`, _t.ref`arguments`], {
24
27
  verb: _t.node("Instruction", "Identifier", [_t.lit`eatMatch`], {}, {}),
25
- arguments: _t.node("Instruction", "Tuple", [_t.ref`open`, _t.ref`values[]`, _t.ref`close`], {
28
+ arguments: _t.node("Instruction", "Tuple", [_t.ref`open`, _t.ref`values[]`, _t.trivia` `, _t.ref`values[]`, _t.ref`close`], {
26
29
  open: _t.node("Instruction", "Punctuator", [_t.lit`(`], {}, {}),
27
- values: [..._interpolateArray(separator)],
30
+ values: [..._interpolateArray(separator), _t.node("String", "String", [_t.ref`open`, _t.ref`content`, _t.ref`close`], {
31
+ open: _t.node("String", "Punctuator", [_t.lit`'`], {}, {}),
32
+ content: _t.node("String", "Content", [_t.lit`separators[]`], {}, {}),
33
+ close: _t.node("String", "Punctuator", [_t.lit`'`], {}, {})
34
+ }, {})],
28
35
  close: _t.node("Instruction", "Punctuator", [_t.lit`)`], {}, {})
29
36
  }, {})
30
37
  }, {});
@@ -32,12 +39,8 @@ export function* List({
32
39
  if (!(sep || allowHoles)) break;
33
40
  }
34
41
  }
35
- export function* Any({
36
- props: {
37
- matchers
38
- }
39
- }) {
40
- for (const matcher of matchers) {
42
+ export function* Any(matchers, s, ctx) {
43
+ for (const matcher of ctx.unbox(matchers)) {
41
44
  if (yield _t.node("Instruction", "Call", [_t.ref`verb`, _t.ref`arguments`], {
42
45
  verb: _t.node("Instruction", "Identifier", [_t.lit`eatMatch`], {}, {}),
43
46
  arguments: _t.node("Instruction", "Tuple", [_t.ref`open`, _t.ref`values[]`, _t.ref`close`], {
@@ -48,12 +51,8 @@ export function* Any({
48
51
  }, {})) break;
49
52
  }
50
53
  }
51
- export function* All({
52
- props: {
53
- matchers
54
- }
55
- }) {
56
- for (const matcher of matchers) {
54
+ export function* All(matchers, s, ctx) {
55
+ for (const matcher of ctx.unbox(matchers)) {
57
56
  yield _t.node("Instruction", "Call", [_t.ref`verb`, _t.ref`arguments`], {
58
57
  verb: _t.node("Instruction", "Identifier", [_t.lit`eat`], {}, {}),
59
58
  arguments: _t.node("Instruction", "Tuple", [_t.ref`open`, _t.ref`values[]`, _t.ref`close`], {
@@ -64,19 +63,59 @@ export function* All({
64
63
  }, {});
65
64
  }
66
65
  }
67
- export function* Optional({
68
- props: {
69
- matchers
66
+ export function* Match(cases, s, ctx) {
67
+ for (const case_ of ctx.unbox(cases)) {
68
+ const {
69
+ 0: matcher,
70
+ 1: guard
71
+ } = ctx.unbox(case_);
72
+ if (yield _t.node("Instruction", "Call", [_t.ref`verb`, _t.ref`arguments`], {
73
+ verb: _t.node("Instruction", "Identifier", [_t.lit`match`], {}, {}),
74
+ arguments: _t.node("Instruction", "Tuple", [_t.ref`open`, _t.ref`values[]`, _t.ref`close`], {
75
+ open: _t.node("Instruction", "Punctuator", [_t.lit`(`], {}, {}),
76
+ values: [..._interpolateArray(guard)],
77
+ close: _t.node("Instruction", "Punctuator", [_t.lit`)`], {}, {})
78
+ }, {})
79
+ }, {})) {
80
+ yield _t.node("Instruction", "Call", [_t.ref`verb`, _t.ref`arguments`], {
81
+ verb: _t.node("Instruction", "Identifier", [_t.lit`eat`], {}, {}),
82
+ arguments: _t.node("Instruction", "Tuple", [_t.ref`open`, _t.ref`values[]`, _t.ref`close`], {
83
+ open: _t.node("Instruction", "Punctuator", [_t.lit`(`], {}, {}),
84
+ values: [..._interpolateArray(matcher)],
85
+ close: _t.node("Instruction", "Punctuator", [_t.lit`)`], {}, {})
86
+ }, {})
87
+ }, {});
88
+ break;
89
+ }
70
90
  }
71
- }) {
72
- if (matchers.length > 1) {
91
+ }
92
+ export function* Punctuator(obj, s, ctx) {
93
+ const {
94
+ value,
95
+ attrs
96
+ } = ctx.unbox(obj);
97
+ yield _t.node("Instruction", "Call", [_t.ref`verb`, _t.ref`arguments`], {
98
+ verb: _t.node("Instruction", "Identifier", [_t.lit`eat`], {}, {}),
99
+ arguments: _t.node("Instruction", "Tuple", [_t.ref`open`, _t.ref`values[]`, _t.ref`close`], {
100
+ open: _t.node("Instruction", "Punctuator", [_t.lit`(`], {}, {}),
101
+ values: [..._interpolateArray(value)],
102
+ close: _t.node("Instruction", "Punctuator", [_t.lit`)`], {}, {})
103
+ }, {})
104
+ }, {});
105
+ return {
106
+ attrs
107
+ };
108
+ }
109
+ export function* Optional(matchers, s, ctx) {
110
+ const matchers_ = ctx.unbox(matchers);
111
+ if (matchers_.length > 1) {
73
112
  throw new Error('Optional only allows one matcher');
74
113
  }
75
114
  yield _t.node("Instruction", "Call", [_t.ref`verb`, _t.ref`arguments`], {
76
115
  verb: _t.node("Instruction", "Identifier", [_t.lit`eatMatch`], {}, {}),
77
116
  arguments: _t.node("Instruction", "Tuple", [_t.ref`open`, _t.ref`values[]`, _t.ref`close`], {
78
117
  open: _t.node("Instruction", "Punctuator", [_t.lit`(`], {}, {}),
79
- values: [..._interpolateArray(matchers[0])],
118
+ values: [..._interpolateArray(matchers_[0])],
80
119
  close: _t.node("Instruction", "Punctuator", [_t.lit`)`], {}, {})
81
120
  }, {})
82
121
  }, {});
@@ -1,33 +1,56 @@
1
1
  import { i } from '@bablr/boot/shorthand.macro';
2
2
 
3
- export function* List({
4
- props: { element, separator, allowHoles = false, allowTrailingSeparator = true },
5
- }) {
3
+ export function* List(props, s, ctx) {
4
+ const {
5
+ element,
6
+ separator,
7
+ allowHoles = false,
8
+ allowTrailingSeparator = true,
9
+ } = ctx.unbox(props);
10
+
6
11
  let sep, it;
7
12
  for (;;) {
8
- it = yield i`eatMatch(${element})`;
13
+ it = yield i`eatMatch(${element} 'elements[]')`;
9
14
  if (it || allowTrailingSeparator) {
10
- sep = yield i`eatMatch(${separator})`;
15
+ sep = yield i`eatMatch(${separator} 'separators[]')`;
11
16
  }
12
17
  if (!(sep || allowHoles)) break;
13
18
  }
14
19
  }
15
20
 
16
- export function* Any({ props: { matchers } }) {
17
- for (const matcher of matchers) {
21
+ export function* Any(matchers, s, ctx) {
22
+ for (const matcher of ctx.unbox(matchers)) {
18
23
  if (yield i`eatMatch(${matcher})`) break;
19
24
  }
20
25
  }
21
26
 
22
- export function* All({ props: { matchers } }) {
23
- for (const matcher of matchers) {
27
+ export function* All(matchers, s, ctx) {
28
+ for (const matcher of ctx.unbox(matchers)) {
24
29
  yield i`eat(${matcher})`;
25
30
  }
26
31
  }
27
32
 
28
- export function* Optional({ props: { matchers } }) {
29
- if (matchers.length > 1) {
33
+ export function* Match(cases, s, ctx) {
34
+ for (const case_ of ctx.unbox(cases)) {
35
+ const { 0: matcher, 1: guard } = ctx.unbox(case_);
36
+ if (yield i`match(${guard})`) {
37
+ yield i`eat(${matcher})`;
38
+ break;
39
+ }
40
+ }
41
+ }
42
+
43
+ export function* Punctuator(obj, s, ctx) {
44
+ const { value, attrs } = ctx.unbox(obj);
45
+ yield i`eat(${value})`;
46
+
47
+ return { attrs };
48
+ }
49
+
50
+ export function* Optional(matchers, s, ctx) {
51
+ const matchers_ = ctx.unbox(matchers);
52
+ if (matchers_.length > 1) {
30
53
  throw new Error('Optional only allows one matcher');
31
54
  }
32
- yield i`eatMatch(${matchers[0]})`;
55
+ yield i`eatMatch(${matchers_[0]})`;
33
56
  }
package/lib/trivia.js CHANGED
@@ -15,7 +15,7 @@ const lookbehind = (context, s) => {
15
15
  // eslint-disable-next-line no-undef
16
16
  const matchedResults = new WeakSet();
17
17
 
18
- export const triviaEnhancer = ({ spaceIsAllowed, eatMatchTrivia }, grammar) => {
18
+ export const triviaEnhancer = ({ triviaIsAllowed, eatMatchTrivia }, grammar) => {
19
19
  return mapProductions((production) => {
20
20
  return function* (props, s, ctx, ...args) {
21
21
  const co = new Coroutine(production(props, s, ctx, ...args));
@@ -48,7 +48,7 @@ export const triviaEnhancer = ({ spaceIsAllowed, eatMatchTrivia }, grammar) => {
48
48
  verbSuffix !== '#'
49
49
  ) {
50
50
  const previous = lookbehind(ctx, s);
51
- if (spaceIsAllowed(s) && !matchedResults.has(previous)) {
51
+ if (triviaIsAllowed(s) && !matchedResults.has(previous)) {
52
52
  matchedResults.add(previous);
53
53
  yield eatMatchTrivia;
54
54
  matchedResults.add(s.result);
@@ -69,7 +69,7 @@ export const triviaEnhancer = ({ spaceIsAllowed, eatMatchTrivia }, grammar) => {
69
69
 
70
70
  if (!s.isTerminal) {
71
71
  const previous = lookbehind(ctx, s);
72
- if (spaceIsAllowed(s) && !matchedResults.has(previous)) {
72
+ if (triviaIsAllowed(s) && !matchedResults.has(previous)) {
73
73
  matchedResults.add(previous);
74
74
  yield eatMatchTrivia;
75
75
  matchedResults.add(s.result);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@bablr/helpers",
3
3
  "description": "Command helpers for use in writing BABLR grammars",
4
- "version": "0.1.3",
4
+ "version": "0.1.5",
5
5
  "author": "Conrad Buck<conartist6@gmail.com>",
6
6
  "type": "module",
7
7
  "files": [
@@ -25,7 +25,7 @@
25
25
  "clean": "rm lib/productions.generated.js"
26
26
  },
27
27
  "dependencies": {
28
- "@bablr/boot": "0.1.6",
28
+ "@bablr/boot": "0.1.9",
29
29
  "@bablr/boot-helpers": "0.1.3",
30
30
  "iter-tools-es": "^7.5.3"
31
31
  },