]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/parsing/parser/SCL.grammar
Added module header feature to SCL language.
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / internal / parsing / parser / SCL.grammar
1
2 /******************************************************************************
3  * Initial
4  */
5
6 initial module ;
7 initial commands ;
8 initial import ;
9 initial type ;
10 initial exp ;
11 initial equationBlock ;
12
13 module
14     = (declaration (SEMICOLON declaration)*)?                # Module
15     ;
16
17 commands 
18     = command?                                               # OneCommand
19     | commands SEMICOLON command                             # ManyCommands 
20     ;
21     
22 command
23     = statement                                              # StatementCommand
24     | import                                                 # ImportCommand
25     ;
26
27 /******************************************************************************
28  * Declarations
29  */
30  
31 declarations
32     = LBRACE (declaration (SEMICOLON declaration)*)? RBRACE  # Declarations
33     ;
34
35 declaration
36     = MODULE LBRACE (field (COMMA field)*)? RBRACE           # ModuleHeader
37     | var (COMMA var)* HASTYPE type                          # TypeAnnotation
38     | bexp rhs                                               # ValueDefinition
39     | DATA ID+ (EQUALS constructor (BAR constructor)*)?      # DataDefinition
40     | TYPE ID+ EQUALS type                                   # TypeDefinition
41     | CLASS context? ID+ 
42       (BAR fundeps)?
43       (WHERE declarations)?                                  # ClassDefinition
44     | INSTANCE context? ID atype+ (WHERE declarations)?      # InstanceDefinition
45     | DERIVING INSTANCE context? ID atype+                   # DerivingInstanceDefinition
46     | BEGIN_STRING END_STRING                                # DocumentationString
47     | ANNOTATION_ID aexp*                                    # Annotation
48     | (INFIX | INFIXL | INFIXR) INTEGER var (COMMA var)*     # PrecedenceDefinition
49     | import                                                 # JustImport
50     | IMPORTJAVA BEGIN_STRING END_STRING WHERE declarations  # ImportJava
51     | EFFECT ID BEGIN_STRING END_STRING 
52                 BEGIN_STRING END_STRING                      # EffectDefinition
53     | (RULE | ABSTRACT_RULE) ID 
54       (EXTENDS ID (COMMA ID)*)?
55       WHERE ruleDeclarations                                 # RuleDefinition
56     | MAPPING_RELATION ID atype*                             # MappingRelationDefinition
57     | bexp FOLLOWS ruleDeclarations                          # RelationDefinition
58     ;
59
60 import
61     = (IMPORT | INCLUDE)
62       BEGIN_STRING END_STRING
63       (AS ID)?    
64       importSpec?                                            # Import
65     ;
66     
67 importSpec 
68     = LPAREN (importItem (COMMA importItem)*)? RPAREN         # ImportShowing
69     | HIDING LPAREN (importItem (COMMA importItem)*)? RPAREN  # ImportHiding
70     ;
71
72 importItem 
73     = ID                                                      # ImportValueItem
74 //    | (TYPE | DATA) ID                                        # ImportTypeItem
75 //    | CLASS ID                                                # ImportClassItem
76     ;
77     
78
79 constructor
80     = (ANNOTATION_ID aexp)* ID atype*                        # Constructor
81     | (ANNOTATION_ID aexp)* ID LBRACE
82           fieldDeclaration (COMMA fieldDeclaration)* RBRACE  # RecordConstructor
83     ;
84     
85 fieldDeclaration
86     = ID HASTYPE type                                        # FieldDescription
87     ;
88
89 rhs = EQUALS exp (WHERE statements)?                         # SimpleRhs
90     | guardedExpEq+ (WHERE statements)?                      # GuardedRhs
91     ;
92
93 guardedExpEq
94     = BAR exp (COMMA exp)* EQUALS exp                        # GuardedExpEq
95     ;
96     
97 fundep
98     = ID+ ARROW ID                                           # Fundep
99     ;
100
101 fundeps
102     = fundep (COMMA fundep)*                                 # Fundeps
103     ;
104
105 /******************************************************************************
106  * Rules
107  */
108
109 ruleDeclarations
110     = LBRACE (ruleDeclaration (SEMICOLON ruleDeclaration)*)? RBRACE  # RuleDeclarations
111     ;
112
113 ruleDeclaration
114     = query                                                  # QueryRuleDeclaration
115     | ANNOTATION_ID aexp*                                    # Annotation
116     ;
117     
118 /******************************************************************************
119  * Expressions
120  */
121  
122 exp = bexp (HASTYPE type)?                                   # LocalTypeAnnotation, shift HASTYPE, shift COLON
123     | bexp COLON ID WITH? queryBlock?                        # EntityTypeAnnotation, shift LBRACE, shift WITH
124     ;
125
126 bexp 
127     =  MINUS? lexp (symbol lexp)*                            # Binary, shift MINUS, shift SYMBOL,
128                                                                shift LESS, shift GREATER, shift SEPARATED_DOT,
129                                                                shift ESCAPED_ID
130     ;
131
132 lexp 
133     = faexp+                                                 # Apply, shift ID, shift LAMBDA, shift LAMBDA_MATCH,
134                                                                shift LET, shift INTEGER, shift BEGIN_STRING,
135                                                                shift IF, shift MATCH, shift DO,
136                                                                shift MDO, shift ENFORCE, shift BLANK,
137                                                                shift FLOAT, shift LPAREN, shift LBRACKET,
138                                                                shift ESCAPED_SYMBOL, shift CHAR, shift LBRACE,
139                                                                shift WHEN, shift ATTACHED_HASH,
140                                                                shift SELECT, shift SELECT_FIRST, shift SELECT_DISTINCT,
141                                                                shift TRANSFORMATION, shift EQ
142     ; 
143
144 faexp
145     = aexp ((ATTACHED_DOT | ATTACHED_HASH) accessor)*        # FieldAccess, shift ATTACHED_DOT, shift ATTACHED_HASH
146     ;
147
148 accessor
149     = ID                                                     # IdAccessor
150     | BEGIN_STRING END_STRING                                # StringAccessor
151     | LPAREN exp RPAREN                                      # ExpAccessor
152     ;
153
154 aexp 
155     = LAMBDA aexp+ ARROW exp                                 # Lambda, shift HASTYPE
156     | LAMBDA_MATCH LBRACE case (SEMICOLON case)* RBRACE      # LambdaMatch, shift HASTYPE
157     | LET statements IN exp                                  # Let, shift HASTYPE
158     | IF exp THEN exp (ELSE exp)?                            # If, shift HASTYPE, shift ELSE
159     | MATCH exp WITH
160       LBRACE case (SEMICOLON case)* RBRACE                   # Match
161     | (DO | MDO) statements                                  # Do
162     | (SELECT | SELECT_FIRST | SELECT_DISTINCT) 
163       exp WHERE queryBlock                                   # Select
164     | ENFORCE queryBlock                                     # Enforce
165     //| WHEN queryBlock SEMICOLON exp                          # When
166     | var                                                    # Var
167     | ATTACHED_HASH ID                                       # HashedId
168     | BLANK                                                  # Blank
169     | INTEGER                                                # Integer
170     | FLOAT                                                  # Float
171     | stringLiteral                                          # String
172     | CHAR                                                   # Char
173     | LPAREN (exp (COMMA exp)*)? RPAREN                      # Tuple
174     | LPAREN exp ARROW exp RPAREN                            # ViewPattern    
175     | LPAREN symbolWithoutMinus lexp RPAREN                  # RightSection
176     | LPAREN lexp symbol RPAREN                              # LeftSection
177     | LBRACKET (exp (COMMA exp)*)? RBRACKET                  # ListLiteral
178     | LBRACKET exp DOTDOT exp RBRACKET                       # Range
179     | LBRACKET exp BAR 
180       listQualifier (COMMA listQualifier)* RBRACKET          # ListComprehension
181     | ID AT aexp                                             # As
182     | ID LBRACE (field (COMMA field)*)? RBRACE               # Record
183     | TRANSFORMATION ID WHERE queryBlock                     # Transformation
184     | EQ LBRACE equationBlock RBRACE                         # Eq
185     ;
186     
187 stringLiteral
188     = BEGIN_STRING (SUSPEND_STRING exp CONTINUE_STRING)* 
189       END_STRING                                             # StringLiteral
190     ;
191
192 statements
193     = LBRACE (statement (SEMICOLON statement)*)? RBRACE      # Statements
194     ;
195
196 statement
197     = exp                                                    # GuardStatement
198     | exp rhs                                                # LetStatement
199     | exp BINDS exp                                          # BindStatement
200     | exp FOLLOWS queryBlock                                 # RuleStatement
201     | chrQuery IMPLIES chrQuery                              # CHRStatement
202     | WHEN verboseChrQuery THEN_AFTER_WHEN verboseChrQuery   # VerboseCHRStatement
203     | CONSTRAINT ID atype*                                   # ConstraintStatement
204     ;
205
206 chrQuery 
207     = listQualifier (COMMA listQualifier)*                   # CHRQuery
208     ;
209
210 verboseChrQuery
211     = LBRACE listQualifier (SEMICOLON listQualifier)* RBRACE # VerboseCHRQuery
212     ;
213
214 listQualifier
215     = exp                                                    # GuardQualifier
216     | exp EQUALS exp                                         # LetQualifier
217     | exp BINDS exp                                          # BindQualifier
218     | THEN exp (BY exp)?                                     # ThenQualifier
219     ;
220     
221 case
222     = exp caseRhs                                            # Case
223     ;
224
225 caseRhs 
226     = ARROW exp (WHERE statements)?                          # SimpleCaseRhs
227     | guardedExpArrow+ (WHERE statements)?                   # GuardedCaseRhs
228     ;
229
230 guardedExpArrow
231     = BAR exp (COMMA exp)* ARROW exp                         # GuardedExpArrow
232     ;
233
234 field
235     = ID EQUALS exp                                          # Field
236     | ID                                                     # FieldShorthand
237     ;
238
239 /******************************************************************************
240  * Queries
241  */
242
243 queryBlock 
244     = LBRACE (query (SEMICOLON query)*)? RBRACE              # QueryBlock
245     ;
246
247 query
248     = exp                                                    # GuardQuery, shift BINDS, shift EQUALS
249     | exp EQUALS exp                                         # EqualsQuery
250     | exp BINDS exp                                          # BindQuery
251     | QUERY_OP queryBlock                                    # CompositeQuery
252     ;
253
254 /******************************************************************************
255  * Equations
256  */
257  
258 equationBlock
259     = (equation (SEMICOLON equation)*)?                      # EquationBlock
260     ;
261     
262 equation
263     = exp                                                    # GuardEquation
264     | exp EQUALS exp                                         # BasicEquation
265     ;
266  
267 /******************************************************************************
268  * Types
269  */
270
271 context
272     = LPAREN type (COMMA type)* RPAREN IMPLIES               # Context
273     ;
274
275 type 
276     = etype ((ARROW | IMPLIES) etype)*                       # Arrow, shift ARROW, shift IMPLIES
277     ;
278
279 etype
280     = LESS ID (COMMA ID)* GREATER btype                      # Effect
281     | btype                                                  # JustEtype
282     | FORALL ID+ (SEPARATED_DOT | ATTACHED_DOT) type         # ForAll
283     ;
284
285 btype 
286     = atype+                                                 # ApplyType, shift ID, shift LPAREN,
287                                                                shift LBRACKET
288     ;
289
290 atype
291     = ID                                                     # TypeVar
292     | LPAREN (type (COMMA type)*)? RPAREN                    # TupleType
293     | LBRACKET type RBRACKET                                 # ListType
294     | LBRACKET RBRACKET                                      # ListTypeConstructor
295     | LPAREN COMMA+ RPAREN                                   # TupleTypeConstructor
296     ;
297
298 /******************************************************************************
299  * Variables and symbols
300  */
301
302 var = ID                                                     # VarId
303     | ESCAPED_SYMBOL                                         # EscapedSymbol
304     | LPAREN COMMA+ RPAREN                                   # TupleConstructor
305     ;
306
307 symbol
308     = SYMBOL                                                 # Symbol
309     | ESCAPED_ID                                             # EscapedId
310     | MINUS                                                  # Minus
311     | LESS                                                   # Less
312     | GREATER                                                # Greater
313     | SEPARATED_DOT                                          # Dot
314     ;
315     
316 symbolWithoutMinus
317     = SYMBOL                                                 # Symbol
318     | ESCAPED_ID                                             # EscapedId
319     | LESS                                                   # Less
320     | GREATER                                                # Greater
321     | SEPARATED_DOT                                          # Dot
322     ;
323
324 /******************************************************************************
325  * Auxiliary tokens
326  */
327 dummy = COMMENT EOL                                          # Dummy 
328       ;