clarity-pattern-parser 8.0.0 → 8.1.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clarity-pattern-parser",
3
- "version": "8.0.0",
3
+ "version": "8.1.0",
4
4
  "description": "Parsing Library for Typescript and Javascript.",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.esm.js",
@@ -1,5 +1,6 @@
1
1
  import { Cursor } from "../patterns/Cursor";
2
2
  import { Match } from "../patterns/CursorHistory";
3
+ import { ParseError } from "../patterns/ParseError";
3
4
  import { Pattern } from "../patterns/Pattern";
4
5
  import { Suggestion } from "./Suggestion";
5
6
  import { SuggestionOption } from "./SuggestionOption";
@@ -32,25 +33,28 @@ export class AutoComplete {
32
33
  this._text = "";
33
34
  }
34
35
 
35
- suggestFor(text: string): Suggestion {
36
- if (text.length === 0) {
36
+ suggestForWithCursor(cursor: Cursor): Suggestion {
37
+ cursor.moveTo(0);
38
+
39
+ this._cursor = cursor;
40
+ this._text = cursor.text;
41
+ this._cursor.startRecording();
42
+
43
+ if (cursor.length === 0) {
37
44
  return {
38
45
  isComplete: false,
39
46
  options: this._createSuggestionsFromRoot(),
47
+ error: new ParseError(0, 0, this._pattern),
40
48
  errorAtIndex: 0,
41
49
  cursor: null,
42
50
  ast: null
43
51
  }
44
52
  }
45
53
 
46
- this._text = text;
47
- this._cursor = new Cursor(text);
48
- this._cursor.startRecording();
49
-
50
54
  let errorAtIndex = null;
51
55
 
52
56
  const ast = this._pattern.parse(this._cursor);
53
- const isComplete = ast?.value === text;
57
+ const isComplete = ast?.value === this._text;
54
58
  const options = this._getAllOptions();
55
59
 
56
60
  if (this._cursor.hasError && this._cursor.furthestError != null) {
@@ -64,10 +68,16 @@ export class AutoComplete {
64
68
  return {
65
69
  isComplete: isComplete,
66
70
  options: options,
71
+ error: this._cursor.furthestError,
67
72
  errorAtIndex,
68
73
  cursor: this._cursor,
69
74
  ast,
70
75
  }
76
+
77
+ }
78
+
79
+ suggestFor(text: string): Suggestion {
80
+ return this.suggestForWithCursor(new Cursor(text));
71
81
  }
72
82
 
73
83
  private _getAllOptions() {
@@ -192,6 +202,14 @@ export class AutoComplete {
192
202
  startIndex: furthestMatch,
193
203
  }
194
204
  }
205
+
206
+ static suggestFor(text: string, pattern: Pattern, options?: AutoCompleteOptions) {
207
+ return new AutoComplete(pattern, options).suggestFor(text);
208
+ }
209
+
210
+ static suggestForWithCursor(cursor: Cursor, pattern: Pattern, options?: AutoCompleteOptions) {
211
+ return new AutoComplete(pattern, options).suggestForWithCursor(cursor);
212
+ }
195
213
  }
196
214
 
197
215
  function findMatchIndex(str1: string, str2: string): number {
@@ -1,10 +1,12 @@
1
1
  import { Node } from "../ast/Node";
2
+ import { ParseError } from "../index.js";
2
3
  import { Cursor } from "../patterns/Cursor";
3
4
  import { SuggestionOption } from "./SuggestionOption";
4
5
 
5
6
  export interface Suggestion {
6
7
  isComplete: boolean;
7
8
  options: SuggestionOption[];
9
+ error: ParseError | null;
8
10
  errorAtIndex: number | null;
9
11
  cursor: Cursor | null;
10
12
  ast: Node | null;
@@ -4,9 +4,16 @@ import { Node } from "../ast/Node"
4
4
 
5
5
  describe("Cursor", () => {
6
6
  test("Empty Text", () => {
7
- expect(() => {
8
- new Cursor("");
9
- }).toThrowError()
7
+ const cursor = new Cursor("");
8
+
9
+ cursor.next();
10
+ expect(cursor.index).toBe(0);
11
+
12
+ cursor.previous();
13
+ expect(cursor.index).toBe(0);
14
+
15
+ const text = cursor.getChars(0, 1);
16
+ expect(text).toBe("");
10
17
  });
11
18
 
12
19
  test("Move Cursor", () => {
@@ -66,10 +66,6 @@ export class Cursor {
66
66
  }
67
67
 
68
68
  constructor(text: string) {
69
- if (text.length === 0) {
70
- throw new Error("Cannot have a empty string.");
71
- }
72
-
73
69
  this._text = text;
74
70
  this._index = 0;
75
71
  this._length = text.length;