]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/header/ModuleHeader.java
(refs #7307) Added features field to SCL module header
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / internal / header / ModuleHeader.java
1 package org.simantics.scl.compiler.internal.header;
2
3 import java.util.List;
4
5 import org.simantics.scl.compiler.elaboration.expressions.EVar;
6 import org.simantics.scl.compiler.elaboration.expressions.annotations.AnnotationUtils;
7 import org.simantics.scl.compiler.elaboration.expressions.records.FieldAssignment;
8 import org.simantics.scl.compiler.errors.ErrorLog;
9
10 public class ModuleHeader {
11     public String classLoader;
12     public long classLoaderLocation;
13     public String defaultLocalName;
14     public List<EVar> export;
15     
16     // Features
17     public boolean chr;
18     public boolean fields;
19     
20     private void read(ErrorLog errorLog, FieldAssignment[] fields) {
21         for(FieldAssignment assignment : fields)
22             switch(assignment.name) {
23             case "bundle":
24                 if(assignment.value == null)
25                     errorLog.log(assignment.location, "Property classLoader needs to be given a string value.");
26                 else {
27                     classLoader = AnnotationUtils.extractString(assignment.value);
28                     if(classLoader == null)
29                         errorLog.log(assignment.value.location, "Expected bundle name here.");
30                     else 
31                         classLoaderLocation = assignment.location;
32                 }
33                 break;
34             case "export":
35                 if(assignment.value == null)
36                     errorLog.log(assignment.location, "Property export needs to be given a list of exported symbols.");
37                 else {
38                     export = AnnotationUtils.extractIdentifierList(assignment.value);
39                     if(export == null)
40                         errorLog.log(assignment.value.location, "Expected a list of exported symbols.");
41                 }
42                 break;
43             case "features":
44                 if(assignment.value == null)
45                     errorLog.log(assignment.location, "Property features needs to be given a list of features (identifiers).");
46                 else {
47                     List<EVar> features = AnnotationUtils.extractIdentifierList(assignment.value);
48                     if(features == null)
49                         errorLog.log(assignment.value.location, "Expected a list of features (identifiers).");
50                     for(EVar feature : features)
51                         handleFeature(errorLog, feature);
52                 }
53                 break;
54             case "defaultLocalName":
55                 if(assignment.value == null)
56                     errorLog.log(assignment.location, "Property defaultLocalName needs to be given a string value.");
57                 else {
58                     defaultLocalName = AnnotationUtils.extractString(assignment.value);
59                     if(defaultLocalName == null)
60                         errorLog.log(assignment.value.location, "Expected string here.");
61                 }
62                 break;
63             default:
64                 errorLog.logWarning(assignment.location, "Unknown module header field was skipped.");
65             }
66     }
67     
68     private void handleFeature(ErrorLog errorLog, EVar feature) {
69         switch(feature.name) {
70         case "chr": chr = true; break;
71         case "fields": fields = true; break;
72         default:
73             errorLog.log(feature.location, "Unknown feature " + feature.name + ".");
74         }
75     }
76
77     public static ModuleHeader process(ErrorLog errorLog, FieldAssignment[] fields) {
78         if(fields == null)
79             return null;
80         ModuleHeader result = new ModuleHeader();
81         result.read(errorLog, fields);
82         return result;
83     }
84 }