rltk3 3.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/AUTHORS +1 -0
- data/LICENSE +27 -0
- data/README.md +852 -0
- data/Rakefile +197 -0
- data/lib/rltk/ast.rb +573 -0
- data/lib/rltk/cfg.rb +683 -0
- data/lib/rltk/cg/basic_block.rb +157 -0
- data/lib/rltk/cg/bindings.rb +151 -0
- data/lib/rltk/cg/builder.rb +1127 -0
- data/lib/rltk/cg/context.rb +48 -0
- data/lib/rltk/cg/contractor.rb +51 -0
- data/lib/rltk/cg/execution_engine.rb +194 -0
- data/lib/rltk/cg/function.rb +237 -0
- data/lib/rltk/cg/generated_bindings.rb +8118 -0
- data/lib/rltk/cg/generic_value.rb +95 -0
- data/lib/rltk/cg/instruction.rb +519 -0
- data/lib/rltk/cg/llvm.rb +150 -0
- data/lib/rltk/cg/memory_buffer.rb +75 -0
- data/lib/rltk/cg/module.rb +451 -0
- data/lib/rltk/cg/pass_manager.rb +252 -0
- data/lib/rltk/cg/support.rb +29 -0
- data/lib/rltk/cg/target.rb +230 -0
- data/lib/rltk/cg/triple.rb +58 -0
- data/lib/rltk/cg/type.rb +554 -0
- data/lib/rltk/cg/value.rb +1272 -0
- data/lib/rltk/cg.rb +32 -0
- data/lib/rltk/lexer.rb +372 -0
- data/lib/rltk/lexers/calculator.rb +44 -0
- data/lib/rltk/lexers/ebnf.rb +38 -0
- data/lib/rltk/parser.rb +1702 -0
- data/lib/rltk/parsers/infix_calc.rb +43 -0
- data/lib/rltk/parsers/postfix_calc.rb +34 -0
- data/lib/rltk/parsers/prefix_calc.rb +34 -0
- data/lib/rltk/token.rb +90 -0
- data/lib/rltk/version.rb +11 -0
- data/lib/rltk.rb +16 -0
- data/test/cg/tc_basic_block.rb +83 -0
- data/test/cg/tc_control_flow.rb +191 -0
- data/test/cg/tc_function.rb +54 -0
- data/test/cg/tc_generic_value.rb +33 -0
- data/test/cg/tc_instruction.rb +256 -0
- data/test/cg/tc_llvm.rb +25 -0
- data/test/cg/tc_math.rb +88 -0
- data/test/cg/tc_module.rb +89 -0
- data/test/cg/tc_transforms.rb +68 -0
- data/test/cg/tc_type.rb +69 -0
- data/test/cg/tc_value.rb +151 -0
- data/test/cg/ts_cg.rb +23 -0
- data/test/tc_ast.rb +332 -0
- data/test/tc_cfg.rb +164 -0
- data/test/tc_lexer.rb +216 -0
- data/test/tc_parser.rb +711 -0
- data/test/tc_token.rb +34 -0
- data/test/ts_rltk.rb +47 -0
- metadata +317 -0
data/test/tc_lexer.rb
ADDED
@@ -0,0 +1,216 @@
|
|
1
|
+
# Author: Chris Wailes <chris.wailes@gmail.com>
|
2
|
+
# Project: Ruby Language Toolkit
|
3
|
+
# Date: 2011/04/06
|
4
|
+
# Description: This file contains unit tests for the RLTK::Lexer class.
|
5
|
+
|
6
|
+
############
|
7
|
+
# Requires #
|
8
|
+
############
|
9
|
+
|
10
|
+
# Gems
|
11
|
+
require 'minitest/autorun'
|
12
|
+
|
13
|
+
# Ruby Language Toolkit
|
14
|
+
require 'rltk/token'
|
15
|
+
require 'rltk/lexer'
|
16
|
+
require 'rltk/lexers/calculator'
|
17
|
+
require 'rltk/lexers/ebnf'
|
18
|
+
|
19
|
+
#######################
|
20
|
+
# Classes and Modules #
|
21
|
+
#######################
|
22
|
+
|
23
|
+
class LexerTester < Minitest::Test
|
24
|
+
class ABLongest < RLTK::Lexer
|
25
|
+
rule(/a+/) { :APLUS }
|
26
|
+
rule(/b+/) { :BPLUS }
|
27
|
+
|
28
|
+
rule(/a+b+/) { :APLUSBPLUS }
|
29
|
+
end
|
30
|
+
|
31
|
+
class ABFirst < RLTK::Lexer
|
32
|
+
match_first
|
33
|
+
|
34
|
+
rule(/a+/) { :APLUS }
|
35
|
+
rule(/b+/) { :BPLUS }
|
36
|
+
|
37
|
+
rule(/a+b+/) { :APLUSBPLUS }
|
38
|
+
end
|
39
|
+
|
40
|
+
class ENVLexer < RLTK::Lexer
|
41
|
+
rule(/a/) { [:A, next_value] }
|
42
|
+
|
43
|
+
class Environment < Environment
|
44
|
+
def initialize(*args)
|
45
|
+
super(*args)
|
46
|
+
@value = -1
|
47
|
+
end
|
48
|
+
|
49
|
+
def next_value
|
50
|
+
@value += 1
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
class FlagLexer < RLTK::Lexer
|
56
|
+
rule(/a/) { set_flag(:a); :A }
|
57
|
+
rule(/\s/)
|
58
|
+
|
59
|
+
rule(/b/, :default, [:a]) { set_flag(:b); :B }
|
60
|
+
rule(/c/, :default, [:a, :b]) { :C }
|
61
|
+
end
|
62
|
+
|
63
|
+
class StateLexer < RLTK::Lexer
|
64
|
+
rule(/a/) { :A }
|
65
|
+
rule(/\s/)
|
66
|
+
|
67
|
+
rule(/\(\*/) { push_state(:comment) }
|
68
|
+
|
69
|
+
rule(/\(\*/, :comment) { push_state(:comment) }
|
70
|
+
rule(/\*\)/, :comment) { pop_state }
|
71
|
+
rule(/./, :comment)
|
72
|
+
end
|
73
|
+
|
74
|
+
class MatchDataLexer < RLTK::Lexer
|
75
|
+
rule(/a(b*)(c+)/) { [:FOO, match[1,2]] }
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_calc
|
79
|
+
expected = [
|
80
|
+
RLTK::Token.new(:NUM, 1),
|
81
|
+
|
82
|
+
RLTK::Token.new(:PLS),
|
83
|
+
RLTK::Token.new(:SUB),
|
84
|
+
RLTK::Token.new(:MUL),
|
85
|
+
RLTK::Token.new(:DIV),
|
86
|
+
|
87
|
+
RLTK::Token.new(:LPAREN),
|
88
|
+
RLTK::Token.new(:RPAREN),
|
89
|
+
RLTK::Token.new(:EOS)
|
90
|
+
]
|
91
|
+
|
92
|
+
actual = RLTK::Lexers::Calculator.lex('1 + - * / ( )')
|
93
|
+
|
94
|
+
assert_equal(expected, actual)
|
95
|
+
end
|
96
|
+
|
97
|
+
def test_ebnf
|
98
|
+
expected = [
|
99
|
+
RLTK::Token.new(:NONTERM, :aaa),
|
100
|
+
RLTK::Token.new(:TERM, :BBB),
|
101
|
+
|
102
|
+
RLTK::Token.new(:STAR),
|
103
|
+
RLTK::Token.new(:PLUS),
|
104
|
+
RLTK::Token.new(:QUESTION),
|
105
|
+
RLTK::Token.new(:EOS)
|
106
|
+
]
|
107
|
+
|
108
|
+
actual = RLTK::Lexers::EBNF.lex('aaa BBB * + ?')
|
109
|
+
|
110
|
+
assert_equal(expected, actual)
|
111
|
+
end
|
112
|
+
|
113
|
+
def test_environment
|
114
|
+
expected = [
|
115
|
+
RLTK::Token.new(:A, 0),
|
116
|
+
RLTK::Token.new(:A, 1),
|
117
|
+
RLTK::Token.new(:A, 2),
|
118
|
+
RLTK::Token.new(:EOS)
|
119
|
+
]
|
120
|
+
|
121
|
+
actual = ENVLexer.lex('aaa')
|
122
|
+
|
123
|
+
assert_equal(expected, actual)
|
124
|
+
|
125
|
+
lexer = ENVLexer.new
|
126
|
+
|
127
|
+
assert_equal(expected, lexer.lex('aaa'))
|
128
|
+
|
129
|
+
expected = [
|
130
|
+
RLTK::Token.new(:A, 3),
|
131
|
+
RLTK::Token.new(:A, 4),
|
132
|
+
RLTK::Token.new(:A, 5),
|
133
|
+
RLTK::Token.new(:EOS)
|
134
|
+
]
|
135
|
+
|
136
|
+
assert_equal(expected, lexer.lex('aaa'))
|
137
|
+
end
|
138
|
+
|
139
|
+
def test_first_match
|
140
|
+
expected = [
|
141
|
+
RLTK::Token.new(:APLUS),
|
142
|
+
RLTK::Token.new(:BPLUS),
|
143
|
+
RLTK::Token.new(:EOS)
|
144
|
+
]
|
145
|
+
|
146
|
+
actual = ABFirst.lex('aaabbb')
|
147
|
+
|
148
|
+
assert_equal(expected, actual)
|
149
|
+
end
|
150
|
+
|
151
|
+
def test_flags
|
152
|
+
|
153
|
+
assert_raises(RLTK::LexingError) { FlagLexer.lex('b') }
|
154
|
+
assert_raises(RLTK::LexingError) { FlagLexer.lex('ac') }
|
155
|
+
|
156
|
+
expected = [
|
157
|
+
RLTK::Token.new(:A),
|
158
|
+
RLTK::Token.new(:B),
|
159
|
+
RLTK::Token.new(:C),
|
160
|
+
RLTK::Token.new(:EOS)
|
161
|
+
]
|
162
|
+
|
163
|
+
actual = FlagLexer.lex('abc')
|
164
|
+
assert_equal(expected, actual)
|
165
|
+
|
166
|
+
expected = [
|
167
|
+
RLTK::Token.new(:A),
|
168
|
+
RLTK::Token.new(:B),
|
169
|
+
RLTK::Token.new(:C),
|
170
|
+
RLTK::Token.new(:A),
|
171
|
+
RLTK::Token.new(:B),
|
172
|
+
RLTK::Token.new(:C),
|
173
|
+
RLTK::Token.new(:EOS)
|
174
|
+
]
|
175
|
+
|
176
|
+
actual = FlagLexer.lex('abcabc')
|
177
|
+
assert_equal(expected, actual)
|
178
|
+
end
|
179
|
+
|
180
|
+
def test_lex
|
181
|
+
assert_raises(RLTK::LexingError) { ABFirst.lex('aaabbbCCC') }
|
182
|
+
assert_raises(RLTK::LexingError) { ABLongest.lex('aaabbbCCC') }
|
183
|
+
end
|
184
|
+
|
185
|
+
def test_longest_match
|
186
|
+
expected = [
|
187
|
+
RLTK::Token.new(:APLUSBPLUS),
|
188
|
+
RLTK::Token.new(:EOS)
|
189
|
+
]
|
190
|
+
|
191
|
+
actual = ABLongest.lex('aaabbb')
|
192
|
+
|
193
|
+
assert_equal(expected, actual)
|
194
|
+
end
|
195
|
+
|
196
|
+
def test_match_data
|
197
|
+
expected = [RLTK::Token.new(:FOO, ['', 'ccc']), RLTK::Token.new(:EOS)]
|
198
|
+
actual = MatchDataLexer.lex('accc')
|
199
|
+
|
200
|
+
assert_equal(expected, actual)
|
201
|
+
end
|
202
|
+
|
203
|
+
def test_state
|
204
|
+
expected = [
|
205
|
+
RLTK::Token.new(:A),
|
206
|
+
RLTK::Token.new(:A),
|
207
|
+
RLTK::Token.new(:EOS)
|
208
|
+
]
|
209
|
+
|
210
|
+
actual = StateLexer.lex('a (* bbb *) a')
|
211
|
+
assert_equal(expected, actual)
|
212
|
+
|
213
|
+
actual = StateLexer.lex('a (* b (* ccc *) b *) a')
|
214
|
+
assert_equal(expected, actual)
|
215
|
+
end
|
216
|
+
end
|