ScriptLanguage.g4 4 KB
Newer Older
1
grammar ScriptLanguage;
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
30
    :   //scriptDecl
31
         assignment
32 33 34 35 36
    |   repeatStmt
    |   casesStmt
    |   forEachStmt
    |   theOnlyStmt
    |   scriptCommand
Alexander Weigl's avatar
Alexander Weigl committed
37
  //  |   callStmt
38 39
    ;

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

expression
Alexander Weigl's avatar
Alexander Weigl committed
51
    :
52 53 54 55 56 57 58 59
      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
60
    //|   expression EQUIV expression already covered by EQ/NEQ
61
    | expression LBRACKET substExpressionList RBRACKET #exprSubst
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 105 106 107 108
repeatStmt
    :   REPEAT INDENT stmtList DEDENT
    ;

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

casesList
118 119
    :    (TRY |
            (CASE  (expression
120
                | (CLOSES  INDENT closesGuard=stmtList DEDENT) ) ) )
121
                COLON INDENT? body=stmtList DEDENT?
122
    ;
123

124
/*closesExpression
125
    : CLOSES  INDENT closesGuard=stmtList DEDENT
126
    ;*/
127

128 129 130 131 132 133 134 135
forEachStmt
    :   FOREACH INDENT stmtList DEDENT
    ;

theOnlyStmt
    :   THEONLY INDENT stmtList DEDENT
    ;

136

137
scriptCommand
Alexander Weigl's avatar
Alexander Weigl committed
138
    :   cmd=ID parameters? SEMICOLON
139 140
    ;

Alexander Weigl's avatar
Alexander Weigl committed
141
parameters: parameter+;
142
parameter :  ((pname=ID EQ)? expr=expression);
143

Alexander Weigl's avatar
Alexander Weigl committed
144
/*
145 146 147
callStmt
    :   CALL scriptCommand SEMICOLON
    ;
Alexander Weigl's avatar
Alexander Weigl committed
148
*/
149 150

//LEXER Rules
151
WS : [ \t\n\r]+ -> channel(HIDDEN) ;
152 153

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

Alexander Weigl's avatar
Alexander Weigl committed
157 158
CASES: 'cases';
CASE: 'case';
159 160
TRY: 'try';
CLOSES: 'closes';
161
DERIVABLE : 'derivable';
Alexander Weigl's avatar
Alexander Weigl committed
162 163 164 165 166 167
DEFAULT: 'default';
ASSIGN : ':=';
LBRACKET: '[';
RBRACKET:']';
USING : 'using';
MATCH : 'match';
168 169 170 171 172
SCRIPT : 'script' ;
TRUE : 'true' ;
FALSE : 'false' ;
CALL : 'call' ;
REPEAT : 'repeat' ;
Alexander Weigl's avatar
Alexander Weigl committed
173
/*INT : 'int' ;
174
BOOL: 'bool' ;
Alexander Weigl's avatar
Alexander Weigl committed
175
TERMTYPE : 'term' ;*/
176 177 178 179 180 181
FOREACH : 'foreach' ;
THEONLY : 'theonly' ;
INDENT : '{' ;
DEDENT : '}' ;
SEMICOLON : ';' ;
COLON : ':' ;
182
HEAPSIMP:'heap-simp';
183
SUBST_TO: '\\';
Alexander Weigl's avatar
Alexander Weigl committed
184

185

Alexander Weigl's avatar
Alexander Weigl committed
186 187 188 189 190 191 192 193
STRING_LITERAL
   : '\'' ('\'\'' | ~ ('\''))* '\''
   ;

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

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

215 216
DIGITS : DIGIT+ ;
fragment DIGIT : [0-9] ;
217
ID : ([a-zA-Z]|'#') ([_a-zA-Z0-9] | '.' | '\\'| '#')*;