clarity-pattern-parser 10.3.2 → 10.3.3

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