rhcl 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4f4fec10586c8cabcb7f667f0796e2000cd60bb1
4
- data.tar.gz: 66103b7b1b76c3784a79d28864eb96c1aa091aa7
3
+ metadata.gz: 7912283f77ce4f133e772641e1b258bc77bddadd
4
+ data.tar.gz: 2747ad96ebb024b9534ecd3d54b23d669cb14df5
5
5
  SHA512:
6
- metadata.gz: f3d97b092c6914e3c087825e7983b207e73014fc18c2e5b693d282fc80463a7ef46e76ea124db54284e68b023bf957b8e8b9fca2f0939c835670fe5d657aa5ae
7
- data.tar.gz: b7b82103a49c43892ade824a5db41f3000276cebcc60a7148e04d2120551280bd423b3602b2e32d5b85c82837d624deb7a603984f868e2e5af0dde187ab0beb8
6
+ metadata.gz: 8f2567babef26dd87c6aed2335419cfccf7a75f4a7cab6e4d2a80e595e15fee51709ed49f0a5e5d7433fdeb0814481e37aaafe56004d405fd9de667adba22b9a
7
+ data.tar.gz: a8f8da9aa5a07a54e4609efbc3d2e68110535cb8c34dfbed488fb80af3d5ab5e25f5b0127415c79e2d66f2f063c6e3b27a430d81addb7cd65492fb9433a60b4b
@@ -21,32 +21,44 @@ end
21
21
 
22
22
  def scan
23
23
  tok = nil
24
+ @prev_tokens = []
24
25
 
25
26
  until @ss.eos?
26
27
  if (tok = @ss.scan /\s+/)
27
28
  # nothing to do
28
29
  elsif (tok = @ss.scan(/#/))
29
- @ss.scan_until(/\n/)
30
+ @prev_tokens << tok
31
+ @prev_tokens << @ss.scan_until(/\n/)
32
+ tok = ''
30
33
  elsif (tok = @ss.scan(%r|/|))
31
- case @ss.getch
34
+ @prev_tokens << tok
35
+
36
+ case (tok = @ss.getch)
32
37
  when '/'
33
- @ss.scan_until(/(\n|\z)/)
38
+ @prev_tokens << tok
39
+ @prev_tokens << @ss.scan_until(/(\n|\z)/)
34
40
  when '*'
41
+ @prev_tokens << tok
35
42
  nested = 1
36
43
 
37
44
  until nested.zero?
38
- case @ss.scan_until(%r{(/\*|\*/|\z)})
45
+ case (tok = @ss.scan_until(%r{(/\*|\*/|\z)}))
39
46
  when %r|/\*\z|
47
+ @prev_tokens << tok
40
48
  nested += 1
41
49
  when %r|\*/\z|
50
+ @prev_tokens << tok
42
51
  nested -= 1
43
52
  else
53
+ @prev_tokens << tok
44
54
  break
45
55
  end
46
56
  end
47
57
  else
48
58
  raise "comment expected, got '#{tok}'"
49
59
  end
60
+
61
+ tok = ''
50
62
  elsif (tok = @ss.scan(/-?\d+\.\d+/))
51
63
  yield [:FLOAT, tok.to_f]
52
64
  elsif (tok = @ss.scan(/-?\d+/))
@@ -66,16 +78,18 @@ def scan
66
78
  elsif (tok = @ss.scan(/"/))
67
79
  yield [:STRING, (@ss.scan_until(/("|\z)/) || '').sub(/"\z/, '')]
68
80
  else
69
- identifier = (@ss.scan_until(/(\s|\z)/) || '').sub(/\s\z/, '')
81
+ tok = (@ss.scan_until(/(\s|\z)/) || '').sub(/\s\z/, '')
70
82
  token_type = :IDENTIFIER
71
83
 
72
- if ['true', 'false'].include?(identifier)
73
- identifier = !!(identifier =~ /true/)
84
+ if ['true', 'false'].include?(tok)
85
+ tok = !!(tok =~ /true/)
74
86
  token_type = :BOOL
75
87
  end
76
88
 
77
- yield [token_type, identifier]
89
+ yield [token_type, tok]
78
90
  end
91
+
92
+ @prev_tokens << tok
79
93
  end
80
94
 
81
95
  yield [false, '$end']
@@ -86,6 +100,36 @@ def parse
86
100
  yyparse self, :scan
87
101
  end
88
102
 
103
+ def on_error(error_token_id, error_value, value_stack)
104
+ raise_error(error_value)
105
+ end
106
+
107
+ def raise_error(error_value)
108
+ header = "parse error on value: #{error_value}\n"
109
+ prev = (@prev_tokens || [])
110
+ prev = prev.empty? ? '' : prev.join + ' '
111
+ errmsg = prev + "__#{error_value}__"
112
+
113
+ if @ss and @ss.rest?
114
+ errmsg << ' ' + @ss.rest
115
+ end
116
+
117
+ lines = errmsg.lines
118
+ err_num = prev.count("\n")
119
+ from_num = err_num - 3
120
+ from_num = 0 if from_num < 0
121
+ to_num = err_num + 3
122
+ digit_num = lines.count.to_s.length
123
+
124
+ errmsg = lines.each_with_index.map {|line, i|
125
+ mark = (i == err_num) ? '*' : ' '
126
+ '%s %*d: %s' % [mark, digit_num, i + 1, line]
127
+ }.slice(from_num..to_num).join
128
+
129
+ raise Racc::ParseError, header + errmsg
130
+ end
131
+ private :raise_error
132
+
89
133
  def self.parse(obj)
90
134
  self.new(obj).parse
91
135
  end
data/lib/rhcl/parse.y CHANGED
@@ -112,32 +112,44 @@ end
112
112
 
113
113
  def scan
114
114
  tok = nil
115
+ @prev_tokens = []
115
116
 
116
117
  until @ss.eos?
117
118
  if (tok = @ss.scan /\s+/)
118
119
  # nothing to do
119
120
  elsif (tok = @ss.scan(/#/))
120
- @ss.scan_until(/\n/)
121
+ @prev_tokens << tok
122
+ @prev_tokens << @ss.scan_until(/\n/)
123
+ tok = ''
121
124
  elsif (tok = @ss.scan(%r|/|))
122
- case @ss.getch
125
+ @prev_tokens << tok
126
+
127
+ case (tok = @ss.getch)
123
128
  when '/'
124
- @ss.scan_until(/(\n|\z)/)
129
+ @prev_tokens << tok
130
+ @prev_tokens << @ss.scan_until(/(\n|\z)/)
125
131
  when '*'
132
+ @prev_tokens << tok
126
133
  nested = 1
127
134
 
128
135
  until nested.zero?
129
- case @ss.scan_until(%r{(/\*|\*/|\z)})
136
+ case (tok = @ss.scan_until(%r{(/\*|\*/|\z)}))
130
137
  when %r|/\*\z|
138
+ @prev_tokens << tok
131
139
  nested += 1
132
140
  when %r|\*/\z|
141
+ @prev_tokens << tok
133
142
  nested -= 1
134
143
  else
144
+ @prev_tokens << tok
135
145
  break
136
146
  end
137
147
  end
138
148
  else
139
149
  raise "comment expected, got '#{tok}'"
140
150
  end
151
+
152
+ tok = ''
141
153
  elsif (tok = @ss.scan(/-?\d+\.\d+/))
142
154
  yield [:FLOAT, tok.to_f]
143
155
  elsif (tok = @ss.scan(/-?\d+/))
@@ -157,16 +169,18 @@ def scan
157
169
  elsif (tok = @ss.scan(/"/))
158
170
  yield [:STRING, (@ss.scan_until(/("|\z)/) || '').sub(/"\z/, '')]
159
171
  else
160
- identifier = (@ss.scan_until(/(\s|\z)/) || '').sub(/\s\z/, '')
172
+ tok = (@ss.scan_until(/(\s|\z)/) || '').sub(/\s\z/, '')
161
173
  token_type = :IDENTIFIER
162
174
 
163
- if ['true', 'false'].include?(identifier)
164
- identifier = !!(identifier =~ /true/)
175
+ if ['true', 'false'].include?(tok)
176
+ tok = !!(tok =~ /true/)
165
177
  token_type = :BOOL
166
178
  end
167
179
 
168
- yield [token_type, identifier]
180
+ yield [token_type, tok]
169
181
  end
182
+
183
+ @prev_tokens << tok
170
184
  end
171
185
 
172
186
  yield [false, '$end']
@@ -177,6 +191,36 @@ def parse
177
191
  yyparse self, :scan
178
192
  end
179
193
 
194
+ def on_error(error_token_id, error_value, value_stack)
195
+ raise_error(error_value)
196
+ end
197
+
198
+ def raise_error(error_value)
199
+ header = "parse error on value: #{error_value}\n"
200
+ prev = (@prev_tokens || [])
201
+ prev = prev.empty? ? '' : prev.join + ' '
202
+ errmsg = prev + "__#{error_value}__"
203
+
204
+ if @ss and @ss.rest?
205
+ errmsg << ' ' + @ss.rest
206
+ end
207
+
208
+ lines = errmsg.lines
209
+ err_num = prev.count("\n")
210
+ from_num = err_num - 3
211
+ from_num = 0 if from_num < 0
212
+ to_num = err_num + 3
213
+ digit_num = lines.count.to_s.length
214
+
215
+ errmsg = lines.each_with_index.map {|line, i|
216
+ mark = (i == err_num) ? '*' : ' '
217
+ '%s %*d: %s' % [mark, digit_num, i + 1, line]
218
+ }.slice(from_num..to_num).join
219
+
220
+ raise Racc::ParseError, header + errmsg
221
+ end
222
+ private :raise_error
223
+
180
224
  def self.parse(obj)
181
225
  self.new(obj).parse
182
226
  end
data/lib/rhcl/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Rhcl
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rhcl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Genki Sugawara
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-13 00:00:00.000000000 Z
11
+ date: 2014-08-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: deep_merge