ScriptLanguage.g4 4.12 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
  //  |   callStmt
37 38
    ;

Sarah Grebing's avatar
Sarah Grebing committed
39 40 41 42 43
/*scriptDecl
    :
    SCRIPT name=ID '(' signature=argList? ')' INDENT body=stmtList DEDENT
    ;
*/
44
assignment
45 46
    :   variable=ID COLON type=ID SEMICOLON
    |   variable=ID (COLON type=ID)? ASSIGN expression SEMICOLON
47 48 49
    ;

expression
Alexander Weigl's avatar
Alexander Weigl committed
50
    :
51 52 53 54 55 56 57 58
      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
59
    //|   expression EQUIV expression already covered by EQ/NEQ
60
    | expression LBRACKET substExpressionList RBRACKET #exprSubst
Alexander Weigl's avatar
Alexander Weigl committed
61
    | ID LPAREN  (expression (',' expression)*)? RPAREN  #function
62 63 64 65 66
    | MINUS expression         #exprNegate
    | NOT expression           #exprNot
    | LPAREN expression RPAREN #exprParen
    | literals                 #exprLiterals
    | matchPattern             #exprMatch
Alexander Weigl's avatar
Alexander Weigl committed
67 68
;

69 70 71

substExpressionList
    :
72 73
    (scriptVar SUBST_TO expression
        (COMMA scriptVar SUBST_TO expression)*
74
    )?
75 76
    ;

Alexander Weigl's avatar
Alexander Weigl committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90
literals :
        ID             #literalID
    |   DIGITS         #literalDigits
    |   TERM_LITERAL   #literalTerm
    |   STRING_LITERAL #literalString
    |   TRUE           #literalTrue
    |   FALSE          #literalFalse
;

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

     </pre>*/
91
matchPattern
92 93 94 95 96
    :
       MATCH (
         derivable=DERIVABLE derivableExpression=expression
       | (pattern=expression (USING LBRACKET argList RBRACKET)?)
       )
97 98 99
    ;

scriptVar
100
    :   QUESTION_MARK ID
101 102
    ;

103

104
casesStmt
Alexander Weigl's avatar
Alexander Weigl committed
105 106
    :   CASES INDENT
            casesList*
Sarah Grebing's avatar
Sarah Grebing committed
107
        (DEFAULT  COLON? INDENT?
Alexander Weigl's avatar
Alexander Weigl committed
108
            defList=stmtList
Sarah Grebing's avatar
Sarah Grebing committed
109
          DEDENT?)?
Alexander Weigl's avatar
Alexander Weigl committed
110
        DEDENT
111 112 113
    ;

casesList
114 115
    :    (TRY |
            (CASE  (expression
116
                | (CLOSES  INDENT closesGuard=stmtList DEDENT) ) ) )
117
                COLON INDENT? body=stmtList DEDENT?
118
    ;
Sarah Grebing's avatar
Sarah Grebing committed
119

120
/*closesExpression
121
    : CLOSES  INDENT closesGuard=stmtList DEDENT
122
    ;*/
Sarah Grebing's avatar
Sarah Grebing committed
123

Alexander Weigl's avatar
Alexander Weigl committed
124
unconditionalBlock
Alexander Weigl's avatar
Alexander Weigl committed
125
    : (kind+=(FOREACH|THEONLY|STRICT|RELAX|REPEAT))+ INDENT stmtList DEDENT
126 127
    ;

Alexander Weigl's avatar
Alexander Weigl committed
128
conditionalBlock
Alexander Weigl's avatar
Alexander Weigl committed
129
    : kind=(IF|WHILE) LPAREN expression RPAREN INDENT stmtList DEDENT
130 131
    ;

Sarah Grebing's avatar
Sarah Grebing committed
132

133
scriptCommand
Alexander Weigl's avatar
Alexander Weigl committed
134
    :   cmd=ID parameters? SEMICOLON
135 136
    ;

Alexander Weigl's avatar
Alexander Weigl committed
137
parameters: parameter+;
138
parameter :  ((pname=ID EQ)? expr=expression);
Alexander Weigl's avatar
Alexander Weigl committed
139

Alexander Weigl's avatar
Alexander Weigl committed
140
/*
141 142 143
callStmt
    :   CALL scriptCommand SEMICOLON
    ;
Alexander Weigl's avatar
Alexander Weigl committed
144
*/
145 146

//LEXER Rules
147
WS : [ \t\n\r]+ -> channel(HIDDEN) ;
148 149

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

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

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

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

192 193 194 195 196 197 198 199 200 201 202 203
PLUS : '+' ;
MINUS : '-' ;
MUL : '*' ;
DIV : '/' ;
EQ : '=' ;
NEQ : '!=' ;
GEQ : '>=' ;
LEQ : '<=' ;
GE : '>' ;
LE : '<' ;
AND : '&' ;
OR: '|' ;
204
IMP : '==>' ;
205
EQUIV : '<=>' ;
Alexander Weigl's avatar
Alexander Weigl committed
206
NOT: 'not';
207 208 209
COMMA: ',';
LPAREN: '(';
RPAREN: ')';
Alexander Weigl's avatar
Alexander Weigl committed
210
EXE_MARKER: '\u2316' -> channel(HIDDEN);
211
QUESTION_MARK: '?';
Alexander Weigl's avatar
Alexander Weigl committed
212

213 214
DIGITS : DIGIT+ ;
fragment DIGIT : [0-9] ;
Sarah Grebing's avatar
Sarah Grebing committed
215
ID : ([a-zA-Z]|'#'|'_') ([_a-zA-Z0-9] | '.' | '\\'| '#')*;