ScriptLanguage.g4 4.19 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
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
    ;

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

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
60
    | expression USING LBRACKET argList RBRACKET #namespaceset
Alexander Weigl's avatar
Alexander Weigl committed
61
    //|   expression EQUIV expression already covered by EQ/NEQ
62
    | expression LBRACKET substExpressionList RBRACKET #exprSubst
Alexander Weigl's avatar
Alexander Weigl committed
63
    | ID LPAREN  (expression (',' expression)*)? RPAREN  #function
64 65 66 67 68
    | MINUS expression         #exprNegate
    | NOT expression           #exprNot
    | LPAREN expression RPAREN #exprParen
    | literals                 #exprLiterals
    | matchPattern             #exprMatch
Alexander Weigl's avatar
Alexander Weigl committed
69 70
;

71 72 73

substExpressionList
    :
74 75
    (scriptVar SUBST_TO expression
        (COMMA scriptVar SUBST_TO expression)*
76
    )?
77 78
    ;

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

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

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

scriptVar
102
    :   QUESTION_MARK ID
103 104
    ;

105

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

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

122
/*closesExpression
123
    : CLOSES  INDENT closesGuard=stmtList DEDENT
124
    ;*/
125

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

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

134

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

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

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

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

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

Alexander Weigl's avatar
Alexander Weigl committed
155 156
CASES: 'cases';
CASE: 'case';
157 158
TRY: 'try';
CLOSES: 'closes';
159
DERIVABLE : 'derivable';
Alexander Weigl's avatar
Alexander Weigl committed
160 161 162 163 164 165
DEFAULT: 'default';
ASSIGN : ':=';
LBRACKET: '[';
RBRACKET:']';
USING : 'using';
MATCH : 'match';
166 167 168 169 170
SCRIPT : 'script' ;
TRUE : 'true' ;
FALSE : 'false' ;
CALL : 'call' ;
REPEAT : 'repeat' ;
Alexander Weigl's avatar
Alexander Weigl committed
171
/*INT : 'int' ;
172
BOOL: 'bool' ;
Alexander Weigl's avatar
Alexander Weigl committed
173
TERMTYPE : 'term' ;*/
174 175
FOREACH : 'foreach' ;
THEONLY : 'theonly' ;
Alexander Weigl's avatar
Alexander Weigl committed
176 177 178 179
STRICT : 'strict' ;
RELAX : 'relax';
IF:'if';
WHILE:'while';
180 181 182 183
INDENT : '{' ;
DEDENT : '}' ;
SEMICOLON : ';' ;
COLON : ':' ;
184
SUBST_TO: '\\';
Alexander Weigl's avatar
Alexander Weigl committed
185 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] ;
Sarah Grebing's avatar
Sarah Grebing committed
217
ID : ([a-zA-Z]|'#'|'_') ([_a-zA-Z0-9] | '.' | '\\'| '#'|'<'|'>')*;