jssm 5.142.4 → 5.143.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.
@@ -1,4 +1,22 @@
1
- import { JssmTransition, JssmCompileSe, JssmParseTree, JssmGenericConfig } from './jssm_types';
1
+ import { JssmTransition, JssmCompileSe, JssmCompileSeStart, JssmParseTree, JssmGenericConfig, FslSourceLocation } from './jssm_types';
2
+ /*********
3
+ *
4
+ * Returns the source span of the `n`-th parse-tree node (1-based) matching
5
+ * `predicate`, or `undefined` if there are fewer than `n` matches or the
6
+ * matched node carries no location. Used to point semantic compile errors
7
+ * at the offending statement when the tree was produced with
8
+ * `parse(input, { locations: true })`.
9
+ *
10
+ * @internal
11
+ *
12
+ * @param tree The parse tree to scan.
13
+ * @param predicate Node test.
14
+ * @param n 1-based ordinal of the matching node to return.
15
+ *
16
+ * @returns The matching node's `loc`, or `undefined`.
17
+ *
18
+ */
19
+ declare function nth_matching_loc<StateType, mDT>(tree: JssmParseTree<StateType, mDT>, predicate: (node: JssmCompileSeStart<StateType, mDT>) => boolean, n: number): FslSourceLocation | undefined;
2
20
  /*********
3
21
  *
4
22
  * Internal method meant to perform factory assembly of an edge. Not meant for
@@ -51,6 +69,34 @@ declare function makeTransition<StateType, mDT>(this_se: JssmCompileSe<StateType
51
69
  * This method is mostly for plugin and intermediate tool authors, or people
52
70
  * who need to work with the machine's intermediate representation.
53
71
  *
72
+ * ## Opt-in source locations
73
+ *
74
+ * Pass `{ locations: true }` to attach source-span information to every
75
+ * object node in the AST. Each node gains a `loc` field of type
76
+ * {@link FslSourceLocation} covering its full statement span. Selected nodes
77
+ * also gain curated sub-span fields that pinpoint individual tokens within the
78
+ * statement:
79
+ *
80
+ * - Transition nodes: `from_loc` (source state), `to_loc` (target state, on
81
+ * the nested `se` object), `l_action_loc` / `r_action_loc` (action labels).
82
+ * - State-declaration nodes: `name_loc` (state name), plus `value_loc` on
83
+ * each color-bearing item inside the declaration block.
84
+ * - Machine-attribute nodes (`machine_name`, `fsl_version`, etc.): `value_loc`
85
+ * (the attribute value token).
86
+ *
87
+ * Without `{ locations: true }` the AST is byte-for-byte identical to the
88
+ * default output; no `loc` or `*_loc` fields are present.
89
+ *
90
+ * ```typescript
91
+ * const tree = wrap_parse('a -> b;', { locations: true });
92
+ * // tree[0].loc === { start: { offset: 0, line: 1, column: 1 },
93
+ * // end: { offset: 7, line: 1, column: 8 } }
94
+ * // tree[0].from_loc.start.offset === 0 // 'a'
95
+ * // tree[0].se.to_loc.start.offset === 5 // 'b'
96
+ * ```
97
+ *
98
+ * @see {@link FslSourceLocation}
99
+ *
54
100
  * # Hey!
55
101
  *
56
102
  * Most people looking at this want either the `sm` operator or method `from`,
@@ -79,7 +125,9 @@ declare function makeTransition<StateType, mDT>(this_se: JssmCompileSe<StateType
79
125
  *
80
126
  * @param input The FSL code to be evaluated
81
127
  *
82
- * @param options Things to control about the instance
128
+ * @param options Things to control about the instance. Pass
129
+ * `{ locations: true }` to enable opt-in source location
130
+ * tracking on every AST node.
83
131
  *
84
132
  */
85
133
  declare function wrap_parse(input: string, options?: Object): any;
@@ -106,6 +154,31 @@ declare function wrap_parse(input: string, options?: Object): any;
106
154
  * This method is mostly for plugin and intermediate tool authors, or people
107
155
  * who need to work with the machine's intermediate representation.
108
156
  *
157
+ * ## Source-location-aware error reporting
158
+ *
159
+ * `compile()` ignores `loc` and `*_loc` fields during machine construction —
160
+ * the resulting config is identical whether or not the tree was parsed with
161
+ * `{ locations: true }`. However, when those fields are present, `compile()`
162
+ * attaches the offending node's source span to any semantic {@link JssmError}
163
+ * it throws, via the error's `source_location` field
164
+ * (type {@link FslSourceLocation}). This lets downstream tooling (e.g. a
165
+ * CodeMirror 6 linter) map the error to a precise editor range without any
166
+ * additional source-scanning.
167
+ *
168
+ * ```typescript
169
+ * import { parse, compile } from 'jssm';
170
+ *
171
+ * try {
172
+ * compile(parse('fsl_version: 1.0.0;\nfsl_version: 2.0.0;\na -> b;',
173
+ * { locations: true }));
174
+ * } catch (err) {
175
+ * // err.source_location.start.offset points at the second fsl_version line
176
+ * console.log(err.source_location);
177
+ * }
178
+ * ```
179
+ *
180
+ * @see {@link FslSourceLocation}
181
+ *
109
182
  * # Hey!
110
183
  *
111
184
  * Most people looking at this want either the `sm` operator or method `from`,
@@ -131,7 +204,10 @@ declare function wrap_parse(input: string, options?: Object): any;
131
204
  *
132
205
  * @typeparam mDT The type of the machine data member; usually omitted
133
206
  *
134
- * @param tree The parse tree to be boiled down into a machine config
207
+ * @param tree The parse tree to be boiled down into a machine config. If the
208
+ * tree was produced with `parse(input, { locations: true })`, any
209
+ * semantic error thrown will carry a `source_location` span
210
+ * pointing at the offending statement.
135
211
  *
136
212
  */
137
213
  declare function compile<StateType, mDT>(tree: JssmParseTree<StateType, mDT>): JssmGenericConfig<StateType, mDT>;
@@ -147,4 +223,4 @@ declare function compile<StateType, mDT>(tree: JssmParseTree<StateType, mDT>): J
147
223
  *
148
224
  */
149
225
  declare function make<StateType, mDT>(plan: string): JssmGenericConfig<StateType, mDT>;
150
- export { compile, make, makeTransition, wrap_parse };
226
+ export { compile, make, makeTransition, wrap_parse, nth_matching_loc };
@@ -1,10 +1,16 @@
1
- import { JssmErrorExtendedInfo } from './jssm_types';
1
+ import { JssmErrorExtendedInfo, FslSourceLocation } from './jssm_types';
2
2
  /*******
3
3
  *
4
4
  * Custom error class for jssm. Enriches the standard `Error` with
5
5
  * machine context (current state, instance name) and an optional
6
6
  * `requested_state` so that error messages are self-describing.
7
7
  *
8
+ * When a semantic error is detected during `compile()` and the parse tree
9
+ * was produced with `parse(input, { locations: true })`, the thrown error
10
+ * also carries a `source_location` field — the FSL source span of the
11
+ * offending statement — so downstream tooling can map the error to a precise
12
+ * position in the original source text without additional scanning.
13
+ *
8
14
  * ```typescript
9
15
  * throw new JssmError(machine, 'no such state', { requested_state: 'Blue' });
10
16
  * // JssmError: [[my-light]]: no such state (at "Red", requested "Blue")
@@ -15,13 +21,17 @@ import { JssmErrorExtendedInfo } from './jssm_types';
15
21
  * read `state()` and `instance_name()` for context.
16
22
  * @param message - A human-readable description of the error.
17
23
  * @param JEEI - Optional {@link JssmErrorExtendedInfo} with extra
18
- * context such as `requested_state`.
24
+ * context such as `requested_state` and/or
25
+ * `source_location` (the FSL source span of the
26
+ * offending statement, present when the error
27
+ * originated from a located parse tree).
19
28
  *
20
29
  */
21
30
  declare class JssmError extends Error {
22
31
  message: string;
23
32
  base_message: string;
24
33
  requested_state: string | undefined;
34
+ source_location: FslSourceLocation | undefined;
25
35
  constructor(machine: any, message: string, JEEI?: JssmErrorExtendedInfo);
26
36
  }
27
37
  export { JssmError };
@@ -247,6 +247,26 @@ declare type JssmGenericMachine<DataType> = {
247
247
  allow_force?: boolean;
248
248
  keep_history?: boolean | number;
249
249
  };
250
+ /**
251
+ * A source span produced by the FSL parser when `parse(input, { locations:
252
+ * true })` is used. Mirrors PEG.js's native `location()` shape: byte
253
+ * `offset`s (0-based, half-open) plus 1-based `line`/`column` for display.
254
+ *
255
+ * ```typescript
256
+ * const [t] = parse('a -> b;', { locations: true });
257
+ * // t.loc === { start: { offset: 0, line: 1, column: 1 },
258
+ * // end: { offset: 7, line: 1, column: 8 } }
259
+ * ```
260
+ */
261
+ declare type FslSourcePoint = {
262
+ offset: number;
263
+ line: number;
264
+ column: number;
265
+ };
266
+ declare type FslSourceLocation = {
267
+ start: FslSourcePoint;
268
+ end: FslSourcePoint;
269
+ };
250
270
  /**
251
271
  * A single key/value pair from an FSL `state X: { ... };` block, in the
252
272
  * raw form produced by the parser before being condensed into a
@@ -256,6 +276,8 @@ declare type JssmStateDeclarationRule = {
256
276
  key: string;
257
277
  value: any;
258
278
  name?: string;
279
+ loc?: FslSourceLocation;
280
+ value_loc?: FslSourceLocation;
259
281
  };
260
282
  /**
261
283
  * The fully-condensed declaration for a single state, including its raw
@@ -472,6 +494,10 @@ declare type JssmCompileSe<StateType, mDT> = {
472
494
  r_probability: number;
473
495
  l_after?: number;
474
496
  r_after?: number;
497
+ loc?: FslSourceLocation;
498
+ to_loc?: FslSourceLocation;
499
+ l_action_loc?: FslSourceLocation;
500
+ r_action_loc?: FslSourceLocation;
475
501
  };
476
502
  /**
477
503
  * Internal compiler intermediate: the root of a chained transition
@@ -486,11 +512,15 @@ declare type JssmCompileSeStart<StateType, DataType> = {
486
512
  from: StateType;
487
513
  se: JssmCompileSe<StateType, DataType>;
488
514
  key: string;
489
- value?: string | number;
515
+ value?: string | number | Array<JssmStateDeclarationRule>;
490
516
  name?: string;
491
517
  state?: string;
492
518
  default_value?: any;
493
519
  required?: boolean;
520
+ loc?: FslSourceLocation;
521
+ from_loc?: FslSourceLocation;
522
+ value_loc?: FslSourceLocation;
523
+ name_loc?: FslSourceLocation;
494
524
  };
495
525
  /**
496
526
  * The output shape of the FSL parser: a flat array of
@@ -712,6 +742,7 @@ declare type PostEverythingHookHandler<mDT> = (hook_context: EverythingHookConte
712
742
  */
713
743
  declare type JssmErrorExtendedInfo = {
714
744
  requested_state?: StateType | undefined;
745
+ source_location?: FslSourceLocation;
715
746
  };
716
747
  /**
717
748
  * Bounded history of recently-visited states paired with the data payload
@@ -933,4 +964,4 @@ declare type JssmEventHandler<mDT, Ev extends JssmEventName> = (detail: JssmEven
933
964
  * removes the subscription. Calling it more than once is a no-op.
934
965
  */
935
966
  declare type JssmUnsubscribe = () => void;
936
- export { JssmColor, JssmShape, JssmTransition, JssmTransitions, JssmTransitionList, JssmTransitionRule, JssmArrow, JssmArrowKind, JssmArrowDirection, JssmGenericConfig, JssmGenericState, JssmGenericMachine, JssmParseTree, JssmCompileSe, JssmCompileSeStart, JssmCompileRule, JssmPermitted, JssmPermittedOpt, JssmResult, JssmStateDeclaration, JssmStateDeclarationRule, JssmStateConfig, JssmStateStyleKey, JssmStateStyleKeyList, JssmBaseTheme, JssmTheme, JssmLayout, JssmHistory, JssmSerialization, JssmPropertyDefinition, JssmAllowsOverride, JssmAllowIslands, JssmDefaultSize, JssmParseFunctionType, JssmMachineInternalState, JssmErrorExtendedInfo, FslDirections, FslDirection, FslThemes, FslTheme, HookDescription, HookHandler, HookContext, HookResult, HookComplexResult, EverythingHookContext, EverythingHookHandler, PostEverythingHookHandler, JssmEventName, JssmEventDetailMap, JssmEventFilterMap, JssmEventFilter, JssmEventHandler, JssmUnsubscribe, JssmTransitionEventDetail, JssmRejectionEventDetail, JssmActionEventDetail, JssmEntryEventDetail, JssmExitEventDetail, JssmTerminalEventDetail, JssmCompleteEventDetail, JssmErrorEventDetail, JssmDataChangeEventDetail, JssmOverrideEventDetail, JssmTimeoutEventDetail, JssmHookLifecycleEventDetail, JssmRng };
967
+ export { JssmColor, JssmShape, JssmTransition, JssmTransitions, JssmTransitionList, JssmTransitionRule, JssmArrow, JssmArrowKind, JssmArrowDirection, JssmGenericConfig, JssmGenericState, JssmGenericMachine, JssmParseTree, JssmCompileSe, JssmCompileSeStart, JssmCompileRule, JssmPermitted, JssmPermittedOpt, JssmResult, JssmStateDeclaration, JssmStateDeclarationRule, JssmStateConfig, JssmStateStyleKey, JssmStateStyleKeyList, JssmBaseTheme, JssmTheme, JssmLayout, JssmHistory, JssmSerialization, JssmPropertyDefinition, JssmAllowsOverride, JssmAllowIslands, JssmDefaultSize, JssmParseFunctionType, JssmMachineInternalState, JssmErrorExtendedInfo, FslDirections, FslDirection, FslThemes, FslTheme, FslSourcePoint, FslSourceLocation, HookDescription, HookHandler, HookContext, HookResult, HookComplexResult, EverythingHookContext, EverythingHookHandler, PostEverythingHookHandler, JssmEventName, JssmEventDetailMap, JssmEventFilterMap, JssmEventFilter, JssmEventHandler, JssmUnsubscribe, JssmTransitionEventDetail, JssmRejectionEventDetail, JssmActionEventDetail, JssmEntryEventDetail, JssmExitEventDetail, JssmTerminalEventDetail, JssmCompleteEventDetail, JssmErrorEventDetail, JssmDataChangeEventDetail, JssmOverrideEventDetail, JssmTimeoutEventDetail, JssmHookLifecycleEventDetail, JssmRng };