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