ScriptLanguage.g4 3.45 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
Alexander Weigl's avatar
Alexander Weigl committed
50 51 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
    |   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>*/
78
matchPattern
79 80
    :   MATCH (pattern=expression
        (USING LBRACKET argList RBRACKET)?) | DERIVABLE TERM_LITERAL | IFCLOSED
81 82 83 84 85 86 87 88 89 90 91
    ;

scriptVar
    :   '?' ID
    ;

repeatStmt
    :   REPEAT INDENT stmtList DEDENT
    ;

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

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

forEachStmt
    :   FOREACH INDENT stmtList DEDENT
    ;

theOnlyStmt
    :   THEONLY INDENT stmtList DEDENT
    ;

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

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

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

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

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

Alexander Weigl's avatar
Alexander Weigl committed
132 133 134 135 136 137 138 139
CASES: 'cases';
CASE: 'case';
DEFAULT: 'default';
ASSIGN : ':=';
LBRACKET: '[';
RBRACKET:']';
USING : 'using';
MATCH : 'match';
140 141 142 143 144
SCRIPT : 'script' ;
TRUE : 'true' ;
FALSE : 'false' ;
CALL : 'call' ;
REPEAT : 'repeat' ;
Alexander Weigl's avatar
Alexander Weigl committed
145
/*INT : 'int' ;
146
BOOL: 'bool' ;
Alexander Weigl's avatar
Alexander Weigl committed
147
TERMTYPE : 'term' ;*/
148 149
FOREACH : 'foreach' ;
THEONLY : 'theonly' ;
150
ISCLOSED: 'isClosable';
Sarah Grebing's avatar
merge  
Sarah Grebing committed
151
DERIVABLE : 'derivable';
152 153 154 155
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] ;
185
ID : [a-zA-Z] ([_a-zA-Z0-9] | '.' | '\\' | '[]')* ;