ScriptLanguage.g4 4.26 KB
Newer Older
1
grammar ScriptLanguage;
Sarah Grebing's avatar
Sarah Grebing committed
2
3
4
5
/*start
    : stmtList
    ;
*/
6

7
start
8
    :   (script)*
9
10
    ;

11
script
12
    : SCRIPT name=ID LPAREN signature=argList? RPAREN INDENT body=stmtList DEDENT
13
14
15
    ;


16
argList
17
    :   varDecl (COMMA varDecl)*
18
19
20
    ;

varDecl
21
    :   name=ID COLON type=ID
22
23
24
    ;

stmtList
Alexander Weigl's avatar
Alexander Weigl committed
25
    :   statement*
26
27
    ;

28

29
statement
Sarah Grebing's avatar
Sarah Grebing committed
30
    :   //scriptDecl
Alexander Weigl's avatar
Alexander Weigl committed
31
32
33
34
35
      assignment
    | casesStmt
    | scriptCommand
    | unconditionalBlock
    | conditionalBlock
Alexander Weigl's avatar
Alexander Weigl committed
36
    | letStmt
Alexander Weigl's avatar
Alexander Weigl committed
37
  //  |   callStmt
38
39
    ;

Alexander Weigl's avatar
Alexander Weigl committed
40
41
42
43
44
45
46
47
letStmt
    :
        'bind' expression
        ( SEMICOLON
        | 'in' ( statement | INDENT stmtList DEDENT)
        )
    ;

Sarah Grebing's avatar
Sarah Grebing committed
48
49
50
51
52
/*scriptDecl
    :
    SCRIPT name=ID '(' signature=argList? ')' INDENT body=stmtList DEDENT
    ;
*/
53
assignment
54
55
    :   variable=ID COLON type=ID SEMICOLON
    |   variable=ID (COLON type=ID)? ASSIGN expression SEMICOLON
56
57
    ;

58

59
expression
Alexander Weigl's avatar
Alexander Weigl committed
60
    :
61
62
63
64
65
66
67
68
      expression MUL expression #exprMultiplication
    | <assoc=right> expression DIV expression #exprDivision
    | expression op=(PLUS|MINUS) expression #exprLineOperators
    | expression op=(LE|GE|LEQ|GEQ) expression #exprComparison
    | expression op=(NEQ|EQ) expression  #exprEquality
    | expression AND expression   #exprAnd
    | expression OR expression    #exprOr
    | expression IMP expression   #exprIMP
Alexander Weigl's avatar
Alexander Weigl committed
69
    //|   expression EQUIV expression already covered by EQ/NEQ
70
    | expression LBRACKET substExpressionList RBRACKET #exprSubst
Alexander Weigl's avatar
Alexander Weigl committed
71
    | ID LPAREN  (expression (',' expression)*)? RPAREN  #function
72
73
74
75
76
    | MINUS expression         #exprNegate
    | NOT expression           #exprNot
    | LPAREN expression RPAREN #exprParen
    | literals                 #exprLiterals
    | matchPattern             #exprMatch
Alexander Weigl's avatar
Alexander Weigl committed
77
78
;

79
80
81

substExpressionList
    :
82
83
    (scriptVar SUBST_TO expression
        (COMMA scriptVar SUBST_TO expression)*
84
    )?
85
86
    ;

Alexander Weigl's avatar
Alexander Weigl committed
87
88
89
90
91
92
93
94
95
96
97
98
99
100
literals :
        ID             #literalID
    |   DIGITS         #literalDigits
    |   TERM_LITERAL   #literalTerm
    |   STRING_LITERAL #literalString
    |   TRUE           #literalTrue
    |   FALSE          #literalFalse
;

/**
 * Example: <pre>
    match `f(x) ==>` using [x:term]

     </pre>*/
101
matchPattern
102
    :
Sarah Grebing's avatar
Sarah Grebing committed
103
       MATCH ((pattern=expression (USING LBRACKET argList RBRACKET)?)
104
       )
105
106
107
    ;

scriptVar
108
    :   QUESTION_MARK ID
109
110
    ;

111

112
casesStmt
Alexander Weigl's avatar
Alexander Weigl committed
113
114
    :   CASES INDENT
            casesList*
Sarah Grebing's avatar
Sarah Grebing committed
115
        (DEFAULT  COLON? INDENT?
Alexander Weigl's avatar
Alexander Weigl committed
116
            defList=stmtList
Sarah Grebing's avatar
Sarah Grebing committed
117
          DEDENT?)?
Alexander Weigl's avatar
Alexander Weigl committed
118
        DEDENT
119
120
121
    ;

casesList
122
    :    (TRY |
Sarah Grebing's avatar
Sarah Grebing committed
123
124
125
            (CASE (expression
                |(CLOSES  INDENT closesGuard=stmtList DEDENT)
                |(derivable=DERIVABLE derivableExpression=expression) ) ))
126
                COLON INDENT? body=stmtList DEDENT?
127
    ;
Sarah Grebing's avatar
Sarah Grebing committed
128

129
/*closesExpression
130
    : CLOSES  INDENT closesGuard=stmtList DEDENT
131
    ;*/
Sarah Grebing's avatar
Sarah Grebing committed
132

Alexander Weigl's avatar
Alexander Weigl committed
133
unconditionalBlock
Alexander Weigl's avatar
Alexander Weigl committed
134
    : (kind+=(FOREACH|THEONLY|STRICT|RELAX|REPEAT))+ INDENT stmtList DEDENT
135
136
    ;

Alexander Weigl's avatar
Alexander Weigl committed
137
conditionalBlock
Alexander Weigl's avatar
Alexander Weigl committed
138
    : kind=(IF|WHILE) LPAREN expression RPAREN INDENT stmtList DEDENT
139
140
    ;

Sarah Grebing's avatar
Sarah Grebing committed
141

142
scriptCommand
Alexander Weigl's avatar
Alexander Weigl committed
143
    :   cmd=ID parameters? SEMICOLON
144
145
    ;

Alexander Weigl's avatar
Alexander Weigl committed
146
parameters: parameter+;
147
parameter :  ((pname=ID EQ)? expr=expression);
Alexander Weigl's avatar
Alexander Weigl committed
148

Alexander Weigl's avatar
Alexander Weigl committed
149
/*
150
151
152
callStmt
    :   CALL scriptCommand SEMICOLON
    ;
Alexander Weigl's avatar
Alexander Weigl committed
153
*/
154
155

//LEXER Rules
156
WS : [ \t\n\r]+ -> channel(HIDDEN) ;
157
158

//comments, allowing nesting.
159
160
SINGLE_LINE_COMMENT : '//' ~[\r\n]* -> channel(HIDDEN);
MULTI_LINE_COMMENT  : '/*' (MULTI_LINE_COMMENT|.)*? '*/' -> channel(HIDDEN);
161

Alexander Weigl's avatar
Alexander Weigl committed
162
163
CASES: 'cases';
CASE: 'case';
Sarah Grebing's avatar
Sarah Grebing committed
164
165
TRY: 'try';
CLOSES: 'closes';
166
DERIVABLE : 'derivable';
Alexander Weigl's avatar
Alexander Weigl committed
167
168
169
170
171
172
DEFAULT: 'default';
ASSIGN : ':=';
LBRACKET: '[';
RBRACKET:']';
USING : 'using';
MATCH : 'match';
173
174
175
176
177
SCRIPT : 'script' ;
TRUE : 'true' ;
FALSE : 'false' ;
CALL : 'call' ;
REPEAT : 'repeat' ;
Alexander Weigl's avatar
Alexander Weigl committed
178
/*INT : 'int' ;
179
BOOL: 'bool' ;
Alexander Weigl's avatar
Alexander Weigl committed
180
TERMTYPE : 'term' ;*/
181
182
FOREACH : 'foreach' ;
THEONLY : 'theonly' ;
Alexander Weigl's avatar
Alexander Weigl committed
183
184
185
186
STRICT : 'strict' ;
RELAX : 'relax';
IF:'if';
WHILE:'while';
187
188
189
190
INDENT : '{' ;
DEDENT : '}' ;
SEMICOLON : ';' ;
COLON : ':' ;
191
SUBST_TO: '\\';
Alexander Weigl's avatar
Alexander Weigl committed
192
193
194
195
196
197
198
199
200

STRING_LITERAL
   : '\'' ('\'\'' | ~ ('\''))* '\''
   ;

TERM_LITERAL
   : '`' ~('`')* '`'
   ;

201
202
203
204
205
206
207
208
209
210
211
212
PLUS : '+' ;
MINUS : '-' ;
MUL : '*' ;
DIV : '/' ;
EQ : '=' ;
NEQ : '!=' ;
GEQ : '>=' ;
LEQ : '<=' ;
GE : '>' ;
LE : '<' ;
AND : '&' ;
OR: '|' ;
213
IMP : '==>' ;
214
EQUIV : '<=>' ;
Alexander Weigl's avatar
Alexander Weigl committed
215
NOT: 'not';
216
217
218
COMMA: ',';
LPAREN: '(';
RPAREN: ')';
Alexander Weigl's avatar
Alexander Weigl committed
219
EXE_MARKER: '\u2316' -> channel(HIDDEN);
220
QUESTION_MARK: '?';
Alexander Weigl's avatar
Alexander Weigl committed
221

222
223
DIGITS : DIGIT+ ;
fragment DIGIT : [0-9] ;
Sarah Grebing's avatar
Sarah Grebing committed
224
ID : ([a-zA-Z]|'#'|'_') ([_a-zA-Z0-9] | '.' | '\\'| '#'|'<'|'>')*;