harmonyc 0.8.0 → 0.8.2
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/code_generator/JavaScript.js +22 -18
- package/compiler/compiler.js +1 -1
- package/package.json +1 -1
- package/parser/lexer_rules.js +1 -1
- package/parser/parser.js +1 -1
|
@@ -60,6 +60,7 @@ export class NodeTest {
|
|
|
60
60
|
}
|
|
61
61
|
errorStep(action, errorMessage) {
|
|
62
62
|
var _a;
|
|
63
|
+
this.declareFeatureVariables([action]);
|
|
63
64
|
this.tf.print(`expect(async () => {`);
|
|
64
65
|
this.tf.indent(() => {
|
|
65
66
|
action.toCode(this);
|
|
@@ -67,14 +68,7 @@ export class NodeTest {
|
|
|
67
68
|
this.tf.print(`}).rejects.toThrow(${(_a = errorMessage === null || errorMessage === void 0 ? void 0 : errorMessage.toCode(this)) !== null && _a !== void 0 ? _a : ''});`);
|
|
68
69
|
}
|
|
69
70
|
step(action, responses) {
|
|
70
|
-
|
|
71
|
-
const feature = p.feature.name;
|
|
72
|
-
let f = this.featureVars.get(feature);
|
|
73
|
-
if (!f) {
|
|
74
|
-
f = toId(feature, abbrev, this.featureVars);
|
|
75
|
-
this.tf.print(`const ${f} = new ${pascalCase(feature)}Phrases();`);
|
|
76
|
-
}
|
|
77
|
-
}
|
|
71
|
+
this.declareFeatureVariables([action, ...responses]);
|
|
78
72
|
if (responses.length === 0) {
|
|
79
73
|
action.toCode(this);
|
|
80
74
|
return;
|
|
@@ -94,14 +88,24 @@ export class NodeTest {
|
|
|
94
88
|
}
|
|
95
89
|
});
|
|
96
90
|
}
|
|
91
|
+
declareFeatureVariables(phrases) {
|
|
92
|
+
for (const p of phrases) {
|
|
93
|
+
const feature = p.feature.name;
|
|
94
|
+
let f = this.featureVars.get(feature);
|
|
95
|
+
if (!f) {
|
|
96
|
+
f = toId(feature, abbrev, this.featureVars);
|
|
97
|
+
this.tf.print(`const ${f} = new ${pascalCase(feature)}Phrases();`);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
97
101
|
phrase(p) {
|
|
98
|
-
const phrasefn =
|
|
102
|
+
const phrasefn = functionName(p);
|
|
99
103
|
if (!this.phraseFns.has(phrasefn))
|
|
100
104
|
this.phraseFns.set(phrasefn, p);
|
|
101
105
|
const f = this.featureVars.get(p.feature.name);
|
|
102
106
|
const args = p.args.map((a) => a.toCode(this));
|
|
103
107
|
args.push(...this.extraArgs);
|
|
104
|
-
this.tf.print(`await ${f}.${
|
|
108
|
+
this.tf.print(`await ${f}.${functionName(p)}(${args.join(', ')});`);
|
|
105
109
|
}
|
|
106
110
|
stringLiteral(text) {
|
|
107
111
|
return str(text);
|
|
@@ -118,14 +122,6 @@ export class NodeTest {
|
|
|
118
122
|
variantParamDeclaration(index) {
|
|
119
123
|
return `${this.paramName(index)}: any`;
|
|
120
124
|
}
|
|
121
|
-
functionName(phrase) {
|
|
122
|
-
const { kind } = phrase;
|
|
123
|
-
return ((kind === 'response' ? 'Then_' : 'When_') +
|
|
124
|
-
([...phrase.content, phrase.docstring ? [phrase.docstring] : []]
|
|
125
|
-
.map((c) => c instanceof Word ? underscore(c.text) : c instanceof Arg ? '_' : '')
|
|
126
|
-
.filter((x) => x)
|
|
127
|
-
.join('_') || '_'));
|
|
128
|
-
}
|
|
129
125
|
}
|
|
130
126
|
function str(s) {
|
|
131
127
|
if (s.includes('\n'))
|
|
@@ -167,3 +163,11 @@ function abbrev(s) {
|
|
|
167
163
|
.map((x) => x.charAt(0).toUpperCase())
|
|
168
164
|
.join('');
|
|
169
165
|
}
|
|
166
|
+
export function functionName(phrase) {
|
|
167
|
+
const { kind } = phrase;
|
|
168
|
+
return ((kind === 'response' ? 'Then_' : 'When_') +
|
|
169
|
+
([...phrase.content, phrase.docstring ? [phrase.docstring] : []]
|
|
170
|
+
.flatMap((c) => c instanceof Word ? words(c.text) : c instanceof Arg ? ['_'] : [])
|
|
171
|
+
.filter((x) => x)
|
|
172
|
+
.join('_') || '_'));
|
|
173
|
+
}
|
package/compiler/compiler.js
CHANGED
|
@@ -16,7 +16,7 @@ export async function compileFiles(pattern) {
|
|
|
16
16
|
console.log(`Compiled ${fns.length} file${fns.length === 1 ? '' : 's'}.`);
|
|
17
17
|
const generated = features.filter((f) => f.phrasesFileAction === 'generated');
|
|
18
18
|
if (generated.length) {
|
|
19
|
-
console.log(`Generated ${generated.length}
|
|
19
|
+
console.log(`Generated ${generated.length} phrases file${generated.length === 1 ? '' : 's'}.`);
|
|
20
20
|
}
|
|
21
21
|
return { fns, outFns: features.map((f) => f.outFile.name) };
|
|
22
22
|
}
|
package/package.json
CHANGED
package/parser/lexer_rules.js
CHANGED
|
@@ -34,7 +34,7 @@ const rules = [
|
|
|
34
34
|
[true, /^:(?=\s*(?:\n|$))/g, T.Colon],
|
|
35
35
|
[
|
|
36
36
|
true,
|
|
37
|
-
/^(?!\s|=>|!!|- |\+ |[\[\]"`|]).+?(?=[\[\]"`|]|\n|$|=>|!!|:\s*(?:\n|$)|$)/g,
|
|
37
|
+
/^(?!\s|=>|!!|- |\+ |[\[\]"`|]|:\s*(?:\n|$)).+?(?=[\[\]"`|]|\n|$|=>|!!|:\s*(?:\n|$)|$)/g,
|
|
38
38
|
T.Words,
|
|
39
39
|
],
|
|
40
40
|
[true, /^-/g, T.Minus],
|
package/parser/parser.js
CHANGED
|
@@ -25,7 +25,7 @@ export const RESPONSE = apply(PHRASE, ([parts, docstring]) => {
|
|
|
25
25
|
export const ARROW = kmid(seq(opt_sc(NEWLINES), SPACES), tok(T.ResponseArrow), SPACES);
|
|
26
26
|
export const RESPONSE_ITEM = kright(ARROW, RESPONSE);
|
|
27
27
|
export const STEP = apply(seq(ACTION, rep_sc(RESPONSE_ITEM)), ([action, responses]) => new Step(action, responses).setFork(true));
|
|
28
|
-
export const LABEL = apply(kleft(list_sc(PART, SPACES), tok(T.Colon)), (words) => new Label(words.map((w) => w.toString()).join(' ')));
|
|
28
|
+
export const LABEL = apply(kleft(list_sc(PART, SPACES), seq(tok(T.Colon), SPACES)), (words) => new Label(words.map((w) => w.toString()).join(' ')));
|
|
29
29
|
export const SECTION = apply(LABEL, (text) => new Section(text));
|
|
30
30
|
export const BRANCH = alt_sc(SECTION, STEP); // section first, to make sure there is no colon after step
|
|
31
31
|
export const DENTS = apply(opt_sc(seq(SPACES, alt_sc(tok(T.Plus), tok(T.Minus)), tok(T.Space))), (lineHead) => {
|