]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/parsing/parser/SCL.grammar
(refs #6924) Support for record field access syntax.
[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     ;
124
125 bexp 
126     =  MINUS? lexp (symbol lexp)*                            # Binary, shift MINUS, shift SYMBOL,
127                                                                shift LESS, shift GREATER, shift SEPARATED_DOT,
128                                                                shift ESCAPED_ID
129     ;
130
131 lexp 
132     = faexp+                                                 # Apply, shift ID, shift LAMBDA, shift LAMBDA_MATCH,
133                                                                shift LET, shift INTEGER, shift BEGIN_STRING,
134                                                                shift IF, shift MATCH, shift DO,
135                                                                shift MDO, shift ENFORCE, shift BLANK,
136                                                                shift FLOAT, shift LPAREN, shift LBRACKET,
137                                                                shift ESCAPED_SYMBOL, shift CHAR, shift LBRACE,
138                                                                shift WHEN, shift ATTACHED_HASH,
139                                                                shift SELECT, shift SELECT_FIRST, shift SELECT_DISTINCT,
140                                                                shift TRANSFORMATION, shift EQ
141     ; 
142
143 faexp
144     = aexp ((ATTACHED_DOT | ATTACHED_HASH) accessor)*        # FieldAccess, shift ATTACHED_DOT, shift ATTACHED_HASH
145     ;
146
147 accessor
148     = ID                                                     # IdAccessor
149     | BEGIN_STRING END_STRING                                # StringAccessor
150     | LPAREN exp RPAREN                                      # ExpAccessor
151     ;
152
153 aexp 
154     = LAMBDA aexp+ ARROW exp                                 # Lambda, shift HASTYPE
155     | LAMBDA_MATCH LBRACE case (SEMICOLON case)* RBRACE      # LambdaMatch, shift HASTYPE
156     | LET statements IN exp                                  # Let, shift HASTYPE
157     | IF exp THEN exp (ELSE exp)?                            # If, shift HASTYPE, shift ELSE
158     | MATCH exp WITH
159       LBRACE case (SEMICOLON case)* RBRACE                   # Match
160     | (DO | MDO) statements                                  # Do
161     | (SELECT | SELECT_FIRST | SELECT_DISTINCT) 
162       exp WHERE queryBlock                                   # Select
163     | ENFORCE queryBlock                                     # Enforce
164     //| WHEN queryBlock SEMICOLON exp                          # When
165     | var                                                    # Var
166     | ATTACHED_HASH ID                                       # HashedId
167     | BLANK                                                  # Blank
168     | INTEGER                                                # Integer
169     | FLOAT                                                  # Float
170     | stringLiteral                                          # String
171     | CHAR                                                   # Char
172     | LPAREN (exp (COMMA exp)*)? RPAREN                      # Tuple
173     | LPAREN exp ARROW exp RPAREN                            # ViewPattern    
174     | LPAREN symbolWithoutMinus lexp RPAREN                  # RightSection
175     | LPAREN lexp symbol RPAREN                              # LeftSection
176     | LBRACKET (exp (COMMA exp)*)? RBRACKET                  # ListLiteral
177     | LBRACKET exp DOTDOT exp RBRACKET                       # Range
178     | LBRACKET exp BAR 
179       listQualifier (COMMA listQualifier)* RBRACKET          # ListComprehension
180     | ID AT aexp                                             # As
181     | ID LBRACE (field (COMMA field)*)? RBRACE               # Record
182     | TRANSFORMATION ID WHERE queryBlock                     # Transformation
183     | EQ LBRACE equationBlock RBRACE                         # Eq
184     ;
185     
186 stringLiteral
187     = BEGIN_STRING (SUSPEND_STRING exp CONTINUE_STRING)* 
188       END_STRING                                             # StringLiteral
189     ;
190
191 statements
192     = LBRACE (statement (SEMICOLON statement)*)? RBRACE      # Statements
193     ;
194
195 statement
196     = exp                                                    # GuardStatement
197     | exp rhs                                                # LetStatement
198     | exp BINDS exp                                          # BindStatement
199     | exp FOLLOWS queryBlock                                 # RuleStatement
200     | chrQuery IMPLIES chrQuery                              # CHRStatement
201     | WHEN verboseChrQuery THEN_AFTER_WHEN verboseChrQuery   # VerboseCHRStatement
202     | CONSTRAINT ID atype*                                   # ConstraintStatement
203     ;
204
205 chrQuery 
206     = listQualifier (COMMA listQualifier)*                   # CHRQuery
207     ;
208
209 verboseChrQuery
210     = LBRACE listQualifier (SEMICOLON listQualifier)* RBRACE # VerboseCHRQuery
211     ;
212
213 listQualifier
214     = exp                                                    # GuardQualifier
215     | exp EQUALS exp                                         # LetQualifier
216     | exp BINDS exp                                          # BindQualifier
217     | THEN exp (BY exp)?                                     # ThenQualifier
218     ;
219     
220 case
221     = exp caseRhs                                            # Case
222     ;
223
224 caseRhs 
225     = ARROW exp (WHERE statements)?                          # SimpleCaseRhs
226     | guardedExpArrow+ (WHERE statements)?                   # GuardedCaseRhs
227     ;
228
229 guardedExpArrow
230     = BAR exp (COMMA exp)* ARROW exp                         # GuardedExpArrow
231     ;
232
233 field
234     = ID EQUALS exp                                          # Field
235     | ID                                                     # FieldShorthand
236     ;
237
238 /******************************************************************************
239  * Queries
240  */
241
242 queryBlock 
243     = LBRACE (query (SEMICOLON query)*)? RBRACE              # QueryBlock
244     ;
245
246 query
247     = exp                                                    # GuardQuery, shift BINDS, shift EQUALS
248     | exp EQUALS exp                                         # EqualsQuery
249     | exp BINDS exp                                          # BindQuery
250     | QUERY_OP queryBlock                                    # CompositeQuery
251     ;
252
253 /******************************************************************************
254  * Equations
255  */
256  
257 equationBlock
258     = (equation (SEMICOLON equation)*)?                      # EquationBlock
259     ;
260     
261 equation
262     = exp                                                    # GuardEquation
263     | exp EQUALS exp                                         # BasicEquation
264     ;
265  
266 /******************************************************************************
267  * Types
268  */
269
270 context
271     = LPAREN type (COMMA type)* RPAREN IMPLIES               # Context
272     ;
273
274 type 
275     = etype ((ARROW | IMPLIES) etype)*                       # Arrow, shift ARROW, shift IMPLIES
276     ;
277
278 etype
279     = LESS ID (COMMA ID)* GREATER btype                      # Effect
280     | btype                                                  # JustEtype
281     | FORALL ID+ (SEPARATED_DOT | ATTACHED_DOT) type         # ForAll
282     ;
283
284 btype 
285     = atype+                                                 # ApplyType, shift ID, shift LPAREN,
286                                                                shift LBRACKET
287     ;
288
289 atype
290     = ID                                                     # TypeVar
291     | LPAREN (type (COMMA type)*)? RPAREN                    # TupleType
292     | LBRACKET type RBRACKET                                 # ListType
293     | LBRACKET RBRACKET                                      # ListTypeConstructor
294     | LPAREN COMMA+ RPAREN                                   # TupleTypeConstructor
295     ;
296
297 /******************************************************************************
298  * Variables and symbols
299  */
300
301 var = ID                                                     # VarId
302     | ESCAPED_SYMBOL                                         # EscapedSymbol
303     | LPAREN COMMA+ RPAREN                                   # TupleConstructor
304     ;
305
306 symbol
307     = SYMBOL                                                 # Symbol
308     | ESCAPED_ID                                             # EscapedId
309     | MINUS                                                  # Minus
310     | LESS                                                   # Less
311     | GREATER                                                # Greater
312     | SEPARATED_DOT                                          # Dot
313     ;
314     
315 symbolWithoutMinus
316     = SYMBOL                                                 # Symbol
317     | ESCAPED_ID                                             # EscapedId
318     | LESS                                                   # Less
319     | GREATER                                                # Greater
320     | SEPARATED_DOT                                          # Dot
321     ;
322
323 /******************************************************************************
324  * Auxiliary tokens
325  */
326 dummy = COMMENT EOL                                          # Dummy 
327       ;