flowquery 1.0.0

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.
Files changed (128) hide show
  1. package/.github/workflows/npm-publish.yml +30 -0
  2. package/.github/workflows/release.yml +84 -0
  3. package/CODE_OF_CONDUCT.md +10 -0
  4. package/FlowQueryLogoIcon.png +0 -0
  5. package/LICENSE +21 -0
  6. package/README.md +113 -0
  7. package/SECURITY.md +14 -0
  8. package/SUPPORT.md +13 -0
  9. package/docs/flowquery.min.js +1 -0
  10. package/docs/index.html +105 -0
  11. package/flowquery-vscode/.vscode-test.mjs +5 -0
  12. package/flowquery-vscode/.vscodeignore +13 -0
  13. package/flowquery-vscode/LICENSE +21 -0
  14. package/flowquery-vscode/README.md +11 -0
  15. package/flowquery-vscode/demo/FlowQueryVSCodeDemo.gif +0 -0
  16. package/flowquery-vscode/eslint.config.mjs +25 -0
  17. package/flowquery-vscode/extension.js +508 -0
  18. package/flowquery-vscode/flowQueryEngine/flowquery.min.js +1 -0
  19. package/flowquery-vscode/flowquery-worker.js +66 -0
  20. package/flowquery-vscode/images/FlowQueryLogoIcon.png +0 -0
  21. package/flowquery-vscode/jsconfig.json +13 -0
  22. package/flowquery-vscode/libs/page.css +53 -0
  23. package/flowquery-vscode/libs/table.css +13 -0
  24. package/flowquery-vscode/libs/tabs.css +66 -0
  25. package/flowquery-vscode/package-lock.json +2917 -0
  26. package/flowquery-vscode/package.json +51 -0
  27. package/flowquery-vscode/test/extension.test.js +196 -0
  28. package/flowquery-vscode/test/worker.test.js +25 -0
  29. package/flowquery-vscode/vsc-extension-quickstart.md +42 -0
  30. package/jest.config.js +11 -0
  31. package/package.json +28 -0
  32. package/queries/analyze_catfacts.cql +75 -0
  33. package/queries/azure_openai_completions.cql +13 -0
  34. package/queries/azure_openai_models.cql +9 -0
  35. package/queries/mock_pipeline.cql +84 -0
  36. package/queries/openai_completions.cql +15 -0
  37. package/queries/openai_models.cql +13 -0
  38. package/queries/test.cql +6 -0
  39. package/queries/tool_inference.cql +24 -0
  40. package/queries/wisdom.cql +6 -0
  41. package/queries/wisdom_letter_histogram.cql +8 -0
  42. package/src/compute/runner.ts +65 -0
  43. package/src/index.browser.ts +11 -0
  44. package/src/index.ts +12 -0
  45. package/src/io/command_line.ts +74 -0
  46. package/src/parsing/alias.ts +23 -0
  47. package/src/parsing/alias_option.ts +5 -0
  48. package/src/parsing/ast_node.ts +153 -0
  49. package/src/parsing/base_parser.ts +92 -0
  50. package/src/parsing/components/csv.ts +9 -0
  51. package/src/parsing/components/from.ts +12 -0
  52. package/src/parsing/components/headers.ts +12 -0
  53. package/src/parsing/components/json.ts +9 -0
  54. package/src/parsing/components/null.ts +9 -0
  55. package/src/parsing/components/post.ts +9 -0
  56. package/src/parsing/components/text.ts +9 -0
  57. package/src/parsing/context.ts +48 -0
  58. package/src/parsing/data_structures/associative_array.ts +43 -0
  59. package/src/parsing/data_structures/json_array.ts +31 -0
  60. package/src/parsing/data_structures/key_value_pair.ts +37 -0
  61. package/src/parsing/data_structures/lookup.ts +40 -0
  62. package/src/parsing/data_structures/range_lookup.ts +36 -0
  63. package/src/parsing/expressions/expression.ts +142 -0
  64. package/src/parsing/expressions/f_string.ts +26 -0
  65. package/src/parsing/expressions/identifier.ts +22 -0
  66. package/src/parsing/expressions/number.ts +40 -0
  67. package/src/parsing/expressions/operator.ts +179 -0
  68. package/src/parsing/expressions/reference.ts +42 -0
  69. package/src/parsing/expressions/string.ts +34 -0
  70. package/src/parsing/functions/aggregate_function.ts +58 -0
  71. package/src/parsing/functions/avg.ts +37 -0
  72. package/src/parsing/functions/collect.ts +44 -0
  73. package/src/parsing/functions/function.ts +60 -0
  74. package/src/parsing/functions/function_factory.ts +66 -0
  75. package/src/parsing/functions/join.ts +26 -0
  76. package/src/parsing/functions/predicate_function.ts +44 -0
  77. package/src/parsing/functions/predicate_function_factory.ts +15 -0
  78. package/src/parsing/functions/predicate_sum.ts +29 -0
  79. package/src/parsing/functions/rand.ts +13 -0
  80. package/src/parsing/functions/range.ts +18 -0
  81. package/src/parsing/functions/reducer_element.ts +10 -0
  82. package/src/parsing/functions/replace.ts +19 -0
  83. package/src/parsing/functions/round.ts +17 -0
  84. package/src/parsing/functions/size.ts +17 -0
  85. package/src/parsing/functions/split.ts +26 -0
  86. package/src/parsing/functions/stringify.ts +26 -0
  87. package/src/parsing/functions/sum.ts +31 -0
  88. package/src/parsing/functions/to_json.ts +17 -0
  89. package/src/parsing/functions/value_holder.ts +13 -0
  90. package/src/parsing/logic/case.ts +26 -0
  91. package/src/parsing/logic/else.ts +12 -0
  92. package/src/parsing/logic/end.ts +9 -0
  93. package/src/parsing/logic/then.ts +12 -0
  94. package/src/parsing/logic/when.ts +12 -0
  95. package/src/parsing/operations/aggregated_return.ts +18 -0
  96. package/src/parsing/operations/aggregated_with.ts +18 -0
  97. package/src/parsing/operations/group_by.ts +124 -0
  98. package/src/parsing/operations/limit.ts +22 -0
  99. package/src/parsing/operations/load.ts +92 -0
  100. package/src/parsing/operations/operation.ts +65 -0
  101. package/src/parsing/operations/projection.ts +18 -0
  102. package/src/parsing/operations/return.ts +43 -0
  103. package/src/parsing/operations/unwind.ts +32 -0
  104. package/src/parsing/operations/where.ts +38 -0
  105. package/src/parsing/operations/with.ts +20 -0
  106. package/src/parsing/parser.ts +762 -0
  107. package/src/parsing/token_to_node.ts +91 -0
  108. package/src/tokenization/keyword.ts +43 -0
  109. package/src/tokenization/operator.ts +25 -0
  110. package/src/tokenization/string_walker.ts +194 -0
  111. package/src/tokenization/symbol.ts +15 -0
  112. package/src/tokenization/token.ts +633 -0
  113. package/src/tokenization/token_mapper.ts +53 -0
  114. package/src/tokenization/token_type.ts +15 -0
  115. package/src/tokenization/tokenizer.ts +229 -0
  116. package/src/tokenization/trie.ts +117 -0
  117. package/src/utils/object_utils.ts +17 -0
  118. package/src/utils/string_utils.ts +114 -0
  119. package/tests/compute/runner.test.ts +498 -0
  120. package/tests/parsing/context.test.ts +27 -0
  121. package/tests/parsing/expression.test.ts +40 -0
  122. package/tests/parsing/parser.test.ts +434 -0
  123. package/tests/tokenization/token_mapper.test.ts +47 -0
  124. package/tests/tokenization/tokenizer.test.ts +67 -0
  125. package/tests/tokenization/trie.test.ts +20 -0
  126. package/tsconfig.json +15 -0
  127. package/typedoc.json +16 -0
  128. package/webpack.config.js +26 -0
@@ -0,0 +1,91 @@
1
+ import { Add, Subtract, Multiply, Divide, Modulo, Power, Equals, LessThan, GreaterThan, GreaterThanOrEqual, LessThanOrEqual, And, Or, Not, Is } from './expressions/operator';
2
+ import String from './expressions/string';
3
+ import Identifier from './expressions/identifier';
4
+ import Number from './expressions/number';
5
+ import Token from '../tokenization/token';
6
+ import ASTNode from './ast_node';
7
+ import JSON from './components/json';
8
+ import CSV from './components/csv';
9
+ import Text from './components/text';
10
+ import When from './logic/when';
11
+ import Then from './logic/then';
12
+ import Else from './logic/else';
13
+ import End from './logic/end';
14
+ import Null from './components/null';
15
+ class TokenToNode {
16
+ public static convert(token: Token): ASTNode {
17
+ if(token.isNumber()) {
18
+ if(token.value === null) {
19
+ throw new Error('Number token has no value');
20
+ }
21
+ return new Number(token.value);
22
+ } else if(token.isString()) {
23
+ if(token.value === null) {
24
+ throw new Error('String token has no value');
25
+ }
26
+ return new String(token.value);
27
+ } else if(token.isIdentifier()) {
28
+ if(token.value === null) {
29
+ throw new Error('Identifier token has no value');
30
+ }
31
+ return new Identifier(token.value);
32
+ } else if(token.isOperator()) {
33
+ if(token.isAdd()) {
34
+ return new Add();
35
+ } else if(token.isSubtract()) {
36
+ return new Subtract();
37
+ } else if(token.isMultiply()) {
38
+ return new Multiply();
39
+ } else if(token.isDivide()) {
40
+ return new Divide();
41
+ } else if(token.isModulo()) {
42
+ return new Modulo();
43
+ } else if(token.isExponent()) {
44
+ return new Power();
45
+ } else if(token.isEquals()) {
46
+ return new Equals();
47
+ } else if(token.isLessThan()) {
48
+ return new LessThan();
49
+ } else if(token.isGreaterThan()) {
50
+ return new GreaterThan();
51
+ } else if(token.isGreaterThanOrEqual()) {
52
+ return new GreaterThanOrEqual();
53
+ } else if(token.isLessThanOrEqual()) {
54
+ return new LessThanOrEqual();
55
+ } else if(token.isAnd()) {
56
+ return new And();
57
+ } else if(token.isOr()) {
58
+ return new Or();
59
+ } else if(token.isIs()) {
60
+ return new Is();
61
+ }
62
+ } else if(token.isUnaryOperator()) {
63
+ if(token.isNot()) {
64
+ return new Not();
65
+ }
66
+ } else if (token.isKeyword()) {
67
+ if(token.isJSON()) {
68
+ return new JSON();
69
+ } else if(token.isCSV()) {
70
+ return new CSV();
71
+ } else if(token.isText()) {
72
+ return new Text();
73
+ } else if(token.isWhen()) {
74
+ return new When();
75
+ } else if(token.isThen()) {
76
+ return new Then();
77
+ } else if(token.isElse()) {
78
+ return new Else();
79
+ } else if(token.isEnd()) {
80
+ return new End();
81
+ } else if(token.isNull()) {
82
+ return new Null();
83
+ }
84
+ } else {
85
+ throw new Error('Unknown token');
86
+ }
87
+ return new ASTNode();
88
+ }
89
+ }
90
+
91
+ export default TokenToNode;
@@ -0,0 +1,43 @@
1
+ enum Keyword {
2
+ RETURN = 'RETURN',
3
+ MATCH = 'MATCH',
4
+ WHERE = 'WHERE',
5
+ CREATE = 'CREATE',
6
+ MERGE = 'MERGE',
7
+ DELETE = 'DELETE',
8
+ DETACH = 'DETACH',
9
+ SET = 'SET',
10
+ REMOVE = 'REMOVE',
11
+ FOREACH = 'FOREACH',
12
+ WITH = 'WITH',
13
+ CALL = 'CALL',
14
+ YIELD = 'YIELD',
15
+ LOAD = 'LOAD',
16
+ HEADERS = 'HEADERS',
17
+ POST = 'POST',
18
+ FROM = 'FROM',
19
+ CSV = 'CSV',
20
+ JSON = 'JSON',
21
+ TEXT = 'TEXT',
22
+ AS = 'AS',
23
+ UNWIND = 'UNWIND',
24
+ SUM = 'SUM',
25
+ COLLECT = 'COLLECT',
26
+ DISTINCT = 'DISTINCT',
27
+ ORDER = 'ORDER',
28
+ BY = 'BY',
29
+ ASC = 'ASC',
30
+ DESC = 'DESC',
31
+ SKIP = 'SKIP',
32
+ LIMIT = 'LIMIT',
33
+ EOF = 'EOF',
34
+ CASE = 'CASE',
35
+ WHEN = 'WHEN',
36
+ THEN = 'THEN',
37
+ ELSE = 'ELSE',
38
+ END = 'END',
39
+ NULL = 'NULL',
40
+ IN = 'IN',
41
+ }
42
+
43
+ export default Keyword;
@@ -0,0 +1,25 @@
1
+ enum Operator {
2
+ // Arithmetic
3
+ ADD = '+',
4
+ SUBTRACT = '-',
5
+ MULTIPLY = '*',
6
+ DIVIDE = '/',
7
+ MODULO = '%',
8
+ EXPONENT = '^',
9
+ // Comparison
10
+ EQUALS = '=',
11
+ NOT_EQUALS = '<>',
12
+ LESS_THAN = '<',
13
+ LESS_THAN_OR_EQUAL = '<=',
14
+ GREATER_THAN = '>',
15
+ GREATER_THAN_OR_EQUAL = '>=',
16
+ IS = 'IS',
17
+ // Logical
18
+ AND = 'AND',
19
+ OR = 'OR',
20
+ NOT = 'NOT',
21
+ IN = 'IN',
22
+ PIPE = '|',
23
+ }
24
+
25
+ export default Operator;
@@ -0,0 +1,194 @@
1
+ import StringUtils from "../utils/string_utils";
2
+
3
+ /**
4
+ * Utility class for walking through a string character by character during tokenization.
5
+ *
6
+ * Provides methods to check for specific character patterns, move through the string,
7
+ * and extract substrings. Used by the Tokenizer to process input text.
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * const walker = new StringWalker("WITH x as variable");
12
+ * while (!walker.isAtEnd) {
13
+ * // Process characters
14
+ * }
15
+ * ```
16
+ */
17
+ class StringWalker {
18
+ private _position: number;
19
+ private readonly text: string;
20
+
21
+ /**
22
+ * Creates a new StringWalker for the given text.
23
+ *
24
+ * @param text - The input text to walk through
25
+ */
26
+ constructor(text: string) {
27
+ this.text = text;
28
+ this._position = 0;
29
+ }
30
+
31
+ public get position(): number {
32
+ return this._position;
33
+ }
34
+
35
+ public get currentChar(): string {
36
+ return this.text[this.position];
37
+ }
38
+
39
+ public get nextChar(): string {
40
+ return this.text[this.position + 1];
41
+ }
42
+
43
+ public get previousChar(): string {
44
+ return this.text[this.position - 1];
45
+ }
46
+
47
+ public get isAtEnd(): boolean {
48
+ return this.position >= this.text.length;
49
+ }
50
+
51
+ public getString(startPosition: number): string {
52
+ return this.text.substring(startPosition, this.position);
53
+ }
54
+
55
+ public getRemainingString(): string {
56
+ return this.text.substring(this.position);
57
+ }
58
+
59
+ public checkForSingleComment(): boolean {
60
+ if (this.singleLineCommentStart()) {
61
+ while (!this.isAtEnd && !this.newLine()) {
62
+ this._position++;
63
+ }
64
+ return true;
65
+ }
66
+ return false;
67
+ }
68
+
69
+ public checkForMultiLineComment(): boolean {
70
+ if (this.multiLineCommentStart()) {
71
+ while (!this.isAtEnd) {
72
+ if (this.multiLineCommentEnd()) {
73
+ this._position += 2;
74
+ return true;
75
+ }
76
+ this._position++;
77
+ }
78
+ throw new Error(`Unterminated multi-line comment at position ${this.position}`);
79
+ }
80
+ return false;
81
+ }
82
+
83
+ public singleLineCommentStart(): boolean {
84
+ return this.currentChar === '/' && this.nextChar === '/';
85
+ }
86
+
87
+ public multiLineCommentStart(): boolean {
88
+ return this.currentChar === '/' && this.nextChar === '*';
89
+ }
90
+
91
+ public multiLineCommentEnd(): boolean {
92
+ return this.currentChar === '*' && this.nextChar === '/';
93
+ }
94
+
95
+ public newLine(): boolean {
96
+ if (this.currentChar === '\n') {
97
+ return true;
98
+ }
99
+ return false;
100
+ }
101
+
102
+ public escaped(char: string): boolean {
103
+ return this.currentChar === '\\' && this.nextChar === char;
104
+ }
105
+
106
+ public escapedBrace(): boolean {
107
+ return (this.currentChar === '{' && this.nextChar === '{') || (this.currentChar === '}' && this.nextChar === '}');
108
+ }
109
+
110
+ public openingBrace(): boolean {
111
+ return this.currentChar === '{';
112
+ }
113
+
114
+ public closingBrace(): boolean {
115
+ return this.currentChar === '}';
116
+ }
117
+
118
+ public checkForUnderScore(): boolean {
119
+ const foundUnderScore = this.currentChar === '_';
120
+ if (foundUnderScore) {
121
+ this._position++;
122
+ }
123
+ return foundUnderScore;
124
+ }
125
+
126
+ public checkForLetter(): boolean {
127
+ const foundLetter = StringUtils.letters.includes(this.currentChar.toLowerCase());
128
+ if (foundLetter) {
129
+ this._position++;
130
+ }
131
+ return foundLetter;
132
+ }
133
+
134
+ public checkForDigit(): boolean {
135
+ const foundDigit = StringUtils.digits.includes(this.currentChar);
136
+ if (foundDigit) {
137
+ this._position++;
138
+ }
139
+ return foundDigit;
140
+ }
141
+
142
+ public checkForQuote(): string | null {
143
+ const quoteChar = this.currentChar;
144
+ if (quoteChar === '"' || quoteChar === "'" || quoteChar === '`') {
145
+ this._position++;
146
+ return quoteChar;
147
+ }
148
+ return null;
149
+ }
150
+
151
+ public checkForString(value: string): boolean {
152
+ const _string = this.text.substring(this.position, this.position + value.length);
153
+
154
+ const foundString = _string.toLowerCase() === value.toLowerCase();
155
+ if (foundString) {
156
+ this._position += value.length;
157
+ }
158
+ return foundString;
159
+ }
160
+
161
+ public checkForWhitespace(): boolean {
162
+ return StringUtils.whitespace.includes(this.currentChar);
163
+ }
164
+
165
+ public checkForFStringStart(): boolean {
166
+ return this.currentChar.toLowerCase() === 'f' && ['\'', '"', '`'].includes(this.nextChar);
167
+ }
168
+
169
+ public moveNext(): void {
170
+ this._position++;
171
+ }
172
+
173
+ public moveBy(steps: number): void {
174
+ this._position += steps;
175
+ }
176
+
177
+ public movePrevious(): void {
178
+ this._position--;
179
+ }
180
+
181
+ public is_word(word: string | null): boolean {
182
+ if (word === null) {
183
+ return false;
184
+ }
185
+ return this.text.substring(this.position, this.position + word.length) === word;
186
+ }
187
+
188
+ public word_continuation(word: string): boolean {
189
+ const next = this.text[this.position + word.length];
190
+ return StringUtils.word_valid_chars.includes(next);
191
+ }
192
+ }
193
+
194
+ export default StringWalker;
@@ -0,0 +1,15 @@
1
+ enum Symbol {
2
+ LEFT_PARENTHESIS = '(',
3
+ RIGHT_PARENTHESIS = ')',
4
+ COMMA = ',',
5
+ DOT = '.',
6
+ COLON = ':',
7
+ WHITESPACE = '',
8
+ OPENING_BRACE = '{',
9
+ CLOSING_BRACE = '}',
10
+ OPENING_BRACKET = '[',
11
+ CLOSING_BRACKET = ']',
12
+ BACKTICK = '`',
13
+ }
14
+
15
+ export default Symbol;