From 2e1f6750ccdc82742eaede7025be5a22f65e8927 Mon Sep 17 00:00:00 2001 From: Jean Privat Date: Mon, 22 Jul 2024 12:10:21 -0400 Subject: [PATCH] nitcc: add a Smalltalk grammar as example Signed-off-by: Jean Privat --- contrib/nitcc/examples/smalltalk.sablecc | 119 +++++++++ contrib/nitcc/tests/sav/smalltalk.input.res | 263 ++++++++++++++++++++ contrib/nitcc/tests/smalltalk.input | 21 ++ 3 files changed, 403 insertions(+) create mode 100644 contrib/nitcc/examples/smalltalk.sablecc create mode 100644 contrib/nitcc/tests/sav/smalltalk.input.res create mode 100644 contrib/nitcc/tests/smalltalk.input diff --git a/contrib/nitcc/examples/smalltalk.sablecc b/contrib/nitcc/examples/smalltalk.sablecc new file mode 100644 index 0000000000..5e3183ec31 --- /dev/null +++ b/contrib/nitcc/examples/smalltalk.sablecc @@ -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 + ; diff --git a/contrib/nitcc/tests/sav/smalltalk.input.res b/contrib/nitcc/tests/sav/smalltalk.input.res new file mode 100644 index 0000000000..c5a4ffd716 --- /dev/null +++ b/contrib/nitcc/tests/sav/smalltalk.input.res @@ -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)='' diff --git a/contrib/nitcc/tests/smalltalk.input b/contrib/nitcc/tests/smalltalk.input new file mode 100644 index 0000000000..a3cdb415f2 --- /dev/null +++ b/contrib/nitcc/tests/smalltalk.input @@ -0,0 +1,21 @@ +exampleWithNumber: x + "This method illustrates the ""complete"" syntax." + + + | y | + + true & false not & (nil isNil) + ifFalse: [ self perform: #add: with: x ]. + + y := thisContext stack size + super size. + + byteArray := #[ 2 2r100 8r20 16rFF ]. + + { -42 . #($a #a 'a' 1 1.0) } + do: [ :each | + | var | + Transcript + show: each class name; + show: each printString; + show: ' ' ]. + ^ x < y