Skip to content

Commit

Permalink
grammar formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
teverett committed Jun 8, 2019
1 parent 4bee2ea commit 9c46498
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 128 deletions.
87 changes: 15 additions & 72 deletions cool/COOL.g4
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ COOL grammar derived from:
http://sist.shanghaitech.edu.cn/faculty/songfu/course/spring2017/cs131/COOL/COOLAid.pdf
*/

grammar COOL;

program
Expand All @@ -50,16 +49,17 @@ feature
formal
: OBJECTID ':' TYPEID
;

/* method argument */


expression
: expression ('@' TYPEID)? '.' OBJECTID '(' (expression (',' expression)*)* ')' # methodCall
| OBJECTID '(' (expression (',' expression)*)* ')' # ownMethodCall
| IF expression THEN expression ELSE expression FI # if
| WHILE expression LOOP expression POOL # while
| '{' (expression ';') + '}' # block
| '{' (expression ';')+ '}' # block
| LET OBJECTID ':' TYPEID (ASSIGNMENT expression)? (',' OBJECTID ':' TYPEID (ASSIGNMENT expression)?)* IN expression # letIn
| CASE expression OF (OBJECTID ':' TYPEID CASE_ARROW expression ';') + ESAC # case
| CASE expression OF (OBJECTID ':' TYPEID CASE_ARROW expression ';')+ ESAC # case
| NEW TYPEID # new
| INTEGER_NEGATIVE expression # negative
| ISVOID expression # isvoid
Expand All @@ -79,297 +79,240 @@ expression
| FALSE # false
| OBJECTID ASSIGNMENT expression # assignment
;

// key words

// key words

CLASS
: C L A S S
;


ELSE
: E L S E
;


FALSE
: 'f' A L S E
;


FI
: F I
;


IF
: I F
;


IN
: I N
;


INHERITS
: I N H E R I T S
;


ISVOID
: I S V O I D
;


LET
: L E T
;


LOOP
: L O O P
;


POOL
: P O O L
;


THEN
: T H E N
;


WHILE
: W H I L E
;


CASE
: C A S E
;


ESAC
: E S A C
;


NEW
: N E W
;


OF
: O F
;


NOT
: N O T
;


TRUE
: 't' R U E
;

// primitives

// primitives

STRING
: '"' (ESC | ~ ["\\])* '"'
;
INT
: [0-9] +
: [0-9]+
;
TYPEID
: [A-Z] [_0-9A-Za-z]*
;
OBJECTID
: [a-z] [_0-9A-Za-z]*
;
ASSIGNMENT
: '<-'
;
CASE_ARROW
: '=>'
;
ADD
: '+'
;
MINUS
: '-'
;
MULTIPLY
: '*'
;
DIVISION
: '/'
;
LESS_THAN
: '<'
;
LESS_EQUAL
: '<='
;
EQUAL
: '='
;
INTEGER_NEGATIVE
: '~'
;
fragment A
: [aA]
;
fragment C
: [cC]
;
fragment D
: [dD]
;
fragment E
: [eE]
;
fragment F
: [fF]
;
fragment H
: [hH]
;
fragment I
: [iI]
;
fragment L
: [lL]
;
fragment N
: [nN]
;
fragment O
: [oO]
;
fragment P
: [pP]
;
fragment R
: [rR]
;
fragment S
: [sS]
;
fragment T
: [tT]
;
fragment U
: [uU]
;
fragment V
: [vV]
;
fragment W
: [wW]
;
fragment ESC
: '\\' (["\\/bfnrt] | UNICODE)
;
fragment UNICODE
: 'u' HEX HEX HEX HEX
;
fragment HEX
: [0-9a-fA-F]
;
// comments
// comments
OPEN_COMMENT
: '(*'
;
CLOSE_COMMENT
: '*)'
;
COMMENT
: OPEN_COMMENT (COMMENT | .)*? CLOSE_COMMENT -> skip
;
ONE_LINE_COMMENT
: '--' (~ '\n')* '\n'? -> skip
;
// skip spaces, tabs, newlines, note that \v is not suppoted in antlr
// skip spaces, tabs, newlines, note that \v is not suppoted in antlr
WHITESPACE
: [ \t\r\n\f] + -> skip
: [ \t\r\n\f]+ -> skip
;
Loading

0 comments on commit 9c46498

Please sign in to comment.