Selaa lähdekoodia

MAP may have only STRING key

Vladislav Folts 10 vuotta sitten
vanhempi
commit
3a70c10701

BIN
bin/compiled.zip


+ 6 - 6
src/eberon/EberonMap.ob

@@ -2,9 +2,9 @@ MODULE EberonMap;
 IMPORT Context, Types;
 TYPE
     Type* = RECORD(Types.StorageType)
-        PROCEDURE Type*(from, to: Types.PType);
+        PROCEDURE Type*(type: Types.PType);
 
-        from, to: Types.PType;
+        type: Types.PType;
     END;
 
 PROCEDURE Type.initializer(cx: Context.Type): STRING;
@@ -12,12 +12,12 @@ PROCEDURE Type.initializer(cx: Context.Type): STRING;
 END;
 
 PROCEDURE Type.description(): STRING;
-    RETURN "MAP " + SELF.from.description() + " TO " + SELF.to.description();
+    RETURN "MAP OF " + SELF.type.description();
 END;
 
-PROCEDURE Type.Type(from, to: Types.PType)
-    | from(from), 
-      to(to);
+PROCEDURE Type.Type(type: Types.PType)
+    | type(type);
 END;
 
 END EberonMap.
+

+ 3 - 23
src/eberon/eberon_context.js

@@ -1147,44 +1147,24 @@ var ArrayDimensions = Context.ArrayDimensions.extend({
     }
 });
 
-function checkMapFromType(type){
-    if (Type.numeric().indexOf(type) != -1
-        || type == Type.basic().set
-        || type == EberonString.string())
-        return;
-    throw new Errors.Error("cannot use '" + type.description() + "' as a key of the map, numeric type or SET or STRING or CHAR expected");
-}
-
 var MapDecl = Context.Chained.extend({
     init: function EberonContext$MapDecl(context){
         Context.Chained.prototype.init.call(this, context);
-        this.__fromType = undefined;
-        this.__toType = undefined;
+        this.__type = undefined;
     },
     handleQIdent: function(q){
         var s = Context.getQIdSymbolAndScope(this, q);
         var type = Context.unwrapType(s.symbol().info());
-        
-        if (!type && !this.__fromType)
-        // This is yet-to-declare type - i.e. MAP is declared during RECORD/ARRAY/PROCEDURE declarion.
-        // None of those types are supported as map's key (but can be map's value)
-            throw new Errors.Error("cannot use '" + q.id + "' as a key of the map, numeric type or SET or STRING or CHAR expected");
-
         this.setType(type);
     },
     // anonymous types can be used in map declaration
     setType: function(type){
-        if (!this.__fromType){
-            checkMapFromType(type);
-            this.__fromType = type;
-        }
-        else
-            this.__toType = type;
+        this.__type = type;
     },
     isAnonymousDeclaration: function(){return true;},
     typeName: function(){return undefined;},
     endParse: function(){
-        this.parent().setType(new EberonMap.Type(this.__fromType, this.__toType));
+        this.parent().setType(new EberonMap.Type(this.__type));
     }
 });
 

+ 1 - 1
src/eberon/eberon_grammar.js

@@ -17,7 +17,7 @@ var repeat = Parser.repeat;
 var required = Parser.required;
 
 function makeStrucType(base, type){
-    var mapType = context(and("MAP", type, "TO", type), EbContext.MapDecl);
+    var mapType = context(and("MAP", "OF", type), EbContext.MapDecl);
     return or(base, mapType);
 }
 

+ 1 - 1
test/input/eberon/map.ob

@@ -1,4 +1,4 @@
 MODULE m;
 VAR
-    v: MAP STRING TO INTEGER;
+    v: MAP OF INTEGER;
 END m.

+ 13 - 23
test/test_unit_eberon.js

@@ -1325,30 +1325,20 @@ exports.suite = {
 "map": {
     "declaration": testWithGrammar(
         grammar.declarationSequence, 
-        pass("TYPE M = MAP INTEGER TO INTEGER;",
-             "TYPE M = MAP INTEGER TO PROCEDURE;",
-             "TYPE M = MAP INTEGER TO PROCEDURE();",
-             "TYPE M = MAP INTEGER TO PROCEDURE(): INTEGER;",
-             "TYPE M = MAP INTEGER TO PROCEDURE(): M;",
-             "TYPE M = MAP STRING TO RECORD END;",
-             "TYPE M = MAP STRING TO POINTER TO RECORD END;",
-             "TYPE M = MAP INTEGER TO MAP INTEGER TO INTEGER;",
-             "TYPE M = MAP INTEGER TO M;",
-             "TYPE T = RECORD field: MAP INTEGER TO T; END;",
-             "VAR v: MAP SET TO STRING;"
+        pass("TYPE M = MAP OF INTEGER;",
+             "TYPE M = MAP OF PROCEDURE;",
+             "TYPE M = MAP OF PROCEDURE();",
+             "TYPE M = MAP OF PROCEDURE(): INTEGER;",
+             "TYPE M = MAP OF PROCEDURE(): M;",
+             "TYPE M = MAP OF RECORD END;",
+             "TYPE M = MAP OF POINTER TO RECORD END;",
+             "TYPE M = MAP OF MAP OF INTEGER;",
+             "TYPE M = MAP OF M;",
+             "TYPE T = RECORD field: MAP OF T; END;",
+             "VAR v: MAP OF SET;"
             ),
-        fail(["TYPE T = RECORD END; M = MAP T TO INTEGER;", "cannot use 'T' as a key of the map, numeric type or SET or STRING or CHAR expected"],
-             ["TYPE M = MAP RECORD END TO INTEGER;", "cannot use 'anonymous RECORD' as a key of the map, numeric type or SET or STRING or CHAR expected"],
-             ["TYPE M = MAP ARRAY 3 OF INTEGER TO INTEGER;", "cannot use 'ARRAY 3 OF INTEGER' as a key of the map, numeric type or SET or STRING or CHAR expected"],
-             ["TYPE M = MAP ARRAY 3 OF CHAR TO INTEGER;", "cannot use 'ARRAY 3 OF CHAR' as a key of the map, numeric type or SET or STRING or CHAR expected"],
-             ["TYPE M = MAP ARRAY * OF INTEGER TO INTEGER;", "cannot use 'ARRAY * OF INTEGER' as a key of the map, numeric type or SET or STRING or CHAR expected"],
-             ["TYPE M = MAP MAP INTEGER TO INTEGER TO INTEGER;", "cannot use 'MAP INTEGER TO INTEGER' as a key of the map, numeric type or SET or STRING or CHAR expected"],
-             ["TYPE P = POINTER TO MAP INTEGER TO INTEGER;", "RECORD is expected as a POINTER base type, got 'MAP INTEGER TO INTEGER'"],
-             ["TYPE M = MAP POINTER TO RECORD END TO BOOLEAN;", "cannot use 'POINTER TO anonymous RECORD' as a key of the map, numeric type or SET or STRING or CHAR expected"],
-             ["TYPE M = MAP PROCEDURE TO PROCEDURE;", "cannot use 'PROCEDURE' as a key of the map, numeric type or SET or STRING or CHAR expected"],
-             ["TYPE M = MAP INTEGER TO Undeclared;", "undeclared identifier: 'Undeclared'"],
-             ["TYPE M = MAP M TO INTEGER;", "cannot use 'M' as a key of the map, numeric type or SET or STRING or CHAR expected"],
-             ["TYPE T = RECORD field: MAP T TO INTEGER; END", "cannot use 'T' as a key of the map, numeric type or SET or STRING or CHAR expected"],
+        fail(["TYPE P = POINTER TO MAP OF INTEGER;", "RECORD is expected as a POINTER base type, got 'MAP OF INTEGER'"],
+             ["TYPE M = MAP OF Undeclared;", "undeclared identifier: 'Undeclared'"],
              ["VAR MAP: INTEGER;", "not parsed"]
             )
         )