From 01ee3b1c456e43355374b1af4ba188d6c890e6f8 Mon Sep 17 00:00:00 2001 From: Sven Gestegard Robertz <sven.robertz@cs.lth.se> Date: Wed, 20 May 2015 12:05:20 +0200 Subject: [PATCH] refactored and added annotations to Decl --- compiler/2014/Annotations.jrag | 24 +++++++++++++++--------- compiler/2014/LabComm.ast | 9 +++++---- compiler/2014/Refactoring.jrag | 32 ++++++++++++++++++++++++++++++-- examples/user_types/test.lc | 2 +- 4 files changed, 51 insertions(+), 16 deletions(-) diff --git a/compiler/2014/Annotations.jrag b/compiler/2014/Annotations.jrag index 987dad9..ea43c46 100644 --- a/compiler/2014/Annotations.jrag +++ b/compiler/2014/Annotations.jrag @@ -2,21 +2,27 @@ aspect Annotations { syn boolean TypeInstance.hasAnnotations() = getAnnotations().getNumAnnotation()>0; syn boolean TypeInstance.hasIntentions() = ! intentionSet().isEmpty(); -// syn String Annotations.getName() = new String(lookup("")); -// -// syn byte[] Annotation.lookup(String key) = (getKey().equals(key) ? getValue() : null); + syn String Annotations.getName() = new String(lookup("")); + + syn byte[] Annotations.lookup(String key) { + for(Annotation a: getAnnotations()) { + byte[] res = a.lookup(key); + if(res != null) return res; + } + return null; + } + + syn byte[] Annotation.lookup(String key) = (getKey().equals(key) ? getValue() : null); syn boolean Annotation.isIntention() = false; eq Intention.isIntention() = true; - inh ASTNode Annotation.parentInstance(); - eq TypeInstance.getAnnotations().parentInstance() = this; - eq Decl.getAnnotations().parentInstance() = this; - + inh AnnotatedNode Annotation.parentInstance(); + eq AnnotatedNode.getAnnotations().parentInstance() = this; - coll Set<Intention> TypeInstance.intentionSet() [new HashSet<Intention>()] with add; + coll Set<Intention> AnnotatedNode.intentionSet() [new HashSet<Intention>()] with add; Intention contributes this - to TypeInstance.intentionSet() + to AnnotatedNode.intentionSet() for parentInstance(); } diff --git a/compiler/2014/LabComm.ast b/compiler/2014/LabComm.ast index ad97733..92d2aab 100644 --- a/compiler/2014/LabComm.ast +++ b/compiler/2014/LabComm.ast @@ -1,8 +1,9 @@ Specification ::= Decl*; -abstract Decl ::= TypeInstance Annotations /Signature/; +abstract AnnotatedNode ::= Annotations; +abstract Decl : AnnotatedNode ::= TypeInstance /Signature/; -TypeInstance ::= DataType <Name:String> Annotations; +TypeInstance :AnnotatedNode ::= DataType; Annotations ::= Annotation*; Annotation ::= <Key:String> <Value:byte[]>; @@ -16,9 +17,9 @@ SampleDecl : Decl; //Signatures are in the abstract grammar, so that //they can be extended and refined by aspects. -Signature ::= SignatureList FlatSignatureList:SignatureList; +Signature ::= SignatureList FlatSignatureList:SignatureList; SignatureList ::= SignatureLine*; -abstract SignatureLine ::= <Indent:int> <Comment:String>; +abstract SignatureLine ::= <Indent:int> <Comment:String>; abstract DataSignatureLine : SignatureLine; ByteArraySignatureLine : DataSignatureLine ::= <Data:byte[]>; IntSignatureLine : DataSignatureLine ::= <Data:int>; diff --git a/compiler/2014/Refactoring.jrag b/compiler/2014/Refactoring.jrag index 43bddd2..42ecead 100644 --- a/compiler/2014/Refactoring.jrag +++ b/compiler/2014/Refactoring.jrag @@ -3,12 +3,40 @@ aspect Refactoring { syn String Decl.getName() = getTypeInstance().getName(); syn DataType Decl.getDataType() = getTypeInstance().getDataType(); + syn String TypeInstance.getName() = getAnnotations().getName(); + + public TypeDecl.TypeDecl(TypeInstance t, Annotations a) { + this(a, t); + } + + public SampleDecl.SampleDecl(TypeInstance t, Annotations a) { + this(a, t); + } + + public TypeInstance.TypeInstance(DataType t, Annotations a) { + this(a, t); + } + + public Field.Field(DataType t, Annotations a) { + this(a, t); + } + + public Annotations Annotations.addName(String n) { + //XXX TODO: check if name already exists + addAnnotation(new Annotation("",n.getBytes())); + return this; + } + public Field.Field(TypeInstance t) { - this(t.getDataType(), t.getName(), t.getAnnotations()); + this(t.getDataType(), t.getAnnotations().addName(t.getName())); + } + + public TypeInstance.TypeInstance(DataType t, String n, Annotations a) { + this(t, a.addName(n)); } public TypeInstance.TypeInstance(DataType t, String n) { - this(t, n, new Annotations()); + this(t, new Annotations().addName(n)); System.out.println("WARNING! TypeInstance(DataType, String) ignoring intention list"); } public TypeDecl.TypeDecl(TypeInstance t) { diff --git a/examples/user_types/test.lc b/examples/user_types/test.lc index fdc9d54..353877f 100644 --- a/examples/user_types/test.lc +++ b/examples/user_types/test.lc @@ -28,7 +28,7 @@ typedef struct { boolean c; } foo; -sample struct { +(nisse:hej) sample struct { (name:l1)line l1; (name:l2)line l2; (b:"kalle anka")(c:hejdu)(a:"kalle anka")foo f; -- GitLab