sportdb-parser 0.7.1 → 0.8.0

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 (54) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +1 -1
  3. data/Manifest.txt +25 -7
  4. data/Rakefile +23 -0
  5. data/lib/sportdb/parser/debuggable.rb +53 -0
  6. data/lib/sportdb/parser/lexer-logger.rb +20 -0
  7. data/lib/sportdb/parser/lexer-on_goal.rb +167 -0
  8. data/lib/sportdb/parser/lexer-on_group_def.rb +31 -0
  9. data/lib/sportdb/parser/lexer-on_prop_lineup.rb +79 -0
  10. data/lib/sportdb/parser/lexer-on_prop_misc.rb +123 -0
  11. data/lib/sportdb/parser/lexer-on_prop_penalties.rb +40 -0
  12. data/lib/sportdb/parser/lexer-on_round_def.rb +37 -0
  13. data/lib/sportdb/parser/lexer-on_top.rb +133 -0
  14. data/lib/sportdb/parser/lexer-prep_doc.rb +131 -0
  15. data/lib/sportdb/parser/lexer-prep_line.rb +63 -0
  16. data/lib/sportdb/parser/lexer-tokenize.rb +468 -0
  17. data/lib/sportdb/parser/lexer.rb +122 -1376
  18. data/lib/sportdb/parser/lexer_buffer.rb +8 -37
  19. data/lib/sportdb/parser/lexer_token.rb +126 -0
  20. data/lib/sportdb/parser/parse_tree--core.rb +29 -0
  21. data/lib/sportdb/parser/parse_tree-match.rb +309 -0
  22. data/lib/sportdb/parser/parse_tree-props.rb +233 -0
  23. data/lib/sportdb/parser/parse_tree.rb +202 -0
  24. data/lib/sportdb/parser/parser-top.rb +102 -0
  25. data/lib/sportdb/parser/parser.rb +1253 -1392
  26. data/lib/sportdb/parser/token-date--helpers.rb +130 -0
  27. data/lib/sportdb/parser/token-date--names.rb +108 -0
  28. data/lib/sportdb/parser/token-date.rb +20 -192
  29. data/lib/sportdb/parser/token-date_duration.rb +8 -27
  30. data/lib/sportdb/parser/token-geo.rb +16 -16
  31. data/lib/sportdb/parser/token-goals--helpers.rb +114 -0
  32. data/lib/sportdb/parser/token-goals.rb +101 -255
  33. data/lib/sportdb/parser/token-group.rb +8 -22
  34. data/lib/sportdb/parser/token-prop.rb +178 -124
  35. data/lib/sportdb/parser/token-prop_name.rb +48 -39
  36. data/lib/sportdb/parser/token-round.rb +21 -35
  37. data/lib/sportdb/parser/token-score--helpers.rb +189 -0
  38. data/lib/sportdb/parser/token-score.rb +9 -393
  39. data/lib/sportdb/parser/token-score_full.rb +331 -0
  40. data/lib/sportdb/parser/token-status.rb +44 -46
  41. data/lib/sportdb/parser/token-status_inline.rb +112 -0
  42. data/lib/sportdb/parser/token-text.rb +41 -31
  43. data/lib/sportdb/parser/token-time.rb +29 -26
  44. data/lib/sportdb/parser/token.rb +90 -158
  45. data/lib/sportdb/parser/version.rb +2 -2
  46. data/lib/sportdb/parser.rb +88 -62
  47. metadata +27 -9
  48. data/lib/sportdb/parser/blocktxt.rb +0 -99
  49. data/lib/sportdb/parser/lexer_tty.rb +0 -111
  50. data/lib/sportdb/parser/racc_parser.rb +0 -88
  51. data/lib/sportdb/parser/racc_tree.rb +0 -557
  52. data/lib/sportdb/parser/token-table.rb +0 -149
  53. data/lib/sportdb/parser/token_helpers.rb +0 -92
  54. /data/lib/sportdb/parser/{parser_runtime.rb → parser-runtime.rb} +0 -0
@@ -0,0 +1,468 @@
1
+ module SportDb
2
+ class Lexer
3
+
4
+
5
+ ###
6
+ ## use nested class for context - why? why not?
7
+ ## note: first arg passed in MUST be ref to lexer (instance)
8
+ class Context
9
+ ## passed along to on_round_def etc. handlers in tokenize_line
10
+ ## note - for now only offset (in line begin/end) gets updated !!!
11
+ attr_writer :offset
12
+ attr_reader :lineno
13
+
14
+ def initialize( lexer,
15
+ line:,
16
+ lineno:,
17
+ errors: )
18
+ @lexer = lexer
19
+ @line = line
20
+ @lineno = lineno
21
+ @errors = errors
22
+
23
+ @offset = [0,0] ## or use [] aka [nil,nil] for not defined??? why? why not?
24
+ ## @offset = offset ## MatchData offset e.g. [m.begin(0),m.end(0)]
25
+ end
26
+
27
+
28
+
29
+ def warn_on_else( match, mode: 'TOP' )
30
+ if match[:any]
31
+ _add_warn( "unexpected char >#{match[:any]}< (#{mode})" )
32
+ else
33
+ ## internal error - shouldn't really happen
34
+ _add_warn( "internal error - unknown match (#{mode}): #{match.inspect}")
35
+ end
36
+ end
37
+
38
+
39
+ def _add_warn( msg )
40
+ ## note - warns gets logged as error for now too
41
+ ## maybe add @warns later - why? why not?
42
+ ##
43
+ ## note - add +1 to offset (start at one - not zero-based)
44
+ ## will match with (external) text editors
45
+ msg = "parse error (tokenize) - " +
46
+ msg +
47
+ " in line @#{@lineno}:#{@offset[0]+1},#{@offset[1]+1} >#{@line}< "
48
+
49
+ @errors << msg
50
+ @lexer.log( "!! WARN - #{msg}" )
51
+
52
+ @lexer._warn( msg )
53
+ end
54
+
55
+ =begin
56
+ ## use report/log/??_parses_error
57
+ def _add_error( msg )
58
+ msg = "parse error (tokenize) -" +
59
+ msg +
60
+ " in line #{@lineno}@#{@offset[0]},#{@offse[1]} >#{@line}< "
61
+
62
+ @errors << msg
63
+ end
64
+ =end
65
+
66
+ end # class Context
67
+
68
+
69
+
70
+
71
+
72
+ def _tokenize_line( line, lineno )
73
+ tokens = []
74
+ errors = [] ## keep a list of errors - why? why not?
75
+
76
+
77
+ pos = 0 ## note - usually same as offset[1] aka offset[end] after match
78
+ ## track last offset (begin/end) - to report error on no match
79
+ ## or no match in end of string
80
+ offset = [0,0]
81
+ m = nil
82
+
83
+ ## track number of geo text seen
84
+ ## (use for - do NOT break on two spaces if no geo text seen yet!!)
85
+ @geo_count = 0
86
+
87
+ ####
88
+ ## quick hack - keep re state/mode between tokenize calls!!!
89
+ @re ||= RE ## note - switch between RE & INSIDE_RE
90
+
91
+
92
+ if @re == RE ## top-level
93
+ ### check for modes once (per line) here to speed-up parsing
94
+ ### for now goals only possible for start of line!!
95
+ ### fix - remove optional [] - why? why not?
96
+
97
+ ####
98
+ ## note - ord e.g. (45) for match number can only start a (match) line
99
+ ## "inline" use NOT possible
100
+ ## note - ord (for ordinal number!!!) e.g match number (1), (42), etc.
101
+ if (m = START_WITH_ORD.match(line))
102
+ ## note - strip enclosing () and convert to integer
103
+ tokens << Token.new(:ORD, m[:ord],
104
+ lineno: lineno, offset: m.offset(:ord),
105
+ value: m[:value].to_i(10) )
106
+
107
+ offset = m.offset(0)
108
+ pos = offset[1] ## update pos
109
+ elsif (m = START_WITH_YEAR.match(line))
110
+ tokens << Token.new(:YEAR, m[:year],
111
+ lineno: lineno, offset: m.offset(:year),
112
+ value: m[:year].to_i(10) )
113
+
114
+ offset = m.offset(0)
115
+ pos = offset[1] ## update pos
116
+
117
+ elsif (m = START_WITH_GROUP_DEF_LINE_RE.match( line ))
118
+ _trace( "ENTER GROUP_DEF_RE MODE" )
119
+ @re = GROUP_DEF_RE
120
+
121
+ tokens << Token.new( :GROUP_DEF, m[:group_def],
122
+ lineno: lineno, offset: m.offset(:group_def) )
123
+
124
+
125
+ offset = m.offset(0)
126
+ pos = offset[1] ## update pos
127
+
128
+ elsif (m = START_WITH_PROP_KEY_RE.match( line ))
129
+ ## start with prop key (match will switch into prop mode!!!)
130
+ ## - fix - remove leading spaces in regex (upstream) - why? why not?
131
+ ##
132
+ ### switch into new mode
133
+ ## switch context to PROP_RE
134
+ _trace("ENTER PROP_RE MODE" )
135
+ key = m[:key]
136
+
137
+
138
+ ### todo/fix - add prop yellow/red cards too - why? why not?
139
+ ## todo/fix - separate sent off and red card
140
+ ## sent-off - incl. red card, yellow/red card and the era before red cards!!
141
+ if ['sent-off',
142
+ 'sent off'].include?( key.downcase)
143
+ @re = PROP_CARDS_RE ## use CARDS_RE ???
144
+ tokens << Token.new(:PROP_SENTOFF, m[:key],
145
+ lineno: lineno, offset: m.offset(:key))
146
+ elsif ['red cards'].include?( key.downcase )
147
+ @re = PROP_CARDS_RE ## use CARDS_RE ???
148
+ tokens << Token.new(:PROP_REDCARDS, m[:key],
149
+ lineno: lineno, offset: m.offset(:key))
150
+ elsif ['yellow cards'].include?( key.downcase )
151
+ @re = PROP_CARDS_RE
152
+ tokens << Token.new(:PROP_YELLOWCARDS, m[:key],
153
+ lineno: lineno, offset: m.offset(:key))
154
+
155
+ ## todo: check allow yellow/red cards: or such as key!!!
156
+ ## check for alternate spellings
157
+ ## 'yellow/red cards' - note - meaning closes to yellow or red !!
158
+ ## thus use yellow-red cards as default/standard - why? why not?
159
+ elsif ['yellow-red cards',
160
+ 'yellowred cards'].include?( key.downcase )
161
+ @re = PROP_CARDS_RE
162
+ tokens << Token.new(:PROP_YELLOWREDCARDS, m[:key],
163
+ lineno: lineno, offset: m.offset(:key))
164
+
165
+
166
+ elsif ['ref', 'referee',
167
+ 'refs', 'referees' ## note - allow/support assistant refs
168
+ ].include?( key.downcase )
169
+ @re = PROP_REFEREE_RE
170
+ tokens << Token.new(:PROP_REFEREE, m[:key],
171
+ lineno: lineno, offset: m.offset(:key))
172
+ elsif ['att', 'attn', 'attendance'].include?( key.downcase )
173
+ @re = PROP_ATTENDANCE_RE
174
+ tokens << Token.new(:PROP_ATTENDANCE, m[:key],
175
+ lineno: lineno, offset: m.offset(:key))
176
+
177
+ # elsif ['goals'].include?( key.downcase )
178
+ # @re = PROP_GOAL_RE
179
+ # tokens << [:PROP_GOALS, m[:key]]
180
+
181
+ elsif ['penalties',
182
+ 'penalty shootout',
183
+ 'penalty shoot-out',
184
+ 'penalty kicks'].include?( key.downcase )
185
+ @re = PROP_PENALTIES_RE
186
+ tokens << Token.new(:PROP_PENALTIES, m[:key],
187
+ lineno: lineno, offset: m.offset(:key))
188
+ else ## assume (team) line-up
189
+ @re = PROP_LINEUP_RE
190
+ ## fix-fix-fix - rename to PROP_LINEUP !!
191
+ tokens << Token.new(:PROP, m[:key],
192
+ lineno: lineno, offset: m.offset(:key))
193
+ end
194
+
195
+ offset = m.offset(0)
196
+ pos = offset[1] ## update pos
197
+ ###
198
+ ### todo/fix
199
+ ### rename to START_WITH_ROUND_DEF_OUTLINE_RE !!!!
200
+ elsif (m = ROUND_DEF_OUTLINE_RE.match( line ))
201
+ _trace( "ENTER ROUND_DEF_RE MODE" )
202
+ @re = ROUND_DEF_RE
203
+
204
+ ## note - return ROUND_DEF NOT ROUND_OUTLINE token
205
+ ## fix - add leading ▪ too!!
206
+ tokens << Token.new( :ROUND_DEF, m[:round_outline],
207
+ lineno: lineno, offset: m.offset(:round_outline))
208
+
209
+ offset = m.offset(0)
210
+ pos = offset[1] ## update pos
211
+ elsif (m = ROUND_OUTLINE_RE.match( line ))
212
+ _trace( "ROUND_OUTLINE" )
213
+ ## note - derive round level from no of (leading) markers
214
+ ## e.g. ▪/:: is 1, ▪▪/::: is 2, ▪▪▪/:::: is 3, etc.
215
+ ## note - ascii-style starts with double ::, thus, autodecrement by one!
216
+ round_level = m[:round_marker].size
217
+ round_level -= 1 if m[:round_marker].start_with?( '::' )
218
+
219
+ tokens << Token.new( :ROUND_OUTLINE, m[:round_outline],
220
+ lineno: lineno, offset: m.offset(:round_outline),
221
+ value: { outline: m[:round_outline],
222
+ level: round_level})
223
+
224
+ ## note - eats-up line for now (change later to only eat-up marker e.g. »|>>)
225
+ offset = m.offset(0)
226
+ pos = offset[1] ## update pos
227
+ elsif (m = START_GOAL_LINE_RE.match( line )) ## line starting with ( - assume
228
+ ## switch context to GOAL_RE (goalline(s))
229
+ ####
230
+ ## note - check for alternate goal line styles / formats
231
+ if START_GOAL_LINE_COMPAT_RE.match(line )
232
+ ## "legacy" style starting with minute e.g.
233
+ ## (6 Puskás 0-1, 9 Czibor 0-2, 11 Morlock 1-2, 18 Rahn 2-2,
234
+ ## 84 Rahn 3-2)
235
+ @re = GOAL_COMPAT_RE
236
+ _trace( "ENTER GOAL_COMPAT_RE MODE" )
237
+
238
+ tokens << Token.virtual( :GOALS_COMPAT, lineno: lineno )
239
+ elsif START_GOAL_LINE_ALT_RE.match( line )
240
+ ## goals with scores e.g.
241
+ ## (1-0 Franck Ribéry, 2-0 Ivica Olić, 2-1 Wayne Rooney)
242
+ ## -or-
243
+ ## (Dion Beljo 1-0
244
+ ## 1-1 Andreas Gruber
245
+ ## Matthias Seidl 2-1)
246
+ @re = GOAL_ALT_RE
247
+ _trace( "ENTER GOAL_ALT_RE MODE" )
248
+
249
+ tokens << Token.virtual( :GOALS_ALT, lineno: lineno )
250
+ else
251
+ ## "standard" / default style
252
+ @re = GOAL_RE
253
+ _trace( "ENTER GOAL_RE MODE" )
254
+
255
+ tokens << Token.virtual( :GOALS, lineno: lineno )
256
+ end
257
+
258
+ ## note - eat-up ( for now
259
+ ## pass along "virtual" GOALS or GOALS_ALT token
260
+ ## (see INLINE_GOALS for the starting goal line inline)
261
+ ##
262
+ ## fix-fix-fix
263
+ ## keep offset at [0,0] - why? why not?
264
+ ## do NOT eat-up
265
+ ## or better
266
+ ## add tokens << Token.literal( '(', lineno: lineno, offset: ...) !!!
267
+ offset = m.offset(0)
268
+ pos = offset[1] ## update pos
269
+ end
270
+ end
271
+
272
+
273
+
274
+ old_pos = -1 ## allows to backtrack to old pos (used in geo)
275
+
276
+
277
+
278
+
279
+ ctx = Context.new( self,
280
+ line: line,
281
+ lineno: lineno,
282
+ errors: errors )
283
+
284
+
285
+ while m = @re.match( line, pos )
286
+ # if debug?
287
+ # pp m
288
+ # puts "pos: #{pos}"
289
+ # end
290
+ offset = m.offset(0)
291
+ ctx.offset = offset
292
+
293
+
294
+
295
+ if offset[0] != pos
296
+ ## match NOT starting at start/begin position!!!
297
+ ## report parse error!!!
298
+ msg = "parse error (tokenize) - skipping >#{line[pos..(offset[0]-1)]}< in line #{lineno}@#{offset[0]},#{offset[1]} >#{line}<"
299
+ errors << msg
300
+
301
+ log( msg )
302
+ puts "!! WARN - #{msg}"
303
+ end
304
+
305
+
306
+ ##
307
+ ## todo/fix - also check if possible
308
+ ## if no match but not yet end off string!!!!
309
+ ## report skipped text run too!!!
310
+
311
+ old_pos = pos
312
+ pos = offset[1]
313
+
314
+ # pp offset if debug?
315
+
316
+ ##
317
+ ## note: racc requires pairs e.g. [:TOKEN, VAL]
318
+ ## for VAL use "text" or ["text", { opts }] array
319
+
320
+
321
+
322
+ t = if @re == ROUND_DEF_RE then _on_round_def( m, ctx: ctx )
323
+ elsif @re == GROUP_DEF_RE then _on_group_def( m, ctx: ctx )
324
+ elsif @re == GEO_RE
325
+ ### note - possibly end inline geo on [ (and others?? in the future
326
+ ## note: break on double spaces e.g.
327
+ ## e.g. Jul/16 @ Arena Auf Schalke, Gelsenkirchen Serbia 0-1 England
328
+ if m[:spaces]
329
+ ### note - do NOT break out
330
+ ## if not text seen yet!!!
331
+ if @geo_count > 0
332
+ ## get out-off geo mode and backtrack (w/ next)
333
+ ##
334
+ ## todo/fix
335
+ ## add virtual geo_end token!!!
336
+ _trace( "LEAVE GEO_RE MODE, BACK TO TOP_LEVEL/RE" )
337
+ @re = RE
338
+ pos = old_pos
339
+ next ## backtrack (resume new loop step)
340
+ else
341
+ nil ## skip spaces
342
+ end
343
+ elsif m[:space]
344
+ nil ## skip (single) space
345
+ elsif m[:text]
346
+ @geo_count += 1
347
+ ## keep pos - why? why not?
348
+ Token.new(:GEO, m[:text],
349
+ lineno: lineno, offset: m.offset(:text))
350
+ elsif m[:geo_end] ## "hacky" special comma; always ends geo mode!!!
351
+ ## get out-off geo mode and backtrack (w/ next)
352
+ ## todo/fix
353
+ ## add (semi-) virtual geo_end token!!!
354
+ _trace( "LEAVE GEO_RE MODE, BACK TO TOP_LEVEL/RE" )
355
+ @re = RE
356
+ pos = old_pos
357
+ next ## backtrack (resume new loop step)
358
+ elsif m[:sym]
359
+ case m[:sym]
360
+ ## note - reset geo_count to 0 (avoids break on two spaces)
361
+ ## if separator seen!!
362
+ when ',' then @geo_count = 0
363
+ Token.literal( m[:sym], lineno: lineno, offset: m.offset(:sym))
364
+ when '›' then @geo_count = 0;
365
+ Token.literal( ',', lineno: lineno, offset: m.offset(:sym))
366
+ ## note - treat geo sep › (unicode) like comma for now!!!
367
+ when '>' then @geo_count = 0;
368
+ Token.literal( ',', lineno: lineno, offset: m.offset(:sym))
369
+ ## note - treat geo sep > (ascii) like comma for now!!!
370
+
371
+
372
+ when '[','▪'
373
+ ##
374
+ ## todo/fix
375
+ ## add virtual geo_end token!!!
376
+ ## get out-off geo mode and backtrack (w/ next)
377
+ _trace( "LEAVE GEO_RE MODE, BACK TO TOP_LEVEL/RE" )
378
+ @re = RE
379
+ pos = old_pos
380
+ next ## backtrack (resume new loop step)
381
+ ## fix-fix-fix merge '▪' with '['
382
+ else
383
+ Token.literal( m[:sym], lineno: lineno, offset: m.offset(:sym))
384
+ end
385
+ else
386
+ ctx.warn_on_else( m, mode: 'GEO' )
387
+ nil
388
+ end
389
+ elsif @re == PROP_CARDS_RE then _on_prop_cards( m, ctx: ctx )
390
+ elsif @re == PROP_LINEUP_RE then _on_prop_lineup( m, ctx: ctx )
391
+ elsif @re == PROP_ATTENDANCE_RE then _on_prop_attendance( m, ctx: ctx )
392
+ elsif @re == PROP_REFEREE_RE then _on_prop_referee( m, ctx: ctx )
393
+ elsif @re == PROP_PENALTIES_RE then _on_prop_penalties( m, ctx: ctx )
394
+ elsif @re == GOAL_COMPAT_RE then _on_goal_compat( m, ctx: ctx )
395
+ elsif @re == GOAL_ALT_RE then _on_goal_alt( m, ctx: ctx )
396
+ elsif @re == GOAL_RE then _on_goal( m, ctx: ctx )
397
+ ###################################################
398
+ ## assume TOP_LEVEL (a.k.a. RE) machinery
399
+ else
400
+ _on_top( m, ctx: ctx )
401
+ end
402
+
403
+
404
+ tokens << t if t
405
+
406
+ # if debug?
407
+ # print ">"
408
+ # print "*" * pos
409
+ # puts "#{line[pos..-1]}<"
410
+ # end
411
+ end
412
+
413
+ ## check if no match in end of string
414
+ if offset[1] != line.size
415
+ msg = "parse error (tokenize) - skipping >#{line[offset[1]..-1]}< in line #{lineno}@#{offset[1]},#{line.size} >#{line}<"
416
+ errors << msg
417
+
418
+ log( msg )
419
+ puts "!! WARN - #{msg}"
420
+ end
421
+
422
+
423
+ # if @re == GOAL_RE ### ALWAYS switch back to top level mode
424
+ # puts " LEAVE GOAL_RE MODE, BACK TO TOP_LEVEL/RE" if debug?
425
+ # @re = RE
426
+ # end
427
+
428
+ if @re == GEO_RE ### ALWAYS switch back to top level mode
429
+ _trace( "LEAVE GEO_RE MODE, BACK TO TOP_LEVEL/RE" )
430
+ @re = RE
431
+ end
432
+
433
+ ### ALWAYS switch back to top level mode
434
+ @re = RE if @re == GROUP_DEF_RE ||
435
+ @re == ROUND_DEF_RE
436
+
437
+ ##
438
+ ## if in prop mode continue if last token is [,-]
439
+ ## otherwise change back to "standard" mode
440
+ if @re == PROP_LINEUP_RE ||
441
+ @re == PROP_CARDS_RE ||
442
+ @re == PROP_PENALTIES_RE ||
443
+ @re == PROP_ATTENDANCE_RE ||
444
+ @re == PROP_REFEREE_RE
445
+
446
+ ## note - add :CARDS_SEP_ALT (aka -)!!! too
447
+ ## maybe later CARDS_NONE_LEFT too - why? why not?
448
+ if [',', '-', ';', :CARDS_SEP_ALT].include?( tokens[-1].type)
449
+ ## continue/stay in PROP_RE mode
450
+ ## todo/check - auto-add PROP_CONT token or such
451
+ ## to help parser with possible NEWLINE
452
+ ## conflicts - why? why not?
453
+ else
454
+ ## switch back to top-level mode!!
455
+ _trace( "LEAVE PROP_RE MODE, BACK TO TOP_LEVEL/RE" )
456
+ @re = RE
457
+ ## note - auto-add PROP_END (<PROP_END>)
458
+ tokens << Token.virtual(:PROP_END, lineno: lineno)
459
+ end
460
+ end
461
+
462
+
463
+ [tokens,errors]
464
+ end
465
+
466
+
467
+ end ## class Lexer
468
+ end ## module SportDb