clarity-pattern-parser 11.4.1 → 11.4.2

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.
@@ -13,10 +13,8 @@ export class Literal implements Pattern {
13
13
  private _name: string;
14
14
  private _parent: Pattern | null;
15
15
  private _token: string;
16
- private _runes: string[];
17
16
  private _firstIndex: number;
18
17
  private _lastIndex: number;
19
- private _endIndex: number;
20
18
 
21
19
  get id(): string {
22
20
  return this._id;
@@ -59,11 +57,9 @@ export class Literal implements Pattern {
59
57
  this._type = "literal";
60
58
  this._name = name;
61
59
  this._token = value;
62
- this._runes = Array.from(value);
63
60
  this._parent = null;
64
61
  this._firstIndex = 0;
65
62
  this._lastIndex = 0;
66
- this._endIndex = 0;
67
63
  }
68
64
 
69
65
  test(text: string, record = false): boolean {
@@ -76,6 +72,7 @@ export class Literal implements Pattern {
76
72
 
77
73
  parse(cursor: Cursor): Node | null {
78
74
  this._firstIndex = cursor.index;
75
+ this._lastIndex = cursor.index;
79
76
  const passed = this._tryToParse(cursor);
80
77
 
81
78
  if (passed) {
@@ -86,38 +83,33 @@ export class Literal implements Pattern {
86
83
  return node;
87
84
  }
88
85
 
89
- cursor.recordErrorAt(this._firstIndex, this._endIndex, this);
86
+ cursor.recordErrorAt(this._firstIndex, this._lastIndex, this);
90
87
  return null;
91
88
  }
92
89
 
93
90
  private _tryToParse(cursor: Cursor): boolean {
94
- let passed = false;
95
- const literalRuneLength = this._runes.length;
96
-
97
- for (let i = 0; i < literalRuneLength; i++) {
98
- const literalRune = this._runes[i];
99
- const cursorRune = cursor.currentChar;
100
-
101
- if (literalRune !== cursorRune) {
102
- this._endIndex = cursor.index;
103
- break;
104
- }
105
-
106
- if (i + 1 === literalRuneLength) {
107
- this._lastIndex = this._firstIndex + this._token.length - 1;
108
- passed = true;
109
- break;
110
- }
111
-
112
- if (!cursor.hasNext()) {
113
- this._endIndex = cursor.index + 1;
114
- break;
91
+ const token = this._token;
92
+ const compareToToken = cursor.text.slice(this._firstIndex, this._firstIndex + this._token.length);
93
+ const length = Math.min(token.length, compareToToken.length);
94
+
95
+ for (let i = 0 ; i < length; i++){
96
+ if (token[i] !== compareToToken[i]) {
97
+ this._lastIndex = this._firstIndex + i;
98
+ cursor.moveTo(this._lastIndex);
99
+ return false;
115
100
  }
101
+ }
116
102
 
117
- cursor.next();
103
+ if (token != compareToToken){
104
+ this._lastIndex = this._firstIndex + compareToToken.length - 1;
105
+ cursor.moveTo(this._lastIndex);
106
+ return false;
118
107
  }
119
108
 
120
- return passed;
109
+ this._lastIndex = this._firstIndex + this._token.length - 1;
110
+ cursor.moveTo(this._lastIndex);
111
+ return true;
112
+
121
113
  }
122
114
 
123
115
  private _createNode(): Node {
@@ -58,7 +58,7 @@ export class Regex implements Pattern {
58
58
  this._name = name;
59
59
  this._parent = null;
60
60
  this._originalRegexString = regex;
61
- this._regex = new RegExp(`^${regex}`, "g");
61
+ this._regex = new RegExp(`^${regex}`, "gu");
62
62
  this.assertArguments();
63
63
  }
64
64
 
@@ -101,7 +101,7 @@ export class Regex implements Pattern {
101
101
  private resetState(cursor: Cursor) {
102
102
  this._cursor = cursor;
103
103
  this._regex.lastIndex = 0;
104
- this._substring = this._cursor.text.substr(this._cursor.index);
104
+ this._substring = this._cursor.text.slice(this._cursor.index);
105
105
  this._node = null;
106
106
  }
107
107
 
@@ -118,19 +118,19 @@ export class Regex implements Pattern {
118
118
  private processResult(cursor: Cursor, result: RegExpExecArray) {
119
119
  const currentIndex = cursor.index;
120
120
  const match = result[0];
121
- const matchLength = [...match].length;
122
- const newIndex = currentIndex + matchLength - 1;
121
+ const lastIndex = cursor.getCharLastIndex(currentIndex + match.length - 1);
122
+
123
123
 
124
124
  this._node = new Node(
125
125
  "regex",
126
126
  this._name,
127
127
  currentIndex,
128
- newIndex,
128
+ lastIndex,
129
129
  undefined,
130
130
  result[0]
131
131
  );
132
132
 
133
- cursor.moveTo(newIndex);
133
+ cursor.moveTo(lastIndex);
134
134
  cursor.recordMatch(this, this._node);
135
135
  }
136
136
 
@@ -92,7 +92,7 @@ export class TakeUntil implements Pattern {
92
92
 
93
93
  if (foundMatch) {
94
94
  cursor.moveTo(cursorIndex - 1);
95
- const value = cursor.getChars(this.startedOnIndex, cursorIndex - 1);
95
+ const value = cursor.substring(this.startedOnIndex, cursorIndex - 1);
96
96
  const node = Node.createValueNode(this._type, this._name, value);
97
97
 
98
98
  cursor.recordMatch(this, node);
@@ -18,7 +18,7 @@ export function generateErrorMessage(pattern: Pattern, cursor: Cursor) {
18
18
 
19
19
  const lastPattern = furthestMatch.pattern as Pattern;
20
20
  const suggestions = cleanSuggestions(lastPattern.getNextTokens());
21
- const strUpToError = cursor.getChars(0, endIndex);
21
+ const strUpToError = cursor.substring(0, endIndex);
22
22
  const lines = strUpToError.split("\n");
23
23
  const lastLine = lines[lines.length - 1];
24
24
  const line = lines.length;
package/tsconfig.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "moduleResolution": "node",
4
4
  "target": "es6",
5
5
  "module": "esnext",
6
- "lib": ["es2021"],
6
+ "lib": ["esnext"],
7
7
  "strict": true,
8
8
  "sourceMap": true,
9
9
  "declaration": true,