]> gerrit.simantics Code Review - simantics/platform.git/blob
8cc088b955b8ded475ba86d150551f5a5f2ec44c
[simantics/platform.git] /
1 package org.simantics.scl.compiler.elaboration.expressions.annotations;
2
3 import org.simantics.scl.compiler.constants.StringConstant;
4 import org.simantics.scl.compiler.elaboration.expressions.ELiteral;
5 import org.simantics.scl.compiler.elaboration.expressions.EStringLiteral;
6 import org.simantics.scl.compiler.elaboration.expressions.EVar;
7 import org.simantics.scl.compiler.elaboration.expressions.Expression;
8 import org.simantics.scl.compiler.errors.ErrorLog;
9 import org.simantics.scl.compiler.internal.parsing.declarations.DAnnotationAst;
10
11 public class AnnotationUtils {
12     /**
13      * Processes an annotation of form
14      * <pre>&#64;Annotation "text"</pre>
15      * or
16      * <pre>&#64;Annotation text</pre>
17      */
18     public static String processStringAnnotation(ErrorLog errorLog, DAnnotationAst annotation) {
19         if(annotation.parameters.length != 1)
20             errorLog.log(annotation.location, "Expected one string parameter for " + annotation.id.text);
21         String result = extractString(annotation.parameters[0]);
22         if(result == null)
23             errorLog.log(annotation.location, "Expected a string parameter for " + annotation.id.text);
24         return result;
25     }
26     
27     public static void processTagAnnotation(ErrorLog errorLog, DAnnotationAst annotation) {
28         if(annotation.parameters.length != 0)
29             errorLog.log(annotation.location, "Expected no parameters for " + annotation.id.text);
30     }
31     
32     public static String extractString(Expression expression) {
33         if(expression instanceof EVar)
34             return ((EVar)expression).name;
35         else if(expression instanceof EStringLiteral) {
36             EStringLiteral literal = (EStringLiteral)expression;
37             if(literal.strings.length == 1)
38                 return literal.strings[0];
39         }
40         else if(expression instanceof ELiteral) {
41             ELiteral literal = (ELiteral)expression;
42             if(literal.getValue() instanceof StringConstant) {
43                 StringConstant constant = (StringConstant)literal.getValue();
44                 return constant.getValue();
45             }
46         }
47         return null;
48     }
49 }