clarity-pattern-parser 3.0.15 → 4.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 (168) hide show
  1. package/README.md +0 -191
  2. package/dist/ast/Node.d.ts +4 -5
  3. package/dist/index.browser.js +476 -731
  4. package/dist/index.browser.js.map +1 -1
  5. package/dist/index.d.ts +10 -18
  6. package/dist/index.esm.js +469 -716
  7. package/dist/index.esm.js.map +1 -1
  8. package/dist/index.js +476 -731
  9. package/dist/index.js.map +1 -1
  10. package/dist/patterns/And.d.ts +24 -0
  11. package/dist/patterns/Literal.d.ts +19 -0
  12. package/dist/patterns/LookAhead.d.ts +8 -0
  13. package/dist/patterns/Not.d.ts +11 -0
  14. package/dist/patterns/Or.d.ts +22 -0
  15. package/dist/patterns/Pattern.d.ts +6 -7
  16. package/dist/patterns/{RecursivePattern.d.ts → Recursive.d.ts} +4 -4
  17. package/dist/patterns/Reference.d.ts +14 -0
  18. package/dist/patterns/Regex.d.ts +21 -0
  19. package/dist/patterns/Repeat.d.ts +20 -0
  20. package/package.json +1 -1
  21. package/src/CursorHistory.ts +1 -1
  22. package/src/ast/Node.ts +20 -17
  23. package/src/ast/Visitor.ts +14 -18
  24. package/src/index.ts +17 -33
  25. package/src/patterns/And.ts +178 -0
  26. package/src/patterns/Literal.ts +91 -0
  27. package/src/patterns/LookAhead.ts +32 -0
  28. package/src/patterns/Not.ts +50 -0
  29. package/src/patterns/Or.ts +132 -0
  30. package/src/patterns/Pattern.ts +31 -47
  31. package/src/patterns/Recursive.ts +92 -0
  32. package/src/patterns/Reference.ts +127 -0
  33. package/src/patterns/Regex.ts +123 -0
  34. package/src/patterns/Repeat.ts +155 -0
  35. package/src/tests/{AndValue.test.ts → And.test.ts} +31 -41
  36. package/src/tests/CursorHistory.test.ts +6 -6
  37. package/src/tests/Cusor.test.ts +7 -10
  38. package/src/tests/Literal.test.ts +3 -5
  39. package/src/tests/LookAhead.test.ts +44 -0
  40. package/src/tests/Not.test.ts +51 -0
  41. package/src/tests/Or.test.ts +113 -0
  42. package/src/tests/Pattern.test.ts +41 -141
  43. package/src/tests/{RecursivePattern.test.ts → Recursive.test.ts} +10 -8
  44. package/src/tests/Reference.test.ts +16 -0
  45. package/src/tests/{RepeatValue.test.ts → Repeat.test.ts} +10 -42
  46. package/src/tests/TextSuggester.test.ts +20 -28
  47. package/src/tests/{NodeVisitor.test.ts → Visitor.test.ts} +42 -21
  48. package/src/tests/cssPatterns/cssValue.ts +2 -2
  49. package/src/tests/cssPatterns/divider.ts +2 -2
  50. package/src/tests/cssPatterns/hex.ts +2 -2
  51. package/src/tests/cssPatterns/method.ts +7 -9
  52. package/src/tests/cssPatterns/name.ts +2 -2
  53. package/src/tests/cssPatterns/number.ts +2 -2
  54. package/src/tests/cssPatterns/optionalSpaces.ts +1 -2
  55. package/src/tests/cssPatterns/spaces.ts +2 -2
  56. package/src/tests/cssPatterns/unit.ts +3 -3
  57. package/src/tests/cssPatterns/value.ts +2 -2
  58. package/src/tests/cssPatterns/values.ts +2 -2
  59. package/src/tests/htmlPatterns/element.ts +18 -33
  60. package/src/tests/javascriptPatterns/boolean.ts +2 -3
  61. package/src/tests/javascriptPatterns/json.ts +14 -24
  62. package/src/tests/javascriptPatterns/name.ts +3 -20
  63. package/src/tests/javascriptPatterns/number.ts +2 -2
  64. package/src/tests/javascriptPatterns/objectLiteral.ts +9 -16
  65. package/src/tests/javascriptPatterns/string.ts +26 -24
  66. package/src/tests/javascriptPatterns/unit.ts +3 -6
  67. package/src/tests/javascriptPatterns/whitespace.ts +8 -12
  68. package/src/tests/naturalLanguage/filter.ts +16 -33
  69. package/src/tests/patterns/sentence.ts +8 -8
  70. package/dist/Cursor.js +0 -105
  71. package/dist/Cursor.js.map +0 -1
  72. package/dist/CursorHistory.js +0 -104
  73. package/dist/CursorHistory.js.map +0 -1
  74. package/dist/Permutor.d.ts +0 -13
  75. package/dist/Permutor.js +0 -52
  76. package/dist/Permutor.js.map +0 -1
  77. package/dist/TextSuggester.js +0 -244
  78. package/dist/TextSuggester.js.map +0 -1
  79. package/dist/ast/CompositeNode.d.ts +0 -6
  80. package/dist/ast/CompositeNode.js +0 -17
  81. package/dist/ast/CompositeNode.js.map +0 -1
  82. package/dist/ast/Node.js +0 -16
  83. package/dist/ast/Node.js.map +0 -1
  84. package/dist/ast/NodeVisitor.d.ts +0 -31
  85. package/dist/ast/ValueNode.d.ts +0 -6
  86. package/dist/ast/ValueNode.js +0 -14
  87. package/dist/ast/ValueNode.js.map +0 -1
  88. package/dist/ast/Visitor.js +0 -209
  89. package/dist/ast/Visitor.js.map +0 -1
  90. package/dist/patterns/ParseError.js +0 -9
  91. package/dist/patterns/ParseError.js.map +0 -1
  92. package/dist/patterns/Pattern.js +0 -127
  93. package/dist/patterns/Pattern.js.map +0 -1
  94. package/dist/patterns/RecursivePattern.js +0 -65
  95. package/dist/patterns/RecursivePattern.js.map +0 -1
  96. package/dist/patterns/composite/AndComposite.d.ts +0 -22
  97. package/dist/patterns/composite/AndComposite.js +0 -117
  98. package/dist/patterns/composite/AndComposite.js.map +0 -1
  99. package/dist/patterns/composite/CompositePattern.d.ts +0 -4
  100. package/dist/patterns/composite/CompositePattern.js +0 -7
  101. package/dist/patterns/composite/CompositePattern.js.map +0 -1
  102. package/dist/patterns/composite/OptionalComposite.d.ts +0 -10
  103. package/dist/patterns/composite/OptionalComposite.js +0 -29
  104. package/dist/patterns/composite/OptionalComposite.js.map +0 -1
  105. package/dist/patterns/composite/OrComposite.d.ts +0 -16
  106. package/dist/patterns/composite/OrComposite.js +0 -69
  107. package/dist/patterns/composite/OrComposite.js.map +0 -1
  108. package/dist/patterns/composite/RepeatComposite.d.ts +0 -21
  109. package/dist/patterns/composite/RepeatComposite.js +0 -88
  110. package/dist/patterns/composite/RepeatComposite.js.map +0 -1
  111. package/dist/patterns/value/AndValue.d.ts +0 -21
  112. package/dist/patterns/value/AndValue.js +0 -118
  113. package/dist/patterns/value/AndValue.js.map +0 -1
  114. package/dist/patterns/value/AnyOfThese.d.ts +0 -18
  115. package/dist/patterns/value/AnyOfThese.js +0 -59
  116. package/dist/patterns/value/AnyOfThese.js.map +0 -1
  117. package/dist/patterns/value/Literal.d.ts +0 -20
  118. package/dist/patterns/value/Literal.js +0 -63
  119. package/dist/patterns/value/Literal.js.map +0 -1
  120. package/dist/patterns/value/NotValue.d.ts +0 -17
  121. package/dist/patterns/value/NotValue.js +0 -70
  122. package/dist/patterns/value/NotValue.js.map +0 -1
  123. package/dist/patterns/value/OptionalValue.d.ts +0 -9
  124. package/dist/patterns/value/OptionalValue.js +0 -32
  125. package/dist/patterns/value/OptionalValue.js.map +0 -1
  126. package/dist/patterns/value/OrValue.d.ts +0 -19
  127. package/dist/patterns/value/OrValue.js +0 -73
  128. package/dist/patterns/value/OrValue.js.map +0 -1
  129. package/dist/patterns/value/RegexValue.d.ts +0 -19
  130. package/dist/patterns/value/RegexValue.js +0 -69
  131. package/dist/patterns/value/RegexValue.js.map +0 -1
  132. package/dist/patterns/value/RepeatValue.d.ts +0 -19
  133. package/dist/patterns/value/RepeatValue.js +0 -89
  134. package/dist/patterns/value/RepeatValue.js.map +0 -1
  135. package/dist/patterns/value/ValuePattern.d.ts +0 -5
  136. package/dist/patterns/value/ValuePattern.js +0 -7
  137. package/dist/patterns/value/ValuePattern.js.map +0 -1
  138. package/src/Permutor.ts +0 -64
  139. package/src/ast/CompositeNode.ts +0 -26
  140. package/src/ast/ValueNode.ts +0 -28
  141. package/src/patterns/RecursivePattern.ts +0 -86
  142. package/src/patterns/composite/AndComposite.ts +0 -159
  143. package/src/patterns/composite/CompositePattern.ts +0 -7
  144. package/src/patterns/composite/OptionalComposite.ts +0 -37
  145. package/src/patterns/composite/OrComposite.ts +0 -96
  146. package/src/patterns/composite/RepeatComposite.ts +0 -130
  147. package/src/patterns/value/AndValue.ts +0 -153
  148. package/src/patterns/value/AnyOfThese.ts +0 -81
  149. package/src/patterns/value/Literal.ts +0 -92
  150. package/src/patterns/value/NotValue.ts +0 -95
  151. package/src/patterns/value/OptionalValue.ts +0 -39
  152. package/src/patterns/value/OrValue.ts +0 -103
  153. package/src/patterns/value/RegexValue.ts +0 -103
  154. package/src/patterns/value/RepeatValue.ts +0 -131
  155. package/src/patterns/value/ValuePattern.ts +0 -8
  156. package/src/tests/AndComposite.test.ts +0 -102
  157. package/src/tests/AnyOfThese.test.ts +0 -74
  158. package/src/tests/CompositeNode.test.ts +0 -33
  159. package/src/tests/NotValue.test.ts +0 -69
  160. package/src/tests/OptionalValue.test.ts +0 -50
  161. package/src/tests/OrComposite.test.ts +0 -75
  162. package/src/tests/OrValue.test.ts +0 -171
  163. package/src/tests/Permutor.test.ts +0 -30
  164. package/src/tests/RegexValue.test.ts +0 -22
  165. package/src/tests/RepeatComposite.test.ts +0 -58
  166. package/src/tests/ValueNode.test.ts +0 -24
  167. package/src/tests/javascriptPatterns/varStatement.ts +0 -0
  168. package/src/tests/readmeDemo.test.ts +0 -124
package/README.md CHANGED
@@ -5,194 +5,3 @@ npm install clarity-pattern-parser
5
5
  ```
6
6
 
7
7
  ## How to use
8
-
9
- There are two constructs within clarity pattern parser. The first is value based patterns. The second is composite based patterns.
10
-
11
- Value patterns can be composed, however, they are reduced to a single value even if they are composed of many parts.
12
-
13
- Composite patterns can also be composed, however, they are left with their parts. The best way to understand this is by an example.
14
-
15
- We will create a pattern for line ending comments, first with value based patterns, and second with some composite based patterns.
16
-
17
- ### Value based comment
18
-
19
- ```javascript
20
- import {
21
- Cursor,
22
- Literal,
23
- AndValue,
24
- OrValue,
25
- RepeatValue,
26
- NotValue,
27
- AndComposite
28
- } from "clarity-pattern-parser";
29
- import assert from "assert";
30
-
31
- exports["readme.md: value"] = () => {
32
- const forwardSlashes = new Literal("forward-slashes", "//");
33
- const newLine = new Literal("new-line", "\n");
34
- const returnCarriage = new Literal("return-carriage", "\r");
35
- const windowsNewLine = new AndValue("windows-new-line", [
36
- returnCarriage,
37
- newLine
38
- ]);
39
- const lineEnd = new OrValue("line-end", [newLine, windowsNewLine]);
40
- const character = new NotValue("character", lineEnd);
41
- const comment = new RepeatValue("comment", character);
42
-
43
- const lineEndingComment = new AndValue("line-ending-comment", [
44
- forwardSlashes,
45
- comment,
46
- lineEnd
47
- ]);
48
-
49
- const string = "// This is a comment\n";
50
- const cursor = new Cursor(string);
51
- const node = lineEndingComment.parse(cursor);
52
-
53
- assert.equal(node.name, "line-ending-comment"); // --> true
54
- assert.equal(node.value, string); // --> true
55
-
56
- const expectedValue = {
57
- type: "and-value",
58
- name: "line-ending-comment",
59
- startIndex: 0,
60
- endIndex: 20,
61
- value: "// This is a comment\n"
62
- };
63
-
64
- assert.equal(JSON.stringify(node), JSON.stringify(expectedValue)); // --> true
65
- };
66
- ```
67
-
68
- ### Composite based comment
69
-
70
- ```javascript
71
- import {
72
- Cursor,
73
- Literal,
74
- AndValue,
75
- OrValue,
76
- RepeatValue,
77
- NotValue,
78
- AndComposite
79
- } from "clarity-pattern-parser";
80
- import assert from "assert";
81
-
82
- exports["readme.md: composite"] = () => {
83
- const forwardSlashes = new Literal("forward-slashes", "//");
84
- const newLine = new Literal("new-line", "\n");
85
- const returnCarriage = new Literal("return-carriage", "\r");
86
- const windowsNewLine = new AndValue("windows-new-line", [
87
- returnCarriage,
88
- newLine
89
- ]);
90
- const lineEnd = new OrValue("line-end", [newLine, windowsNewLine]);
91
- const character = new NotValue("character", lineEnd);
92
- const comment = new RepeatValue("comment", character);
93
-
94
- const lineEndingComment = new AndComposite("line-ending-comment", [
95
- forwardSlashes,
96
- comment,
97
- lineEnd
98
- ]);
99
-
100
- const string = "// This is a comment\n";
101
- const cursor = new Cursor(string);
102
- const node = lineEndingComment.parse(cursor);
103
-
104
- assert.equal(node.name, "line-ending-comment");
105
-
106
- assert.equal(node.children[0].name, "forward-slashes");
107
- assert.equal(node.children[0].value, "//");
108
-
109
- assert.equal(node.children[1].name, "comment");
110
- assert.equal(node.children[1].value, " This is a comment");
111
-
112
- assert.equal(node.children[2].name, "line-end");
113
- assert.equal(node.children[2].value, "\n");
114
-
115
- const expectedValue = {
116
- type: "and-composite",
117
- name: "line-ending-comment",
118
- startIndex: 0,
119
- endIndex: 20,
120
- children: [
121
- {
122
- type: "literal",
123
- name: "forward-slashes",
124
- startIndex: 0,
125
- endIndex: 1,
126
- value: "//"
127
- },
128
- {
129
- type: "repeat-value",
130
- name: "comment",
131
- startIndex: 2,
132
- endIndex: 19,
133
- value: " This is a comment"
134
- },
135
- {
136
- type: "or-value",
137
- name: "line-end",
138
- startIndex: 20,
139
- endIndex: 20,
140
- value: "\n"
141
- }
142
- ]
143
- };
144
-
145
- assert.equal(JSON.stringify(node), JSON.stringify(expectedValue)); // --> true
146
- };
147
- ```
148
-
149
- ### Literal
150
-
151
- ```javascript
152
- import { Literal } from "clarity-pattern-parser";
153
- import assert from "assert";
154
-
155
- // The first parameter is the name of the pattern, the second is the string you are matching.
156
- const john = new Literal("john", "John");
157
-
158
- const result = john.exec("John");
159
- const result2 = john.exec("Jane");
160
-
161
- const expectedValue = {
162
- type: "literal",
163
- name: "john",
164
- startIndex: 0,
165
- endIndex: 3,
166
- value: "John"
167
- };
168
-
169
- assert.equal(JSON.stringify(result), JSON.stringify(expectedValue));
170
- assert.equal(result2, null);
171
- ```
172
-
173
- ### RegexValue
174
-
175
- ```javascript
176
- import { RegexValue } from "clarity-pattern-parser";
177
- import assert from "assert";
178
-
179
- // Notice this isn't a regex literal /[^a]+/
180
- // This is because we need to modify the expression to use it in the middle of a parse.
181
- // This also means we shouldn't use things like the start "^" character or the end "$" character.
182
- // There are several gotchas with RegexValue, however the speed gains are undeniable.
183
- const notA = new RegexValue("not-a", "[^a]+");
184
-
185
- const result = notA.exec("John");
186
- const result2 = notA.exec("a");
187
-
188
- const expectedValue = {
189
- type: "regex-value",
190
- name: "not-a",
191
- startIndex: 0,
192
- endIndex: 3,
193
- value: "John"
194
- };
195
-
196
- assert.equal(JSON.stringify(result), JSON.stringify(expectedValue));
197
- assert.equal(result2, null);
198
- ```
@@ -1,12 +1,11 @@
1
- export default abstract class Node {
1
+ export default class Node {
2
2
  type: string;
3
3
  name: string;
4
4
  startIndex: number;
5
5
  endIndex: number;
6
- isComposite: boolean;
7
6
  children: Node[];
8
7
  value: string;
9
- constructor(type: string, name: string, startIndex: number, endIndex: number, isComposite?: boolean);
10
- abstract clone(): Node;
11
- abstract toString(): string;
8
+ constructor(type: string, name: string, startIndex: number, endIndex: number, children?: Node[], value?: string);
9
+ clone(): Node;
10
+ toString(): string;
12
11
  }