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.js CHANGED
@@ -2801,7 +2801,7 @@ class ExpressionPattern {
2801
2801
  return this._patterns;
2802
2802
  }
2803
2803
  get unaryPatterns() {
2804
- return this._unaryPatterns;
2804
+ return this._atomPatterns;
2805
2805
  }
2806
2806
  get binaryPatterns() {
2807
2807
  return this._binaryPatterns;
@@ -2822,7 +2822,9 @@ class ExpressionPattern {
2822
2822
  this._name = name;
2823
2823
  this._parent = null;
2824
2824
  this._firstIndex = -1;
2825
- this._unaryPatterns = [];
2825
+ this._atomPatterns = [];
2826
+ this._unaryPrefixPatterns = [];
2827
+ this._unaryPrefixNames = [];
2826
2828
  this._binaryPatterns = [];
2827
2829
  this._recursivePatterns = [];
2828
2830
  this._recursiveNames = [];
@@ -2833,7 +2835,7 @@ class ExpressionPattern {
2833
2835
  this._originalPatterns = patterns;
2834
2836
  this._shouldCompactPatternsMap = {};
2835
2837
  this._patterns = this._organizePatterns(patterns);
2836
- if (this._unaryPatterns.length === 0) {
2838
+ if (this._atomPatterns.length === 0) {
2837
2839
  throw new Error("Need at least one operand pattern with an 'expression' pattern.");
2838
2840
  }
2839
2841
  }
@@ -2841,7 +2843,14 @@ class ExpressionPattern {
2841
2843
  const finalPatterns = [];
2842
2844
  patterns.forEach((pattern) => {
2843
2845
  this._shouldCompactPatternsMap[pattern.name] = pattern.shouldCompactAst;
2844
- if (this._isBinary(pattern)) {
2846
+ if (this._isUnary(pattern)) {
2847
+ const unaryPrefix = this._extractUnaryPrefixPattern(pattern).clone();
2848
+ this._unaryPrefixPatterns.push(pattern);
2849
+ this._unaryPrefixNames.push(pattern.name);
2850
+ unaryPrefix.parent = this;
2851
+ finalPatterns.push(unaryPrefix);
2852
+ }
2853
+ else if (this._isBinary(pattern)) {
2845
2854
  const binaryName = this._extractName(pattern);
2846
2855
  const clone = this._extractDelimiter(pattern).clone();
2847
2856
  clone.parent = this;
@@ -2868,7 +2877,7 @@ class ExpressionPattern {
2868
2877
  else {
2869
2878
  const clone = pattern.clone();
2870
2879
  clone.parent = this;
2871
- this._unaryPatterns.push(clone);
2880
+ this._atomPatterns.push(clone);
2872
2881
  finalPatterns.push(clone);
2873
2882
  }
2874
2883
  });
@@ -2900,6 +2909,26 @@ class ExpressionPattern {
2900
2909
  }
2901
2910
  return pattern.name;
2902
2911
  }
2912
+ _isUnary(pattern) {
2913
+ if (pattern.type === "right-associated" && this._isUnaryPattern(pattern.children[0])) {
2914
+ return true;
2915
+ }
2916
+ return this._isUnaryPattern(pattern);
2917
+ }
2918
+ _isUnaryPattern(pattern) {
2919
+ return pattern.type === "sequence" &&
2920
+ pattern.children[0].type !== "reference" &&
2921
+ pattern.children[0].name !== this.name &&
2922
+ pattern.children[1].type === "reference" &&
2923
+ pattern.children[1].name === this.name &&
2924
+ pattern.children.length === 2;
2925
+ }
2926
+ _extractUnaryPrefixPattern(pattern) {
2927
+ if (pattern.type === "right-associated") {
2928
+ return pattern.children[0].children[0];
2929
+ }
2930
+ return pattern.children[0];
2931
+ }
2903
2932
  _isRecursive(pattern) {
2904
2933
  if (pattern.type === "right-associated" && this._isRecursivePattern(pattern.children[0])) {
2905
2934
  return true;
@@ -2910,7 +2939,7 @@ class ExpressionPattern {
2910
2939
  return pattern.type === "sequence" &&
2911
2940
  pattern.children[0].type === "reference" &&
2912
2941
  pattern.children[0].name === this.name &&
2913
- pattern.children.length > 1;
2942
+ pattern.children.length > 2;
2914
2943
  }
2915
2944
  _extractRecursiveTail(pattern) {
2916
2945
  if (pattern.type === "right-associated") {
@@ -2964,34 +2993,59 @@ class ExpressionPattern {
2964
2993
  cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
2965
2994
  return null;
2966
2995
  }
2967
- let lastUnaryNode = null;
2996
+ let lastAtomNode = null;
2968
2997
  let lastBinaryNode = null;
2969
2998
  let onIndex = cursor.index;
2970
2999
  outer: while (true) {
2971
3000
  cursor.resolveError();
2972
3001
  onIndex = cursor.index;
2973
- for (let i = 0; i < this._unaryPatterns.length; i++) {
3002
+ let prefix = null;
3003
+ let prefixName = "";
3004
+ for (let i = 0; i < this._unaryPrefixPatterns.length; i++) {
2974
3005
  cursor.moveTo(onIndex);
2975
- const pattern = this._unaryPatterns[i];
3006
+ const pattern = this._unaryPrefixPatterns[i];
2976
3007
  const node = pattern.parse(cursor);
2977
3008
  if (node != null) {
2978
- lastUnaryNode = node;
3009
+ prefix = node;
3010
+ prefixName = this._unaryPrefixNames[i];
3011
+ if (cursor.hasNext()) {
3012
+ cursor.next();
3013
+ }
3014
+ else {
3015
+ break outer;
3016
+ }
2979
3017
  break;
2980
3018
  }
2981
3019
  else {
2982
- lastUnaryNode = null;
2983
3020
  cursor.resolveError();
2984
3021
  }
2985
3022
  }
2986
- if (lastUnaryNode == null) {
3023
+ onIndex = cursor.index;
3024
+ for (let i = 0; i < this._atomPatterns.length; i++) {
3025
+ cursor.moveTo(onIndex);
3026
+ const pattern = this._atomPatterns[i];
3027
+ const node = pattern.parse(cursor);
3028
+ if (node != null) {
3029
+ lastAtomNode = node;
3030
+ break;
3031
+ }
3032
+ else {
3033
+ lastAtomNode = null;
3034
+ cursor.resolveError();
3035
+ }
3036
+ }
3037
+ if (lastAtomNode == null) {
2987
3038
  break;
2988
3039
  }
2989
3040
  if (cursor.hasNext()) {
2990
3041
  cursor.next();
2991
3042
  }
2992
3043
  else {
2993
- if (lastBinaryNode != null && lastUnaryNode != null) {
2994
- lastBinaryNode.appendChild(lastUnaryNode);
3044
+ if (lastBinaryNode != null && lastAtomNode != null) {
3045
+ if (prefix != null) {
3046
+ lastAtomNode = createNode(prefixName, [prefix, lastAtomNode]);
3047
+ }
3048
+ lastBinaryNode.appendChild(lastAtomNode);
2995
3049
  }
2996
3050
  break;
2997
3051
  }
@@ -3002,24 +3056,30 @@ class ExpressionPattern {
3002
3056
  if (node != null) {
3003
3057
  const name = this._recursiveNames[i];
3004
3058
  if (this._endsInRecursion[i]) {
3005
- if (lastBinaryNode != null && lastUnaryNode != null) {
3006
- lastBinaryNode.appendChild(lastUnaryNode);
3059
+ if (lastBinaryNode != null && lastAtomNode != null) {
3060
+ if (prefix != null) {
3061
+ lastAtomNode = createNode(prefixName, [prefix, lastAtomNode]);
3062
+ }
3063
+ lastBinaryNode.appendChild(lastAtomNode);
3007
3064
  }
3008
- const frontExpression = lastBinaryNode == null ? lastUnaryNode : lastBinaryNode.findRoot();
3065
+ const frontExpression = lastBinaryNode == null ? lastAtomNode : lastBinaryNode.findRoot();
3009
3066
  const recursiveNode = createNode(name, [frontExpression, ...node.children]);
3010
3067
  recursiveNode.normalize(this._firstIndex);
3011
3068
  return recursiveNode;
3012
3069
  }
3013
3070
  else {
3014
- const recursiveNode = createNode(name, [lastUnaryNode, ...node.children]);
3015
- recursiveNode.normalize(lastUnaryNode.startIndex);
3016
- lastUnaryNode = recursiveNode;
3071
+ if (prefix != null) {
3072
+ lastAtomNode = createNode(prefixName, [prefix, lastAtomNode]);
3073
+ }
3074
+ const recursiveNode = createNode(name, [lastAtomNode, ...node.children]);
3075
+ recursiveNode.normalize(lastAtomNode.startIndex);
3076
+ lastAtomNode = recursiveNode;
3017
3077
  if (cursor.hasNext()) {
3018
3078
  cursor.next();
3019
3079
  }
3020
3080
  else {
3021
- if (lastBinaryNode != null) {
3022
- lastBinaryNode.appendChild(lastUnaryNode);
3081
+ if (lastBinaryNode != null && lastAtomNode != null) {
3082
+ lastBinaryNode.appendChild(lastAtomNode);
3023
3083
  }
3024
3084
  break outer;
3025
3085
  }
@@ -3041,31 +3101,31 @@ class ExpressionPattern {
3041
3101
  if (delimiterNode == null) {
3042
3102
  if (i === this._binaryPatterns.length - 1) {
3043
3103
  if (lastBinaryNode == null) {
3044
- return lastUnaryNode;
3104
+ return lastAtomNode;
3045
3105
  }
3046
- else if (lastUnaryNode != null) {
3047
- lastBinaryNode.appendChild(lastUnaryNode);
3106
+ else if (lastAtomNode != null) {
3107
+ lastBinaryNode.appendChild(lastAtomNode);
3048
3108
  }
3049
3109
  }
3050
3110
  continue;
3051
3111
  }
3052
- if (lastBinaryNode == null && lastUnaryNode != null && delimiterNode != null) {
3053
- const node = createNode(name, [lastUnaryNode, delimiterNode]);
3112
+ if (lastBinaryNode == null && lastAtomNode != null && delimiterNode != null) {
3113
+ const node = createNode(name, [lastAtomNode, delimiterNode]);
3054
3114
  lastBinaryNode = node;
3055
3115
  }
3056
- else if (lastBinaryNode != null && lastUnaryNode != null && delimiterNode != null) {
3116
+ else if (lastBinaryNode != null && lastAtomNode != null && delimiterNode != null) {
3057
3117
  const precedence = this._precedenceMap[name];
3058
3118
  const lastPrecendece = lastBinaryNode == null ? 0 : this._precedenceMap[lastBinaryNode.name] == null ? -1 : this._precedenceMap[lastBinaryNode.name];
3059
3119
  const association = this._binaryAssociation[i];
3060
3120
  if (precedence === lastPrecendece && association === Association.right) {
3061
- const node = createNode(name, [lastUnaryNode, delimiterNode]);
3121
+ const node = createNode(name, [lastAtomNode, delimiterNode]);
3062
3122
  lastBinaryNode.appendChild(node);
3063
3123
  lastBinaryNode = node;
3064
3124
  }
3065
3125
  else if (precedence === lastPrecendece) {
3066
3126
  const node = createNode(name, []);
3067
3127
  lastBinaryNode.replaceWith(node);
3068
- lastBinaryNode.appendChild(lastUnaryNode);
3128
+ lastBinaryNode.appendChild(lastAtomNode);
3069
3129
  node.append(lastBinaryNode, delimiterNode);
3070
3130
  lastBinaryNode = node;
3071
3131
  }
@@ -3080,7 +3140,7 @@ class ExpressionPattern {
3080
3140
  root = ancestor;
3081
3141
  ancestor = ancestor.parent;
3082
3142
  }
3083
- lastBinaryNode.appendChild(lastUnaryNode);
3143
+ lastBinaryNode.appendChild(lastAtomNode);
3084
3144
  if (root != null) {
3085
3145
  const node = createNode(name, []);
3086
3146
  root.replaceWith(node);
@@ -3088,12 +3148,12 @@ class ExpressionPattern {
3088
3148
  lastBinaryNode = node;
3089
3149
  }
3090
3150
  else {
3091
- const node = createNode(name, [lastUnaryNode, delimiterNode]);
3151
+ const node = createNode(name, [lastAtomNode, delimiterNode]);
3092
3152
  lastBinaryNode = node;
3093
3153
  }
3094
3154
  }
3095
3155
  else {
3096
- const node = createNode(name, [lastUnaryNode, delimiterNode]);
3156
+ const node = createNode(name, [lastAtomNode, delimiterNode]);
3097
3157
  lastBinaryNode.appendChild(node);
3098
3158
  lastBinaryNode = node;
3099
3159
  }
@@ -3111,14 +3171,14 @@ class ExpressionPattern {
3111
3171
  }
3112
3172
  }
3113
3173
  if (lastBinaryNode == null) {
3114
- return lastUnaryNode;
3174
+ return lastAtomNode;
3115
3175
  }
3116
3176
  else {
3117
3177
  const root = lastBinaryNode.findAncestor(n => n.parent == null) || lastBinaryNode;
3118
3178
  if (lastBinaryNode.children.length < 3) {
3119
3179
  lastBinaryNode.remove();
3120
3180
  if (lastBinaryNode === root) {
3121
- return lastUnaryNode;
3181
+ return lastAtomNode;
3122
3182
  }
3123
3183
  }
3124
3184
  root.normalize(this._firstIndex);
@@ -3166,7 +3226,7 @@ class ExpressionPattern {
3166
3226
  return this._binaryPatterns.map(p => p.getTokens()).flat();
3167
3227
  }
3168
3228
  if (this.binaryPatterns.indexOf(childReference)) {
3169
- const unaryTokens = this._unaryPatterns.map(p => p.getTokens()).flat();
3229
+ const unaryTokens = this._atomPatterns.map(p => p.getTokens()).flat();
3170
3230
  if (this._parent != null) {
3171
3231
  const nextTokens = this._parent.getTokensAfter(this);
3172
3232
  return [...unaryTokens, ...nextTokens];
@@ -3194,7 +3254,7 @@ class ExpressionPattern {
3194
3254
  return this._binaryPatterns.map(p => p.getPatterns()).flat();
3195
3255
  }
3196
3256
  if (this.binaryPatterns.indexOf(childReference)) {
3197
- const unaryPatterns = this._unaryPatterns.map(p => p.getPatterns()).flat();
3257
+ const unaryPatterns = this._atomPatterns.map(p => p.getPatterns()).flat();
3198
3258
  if (this._parent != null) {
3199
3259
  const nextPatterns = this._parent.getPatternsAfter(this);
3200
3260
  return [...unaryPatterns, ...nextPatterns];