ScriptLanguage.g4 3.46 KB
Newer Older
1
grammar ScriptLanguage;
Sarah Grebing's avatar
Sarah Grebing committed
2
//TODO There is still sth. missing:import statements at the beginning to allow scripts from other files
3

Sarah Grebing's avatar
Sarah Grebing committed
4 5 6 7
/*start
    : stmtList
    ;
*/
8

9
start
10
    :   (script)*
11 12
    ;

13
script: SCRIPT name=ID '(' signature=argList? ')' INDENT body=stmtList DEDENT;
14
argList
Alexander Weigl's avatar
Alexander Weigl committed
15
    :   varDecl (',' varDecl)*
16 17 18
    ;

varDecl
Alexander Weigl's avatar
Alexander Weigl committed
19
    :   name=ID ':' type=ID
20 21 22
    ;

stmtList
Alexander Weigl's avatar
Alexander Weigl committed
23
    :   statement*
24 25
    ;

26

27
statement
Sarah Grebing's avatar
Sarah Grebing committed
28
    :   //scriptDecl
29
         assignment
30 31 32 33 34
    |   repeatStmt
    |   casesStmt
    |   forEachStmt
    |   theOnlyStmt
    |   scriptCommand
Alexander Weigl's avatar
Alexander Weigl committed
35
  //  |   callStmt
36 37
    ;

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

expression
Alexander Weigl's avatar
Alexander Weigl committed
49
    :
Alexander Weigl's avatar
Alexander Weigl committed
50 51
        MINUS expression   #exprNegate
    |   NOT expression  #exprNot
Alexander Weigl's avatar
Alexander Weigl committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
    |   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
;

literals :
        ID             #literalID
    |   DIGITS         #literalDigits
    |   TERM_LITERAL   #literalTerm
    |   STRING_LITERAL #literalString
    |   TRUE           #literalTrue
    |   FALSE          #literalFalse
;

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

     </pre>*/
80
matchPattern
Sarah Grebing's avatar
Sarah Grebing committed
81
    :   MATCH pattern=expression
Alexander Weigl's avatar
Alexander Weigl committed
82
        (USING LBRACKET argList RBRACKET)?
83 84 85 86 87 88 89 90 91 92 93
    ;

scriptVar
    :   '?' ID
    ;

repeatStmt
    :   REPEAT INDENT stmtList DEDENT
    ;

casesStmt
Alexander Weigl's avatar
Alexander Weigl committed
94 95 96 97 98 99
    :   CASES INDENT
            casesList*
        (DEFAULT  COLON? INDENT
            defList=stmtList
          DEDENT)?
        DEDENT
100 101 102
    ;

casesList
Alexander Weigl's avatar
Alexander Weigl committed
103
    :   CASE expression COLON? INDENT stmtList DEDENT
104 105 106 107 108 109 110 111 112 113 114
    ;

forEachStmt
    :   FOREACH INDENT stmtList DEDENT
    ;

theOnlyStmt
    :   THEONLY INDENT stmtList DEDENT
    ;

scriptCommand
Alexander Weigl's avatar
Alexander Weigl committed
115
    :   cmd=ID parameters? SEMICOLON
116 117
    ;

Alexander Weigl's avatar
Alexander Weigl committed
118 119
parameters: parameter+;
parameter :  ((pname=ID '=')? expr=expression);
Alexander Weigl's avatar
Alexander Weigl committed
120

Alexander Weigl's avatar
Alexander Weigl committed
121
/*
122 123 124
callStmt
    :   CALL scriptCommand SEMICOLON
    ;
Alexander Weigl's avatar
Alexander Weigl committed
125
*/
126 127

//LEXER Rules
128
WS : [ \t\n\r]+ -> channel(HIDDEN) ;
129 130

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

Alexander Weigl's avatar
Alexander Weigl committed
134 135 136 137 138 139 140 141
CASES: 'cases';
CASE: 'case';
DEFAULT: 'default';
ASSIGN : ':=';
LBRACKET: '[';
RBRACKET:']';
USING : 'using';
MATCH : 'match';
142 143 144 145 146
SCRIPT : 'script' ;
TRUE : 'true' ;
FALSE : 'false' ;
CALL : 'call' ;
REPEAT : 'repeat' ;
Alexander Weigl's avatar
Alexander Weigl committed
147
/*INT : 'int' ;
148
BOOL: 'bool' ;
Alexander Weigl's avatar
Alexander Weigl committed
149
TERMTYPE : 'term' ;*/
150 151 152 153 154 155
FOREACH : 'foreach' ;
THEONLY : 'theonly' ;
INDENT : '{' ;
DEDENT : '}' ;
SEMICOLON : ';' ;
COLON : ':' ;
Alexander Weigl's avatar
Alexander Weigl committed
156 157 158 159 160 161 162 163 164

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

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

165 166 167 168 169 170 171 172 173 174 175 176
PLUS : '+' ;
MINUS : '-' ;
MUL : '*' ;
DIV : '/' ;
EQ : '=' ;
NEQ : '!=' ;
GEQ : '>=' ;
LEQ : '<=' ;
GE : '>' ;
LE : '<' ;
AND : '&' ;
OR: '|' ;
177
IMP : '==>' ;
178
EQUIV : '<=>' ;
Alexander Weigl's avatar
Alexander Weigl committed
179
NOT: 'not';
180

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

183 184
DIGITS : DIGIT+ ;
fragment DIGIT : [0-9] ;
Alexander Weigl's avatar
Alexander Weigl committed
185
ID : [a-zA-Z] ([_a-zA-Z0-9] | '.' | '\\' )* ;