rubex 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,129 @@
1
+ # encoding: UTF-8
2
+ #--
3
+ # This file is automatically generated. Do not modify it.
4
+ # Generated by: oedipus_lex version 2.4.1.
5
+ # Source: lib/rubex/lexer.rex
6
+ #++
7
+
8
+ class Rubex::Lexer
9
+ require 'strscan'
10
+
11
+ DEF = /def/
12
+ RETURN = /return/
13
+ IDENTIFIER = /[a-z_][a-zA-Z_0-9]*/
14
+ LPAREN = /\(/
15
+ RPAREN = /\)/
16
+ NL = /\n/
17
+ COMMA = /,/
18
+ EXPO = /\*\*/
19
+ MULTIPLY = /\*/
20
+ DIVIDE = /\//
21
+ PLUS = /\+/
22
+ MINUS = /\-/
23
+ MODULUS = /%/
24
+ EQUAL = /=/
25
+
26
+ class ScanError < StandardError ; end
27
+
28
+ attr_accessor :filename
29
+ attr_accessor :ss
30
+ attr_accessor :state
31
+
32
+ alias :match :ss
33
+
34
+ def matches
35
+ m = (1..9).map { |i| ss[i] }
36
+ m.pop until m[-1] or m.empty?
37
+ m
38
+ end
39
+
40
+ def action
41
+ yield
42
+ end
43
+
44
+
45
+ def scanner_class
46
+ StringScanner
47
+ end unless instance_methods(false).map(&:to_s).include?("scanner_class")
48
+
49
+ def parse str
50
+ self.ss = scanner_class.new str
51
+ self.state ||= nil
52
+
53
+ do_parse
54
+ end
55
+
56
+ def parse_file path
57
+ self.filename = path
58
+ open path do |f|
59
+ parse f.read
60
+ end
61
+ end
62
+
63
+ def next_token
64
+
65
+ token = nil
66
+
67
+ until ss.eos? or token do
68
+ token =
69
+ case state
70
+ when nil then
71
+ case
72
+ when text = ss.scan(/#{DEF}/) then
73
+ action { [:kDEF, text] }
74
+ when text = ss.scan(/end/) then
75
+ action { [:kEND, text] }
76
+ when text = ss.scan(/#{RETURN}/) then
77
+ action { [:kRETURN, text] }
78
+ when text = ss.scan(/i32/) then
79
+ action { [:kDTYPE_I32, text] }
80
+ when text = ss.scan(/#{IDENTIFIER}/) then
81
+ action { [:tIDENTIFIER, text] }
82
+ when text = ss.scan(/#{LPAREN}/) then
83
+ action { [:tLPAREN, text] }
84
+ when text = ss.scan(/#{RPAREN}/) then
85
+ action { [:tRPAREN, text] }
86
+ when text = ss.scan(/#{NL}/) then
87
+ action { [:tNL, text] }
88
+ when text = ss.scan(/#{COMMA}/) then
89
+ action { [:tCOMMA, text] }
90
+ when text = ss.scan(/#{PLUS}/) then
91
+ action { [:tPLUS, text]}
92
+ when text = ss.scan(/#{MINUS}/) then
93
+ action { [:tMINUS, text]}
94
+ when text = ss.scan(/#{MULTIPLY}/) then
95
+ action { [:tMULTIPLY, text]}
96
+ when text = ss.scan(/#{DIVIDE}/) then
97
+ action { [:tDIVIDE, text]}
98
+ when text = ss.scan(/#{EXPO}/) then
99
+ action { [:tEXPO, text]}
100
+ when text = ss.scan(/#{MODULUS}/) then
101
+ action { [:tMODULUS, text]}
102
+ when text = ss.scan(/#{EXPO}/) then
103
+ action { [:tEXPO, text]}
104
+ when ss.skip(/ /) then
105
+ action {}
106
+ else
107
+ text = ss.string[ss.pos .. -1]
108
+ raise ScanError, "can not match (#{state.inspect}): '#{text}'"
109
+ end
110
+ else
111
+ raise ScanError, "undefined state: '#{state}'"
112
+ end # token = case state
113
+
114
+ next unless token # allow functions to trigger redo w/ nil
115
+ end # while
116
+
117
+ raise "bad lexical result: #{token.inspect}" unless
118
+ token.nil? || (Array === token && token.size >= 2)
119
+
120
+ # auto-switch state
121
+ self.state = token.last if token && token.first == :state
122
+
123
+ token
124
+ end # def next_token
125
+ def do_parse
126
+ end
127
+ end # class
128
+
129
+ # Rubex::Lexer
@@ -0,0 +1,67 @@
1
+ class Rubex::Parser
2
+ token
3
+ kDEF kEND kRETURN kDTYPE_I32 tLPAREN tRPAREN tIDENTIFIER tPLUS tNL tCOMMA
4
+
5
+ prechigh
6
+ left tEXPO
7
+ left tMULTIPLY tDIVIDE tMODULUS
8
+ left tPLUS tMINUS
9
+
10
+ right tEQUAL
11
+ preclow
12
+
13
+ rule
14
+
15
+ program:
16
+ stmts { result = Node.new(val[0]) }
17
+
18
+ stmts:
19
+ stmts stmt tNL { result = [val[0], val[1]] }
20
+ | stmt tNL { result = [val[0]] }
21
+
22
+ stmt:
23
+ kDEF fname f_arglist stmts kEND
24
+ {
25
+ result = RubyMethodDef.new(val[1], val[2])
26
+ result.add_statements val[3]
27
+ }
28
+ | kRETURN expr { result = Statement::Return.new val[1] }
29
+ | {}
30
+
31
+ fname:
32
+ tIDENTIFIER {}
33
+
34
+ f_arglist:
35
+ tLPAREN f_args tRPAREN tNL { result = val[1] }
36
+
37
+ f_args:
38
+ f_args tCOMMA f_normal_arg
39
+ {
40
+ result = ArgumentList.new
41
+ result.push val[0]
42
+ result.push val[2]
43
+ }
44
+ | f_normal_arg { }
45
+
46
+ f_normal_arg:
47
+ kDTYPE_I32 tIDENTIFIER { result = CBaseType.new val[0], val[1] }
48
+
49
+ expr:
50
+ tIDENTIFIER tPLUS tIDENTIFIER { result = Expression::Addition.new val[0], val[2] }
51
+ end
52
+
53
+ ---- header
54
+ require_relative 'lexer.rex.rb'
55
+ require_relative 'ast.rb'
56
+
57
+ include Rubex::AST
58
+
59
+ ---- inner
60
+ def parse file_name
61
+ @lexer = Rubex::Lexer.new
62
+ @lexer.parse_file file_name
63
+ end
64
+
65
+ def next_token
66
+ @lexer.next_token
67
+ end
@@ -0,0 +1,251 @@
1
+ #
2
+ # DO NOT MODIFY!!!!
3
+ # This file is automatically generated by Racc 1.4.14
4
+ # from Racc grammer file "".
5
+ #
6
+
7
+ require 'racc/parser.rb'
8
+
9
+ require_relative 'lexer.rex.rb'
10
+ require_relative 'ast.rb'
11
+
12
+ include Rubex::AST
13
+
14
+ module Rubex
15
+ class Parser < Racc::Parser
16
+
17
+ module_eval(<<'...end parser.racc/module_eval...', 'parser.racc', 60)
18
+ def parse file_name
19
+ @lexer = Rubex::Lexer.new
20
+ @lexer.parse_file file_name
21
+ end
22
+
23
+ def next_token
24
+ @lexer.next_token
25
+ end
26
+ ...end parser.racc/module_eval...
27
+ ##### State transition tables begin ###
28
+
29
+ racc_action_table = [
30
+ 4, 24, 5, 6, 4, 25, 5, 4, -6, 5,
31
+ 4, 23, 5, 8, 10, 12, 13, 14, 16, 17,
32
+ 21, 22, 26, 27, 21 ]
33
+
34
+ racc_action_check = [
35
+ 2, 19, 2, 1, 0, 19, 0, 15, 2, 15,
36
+ 18, 18, 18, 3, 4, 5, 6, 7, 9, 12,
37
+ 16, 17, 21, 24, 25 ]
38
+
39
+ racc_action_pointer = [
40
+ 2, 3, -2, 3, 6, 7, 16, 7, nil, 12,
41
+ nil, nil, 10, nil, nil, 5, 15, 13, 8, -6,
42
+ nil, 14, nil, nil, 13, 19, nil, nil, nil ]
43
+
44
+ racc_action_default = [
45
+ -6, -13, -1, -13, -13, -13, -13, -13, -3, -13,
46
+ -7, -5, -13, 29, -2, -6, -13, -13, -6, -13,
47
+ -10, -13, -12, -4, -13, -13, -11, -8, -9 ]
48
+
49
+ racc_goto_table = [
50
+ 7, 20, 2, 1, 9, 15, 11, 19, nil, nil,
51
+ 28, nil, nil, nil, nil, nil, 7, 18 ]
52
+
53
+ racc_goto_check = [
54
+ 3, 8, 2, 1, 4, 5, 6, 7, nil, nil,
55
+ 8, nil, nil, nil, nil, nil, 3, 2 ]
56
+
57
+ racc_goto_pointer = [
58
+ nil, 3, 2, -2, 0, -4, 1, -9, -15 ]
59
+
60
+ racc_goto_default = [
61
+ nil, nil, nil, 3, nil, nil, nil, nil, nil ]
62
+
63
+ racc_reduce_table = [
64
+ 0, 0, :racc_error,
65
+ 1, 19, :_reduce_1,
66
+ 3, 20, :_reduce_2,
67
+ 2, 20, :_reduce_3,
68
+ 5, 21, :_reduce_4,
69
+ 2, 21, :_reduce_5,
70
+ 0, 21, :_reduce_6,
71
+ 1, 22, :_reduce_7,
72
+ 4, 23, :_reduce_8,
73
+ 3, 25, :_reduce_9,
74
+ 1, 25, :_reduce_10,
75
+ 2, 26, :_reduce_11,
76
+ 3, 24, :_reduce_12 ]
77
+
78
+ racc_reduce_n = 13
79
+
80
+ racc_shift_n = 29
81
+
82
+ racc_token_table = {
83
+ false => 0,
84
+ :error => 1,
85
+ :kDEF => 2,
86
+ :kEND => 3,
87
+ :kRETURN => 4,
88
+ :kDTYPE_I32 => 5,
89
+ :tLPAREN => 6,
90
+ :tRPAREN => 7,
91
+ :tIDENTIFIER => 8,
92
+ :tPLUS => 9,
93
+ :tNL => 10,
94
+ :tCOMMA => 11,
95
+ :tEXPO => 12,
96
+ :tMULTIPLY => 13,
97
+ :tDIVIDE => 14,
98
+ :tMODULUS => 15,
99
+ :tMINUS => 16,
100
+ :tEQUAL => 17 }
101
+
102
+ racc_nt_base = 18
103
+
104
+ racc_use_result_var = true
105
+
106
+ Racc_arg = [
107
+ racc_action_table,
108
+ racc_action_check,
109
+ racc_action_default,
110
+ racc_action_pointer,
111
+ racc_goto_table,
112
+ racc_goto_check,
113
+ racc_goto_default,
114
+ racc_goto_pointer,
115
+ racc_nt_base,
116
+ racc_reduce_table,
117
+ racc_token_table,
118
+ racc_shift_n,
119
+ racc_reduce_n,
120
+ racc_use_result_var ]
121
+
122
+ Racc_token_to_s_table = [
123
+ "$end",
124
+ "error",
125
+ "kDEF",
126
+ "kEND",
127
+ "kRETURN",
128
+ "kDTYPE_I32",
129
+ "tLPAREN",
130
+ "tRPAREN",
131
+ "tIDENTIFIER",
132
+ "tPLUS",
133
+ "tNL",
134
+ "tCOMMA",
135
+ "tEXPO",
136
+ "tMULTIPLY",
137
+ "tDIVIDE",
138
+ "tMODULUS",
139
+ "tMINUS",
140
+ "tEQUAL",
141
+ "$start",
142
+ "program",
143
+ "stmts",
144
+ "stmt",
145
+ "fname",
146
+ "f_arglist",
147
+ "expr",
148
+ "f_args",
149
+ "f_normal_arg" ]
150
+
151
+ Racc_debug_parser = false
152
+
153
+ ##### State transition tables end #####
154
+
155
+ # reduce 0 omitted
156
+
157
+ module_eval(<<'.,.,', 'parser.racc', 15)
158
+ def _reduce_1(val, _values, result)
159
+ result = Node.new(val[0])
160
+ result
161
+ end
162
+ .,.,
163
+
164
+ module_eval(<<'.,.,', 'parser.racc', 18)
165
+ def _reduce_2(val, _values, result)
166
+ result = [val[0], val[1]]
167
+ result
168
+ end
169
+ .,.,
170
+
171
+ module_eval(<<'.,.,', 'parser.racc', 19)
172
+ def _reduce_3(val, _values, result)
173
+ result = [val[0]]
174
+ result
175
+ end
176
+ .,.,
177
+
178
+ module_eval(<<'.,.,', 'parser.racc', 24)
179
+ def _reduce_4(val, _values, result)
180
+ result = RubyMethodDef.new(val[1], val[2])
181
+ result.add_statements val[3]
182
+
183
+ result
184
+ end
185
+ .,.,
186
+
187
+ module_eval(<<'.,.,', 'parser.racc', 27)
188
+ def _reduce_5(val, _values, result)
189
+ result = Statement::Return.new val[1]
190
+ result
191
+ end
192
+ .,.,
193
+
194
+ module_eval(<<'.,.,', 'parser.racc', 28)
195
+ def _reduce_6(val, _values, result)
196
+
197
+ result
198
+ end
199
+ .,.,
200
+
201
+ module_eval(<<'.,.,', 'parser.racc', 31)
202
+ def _reduce_7(val, _values, result)
203
+
204
+ result
205
+ end
206
+ .,.,
207
+
208
+ module_eval(<<'.,.,', 'parser.racc', 34)
209
+ def _reduce_8(val, _values, result)
210
+ result = val[1]
211
+ result
212
+ end
213
+ .,.,
214
+
215
+ module_eval(<<'.,.,', 'parser.racc', 39)
216
+ def _reduce_9(val, _values, result)
217
+ result = ArgumentList.new
218
+ result.push val[0]
219
+ result.push val[2]
220
+
221
+ result
222
+ end
223
+ .,.,
224
+
225
+ module_eval(<<'.,.,', 'parser.racc', 43)
226
+ def _reduce_10(val, _values, result)
227
+
228
+ result
229
+ end
230
+ .,.,
231
+
232
+ module_eval(<<'.,.,', 'parser.racc', 46)
233
+ def _reduce_11(val, _values, result)
234
+ result = CBaseType.new val[0], val[1]
235
+ result
236
+ end
237
+ .,.,
238
+
239
+ module_eval(<<'.,.,', 'parser.racc', 49)
240
+ def _reduce_12(val, _values, result)
241
+ result = Expression::Addition.new val[0], val[2]
242
+ result
243
+ end
244
+ .,.,
245
+
246
+ def _reduce_none(val, _values, result)
247
+ val[0]
248
+ end
249
+
250
+ end # class Parser
251
+ end # module Rubex
@@ -0,0 +1,2 @@
1
+ require 'rubex/symbol_table/scope'
2
+ require 'rubex/symbol_table/entry'