agency-lang 0.0.1
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/README.md +7 -0
- package/dist/backends/baseGenerator.js +194 -0
- package/dist/backends/graphGenerator.integration.test.js +119 -0
- package/dist/backends/graphGenerator.js +308 -0
- package/dist/backends/index.js +3 -0
- package/dist/backends/typescriptGenerator/builtins.js +46 -0
- package/dist/backends/typescriptGenerator/typeToString.js +36 -0
- package/dist/backends/typescriptGenerator/typeToZodSchema.js +54 -0
- package/dist/backends/typescriptGenerator.integration.test.js +119 -0
- package/dist/backends/typescriptGenerator.js +340 -0
- package/dist/backends/typescriptGenerator.test.js +763 -0
- package/dist/backends/utils.js +6 -0
- package/dist/generate-graph-file.js +25 -0
- package/dist/generate-ts-file.js +26 -0
- package/dist/index.js +3 -0
- package/dist/parser.js +38 -0
- package/dist/parser.test.js +306 -0
- package/dist/parsers/access.js +19 -0
- package/dist/parsers/access.test.js +929 -0
- package/dist/parsers/assignment.js +8 -0
- package/dist/parsers/body.test.js +106 -0
- package/dist/parsers/comment.js +6 -0
- package/dist/parsers/comment.test.js +100 -0
- package/dist/parsers/dataStructures.js +14 -0
- package/dist/parsers/dataStructures.test.js +660 -0
- package/dist/parsers/function.js +22 -0
- package/dist/parsers/function.test.js +591 -0
- package/dist/parsers/functionCall.js +10 -0
- package/dist/parsers/functionCall.test.js +119 -0
- package/dist/parsers/importStatement.js +3 -0
- package/dist/parsers/importStatement.test.js +290 -0
- package/dist/parsers/literals.js +12 -0
- package/dist/parsers/literals.test.js +639 -0
- package/dist/parsers/matchBlock.js +24 -0
- package/dist/parsers/matchBlock.test.js +506 -0
- package/dist/parsers/parserUtils.js +2 -0
- package/dist/parsers/returnStatement.js +8 -0
- package/dist/parsers/tools.js +3 -0
- package/dist/parsers/tools.test.js +170 -0
- package/dist/parsers/typeHints.js +59 -0
- package/dist/parsers/typeHints.test.js +2754 -0
- package/dist/parsers/utils.js +5 -0
- package/dist/parsers/whileLoop.test.js +342 -0
- package/dist/templates/backends/graphGenerator/builtinTools.js +36 -0
- package/dist/templates/backends/graphGenerator/conditionalEdge.js +10 -0
- package/dist/templates/backends/graphGenerator/edge.js +10 -0
- package/dist/templates/backends/graphGenerator/goToNode.js +9 -0
- package/dist/templates/backends/graphGenerator/graphNode.js +15 -0
- package/dist/templates/backends/graphGenerator/imports.js +47 -0
- package/dist/templates/backends/graphGenerator/node.js +18 -0
- package/dist/templates/backends/graphGenerator/promptNode.js +16 -0
- package/dist/templates/backends/graphGenerator/startNode.js +10 -0
- package/dist/templates/backends/typescriptGenerator/builtinFunctions/fetch.js +17 -0
- package/dist/templates/backends/typescriptGenerator/builtinFunctions/fetchJSON.js +17 -0
- package/dist/templates/backends/typescriptGenerator/builtinFunctions/input.js +21 -0
- package/dist/templates/backends/typescriptGenerator/builtinFunctions/read.js +13 -0
- package/dist/templates/backends/typescriptGenerator/builtinTools.js +36 -0
- package/dist/templates/backends/typescriptGenerator/functionDefinition.js +11 -0
- package/dist/templates/backends/typescriptGenerator/imports.js +25 -0
- package/dist/templates/backends/typescriptGenerator/promptFunction.js +76 -0
- package/dist/templates/backends/typescriptGenerator/tool.js +23 -0
- package/dist/templates/backends/typescriptGenerator/toolCall.js +35 -0
- package/dist/types/access.js +1 -0
- package/dist/types/dataStructures.js +1 -0
- package/dist/types/function.js +1 -0
- package/dist/types/graphNode.js +1 -0
- package/dist/types/importStatement.js +1 -0
- package/dist/types/literals.js +1 -0
- package/dist/types/matchBlock.js +1 -0
- package/dist/types/returnStatement.js +1 -0
- package/dist/types/tools.js +1 -0
- package/dist/types/typeHints.js +1 -0
- package/dist/types/whileLoop.js +1 -0
- package/dist/types.js +11 -0
- package/dist/utils.js +18 -0
- package/package.json +52 -0
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { capture, char, many1WithJoin, or, seqC, set, trace, } from "tarsec";
|
|
2
|
+
import { accessExpressionParser } from "./access.js";
|
|
3
|
+
import { agencyArrayParser, agencyObjectParser } from "./dataStructures.js";
|
|
4
|
+
import { functionCallParser } from "./functionCall.js";
|
|
5
|
+
import { literalParser } from "./literals.js";
|
|
6
|
+
import { optionalSemicolon } from "./parserUtils.js";
|
|
7
|
+
import { optionalSpaces, varNameChar } from "./utils.js";
|
|
8
|
+
export const assignmentParser = trace("assignmentParser", seqC(set("type", "assignment"), optionalSpaces, capture(many1WithJoin(varNameChar), "variableName"), optionalSpaces, char("="), optionalSpaces, capture(or(functionCallParser, accessExpressionParser, agencyArrayParser, agencyObjectParser, literalParser), "value"), optionalSemicolon));
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { bodyParser } from "./function.js";
|
|
3
|
+
describe("functionBodyParser", () => {
|
|
4
|
+
const testCases = [
|
|
5
|
+
{
|
|
6
|
+
input: "foo = 1",
|
|
7
|
+
expected: {
|
|
8
|
+
success: true,
|
|
9
|
+
result: [
|
|
10
|
+
{
|
|
11
|
+
type: "assignment",
|
|
12
|
+
variableName: "foo",
|
|
13
|
+
value: { type: "number", value: "1" },
|
|
14
|
+
},
|
|
15
|
+
],
|
|
16
|
+
},
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
input: 'bar = "hello"',
|
|
20
|
+
expected: {
|
|
21
|
+
success: true,
|
|
22
|
+
result: [
|
|
23
|
+
{
|
|
24
|
+
type: "assignment",
|
|
25
|
+
variableName: "bar",
|
|
26
|
+
value: { type: "string", value: "hello" },
|
|
27
|
+
},
|
|
28
|
+
],
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
input: "bar = `hello`\nfoo",
|
|
33
|
+
expected: {
|
|
34
|
+
success: true,
|
|
35
|
+
result: [
|
|
36
|
+
{
|
|
37
|
+
type: "assignment",
|
|
38
|
+
variableName: "bar",
|
|
39
|
+
value: {
|
|
40
|
+
type: "prompt",
|
|
41
|
+
segments: [{ type: "text", value: "hello" }],
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
type: "variableName",
|
|
46
|
+
value: "foo",
|
|
47
|
+
},
|
|
48
|
+
],
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
input: "x = 5\ny = 10",
|
|
53
|
+
expected: {
|
|
54
|
+
success: true,
|
|
55
|
+
result: [
|
|
56
|
+
{
|
|
57
|
+
type: "assignment",
|
|
58
|
+
variableName: "x",
|
|
59
|
+
value: { type: "number", value: "5" },
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
type: "assignment",
|
|
63
|
+
variableName: "y",
|
|
64
|
+
value: { type: "number", value: "10" },
|
|
65
|
+
},
|
|
66
|
+
],
|
|
67
|
+
},
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
input: "42",
|
|
71
|
+
expected: {
|
|
72
|
+
success: true,
|
|
73
|
+
result: [
|
|
74
|
+
{
|
|
75
|
+
type: "number",
|
|
76
|
+
value: "42",
|
|
77
|
+
},
|
|
78
|
+
],
|
|
79
|
+
},
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
input: "",
|
|
83
|
+
expected: {
|
|
84
|
+
success: true,
|
|
85
|
+
result: [],
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
];
|
|
89
|
+
testCases.forEach(({ input, expected }) => {
|
|
90
|
+
if (expected.success) {
|
|
91
|
+
it(`should parse "${input.replace(/\n/g, "\\n")}" successfully`, () => {
|
|
92
|
+
const result = bodyParser(input);
|
|
93
|
+
expect(result.success).toBe(true);
|
|
94
|
+
if (result.success) {
|
|
95
|
+
expect(result.result).toEqual(expected.result);
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
it(`should fail to parse "${input.replace(/\n/g, "\\n")}"`, () => {
|
|
101
|
+
const result = bodyParser(input);
|
|
102
|
+
expect(result.success).toBe(false);
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
});
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { seqC, set, str, capture, many1Till, newline } from "tarsec";
|
|
2
|
+
import { optionalSpaces } from "./utils.js";
|
|
3
|
+
export const commentParser = (input) => {
|
|
4
|
+
const parser = seqC(set("type", "comment"), optionalSpaces, str("//"), capture(many1Till(newline), "content"));
|
|
5
|
+
return parser(input);
|
|
6
|
+
};
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { commentParser } from "./comment.js";
|
|
3
|
+
describe("commentParser", () => {
|
|
4
|
+
const testCases = [
|
|
5
|
+
// Happy path - basic comments
|
|
6
|
+
{
|
|
7
|
+
input: "// this is a comment\n",
|
|
8
|
+
expected: {
|
|
9
|
+
success: true,
|
|
10
|
+
result: { type: "comment", content: " this is a comment" },
|
|
11
|
+
},
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
input: "// hello world\n",
|
|
15
|
+
expected: {
|
|
16
|
+
success: true,
|
|
17
|
+
result: { type: "comment", content: " hello world" },
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
input: "//no space after slashes\n",
|
|
22
|
+
expected: {
|
|
23
|
+
success: true,
|
|
24
|
+
result: { type: "comment", content: "no space after slashes" },
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
// Comments with special characters
|
|
28
|
+
{
|
|
29
|
+
input: "// comment with numbers 123\n",
|
|
30
|
+
expected: {
|
|
31
|
+
success: true,
|
|
32
|
+
result: { type: "comment", content: " comment with numbers 123" },
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
input: "// special chars !@#$%^&*()\n",
|
|
37
|
+
expected: {
|
|
38
|
+
success: true,
|
|
39
|
+
result: { type: "comment", content: " special chars !@#$%^&*()" },
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
input: "// code comment: x = 5\n",
|
|
44
|
+
expected: {
|
|
45
|
+
success: true,
|
|
46
|
+
result: { type: "comment", content: " code comment: x = 5" },
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
// Empty comment (fails because many1Till requires at least one char)
|
|
50
|
+
{
|
|
51
|
+
input: "//\n",
|
|
52
|
+
expected: { success: false },
|
|
53
|
+
},
|
|
54
|
+
// Comments with tabs and spaces
|
|
55
|
+
{
|
|
56
|
+
input: "// multiple spaces \n",
|
|
57
|
+
expected: {
|
|
58
|
+
success: true,
|
|
59
|
+
result: { type: "comment", content: " multiple spaces " },
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
input: "//\ttab character\n",
|
|
64
|
+
expected: {
|
|
65
|
+
success: true,
|
|
66
|
+
result: { type: "comment", content: "\ttab character" },
|
|
67
|
+
},
|
|
68
|
+
},
|
|
69
|
+
// Comments without newline (succeeds, consumes until EOF)
|
|
70
|
+
{
|
|
71
|
+
input: "// comment without newline",
|
|
72
|
+
expected: {
|
|
73
|
+
success: true,
|
|
74
|
+
result: { type: "comment", content: " comment without newline" },
|
|
75
|
+
},
|
|
76
|
+
},
|
|
77
|
+
// Failure cases
|
|
78
|
+
{ input: "/ single slash\n", expected: { success: false } },
|
|
79
|
+
{ input: "# not a comment\n", expected: { success: false } },
|
|
80
|
+
{ input: "", expected: { success: false } },
|
|
81
|
+
{ input: "text // comment\n", expected: { success: false } },
|
|
82
|
+
];
|
|
83
|
+
testCases.forEach(({ input, expected }) => {
|
|
84
|
+
if (expected.success) {
|
|
85
|
+
it(`should parse "${input.replace(/\n/g, "\\n")}" successfully`, () => {
|
|
86
|
+
const result = commentParser(input);
|
|
87
|
+
expect(result.success).toBe(true);
|
|
88
|
+
if (result.success) {
|
|
89
|
+
expect(result.result).toEqual(expected.result);
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
it(`should fail to parse "${input.replace(/\n/g, "\\n")}"`, () => {
|
|
95
|
+
const result = commentParser(input);
|
|
96
|
+
expect(result.success).toBe(false);
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { capture, char, manyWithJoin, noneOf, optional, or, sepBy, seqC, set, succeed, trace, } from "tarsec";
|
|
2
|
+
import { accessExpressionParser } from "./access.js";
|
|
3
|
+
import { functionCallParser } from "./functionCall.js";
|
|
4
|
+
import { literalParser } from "./literals.js";
|
|
5
|
+
import { comma, optionalSpaces } from "./utils.js";
|
|
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("]")));
|
|
8
|
+
return parser(input);
|
|
9
|
+
};
|
|
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")));
|
|
12
|
+
return parser(input);
|
|
13
|
+
};
|
|
14
|
+
export const agencyObjectParser = seqC(set("type", "agencyObject"), char("{"), optionalSpaces, capture(or(sepBy(comma, agencyObjectKVParser), succeed([])), "entries"), optional(char(",")), optionalSpaces, char("}"));
|