agency-lang 0.0.15 → 0.0.16
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/lib/backends/agencyGenerator.js +20 -3
- package/dist/lib/backends/agencyGenerator.test.d.ts +1 -0
- package/dist/lib/backends/agencyGenerator.test.js +205 -0
- package/dist/lib/backends/baseGenerator.js +3 -1
- package/dist/lib/backends/graphGenerator.js +7 -5
- package/dist/lib/backends/typescriptGenerator.d.ts +3 -3
- package/dist/lib/backends/typescriptGenerator.js +14 -12
- package/dist/lib/parsers/access.d.ts +3 -5
- package/dist/lib/parsers/access.js +54 -13
- package/dist/lib/parsers/access.test.js +40 -500
- package/dist/lib/parsers/assignment.js +2 -2
- package/dist/lib/parsers/assignment.test.d.ts +1 -0
- package/dist/lib/parsers/assignment.test.js +279 -0
- package/dist/lib/parsers/dataStructures.js +3 -3
- package/dist/lib/parsers/function.d.ts +3 -1
- package/dist/lib/parsers/function.js +6 -4
- package/dist/lib/parsers/function.test.js +653 -8
- package/dist/lib/parsers/functionCall.js +3 -2
- package/dist/lib/parsers/functionCall.test.js +310 -0
- package/dist/lib/parsers/literals.js +11 -2
- package/dist/lib/parsers/literals.test.js +7 -7
- package/dist/lib/parsers/matchBlock.js +2 -2
- package/dist/lib/parsers/parserUtils.test.d.ts +1 -0
- package/dist/lib/parsers/parserUtils.test.js +46 -0
- package/dist/lib/parsers/returnStatement.js +2 -2
- package/dist/lib/parsers/returnStatement.test.d.ts +1 -0
- package/dist/lib/parsers/returnStatement.test.js +268 -0
- package/dist/lib/parsers/specialVar.test.d.ts +1 -0
- package/dist/lib/parsers/specialVar.test.js +219 -0
- package/dist/lib/types/access.d.ts +5 -5
- package/dist/lib/types/access.js +6 -1
- package/dist/lib/types/dataStructures.d.ts +3 -3
- package/dist/lib/types/function.d.ts +10 -4
- package/dist/lib/types/matchBlock.d.ts +2 -2
- package/dist/lib/types/returnStatement.d.ts +2 -2
- package/dist/lib/types/whileLoop.d.ts +2 -2
- package/dist/lib/types.d.ts +3 -3
- package/package.json +3 -3
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { assignmentParser } from "./assignment.js";
|
|
3
|
+
describe("assignmentParser", () => {
|
|
4
|
+
const testCases = [
|
|
5
|
+
// Happy path - simple literal assignments
|
|
6
|
+
{
|
|
7
|
+
input: "x = 5",
|
|
8
|
+
expected: {
|
|
9
|
+
success: true,
|
|
10
|
+
result: {
|
|
11
|
+
type: "assignment",
|
|
12
|
+
variableName: "x",
|
|
13
|
+
value: { type: "number", value: "5" },
|
|
14
|
+
},
|
|
15
|
+
},
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
input: 'name = "Alice"',
|
|
19
|
+
expected: {
|
|
20
|
+
success: true,
|
|
21
|
+
result: {
|
|
22
|
+
type: "assignment",
|
|
23
|
+
variableName: "name",
|
|
24
|
+
value: { type: "string", value: "Alice" },
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
input: "bar = `the number 1`",
|
|
30
|
+
expected: {
|
|
31
|
+
success: true,
|
|
32
|
+
result: {
|
|
33
|
+
type: "assignment",
|
|
34
|
+
variableName: "bar",
|
|
35
|
+
value: {
|
|
36
|
+
type: "prompt",
|
|
37
|
+
segments: [{ type: "text", value: "the number 1" }],
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
input: "result = someVariable",
|
|
44
|
+
expected: {
|
|
45
|
+
success: true,
|
|
46
|
+
result: {
|
|
47
|
+
type: "assignment",
|
|
48
|
+
variableName: "result",
|
|
49
|
+
value: { type: "variableName", value: "someVariable" },
|
|
50
|
+
},
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
// With optional semicolon
|
|
54
|
+
{
|
|
55
|
+
input: "x = 42;",
|
|
56
|
+
expected: {
|
|
57
|
+
success: true,
|
|
58
|
+
result: {
|
|
59
|
+
type: "assignment",
|
|
60
|
+
variableName: "x",
|
|
61
|
+
value: { type: "number", value: "42" },
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
// With optional spaces
|
|
66
|
+
{
|
|
67
|
+
input: " x = 5 ",
|
|
68
|
+
expected: {
|
|
69
|
+
success: true,
|
|
70
|
+
result: {
|
|
71
|
+
type: "assignment",
|
|
72
|
+
variableName: "x",
|
|
73
|
+
value: { type: "number", value: "5" },
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
input: "x=5",
|
|
79
|
+
expected: {
|
|
80
|
+
success: true,
|
|
81
|
+
result: {
|
|
82
|
+
type: "assignment",
|
|
83
|
+
variableName: "x",
|
|
84
|
+
value: { type: "number", value: "5" },
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
// Variable names with underscores and numbers
|
|
89
|
+
{
|
|
90
|
+
input: "my_var_123 = 999",
|
|
91
|
+
expected: {
|
|
92
|
+
success: true,
|
|
93
|
+
result: {
|
|
94
|
+
type: "assignment",
|
|
95
|
+
variableName: "my_var_123",
|
|
96
|
+
value: { type: "number", value: "999" },
|
|
97
|
+
},
|
|
98
|
+
},
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
input: "_privateVar = true",
|
|
102
|
+
expected: {
|
|
103
|
+
success: true,
|
|
104
|
+
result: {
|
|
105
|
+
type: "assignment",
|
|
106
|
+
variableName: "_privateVar",
|
|
107
|
+
value: { type: "variableName", value: "true" },
|
|
108
|
+
},
|
|
109
|
+
},
|
|
110
|
+
},
|
|
111
|
+
// Function call assignment
|
|
112
|
+
{
|
|
113
|
+
input: "result = foo()",
|
|
114
|
+
expected: {
|
|
115
|
+
success: true,
|
|
116
|
+
result: {
|
|
117
|
+
type: "assignment",
|
|
118
|
+
variableName: "result",
|
|
119
|
+
value: {
|
|
120
|
+
type: "functionCall",
|
|
121
|
+
functionName: "foo",
|
|
122
|
+
arguments: [],
|
|
123
|
+
},
|
|
124
|
+
},
|
|
125
|
+
},
|
|
126
|
+
},
|
|
127
|
+
{
|
|
128
|
+
input: 'output = calculate(1, "test")',
|
|
129
|
+
expected: {
|
|
130
|
+
success: true,
|
|
131
|
+
result: {
|
|
132
|
+
type: "assignment",
|
|
133
|
+
variableName: "output",
|
|
134
|
+
value: {
|
|
135
|
+
type: "functionCall",
|
|
136
|
+
functionName: "calculate",
|
|
137
|
+
arguments: [
|
|
138
|
+
{ type: "number", value: "1" },
|
|
139
|
+
{ type: "string", value: "test" },
|
|
140
|
+
],
|
|
141
|
+
},
|
|
142
|
+
},
|
|
143
|
+
},
|
|
144
|
+
},
|
|
145
|
+
// Array assignment
|
|
146
|
+
{
|
|
147
|
+
input: "arr = [1, 2, 3]",
|
|
148
|
+
expected: {
|
|
149
|
+
success: true,
|
|
150
|
+
result: {
|
|
151
|
+
type: "assignment",
|
|
152
|
+
variableName: "arr",
|
|
153
|
+
value: {
|
|
154
|
+
type: "agencyArray",
|
|
155
|
+
items: [
|
|
156
|
+
{ type: "number", value: "1" },
|
|
157
|
+
{ type: "number", value: "2" },
|
|
158
|
+
{ type: "number", value: "3" },
|
|
159
|
+
],
|
|
160
|
+
},
|
|
161
|
+
},
|
|
162
|
+
},
|
|
163
|
+
},
|
|
164
|
+
{
|
|
165
|
+
input: "empty = []",
|
|
166
|
+
expected: {
|
|
167
|
+
success: true,
|
|
168
|
+
result: {
|
|
169
|
+
type: "assignment",
|
|
170
|
+
variableName: "empty",
|
|
171
|
+
value: {
|
|
172
|
+
type: "agencyArray",
|
|
173
|
+
items: [],
|
|
174
|
+
},
|
|
175
|
+
},
|
|
176
|
+
},
|
|
177
|
+
},
|
|
178
|
+
// Object assignment
|
|
179
|
+
{
|
|
180
|
+
input: 'obj = { key: "value" }',
|
|
181
|
+
expected: {
|
|
182
|
+
success: true,
|
|
183
|
+
result: {
|
|
184
|
+
type: "assignment",
|
|
185
|
+
variableName: "obj",
|
|
186
|
+
value: {
|
|
187
|
+
type: "agencyObject",
|
|
188
|
+
entries: [
|
|
189
|
+
{
|
|
190
|
+
key: "key",
|
|
191
|
+
value: { type: "string", value: "value" },
|
|
192
|
+
},
|
|
193
|
+
],
|
|
194
|
+
},
|
|
195
|
+
},
|
|
196
|
+
},
|
|
197
|
+
},
|
|
198
|
+
{
|
|
199
|
+
input: "emptyObj = {}",
|
|
200
|
+
expected: {
|
|
201
|
+
success: true,
|
|
202
|
+
result: {
|
|
203
|
+
type: "assignment",
|
|
204
|
+
variableName: "emptyObj",
|
|
205
|
+
value: {
|
|
206
|
+
type: "agencyObject",
|
|
207
|
+
entries: [],
|
|
208
|
+
},
|
|
209
|
+
},
|
|
210
|
+
},
|
|
211
|
+
},
|
|
212
|
+
// Access expression assignment
|
|
213
|
+
{
|
|
214
|
+
input: "value = obj.property",
|
|
215
|
+
expected: {
|
|
216
|
+
success: true,
|
|
217
|
+
result: {
|
|
218
|
+
type: "assignment",
|
|
219
|
+
variableName: "value",
|
|
220
|
+
value: {
|
|
221
|
+
type: "accessExpression",
|
|
222
|
+
expression: {
|
|
223
|
+
type: "dotProperty",
|
|
224
|
+
object: { type: "variableName", value: "obj" },
|
|
225
|
+
propertyName: "property",
|
|
226
|
+
},
|
|
227
|
+
},
|
|
228
|
+
},
|
|
229
|
+
},
|
|
230
|
+
},
|
|
231
|
+
// Negative numbers
|
|
232
|
+
{
|
|
233
|
+
input: "negative = -42",
|
|
234
|
+
expected: {
|
|
235
|
+
success: true,
|
|
236
|
+
result: {
|
|
237
|
+
type: "assignment",
|
|
238
|
+
variableName: "negative",
|
|
239
|
+
value: { type: "number", value: "-42" },
|
|
240
|
+
},
|
|
241
|
+
},
|
|
242
|
+
},
|
|
243
|
+
// Decimal numbers
|
|
244
|
+
{
|
|
245
|
+
input: "pi = 3.14",
|
|
246
|
+
expected: {
|
|
247
|
+
success: true,
|
|
248
|
+
result: {
|
|
249
|
+
type: "assignment",
|
|
250
|
+
variableName: "pi",
|
|
251
|
+
value: { type: "number", value: "3.14" },
|
|
252
|
+
},
|
|
253
|
+
},
|
|
254
|
+
},
|
|
255
|
+
// Failure cases
|
|
256
|
+
{ input: "=5", expected: { success: false } },
|
|
257
|
+
{ input: "x =", expected: { success: false } },
|
|
258
|
+
{ input: "", expected: { success: false } },
|
|
259
|
+
{ input: "x", expected: { success: false } },
|
|
260
|
+
{ input: "x ==5", expected: { success: false } },
|
|
261
|
+
];
|
|
262
|
+
testCases.forEach(({ input, expected }) => {
|
|
263
|
+
if (expected.success) {
|
|
264
|
+
it(`should parse "${input}" successfully`, () => {
|
|
265
|
+
const result = assignmentParser(input);
|
|
266
|
+
expect(result.success).toBe(true);
|
|
267
|
+
if (result.success) {
|
|
268
|
+
expect(result.result).toEqual(expected.result);
|
|
269
|
+
}
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
else {
|
|
273
|
+
it(`should fail to parse "${input}"`, () => {
|
|
274
|
+
const result = assignmentParser(input);
|
|
275
|
+
expect(result.success).toBe(false);
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
});
|
|
279
|
+
});
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import { capture, char, manyWithJoin, noneOf, optional, or, sepBy, seqC, set, succeed, trace, } from "tarsec";
|
|
2
|
-
import { accessExpressionParser } from "./access.js";
|
|
2
|
+
import { accessExpressionParser, indexAccessParser } from "./access.js";
|
|
3
3
|
import { functionCallParser } from "./functionCall.js";
|
|
4
4
|
import { literalParser } from "./literals.js";
|
|
5
5
|
import { comma, optionalSpaces } from "./utils.js";
|
|
6
6
|
export const agencyArrayParser = (input) => {
|
|
7
|
-
const parser = trace("agencyArrayParser", seqC(set("type", "agencyArray"), char("["), capture(sepBy(comma, or(accessExpressionParser, functionCallParser, literalParser, agencyObjectParser, agencyArrayParser)), "items"), char("]")));
|
|
7
|
+
const parser = trace("agencyArrayParser", seqC(set("type", "agencyArray"), char("["), capture(sepBy(comma, or(indexAccessParser, accessExpressionParser, functionCallParser, literalParser, agencyObjectParser, agencyArrayParser)), "items"), char("]")));
|
|
8
8
|
return parser(input);
|
|
9
9
|
};
|
|
10
10
|
export const agencyObjectKVParser = (input) => {
|
|
11
|
-
const parser = trace("agencyObjectKVParser", seqC(optionalSpaces, optional(char('"')), capture(manyWithJoin(noneOf('":\n\t ')), "key"), optional(char('"')), optionalSpaces, char(":"), optionalSpaces, capture(or(accessExpressionParser, functionCallParser, literalParser, agencyObjectParser, agencyArrayParser), "value")));
|
|
11
|
+
const parser = trace("agencyObjectKVParser", seqC(optionalSpaces, optional(char('"')), capture(manyWithJoin(noneOf('":\n\t ')), "key"), optional(char('"')), optionalSpaces, char(":"), optionalSpaces, capture(or(indexAccessParser, accessExpressionParser, functionCallParser, literalParser, agencyObjectParser, agencyArrayParser), "value")));
|
|
12
12
|
return parser(input);
|
|
13
13
|
};
|
|
14
14
|
export const agencyObjectParser = seqC(set("type", "agencyObject"), char("{"), optionalSpaces, capture(or(sepBy(comma, agencyObjectKVParser), succeed([])), "entries"), optional(char(",")), optionalSpaces, char("}"));
|
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
import { AgencyNode, DocString, FunctionDefinition } from "../types.js";
|
|
1
|
+
import { AgencyNode, DocString, FunctionDefinition, FunctionParameter } from "../types.js";
|
|
2
2
|
import { Parser } from "tarsec";
|
|
3
3
|
import { GraphNodeDefinition } from "../types/graphNode.js";
|
|
4
4
|
import { WhileLoop } from "../types/whileLoop.js";
|
|
5
5
|
export declare const docStringParser: Parser<DocString>;
|
|
6
6
|
export declare const bodyParser: Parser<AgencyNode[]>;
|
|
7
7
|
export declare const whileLoopParser: Parser<WhileLoop>;
|
|
8
|
+
export declare const functionParameterParserWithTypeHint: Parser<FunctionParameter>;
|
|
9
|
+
export declare const functionParameterParser: Parser<FunctionParameter>;
|
|
8
10
|
export declare const functionParser: Parser<FunctionDefinition>;
|
|
9
11
|
export declare const graphNodeParser: Parser<GraphNodeDefinition>;
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { capture, char, debug, many1, many1Till, many1WithJoin, map, or, sepBy, seqC, set, space, spaces, str, succeed, trace, } from "tarsec";
|
|
2
|
-
import { accessExpressionParser } from "./access.js";
|
|
2
|
+
import { accessExpressionParser, indexAccessParser } from "./access.js";
|
|
3
3
|
import { assignmentParser } from "./assignment.js";
|
|
4
4
|
import { commentParser } from "./comment.js";
|
|
5
5
|
import { functionCallParser } from "./functionCall.js";
|
|
6
6
|
import { literalParser } from "./literals.js";
|
|
7
7
|
import { matchBlockParser } from "./matchBlock.js";
|
|
8
8
|
import { optionalSemicolon } from "./parserUtils.js";
|
|
9
|
-
import { typeAliasParser, typeHintParser } from "./typeHints.js";
|
|
9
|
+
import { typeAliasParser, typeHintParser, variableTypeParser, } from "./typeHints.js";
|
|
10
10
|
import { comma, optionalSpaces, varNameChar } from "./utils.js";
|
|
11
11
|
import { returnStatementParser } from "./returnStatement.js";
|
|
12
12
|
import { usesToolParser } from "./tools.js";
|
|
@@ -18,6 +18,8 @@ export const bodyParser = trace("functionBodyParser", (input) => {
|
|
|
18
18
|
const result = parser(input);
|
|
19
19
|
return result;
|
|
20
20
|
});
|
|
21
|
-
export const whileLoopParser = trace("whileLoopParser", seqC(set("type", "whileLoop"), str("while"), optionalSpaces, char("("), optionalSpaces, capture(or(functionCallParser, accessExpressionParser, literalParser), "condition"), optionalSpaces, char(")"), optionalSpaces, char("{"), spaces, capture(bodyParser, "body"), optionalSpaces, char("}")));
|
|
22
|
-
export const
|
|
21
|
+
export const whileLoopParser = trace("whileLoopParser", seqC(set("type", "whileLoop"), str("while"), optionalSpaces, char("("), optionalSpaces, capture(or(indexAccessParser, functionCallParser, accessExpressionParser, literalParser), "condition"), optionalSpaces, char(")"), optionalSpaces, char("{"), spaces, capture(bodyParser, "body"), optionalSpaces, char("}")));
|
|
22
|
+
export const functionParameterParserWithTypeHint = trace("functionParameterParserWithTypeHint", seqC(set("type", "functionParameter"), capture(many1WithJoin(varNameChar), "name"), optionalSpaces, char(":"), optionalSpaces, capture(variableTypeParser, "typeHint")));
|
|
23
|
+
export const functionParameterParser = trace("functionParameterParser", seqC(set("type", "functionParameter"), capture(many1WithJoin(varNameChar), "name")));
|
|
24
|
+
export const functionParser = trace("functionParser", seqC(set("type", "function"), str("def"), many1(space), capture(many1Till(char("(")), "functionName"), char("("), optionalSpaces, capture(sepBy(comma, or(functionParameterParserWithTypeHint, functionParameterParser)), "parameters"), optionalSpaces, char(")"), optionalSpaces, char("{"), optionalSpaces, capture(or(docStringParser, succeed(undefined)), "docString"), optionalSpaces, capture(bodyParser, "body"), optionalSpaces, char("}"), optionalSemicolon));
|
|
23
25
|
export const graphNodeParser = trace("graphNodeParser", seqC(set("type", "graphNode"), str("node"), many1(space), capture(many1Till(char("(")), "nodeName"), char("("), optionalSpaces, capture(or(sepBy(comma, many1WithJoin(varNameChar)), succeed([])), "parameters"), optionalSpaces, char(")"), optionalSpaces, char("{"), optionalSpaces, capture(bodyParser, "body"), optionalSpaces, char("}"), optionalSemicolon));
|