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