Explorar o código

escape reserved JavaScript keywords

Vladislav Folts %!s(int64=11) %!d(string=hai) anos
pai
achega
3902a52e2c
Modificáronse 3 ficheiros con 31 adicións e 0 borrados
  1. 8 0
      src/lexer.js
  2. 10 0
      test/expected/js_keyword.js
  3. 13 0
      test/input/js_keyword.ob

+ 8 - 0
src/lexer.js

@@ -67,6 +67,11 @@ var reservedWords
        "NIL", "VAR", "DO", "OF", "WHILE", "ELSE", "OR", "ELSIF", "POINTER",
        "NIL", "VAR", "DO", "OF", "WHILE", "ELSE", "OR", "ELSIF", "POINTER",
        "END", "PROCEDURE", "FALSE", "RECORD", "FOR", "REPEAT", "IF", "RETURN"
        "END", "PROCEDURE", "FALSE", "RECORD", "FOR", "REPEAT", "IF", "RETURN"
       ];
       ];
+var jsReservedWords 
+    = ["break", "case", "catch", "continue", "debugger", "default", "delete",
+       "do", "else", "finally", "for", "function", "if", "in", "instanceof",
+       "new", "return", "switch", "this", "throw", "try", "typeof", "var",
+       "void", "while", "with"];
 
 
 exports.ident = function(stream, context){
 exports.ident = function(stream, context){
     if (!isLetter(stream.peekChar()))
     if (!isLetter(stream.peekChar()))
@@ -85,6 +90,9 @@ exports.ident = function(stream, context){
         stream.setPos(savePos);
         stream.setPos(savePos);
         return false;
         return false;
     }
     }
+    if (jsReservedWords.indexOf(result) != -1)
+        result += "$";
+
     context.setIdent(result);
     context.setIdent(result);
     return true;
     return true;
 };
 };

+ 10 - 0
test/expected/js_keyword.js

@@ -0,0 +1,10 @@
+var do$ = function (){
+var case$ = 0;
+
+function function$(){
+	var i = null;
+	i = function$;
+	case$ = 123;
+}
+function$();
+}();

+ 13 - 0
test/input/js_keyword.ob

@@ -0,0 +1,13 @@
+MODULE do;
+TYPE throw = PROCEDURE;
+VAR case: INTEGER;
+PROCEDURE function();
+VAR i: throw;
+BEGIN
+	i := function;
+	case := 123;
+END function;
+
+BEGIN
+	function();
+END do.