sportdb-parser 0.7.0 → 0.7.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.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +1 -1
  3. data/Manifest.txt +18 -4
  4. data/Rakefile +0 -1
  5. data/lib/sportdb/parser/lexer-on_goal.rb +172 -0
  6. data/lib/sportdb/parser/lexer-on_group_def.rb +31 -0
  7. data/lib/sportdb/parser/lexer-on_prop_lineup.rb +79 -0
  8. data/lib/sportdb/parser/lexer-on_prop_misc.rb +110 -0
  9. data/lib/sportdb/parser/lexer-on_prop_penalties.rb +40 -0
  10. data/lib/sportdb/parser/lexer-on_round_def.rb +37 -0
  11. data/lib/sportdb/parser/lexer-on_top.rb +125 -0
  12. data/lib/sportdb/parser/lexer-prep_doc.rb +131 -0
  13. data/lib/sportdb/parser/lexer-prep_line.rb +63 -0
  14. data/lib/sportdb/parser/lexer-tokenize.rb +449 -0
  15. data/lib/sportdb/parser/lexer.rb +133 -1363
  16. data/lib/sportdb/parser/lexer_buffer.rb +8 -37
  17. data/lib/sportdb/parser/lexer_token.rb +126 -0
  18. data/lib/sportdb/parser/parser.rb +1104 -1401
  19. data/lib/sportdb/parser/parser_runtime.rb +379 -0
  20. data/lib/sportdb/parser/racc_parser.rb +36 -32
  21. data/lib/sportdb/parser/racc_tree.rb +65 -98
  22. data/lib/sportdb/parser/token-date--helpers.rb +130 -0
  23. data/lib/sportdb/parser/token-date--names.rb +108 -0
  24. data/lib/sportdb/parser/token-date.rb +20 -192
  25. data/lib/sportdb/parser/token-date_duration.rb +8 -27
  26. data/lib/sportdb/parser/token-geo.rb +16 -16
  27. data/lib/sportdb/parser/token-goals--helpers.rb +114 -0
  28. data/lib/sportdb/parser/token-goals.rb +103 -249
  29. data/lib/sportdb/parser/token-group.rb +8 -22
  30. data/lib/sportdb/parser/token-prop.rb +138 -124
  31. data/lib/sportdb/parser/token-prop_name.rb +48 -39
  32. data/lib/sportdb/parser/token-round.rb +21 -35
  33. data/lib/sportdb/parser/token-score--helpers.rb +189 -0
  34. data/lib/sportdb/parser/token-score.rb +9 -393
  35. data/lib/sportdb/parser/token-score_full.rb +331 -0
  36. data/lib/sportdb/parser/token-status.rb +44 -46
  37. data/lib/sportdb/parser/token-status_inline.rb +112 -0
  38. data/lib/sportdb/parser/token-text.rb +41 -31
  39. data/lib/sportdb/parser/token-time.rb +29 -26
  40. data/lib/sportdb/parser/token.rb +58 -159
  41. data/lib/sportdb/parser/version.rb +1 -1
  42. data/lib/sportdb/parser.rb +50 -19
  43. metadata +20 -20
  44. data/lib/sportdb/parser/blocktxt.rb +0 -99
  45. data/lib/sportdb/parser/lexer_tty.rb +0 -111
  46. data/lib/sportdb/parser/token-table.rb +0 -149
  47. data/lib/sportdb/parser/token_helpers.rb +0 -92
@@ -0,0 +1,379 @@
1
+
2
+ # frozen_string_literal: true
3
+ #--
4
+ # Copyright (c) 1999-2006 Minero Aoki
5
+ #
6
+ # This program is free software.
7
+ # You can distribute/modify this program under the same terms of ruby.
8
+ #
9
+ # As a special exception, when this code is copied by Racc
10
+ # into a Racc output file, you may use that output file
11
+ # without restriction.
12
+ #++
13
+
14
+
15
+ module Racc
16
+ class ParseError < StandardError; end
17
+ end
18
+ unless defined?(::ParseError)
19
+ ParseError = Racc::ParseError # :nodoc:
20
+ end
21
+
22
+
23
+
24
+ module Racc
25
+ VERSION = '1.8.1'
26
+ Version = VERSION
27
+
28
+
29
+ class Parser
30
+ def _racc_setup
31
+ @yydebug = false unless self.class::Racc_debug_parser
32
+ @yydebug = false unless defined?(@yydebug)
33
+ if @yydebug
34
+ @racc_debug_out = $stderr unless defined?(@racc_debug_out)
35
+ @racc_debug_out ||= $stderr
36
+ end
37
+ arg = self.class::Racc_arg
38
+ arg[13] = true if arg.size < 14
39
+ arg
40
+ end
41
+
42
+ def _racc_init_sysvars
43
+ @racc_state = [0]
44
+ @racc_tstack = []
45
+ @racc_vstack = []
46
+
47
+ @racc_t = nil
48
+ @racc_val = nil
49
+
50
+ @racc_read_next = true
51
+
52
+ @racc_user_yyerror = false
53
+ @racc_error_status = 0
54
+ end
55
+
56
+ # The entry point of the parser. This method is used with #next_token.
57
+ # If Racc wants to get token (and its value), calls next_token.
58
+ #
59
+ # Example:
60
+ # def parse
61
+ # @q = [[1,1],
62
+ # [2,2],
63
+ # [3,3],
64
+ # [false, '$']]
65
+ # do_parse
66
+ # end
67
+ #
68
+ # def next_token
69
+ # @q.shift
70
+ # end
71
+ def do_parse
72
+ _racc_do_parse_rb(_racc_setup(), false)
73
+ end
74
+
75
+ # The method to fetch next token.
76
+ # If you use #do_parse method, you must implement #next_token.
77
+ #
78
+ # The format of return value is [TOKEN_SYMBOL, VALUE].
79
+ # +token-symbol+ is represented by Ruby's symbol by default, e.g. :IDENT
80
+ # for 'IDENT'. ";" (String) for ';'.
81
+ #
82
+ # The final symbol (End of file) must be false.
83
+ def next_token
84
+ raise NotImplementedError, "#{self.class}\#next_token is not defined"
85
+ end
86
+
87
+ def _racc_do_parse_rb(arg, in_debug)
88
+ action_table, action_check, action_default, action_pointer,
89
+ _, _, _, _,
90
+ _, _, token_table, * = arg
91
+
92
+ _racc_init_sysvars
93
+ tok = act = i = nil
94
+
95
+ catch(:racc_end_parse) {
96
+ while true
97
+ if i = action_pointer[@racc_state[-1]]
98
+ if @racc_read_next
99
+ if @racc_t != 0 # not EOF
100
+ tok, @racc_val = next_token()
101
+ unless tok # EOF
102
+ @racc_t = 0
103
+ else
104
+ @racc_t = (token_table[tok] or 1) # error token
105
+ end
106
+ racc_read_token(@racc_t, tok, @racc_val) if @yydebug
107
+ @racc_read_next = false
108
+ end
109
+ end
110
+ i += @racc_t
111
+ unless i >= 0 and
112
+ act = action_table[i] and
113
+ action_check[i] == @racc_state[-1]
114
+ act = action_default[@racc_state[-1]]
115
+ end
116
+ else
117
+ act = action_default[@racc_state[-1]]
118
+ end
119
+ while act = _racc_evalact(act, arg)
120
+ ;
121
+ end
122
+ end
123
+ }
124
+ end
125
+
126
+
127
+
128
+ ###
129
+ ### common
130
+ ###
131
+
132
+ def _racc_evalact(act, arg)
133
+ action_table, action_check, _, action_pointer,
134
+ _, _, _, _,
135
+ _, _, _, shift_n,
136
+ reduce_n, * = arg
137
+ nerr = 0 # tmp
138
+
139
+ if act > 0 and act < shift_n
140
+ #
141
+ # shift
142
+ #
143
+ if @racc_error_status > 0
144
+ @racc_error_status -= 1 unless @racc_t <= 1 # error token or EOF
145
+ end
146
+ @racc_vstack.push @racc_val
147
+ @racc_state.push act
148
+ @racc_read_next = true
149
+ if @yydebug
150
+ @racc_tstack.push @racc_t
151
+ racc_shift @racc_t, @racc_tstack, @racc_vstack
152
+ end
153
+
154
+ elsif act < 0 and act > -reduce_n
155
+ #
156
+ # reduce
157
+ #
158
+ code = catch(:racc_jump) {
159
+ @racc_state.push _racc_do_reduce(arg, act)
160
+ false
161
+ }
162
+ if code
163
+ case code
164
+ when 1 # yyerror
165
+ @racc_user_yyerror = true # user_yyerror
166
+ return -reduce_n
167
+ when 2 # yyaccept
168
+ return shift_n
169
+ else
170
+ raise '[Racc Bug] unknown jump code'
171
+ end
172
+ end
173
+
174
+ elsif act == shift_n
175
+ #
176
+ # accept
177
+ #
178
+ racc_accept if @yydebug
179
+ throw :racc_end_parse, @racc_vstack[0]
180
+
181
+ elsif act == -reduce_n
182
+ #
183
+ # error
184
+ #
185
+ case @racc_error_status
186
+ when 0
187
+ unless arg[21] # user_yyerror
188
+ nerr += 1
189
+ on_error @racc_t, @racc_val, @racc_vstack
190
+ end
191
+ when 3
192
+ if @racc_t == 0 # is $
193
+ # We're at EOF, and another error occurred immediately after
194
+ # attempting auto-recovery
195
+ throw :racc_end_parse, nil
196
+ end
197
+ @racc_read_next = true
198
+ end
199
+ @racc_user_yyerror = false
200
+ @racc_error_status = 3
201
+ while true
202
+ if i = action_pointer[@racc_state[-1]]
203
+ i += 1 # error token
204
+ if i >= 0 and
205
+ (act = action_table[i]) and
206
+ action_check[i] == @racc_state[-1]
207
+ break
208
+ end
209
+ end
210
+ throw :racc_end_parse, nil if @racc_state.size <= 1
211
+ @racc_state.pop
212
+ @racc_vstack.pop
213
+ if @yydebug
214
+ @racc_tstack.pop
215
+ racc_e_pop @racc_state, @racc_tstack, @racc_vstack
216
+ end
217
+ end
218
+ return act
219
+
220
+ else
221
+ raise "[Racc Bug] unknown action #{act.inspect}"
222
+ end
223
+
224
+ racc_next_state(@racc_state[-1], @racc_state) if @yydebug
225
+
226
+ nil
227
+ end
228
+
229
+ def _racc_do_reduce(arg, act)
230
+ _, _, _, _,
231
+ goto_table, goto_check, goto_default, goto_pointer,
232
+ nt_base, reduce_table, _, _,
233
+ _, use_result, * = arg
234
+
235
+ state = @racc_state
236
+ vstack = @racc_vstack
237
+ tstack = @racc_tstack
238
+
239
+ i = act * -3
240
+ len = reduce_table[i]
241
+ reduce_to = reduce_table[i+1]
242
+ method_id = reduce_table[i+2]
243
+ void_array = []
244
+
245
+ tmp_t = tstack[-len, len] if @yydebug
246
+ tmp_v = vstack[-len, len]
247
+ tstack[-len, len] = void_array if @yydebug
248
+ vstack[-len, len] = void_array
249
+ state[-len, len] = void_array
250
+
251
+ # tstack must be updated AFTER method call
252
+ if use_result
253
+ vstack.push __send__(method_id, tmp_v, vstack, tmp_v[0])
254
+ else
255
+ vstack.push __send__(method_id, tmp_v, vstack)
256
+ end
257
+ tstack.push reduce_to
258
+
259
+ racc_reduce(tmp_t, reduce_to, tstack, vstack) if @yydebug
260
+
261
+ k1 = reduce_to - nt_base
262
+ if i = goto_pointer[k1]
263
+ i += state[-1]
264
+ if i >= 0 and (curstate = goto_table[i]) and goto_check[i] == k1
265
+ return curstate
266
+ end
267
+ end
268
+ goto_default[k1]
269
+ end
270
+
271
+ # This method is called when a parse error is found.
272
+ #
273
+ # ERROR_TOKEN_ID is an internal ID of token which caused error.
274
+ # You can get string representation of this ID by calling
275
+ # #token_to_str.
276
+ #
277
+ # ERROR_VALUE is a value of error token.
278
+ #
279
+ # value_stack is a stack of symbol values.
280
+ # DO NOT MODIFY this object.
281
+ #
282
+ # This method raises ParseError by default.
283
+ #
284
+ # If this method returns, parsers enter "error recovering mode".
285
+ def on_error(t, val, vstack)
286
+ raise ParseError, sprintf("parse error on value %s (%s)",
287
+ val.inspect, token_to_str(t) || '?')
288
+ end
289
+
290
+ # Enter error recovering mode.
291
+ # This method does not call #on_error.
292
+ def yyerror
293
+ throw :racc_jump, 1
294
+ end
295
+
296
+ # Exit parser.
297
+ # Return value is +Symbol_Value_Stack[0]+.
298
+ def yyaccept
299
+ throw :racc_jump, 2
300
+ end
301
+
302
+ # Leave error recovering mode.
303
+ def yyerrok
304
+ @racc_error_status = 0
305
+ end
306
+
307
+ # For debugging output
308
+ def racc_read_token(t, tok, val)
309
+ @racc_debug_out.print 'read '
310
+ @racc_debug_out.print tok.inspect, '(', racc_token2str(t), ') '
311
+ @racc_debug_out.puts val.inspect
312
+ @racc_debug_out.puts
313
+ end
314
+
315
+ def racc_shift(tok, tstack, vstack)
316
+ @racc_debug_out.puts "shift #{racc_token2str tok}"
317
+ racc_print_stacks tstack, vstack
318
+ @racc_debug_out.puts
319
+ end
320
+
321
+ def racc_reduce(toks, sim, tstack, vstack)
322
+ out = @racc_debug_out
323
+ out.print 'reduce '
324
+ if toks.empty?
325
+ out.print ' <none>'
326
+ else
327
+ toks.each {|t| out.print ' ', racc_token2str(t) }
328
+ end
329
+ out.puts " --> #{racc_token2str(sim)}"
330
+ racc_print_stacks tstack, vstack
331
+ @racc_debug_out.puts
332
+ end
333
+
334
+ def racc_accept
335
+ @racc_debug_out.puts 'accept'
336
+ @racc_debug_out.puts
337
+ end
338
+
339
+ def racc_e_pop(state, tstack, vstack)
340
+ @racc_debug_out.puts 'error recovering mode: pop token'
341
+ racc_print_states state
342
+ racc_print_stacks tstack, vstack
343
+ @racc_debug_out.puts
344
+ end
345
+
346
+ def racc_next_state(curstate, state)
347
+ @racc_debug_out.puts "goto #{curstate}"
348
+ racc_print_states state
349
+ @racc_debug_out.puts
350
+ end
351
+
352
+ def racc_print_stacks(t, v)
353
+ out = @racc_debug_out
354
+ out.print ' ['
355
+ t.each_index do |i|
356
+ out.print ' (', racc_token2str(t[i]), ' ', v[i].inspect, ')'
357
+ end
358
+ out.puts ' ]'
359
+ end
360
+
361
+ def racc_print_states(s)
362
+ out = @racc_debug_out
363
+ out.print ' ['
364
+ s.each {|st| out.print ' ', st }
365
+ out.puts ' ]'
366
+ end
367
+
368
+ def racc_token2str(tok)
369
+ self.class::Racc_token_to_s_table[tok] or
370
+ raise "[Racc Bug] can't convert token #{tok} to string"
371
+ end
372
+
373
+ # Convert internal ID of token symbol to the string.
374
+ def token_to_str(t)
375
+ self.class::Racc_token_to_s_table[t]
376
+ end
377
+
378
+ end
379
+ end
@@ -5,61 +5,70 @@
5
5
  class RaccMatchParser
6
6
 
7
7
 
8
- def initialize( txt, debug: false )
9
- ## puts "==> txt:"
10
- ## puts txt
11
-
12
- @tree = []
8
+
9
+ def initialize( txt, debug: false )
10
+ @tree = []
13
11
  @errors = []
14
12
 
15
- ### todo:
16
- ## - pass along debug flag
17
13
  lexer = SportDb::Lexer.new( txt, debug: debug )
18
14
  ## note - use tokenize_with_errors and add/collect tokenize errors
19
15
  @tokens, @errors = lexer.tokenize_with_errors
20
16
  ## pp @tokens
21
-
22
- ## quick hack - convert to racc format single char literal tokens e.g. '@' etc.
23
- @tokens = @tokens.map do |tok|
24
- if tok.size == 1
25
- [tok[0].to_s, tok[0].to_s]
26
- else
27
- tok
28
- end
29
- end
17
+
30
18
  end
31
19
 
32
20
 
33
21
  def debug( value ) @debug = value; end ### fix: use setter-style e.g. debug=(value) !!!
34
22
  def debug?() @debug == true; end
35
23
 
24
+ ###
25
+ ### fix-fix-fix rename to _trace
26
+ ## check lexer - add [debug] Parser - or such!!!
27
+ ##
36
28
  ## debug - trace / print message
37
29
  def trace( msg )
38
- puts " [parse] " + msg if debug?
30
+ puts " fix-fix-fix use _trace() in parser!!"
31
+ puts " [parse] " + msg
39
32
  end
40
33
 
41
-
34
+ def _trace( *args )
35
+ ## if debug?
36
+ print "[DEBUG] Parser -- "
37
+ args.each { |arg| puts args }
38
+ ## end
39
+ end
42
40
 
43
41
 
44
42
  def next_token
45
43
  tok = @tokens.shift
46
- trace( "next_token => #{tok.pretty_inspect}" )
44
+ _trace( "next_token => #{tok.pretty_inspect}" )
45
+
46
+ ## convert to racc format single char literal tokens e.g. '@' etc.
47
+ ## note - literal token MUST be string (NOT symbol)
48
+ ## note - racc expects array with to items
49
+ ## - item[0] is the token id
50
+ ## - item[1] is the token value
51
+
52
+ ## note - returns nil for end-of-file !!!
53
+ tok = [tok.type, tok] if tok
54
+
47
55
  tok
48
56
  end
49
-
57
+
58
+
50
59
  # on_error do |error_token_id, error_value, value_stack|
51
60
  # puts "Parse error on token: #{error_token_id}, value: #{error_value}"
52
- # end
61
+ # end
53
62
 
54
- def parse_with_errors
55
- trace( "start parse:" )
63
+ def parse_with_errors
64
+ _trace( "start parse:" )
56
65
  do_parse
57
66
  [@tree, @errors]
58
67
  end
59
68
 
60
69
  def parse ## convenience shortcut (ignores errors)
61
70
  tree, _ = parse_with_errors
62
- tree
71
+ tree
63
72
  end
64
73
 
65
74
 
@@ -69,20 +78,15 @@ def initialize( txt, debug: false )
69
78
 
70
79
  def on_error(error_token_id, error_value, value_stack)
71
80
  ## auto-add error_token (as string)
72
- error_token = Racc_token_to_s_table[error_token_id]
81
+ error_token = Racc_token_to_s_table[error_token_id]
73
82
  args = [error_token, error_token_id, error_value, value_stack]
83
+
74
84
  puts
75
85
  puts "!! on parse error:"
76
86
  puts "args=#{args.pretty_inspect}"
77
87
 
78
- @errors << "parse error on token: #{error_token} (#{error_token_id}) with value: #{error_value}, stack: #{value_stack.pretty_inspect}"
79
- ## exit 1 ## exit for now - get and print more info about context etc.!!
88
+ @errors << "parse error on token: #{error_token} >#{error_value.pretty_inspect}<, stack: #{value_stack.pretty_inspect}"
80
89
  end
81
90
 
82
91
 
83
- =begin
84
- on_error do |error_token_id, error_value, value_stack|
85
- puts "Parse error on token: #{error_token_id}, value: #{error_value}"
86
- end
87
- =end
88
92
  end # class RaccMatchParser