Skip to content

Commit

Permalink
nitcc: add a Smalltalk grammar as example
Browse files Browse the repository at this point in the history
Signed-off-by: Jean Privat <[email protected]>
  • Loading branch information
privat committed Jul 22, 2024
1 parent e246df4 commit 2e1f675
Show file tree
Hide file tree
Showing 3 changed files with 403 additions and 0 deletions.
119 changes: 119 additions & 0 deletions contrib/nitcc/examples/smalltalk.sablecc
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
Grammar smalltalk;

Lexer

id = letter (letter|digit)* ;
letter = 'a'..'z' | 'A'..'Z' ;
digit = '0'..'9' ;
symbol = '#'(id|':')+ ;
string = quote (Any - quote | quote quote)* quote ;
quote = '\'' ;
comment = '"' (Any - '"' | '""')* '"' ;
char = '$' Any ;

binopb = binsym+ ;
binsym = '+' | '-' | '*' | '/' | '\\' | '>' | '<' | '=' | ',' | '@' | '!' | '%' | '^' | '&' | '|' | '?';

kwop = id ':' ;

num =
digit+ ('.' digit+)? (('e'|'E') ('+'|'-')? digit+)? |
digit+ 'r' (letter|digit)+ ;

blank = #9 | #10 | #13 | #32;

Parser
Ignored blank, comment;

method
= pattern pragma* temps? exprs?
;

pattern
= id
| binop id
| (kwop id)+
;

binop
= binopb | '<' | '>' | '|' | '-';

pragma
= '<' pragma_message '>'
;

pragma_message
= id
| binop expr6
| (kwop expr6)+
;

temps
= '|' id* '|'
;

exprs
= expr_list '.'?
;

expr_list
= expr_list '.' expr
| expr
;

expr
= id ':=' expr1
| '^' expr1
| expr1
;

expr1
= expr2 (';' message)+
| expr2
;

message
= id
| binop expr4
| (kwop expr3)+
;

expr2
= expr3 (kwop expr3)+
| expr3
;

expr3
= expr3 binop expr4
| expr4
;

expr4
= expr4 id
| expr5
;

expr5
= expr6
| '(' expr ')'
| '[' blockargs? temps? exprs? ']'
| '{' exprs? '}'
| '#(' expr6* ')'
| '#[' expr6* ']'
;

expr6
= id
| '-'? num
| char
| symbol
| string
;

blockargs
= blockarg '|'
;

blockarg
= ':' id
;
263 changes: 263 additions & 0 deletions contrib/nitcc/tests/sav/smalltalk.input.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,263 @@
Start
method
pattern_2
Nodes[N_group0]
_group0
kwop@(1:1-1:19)='exampleWithNumber:'
id@(1:20-1:21)='x'
Nodes[Npragma]
pragma
'<'@(3:3-3:4)
pragma_message_2
Nodes[N_group1]
_group1
kwop@(3:4-3:13)='syntaxOn:'
expr6_3
symbol@(3:14-3:23)='#postcard'
'>'@(3:23-3:24)
temps
'|'@(5:3-5:4)
Nodes[Nid]
id@(5:5-5:6)='y'
'|'@(5:7-5:8)
exprs
expr_list_0
expr_list_0
expr_list_0
expr_list_0
expr_list_1
expr_2
expr1_1
expr2_0
expr3_0
expr3_0
expr3_1
expr4_1
expr5_0
expr6_0
id@(7:3-7:7)='true'
binop_0
binopb@(7:8-7:9)='&'
expr4_0
expr4_1
expr5_0
expr6_0
id@(7:10-7:15)='false'
id@(7:16-7:19)='not'
binop_0
binopb@(7:20-7:21)='&'
expr4_1
expr5_1
'('@(7:22-7:23)
expr_2
expr1_1
expr2_1
expr3_1
expr4_0
expr4_1
expr5_0
expr6_0
id@(7:23-7:26)='nil'
id@(7:27-7:32)='isNil'
')'@(7:32-7:33)
Nodes[N_group3]
_group3
kwop@(8:5-8:13)='ifFalse:'
expr3_1
expr4_1
expr5_2
'['@(8:14-8:15)
exprs
expr_list_1
expr_2
expr1_1
expr2_0
expr3_1
expr4_1
expr5_0
expr6_0
id@(8:16-8:20)='self'
Nodes[N_group3]
_group3
kwop@(8:21-8:29)='perform:'
expr3_1
expr4_1
expr5_0
expr6_3
symbol@(8:30-8:35)='#add:'
_group3
kwop@(8:36-8:41)='with:'
expr3_1
expr4_1
expr5_0
expr6_0
id@(8:42-8:43)='x'
']'@(8:44-8:45)
'.'@(8:45-8:46)
expr_0
id@(10:3-10:4)='y'
':='@(10:5-10:7)
expr1_1
expr2_1
expr3_0
expr3_1
expr4_0
expr4_0
expr4_1
expr5_0
expr6_0
id@(10:8-10:19)='thisContext'
id@(10:20-10:25)='stack'
id@(10:26-10:30)='size'
binop_0
binopb@(10:31-10:32)='+'
expr4_0
expr4_1
expr5_0
expr6_0
id@(10:33-10:38)='super'
id@(10:39-10:43)='size'
'.'@(10:43-10:44)
expr_0
id@(12:3-12:12)='byteArray'
':='@(12:13-12:15)
expr1_1
expr2_1
expr3_1
expr4_1
expr5_5
'#['@(12:16-12:18)
Nodes[Nexpr6]
expr6_1
num@(12:19-12:20)='2'
expr6_1
num@(12:21-12:26)='2r100'
expr6_1
num@(12:27-12:31)='8r20'
expr6_1
num@(12:32-12:37)='16rFF'
']'@(12:38-12:39)
'.'@(12:39-12:40)
expr_2
expr1_1
expr2_0
expr3_1
expr4_1
expr5_3
'{'@(14:3-14:4)
exprs
expr_list_0
expr_list_1
expr_2
expr1_1
expr2_1
expr3_1
expr4_1
expr5_0
expr6_1
'-'@(14:5-14:6)
num@(14:6-14:8)='42'
'.'@(14:9-14:10)
expr_2
expr1_1
expr2_1
expr3_1
expr4_1
expr5_4
'#('@(14:11-14:13)
Nodes[Nexpr6]
expr6_2
char@(14:13-14:15)='$a'
expr6_3
symbol@(14:16-14:18)='#a'
expr6_4
string@(14:19-14:22)='\'a\''
expr6_1
num@(14:23-14:24)='1'
expr6_1
num@(14:25-14:28)='1.0'
')'@(14:28-14:29)
'}'@(14:30-14:31)
Nodes[N_group3]
_group3
kwop@(15:5-15:8)='do:'
expr3_1
expr4_1
expr5_2
'['@(15:9-15:10)
blockargs
blockarg
':'@(15:11-15:12)
id@(15:12-15:16)='each'
'|'@(15:17-15:18)
temps
'|'@(16:7-16:8)
Nodes[Nid]
id@(16:9-16:12)='var'
'|'@(16:13-16:14)
exprs
expr_list_1
expr_2
expr1_0
expr2_0
expr3_1
expr4_1
expr5_0
expr6_0
id@(17:7-17:17)='Transcript'
Nodes[N_group3]
_group3
kwop@(18:9-18:14)='show:'
expr3_1
expr4_0
expr4_0
expr4_1
expr5_0
expr6_0
id@(18:15-18:19)='each'
id@(18:20-18:25)='class'
id@(18:26-18:30)='name'
Nodes[N_group2]
_group2
';'@(18:30-18:31)
message_2
Nodes[N_group3]
_group3
kwop@(19:9-19:14)='show:'
expr3_1
expr4_0
expr4_1
expr5_0
expr6_0
id@(19:15-19:19)='each'
id@(19:20-19:31)='printString'
_group2
';'@(19:31-19:32)
message_2
Nodes[N_group3]
_group3
kwop@(20:9-20:14)='show:'
expr3_1
expr4_1
expr5_0
expr6_4
string@(20:15-20:18)='\' \''
']'@(20:19-20:20)
'.'@(20:20-20:21)
expr_1
'^'@(21:3-21:4)
expr1_1
expr2_1
expr3_0
expr3_1
expr4_1
expr5_0
expr6_0
id@(21:5-21:6)='x'
binop_1
'<'@(21:7-21:8)
expr4_1
expr5_0
expr6_0
id@(21:9-21:10)='y'
Eof@(22:1-22:1)=''
Loading

0 comments on commit 2e1f675

Please sign in to comment.