clarity-pattern-parser 10.3.2 → 10.3.4
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/dist/index.browser.js +97 -37
- package/dist/index.browser.js.map +1 -1
- package/dist/index.esm.js +97 -37
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +97 -37
- package/dist/index.js.map +1 -1
- package/dist/patterns/ExpressionPattern.d.ts +6 -1
- package/package.json +1 -1
- package/src/patterns/ExpressionPattern.ts +107 -38
package/dist/index.esm.js
CHANGED
|
@@ -2797,7 +2797,7 @@ class ExpressionPattern {
|
|
|
2797
2797
|
return this._patterns;
|
|
2798
2798
|
}
|
|
2799
2799
|
get unaryPatterns() {
|
|
2800
|
-
return this.
|
|
2800
|
+
return this._atomPatterns;
|
|
2801
2801
|
}
|
|
2802
2802
|
get binaryPatterns() {
|
|
2803
2803
|
return this._binaryPatterns;
|
|
@@ -2818,7 +2818,9 @@ class ExpressionPattern {
|
|
|
2818
2818
|
this._name = name;
|
|
2819
2819
|
this._parent = null;
|
|
2820
2820
|
this._firstIndex = -1;
|
|
2821
|
-
this.
|
|
2821
|
+
this._atomPatterns = [];
|
|
2822
|
+
this._unaryPrefixPatterns = [];
|
|
2823
|
+
this._unaryPrefixNames = [];
|
|
2822
2824
|
this._binaryPatterns = [];
|
|
2823
2825
|
this._recursivePatterns = [];
|
|
2824
2826
|
this._recursiveNames = [];
|
|
@@ -2829,7 +2831,7 @@ class ExpressionPattern {
|
|
|
2829
2831
|
this._originalPatterns = patterns;
|
|
2830
2832
|
this._shouldCompactPatternsMap = {};
|
|
2831
2833
|
this._patterns = this._organizePatterns(patterns);
|
|
2832
|
-
if (this.
|
|
2834
|
+
if (this._atomPatterns.length === 0) {
|
|
2833
2835
|
throw new Error("Need at least one operand pattern with an 'expression' pattern.");
|
|
2834
2836
|
}
|
|
2835
2837
|
}
|
|
@@ -2837,7 +2839,14 @@ class ExpressionPattern {
|
|
|
2837
2839
|
const finalPatterns = [];
|
|
2838
2840
|
patterns.forEach((pattern) => {
|
|
2839
2841
|
this._shouldCompactPatternsMap[pattern.name] = pattern.shouldCompactAst;
|
|
2840
|
-
if (this.
|
|
2842
|
+
if (this._isUnary(pattern)) {
|
|
2843
|
+
const unaryPrefix = this._extractUnaryPrefixPattern(pattern).clone();
|
|
2844
|
+
this._unaryPrefixPatterns.push(pattern);
|
|
2845
|
+
this._unaryPrefixNames.push(pattern.name);
|
|
2846
|
+
unaryPrefix.parent = this;
|
|
2847
|
+
finalPatterns.push(unaryPrefix);
|
|
2848
|
+
}
|
|
2849
|
+
else if (this._isBinary(pattern)) {
|
|
2841
2850
|
const binaryName = this._extractName(pattern);
|
|
2842
2851
|
const clone = this._extractDelimiter(pattern).clone();
|
|
2843
2852
|
clone.parent = this;
|
|
@@ -2864,7 +2873,7 @@ class ExpressionPattern {
|
|
|
2864
2873
|
else {
|
|
2865
2874
|
const clone = pattern.clone();
|
|
2866
2875
|
clone.parent = this;
|
|
2867
|
-
this.
|
|
2876
|
+
this._atomPatterns.push(clone);
|
|
2868
2877
|
finalPatterns.push(clone);
|
|
2869
2878
|
}
|
|
2870
2879
|
});
|
|
@@ -2896,6 +2905,26 @@ class ExpressionPattern {
|
|
|
2896
2905
|
}
|
|
2897
2906
|
return pattern.name;
|
|
2898
2907
|
}
|
|
2908
|
+
_isUnary(pattern) {
|
|
2909
|
+
if (pattern.type === "right-associated" && this._isUnaryPattern(pattern.children[0])) {
|
|
2910
|
+
return true;
|
|
2911
|
+
}
|
|
2912
|
+
return this._isUnaryPattern(pattern);
|
|
2913
|
+
}
|
|
2914
|
+
_isUnaryPattern(pattern) {
|
|
2915
|
+
return pattern.type === "sequence" &&
|
|
2916
|
+
pattern.children[0].type !== "reference" &&
|
|
2917
|
+
pattern.children[0].name !== this.name &&
|
|
2918
|
+
pattern.children[1].type === "reference" &&
|
|
2919
|
+
pattern.children[1].name === this.name &&
|
|
2920
|
+
pattern.children.length === 2;
|
|
2921
|
+
}
|
|
2922
|
+
_extractUnaryPrefixPattern(pattern) {
|
|
2923
|
+
if (pattern.type === "right-associated") {
|
|
2924
|
+
return pattern.children[0].children[0];
|
|
2925
|
+
}
|
|
2926
|
+
return pattern.children[0];
|
|
2927
|
+
}
|
|
2899
2928
|
_isRecursive(pattern) {
|
|
2900
2929
|
if (pattern.type === "right-associated" && this._isRecursivePattern(pattern.children[0])) {
|
|
2901
2930
|
return true;
|
|
@@ -2906,7 +2935,7 @@ class ExpressionPattern {
|
|
|
2906
2935
|
return pattern.type === "sequence" &&
|
|
2907
2936
|
pattern.children[0].type === "reference" &&
|
|
2908
2937
|
pattern.children[0].name === this.name &&
|
|
2909
|
-
pattern.children.length >
|
|
2938
|
+
pattern.children.length > 2;
|
|
2910
2939
|
}
|
|
2911
2940
|
_extractRecursiveTail(pattern) {
|
|
2912
2941
|
if (pattern.type === "right-associated") {
|
|
@@ -2960,34 +2989,59 @@ class ExpressionPattern {
|
|
|
2960
2989
|
cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
|
|
2961
2990
|
return null;
|
|
2962
2991
|
}
|
|
2963
|
-
let
|
|
2992
|
+
let lastAtomNode = null;
|
|
2964
2993
|
let lastBinaryNode = null;
|
|
2965
2994
|
let onIndex = cursor.index;
|
|
2966
2995
|
outer: while (true) {
|
|
2967
2996
|
cursor.resolveError();
|
|
2968
2997
|
onIndex = cursor.index;
|
|
2969
|
-
|
|
2998
|
+
let prefix = null;
|
|
2999
|
+
let prefixName = "";
|
|
3000
|
+
for (let i = 0; i < this._unaryPrefixPatterns.length; i++) {
|
|
2970
3001
|
cursor.moveTo(onIndex);
|
|
2971
|
-
const pattern = this.
|
|
3002
|
+
const pattern = this._unaryPrefixPatterns[i];
|
|
2972
3003
|
const node = pattern.parse(cursor);
|
|
2973
3004
|
if (node != null) {
|
|
2974
|
-
|
|
3005
|
+
prefix = node;
|
|
3006
|
+
prefixName = this._unaryPrefixNames[i];
|
|
3007
|
+
if (cursor.hasNext()) {
|
|
3008
|
+
cursor.next();
|
|
3009
|
+
}
|
|
3010
|
+
else {
|
|
3011
|
+
break outer;
|
|
3012
|
+
}
|
|
2975
3013
|
break;
|
|
2976
3014
|
}
|
|
2977
3015
|
else {
|
|
2978
|
-
lastUnaryNode = null;
|
|
2979
3016
|
cursor.resolveError();
|
|
2980
3017
|
}
|
|
2981
3018
|
}
|
|
2982
|
-
|
|
3019
|
+
onIndex = cursor.index;
|
|
3020
|
+
for (let i = 0; i < this._atomPatterns.length; i++) {
|
|
3021
|
+
cursor.moveTo(onIndex);
|
|
3022
|
+
const pattern = this._atomPatterns[i];
|
|
3023
|
+
const node = pattern.parse(cursor);
|
|
3024
|
+
if (node != null) {
|
|
3025
|
+
lastAtomNode = node;
|
|
3026
|
+
break;
|
|
3027
|
+
}
|
|
3028
|
+
else {
|
|
3029
|
+
lastAtomNode = null;
|
|
3030
|
+
cursor.resolveError();
|
|
3031
|
+
}
|
|
3032
|
+
}
|
|
3033
|
+
if (lastAtomNode == null) {
|
|
2983
3034
|
break;
|
|
2984
3035
|
}
|
|
2985
3036
|
if (cursor.hasNext()) {
|
|
2986
3037
|
cursor.next();
|
|
2987
3038
|
}
|
|
2988
3039
|
else {
|
|
2989
|
-
if (lastBinaryNode != null &&
|
|
2990
|
-
|
|
3040
|
+
if (lastBinaryNode != null && lastAtomNode != null) {
|
|
3041
|
+
if (prefix != null) {
|
|
3042
|
+
lastAtomNode = createNode(prefixName, [prefix, lastAtomNode]);
|
|
3043
|
+
}
|
|
3044
|
+
lastBinaryNode.appendChild(lastAtomNode);
|
|
2991
3045
|
}
|
|
2992
3046
|
break;
|
|
2993
3047
|
}
|
|
@@ -2998,24 +3052,30 @@ class ExpressionPattern {
|
|
|
2998
3052
|
if (node != null) {
|
|
2999
3053
|
const name = this._recursiveNames[i];
|
|
3000
3054
|
if (this._endsInRecursion[i]) {
|
|
3001
|
-
if (lastBinaryNode != null &&
|
|
3002
|
-
|
|
3055
|
+
if (lastBinaryNode != null && lastAtomNode != null) {
|
|
3056
|
+
if (prefix != null) {
|
|
3057
|
+
lastAtomNode = createNode(prefixName, [prefix, lastAtomNode]);
|
|
3058
|
+
}
|
|
3059
|
+
lastBinaryNode.appendChild(lastAtomNode);
|
|
3003
3060
|
}
|
|
3004
|
-
const frontExpression = lastBinaryNode == null ?
|
|
3061
|
+
const frontExpression = lastBinaryNode == null ? lastAtomNode : lastBinaryNode.findRoot();
|
|
3005
3062
|
const recursiveNode = createNode(name, [frontExpression, ...node.children]);
|
|
3006
3063
|
recursiveNode.normalize(this._firstIndex);
|
|
3007
3064
|
return recursiveNode;
|
|
3008
3065
|
}
|
|
3009
3066
|
else {
|
|
3010
|
-
|
|
3011
|
-
|
|
3012
|
-
|
|
3067
|
+
if (prefix != null) {
|
|
3068
|
+
lastAtomNode = createNode(prefixName, [prefix, lastAtomNode]);
|
|
3069
|
+
}
|
|
3070
|
+
const recursiveNode = createNode(name, [lastAtomNode, ...node.children]);
|
|
3071
|
+
recursiveNode.normalize(lastAtomNode.startIndex);
|
|
3072
|
+
lastAtomNode = recursiveNode;
|
|
3013
3073
|
if (cursor.hasNext()) {
|
|
3014
3074
|
cursor.next();
|
|
3015
3075
|
}
|
|
3016
3076
|
else {
|
|
3017
|
-
if (lastBinaryNode != null) {
|
|
3018
|
-
lastBinaryNode.appendChild(
|
|
3077
|
+
if (lastBinaryNode != null && lastAtomNode != null) {
|
|
3078
|
+
lastBinaryNode.appendChild(lastAtomNode);
|
|
3019
3079
|
}
|
|
3020
3080
|
break outer;
|
|
3021
3081
|
}
|
|
@@ -3037,31 +3097,31 @@ class ExpressionPattern {
|
|
|
3037
3097
|
if (delimiterNode == null) {
|
|
3038
3098
|
if (i === this._binaryPatterns.length - 1) {
|
|
3039
3099
|
if (lastBinaryNode == null) {
|
|
3040
|
-
return
|
|
3100
|
+
return lastAtomNode;
|
|
3041
3101
|
}
|
|
3042
|
-
else if (
|
|
3043
|
-
lastBinaryNode.appendChild(
|
|
3102
|
+
else if (lastAtomNode != null) {
|
|
3103
|
+
lastBinaryNode.appendChild(lastAtomNode);
|
|
3044
3104
|
}
|
|
3045
3105
|
}
|
|
3046
3106
|
continue;
|
|
3047
3107
|
}
|
|
3048
|
-
if (lastBinaryNode == null &&
|
|
3049
|
-
const node = createNode(name, [
|
|
3108
|
+
if (lastBinaryNode == null && lastAtomNode != null && delimiterNode != null) {
|
|
3109
|
+
const node = createNode(name, [lastAtomNode, delimiterNode]);
|
|
3050
3110
|
lastBinaryNode = node;
|
|
3051
3111
|
}
|
|
3052
|
-
else if (lastBinaryNode != null &&
|
|
3112
|
+
else if (lastBinaryNode != null && lastAtomNode != null && delimiterNode != null) {
|
|
3053
3113
|
const precedence = this._precedenceMap[name];
|
|
3054
3114
|
const lastPrecendece = lastBinaryNode == null ? 0 : this._precedenceMap[lastBinaryNode.name] == null ? -1 : this._precedenceMap[lastBinaryNode.name];
|
|
3055
3115
|
const association = this._binaryAssociation[i];
|
|
3056
3116
|
if (precedence === lastPrecendece && association === Association.right) {
|
|
3057
|
-
const node = createNode(name, [
|
|
3117
|
+
const node = createNode(name, [lastAtomNode, delimiterNode]);
|
|
3058
3118
|
lastBinaryNode.appendChild(node);
|
|
3059
3119
|
lastBinaryNode = node;
|
|
3060
3120
|
}
|
|
3061
3121
|
else if (precedence === lastPrecendece) {
|
|
3062
3122
|
const node = createNode(name, []);
|
|
3063
3123
|
lastBinaryNode.replaceWith(node);
|
|
3064
|
-
lastBinaryNode.appendChild(
|
|
3124
|
+
lastBinaryNode.appendChild(lastAtomNode);
|
|
3065
3125
|
node.append(lastBinaryNode, delimiterNode);
|
|
3066
3126
|
lastBinaryNode = node;
|
|
3067
3127
|
}
|
|
@@ -3076,7 +3136,7 @@ class ExpressionPattern {
|
|
|
3076
3136
|
root = ancestor;
|
|
3077
3137
|
ancestor = ancestor.parent;
|
|
3078
3138
|
}
|
|
3079
|
-
lastBinaryNode.appendChild(
|
|
3139
|
+
lastBinaryNode.appendChild(lastAtomNode);
|
|
3080
3140
|
if (root != null) {
|
|
3081
3141
|
const node = createNode(name, []);
|
|
3082
3142
|
root.replaceWith(node);
|
|
@@ -3084,12 +3144,12 @@ class ExpressionPattern {
|
|
|
3084
3144
|
lastBinaryNode = node;
|
|
3085
3145
|
}
|
|
3086
3146
|
else {
|
|
3087
|
-
const node = createNode(name, [
|
|
3147
|
+
const node = createNode(name, [lastAtomNode, delimiterNode]);
|
|
3088
3148
|
lastBinaryNode = node;
|
|
3089
3149
|
}
|
|
3090
3150
|
}
|
|
3091
3151
|
else {
|
|
3092
|
-
const node = createNode(name, [
|
|
3152
|
+
const node = createNode(name, [lastAtomNode, delimiterNode]);
|
|
3093
3153
|
lastBinaryNode.appendChild(node);
|
|
3094
3154
|
lastBinaryNode = node;
|
|
3095
3155
|
}
|
|
@@ -3107,14 +3167,14 @@ class ExpressionPattern {
|
|
|
3107
3167
|
}
|
|
3108
3168
|
}
|
|
3109
3169
|
if (lastBinaryNode == null) {
|
|
3110
|
-
return
|
|
3170
|
+
return lastAtomNode;
|
|
3111
3171
|
}
|
|
3112
3172
|
else {
|
|
3113
3173
|
const root = lastBinaryNode.findAncestor(n => n.parent == null) || lastBinaryNode;
|
|
3114
3174
|
if (lastBinaryNode.children.length < 3) {
|
|
3115
3175
|
lastBinaryNode.remove();
|
|
3116
3176
|
if (lastBinaryNode === root) {
|
|
3117
|
-
return
|
|
3177
|
+
return lastAtomNode;
|
|
3118
3178
|
}
|
|
3119
3179
|
}
|
|
3120
3180
|
root.normalize(this._firstIndex);
|
|
@@ -3162,7 +3222,7 @@ class ExpressionPattern {
|
|
|
3162
3222
|
return this._binaryPatterns.map(p => p.getTokens()).flat();
|
|
3163
3223
|
}
|
|
3164
3224
|
if (this.binaryPatterns.indexOf(childReference)) {
|
|
3165
|
-
const unaryTokens = this.
|
|
3225
|
+
const unaryTokens = this._atomPatterns.map(p => p.getTokens()).flat();
|
|
3166
3226
|
if (this._parent != null) {
|
|
3167
3227
|
const nextTokens = this._parent.getTokensAfter(this);
|
|
3168
3228
|
return [...unaryTokens, ...nextTokens];
|
|
@@ -3190,7 +3250,7 @@ class ExpressionPattern {
|
|
|
3190
3250
|
return this._binaryPatterns.map(p => p.getPatterns()).flat();
|
|
3191
3251
|
}
|
|
3192
3252
|
if (this.binaryPatterns.indexOf(childReference)) {
|
|
3193
|
-
const unaryPatterns = this.
|
|
3253
|
+
const unaryPatterns = this._atomPatterns.map(p => p.getPatterns()).flat();
|
|
3194
3254
|
if (this._parent != null) {
|
|
3195
3255
|
const nextPatterns = this._parent.getPatternsAfter(this);
|
|
3196
3256
|
return [...unaryPatterns, ...nextPatterns];
|