Explorar el Código

Use CONST array to simplify search for reserved word.

Vladislav Folts hace 9 años
padre
commit
22e5a2e059
Se han modificado 4 ficheros con 16 adiciones y 32 borrados
  1. BIN
      bin/compiled.zip
  2. 1 1
      src/eberon/eberon_grammar.js
  3. 3 1
      src/grammar.js
  4. 12 30
      src/ob/Lexer.ob

BIN
bin/compiled.zip


+ 1 - 1
src/eberon/eberon_grammar.js

@@ -182,7 +182,7 @@ exports.language = {
             Repeat:             EberonContextLoop.Repeat,
             ModuleDeclaration:  EberonContextProcedure.ModuleDeclaration
         },
-        Grammar.reservedWords + " SELF SUPER MAP"
+        Grammar.reservedWords.concat(["SELF", "SUPER", "MAP"])
         ),
     stdSymbols: Symbols.makeStd(),
     types: {

+ 3 - 1
src/grammar.js

@@ -24,7 +24,9 @@ var context = Parser.context;
 var emit = Parser.emit;
 var required = Parser.required;
 
-var reservedWords = "ARRAY IMPORT THEN BEGIN IN TO BY IS TRUE CASE MOD TYPE CONST MODULE UNTIL DIV NIL VAR DO OF WHILE ELSE OR ELSIF POINTER END PROCEDURE FALSE RECORD FOR REPEAT IF RETURN";
+var reservedWords = ["ARRAY", "IMPORT", "THEN", "BEGIN", "IN", "TO", "BY", "IS", "TRUE", "CASE", "MOD", "TYPE", "CONST", 
+                     "MODULE", "UNTIL", "DIV", "NIL", "VAR", "DO", "OF", "WHILE", "ELSE", "OR", "ELSIF", "POINTER", "END", 
+                     "PROCEDURE", "FALSE", "RECORD", "FOR", "REPEAT", "IF", "RETURN"];
 
 function make(makeIdentdef,
               makeDesignator,

+ 12 - 30
src/ob/Lexer.ob

@@ -6,15 +6,15 @@ CONST
     commentBegin = "(*";
     commentEnd = "*)";
 
-    jsReservedWords 
-        = "break case catch const continue debugger default delete do else finally "
-        + "for function if in instanceof new return switch this throw try typeof "
-        + "var void while with false true null class enum export extends "
-        + "import super implements interface let package private protected "
-        + "public static yield "
-        + "Object Math Number" (* Object, Math and Number are used in generated code for some functions so it is 
-                    reserved word from code generator standpoint *)
-        ;
+    jsReservedWords = [
+        "break", "case", "catch", "const", "continue", "debugger", "default", "delete", "do", "else", "finally",
+        "for", "function", "if", "in", "instanceof", "new", "return", "switch", "this", "throw", "try", "typeof",
+        "var", "void", "while", "with", "false", "true", "null", "class", "enum", "export", "extends",
+        "import", "super", "implements", "interface", "let", "package", "private", "protected",
+        "public", "static", "yield",
+        "Object", "Math", "Number" (* Object, Math and Number are used in generated code for some functions so it is 
+                                      reserved word from code generator standpoint *)
+        ];
 
 TYPE
     Literal* = RECORD
@@ -252,25 +252,7 @@ BEGIN
     RETURN result
 END string;
 
-PROCEDURE isReservedWord(s: STRING; words: STRING): BOOLEAN;
-VAR
-    i, w: INTEGER;
-BEGIN
-    WHILE (w < LEN(words))
-        & (i < LEN(s))
-        & (words[w] = s[i])
-        & ((i # 0) OR (w = 0) OR (words[w - 1] = " ")) DO
-        INC(w);
-        INC(i);
-    ELSIF (w < LEN(words)) 
-        & ((i < LEN(s)) OR (words[w] # " ")) DO
-        INC(w);
-        i := 0;
-    END;
-    RETURN i = LEN(s)
-END isReservedWord;
-
-PROCEDURE ident*(VAR stream: Stream.Type; context: ContextHierarchy.Node; reservedWords: STRING): BOOLEAN;
+PROCEDURE ident*(VAR stream: Stream.Type; context: ContextHierarchy.Node; reservedWords: ARRAY OF STRING): BOOLEAN;
 VAR
     result: BOOLEAN;
     c: CHAR;
@@ -289,8 +271,8 @@ BEGIN
                 Stream.next(stream, -1);
             END;
 
-            IF ~isReservedWord(s, reservedWords) THEN
-                IF isReservedWord(s, jsReservedWords) THEN
+            IF reservedWords.indexOf(s) = -1 THEN
+                IF jsReservedWords.indexOf(s) # -1 THEN
                     s := s + "$";
                 END;
                 context.handleIdent(s);