1 # frozen_string_literal: true
2
3 require "test_helper"
4
5
6 module Umu
7
8 module Test
9
10 module Lexcical
11 =begin
12 /* See state machine specification in "lib/umu/lexical/lexer.rb" */
13
14
15 /* Token (Entry rule of Lexcical ruleset) */
16
17 <token> ::=
18 <separator>
19 | <number>
20 | <string>
21 | <word>
22 ;
23
24
25 /* Separator */
26
27 <separator> ::=
28 <space>
29 | <newlinw>
30 | <comment>
31 ;
32
33 <space> ::= " "+ ; /* PatSpace */
34 <newline> ::= "\n" | "\r" | "\n\r" ; /* PatNewline */
35 <comment> ::=
36 <line-comment>
37 | <begein-comment>
38 | <end-comment>
39 ;
40
41 <line-comment> ::= "#" ( ^ <newlinw> )* ; /* PatComment */
42 <begin-comment> ::= "(#" ; /* PatBeginComment */
43 <end-comment> ::= "#)" ; /* PatEndComment */
44
45
46 /* Number */
47
48 <number> ::= <int> | <float> ;
49
50 <int> ::= <sign>? <digit>+ ; /* PatNumber */
51 <float> ::= <sign>? <digit>+ "." <digit>+ ; /* PatNumber */
52
53 <sign> ::= "+" | "-" ;
54
55
56 /* String */
57
58 <string> ::=
59 <begin-string>
60 | <begin-symbolized-string>
61 | <end-string> ;
62
63 <begin-string> ::= "\"" ; /* PatBeginString */
64 <begin-symbolizedstring> ::= "@" "\"" ; /* PatBeginString */
65 <end-string> ::= "\"" ; /* PatEndString */
66
67
68 /* Word */
69
70 <word> ::=
71 /* <reserved> -- Reserved words are not defined in test suite */
72 | <id>
73 | <symbol>
74 | <message>
75 | <label>
76 | <directory>
77 | <symbol>
78 | <selector>
79 ;
80
81 <id> ::= <id-word> ; /* PatWord */
82 <symbol> ::= "@" <id-word> ; /* PatWord */
83 <message> ::= "." <id-word> ; /* PatWord */
84 <label> ::= <id-word> ":" ; /* PatWord */
85 <directory> ::= <id-word> "::" ; /* PatWord */
86
87 <basic-symbol> ::= "@" <id-word> ; /* PatWord */
88 <symbolized-symbol> ::= "@" <string> ; /* PatWord */
89
90 <selector> ::=
91 <number-selector>
92 | <label-selector>
93 ;
94
95 <number-selector> ::= "$" <digit>+ ; /* PatWord */
96 <label-selector> ::= "$" <id-word> ; /* PatWord */
97
98
99 /* Common rules */
100
101 <id-ward> ::=
102 "_"*
103 <alpha> <alnum>*
104 ( "-" <alnum>+ )*
105 "_"*
106 ( "?" | "!" )
107 "'"*
108 "_"*
109 ;
110
111 <alnum> ::= <alpha> | <digit> ;
112 <alpha> ::= "a" .. "z" | "A" .. "Z" ;
113 <digit> ::= "0" .. "9" ;
114 =end
115
116 class LexcicalTest < Minitest::Test
117 def setup
118 @interp = Api.setup_interpreter
119 end
120
121
122 def test_int
123 value = Api.eval_expr @interp, "3"
124 assert_instance_of VCAN::Int, value
125 assert_equal 3, value.val
126 end
127
128
129 def test_float
130 value = Api.eval_expr @interp, "3.4"
131 assert_instance_of VCAN::Float, value
132 assert_equal 3.4, value.val
133 end
134
135
136 def test_number
137 value = Api.eval_expr @interp, "3"
138 assert_instance_of VCAN::Int, value
139 assert_equal 3, value.val
140
141 value = Api.eval_expr @interp, "30"
142 assert_instance_of VCAN::Int, value
143 assert_equal 30, value.val
144
145 value = Api.eval_expr @interp, "+3"
146 assert_instance_of VCAN::Int, value
147 assert_equal 3, value.val
148
149 value = Api.eval_expr @interp, "-3"
150 assert_instance_of VCAN::Int, value
151 assert_equal( -3, value.val)
152 end
153
154
155 def test_string
156 value = Api.eval_expr @interp, '"Hello world"'
157 assert_instance_of VCA::String, value
158 assert_equal "Hello world", value.val
159 end
160
161
162 def test_symbol
163 value = Api.eval_expr @interp, "@apple"
164 assert_instance_of VCA::Symbol, value
165 assert_equal :apple, value.val
166
167 value = Api.eval_expr @interp, '@"Hello world"'
168 assert_instance_of VCA::Symbol, value
169 assert_equal :"Hello world", value.val
170 end
171
172
173 def test_identifier
174 value = Api.eval_expr @interp, "@apple"
175 assert_instance_of VCA::Symbol, value
176 assert_equal :apple, value.val
177
178 value = Api.eval_expr @interp, "@mp130"
179 assert_instance_of VCA::Symbol, value
180 assert_equal :mp130, value.val
181
182 value = Api.eval_expr @interp, "@apple-banana"
183 assert_instance_of VCA::Symbol, value
184 assert_equal :"apple-banana", value.val
185
186 value = Api.eval_expr @interp, "@apple-20"
187 assert_instance_of VCA::Symbol, value
188 assert_equal :"apple-20", value.val
189
190 value = Api.eval_expr @interp, "@apple-20-banana"
191 assert_instance_of VCA::Symbol, value
192 assert_equal :"apple-20-banana", value.val
193
194 value = Api.eval_expr @interp, "@__apple__"
195 assert_instance_of VCA::Symbol, value
196 assert_equal :__apple__, value.val
197
198 value = Api.eval_expr @interp, "@apple?"
199 assert_instance_of VCA::Symbol, value
200 assert_equal :"apple?", value.val
201
202 value = Api.eval_expr @interp, "@apple!"
203 assert_instance_of VCA::Symbol, value
204 assert_equal :"apple!", value.val
205
206 value = Api.eval_expr @interp, "@apple''"
207 assert_instance_of VCA::Symbol, value
208 assert_equal :"apple''", value.val
209
210 value = Api.eval_expr @interp, "@__mp130-0-mp2000__?''"
211 assert_instance_of VCA::Symbol, value
212 assert_equal :"__mp130-0-mp2000__?''", value.val
213 end
214
215
216 def test_line_comment
217 value = Api.eval_expr @interp, <<-EOS
218 3 # 4
219 EOS
220 assert_instance_of VCAN::Int, value
221 assert_equal 3, value.val
222
223 value = Api.eval_expr @interp, <<-EOS
224 # AAA
225 # BBB
226 3
227 # DCC
228 EOS
229 assert_instance_of VCAN::Int, value
230 assert_equal 3, value.val
231 end
232
233
234 def test_block_comment
235 value = Api.eval_expr @interp, <<-EOS
236 (# 1 #) 3 (# 2 #)
237 EOS
238 assert_instance_of VCAN::Int, value
239 assert_equal 3, value.val
240
241 value = Api.eval_expr @interp, <<-EOS
242 (# 1 (# 1.1 #) #) 3 (# 2 #)
243 EOS
244 assert_instance_of VCAN::Int, value
245 assert_equal 3, value.val
246 end
247 end
248
249 end # Umu::Test::Lexcical
250
251 end # Umu::Test
252
253 end # Umu