Skip to content
Snippets Groups Projects
Commit 83ee58cb authored by Niklas Fors's avatar Niklas Fors
Browse files

Support parameterized types in intrinsic attributes

parent cc7b2dfa
Branches
Tags
No related merge requests found
...@@ -2,6 +2,8 @@ import java.util.*; ...@@ -2,6 +2,8 @@ import java.util.*;
aspect TypeAnalysis { aspect TypeAnalysis {
public abstract TypeUse Component.getTypeUse();
syn TypeDecl TypeUse.decl() = lookupType(getID()); syn TypeDecl TypeUse.decl() = lookupType(getID());
inh TypeDecl TypeUse.lookupType(String name); inh TypeDecl TypeUse.lookupType(String name);
eq Program.getChild().lookupType(String name) { eq Program.getChild().lookupType(String name) {
...@@ -37,7 +39,7 @@ aspect ComponentAnalysis { ...@@ -37,7 +39,7 @@ aspect ComponentAnalysis {
eq Relation.getLeft().otherSide() = getRight(); eq Relation.getLeft().otherSide() = getRight();
eq Relation.getRight().otherSide() = getLeft(); eq Relation.getRight().otherSide() = getLeft();
eq Program.getChild().otherSide() = null; eq Program.getChild().otherSide() = null;
syn TypeDecl RelationComponent.ofTypeDecl() = otherSide().toTypeDecl(); syn TypeDecl RelationComponent.ofTypeDecl() = otherSide().toTypeDecl();
syn boolean Component.isAlreadyDeclared() syn boolean Component.isAlreadyDeclared()
...@@ -102,9 +104,22 @@ aspect Constructors { ...@@ -102,9 +104,22 @@ aspect Constructors {
} }
aspect Utils { aspect Utils {
public String TypeUse.toString() { public String SimpleTypeUse.toString() {
return getID(); return getID();
} }
public String ParameterizedTypeUse.toString() {
StringBuilder sb = new StringBuilder();
sb.append(getID()).append("<");
int i = 0;
for (TypeUse u: getTypeUses()) {
sb.append(u.toString());
if (++i < getNumTypeUse()) {
sb.append(", ");
}
}
sb.append(">");
return sb.toString();
}
public String TypeDecl.toString() { public String TypeDecl.toString() {
return getID(); return getID();
} }
......
Program ::= TypeDecl* Relation*; Program ::= TypeDecl* Relation*;
TypeDecl ::= <ID> <Abstract:boolean> [Super:TypeUse] Component*; TypeDecl ::= <ID> <Abstract:boolean> [Super:TypeUse] Component*;
abstract Component ::= <ID> TypeUse; abstract Component ::= <ID>;
NormalComponent : Component; abstract SimpleTypeComponent : Component ::= TypeUse:SimpleTypeUse;
ListComponent : Component; NormalComponent : SimpleTypeComponent;
TokenComponent : Component; ListComponent : SimpleTypeComponent;
OptComponent : Component; OptComponent : SimpleTypeComponent;
TokenComponent : Component ::= TypeUse;
TypeUse ::= <ID>; abstract TypeUse ::= <ID>;
SimpleTypeUse : TypeUse;
ParameterizedTypeUse : TypeUse ::= TypeUse*;
Relation ::= Left:RelationComponent Direction Right:RelationComponent; Relation ::= Left:RelationComponent Direction Right:RelationComponent;
abstract RelationComponent : Component; abstract RelationComponent : SimpleTypeComponent;
OneRelationComponent : RelationComponent; OneRelationComponent : RelationComponent;
OptionalRelationComponent : RelationComponent; OptionalRelationComponent : RelationComponent;
ManyRelationComponent : RelationComponent; ManyRelationComponent : RelationComponent;
......
...@@ -17,11 +17,23 @@ TypeDecl type_decl = ...@@ -17,11 +17,23 @@ TypeDecl type_decl =
Opt type_decl_super = Opt type_decl_super =
/* empty */ {: return new Opt(); :} /* empty */ {: return new Opt(); :}
| COL type_use.u {: return new Opt(u); :} | COL s_type_use.u {: return new Opt(u); :}
;
SimpleTypeUse s_type_use =
ID {: return new SimpleTypeUse(ID); :}
; ;
TypeUse type_use = TypeUse type_use =
ID {: return new TypeUse(ID); :} s_type_use.u {: return u; :}
| parameterized_type_use.p {: return p; :}
;
ParameterizedTypeUse parameterized_type_use =
ID LT type_use_list.l GT {: return new ParameterizedTypeUse(ID, l); :}
;
List type_use_list =
type_use.u {: return new List().add(u); :}
| type_use_list.l COMMA type_use.u {: return l.add(u); :}
; ;
List components_opt = List components_opt =
...@@ -35,17 +47,17 @@ List components = ...@@ -35,17 +47,17 @@ List components =
; ;
Component component = Component component =
ID COL type_use.u {: return new NormalComponent(ID, u); :} ID COL s_type_use.u {: return new NormalComponent(ID, u); :}
| type_use.u {: return new NormalComponent(u.getID(), u); :} | s_type_use.u {: return new NormalComponent(u.getID(), u); :}
// List // List
| ID COL type_use.u STAR {: return new ListComponent(ID, u); :} | ID COL s_type_use.u STAR {: return new ListComponent(ID, u); :}
| type_use.u STAR {: return new ListComponent(u.getID(), u); :} | s_type_use.u STAR {: return new ListComponent(u.getID(), u); :}
// Opt // Opt
| ID COL type_use.u QUESTION_MARK {: return new OptComponent(ID, u); :} | ID COL s_type_use.u QUESTION_MARK {: return new OptComponent(ID, u); :}
| type_use.u QUESTION_MARK {: return new OptComponent(u.getID(), u); :} | s_type_use.u QUESTION_MARK {: return new OptComponent(u.getID(), u); :}
// Token // Token
| LT ID COL type_use.u GT {: return new TokenComponent(ID, u); :} | LT ID COL type_use.u GT {: return new TokenComponent(ID, u); :}
| LT ID GT {: return new TokenComponent(ID, new TypeUse("String")); :} | LT ID GT {: return new TokenComponent(ID, new SimpleTypeUse("String")); :}
; ;
List relations = List relations =
...@@ -60,14 +72,14 @@ Relation relation = ...@@ -60,14 +72,14 @@ Relation relation =
RelationComponent relation_comp = RelationComponent relation_comp =
// One // One
type_use.u DOT ID {: return new OneRelationComponent(ID, u); :} s_type_use.u DOT ID {: return new OneRelationComponent(ID, u); :}
| type_use.u {: return new OneRelationComponent("", u); :} | s_type_use.u {: return new OneRelationComponent("", u); :}
// Optional // Optional
| type_use.u DOT ID QUESTION_MARK {: return new OptionalRelationComponent(ID, u); :} | s_type_use.u DOT ID QUESTION_MARK {: return new OptionalRelationComponent(ID, u); :}
| type_use.u QUESTION_MARK {: return new OptionalRelationComponent("", u); :} | s_type_use.u QUESTION_MARK {: return new OptionalRelationComponent("", u); :}
// Many // Many
| type_use.u DOT ID STAR {: return new ManyRelationComponent(ID, u); :} | s_type_use.u DOT ID STAR {: return new ManyRelationComponent(ID, u); :}
| type_use.u STAR {: return new ManyRelationComponent("", u); :} | s_type_use.u STAR {: return new ManyRelationComponent("", u); :}
; ;
Direction direction = Direction direction =
......
...@@ -40,7 +40,7 @@ TraditionalComment = "/*" [^*] ~"*/" | "/*" "*"+ "/" ...@@ -40,7 +40,7 @@ TraditionalComment = "/*" [^*] ~"*/" | "/*" "*"+ "/"
EndOfLineComment = "//" [^\n|\r|\r\n]* EndOfLineComment = "//" [^\n|\r|\r\n]*
Comment = {TraditionalComment} | {EndOfLineComment} Comment = {TraditionalComment} | {EndOfLineComment}
ID = [a-zA-Z$][a-zA-Z0-9$_]* ID = [a-zA-Z$_][a-zA-Z0-9$_]*
%state STRING %state STRING
...@@ -57,6 +57,7 @@ ID = [a-zA-Z$][a-zA-Z0-9$_]* ...@@ -57,6 +57,7 @@ ID = [a-zA-Z$][a-zA-Z0-9$_]*
"::=" { return sym(Terminals.ASSIGN); } "::=" { return sym(Terminals.ASSIGN); }
"*" { return sym(Terminals.STAR); } "*" { return sym(Terminals.STAR); }
"." { return sym(Terminals.DOT); } "." { return sym(Terminals.DOT); }
"," { return sym(Terminals.COMMA); }
"<" { return sym(Terminals.LT); } "<" { return sym(Terminals.LT); }
">" { return sym(Terminals.GT); } ">" { return sym(Terminals.GT); }
"?" { return sym(Terminals.QUESTION_MARK); } "?" { return sym(Terminals.QUESTION_MARK); }
......
/AST/* /AST/*
/AllGen.ast /AllGen.ast
/AllGen.jadd /AllGen.jadd
/AllGenGen.ast
/AllGenGen.jadd
all: compile run all: compile run check-idempotent
compile: compile:
(cd .. && ant jar) (cd .. && ant jar)
java -jar ../relast-compiler.jar All.relast --file java -jar ../relast-compiler.jar All.relast --file
java -jar ../tools/jastadd2.jar --package=AST AllGen.ast AllGen.jadd Utils.jadd java -jar ../tools/jastadd2.jar --package=AST AllGen.ast AllGen.jadd Utils.jadd
run: run:
javac AST/*.java Test.java javac AST/*.java Test.java
java Test java Test
\ No newline at end of file check-idempotent:
java -jar ../relast-compiler.jar AllGen.ast --file
diff AllGen.ast AllGenGen.ast
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment