@halleyassist/rule-parser 1.0.14 → 1.0.15

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 (2) hide show
  1. package/index.d.ts +213 -0
  2. package/package.json +7 -3
package/index.d.ts ADDED
@@ -0,0 +1,213 @@
1
+ // TypeScript definitions for @halleyassist/rule-parser
2
+
3
+ /**
4
+ * Position information for errors
5
+ */
6
+ export interface Position {
7
+ line: number;
8
+ column: number;
9
+ offset: number;
10
+ }
11
+
12
+ /**
13
+ * Rule parsing error with detailed error information
14
+ */
15
+ export class RuleParseError extends Error {
16
+ name: 'RuleParseError';
17
+ code: string;
18
+ hint: string;
19
+ line: number;
20
+ column: number;
21
+ offset: number;
22
+ found: string;
23
+ expected: string[];
24
+ snippet: string;
25
+
26
+ constructor(
27
+ code: string,
28
+ message: string,
29
+ hint: string,
30
+ position: Position,
31
+ found: string,
32
+ expected: string[],
33
+ snippet: string
34
+ );
35
+
36
+ toString(): string;
37
+ toJSON(): {
38
+ code: string;
39
+ message: string;
40
+ hint: string;
41
+ line: number;
42
+ column: number;
43
+ offset: number;
44
+ found: string;
45
+ expected: string[];
46
+ snippet: string;
47
+ };
48
+ }
49
+
50
+ /**
51
+ * Parsing error from the EBNF parser
52
+ */
53
+ export class ParsingError extends Error {
54
+ name: 'ParsingError';
55
+ }
56
+
57
+ /**
58
+ * AST Node from the EBNF parser
59
+ */
60
+ export interface ASTNode {
61
+ type: string;
62
+ text: string;
63
+ children?: ASTNode[];
64
+ parent?: ASTNode;
65
+ [key: string]: any;
66
+ }
67
+
68
+ /**
69
+ * Time of day value with hours and minutes
70
+ */
71
+ export interface TimeOfDay {
72
+ hours: number;
73
+ minutes: number;
74
+ tod: number;
75
+ dow?: string;
76
+ }
77
+
78
+ /**
79
+ * Primitive value types that can appear in the IL
80
+ */
81
+ export type PrimitiveValue = string | number | boolean | TimeOfDay;
82
+
83
+ /**
84
+ * Array value type
85
+ */
86
+ export type ArrayValue = PrimitiveValue[];
87
+
88
+ /**
89
+ * Any value that can appear in the IL
90
+ */
91
+ export type ILValue = PrimitiveValue | ArrayValue;
92
+
93
+ /**
94
+ * Value expression in the IL
95
+ */
96
+ export type ValueExpression = ['Value', ILValue];
97
+
98
+ /**
99
+ * Time period constant expression
100
+ */
101
+ export type TimePeriodConst = ['TimePeriodConst', string];
102
+
103
+ /**
104
+ * Time period constant ago expression
105
+ */
106
+ export type TimePeriodConstAgo = ['TimePeriodConstAgo', number, string];
107
+
108
+ /**
109
+ * Time period between expression
110
+ */
111
+ export type TimePeriodBetween =
112
+ | ['TimePeriodBetween', TimeOfDay | number, TimeOfDay | number]
113
+ | ['TimePeriodBetween', TimeOfDay | number, TimeOfDay | number, string]
114
+ | ['TimePeriodBetween', TimeOfDay | number, TimeOfDay | number, string, string];
115
+
116
+ /**
117
+ * Time period between ago expression
118
+ */
119
+ export type TimePeriodBetweenAgo = ['TimePeriodBetweenAgo', number, TimeOfDay, TimeOfDay];
120
+
121
+ /**
122
+ * Any time period expression
123
+ */
124
+ export type TimePeriodExpression =
125
+ | TimePeriodConst
126
+ | TimePeriodConstAgo
127
+ | TimePeriodBetween
128
+ | TimePeriodBetweenAgo;
129
+
130
+ /**
131
+ * Forward declaration for ILExpression to handle recursive types
132
+ */
133
+ export type ILExpression =
134
+ | ValueExpression
135
+ | TimePeriodExpression
136
+ | FunctionCall
137
+ | ComparisonExpression
138
+ | LogicalExpression
139
+ | ArithmeticExpression
140
+ | BetweenExpression
141
+ | NotExpression;
142
+
143
+ /**
144
+ * Function call expression
145
+ * Note: The first element is the function name (not an operator like 'Gt', 'And', etc.)
146
+ * Functions can have zero or more arguments
147
+ */
148
+ export type FunctionCall = [string, ...ILExpression[]];
149
+
150
+ /**
151
+ * Comparison operators
152
+ */
153
+ export type ComparisonOp = 'Gt' | 'Lt' | 'Gte' | 'Lte' | 'Eq' | 'Neq';
154
+
155
+ /**
156
+ * Comparison expression
157
+ */
158
+ export type ComparisonExpression = [ComparisonOp, ILExpression, ILExpression];
159
+
160
+ /**
161
+ * Logical operators
162
+ */
163
+ export type LogicalOp = 'And' | 'Or';
164
+
165
+ /**
166
+ * Logical expression
167
+ * Note: Logical operators require at least two operands
168
+ */
169
+ export type LogicalExpression = [LogicalOp, ILExpression, ILExpression, ...ILExpression[]];
170
+
171
+ /**
172
+ * Arithmetic operators
173
+ */
174
+ export type ArithmeticOp = 'MathAdd' | 'MathSub' | 'MathDiv' | 'MathMul' | 'MathMod' | 'Default';
175
+
176
+ /**
177
+ * Arithmetic expression
178
+ */
179
+ export type ArithmeticExpression = [ArithmeticOp, ILExpression, ILExpression];
180
+
181
+ /**
182
+ * Between expression
183
+ */
184
+ export type BetweenExpression = ['Between', ILExpression, ILExpression, ILExpression];
185
+
186
+ /**
187
+ * Not expression
188
+ */
189
+ export type NotExpression = ['Not', ILExpression];
190
+
191
+ /**
192
+ * Rule parser class
193
+ */
194
+ declare class RuleParser {
195
+ /**
196
+ * Parse a rule string into an Abstract Syntax Tree (AST)
197
+ * @param txt - The rule string to parse
198
+ * @returns The AST node representing the parsed rule
199
+ * @throws {RuleParseError} If the rule string is invalid
200
+ */
201
+ static toAst(txt: string): ASTNode;
202
+
203
+ /**
204
+ * Parse a rule string into an Intermediate Language (IL) representation
205
+ * @param txt - The rule string to parse
206
+ * @returns The IL expression representing the parsed rule
207
+ * @throws {RuleParseError} If the rule string is invalid
208
+ */
209
+ static toIL(txt: string): ILExpression;
210
+ }
211
+
212
+ export default RuleParser;
213
+ export { RuleParser };
package/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "@halleyassist/rule-parser",
3
- "version": "1.0.14",
3
+ "version": "1.0.15",
4
4
  "description": "The grammar for HalleyAssist rules",
5
5
  "main": "src/RuleParser.production.js",
6
+ "types": "index.d.ts",
6
7
  "scripts": {
7
8
  "test": "mocha",
8
9
  "build": "node ./bin/package.js"
@@ -21,8 +22,10 @@
21
22
  "ebnf": "git+https://github.com/HalleyAssist/node-ebnf.git"
22
23
  },
23
24
  "devDependencies": {
25
+ "@types/node": "^25.1.0",
24
26
  "chai": "^4",
25
- "mocha": "^10.8.2"
27
+ "mocha": "^10.8.2",
28
+ "typescript": "^5.9.3"
26
29
  },
27
30
  "publishConfig": {
28
31
  "access": "public",
@@ -30,6 +33,7 @@
30
33
  },
31
34
  "files": [
32
35
  "src/*",
33
- "index.js"
36
+ "index.js",
37
+ "index.d.ts"
34
38
  ]
35
39
  }