Browse Source

Fixed symbol, #ab'cd' is now error, not #abcd.

Herbert Vojčík 12 years ago
parent
commit
6adcd5067a
2 changed files with 81 additions and 91 deletions
  1. 78 87
      js/parser.js
  2. 3 4
      js/parser.pegjs

+ 78 - 87
js/parser.js

@@ -43,6 +43,7 @@ smalltalk.parser = (function(){
         "identifier": parse_identifier,
         "varIdentifier": parse_varIdentifier,
         "keyword": parse_keyword,
+        "selector": parse_selector,
         "className": parse_className,
         "string": parse_string,
         "symbol": parse_symbol,
@@ -579,6 +580,75 @@ smalltalk.parser = (function(){
         return result0;
       }
       
+      function parse_selector() {
+        var cacheKey = "selector@" + pos.offset;
+        var cachedResult = cache[cacheKey];
+        if (cachedResult) {
+          pos = clone(cachedResult.nextPos);
+          return cachedResult.result;
+        }
+        
+        var result0, result1, result2;
+        var pos0, pos1;
+        
+        pos0 = clone(pos);
+        pos1 = clone(pos);
+        if (/^[a-zA-Z]/.test(input.charAt(pos.offset))) {
+          result0 = input.charAt(pos.offset);
+          advance(pos, 1);
+        } else {
+          result0 = null;
+          if (reportFailures === 0) {
+            matchFailed("[a-zA-Z]");
+          }
+        }
+        if (result0 !== null) {
+          result1 = [];
+          if (/^[a-zA-Z0-9:]/.test(input.charAt(pos.offset))) {
+            result2 = input.charAt(pos.offset);
+            advance(pos, 1);
+          } else {
+            result2 = null;
+            if (reportFailures === 0) {
+              matchFailed("[a-zA-Z0-9:]");
+            }
+          }
+          while (result2 !== null) {
+            result1.push(result2);
+            if (/^[a-zA-Z0-9:]/.test(input.charAt(pos.offset))) {
+              result2 = input.charAt(pos.offset);
+              advance(pos, 1);
+            } else {
+              result2 = null;
+              if (reportFailures === 0) {
+                matchFailed("[a-zA-Z0-9:]");
+              }
+            }
+          }
+          if (result1 !== null) {
+            result0 = [result0, result1];
+          } else {
+            result0 = null;
+            pos = clone(pos1);
+          }
+        } else {
+          result0 = null;
+          pos = clone(pos1);
+        }
+        if (result0 !== null) {
+          result0 = (function(offset, line, column, first, others) {return first + others.join("")})(pos0.offset, pos0.line, pos0.column, result0[0], result0[1]);
+        }
+        if (result0 === null) {
+          pos = clone(pos0);
+        }
+        
+        cache[cacheKey] = {
+          nextPos: clone(pos),
+          result:  result0
+        };
+        return result0;
+      }
+      
       function parse_className() {
         var cacheKey = "className@" + pos.offset;
         var cachedResult = cache[cacheKey];
@@ -778,7 +848,7 @@ smalltalk.parser = (function(){
           return cachedResult.result;
         }
         
-        var result0, result1, result2, result3;
+        var result0, result1;
         var pos0, pos1, pos2;
         
         pos0 = clone(pos);
@@ -793,95 +863,16 @@ smalltalk.parser = (function(){
           }
         }
         if (result0 !== null) {
-          result1 = [];
-          pos2 = clone(pos);
-          if (/^[a-zA-Z0-9:]/.test(input.charAt(pos.offset))) {
-            result3 = input.charAt(pos.offset);
-            advance(pos, 1);
-          } else {
-            result3 = null;
-            if (reportFailures === 0) {
-              matchFailed("[a-zA-Z0-9:]");
-            }
-          }
-          if (result3 !== null) {
-            result2 = [];
-            while (result3 !== null) {
-              result2.push(result3);
-              if (/^[a-zA-Z0-9:]/.test(input.charAt(pos.offset))) {
-                result3 = input.charAt(pos.offset);
-                advance(pos, 1);
-              } else {
-                result3 = null;
-                if (reportFailures === 0) {
-                  matchFailed("[a-zA-Z0-9:]");
-                }
-              }
-            }
-          } else {
-            result2 = null;
-          }
-          if (result2 !== null) {
-            result2 = (function(offset, line, column, digits) {return digits.join("")})(pos2.offset, pos2.line, pos2.column, result2);
-          }
-          if (result2 === null) {
-            pos = clone(pos2);
-          }
-          if (result2 === null) {
-            pos2 = clone(pos);
-            result2 = parse_string();
-            if (result2 !== null) {
-              result2 = (function(offset, line, column, node) {return node._value()})(pos2.offset, pos2.line, pos2.column, result2);
-            }
-            if (result2 === null) {
-              pos = clone(pos2);
-            }
-          }
-          while (result2 !== null) {
-            result1.push(result2);
+          result1 = parse_selector();
+          if (result1 === null) {
             pos2 = clone(pos);
-            if (/^[a-zA-Z0-9:]/.test(input.charAt(pos.offset))) {
-              result3 = input.charAt(pos.offset);
-              advance(pos, 1);
-            } else {
-              result3 = null;
-              if (reportFailures === 0) {
-                matchFailed("[a-zA-Z0-9:]");
-              }
-            }
-            if (result3 !== null) {
-              result2 = [];
-              while (result3 !== null) {
-                result2.push(result3);
-                if (/^[a-zA-Z0-9:]/.test(input.charAt(pos.offset))) {
-                  result3 = input.charAt(pos.offset);
-                  advance(pos, 1);
-                } else {
-                  result3 = null;
-                  if (reportFailures === 0) {
-                    matchFailed("[a-zA-Z0-9:]");
-                  }
-                }
-              }
-            } else {
-              result2 = null;
-            }
-            if (result2 !== null) {
-              result2 = (function(offset, line, column, digits) {return digits.join("")})(pos2.offset, pos2.line, pos2.column, result2);
+            result1 = parse_string();
+            if (result1 !== null) {
+              result1 = (function(offset, line, column, node) {return node._value()})(pos2.offset, pos2.line, pos2.column, result1);
             }
-            if (result2 === null) {
+            if (result1 === null) {
               pos = clone(pos2);
             }
-            if (result2 === null) {
-              pos2 = clone(pos);
-              result2 = parse_string();
-              if (result2 !== null) {
-                result2 = (function(offset, line, column, node) {return node._value()})(pos2.offset, pos2.line, pos2.column, result2);
-              }
-              if (result2 === null) {
-                pos = clone(pos2);
-              }
-            }
           }
           if (result1 !== null) {
             result0 = [result0, result1];
@@ -896,7 +887,7 @@ smalltalk.parser = (function(){
         if (result0 !== null) {
           result0 = (function(offset, line, column, val) {
                               return smalltalk.ValueNode._new()
-                                     ._value_(val.join("").replace(/\"/ig, '"'))
+                                     ._value_(val)
                           })(pos0.offset, pos0.line, pos0.column, result0[1]);
         }
         if (result0 === null) {

+ 3 - 4
js/parser.pegjs

@@ -6,18 +6,17 @@ ws             = (separator / comments)*
 identifier     = first:[a-zA-Z] others:[a-zA-Z0-9]* {return first + others.join("")}
 varIdentifier  = first:[a-z] others:[a-zA-Z0-9]* {return first + others.join("")}
 keyword        = first:identifier last:[:] {return first + last}
+selector      = first:[a-zA-Z] others:[a-zA-Z0-9\:]* {return first + others.join("")}
 className      = first:[A-Z] others:[a-zA-Z0-9]* {return first + others.join("")}
 string         = ['] val:(("''" {return "'"} / [^'])*) ['] {
                      return smalltalk.ValueNode._new()
                             ._value_(val.join("").replace(/\"/ig, '"'))
                  }
 
-symbol         = "#"val:(
-                         digits:[a-zA-Z0-9\:]+ {return digits.join("")}
-                       / node:string {return node._value()})*
+symbol         = "#"val:(selector / node:string {return node._value()})
                   {
                       return smalltalk.ValueNode._new()
-                             ._value_(val.join("").replace(/\"/ig, '"'))
+                             ._value_(val)
                   }
 number         = n:(hex / float / integer) {
                      return smalltalk.ValueNode._new()