meta_compile 0.0.5 → 0.0.6

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.
@@ -0,0 +1,230 @@
1
+ #!/usr/bin/env ruby
2
+ require "strscan"
3
+ class RMetaII
4
+ def compile(str, out)
5
+ @i, @o = StringScanner.new(str), out
6
+ compile_program
7
+ end
8
+ def compile_outarg
9
+ begin
10
+ @i.scan /\s*/; s='$'; l=s.length;
11
+ @f = (@i.peek(l) == s) ? (@t=s; @i.pos += l) : nil
12
+ if @f
13
+ @o.print '@o.print @t'
14
+ @o.print "\n"
15
+ end
16
+ break if @f
17
+ @i.scan /\s*/; @f = @t = @i.scan /\047[^\047]*\047/
18
+ if @f
19
+ @o.print '@o.print '
20
+ @o.print @t
21
+ @o.print "\n"
22
+ end
23
+ end while false
24
+ end
25
+ def compile_out
26
+ begin
27
+ @i.scan /\s*/; s='<'; l=s.length;
28
+ @f = (@i.peek(l) == s) ? (@t=s; @i.pos += l) : nil
29
+ if @f
30
+ begin
31
+ compile_outarg
32
+ end while @f
33
+ @f = true
34
+ raise("error at: " + @i.rest.split("\n")[0]) if !@f
35
+ @i.scan /\s*/; s='>'; l=s.length;
36
+ @f = (@i.peek(l) == s) ? (@t=s; @i.pos += l) : nil
37
+ raise("error at: " + @i.rest.split("\n")[0]) if !@f
38
+ @o.print '@o.print "\n"'
39
+ @o.print "\n"
40
+ end
41
+ end while false
42
+ end
43
+ def compile_exp3
44
+ begin
45
+ @i.scan /\s*/; @f = @t = @i.scan /[A-Za-z]+[A-Za-z0-9_]+/
46
+ if @f
47
+ @o.print 'compile_'
48
+ @o.print @t
49
+ @o.print "\n"
50
+ end
51
+ break if @f
52
+ @i.scan /\s*/; @f = @t = @i.scan /\047[^\047]*\047/
53
+ if @f
54
+ @o.print '@i.scan /\s*/; s='
55
+ @o.print @t
56
+ @o.print '; l=s.length;'
57
+ @o.print "\n"
58
+ @o.print '@f = (@i.peek(l) == s) ? (@t=s; @i.pos += l) : nil'
59
+ @o.print "\n"
60
+ end
61
+ break if @f
62
+ @i.scan /\s*/; s='.id'; l=s.length;
63
+ @f = (@i.peek(l) == s) ? (@t=s; @i.pos += l) : nil
64
+ if @f
65
+ @o.print '@i.scan /\s*/; @f = @t = @i.scan /[A-Za-z]+[A-Za-z0-9_]+/'
66
+ @o.print "\n"
67
+ end
68
+ break if @f
69
+ @i.scan /\s*/; s='.string'; l=s.length;
70
+ @f = (@i.peek(l) == s) ? (@t=s; @i.pos += l) : nil
71
+ if @f
72
+ @o.print '@i.scan /\s*/; @f = @t = @i.scan /\047[^\047]*\047/'
73
+ @o.print "\n"
74
+ end
75
+ break if @f
76
+ @i.scan /\s*/; s='('; l=s.length;
77
+ @f = (@i.peek(l) == s) ? (@t=s; @i.pos += l) : nil
78
+ if @f
79
+ compile_exp1
80
+ raise("error at: " + @i.rest.split("\n")[0]) if !@f
81
+ @i.scan /\s*/; s=')'; l=s.length;
82
+ @f = (@i.peek(l) == s) ? (@t=s; @i.pos += l) : nil
83
+ raise("error at: " + @i.rest.split("\n")[0]) if !@f
84
+ end
85
+ break if @f
86
+ @i.scan /\s*/; s='.e'; l=s.length;
87
+ @f = (@i.peek(l) == s) ? (@t=s; @i.pos += l) : nil
88
+ if @f
89
+ @o.print '@f = true'
90
+ @o.print "\n"
91
+ end
92
+ break if @f
93
+ @i.scan /\s*/; s='*'; l=s.length;
94
+ @f = (@i.peek(l) == s) ? (@t=s; @i.pos += l) : nil
95
+ if @f
96
+ @o.print 'begin'
97
+ @o.print "\n"
98
+ compile_exp3
99
+ raise("error at: " + @i.rest.split("\n")[0]) if !@f
100
+ @o.print 'end while @f'
101
+ @o.print "\n"
102
+ @o.print '@f = true'
103
+ @o.print "\n"
104
+ end
105
+ end while false
106
+ end
107
+ def compile_exp2
108
+ begin
109
+ begin
110
+ compile_exp3
111
+ if @f
112
+ @o.print 'if @f'
113
+ @o.print "\n"
114
+ end
115
+ break if @f
116
+ compile_out
117
+ if @f
118
+ @o.print 'if true'
119
+ @o.print "\n"
120
+ end
121
+ end while false
122
+ if @f
123
+ begin
124
+ begin
125
+ compile_exp3
126
+ if @f
127
+ @o.print 'raise("error at: " + @i.rest.split("\n")[0]) if !@f'
128
+ @o.print "\n"
129
+ end
130
+ break if @f
131
+ compile_out
132
+ if @f
133
+ end
134
+ end while false
135
+ end while @f
136
+ @f = true
137
+ raise("error at: " + @i.rest.split("\n")[0]) if !@f
138
+ @o.print 'end'
139
+ @o.print "\n"
140
+ end
141
+ end while false
142
+ end
143
+ def compile_exp1
144
+ begin
145
+ @o.print 'begin'
146
+ @o.print "\n"
147
+ if true
148
+ compile_exp2
149
+ raise("error at: " + @i.rest.split("\n")[0]) if !@f
150
+ begin
151
+ begin
152
+ @i.scan /\s*/; s='|'; l=s.length;
153
+ @f = (@i.peek(l) == s) ? (@t=s; @i.pos += l) : nil
154
+ if @f
155
+ @o.print 'break if @f'
156
+ @o.print "\n"
157
+ compile_exp2
158
+ raise("error at: " + @i.rest.split("\n")[0]) if !@f
159
+ end
160
+ end while false
161
+ end while @f
162
+ @f = true
163
+ raise("error at: " + @i.rest.split("\n")[0]) if !@f
164
+ @o.print 'end while false'
165
+ @o.print "\n"
166
+ end
167
+ end while false
168
+ end
169
+ def compile_rule
170
+ begin
171
+ @i.scan /\s*/; @f = @t = @i.scan /[A-Za-z]+[A-Za-z0-9_]+/
172
+ if @f
173
+ @o.print 'def compile_'
174
+ @o.print @t
175
+ @o.print "\n"
176
+ @i.scan /\s*/; s='='; l=s.length;
177
+ @f = (@i.peek(l) == s) ? (@t=s; @i.pos += l) : nil
178
+ raise("error at: " + @i.rest.split("\n")[0]) if !@f
179
+ compile_exp1
180
+ raise("error at: " + @i.rest.split("\n")[0]) if !@f
181
+ @i.scan /\s*/; s=';'; l=s.length;
182
+ @f = (@i.peek(l) == s) ? (@t=s; @i.pos += l) : nil
183
+ raise("error at: " + @i.rest.split("\n")[0]) if !@f
184
+ @o.print 'end'
185
+ @o.print "\n"
186
+ end
187
+ end while false
188
+ end
189
+ def compile_program
190
+ begin
191
+ @i.scan /\s*/; s='.syntax'; l=s.length;
192
+ @f = (@i.peek(l) == s) ? (@t=s; @i.pos += l) : nil
193
+ if @f
194
+ @i.scan /\s*/; @f = @t = @i.scan /[A-Za-z]+[A-Za-z0-9_]+/
195
+ raise("error at: " + @i.rest.split("\n")[0]) if !@f
196
+ @o.print '#!/usr/bin/env ruby'
197
+ @o.print "\n"
198
+ @o.print 'require "strscan"'
199
+ @o.print "\n"
200
+ @o.print 'class '
201
+ @o.print @t
202
+ @o.print "\n"
203
+ @o.print 'def compile(str, out)'
204
+ @o.print "\n"
205
+ @o.print '@i, @o = StringScanner.new(str), out'
206
+ @o.print "\n"
207
+ @o.print 'compile_program'
208
+ @o.print "\n"
209
+ @o.print 'end'
210
+ @o.print "\n"
211
+ begin
212
+ compile_rule
213
+ end while @f
214
+ @f = true
215
+ raise("error at: " + @i.rest.split("\n")[0]) if !@f
216
+ @i.scan /\s*/; s='.end'; l=s.length;
217
+ @f = (@i.peek(l) == s) ? (@t=s; @i.pos += l) : nil
218
+ raise("error at: " + @i.rest.split("\n")[0]) if !@f
219
+ @o.print 'end'
220
+ @o.print "\n"
221
+ @o.print 'begin; puts("Use: " + $0 + " <in> <out>"); exit; end if ARGV.length != 2'
222
+ @o.print "\n"
223
+ @o.print 'File.open(ARGV[1], "w") {|f| RMetaII.new.compile(File.read(ARGV[0]), f)}'
224
+ @o.print "\n"
225
+ end
226
+ end while false
227
+ end
228
+ end
229
+ begin; puts("Use: " + $0 + " <in> <out>"); exit; end if ARGV.length != 2
230
+ File.open(ARGV[1], "w") {|f| RMetaII.new.compile(File.read(ARGV[0]), f)}
@@ -0,0 +1,230 @@
1
+ #!/usr/bin/env ruby
2
+ require "strscan"
3
+ class RMetaII
4
+ def compile(str, out)
5
+ @i, @o = StringScanner.new(str), out
6
+ compile_program
7
+ end
8
+ def compile_outarg
9
+ begin
10
+ @i.scan /\s*/; s='$'; l=s.length;
11
+ @f = (@i.peek(l) == s) ? (@t=s; @i.pos += l) : nil
12
+ if @f
13
+ @o.print '@o.print @t'
14
+ @o.print "\n"
15
+ end
16
+ break if @f
17
+ @i.scan /\s*/; @f = @t = @i.scan /\047[^\047]*\047/
18
+ if @f
19
+ @o.print '@o.print '
20
+ @o.print @t
21
+ @o.print "\n"
22
+ end
23
+ end while false
24
+ end
25
+ def compile_out
26
+ begin
27
+ @i.scan /\s*/; s='<'; l=s.length;
28
+ @f = (@i.peek(l) == s) ? (@t=s; @i.pos += l) : nil
29
+ if @f
30
+ begin
31
+ compile_outarg
32
+ end while @f
33
+ @f = true
34
+ raise("error at: " + @i.rest.split("\n")[0]) if !@f
35
+ @i.scan /\s*/; s='>'; l=s.length;
36
+ @f = (@i.peek(l) == s) ? (@t=s; @i.pos += l) : nil
37
+ raise("error at: " + @i.rest.split("\n")[0]) if !@f
38
+ @o.print '@o.print "\n"'
39
+ @o.print "\n"
40
+ end
41
+ end while false
42
+ end
43
+ def compile_exp3
44
+ begin
45
+ @i.scan /\s*/; @f = @t = @i.scan /[A-Za-z]+[A-Za-z0-9_]+/
46
+ if @f
47
+ @o.print 'compile_'
48
+ @o.print @t
49
+ @o.print "\n"
50
+ end
51
+ break if @f
52
+ @i.scan /\s*/; @f = @t = @i.scan /\047[^\047]*\047/
53
+ if @f
54
+ @o.print '@i.scan /\s*/; s='
55
+ @o.print @t
56
+ @o.print '; l=s.length;'
57
+ @o.print "\n"
58
+ @o.print '@f = (@i.peek(l) == s) ? (@t=s; @i.pos += l) : nil'
59
+ @o.print "\n"
60
+ end
61
+ break if @f
62
+ @i.scan /\s*/; s='.id'; l=s.length;
63
+ @f = (@i.peek(l) == s) ? (@t=s; @i.pos += l) : nil
64
+ if @f
65
+ @o.print '@i.scan /\s*/; @f = @t = @i.scan /[A-Za-z]+[A-Za-z0-9_]+/'
66
+ @o.print "\n"
67
+ end
68
+ break if @f
69
+ @i.scan /\s*/; s='.string'; l=s.length;
70
+ @f = (@i.peek(l) == s) ? (@t=s; @i.pos += l) : nil
71
+ if @f
72
+ @o.print '@i.scan /\s*/; @f = @t = @i.scan /\047[^\047]*\047/'
73
+ @o.print "\n"
74
+ end
75
+ break if @f
76
+ @i.scan /\s*/; s='('; l=s.length;
77
+ @f = (@i.peek(l) == s) ? (@t=s; @i.pos += l) : nil
78
+ if @f
79
+ compile_exp1
80
+ raise("error at: " + @i.rest.split("\n")[0]) if !@f
81
+ @i.scan /\s*/; s=')'; l=s.length;
82
+ @f = (@i.peek(l) == s) ? (@t=s; @i.pos += l) : nil
83
+ raise("error at: " + @i.rest.split("\n")[0]) if !@f
84
+ end
85
+ break if @f
86
+ @i.scan /\s*/; s='.e'; l=s.length;
87
+ @f = (@i.peek(l) == s) ? (@t=s; @i.pos += l) : nil
88
+ if @f
89
+ @o.print '@f = true'
90
+ @o.print "\n"
91
+ end
92
+ break if @f
93
+ @i.scan /\s*/; s='*'; l=s.length;
94
+ @f = (@i.peek(l) == s) ? (@t=s; @i.pos += l) : nil
95
+ if @f
96
+ @o.print 'begin'
97
+ @o.print "\n"
98
+ compile_exp3
99
+ raise("error at: " + @i.rest.split("\n")[0]) if !@f
100
+ @o.print 'end while @f'
101
+ @o.print "\n"
102
+ @o.print '@f = true'
103
+ @o.print "\n"
104
+ end
105
+ end while false
106
+ end
107
+ def compile_exp2
108
+ begin
109
+ begin
110
+ compile_exp3
111
+ if @f
112
+ @o.print 'if @f'
113
+ @o.print "\n"
114
+ end
115
+ break if @f
116
+ compile_out
117
+ if @f
118
+ @o.print 'if true'
119
+ @o.print "\n"
120
+ end
121
+ end while false
122
+ if @f
123
+ begin
124
+ begin
125
+ compile_exp3
126
+ if @f
127
+ @o.print 'raise("error at: " + @i.rest.split("\n")[0]) if !@f'
128
+ @o.print "\n"
129
+ end
130
+ break if @f
131
+ compile_out
132
+ if @f
133
+ end
134
+ end while false
135
+ end while @f
136
+ @f = true
137
+ raise("error at: " + @i.rest.split("\n")[0]) if !@f
138
+ @o.print 'end'
139
+ @o.print "\n"
140
+ end
141
+ end while false
142
+ end
143
+ def compile_exp1
144
+ begin
145
+ @o.print 'begin'
146
+ @o.print "\n"
147
+ if true
148
+ compile_exp2
149
+ raise("error at: " + @i.rest.split("\n")[0]) if !@f
150
+ begin
151
+ begin
152
+ @i.scan /\s*/; s='|'; l=s.length;
153
+ @f = (@i.peek(l) == s) ? (@t=s; @i.pos += l) : nil
154
+ if @f
155
+ @o.print 'break if @f'
156
+ @o.print "\n"
157
+ compile_exp2
158
+ raise("error at: " + @i.rest.split("\n")[0]) if !@f
159
+ end
160
+ end while false
161
+ end while @f
162
+ @f = true
163
+ raise("error at: " + @i.rest.split("\n")[0]) if !@f
164
+ @o.print 'end while false'
165
+ @o.print "\n"
166
+ end
167
+ end while false
168
+ end
169
+ def compile_rule
170
+ begin
171
+ @i.scan /\s*/; @f = @t = @i.scan /[A-Za-z]+[A-Za-z0-9_]+/
172
+ if @f
173
+ @o.print 'def compile_'
174
+ @o.print @t
175
+ @o.print "\n"
176
+ @i.scan /\s*/; s='='; l=s.length;
177
+ @f = (@i.peek(l) == s) ? (@t=s; @i.pos += l) : nil
178
+ raise("error at: " + @i.rest.split("\n")[0]) if !@f
179
+ compile_exp1
180
+ raise("error at: " + @i.rest.split("\n")[0]) if !@f
181
+ @i.scan /\s*/; s=';'; l=s.length;
182
+ @f = (@i.peek(l) == s) ? (@t=s; @i.pos += l) : nil
183
+ raise("error at: " + @i.rest.split("\n")[0]) if !@f
184
+ @o.print 'end'
185
+ @o.print "\n"
186
+ end
187
+ end while false
188
+ end
189
+ def compile_program
190
+ begin
191
+ @i.scan /\s*/; s='.syntax'; l=s.length;
192
+ @f = (@i.peek(l) == s) ? (@t=s; @i.pos += l) : nil
193
+ if @f
194
+ @i.scan /\s*/; @f = @t = @i.scan /[A-Za-z]+[A-Za-z0-9_]+/
195
+ raise("error at: " + @i.rest.split("\n")[0]) if !@f
196
+ @o.print '#!/usr/bin/env ruby'
197
+ @o.print "\n"
198
+ @o.print 'require "strscan"'
199
+ @o.print "\n"
200
+ @o.print 'class '
201
+ @o.print @t
202
+ @o.print "\n"
203
+ @o.print 'def compile(str, out)'
204
+ @o.print "\n"
205
+ @o.print '@i, @o = StringScanner.new(str), out'
206
+ @o.print "\n"
207
+ @o.print 'compile_program'
208
+ @o.print "\n"
209
+ @o.print 'end'
210
+ @o.print "\n"
211
+ begin
212
+ compile_rule
213
+ end while @f
214
+ @f = true
215
+ raise("error at: " + @i.rest.split("\n")[0]) if !@f
216
+ @i.scan /\s*/; s='.end'; l=s.length;
217
+ @f = (@i.peek(l) == s) ? (@t=s; @i.pos += l) : nil
218
+ raise("error at: " + @i.rest.split("\n")[0]) if !@f
219
+ @o.print 'end'
220
+ @o.print "\n"
221
+ @o.print 'begin; puts("Use: " + $0 + " <in> <out>"); exit; end if ARGV.length != 2'
222
+ @o.print "\n"
223
+ @o.print 'File.open(ARGV[1], "w") {|f| RMetaII.new.compile(File.read(ARGV[0]), f)}'
224
+ @o.print "\n"
225
+ end
226
+ end while false
227
+ end
228
+ end
229
+ begin; puts("Use: " + $0 + " <in> <out>"); exit; end if ARGV.length != 2
230
+ File.open(ARGV[1], "w") {|f| RMetaII.new.compile(File.read(ARGV[0]), f)}
@@ -0,0 +1,230 @@
1
+ #!/usr/bin/env ruby
2
+ require "strscan"
3
+ class RMetaII
4
+ def compile(str, out)
5
+ @i, @o = StringScanner.new(str), out
6
+ compile_program
7
+ end
8
+ def compile_outarg
9
+ begin
10
+ @i.scan /\s*/; s='$'; l=s.length;
11
+ @f = (@i.peek(l) == s) ? (@t=s; @i.pos += l) : nil
12
+ if @f
13
+ @o.print '@o.print @t'
14
+ @o.print "\n"
15
+ end
16
+ break if @f
17
+ @i.scan /\s*/; @f = @t = @i.scan /\047[^\047]*\047/
18
+ if @f
19
+ @o.print '@o.print '
20
+ @o.print @t
21
+ @o.print "\n"
22
+ end
23
+ end while false
24
+ end
25
+ def compile_out
26
+ begin
27
+ @i.scan /\s*/; s='<'; l=s.length;
28
+ @f = (@i.peek(l) == s) ? (@t=s; @i.pos += l) : nil
29
+ if @f
30
+ begin
31
+ compile_outarg
32
+ end while @f
33
+ @f = true
34
+ raise("error at: " + @i.rest.split("\n")[0]) if !@f
35
+ @i.scan /\s*/; s='>'; l=s.length;
36
+ @f = (@i.peek(l) == s) ? (@t=s; @i.pos += l) : nil
37
+ raise("error at: " + @i.rest.split("\n")[0]) if !@f
38
+ @o.print '@o.print "\n"'
39
+ @o.print "\n"
40
+ end
41
+ end while false
42
+ end
43
+ def compile_exp3
44
+ begin
45
+ @i.scan /\s*/; @f = @t = @i.scan /[A-Za-z]+[A-Za-z0-9_]+/
46
+ if @f
47
+ @o.print 'compile_'
48
+ @o.print @t
49
+ @o.print "\n"
50
+ end
51
+ break if @f
52
+ @i.scan /\s*/; @f = @t = @i.scan /\047[^\047]*\047/
53
+ if @f
54
+ @o.print '@i.scan /\s*/; s='
55
+ @o.print @t
56
+ @o.print '; l=s.length;'
57
+ @o.print "\n"
58
+ @o.print '@f = (@i.peek(l) == s) ? (@t=s; @i.pos += l) : nil'
59
+ @o.print "\n"
60
+ end
61
+ break if @f
62
+ @i.scan /\s*/; s='.id'; l=s.length;
63
+ @f = (@i.peek(l) == s) ? (@t=s; @i.pos += l) : nil
64
+ if @f
65
+ @o.print '@i.scan /\s*/; @f = @t = @i.scan /[A-Za-z]+[A-Za-z0-9_]+/'
66
+ @o.print "\n"
67
+ end
68
+ break if @f
69
+ @i.scan /\s*/; s='.string'; l=s.length;
70
+ @f = (@i.peek(l) == s) ? (@t=s; @i.pos += l) : nil
71
+ if @f
72
+ @o.print '@i.scan /\s*/; @f = @t = @i.scan /\047[^\047]*\047/'
73
+ @o.print "\n"
74
+ end
75
+ break if @f
76
+ @i.scan /\s*/; s='('; l=s.length;
77
+ @f = (@i.peek(l) == s) ? (@t=s; @i.pos += l) : nil
78
+ if @f
79
+ compile_exp1
80
+ raise("error at: " + @i.rest.split("\n")[0]) if !@f
81
+ @i.scan /\s*/; s=')'; l=s.length;
82
+ @f = (@i.peek(l) == s) ? (@t=s; @i.pos += l) : nil
83
+ raise("error at: " + @i.rest.split("\n")[0]) if !@f
84
+ end
85
+ break if @f
86
+ @i.scan /\s*/; s='.e'; l=s.length;
87
+ @f = (@i.peek(l) == s) ? (@t=s; @i.pos += l) : nil
88
+ if @f
89
+ @o.print '@f = true'
90
+ @o.print "\n"
91
+ end
92
+ break if @f
93
+ @i.scan /\s*/; s='*'; l=s.length;
94
+ @f = (@i.peek(l) == s) ? (@t=s; @i.pos += l) : nil
95
+ if @f
96
+ @o.print 'begin'
97
+ @o.print "\n"
98
+ compile_exp3
99
+ raise("error at: " + @i.rest.split("\n")[0]) if !@f
100
+ @o.print 'end while @f'
101
+ @o.print "\n"
102
+ @o.print '@f = true'
103
+ @o.print "\n"
104
+ end
105
+ end while false
106
+ end
107
+ def compile_exp2
108
+ begin
109
+ begin
110
+ compile_exp3
111
+ if @f
112
+ @o.print 'if @f'
113
+ @o.print "\n"
114
+ end
115
+ break if @f
116
+ compile_out
117
+ if @f
118
+ @o.print 'if true'
119
+ @o.print "\n"
120
+ end
121
+ end while false
122
+ if @f
123
+ begin
124
+ begin
125
+ compile_exp3
126
+ if @f
127
+ @o.print 'raise("error at: " + @i.rest.split("\n")[0]) if !@f'
128
+ @o.print "\n"
129
+ end
130
+ break if @f
131
+ compile_out
132
+ if @f
133
+ end
134
+ end while false
135
+ end while @f
136
+ @f = true
137
+ raise("error at: " + @i.rest.split("\n")[0]) if !@f
138
+ @o.print 'end'
139
+ @o.print "\n"
140
+ end
141
+ end while false
142
+ end
143
+ def compile_exp1
144
+ begin
145
+ @o.print 'begin'
146
+ @o.print "\n"
147
+ if true
148
+ compile_exp2
149
+ raise("error at: " + @i.rest.split("\n")[0]) if !@f
150
+ begin
151
+ begin
152
+ @i.scan /\s*/; s='|'; l=s.length;
153
+ @f = (@i.peek(l) == s) ? (@t=s; @i.pos += l) : nil
154
+ if @f
155
+ @o.print 'break if @f'
156
+ @o.print "\n"
157
+ compile_exp2
158
+ raise("error at: " + @i.rest.split("\n")[0]) if !@f
159
+ end
160
+ end while false
161
+ end while @f
162
+ @f = true
163
+ raise("error at: " + @i.rest.split("\n")[0]) if !@f
164
+ @o.print 'end while false'
165
+ @o.print "\n"
166
+ end
167
+ end while false
168
+ end
169
+ def compile_rule
170
+ begin
171
+ @i.scan /\s*/; @f = @t = @i.scan /[A-Za-z]+[A-Za-z0-9_]+/
172
+ if @f
173
+ @o.print 'def compile_'
174
+ @o.print @t
175
+ @o.print "\n"
176
+ @i.scan /\s*/; s='='; l=s.length;
177
+ @f = (@i.peek(l) == s) ? (@t=s; @i.pos += l) : nil
178
+ raise("error at: " + @i.rest.split("\n")[0]) if !@f
179
+ compile_exp1
180
+ raise("error at: " + @i.rest.split("\n")[0]) if !@f
181
+ @i.scan /\s*/; s=';'; l=s.length;
182
+ @f = (@i.peek(l) == s) ? (@t=s; @i.pos += l) : nil
183
+ raise("error at: " + @i.rest.split("\n")[0]) if !@f
184
+ @o.print 'end'
185
+ @o.print "\n"
186
+ end
187
+ end while false
188
+ end
189
+ def compile_program
190
+ begin
191
+ @i.scan /\s*/; s='.syntax'; l=s.length;
192
+ @f = (@i.peek(l) == s) ? (@t=s; @i.pos += l) : nil
193
+ if @f
194
+ @i.scan /\s*/; @f = @t = @i.scan /[A-Za-z]+[A-Za-z0-9_]+/
195
+ raise("error at: " + @i.rest.split("\n")[0]) if !@f
196
+ @o.print '#!/usr/bin/env ruby'
197
+ @o.print "\n"
198
+ @o.print 'require "strscan"'
199
+ @o.print "\n"
200
+ @o.print 'class '
201
+ @o.print @t
202
+ @o.print "\n"
203
+ @o.print 'def compile(str, out)'
204
+ @o.print "\n"
205
+ @o.print '@i, @o = StringScanner.new(str), out'
206
+ @o.print "\n"
207
+ @o.print 'compile_program'
208
+ @o.print "\n"
209
+ @o.print 'end'
210
+ @o.print "\n"
211
+ begin
212
+ compile_rule
213
+ end while @f
214
+ @f = true
215
+ raise("error at: " + @i.rest.split("\n")[0]) if !@f
216
+ @i.scan /\s*/; s='.end'; l=s.length;
217
+ @f = (@i.peek(l) == s) ? (@t=s; @i.pos += l) : nil
218
+ raise("error at: " + @i.rest.split("\n")[0]) if !@f
219
+ @o.print 'end'
220
+ @o.print "\n"
221
+ @o.print 'begin; puts("Use: " + $0 + " <in> <out>"); exit; end if ARGV.length != 2'
222
+ @o.print "\n"
223
+ @o.print 'File.open(ARGV[1], "w") {|f| RMetaII.new.compile(File.read(ARGV[0]), f)}'
224
+ @o.print "\n"
225
+ end
226
+ end while false
227
+ end
228
+ end
229
+ begin; puts("Use: " + $0 + " <in> <out>"); exit; end if ARGV.length != 2
230
+ File.open(ARGV[1], "w") {|f| RMetaII.new.compile(File.read(ARGV[0]), f)}