@bablr/helpers 0.1.4 → 0.1.6

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/decorators.js CHANGED
@@ -1 +1,8 @@
1
1
  export * from '@bablr/boot-helpers/decorators';
2
+
3
+ export const UnboundAttributes = (attributes) => (desc, context) => {
4
+ context.addInitializer(function () {
5
+ this.unboundAttributes = this.unboundAttributes || new Map();
6
+ this.unboundAttributes.set(context.name, attributes);
7
+ });
8
+ };
@@ -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,60 @@ 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 const Keyword = Punctuator;
110
+ export function* Optional(matchers, s, ctx) {
111
+ const matchers_ = ctx.unbox(matchers);
112
+ if (matchers_.length > 1) {
73
113
  throw new Error('Optional only allows one matcher');
74
114
  }
75
115
  yield _t.node("Instruction", "Call", [_t.ref`verb`, _t.ref`arguments`], {
76
116
  verb: _t.node("Instruction", "Identifier", [_t.lit`eatMatch`], {}, {}),
77
117
  arguments: _t.node("Instruction", "Tuple", [_t.ref`open`, _t.ref`values[]`, _t.ref`close`], {
78
118
  open: _t.node("Instruction", "Punctuator", [_t.lit`(`], {}, {}),
79
- values: [..._interpolateArray(matchers[0])],
119
+ values: [..._interpolateArray(matchers_[0])],
80
120
  close: _t.node("Instruction", "Punctuator", [_t.lit`)`], {}, {})
81
121
  }, {})
82
122
  }, {});
@@ -1,33 +1,58 @@
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 const Keyword = Punctuator;
51
+
52
+ export function* Optional(matchers, s, ctx) {
53
+ const matchers_ = ctx.unbox(matchers);
54
+ if (matchers_.length > 1) {
30
55
  throw new Error('Optional only allows one matcher');
31
56
  }
32
- yield i`eatMatch(${matchers[0]})`;
57
+ yield i`eatMatch(${matchers_[0]})`;
33
58
  }
package/lib/symbols.js CHANGED
@@ -1,6 +1 @@
1
1
  export const node = Symbol.for('@bablr/node');
2
-
3
- export const active = Symbol.for('@bablr/active');
4
- export const suspended = Symbol.for('@bablr/suspended');
5
- export const accepted = Symbol.for('@bablr/accepted');
6
- export const rejected = Symbol.for('@bablr/rejected');
package/lib/trivia.js CHANGED
@@ -75,6 +75,8 @@ export const triviaEnhancer = ({ triviaIsAllowed, eatMatchTrivia }, grammar) =>
75
75
  matchedResults.add(s.result);
76
76
  }
77
77
  }
78
+
79
+ return co.value;
78
80
  } catch (e) {
79
81
  co.throw(e);
80
82
  throw e;
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.4",
4
+ "version": "0.1.6",
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.8",
28
+ "@bablr/boot": "0.1.9",
29
29
  "@bablr/boot-helpers": "0.1.3",
30
30
  "iter-tools-es": "^7.5.3"
31
31
  },