langchain 0.0.87 → 0.0.89
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/agents/chat_convo/prompt.cjs +2 -2
- package/dist/agents/chat_convo/prompt.d.ts +2 -2
- package/dist/agents/chat_convo/prompt.js +2 -2
- package/dist/agents/structured_chat/outputParser.cjs +2 -2
- package/dist/agents/structured_chat/outputParser.d.ts +2 -1
- package/dist/agents/structured_chat/outputParser.js +2 -2
- package/dist/agents/structured_chat/prompt.cjs +1 -1
- package/dist/agents/structured_chat/prompt.d.ts +1 -1
- package/dist/agents/structured_chat/prompt.js +1 -1
- package/dist/chains/query_constructor/index.cjs +3 -3
- package/dist/chains/query_constructor/index.d.ts +1 -1
- package/dist/chains/query_constructor/index.js +1 -1
- package/dist/chat_models/openai.cjs +111 -1
- package/dist/chat_models/openai.d.ts +11 -0
- package/dist/chat_models/openai.js +109 -0
- package/dist/document_loaders/fs/unstructured.cjs +6 -0
- package/dist/document_loaders/fs/unstructured.js +6 -0
- package/dist/experimental/generative_agents/generative_agent.cjs +276 -0
- package/dist/experimental/generative_agents/generative_agent.d.ts +43 -0
- package/dist/experimental/generative_agents/generative_agent.js +272 -0
- package/dist/experimental/generative_agents/generative_agent_memory.cjs +326 -0
- package/dist/experimental/generative_agents/generative_agent_memory.d.ts +51 -0
- package/dist/experimental/generative_agents/generative_agent_memory.js +322 -0
- package/dist/experimental/generative_agents/index.cjs +7 -0
- package/dist/experimental/generative_agents/index.d.ts +2 -0
- package/dist/experimental/generative_agents/index.js +2 -0
- package/dist/llms/openai-chat.cjs +29 -20
- package/dist/llms/openai-chat.d.ts +4 -0
- package/dist/llms/openai-chat.js +29 -20
- package/dist/llms/openai.cjs +31 -20
- package/dist/llms/openai.d.ts +3 -0
- package/dist/llms/openai.js +31 -20
- package/dist/output_parsers/expression.cjs +24 -6
- package/dist/output_parsers/expression.d.ts +9 -0
- package/dist/output_parsers/expression.js +24 -6
- package/dist/output_parsers/expression_type_handlers/array_literal_expression_handler.d.ts +2 -3
- package/dist/output_parsers/expression_type_handlers/base.cjs +11 -13
- package/dist/output_parsers/expression_type_handlers/base.d.ts +15 -17
- package/dist/output_parsers/expression_type_handlers/base.js +11 -13
- package/dist/output_parsers/expression_type_handlers/boolean_literal_handler.d.ts +2 -3
- package/dist/output_parsers/expression_type_handlers/call_expression_handler.d.ts +2 -3
- package/dist/output_parsers/expression_type_handlers/factory.d.ts +2 -3
- package/dist/output_parsers/expression_type_handlers/grammar/parser_grammar.cjs +296 -0
- package/dist/output_parsers/expression_type_handlers/grammar/parser_grammar.d.ts +13 -0
- package/dist/output_parsers/expression_type_handlers/grammar/parser_grammar.js +293 -0
- package/dist/output_parsers/expression_type_handlers/identifier_handler.d.ts +2 -3
- package/dist/output_parsers/expression_type_handlers/member_expression_handler.cjs +2 -2
- package/dist/output_parsers/expression_type_handlers/member_expression_handler.d.ts +2 -3
- package/dist/output_parsers/expression_type_handlers/member_expression_handler.js +2 -2
- package/dist/output_parsers/expression_type_handlers/numeric_literal_handler.d.ts +2 -3
- package/dist/output_parsers/expression_type_handlers/object_literal_expression_handler.cjs +1 -1
- package/dist/output_parsers/expression_type_handlers/object_literal_expression_handler.d.ts +2 -3
- package/dist/output_parsers/expression_type_handlers/object_literal_expression_handler.js +1 -1
- package/dist/output_parsers/expression_type_handlers/property_assignment_handler.cjs +1 -1
- package/dist/output_parsers/expression_type_handlers/property_assignment_handler.d.ts +2 -3
- package/dist/output_parsers/expression_type_handlers/property_assignment_handler.js +1 -1
- package/dist/output_parsers/expression_type_handlers/string_literal_handler.d.ts +2 -3
- package/dist/retrievers/self_query/index.cjs +1 -1
- package/dist/retrievers/self_query/index.js +2 -2
- package/dist/retrievers/time_weighted.cjs +6 -0
- package/dist/retrievers/time_weighted.d.ts +1 -0
- package/dist/retrievers/time_weighted.js +6 -0
- package/dist/retrievers/vespa.cjs +38 -0
- package/dist/retrievers/vespa.d.ts +19 -0
- package/dist/retrievers/vespa.js +34 -0
- package/dist/tools/brave_search.cjs +57 -0
- package/dist/tools/brave_search.d.ts +12 -0
- package/dist/tools/brave_search.js +53 -0
- package/dist/tools/index.cjs +3 -1
- package/dist/tools/index.d.ts +1 -0
- package/dist/tools/index.js +1 -0
- package/dist/util/prompt-layer.cjs +28 -0
- package/dist/util/prompt-layer.d.ts +3 -0
- package/dist/util/prompt-layer.js +24 -0
- package/dist/vectorstores/milvus.cjs +2 -0
- package/dist/vectorstores/milvus.d.ts +2 -0
- package/dist/vectorstores/milvus.js +2 -0
- package/dist/vectorstores/singlestore.cjs +109 -0
- package/dist/vectorstores/singlestore.d.ts +25 -0
- package/dist/vectorstores/singlestore.js +105 -0
- package/dist/vectorstores/supabase.cjs +15 -4
- package/dist/vectorstores/supabase.d.ts +6 -3
- package/dist/vectorstores/supabase.js +15 -4
- package/experimental/generative_agents.cjs +1 -0
- package/experimental/generative_agents.d.ts +1 -0
- package/experimental/generative_agents.js +1 -0
- package/package.json +39 -5
- package/retrievers/vespa.cjs +1 -0
- package/retrievers/vespa.d.ts +1 -0
- package/retrievers/vespa.js +1 -0
- package/vectorstores/singlestore.cjs +1 -0
- package/vectorstores/singlestore.d.ts +1 -0
- package/vectorstores/singlestore.js +1 -0
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Here's the main parser for our expression parser. It's a pared down
|
|
4
|
+
* Javascript parser with a whole lot of rules removed, leaving only
|
|
5
|
+
* rules for parsing literals (i.e. string literal,numeric literal,
|
|
6
|
+
* boolean literal, array literal, object literal and null literal),
|
|
7
|
+
* identifiers, and expressions (i.e. call expression, member expression,
|
|
8
|
+
* array expression and object expression).
|
|
9
|
+
*
|
|
10
|
+
* For more information see:
|
|
11
|
+
* https://peggyjs.org/documentation.html
|
|
12
|
+
* https://github.com/peggyjs/peggy/blob/main/examples/javascript.pegjs
|
|
13
|
+
*/
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.GRAMMAR = void 0;
|
|
16
|
+
exports.GRAMMAR = `{{
|
|
17
|
+
var TYPES_TO_PROPERTY_NAMES = {
|
|
18
|
+
CallExpression: "callee",
|
|
19
|
+
MemberExpression: "object",
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
function extractOptional(optional, index) {
|
|
23
|
+
return optional ? optional[index] : null;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
function extractList(list, index) {
|
|
27
|
+
return list.map(function(element) { return element[index]; });
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
function buildList(head, tail, index) {
|
|
31
|
+
return [head].concat(extractList(tail, index));
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
function optionalList(value) {
|
|
35
|
+
return value !== null ? value : [];
|
|
36
|
+
};
|
|
37
|
+
}}
|
|
38
|
+
|
|
39
|
+
Start
|
|
40
|
+
= __ program:Program __ { return program; };
|
|
41
|
+
|
|
42
|
+
SourceCharacter
|
|
43
|
+
= .;
|
|
44
|
+
|
|
45
|
+
WhiteSpace
|
|
46
|
+
= "\\t"
|
|
47
|
+
/ "\\v"
|
|
48
|
+
/ "\\f"
|
|
49
|
+
/ " "
|
|
50
|
+
/ "\\u00A0"
|
|
51
|
+
/ "\\uFEFF";
|
|
52
|
+
|
|
53
|
+
Identifier
|
|
54
|
+
= !ReservedWord name:IdentifierName { return name; };
|
|
55
|
+
|
|
56
|
+
IdentifierName
|
|
57
|
+
= head:IdentifierStart tail:IdentifierPart* {
|
|
58
|
+
return {
|
|
59
|
+
type: "Identifier",
|
|
60
|
+
name: head + tail.join("")
|
|
61
|
+
};
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
IdentifierStart
|
|
65
|
+
= UnicodeLetter
|
|
66
|
+
/ "$"
|
|
67
|
+
/ "_";
|
|
68
|
+
|
|
69
|
+
IdentifierPart
|
|
70
|
+
= IdentifierStart
|
|
71
|
+
/ Nd
|
|
72
|
+
/ "\\u200C"
|
|
73
|
+
/ "\\u200D";
|
|
74
|
+
|
|
75
|
+
UnicodeLetter
|
|
76
|
+
= Lu
|
|
77
|
+
/ Ll;
|
|
78
|
+
|
|
79
|
+
ReservedWord
|
|
80
|
+
= NullToken
|
|
81
|
+
/ TrueToken
|
|
82
|
+
/ FalseToken;
|
|
83
|
+
|
|
84
|
+
Literal
|
|
85
|
+
= NullLiteral
|
|
86
|
+
/ BooleanLiteral
|
|
87
|
+
/ NumericLiteral
|
|
88
|
+
/ StringLiteral;
|
|
89
|
+
|
|
90
|
+
NullLiteral
|
|
91
|
+
= NullToken { return { type: "NullLiteral", value: null }; };
|
|
92
|
+
|
|
93
|
+
BooleanLiteral
|
|
94
|
+
= TrueToken { return { type: "BooleanLiteral", value: true }; }
|
|
95
|
+
/ FalseToken { return { type: "BooleanLiteral", value: false }; };
|
|
96
|
+
|
|
97
|
+
NumericLiteral
|
|
98
|
+
= literal:DecimalLiteral !(IdentifierStart / DecimalDigit) {
|
|
99
|
+
return literal;
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
DecimalLiteral
|
|
103
|
+
= DecimalIntegerLiteral "." DecimalDigit* {
|
|
104
|
+
return { type: "NumericLiteral", value: parseFloat(text()) };
|
|
105
|
+
}
|
|
106
|
+
/ "." DecimalDigit+ {
|
|
107
|
+
return { type: "NumericLiteral", value: parseFloat(text()) };
|
|
108
|
+
}
|
|
109
|
+
/ DecimalIntegerLiteral {
|
|
110
|
+
return { type: "NumericLiteral", value: parseFloat(text()) };
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
DecimalIntegerLiteral
|
|
114
|
+
= "0"
|
|
115
|
+
/ NonZeroDigit DecimalDigit*;
|
|
116
|
+
|
|
117
|
+
DecimalDigit
|
|
118
|
+
= [0-9];
|
|
119
|
+
|
|
120
|
+
NonZeroDigit
|
|
121
|
+
= [1-9];
|
|
122
|
+
|
|
123
|
+
StringLiteral
|
|
124
|
+
= '"' chars:DoubleStringCharacter* '"' {
|
|
125
|
+
return { type: "StringLiteral", value: chars.join("") };
|
|
126
|
+
}
|
|
127
|
+
/ "'" chars:SingleStringCharacter* "'" {
|
|
128
|
+
return { type: "StringLiteral", value: chars.join("") };
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
DoubleStringCharacter
|
|
132
|
+
= !('"' / "\\\\") SourceCharacter { return text(); };
|
|
133
|
+
|
|
134
|
+
SingleStringCharacter
|
|
135
|
+
= !("'" / "\\\\") SourceCharacter { return text(); };
|
|
136
|
+
|
|
137
|
+
SingleEscapeCharacter
|
|
138
|
+
= "'"
|
|
139
|
+
/ '"';
|
|
140
|
+
|
|
141
|
+
Ll = [a-z];
|
|
142
|
+
Lu = [A-Z];
|
|
143
|
+
Nd = [0-9];
|
|
144
|
+
|
|
145
|
+
FalseToken = "false" !IdentifierPart;
|
|
146
|
+
TrueToken = "true" !IdentifierPart;
|
|
147
|
+
NullToken = "null" !IdentifierPart;
|
|
148
|
+
|
|
149
|
+
__ = WhiteSpace*;
|
|
150
|
+
|
|
151
|
+
PrimaryExpression
|
|
152
|
+
= Identifier
|
|
153
|
+
/ Literal
|
|
154
|
+
/ ArrayExpression
|
|
155
|
+
/ ObjectExpression
|
|
156
|
+
/ "(" __ expression:Expression __ ")" { return expression; };
|
|
157
|
+
|
|
158
|
+
ArrayExpression
|
|
159
|
+
= "[" __ "]" { return { type: "ArrayExpression", elements: [] }; }
|
|
160
|
+
/ "[" __ elements:ElementList __ "]" {
|
|
161
|
+
return {
|
|
162
|
+
type: "ArrayExpression",
|
|
163
|
+
elements: elements
|
|
164
|
+
};
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
ElementList
|
|
168
|
+
= head:(
|
|
169
|
+
element:Expression {
|
|
170
|
+
return element;
|
|
171
|
+
}
|
|
172
|
+
)
|
|
173
|
+
tail:(
|
|
174
|
+
__ "," __ element:Expression {
|
|
175
|
+
return element;
|
|
176
|
+
}
|
|
177
|
+
)*
|
|
178
|
+
{ return Array.prototype.concat.apply(head, tail); };
|
|
179
|
+
|
|
180
|
+
ObjectExpression
|
|
181
|
+
= "{" __ "}" { return { type: "ObjectExpression", properties: [] }; }
|
|
182
|
+
/ "{" __ properties:PropertyNameAndValueList __ "}" {
|
|
183
|
+
return { type: "ObjectExpression", properties: properties };
|
|
184
|
+
}
|
|
185
|
+
/ "{" __ properties:PropertyNameAndValueList __ "," __ "}" {
|
|
186
|
+
return { type: "ObjectExpression", properties: properties };
|
|
187
|
+
};
|
|
188
|
+
PropertyNameAndValueList
|
|
189
|
+
= head:PropertyAssignment tail:(__ "," __ PropertyAssignment)* {
|
|
190
|
+
return buildList(head, tail, 3);
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
PropertyAssignment
|
|
194
|
+
= key:PropertyName __ ":" __ value:Expression {
|
|
195
|
+
return { type: "PropertyAssignment", key: key, value: value, kind: "init" };
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
PropertyName
|
|
199
|
+
= IdentifierName
|
|
200
|
+
/ StringLiteral
|
|
201
|
+
/ NumericLiteral;
|
|
202
|
+
|
|
203
|
+
Node
|
|
204
|
+
= ArrayExpression
|
|
205
|
+
/ BooleanLiteral
|
|
206
|
+
/ CallExpression
|
|
207
|
+
/ Identifier
|
|
208
|
+
/ MemberExpression
|
|
209
|
+
/ NumericLiteral
|
|
210
|
+
/ ObjectExpression
|
|
211
|
+
/ PropertyAssignment
|
|
212
|
+
/ StringLiteral
|
|
213
|
+
|
|
214
|
+
MemberExpression
|
|
215
|
+
= head:(PrimaryExpression)
|
|
216
|
+
tail:(
|
|
217
|
+
__ "[" __ property:Expression __ "]" {
|
|
218
|
+
return { property: property, computed: true };
|
|
219
|
+
}
|
|
220
|
+
/ __ "." __ property:IdentifierName {
|
|
221
|
+
return { property: property, computed: false };
|
|
222
|
+
}
|
|
223
|
+
)*
|
|
224
|
+
{
|
|
225
|
+
return tail.reduce(function(result, element) {
|
|
226
|
+
return {
|
|
227
|
+
type: "MemberExpression",
|
|
228
|
+
object: result,
|
|
229
|
+
property: element.property,
|
|
230
|
+
computed: element.computed
|
|
231
|
+
};
|
|
232
|
+
}, head);
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
CallExpression
|
|
236
|
+
= head:(
|
|
237
|
+
callee:MemberExpression __ args:Arguments {
|
|
238
|
+
return { type: "CallExpression", callee: callee, arguments: args };
|
|
239
|
+
}
|
|
240
|
+
)
|
|
241
|
+
tail:(
|
|
242
|
+
__ args:Arguments {
|
|
243
|
+
return { type: "CallExpression", arguments: args };
|
|
244
|
+
}
|
|
245
|
+
/ __ "[" __ property:Expression __ "]" {
|
|
246
|
+
return {
|
|
247
|
+
type: "MemberExpression",
|
|
248
|
+
property: property,
|
|
249
|
+
computed: true
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
/ __ "." __ property:IdentifierName {
|
|
253
|
+
return {
|
|
254
|
+
type: "MemberExpression",
|
|
255
|
+
property: property,
|
|
256
|
+
computed: false
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
)*
|
|
260
|
+
{
|
|
261
|
+
return tail.reduce(function(result, element) {
|
|
262
|
+
element[TYPES_TO_PROPERTY_NAMES[element.type]] = result;
|
|
263
|
+
|
|
264
|
+
return element;
|
|
265
|
+
}, head);
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
Arguments
|
|
269
|
+
= "(" __ args:(ArgumentList __)? ")" {
|
|
270
|
+
return optionalList(extractOptional(args, 0));
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
ArgumentList
|
|
274
|
+
= head:Expression tail:(__ "," __ Expression)* {
|
|
275
|
+
return buildList(head, tail, 3);
|
|
276
|
+
};
|
|
277
|
+
|
|
278
|
+
Expression
|
|
279
|
+
= CallExpression
|
|
280
|
+
/ MemberExpression;
|
|
281
|
+
|
|
282
|
+
ExpressionStatement
|
|
283
|
+
= expression:Expression {
|
|
284
|
+
return {
|
|
285
|
+
type: "ExpressionStatement",
|
|
286
|
+
expression: expression
|
|
287
|
+
};
|
|
288
|
+
};
|
|
289
|
+
|
|
290
|
+
Program
|
|
291
|
+
= exp:ExpressionStatement {
|
|
292
|
+
return {
|
|
293
|
+
type: "Program",
|
|
294
|
+
body: exp
|
|
295
|
+
};
|
|
296
|
+
};`;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Here's the main parser for our expression parser. It's a pared down
|
|
3
|
+
* Javascript parser with a whole lot of rules removed, leaving only
|
|
4
|
+
* rules for parsing literals (i.e. string literal,numeric literal,
|
|
5
|
+
* boolean literal, array literal, object literal and null literal),
|
|
6
|
+
* identifiers, and expressions (i.e. call expression, member expression,
|
|
7
|
+
* array expression and object expression).
|
|
8
|
+
*
|
|
9
|
+
* For more information see:
|
|
10
|
+
* https://peggyjs.org/documentation.html
|
|
11
|
+
* https://github.com/peggyjs/peggy/blob/main/examples/javascript.pegjs
|
|
12
|
+
*/
|
|
13
|
+
export declare const GRAMMAR = "{{\n var TYPES_TO_PROPERTY_NAMES = {\n CallExpression: \"callee\",\n MemberExpression: \"object\",\n };\n \n function extractOptional(optional, index) {\n return optional ? optional[index] : null;\n };\n \n function extractList(list, index) {\n return list.map(function(element) { return element[index]; });\n };\n \n function buildList(head, tail, index) {\n return [head].concat(extractList(tail, index));\n };\n \n function optionalList(value) {\n return value !== null ? value : [];\n };\n }}\n \n Start\n = __ program:Program __ { return program; };\n \n SourceCharacter\n = .;\n \n WhiteSpace\n = \"\\t\"\n / \"\\v\"\n / \"\\f\"\n / \" \"\n / \"\\u00A0\"\n / \"\\uFEFF\";\n \n Identifier\n = !ReservedWord name:IdentifierName { return name; };\n \n IdentifierName\n = head:IdentifierStart tail:IdentifierPart* {\n return {\n type: \"Identifier\",\n name: head + tail.join(\"\")\n };\n };\n \n IdentifierStart\n = UnicodeLetter\n / \"$\"\n / \"_\";\n \n IdentifierPart\n = IdentifierStart\n / Nd\n / \"\\u200C\"\n / \"\\u200D\";\n \n UnicodeLetter\n = Lu\n / Ll;\n \n ReservedWord\n = NullToken\n / TrueToken\n / FalseToken;\n \n Literal\n = NullLiteral\n / BooleanLiteral\n / NumericLiteral\n / StringLiteral;\n \n NullLiteral\n = NullToken { return { type: \"NullLiteral\", value: null }; };\n \n BooleanLiteral\n = TrueToken { return { type: \"BooleanLiteral\", value: true }; }\n / FalseToken { return { type: \"BooleanLiteral\", value: false }; };\n \n NumericLiteral\n = literal:DecimalLiteral !(IdentifierStart / DecimalDigit) {\n return literal;\n };\n \n DecimalLiteral\n = DecimalIntegerLiteral \".\" DecimalDigit* {\n return { type: \"NumericLiteral\", value: parseFloat(text()) };\n }\n / \".\" DecimalDigit+ {\n return { type: \"NumericLiteral\", value: parseFloat(text()) };\n }\n / DecimalIntegerLiteral {\n return { type: \"NumericLiteral\", value: parseFloat(text()) };\n };\n \n DecimalIntegerLiteral\n = \"0\"\n / NonZeroDigit DecimalDigit*;\n \n DecimalDigit\n = [0-9];\n \n NonZeroDigit\n = [1-9];\n \n StringLiteral\n = '\"' chars:DoubleStringCharacter* '\"' {\n return { type: \"StringLiteral\", value: chars.join(\"\") };\n }\n / \"'\" chars:SingleStringCharacter* \"'\" {\n return { type: \"StringLiteral\", value: chars.join(\"\") };\n };\n \n DoubleStringCharacter\n = !('\"' / \"\\\\\") SourceCharacter { return text(); };\n \n SingleStringCharacter\n = !(\"'\" / \"\\\\\") SourceCharacter { return text(); };\n \n SingleEscapeCharacter\n = \"'\"\n / '\"';\n \n Ll = [a-z];\n Lu = [A-Z];\n Nd = [0-9];\n \n FalseToken = \"false\" !IdentifierPart;\n TrueToken = \"true\" !IdentifierPart;\n NullToken = \"null\" !IdentifierPart;\n \n __ = WhiteSpace*;\n \n PrimaryExpression\n = Identifier\n / Literal\n / ArrayExpression\n / ObjectExpression\n / \"(\" __ expression:Expression __ \")\" { return expression; };\n \n ArrayExpression\n = \"[\" __ \"]\" { return { type: \"ArrayExpression\", elements: [] }; }\n / \"[\" __ elements:ElementList __ \"]\" {\n return {\n type: \"ArrayExpression\",\n elements: elements\n };\n };\n \n ElementList\n = head:(\n element:Expression {\n return element;\n }\n )\n tail:(\n __ \",\" __ element:Expression {\n return element;\n }\n )*\n { return Array.prototype.concat.apply(head, tail); };\n \n ObjectExpression\n = \"{\" __ \"}\" { return { type: \"ObjectExpression\", properties: [] }; }\n / \"{\" __ properties:PropertyNameAndValueList __ \"}\" {\n return { type: \"ObjectExpression\", properties: properties };\n }\n / \"{\" __ properties:PropertyNameAndValueList __ \",\" __ \"}\" {\n return { type: \"ObjectExpression\", properties: properties };\n };\n PropertyNameAndValueList\n = head:PropertyAssignment tail:(__ \",\" __ PropertyAssignment)* {\n return buildList(head, tail, 3);\n };\n \n PropertyAssignment\n = key:PropertyName __ \":\" __ value:Expression {\n return { type: \"PropertyAssignment\", key: key, value: value, kind: \"init\" };\n };\n \n PropertyName\n = IdentifierName\n / StringLiteral\n / NumericLiteral;\n \n Node\n = ArrayExpression \n / BooleanLiteral \n / CallExpression \n / Identifier \n / MemberExpression \n / NumericLiteral \n / ObjectExpression \n / PropertyAssignment \n / StringLiteral\n \n MemberExpression\n = head:(PrimaryExpression)\n tail:(\n __ \"[\" __ property:Expression __ \"]\" {\n return { property: property, computed: true };\n }\n / __ \".\" __ property:IdentifierName {\n return { property: property, computed: false };\n }\n )*\n {\n return tail.reduce(function(result, element) {\n return {\n type: \"MemberExpression\",\n object: result,\n property: element.property,\n computed: element.computed\n };\n }, head);\n };\n \n CallExpression\n = head:(\n callee:MemberExpression __ args:Arguments {\n return { type: \"CallExpression\", callee: callee, arguments: args };\n }\n )\n tail:(\n __ args:Arguments {\n return { type: \"CallExpression\", arguments: args };\n }\n / __ \"[\" __ property:Expression __ \"]\" {\n return {\n type: \"MemberExpression\",\n property: property,\n computed: true\n };\n }\n / __ \".\" __ property:IdentifierName {\n return {\n type: \"MemberExpression\",\n property: property,\n computed: false\n };\n }\n )*\n {\n return tail.reduce(function(result, element) {\n element[TYPES_TO_PROPERTY_NAMES[element.type]] = result;\n \n return element;\n }, head);\n };\n \n Arguments\n = \"(\" __ args:(ArgumentList __)? \")\" {\n return optionalList(extractOptional(args, 0));\n };\n \n ArgumentList\n = head:Expression tail:(__ \",\" __ Expression)* {\n return buildList(head, tail, 3);\n };\n \n Expression\n = CallExpression\n / MemberExpression;\n \n ExpressionStatement\n = expression:Expression {\n return {\n type: \"ExpressionStatement\",\n expression: expression\n };\n };\n \n Program\n = exp:ExpressionStatement {\n return {\n type: \"Program\",\n body: exp\n };\n };";
|
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Here's the main parser for our expression parser. It's a pared down
|
|
3
|
+
* Javascript parser with a whole lot of rules removed, leaving only
|
|
4
|
+
* rules for parsing literals (i.e. string literal,numeric literal,
|
|
5
|
+
* boolean literal, array literal, object literal and null literal),
|
|
6
|
+
* identifiers, and expressions (i.e. call expression, member expression,
|
|
7
|
+
* array expression and object expression).
|
|
8
|
+
*
|
|
9
|
+
* For more information see:
|
|
10
|
+
* https://peggyjs.org/documentation.html
|
|
11
|
+
* https://github.com/peggyjs/peggy/blob/main/examples/javascript.pegjs
|
|
12
|
+
*/
|
|
13
|
+
export const GRAMMAR = `{{
|
|
14
|
+
var TYPES_TO_PROPERTY_NAMES = {
|
|
15
|
+
CallExpression: "callee",
|
|
16
|
+
MemberExpression: "object",
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
function extractOptional(optional, index) {
|
|
20
|
+
return optional ? optional[index] : null;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
function extractList(list, index) {
|
|
24
|
+
return list.map(function(element) { return element[index]; });
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
function buildList(head, tail, index) {
|
|
28
|
+
return [head].concat(extractList(tail, index));
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
function optionalList(value) {
|
|
32
|
+
return value !== null ? value : [];
|
|
33
|
+
};
|
|
34
|
+
}}
|
|
35
|
+
|
|
36
|
+
Start
|
|
37
|
+
= __ program:Program __ { return program; };
|
|
38
|
+
|
|
39
|
+
SourceCharacter
|
|
40
|
+
= .;
|
|
41
|
+
|
|
42
|
+
WhiteSpace
|
|
43
|
+
= "\\t"
|
|
44
|
+
/ "\\v"
|
|
45
|
+
/ "\\f"
|
|
46
|
+
/ " "
|
|
47
|
+
/ "\\u00A0"
|
|
48
|
+
/ "\\uFEFF";
|
|
49
|
+
|
|
50
|
+
Identifier
|
|
51
|
+
= !ReservedWord name:IdentifierName { return name; };
|
|
52
|
+
|
|
53
|
+
IdentifierName
|
|
54
|
+
= head:IdentifierStart tail:IdentifierPart* {
|
|
55
|
+
return {
|
|
56
|
+
type: "Identifier",
|
|
57
|
+
name: head + tail.join("")
|
|
58
|
+
};
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
IdentifierStart
|
|
62
|
+
= UnicodeLetter
|
|
63
|
+
/ "$"
|
|
64
|
+
/ "_";
|
|
65
|
+
|
|
66
|
+
IdentifierPart
|
|
67
|
+
= IdentifierStart
|
|
68
|
+
/ Nd
|
|
69
|
+
/ "\\u200C"
|
|
70
|
+
/ "\\u200D";
|
|
71
|
+
|
|
72
|
+
UnicodeLetter
|
|
73
|
+
= Lu
|
|
74
|
+
/ Ll;
|
|
75
|
+
|
|
76
|
+
ReservedWord
|
|
77
|
+
= NullToken
|
|
78
|
+
/ TrueToken
|
|
79
|
+
/ FalseToken;
|
|
80
|
+
|
|
81
|
+
Literal
|
|
82
|
+
= NullLiteral
|
|
83
|
+
/ BooleanLiteral
|
|
84
|
+
/ NumericLiteral
|
|
85
|
+
/ StringLiteral;
|
|
86
|
+
|
|
87
|
+
NullLiteral
|
|
88
|
+
= NullToken { return { type: "NullLiteral", value: null }; };
|
|
89
|
+
|
|
90
|
+
BooleanLiteral
|
|
91
|
+
= TrueToken { return { type: "BooleanLiteral", value: true }; }
|
|
92
|
+
/ FalseToken { return { type: "BooleanLiteral", value: false }; };
|
|
93
|
+
|
|
94
|
+
NumericLiteral
|
|
95
|
+
= literal:DecimalLiteral !(IdentifierStart / DecimalDigit) {
|
|
96
|
+
return literal;
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
DecimalLiteral
|
|
100
|
+
= DecimalIntegerLiteral "." DecimalDigit* {
|
|
101
|
+
return { type: "NumericLiteral", value: parseFloat(text()) };
|
|
102
|
+
}
|
|
103
|
+
/ "." DecimalDigit+ {
|
|
104
|
+
return { type: "NumericLiteral", value: parseFloat(text()) };
|
|
105
|
+
}
|
|
106
|
+
/ DecimalIntegerLiteral {
|
|
107
|
+
return { type: "NumericLiteral", value: parseFloat(text()) };
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
DecimalIntegerLiteral
|
|
111
|
+
= "0"
|
|
112
|
+
/ NonZeroDigit DecimalDigit*;
|
|
113
|
+
|
|
114
|
+
DecimalDigit
|
|
115
|
+
= [0-9];
|
|
116
|
+
|
|
117
|
+
NonZeroDigit
|
|
118
|
+
= [1-9];
|
|
119
|
+
|
|
120
|
+
StringLiteral
|
|
121
|
+
= '"' chars:DoubleStringCharacter* '"' {
|
|
122
|
+
return { type: "StringLiteral", value: chars.join("") };
|
|
123
|
+
}
|
|
124
|
+
/ "'" chars:SingleStringCharacter* "'" {
|
|
125
|
+
return { type: "StringLiteral", value: chars.join("") };
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
DoubleStringCharacter
|
|
129
|
+
= !('"' / "\\\\") SourceCharacter { return text(); };
|
|
130
|
+
|
|
131
|
+
SingleStringCharacter
|
|
132
|
+
= !("'" / "\\\\") SourceCharacter { return text(); };
|
|
133
|
+
|
|
134
|
+
SingleEscapeCharacter
|
|
135
|
+
= "'"
|
|
136
|
+
/ '"';
|
|
137
|
+
|
|
138
|
+
Ll = [a-z];
|
|
139
|
+
Lu = [A-Z];
|
|
140
|
+
Nd = [0-9];
|
|
141
|
+
|
|
142
|
+
FalseToken = "false" !IdentifierPart;
|
|
143
|
+
TrueToken = "true" !IdentifierPart;
|
|
144
|
+
NullToken = "null" !IdentifierPart;
|
|
145
|
+
|
|
146
|
+
__ = WhiteSpace*;
|
|
147
|
+
|
|
148
|
+
PrimaryExpression
|
|
149
|
+
= Identifier
|
|
150
|
+
/ Literal
|
|
151
|
+
/ ArrayExpression
|
|
152
|
+
/ ObjectExpression
|
|
153
|
+
/ "(" __ expression:Expression __ ")" { return expression; };
|
|
154
|
+
|
|
155
|
+
ArrayExpression
|
|
156
|
+
= "[" __ "]" { return { type: "ArrayExpression", elements: [] }; }
|
|
157
|
+
/ "[" __ elements:ElementList __ "]" {
|
|
158
|
+
return {
|
|
159
|
+
type: "ArrayExpression",
|
|
160
|
+
elements: elements
|
|
161
|
+
};
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
ElementList
|
|
165
|
+
= head:(
|
|
166
|
+
element:Expression {
|
|
167
|
+
return element;
|
|
168
|
+
}
|
|
169
|
+
)
|
|
170
|
+
tail:(
|
|
171
|
+
__ "," __ element:Expression {
|
|
172
|
+
return element;
|
|
173
|
+
}
|
|
174
|
+
)*
|
|
175
|
+
{ return Array.prototype.concat.apply(head, tail); };
|
|
176
|
+
|
|
177
|
+
ObjectExpression
|
|
178
|
+
= "{" __ "}" { return { type: "ObjectExpression", properties: [] }; }
|
|
179
|
+
/ "{" __ properties:PropertyNameAndValueList __ "}" {
|
|
180
|
+
return { type: "ObjectExpression", properties: properties };
|
|
181
|
+
}
|
|
182
|
+
/ "{" __ properties:PropertyNameAndValueList __ "," __ "}" {
|
|
183
|
+
return { type: "ObjectExpression", properties: properties };
|
|
184
|
+
};
|
|
185
|
+
PropertyNameAndValueList
|
|
186
|
+
= head:PropertyAssignment tail:(__ "," __ PropertyAssignment)* {
|
|
187
|
+
return buildList(head, tail, 3);
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
PropertyAssignment
|
|
191
|
+
= key:PropertyName __ ":" __ value:Expression {
|
|
192
|
+
return { type: "PropertyAssignment", key: key, value: value, kind: "init" };
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
PropertyName
|
|
196
|
+
= IdentifierName
|
|
197
|
+
/ StringLiteral
|
|
198
|
+
/ NumericLiteral;
|
|
199
|
+
|
|
200
|
+
Node
|
|
201
|
+
= ArrayExpression
|
|
202
|
+
/ BooleanLiteral
|
|
203
|
+
/ CallExpression
|
|
204
|
+
/ Identifier
|
|
205
|
+
/ MemberExpression
|
|
206
|
+
/ NumericLiteral
|
|
207
|
+
/ ObjectExpression
|
|
208
|
+
/ PropertyAssignment
|
|
209
|
+
/ StringLiteral
|
|
210
|
+
|
|
211
|
+
MemberExpression
|
|
212
|
+
= head:(PrimaryExpression)
|
|
213
|
+
tail:(
|
|
214
|
+
__ "[" __ property:Expression __ "]" {
|
|
215
|
+
return { property: property, computed: true };
|
|
216
|
+
}
|
|
217
|
+
/ __ "." __ property:IdentifierName {
|
|
218
|
+
return { property: property, computed: false };
|
|
219
|
+
}
|
|
220
|
+
)*
|
|
221
|
+
{
|
|
222
|
+
return tail.reduce(function(result, element) {
|
|
223
|
+
return {
|
|
224
|
+
type: "MemberExpression",
|
|
225
|
+
object: result,
|
|
226
|
+
property: element.property,
|
|
227
|
+
computed: element.computed
|
|
228
|
+
};
|
|
229
|
+
}, head);
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
CallExpression
|
|
233
|
+
= head:(
|
|
234
|
+
callee:MemberExpression __ args:Arguments {
|
|
235
|
+
return { type: "CallExpression", callee: callee, arguments: args };
|
|
236
|
+
}
|
|
237
|
+
)
|
|
238
|
+
tail:(
|
|
239
|
+
__ args:Arguments {
|
|
240
|
+
return { type: "CallExpression", arguments: args };
|
|
241
|
+
}
|
|
242
|
+
/ __ "[" __ property:Expression __ "]" {
|
|
243
|
+
return {
|
|
244
|
+
type: "MemberExpression",
|
|
245
|
+
property: property,
|
|
246
|
+
computed: true
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
/ __ "." __ property:IdentifierName {
|
|
250
|
+
return {
|
|
251
|
+
type: "MemberExpression",
|
|
252
|
+
property: property,
|
|
253
|
+
computed: false
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
)*
|
|
257
|
+
{
|
|
258
|
+
return tail.reduce(function(result, element) {
|
|
259
|
+
element[TYPES_TO_PROPERTY_NAMES[element.type]] = result;
|
|
260
|
+
|
|
261
|
+
return element;
|
|
262
|
+
}, head);
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
Arguments
|
|
266
|
+
= "(" __ args:(ArgumentList __)? ")" {
|
|
267
|
+
return optionalList(extractOptional(args, 0));
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
ArgumentList
|
|
271
|
+
= head:Expression tail:(__ "," __ Expression)* {
|
|
272
|
+
return buildList(head, tail, 3);
|
|
273
|
+
};
|
|
274
|
+
|
|
275
|
+
Expression
|
|
276
|
+
= CallExpression
|
|
277
|
+
/ MemberExpression;
|
|
278
|
+
|
|
279
|
+
ExpressionStatement
|
|
280
|
+
= expression:Expression {
|
|
281
|
+
return {
|
|
282
|
+
type: "ExpressionStatement",
|
|
283
|
+
expression: expression
|
|
284
|
+
};
|
|
285
|
+
};
|
|
286
|
+
|
|
287
|
+
Program
|
|
288
|
+
= exp:ExpressionStatement {
|
|
289
|
+
return {
|
|
290
|
+
type: "Program",
|
|
291
|
+
body: exp
|
|
292
|
+
};
|
|
293
|
+
};`;
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import type { ESTree } from "meriyah";
|
|
2
1
|
import { NodeHandler } from "./base.js";
|
|
3
2
|
import { IdentifierType } from "./types.js";
|
|
4
3
|
export declare class IdentifierHandler extends NodeHandler {
|
|
5
|
-
accepts(node:
|
|
6
|
-
handle(node:
|
|
4
|
+
accepts(node: ExpressionNode): Promise<Identifier | boolean>;
|
|
5
|
+
handle(node: Identifier): Promise<IdentifierType>;
|
|
7
6
|
}
|
|
@@ -30,13 +30,13 @@ class MemberExpressionHandler extends base_js_1.NodeHandler {
|
|
|
30
30
|
if (base_js_1.ASTParser.isIdentifier(object)) {
|
|
31
31
|
identifier = object.name.replace(/^["'](.+(?=["']$))["']$/, "$1");
|
|
32
32
|
}
|
|
33
|
-
else if (base_js_1.ASTParser.
|
|
33
|
+
else if (base_js_1.ASTParser.isStringLiteral(object)) {
|
|
34
34
|
identifier = `${object.value}`.replace(/^["'](.+(?=["']$))["']$/, "$1");
|
|
35
35
|
}
|
|
36
36
|
else {
|
|
37
37
|
throw new Error("Invalid object type");
|
|
38
38
|
}
|
|
39
|
-
if (object.type !== "Identifier" && object.type !== "
|
|
39
|
+
if (object.type !== "Identifier" && object.type !== "StringLiteral") {
|
|
40
40
|
throw new Error("ArrayExpression is not supported");
|
|
41
41
|
}
|
|
42
42
|
return { type: "member_expression", identifier, property: prop };
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import type { ESTree } from "meriyah";
|
|
2
1
|
import { NodeHandler } from "./base.js";
|
|
3
2
|
import { MemberExpressionType } from "./types.js";
|
|
4
3
|
export declare class MemberExpressionHandler extends NodeHandler {
|
|
5
|
-
accepts(node:
|
|
6
|
-
handle(node:
|
|
4
|
+
accepts(node: ExpressionNode): Promise<MemberExpression | boolean>;
|
|
5
|
+
handle(node: MemberExpression): Promise<MemberExpressionType>;
|
|
7
6
|
}
|
|
@@ -27,13 +27,13 @@ export class MemberExpressionHandler extends NodeHandler {
|
|
|
27
27
|
if (ASTParser.isIdentifier(object)) {
|
|
28
28
|
identifier = object.name.replace(/^["'](.+(?=["']$))["']$/, "$1");
|
|
29
29
|
}
|
|
30
|
-
else if (ASTParser.
|
|
30
|
+
else if (ASTParser.isStringLiteral(object)) {
|
|
31
31
|
identifier = `${object.value}`.replace(/^["'](.+(?=["']$))["']$/, "$1");
|
|
32
32
|
}
|
|
33
33
|
else {
|
|
34
34
|
throw new Error("Invalid object type");
|
|
35
35
|
}
|
|
36
|
-
if (object.type !== "Identifier" && object.type !== "
|
|
36
|
+
if (object.type !== "Identifier" && object.type !== "StringLiteral") {
|
|
37
37
|
throw new Error("ArrayExpression is not supported");
|
|
38
38
|
}
|
|
39
39
|
return { type: "member_expression", identifier, property: prop };
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import type { ESTree } from "meriyah";
|
|
2
1
|
import { NodeHandler } from "./base.js";
|
|
3
2
|
import { NumericLiteralType } from "./types.js";
|
|
4
3
|
export declare class NumericLiteralHandler extends NodeHandler {
|
|
5
|
-
accepts(node:
|
|
6
|
-
handle(node:
|
|
4
|
+
accepts(node: ExpressionNode): Promise<NumericLiteral | boolean>;
|
|
5
|
+
handle(node: NumericLiteral): Promise<NumericLiteralType>;
|
|
7
6
|
}
|
|
@@ -19,7 +19,7 @@ class ObjectLiteralExpressionHandler extends base_js_1.NodeHandler {
|
|
|
19
19
|
const values = [];
|
|
20
20
|
const { properties } = node;
|
|
21
21
|
for (const property of properties) {
|
|
22
|
-
if (base_js_1.ASTParser.
|
|
22
|
+
if (base_js_1.ASTParser.isPropertyAssignment(property)) {
|
|
23
23
|
values.push(await new property_assignment_handler_js_1.PropertyAssignmentHandler(this.parentHandler).handle(property));
|
|
24
24
|
}
|
|
25
25
|
}
|