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
script: SCRIPT name=ID '(' signature=argList? ')' INDENT body=stmtList DEDENT;
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

25
statement
Sarah Grebing's avatar
Sarah Grebing committed
26
    :   //scriptDecl
27
         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
42 43
    :   variable=ID COLON type=ID SEMICOLON
    |   variable=ID (COLON type=ID)? ASSIGN expression SEMICOLON
44 45 46
    ;

expression
Alexander Weigl's avatar
Alexander Weigl committed
47
    :
Alexander Weigl's avatar
Alexander Weigl committed
48 49
        MINUS expression   #exprNegate
    |   NOT expression  #exprNot
50
    |   expression '[' substExpressionList ']'  #exprSubst
Alexander Weigl's avatar
Alexander Weigl committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64
    |   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
;

65 66 67 68 69 70

substExpressionList
    :
    scriptVar '/' expression (',' substExpressionList)*
    ;

Alexander Weigl's avatar
Alexander Weigl committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84
literals :
        ID             #literalID
    |   DIGITS         #literalDigits
    |   TERM_LITERAL   #literalTerm
    |   STRING_LITERAL #literalString
    |   TRUE           #literalTrue
    |   FALSE          #literalFalse
;

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

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

scriptVar
    :   '?' ID
    ;

repeatStmt
    :   REPEAT INDENT stmtList DEDENT
    ;

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

casesList
111 112 113
  //  : simpleCase
   // | closableCase
    :   CASE (MATCH ISCLOSED | expression) COLON? INDENT stmtList DEDENT
114
    ;
115 116 117 118 119 120 121
/*simpleCase
    : CASE expression COLON? INDENT stmtList DEDENT
    ;
closableCase
    : CASE MATCH ISCLOSED COLON? INDENT stmtList DEDENT
    ;
*/
122 123 124 125 126 127 128 129 130
forEachStmt
    :   FOREACH INDENT stmtList DEDENT
    ;

theOnlyStmt
    :   THEONLY INDENT stmtList DEDENT
    ;

scriptCommand
Alexander Weigl's avatar
Alexander Weigl committed
131
    :   cmd=ID parameters? SEMICOLON
132 133
    ;

Alexander Weigl's avatar
Alexander Weigl committed
134 135
parameters: parameter+;
parameter :  ((pname=ID '=')? expr=expression);
Alexander Weigl's avatar
Alexander Weigl committed
136

Alexander Weigl's avatar
Alexander Weigl committed
137
/*
138 139 140
callStmt
    :   CALL scriptCommand SEMICOLON
    ;
Alexander Weigl's avatar
Alexander Weigl committed
141
*/
142 143

//LEXER Rules
144
WS : [ \t\n\r]+ -> channel(HIDDEN) ;
145 146

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

Alexander Weigl's avatar
Alexander Weigl committed
150 151
CASES: 'cases';
CASE: 'case';
152 153
ISCLOSED: 'isCloseable';
DERIVABLE : 'derivable';
Alexander Weigl's avatar
Alexander Weigl committed
154 155 156 157 158 159
DEFAULT: 'default';
ASSIGN : ':=';
LBRACKET: '[';
RBRACKET:']';
USING : 'using';
MATCH : 'match';
160 161 162 163 164
SCRIPT : 'script' ;
TRUE : 'true' ;
FALSE : 'false' ;
CALL : 'call' ;
REPEAT : 'repeat' ;
Alexander Weigl's avatar
Alexander Weigl committed
165
/*INT : 'int' ;
166
BOOL: 'bool' ;
Alexander Weigl's avatar
Alexander Weigl committed
167
TERMTYPE : 'term' ;*/
168 169 170 171 172 173
FOREACH : 'foreach' ;
THEONLY : 'theonly' ;
INDENT : '{' ;
DEDENT : '}' ;
SEMICOLON : ';' ;
COLON : ':' ;
Alexander Weigl's avatar
Alexander Weigl committed
174

175

Alexander Weigl's avatar
Alexander Weigl committed
176 177 178 179 180 181 182 183
STRING_LITERAL
   : '\'' ('\'\'' | ~ ('\''))* '\''
   ;

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

184 185 186 187 188 189 190 191 192 193 194 195
PLUS : '+' ;
MINUS : '-' ;
MUL : '*' ;
DIV : '/' ;
EQ : '=' ;
NEQ : '!=' ;
GEQ : '>=' ;
LEQ : '<=' ;
GE : '>' ;
LE : '<' ;
AND : '&' ;
OR: '|' ;
196
IMP : '==>' ;
197
EQUIV : '<=>' ;
Alexander Weigl's avatar
Alexander Weigl committed
198
NOT: 'not';
199

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

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