瀏覽代碼

escape reserved JavaScript keywords

Vladislav Folts 12 年之前
父節點
當前提交
3902a52e2c
共有 3 個文件被更改,包括 31 次插入0 次删除
  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.