1 package org.simantics.scl.compiler.elaboration.expressions.annotations;
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;
11 public class AnnotationUtils {
13 * Processes an annotation of form
14 * <pre>@Annotation "text"</pre>
16 * <pre>@Annotation text</pre>
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]);
23 errorLog.log(annotation.location, "Expected a string parameter for " + annotation.id.text);
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);
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];
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();