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