ScriptLanguage.g4 3.84 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 12 13 14 15
script
    : SCRIPT name=ID '(' signature=argList? ')' INDENT body=stmtList DEDENT
    ;


16
argList
Alexander Weigl's avatar
Alexander Weigl committed
17
    :   varDecl (',' varDecl)*
18 19 20
    ;

varDecl
Alexander Weigl's avatar
Alexander Weigl committed
21
    :   name=ID ':' 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
31
         assignment
32 33 34 35 36
    |   repeatStmt
    |   casesStmt
    |   forEachStmt
    |   theOnlyStmt
    |   scriptCommand
Alexander Weigl's avatar
Alexander Weigl committed
37
  //  |   callStmt
38 39
    ;

Sarah Grebing's avatar
Sarah Grebing committed
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 62 63 64
    | expression LBRACKET substExpressionList RBRACKET #exprSubst
    | MINUS expression   #exprNegate
    | NOT expression  #exprNot
    | '(' expression ')' #exprParen
Alexander Weigl's avatar
Alexander Weigl committed
65 66 67 68
    | literals             #exprLiterals
    | matchPattern         #exprMatch
;

69 70 71

substExpressionList
    :
72 73 74
    (scriptVar '/' expression
        (',' scriptVar '/' expression)*
    )?
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 100 101 102 103 104 105 106 107
    ;

scriptVar
    :   '?' ID
    ;

repeatStmt
    :   REPEAT INDENT stmtList DEDENT
    ;

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

casesList
117 118 119 120
    :    (TRY |
            (CASE  (expression
                | (CLOSES  INDENT closesScript=stmtList DEDENT) ) ) )
                COLON INDENT? body=stmtList DEDENT?
121
    ;
Sarah Grebing's avatar
Sarah Grebing committed
122

123
/*closesExpression
Sarah Grebing's avatar
Sarah Grebing committed
124
    : CLOSES  INDENT closesScript=stmtList DEDENT
125
    ;*/
Sarah Grebing's avatar
Sarah Grebing committed
126

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

theOnlyStmt
    :   THEONLY INDENT stmtList DEDENT
    ;

Sarah Grebing's avatar
Sarah Grebing committed
135

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

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

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

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

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

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

182

Alexander Weigl's avatar
Alexander Weigl committed
183 184 185 186 187 188 189 190
STRING_LITERAL
   : '\'' ('\'\'' | ~ ('\''))* '\''
   ;

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

191 192 193 194 195 196 197 198 199 200 201 202
PLUS : '+' ;
MINUS : '-' ;
MUL : '*' ;
DIV : '/' ;
EQ : '=' ;
NEQ : '!=' ;
GEQ : '>=' ;
LEQ : '<=' ;
GE : '>' ;
LE : '<' ;
AND : '&' ;
OR: '|' ;
203
IMP : '==>' ;
204
EQUIV : '<=>' ;
Alexander Weigl's avatar
Alexander Weigl committed
205
NOT: 'not';
206

Alexander Weigl's avatar
Alexander Weigl committed
207 208
EXE_MARKER: '\u2316' -> channel(HIDDEN);

209 210
DIGITS : DIGIT+ ;
fragment DIGIT : [0-9] ;
211
ID : [a-zA-Z] ([_a-zA-Z0-9] | '.' | '\\')*;