]> gitweb.factorcode.org Git - factor.git/blob - extra/smalltalk/parser/parser-tests.factor
First checkin of extra/smalltalk
[factor.git] / extra / smalltalk / parser / parser-tests.factor
1 IN: smalltalk.parser.tests
2 USING: smalltalk.parser smalltalk.ast peg.ebnf tools.test accessors
3 io.files io.encodings.ascii kernel ;
4
5 EBNF: test-Character
6 test         = <foreign parse-smalltalk Character>
7 ;EBNF
8
9 [ CHAR: a ] [ "a" test-Character ] unit-test
10
11 EBNF: test-Comment
12 test         = <foreign parse-smalltalk Comment>
13 ;EBNF
14
15 [ T{ ast-comment f "Hello, this is a comment." } ]
16 [ "\"Hello, this is a comment.\"" test-Comment ]
17 unit-test
18
19 [ T{ ast-comment f "Hello, \"this\" is a comment." } ]
20 [ "\"Hello, \"\"this\"\" is a comment.\"" test-Comment ]
21 unit-test
22
23 EBNF: test-Identifier
24 test         = <foreign parse-smalltalk Identifier>
25 ;EBNF
26
27 [ "OrderedCollection" ] [ "OrderedCollection" test-Identifier ] unit-test
28
29 EBNF: test-Literal
30 test         = <foreign parse-smalltalk Literal>
31 ;EBNF
32
33 [ nil ] [ "nil" test-Literal ] unit-test
34 [ 123 ] [ "123" test-Literal ] unit-test
35 [ HEX: deadbeef ] [ "16rdeadbeef" test-Literal ] unit-test
36 [ -123 ] [ "-123" test-Literal ] unit-test
37 [ 1.2 ] [ "1.2" test-Literal ] unit-test
38 [ -1.24 ] [ "-1.24" test-Literal ] unit-test
39 [ 12.4e7 ] [ "12.4e7" test-Literal ] unit-test
40 [ 12.4e-7 ] [ "12.4e-7" test-Literal ] unit-test
41 [ -12.4e7 ] [ "-12.4e7" test-Literal ] unit-test
42 [ CHAR: x ] [ "$x" test-Literal ] unit-test
43 [ "Hello, world" ] [ "'Hello, world'" test-Literal ] unit-test
44 [ "Hello, 'funny' world" ] [ "'Hello, ''funny'' world'" test-Literal ] unit-test
45 [ T{ symbol f "foo" } ] [ "#foo" test-Literal ] unit-test
46 [ T{ symbol f "+" } ] [ "#+" test-Literal ] unit-test
47 [ T{ symbol f "at:put:" } ] [ "#at:put:" test-Literal ] unit-test
48 [ T{ symbol f "Hello world" } ] [ "#'Hello world'" test-Literal ] unit-test
49 [ B{ 1 2 3 4 } ] [ "#[1 2 3 4]" test-Literal ] unit-test
50 [ { nil t f } ] [ "#(nil true false)" test-Literal ] unit-test
51 [ { nil { t f } } ] [ "#(nil (true false))" test-Literal ] unit-test
52 [ T{ ast-block f { } { } } ] [ "[]" test-Literal ] unit-test
53 [ T{ ast-block f { "x" } { T{ ast-return f T{ ast-name f "x" } } } } ] [ "[ :x|^x]" test-Literal ] unit-test
54 [ T{ ast-block f { } { T{ ast-return f self } } } ] [ "[^self]" test-Literal ] unit-test
55
56 EBNF: test-FormalBlockArgumentDeclarationList
57 test         = <foreign parse-smalltalk FormalBlockArgumentDeclarationList>
58 ;EBNF
59
60 [ V{ "x" "y" "elt" } ] [ ":x :y :elt" test-FormalBlockArgumentDeclarationList ] unit-test
61
62 EBNF: test-Operand
63 test         = <foreign parse-smalltalk Operand>
64 ;EBNF
65
66 [ { 123 15.6 { t f } } ] [ "#(123 15.6 (true false))" test-Operand ] unit-test
67 [ T{ ast-name f "x" } ] [ "x" test-Operand ] unit-test
68
69 EBNF: test-Expression
70 test         = <foreign parse-smalltalk Expression>
71 ;EBNF
72
73 [ self ] [ "self" test-Expression ] unit-test
74 [ { 123 15.6 { t f } } ] [ "#(123 15.6 (true false))" test-Expression ] unit-test
75 [ T{ ast-name f "x" } ] [ "x" test-Expression ] unit-test
76 [ T{ ast-message-send f 5 "print" { } } ] [ "5 print" test-Expression ] unit-test
77 [ T{ ast-message-send f T{ ast-message-send f 5 "squared" { } } "print" { } } ] [ "5 squared print" test-Expression ] unit-test
78 [ T{ ast-message-send f 2 "+" { 2 } } ] [ "2+2" test-Expression ] unit-test
79
80 [
81     T{ ast-message-send f
82         T{ ast-message-send f 3 "factorial" { } }
83         "+"
84         { T{ ast-message-send f 4 "factorial" { } } }
85     }
86 ]
87 [ "3 factorial + 4 factorial" test-Expression ] unit-test
88
89 [
90     T{ ast-message-send f
91         T{ ast-message-send f
92             T{ ast-message-send f 3 "factorial" { } }
93             "+"
94             { 4 }
95         }
96         "factorial"
97         { }
98     }
99 ]
100 [ "(3 factorial + 4) factorial" test-Expression ] unit-test
101 EBNF: test-FinalStatement
102 test         = <foreign parse-smalltalk FinalStatement>
103 ;EBNF
104
105 [ T{ ast-return f T{ ast-name f "value" } } ] [ "value" test-FinalStatement ] unit-test
106 [ T{ ast-return f T{ ast-name f "value" } } ] [ "^value" test-FinalStatement ] unit-test
107 [ T{ ast-return f T{ ast-assignment f T{ ast-name f "value" } 5 } } ] [ "value:=5" test-FinalStatement ] unit-test
108
109 EBNF: test-LocalVariableDeclarationList
110 test         = <foreign parse-smalltalk LocalVariableDeclarationList>
111 ;EBNF
112
113 [ T{ ast-local-variables f { "i" "j" } } ] [ " |  i j   |" test-LocalVariableDeclarationList ] unit-test
114
115
116 EBNF: test-KeywordMessageSend
117 test         = <foreign parse-smalltalk KeywordMessageSend>
118 ;EBNF
119
120 [ T{ ast-message-send f T{ ast-name f "x" } "foo:bar:" { 1 2 } } ]
121 [ "x foo:1 bar:2" test-KeywordMessageSend ] unit-test
122
123 [
124     T{ ast-message-send
125         f
126         T{ ast-message-send f
127             T{ ast-message-send f 3 "factorial" { } }
128             "+"
129             { T{ ast-message-send f 4 "factorial" { } } }
130         }
131         "between:and:"
132         { 10 100 }
133     }
134 ]
135 [ "3 factorial + 4 factorial between: 10 and: 100" test-KeywordMessageSend ] unit-test
136
137 [ ] [ "vocab:smalltalk/parser/test.st" ascii file-contents parse-smalltalk drop ] unit-test