(refs #6923) Explicit export annotation for SCL modules
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / elaboration / expressions / annotations / AnnotationUtils.java
1 package org.simantics.scl.compiler.elaboration.expressions.annotations;
2
3 import java.util.Arrays;
4 import java.util.Collections;
5 import java.util.List;
6
7 import org.simantics.scl.compiler.constants.StringConstant;
8 import org.simantics.scl.compiler.elaboration.expressions.EListLiteral;
9 import org.simantics.scl.compiler.elaboration.expressions.ELiteral;
10 import org.simantics.scl.compiler.elaboration.expressions.EStringLiteral;
11 import org.simantics.scl.compiler.elaboration.expressions.EVar;
12 import org.simantics.scl.compiler.elaboration.expressions.Expression;
13 import org.simantics.scl.compiler.errors.ErrorLog;
14 import org.simantics.scl.compiler.internal.parsing.declarations.DAnnotationAst;
15
16 public class AnnotationUtils {
17     /**
18      * Processes an annotation of form
19      * <pre>&#64;Annotation "text"</pre>
20      * or
21      * <pre>&#64;Annotation text</pre>
22      */
23     public static String processStringAnnotation(ErrorLog errorLog, DAnnotationAst annotation) {
24         if(annotation.parameters.length != 1)
25             errorLog.log(annotation.location, "Expected one string parameter for " + annotation.id.text);
26         String result = extractString(annotation.parameters[0]);
27         if(result == null)
28             errorLog.log(annotation.location, "Expected a string parameter for " + annotation.id.text);
29         return result;
30     }
31     
32     public static void processTagAnnotation(ErrorLog errorLog, DAnnotationAst annotation) {
33         if(annotation.parameters.length != 0)
34             errorLog.log(annotation.location, "Expected no parameters for " + annotation.id.text);
35     }
36     
37     public static String extractString(Expression expression) {
38         if(expression instanceof EVar)
39             return ((EVar)expression).name;
40         else if(expression instanceof EStringLiteral) {
41             EStringLiteral literal = (EStringLiteral)expression;
42             if(literal.strings.length == 1)
43                 return literal.strings[0];
44         }
45         else if(expression instanceof ELiteral) {
46             ELiteral literal = (ELiteral)expression;
47             if(literal.getValue() instanceof StringConstant) {
48                 StringConstant constant = (StringConstant)literal.getValue();
49                 return constant.getValue();
50             }
51         }
52         return null;
53     }
54     
55     public static List<EVar> extractIdentifierList(Expression expression) {
56         if(expression instanceof EVar || expression instanceof EStringLiteral || expression instanceof ELiteral)
57             return Collections.singletonList(new EVar(expression.location, extractString(expression)));
58         else if(expression instanceof EListLiteral) {
59             Expression[] components = ((EListLiteral)expression).getComponents();
60             EVar[] items = new EVar[components.length];
61             for(int i=0;i<components.length;++i) {
62                 String value = extractString(components[i]);
63                 if(value == null)
64                     return null;
65                 items[i] = new EVar(components[i].location, value);
66             }
67             return Arrays.asList(items);
68         }
69         return null;
70     }
71 }