yacc_shave 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +24 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +32 -0
- data/lib/yacc_shave/parser/lexer.rb +109 -0
- data/lib/yacc_shave/parser/parser.rb +233 -0
- data/lib/yacc_shave/parser/yacc_shave.rex +39 -0
- data/lib/yacc_shave/parser/yacc_shave.y +69 -0
- data/lib/yacc_shave/parser.rb +1 -0
- data/lib/yacc_shave/version.rb +3 -0
- data/lib/yacc_shave.rb +5 -0
- data/spec/lexer_spec.rb +23 -0
- data/spec/spec_helper.rb +19 -0
- data/yacc_shave.gemspec +26 -0
- metadata +109 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 981b32444b27edd0a4e069946dce8f3a6b30fdce
|
4
|
+
data.tar.gz: b54ddcccb296e3aa3508af38145fdc05edd17bd5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9e144b1b41a8d536e7c97ca02528712e6f355ebd97e8967771c9fe56a603edaa5d2a53c95e107ec591021e6b8b3d4853ff7a8940dcc6710c0dddd856a77f0076
|
7
|
+
data.tar.gz: b9ea2884d0799e7dc152c2ffa48eb421a576a6effdb780bf402e1d4c20d8492a9063a7346a3b4d6b12a146b034e1e920eba211e49844e04c20c7a9026b38d064
|
data/.gitignore
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
23
|
+
.rspec
|
24
|
+
bin/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Jan Schulte
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# YaccShave
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'yacc_shave'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install yacc_shave
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it ( https://github.com/[my-github-username]/yacc_shave/fork )
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require 'rspec/core/rake_task'
|
3
|
+
RSpec::Core::RakeTask.new(:test)
|
4
|
+
|
5
|
+
|
6
|
+
namespace :rexical do
|
7
|
+
desc "clean the generated lexer"
|
8
|
+
task :clean do
|
9
|
+
`rm lib/yacc_shave/parser/lexer.rb`
|
10
|
+
end
|
11
|
+
|
12
|
+
desc "Regenerate the lexer grammar"
|
13
|
+
task :regenerate do
|
14
|
+
`rex lib/yacc_shave/parser/yacc_shave.rex -o lib/yacc_shave/parser/lexer.rb`
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
namespace :racc do
|
19
|
+
desc "It cleans the generated parser"
|
20
|
+
task :clean do
|
21
|
+
`rm lib/yacc_shave/parser/parser.rb`
|
22
|
+
end
|
23
|
+
|
24
|
+
desc "Regenerate the parser grammar"
|
25
|
+
task :regenerate do
|
26
|
+
`racc lib/yacc_shave/parser/yacc_shave.y -o lib/yacc_shave/parser/parser.rb`
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
task :test => ["rexical:regenerate", "racc:regenerate"]
|
31
|
+
task :default => :test
|
32
|
+
|
@@ -0,0 +1,109 @@
|
|
1
|
+
#--
|
2
|
+
# DO NOT MODIFY!!!!
|
3
|
+
# This file is automatically generated by rex 1.0.5
|
4
|
+
# from lexical definition file "lib/yacc_shave/parser/yacc_shave.rex".
|
5
|
+
#++
|
6
|
+
|
7
|
+
require 'racc/parser'
|
8
|
+
class YaccShave::Parser < Racc::Parser
|
9
|
+
require 'strscan'
|
10
|
+
|
11
|
+
class ScanError < StandardError ; end
|
12
|
+
|
13
|
+
attr_reader :lineno
|
14
|
+
attr_reader :filename
|
15
|
+
attr_accessor :state
|
16
|
+
|
17
|
+
def scan_setup(str)
|
18
|
+
@ss = StringScanner.new(str)
|
19
|
+
@lineno = 1
|
20
|
+
@state = nil
|
21
|
+
end
|
22
|
+
|
23
|
+
def action
|
24
|
+
yield
|
25
|
+
end
|
26
|
+
|
27
|
+
def scan_str(str)
|
28
|
+
scan_setup(str)
|
29
|
+
do_parse
|
30
|
+
end
|
31
|
+
alias :scan :scan_str
|
32
|
+
|
33
|
+
def load_file( filename )
|
34
|
+
@filename = filename
|
35
|
+
open(filename, "r") do |f|
|
36
|
+
scan_setup(f.read)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def scan_file( filename )
|
41
|
+
load_file(filename)
|
42
|
+
do_parse
|
43
|
+
end
|
44
|
+
|
45
|
+
|
46
|
+
def next_token
|
47
|
+
return if @ss.eos?
|
48
|
+
|
49
|
+
# skips empty actions
|
50
|
+
until token = _next_token or @ss.eos?; end
|
51
|
+
token
|
52
|
+
end
|
53
|
+
|
54
|
+
def _next_token
|
55
|
+
text = @ss.peek(1)
|
56
|
+
@lineno += 1 if text == "\n"
|
57
|
+
token = case @state
|
58
|
+
when nil
|
59
|
+
case
|
60
|
+
when (text = @ss.scan(/\d+/))
|
61
|
+
action { [:INTEGER, text.to_i] }
|
62
|
+
|
63
|
+
when (text = @ss.scan(/[a-z_?!]+/))
|
64
|
+
action { [:IDENTIFIER, text]}
|
65
|
+
|
66
|
+
when (text = @ss.scan(/\+/))
|
67
|
+
action { [:ADD, text] }
|
68
|
+
|
69
|
+
when (text = @ss.scan(/\=/))
|
70
|
+
action { [:ASSIGN, text]}
|
71
|
+
|
72
|
+
when (text = @ss.scan(/\{/))
|
73
|
+
action { [:LCBRA, text]}
|
74
|
+
|
75
|
+
when (text = @ss.scan(/\}/))
|
76
|
+
action { [:RCBRA, text]}
|
77
|
+
|
78
|
+
when (text = @ss.scan(/\,/))
|
79
|
+
action { [:COMMA, text]}
|
80
|
+
|
81
|
+
when (text = @ss.scan(/\./))
|
82
|
+
action { [:DOT, text]}
|
83
|
+
|
84
|
+
when (text = @ss.scan(/\n/))
|
85
|
+
action { [:NEWLINE, text]}
|
86
|
+
|
87
|
+
when (text = @ss.scan(/./))
|
88
|
+
;
|
89
|
+
|
90
|
+
else
|
91
|
+
text = @ss.string[@ss.pos .. -1]
|
92
|
+
raise ScanError, "can not match: '" + text + "'"
|
93
|
+
end # if
|
94
|
+
|
95
|
+
else
|
96
|
+
raise ScanError, "undefined state: '" + state.to_s + "'"
|
97
|
+
end # case state
|
98
|
+
token
|
99
|
+
end # def _next_token
|
100
|
+
|
101
|
+
def tokenize(code)
|
102
|
+
scan_setup(code)
|
103
|
+
tokens = []
|
104
|
+
while token = next_token
|
105
|
+
tokens << token
|
106
|
+
end
|
107
|
+
tokens
|
108
|
+
end
|
109
|
+
end # class
|
@@ -0,0 +1,233 @@
|
|
1
|
+
#
|
2
|
+
# DO NOT MODIFY!!!!
|
3
|
+
# This file is automatically generated by Racc 1.4.11
|
4
|
+
# from Racc grammer file "".
|
5
|
+
#
|
6
|
+
|
7
|
+
require 'racc/parser.rb'
|
8
|
+
|
9
|
+
require_relative 'lexer'
|
10
|
+
|
11
|
+
module YaccShave
|
12
|
+
class Parser < Racc::Parser
|
13
|
+
|
14
|
+
module_eval(<<'...end yacc_shave.y/module_eval...', 'yacc_shave.y', 52)
|
15
|
+
|
16
|
+
def filename
|
17
|
+
@filename
|
18
|
+
end
|
19
|
+
|
20
|
+
alias_method :parse_string, :scan_str
|
21
|
+
|
22
|
+
def pre_exe
|
23
|
+
[]
|
24
|
+
end
|
25
|
+
|
26
|
+
def on_error(t, val, vstack)
|
27
|
+
raise ParseError, sprintf("\nparse error on value %s (%s) #{@filename}:#{@line}",
|
28
|
+
val.inspect, token_to_str(t) || '?')
|
29
|
+
end
|
30
|
+
|
31
|
+
...end yacc_shave.y/module_eval...
|
32
|
+
##### State transition tables begin ###
|
33
|
+
|
34
|
+
racc_action_table = [
|
35
|
+
4, 4, 4, 5, 5, 5, 4, 11, 13, 5,
|
36
|
+
14, 15, 16, 15, 15 ]
|
37
|
+
|
38
|
+
racc_action_check = [
|
39
|
+
0, 12, 14, 0, 12, 14, 15, 1, 3, 15,
|
40
|
+
5, 10, 11, 17, 18 ]
|
41
|
+
|
42
|
+
racc_action_pointer = [
|
43
|
+
-2, 7, nil, 4, nil, 4, nil, nil, nil, nil,
|
44
|
+
8, 12, -1, nil, 0, 4, nil, 10, 11, nil ]
|
45
|
+
|
46
|
+
racc_action_default = [
|
47
|
+
-2, -17, -1, -3, -4, -5, -9, -10, -11, -12,
|
48
|
+
-14, -17, -17, -16, -17, -17, 20, -15, -6, -13 ]
|
49
|
+
|
50
|
+
racc_goto_table = [
|
51
|
+
10, 1, 2, 3, 12, nil, nil, nil, nil, nil,
|
52
|
+
nil, nil, 17, nil, 18, 19 ]
|
53
|
+
|
54
|
+
racc_goto_check = [
|
55
|
+
7, 1, 2, 3, 10, nil, nil, nil, nil, nil,
|
56
|
+
nil, nil, 7, nil, 7, 7 ]
|
57
|
+
|
58
|
+
racc_goto_pointer = [
|
59
|
+
nil, 1, 2, 3, nil, nil, nil, 0, nil, nil,
|
60
|
+
1 ]
|
61
|
+
|
62
|
+
racc_goto_default = [
|
63
|
+
nil, nil, nil, nil, 6, 8, 9, nil, nil, 7,
|
64
|
+
nil ]
|
65
|
+
|
66
|
+
racc_reduce_table = [
|
67
|
+
0, 0, :racc_error,
|
68
|
+
1, 13, :_reduce_none,
|
69
|
+
0, 14, :_reduce_2,
|
70
|
+
1, 14, :_reduce_3,
|
71
|
+
1, 16, :_reduce_4,
|
72
|
+
1, 17, :_reduce_5,
|
73
|
+
3, 18, :_reduce_6,
|
74
|
+
1, 20, :_reduce_7,
|
75
|
+
3, 20, :_reduce_8,
|
76
|
+
1, 19, :_reduce_none,
|
77
|
+
1, 19, :_reduce_none,
|
78
|
+
1, 19, :_reduce_none,
|
79
|
+
1, 19, :_reduce_none,
|
80
|
+
3, 21, :_reduce_13,
|
81
|
+
1, 15, :_reduce_14,
|
82
|
+
3, 15, :_reduce_15,
|
83
|
+
1, 22, :_reduce_none ]
|
84
|
+
|
85
|
+
racc_reduce_n = 17
|
86
|
+
|
87
|
+
racc_shift_n = 20
|
88
|
+
|
89
|
+
racc_token_table = {
|
90
|
+
false => 0,
|
91
|
+
:error => 1,
|
92
|
+
:INTEGER => 2,
|
93
|
+
:ADD => 3,
|
94
|
+
:NEWLINE => 4,
|
95
|
+
:IDENTIFIER => 5,
|
96
|
+
:ASSIGN => 6,
|
97
|
+
:LCBRA => 7,
|
98
|
+
:RCBRA => 8,
|
99
|
+
:COMMA => 9,
|
100
|
+
:DOT => 10,
|
101
|
+
:SUBTRACT => 11 }
|
102
|
+
|
103
|
+
racc_nt_base = 12
|
104
|
+
|
105
|
+
racc_use_result_var = false
|
106
|
+
|
107
|
+
Racc_arg = [
|
108
|
+
racc_action_table,
|
109
|
+
racc_action_check,
|
110
|
+
racc_action_default,
|
111
|
+
racc_action_pointer,
|
112
|
+
racc_goto_table,
|
113
|
+
racc_goto_check,
|
114
|
+
racc_goto_default,
|
115
|
+
racc_goto_pointer,
|
116
|
+
racc_nt_base,
|
117
|
+
racc_reduce_table,
|
118
|
+
racc_token_table,
|
119
|
+
racc_shift_n,
|
120
|
+
racc_reduce_n,
|
121
|
+
racc_use_result_var ]
|
122
|
+
|
123
|
+
Racc_token_to_s_table = [
|
124
|
+
"$end",
|
125
|
+
"error",
|
126
|
+
"INTEGER",
|
127
|
+
"ADD",
|
128
|
+
"NEWLINE",
|
129
|
+
"IDENTIFIER",
|
130
|
+
"ASSIGN",
|
131
|
+
"LCBRA",
|
132
|
+
"RCBRA",
|
133
|
+
"COMMA",
|
134
|
+
"DOT",
|
135
|
+
"SUBTRACT",
|
136
|
+
"$start",
|
137
|
+
"root",
|
138
|
+
"program",
|
139
|
+
"expressions",
|
140
|
+
"number",
|
141
|
+
"variable_access",
|
142
|
+
"variable_assignment",
|
143
|
+
"expression",
|
144
|
+
"elements",
|
145
|
+
"binary_operation",
|
146
|
+
"terminator" ]
|
147
|
+
|
148
|
+
Racc_debug_parser = false
|
149
|
+
|
150
|
+
##### State transition tables end #####
|
151
|
+
|
152
|
+
# reduce 0 omitted
|
153
|
+
|
154
|
+
# reduce 1 omitted
|
155
|
+
|
156
|
+
module_eval(<<'.,.,', 'yacc_shave.y', 19)
|
157
|
+
def _reduce_2(val, _values)
|
158
|
+
AST::Program.new( filename, lineno, [])
|
159
|
+
end
|
160
|
+
.,.,
|
161
|
+
|
162
|
+
module_eval(<<'.,.,', 'yacc_shave.y', 20)
|
163
|
+
def _reduce_3(val, _values)
|
164
|
+
AST::Program.new( filename, lineno, val[0])
|
165
|
+
end
|
166
|
+
.,.,
|
167
|
+
|
168
|
+
module_eval(<<'.,.,', 'yacc_shave.y', 22)
|
169
|
+
def _reduce_4(val, _values)
|
170
|
+
AST::IntegerNode.new( filename, lineno, val[0])
|
171
|
+
end
|
172
|
+
.,.,
|
173
|
+
|
174
|
+
module_eval(<<'.,.,', 'yacc_shave.y', 24)
|
175
|
+
def _reduce_5(val, _values)
|
176
|
+
AST::VariableAccessNode.new( filename, lineno, val[0])
|
177
|
+
end
|
178
|
+
.,.,
|
179
|
+
|
180
|
+
module_eval(<<'.,.,', 'yacc_shave.y', 26)
|
181
|
+
def _reduce_6(val, _values)
|
182
|
+
AST::VariableAssignmentNode.new( filename, lineno, val[0], val[2])
|
183
|
+
end
|
184
|
+
.,.,
|
185
|
+
|
186
|
+
module_eval(<<'.,.,', 'yacc_shave.y', 28)
|
187
|
+
def _reduce_7(val, _values)
|
188
|
+
[val[0]]
|
189
|
+
end
|
190
|
+
.,.,
|
191
|
+
|
192
|
+
module_eval(<<'.,.,', 'yacc_shave.y', 29)
|
193
|
+
def _reduce_8(val, _values)
|
194
|
+
val[0] << val[2]
|
195
|
+
end
|
196
|
+
.,.,
|
197
|
+
|
198
|
+
# reduce 9 omitted
|
199
|
+
|
200
|
+
# reduce 10 omitted
|
201
|
+
|
202
|
+
# reduce 11 omitted
|
203
|
+
|
204
|
+
# reduce 12 omitted
|
205
|
+
|
206
|
+
module_eval(<<'.,.,', 'yacc_shave.y', 37)
|
207
|
+
def _reduce_13(val, _values)
|
208
|
+
AST::AddNode.new( filename, lineno, val[0], val[2])
|
209
|
+
end
|
210
|
+
.,.,
|
211
|
+
|
212
|
+
module_eval(<<'.,.,', 'yacc_shave.y', 39)
|
213
|
+
def _reduce_14(val, _values)
|
214
|
+
[val[0]]
|
215
|
+
end
|
216
|
+
.,.,
|
217
|
+
|
218
|
+
module_eval(<<'.,.,', 'yacc_shave.y', 40)
|
219
|
+
def _reduce_15(val, _values)
|
220
|
+
val[0] << val[2]
|
221
|
+
end
|
222
|
+
.,.,
|
223
|
+
|
224
|
+
# reduce 16 omitted
|
225
|
+
|
226
|
+
def _reduce_none(val, _values)
|
227
|
+
val[0]
|
228
|
+
end
|
229
|
+
|
230
|
+
end # class Parser
|
231
|
+
end # module YaccShave
|
232
|
+
|
233
|
+
|
@@ -0,0 +1,39 @@
|
|
1
|
+
class YaccShave::Parser
|
2
|
+
macro
|
3
|
+
#macros go here
|
4
|
+
rule
|
5
|
+
|
6
|
+
# Literals
|
7
|
+
\d+ { [:INTEGER, text.to_i] }
|
8
|
+
|
9
|
+
# Identifier
|
10
|
+
[a-z_?!]+ { [:IDENTIFIER, text]}
|
11
|
+
|
12
|
+
# Operators
|
13
|
+
\+ { [:ADD, text] }
|
14
|
+
\= { [:ASSIGN, text]}
|
15
|
+
\{ { [:LCBRA, text]}
|
16
|
+
\} { [:RCBRA, text]}
|
17
|
+
\, { [:COMMA, text]}
|
18
|
+
\. { [:DOT, text]}
|
19
|
+
|
20
|
+
|
21
|
+
|
22
|
+
# NewLines
|
23
|
+
\n { [:NEWLINE, text]}
|
24
|
+
|
25
|
+
# CatchAll
|
26
|
+
. # no action
|
27
|
+
|
28
|
+
inner
|
29
|
+
# here we put any ruby code we want to extend our lexer with.
|
30
|
+
# for example, our own tokenize method.
|
31
|
+
def tokenize(code)
|
32
|
+
scan_setup(code)
|
33
|
+
tokens = []
|
34
|
+
while token = next_token
|
35
|
+
tokens << token
|
36
|
+
end
|
37
|
+
tokens
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
class YaccShave::Parser
|
2
|
+
token INTEGER
|
3
|
+
token ADD
|
4
|
+
token NEWLINE
|
5
|
+
token IDENTIFIER
|
6
|
+
token ASSIGN
|
7
|
+
token LCBRA
|
8
|
+
token RCBRA
|
9
|
+
token COMMA
|
10
|
+
token DOT
|
11
|
+
|
12
|
+
prechigh
|
13
|
+
left ADD SUBTRACT
|
14
|
+
preclow
|
15
|
+
|
16
|
+
options no_result_var
|
17
|
+
rule
|
18
|
+
root : program
|
19
|
+
|
20
|
+
program : /* nothing */ { AST::Program.new( filename, lineno, [])}
|
21
|
+
| expressions { AST::Program.new( filename, lineno, val[0])}
|
22
|
+
|
23
|
+
number : INTEGER { AST::IntegerNode.new( filename, lineno, val[0])}
|
24
|
+
|
25
|
+
variable_access : IDENTIFIER { AST::VariableAccessNode.new( filename, lineno, val[0])}
|
26
|
+
|
27
|
+
variable_assignment : IDENTIFIER ASSIGN expression {AST::VariableAssignmentNode.new( filename, lineno, val[0], val[2]) }
|
28
|
+
|
29
|
+
elements : expression { [val[0]] }
|
30
|
+
| elements COMMA expression { val[0] << val[2] }
|
31
|
+
|
32
|
+
expression : number
|
33
|
+
| binary_operation
|
34
|
+
| variable_access
|
35
|
+
| variable_assignment
|
36
|
+
|
37
|
+
|
38
|
+
binary_operation : expression ADD expression {AST::AddNode.new( filename, lineno, val[0], val[2])}
|
39
|
+
|
40
|
+
expressions : expression { [val[0]] }
|
41
|
+
| expressions terminator expression { val[0] << val[2] }
|
42
|
+
|
43
|
+
terminator : NEWLINE
|
44
|
+
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
---- header ----
|
49
|
+
require_relative 'lexer'
|
50
|
+
|
51
|
+
---- inner ----
|
52
|
+
|
53
|
+
def filename
|
54
|
+
@filename
|
55
|
+
end
|
56
|
+
|
57
|
+
alias_method :parse_string, :scan_str
|
58
|
+
|
59
|
+
def pre_exe
|
60
|
+
[]
|
61
|
+
end
|
62
|
+
|
63
|
+
def on_error(t, val, vstack)
|
64
|
+
raise ParseError, sprintf("\nparse error on value %s (%s) #{@filename}:#{@line}",
|
65
|
+
val.inspect, token_to_str(t) || '?')
|
66
|
+
end
|
67
|
+
|
68
|
+
---- footer ----
|
69
|
+
|
@@ -0,0 +1 @@
|
|
1
|
+
require_relative "parser/parser"
|
data/lib/yacc_shave.rb
ADDED
data/spec/lexer_spec.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
puts "PARSER: #{YaccShave.inspect}"
|
4
|
+
|
5
|
+
describe YaccShave::Parser, '#tokenize' do
|
6
|
+
def tokenize(string)
|
7
|
+
YaccShave::Parser.new.tokenize(string)
|
8
|
+
end
|
9
|
+
it 'tokenizes an addition' do
|
10
|
+
expect(tokenize('43 + 3')).to eq([
|
11
|
+
[:INTEGER, 43],
|
12
|
+
[:ADD, '+'],
|
13
|
+
[:INTEGER, 3]
|
14
|
+
])
|
15
|
+
end
|
16
|
+
it 'tokenizes a pattern match' do
|
17
|
+
expect(tokenize('a = 3')).to eq([
|
18
|
+
[:IDENTIFIER, 'a'],
|
19
|
+
[:ASSIGN, '='],
|
20
|
+
[:INTEGER, 3]
|
21
|
+
])
|
22
|
+
end
|
23
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
RSpec.configure do |config|
|
8
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
9
|
+
config.run_all_when_everything_filtered = true
|
10
|
+
config.filter_run :focus
|
11
|
+
|
12
|
+
# Run specs in random order to surface order dependencies. If you find an
|
13
|
+
# order dependency and want to debug it, you can fix the order by providing
|
14
|
+
# the seed, which is printed after each run.
|
15
|
+
# --seed 1234
|
16
|
+
config.order = 'random'
|
17
|
+
end
|
18
|
+
#$: << 'lib'
|
19
|
+
require 'yacc_shave'
|
data/yacc_shave.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'yacc_shave/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "yacc_shave"
|
8
|
+
spec.version = YaccShave::VERSION
|
9
|
+
spec.authors = ["Jan Schulte"]
|
10
|
+
spec.email = ["hello@unexpected-co.de"]
|
11
|
+
spec.summary = %q{Shaving yaccs}
|
12
|
+
spec.description = %q{Shaving yaccs}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
22
|
+
spec.add_development_dependency "rexical"
|
23
|
+
spec.add_runtime_dependency "racc"
|
24
|
+
spec.add_development_dependency "rake"
|
25
|
+
spec.add_development_dependency "rspec"
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: yacc_shave
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jan Schulte
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2014-04-14 00:00:00 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
prerelease: false
|
17
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: "1.6"
|
22
|
+
type: :development
|
23
|
+
version_requirements: *id001
|
24
|
+
- !ruby/object:Gem::Dependency
|
25
|
+
name: rexical
|
26
|
+
prerelease: false
|
27
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
28
|
+
requirements:
|
29
|
+
- &id003
|
30
|
+
- ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: "0"
|
33
|
+
type: :development
|
34
|
+
version_requirements: *id002
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: racc
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- *id003
|
41
|
+
type: :runtime
|
42
|
+
version_requirements: *id004
|
43
|
+
- !ruby/object:Gem::Dependency
|
44
|
+
name: rake
|
45
|
+
prerelease: false
|
46
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- *id003
|
49
|
+
type: :development
|
50
|
+
version_requirements: *id005
|
51
|
+
- !ruby/object:Gem::Dependency
|
52
|
+
name: rspec
|
53
|
+
prerelease: false
|
54
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- *id003
|
57
|
+
type: :development
|
58
|
+
version_requirements: *id006
|
59
|
+
description: Shaving yaccs
|
60
|
+
email:
|
61
|
+
- hello@unexpected-co.de
|
62
|
+
executables: []
|
63
|
+
|
64
|
+
extensions: []
|
65
|
+
|
66
|
+
extra_rdoc_files: []
|
67
|
+
|
68
|
+
files:
|
69
|
+
- .gitignore
|
70
|
+
- Gemfile
|
71
|
+
- LICENSE.txt
|
72
|
+
- README.md
|
73
|
+
- Rakefile
|
74
|
+
- lib/yacc_shave.rb
|
75
|
+
- lib/yacc_shave/parser.rb
|
76
|
+
- lib/yacc_shave/parser/lexer.rb
|
77
|
+
- lib/yacc_shave/parser/parser.rb
|
78
|
+
- lib/yacc_shave/parser/yacc_shave.rex
|
79
|
+
- lib/yacc_shave/parser/yacc_shave.y
|
80
|
+
- lib/yacc_shave/version.rb
|
81
|
+
- spec/lexer_spec.rb
|
82
|
+
- spec/spec_helper.rb
|
83
|
+
- yacc_shave.gemspec
|
84
|
+
homepage: ""
|
85
|
+
licenses:
|
86
|
+
- MIT
|
87
|
+
metadata: {}
|
88
|
+
|
89
|
+
post_install_message:
|
90
|
+
rdoc_options: []
|
91
|
+
|
92
|
+
require_paths:
|
93
|
+
- lib
|
94
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- *id003
|
97
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- *id003
|
100
|
+
requirements: []
|
101
|
+
|
102
|
+
rubyforge_project:
|
103
|
+
rubygems_version: 2.2.2
|
104
|
+
signing_key:
|
105
|
+
specification_version: 4
|
106
|
+
summary: Shaving yaccs
|
107
|
+
test_files:
|
108
|
+
- spec/lexer_spec.rb
|
109
|
+
- spec/spec_helper.rb
|