ScriptLanguage.g4 3.33 KB
Newer Older
1
grammar ScriptLanguage;
Sarah Grebing's avatar
Sarah Grebing committed
2
//TODO There is still sth. missing:import statements at the beginning to allow scripts from other files
3

Sarah Grebing's avatar
Sarah Grebing committed
4
5
6
7
/*start
    : stmtList
    ;
*/
8
start
Alexander Weigl's avatar
Alexander Weigl committed
9
    :   (SCRIPT name=ID '(' signature=argList? ')' INDENT body=stmtList DEDENT)*
10
11
12
    ;

argList
Alexander Weigl's avatar
Alexander Weigl committed
13
    :   varDecl (',' varDecl)*
14
15
16
    ;

varDecl
Alexander Weigl's avatar
Alexander Weigl committed
17
    :   name=ID ':' type=ID
18
19
20
    ;

stmtList
Alexander Weigl's avatar
Alexander Weigl committed
21
    :   statement*
22
23
24
    ;

statement
Sarah Grebing's avatar
Sarah Grebing committed
25
26
27
    :   //scriptDecl
       varDecl
    |   assignment
28
29
30
31
32
    |   repeatStmt
    |   casesStmt
    |   forEachStmt
    |   theOnlyStmt
    |   scriptCommand
Alexander Weigl's avatar
Alexander Weigl committed
33
  //  |   callStmt
34
35
    ;

Sarah Grebing's avatar
Sarah Grebing committed
36
37
38
39
40
/*scriptDecl
    :
    SCRIPT name=ID '(' signature=argList? ')' INDENT body=stmtList DEDENT
    ;
*/
41
assignment
Alexander Weigl's avatar
Alexander Weigl committed
42
    :   variable=ID (COLON type=ID)? ASSIGN expression SEMICOLON
43
44
45
    ;

expression
Alexander Weigl's avatar
Alexander Weigl committed
46
    :
Alexander Weigl's avatar
Alexander Weigl committed
47
48
        MINUS expression   #exprNegate
    |   NOT expression  #exprNot
Alexander Weigl's avatar
Alexander Weigl committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
    |   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
    //|   expression EQUIV expression already covered by EQ/NEQ
    |   '(' expression ')' #exprParen
    | literals             #exprLiterals
    | matchPattern         #exprMatch
;

literals :
        ID             #literalID
    |   DIGITS         #literalDigits
    |   TERM_LITERAL   #literalTerm
    |   STRING_LITERAL #literalString
    |   TRUE           #literalTrue
    |   FALSE          #literalFalse
;

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

     </pre>*/
77
matchPattern
Alexander Weigl's avatar
Alexander Weigl committed
78
    :   MATCH pattern=expression
Alexander Weigl's avatar
Alexander Weigl committed
79
        (USING LBRACKET argList RBRACKET)?
80
81
82
83
84
85
86
87
88
89
90
    ;

scriptVar
    :   '?' ID
    ;

repeatStmt
    :   REPEAT INDENT stmtList DEDENT
    ;

casesStmt
Alexander Weigl's avatar
Alexander Weigl committed
91
92
93
94
95
96
    :   CASES INDENT
            casesList*
        (DEFAULT  COLON? INDENT
            defList=stmtList
          DEDENT)?
        DEDENT
97
98
99
    ;

casesList
Alexander Weigl's avatar
Alexander Weigl committed
100
    :   CASE expression COLON? INDENT stmtList DEDENT
101
102
103
104
105
106
107
108
109
110
111
    ;

forEachStmt
    :   FOREACH INDENT stmtList DEDENT
    ;

theOnlyStmt
    :   THEONLY INDENT stmtList DEDENT
    ;

scriptCommand
Alexander Weigl's avatar
Alexander Weigl committed
112
    :   cmd=ID parameters? SEMICOLON
113
114
    ;

Alexander Weigl's avatar
Alexander Weigl committed
115
116
parameters: parameter+;
parameter :  ((pname=ID '=')? expr=expression);
Alexander Weigl's avatar
Alexander Weigl committed
117

Alexander Weigl's avatar
Alexander Weigl committed
118
/*
119
120
121
callStmt
    :   CALL scriptCommand SEMICOLON
    ;
Alexander Weigl's avatar
Alexander Weigl committed
122
*/
123
124
125

//LEXER Rules
WS : [ \t\n\r]+ -> skip ;
126
127
128
129
130

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

Alexander Weigl's avatar
Alexander Weigl committed
131
132
133
134
135
136
137
138
CASES: 'cases';
CASE: 'case';
DEFAULT: 'default';
ASSIGN : ':=';
LBRACKET: '[';
RBRACKET:']';
USING : 'using';
MATCH : 'match';
139
140
141
142
143
SCRIPT : 'script' ;
TRUE : 'true' ;
FALSE : 'false' ;
CALL : 'call' ;
REPEAT : 'repeat' ;
Alexander Weigl's avatar
Alexander Weigl committed
144
/*INT : 'int' ;
145
BOOL: 'bool' ;
Alexander Weigl's avatar
Alexander Weigl committed
146
TERMTYPE : 'term' ;*/
147
148
149
150
151
152
FOREACH : 'foreach' ;
THEONLY : 'theonly' ;
INDENT : '{' ;
DEDENT : '}' ;
SEMICOLON : ';' ;
COLON : ':' ;
Alexander Weigl's avatar
Alexander Weigl committed
153
154
155
156
157
158
159
160
161

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

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

162
163
164
165
166
167
168
169
170
171
172
173
PLUS : '+' ;
MINUS : '-' ;
MUL : '*' ;
DIV : '/' ;
EQ : '=' ;
NEQ : '!=' ;
GEQ : '>=' ;
LEQ : '<=' ;
GE : '>' ;
LE : '<' ;
AND : '&' ;
OR: '|' ;
174
IMP : '==>' ;
175
EQUIV : '<=>' ;
Alexander Weigl's avatar
Alexander Weigl committed
176
NOT: 'not';
177

178
179
180
DIGITS : DIGIT+ ;
fragment DIGIT : [0-9] ;
ID : [a-zA-Z] [_a-zA-Z0-9]* ;