sportdb-parser 0.7.1 → 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 (45) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +1 -1
  3. data/Manifest.txt +17 -4
  4. data/lib/sportdb/parser/lexer-on_goal.rb +172 -0
  5. data/lib/sportdb/parser/lexer-on_group_def.rb +31 -0
  6. data/lib/sportdb/parser/lexer-on_prop_lineup.rb +79 -0
  7. data/lib/sportdb/parser/lexer-on_prop_misc.rb +110 -0
  8. data/lib/sportdb/parser/lexer-on_prop_penalties.rb +40 -0
  9. data/lib/sportdb/parser/lexer-on_round_def.rb +37 -0
  10. data/lib/sportdb/parser/lexer-on_top.rb +125 -0
  11. data/lib/sportdb/parser/lexer-prep_doc.rb +131 -0
  12. data/lib/sportdb/parser/lexer-prep_line.rb +63 -0
  13. data/lib/sportdb/parser/lexer-tokenize.rb +449 -0
  14. data/lib/sportdb/parser/lexer.rb +133 -1363
  15. data/lib/sportdb/parser/lexer_buffer.rb +8 -37
  16. data/lib/sportdb/parser/lexer_token.rb +126 -0
  17. data/lib/sportdb/parser/parser.rb +1104 -1403
  18. data/lib/sportdb/parser/racc_parser.rb +36 -32
  19. data/lib/sportdb/parser/racc_tree.rb +65 -98
  20. data/lib/sportdb/parser/token-date--helpers.rb +130 -0
  21. data/lib/sportdb/parser/token-date--names.rb +108 -0
  22. data/lib/sportdb/parser/token-date.rb +20 -192
  23. data/lib/sportdb/parser/token-date_duration.rb +8 -27
  24. data/lib/sportdb/parser/token-geo.rb +16 -16
  25. data/lib/sportdb/parser/token-goals--helpers.rb +114 -0
  26. data/lib/sportdb/parser/token-goals.rb +103 -249
  27. data/lib/sportdb/parser/token-group.rb +8 -22
  28. data/lib/sportdb/parser/token-prop.rb +138 -124
  29. data/lib/sportdb/parser/token-prop_name.rb +48 -39
  30. data/lib/sportdb/parser/token-round.rb +21 -35
  31. data/lib/sportdb/parser/token-score--helpers.rb +189 -0
  32. data/lib/sportdb/parser/token-score.rb +9 -393
  33. data/lib/sportdb/parser/token-score_full.rb +331 -0
  34. data/lib/sportdb/parser/token-status.rb +44 -46
  35. data/lib/sportdb/parser/token-status_inline.rb +112 -0
  36. data/lib/sportdb/parser/token-text.rb +41 -31
  37. data/lib/sportdb/parser/token-time.rb +29 -26
  38. data/lib/sportdb/parser/token.rb +58 -159
  39. data/lib/sportdb/parser/version.rb +1 -1
  40. data/lib/sportdb/parser.rb +45 -17
  41. metadata +19 -6
  42. data/lib/sportdb/parser/blocktxt.rb +0 -99
  43. data/lib/sportdb/parser/lexer_tty.rb +0 -111
  44. data/lib/sportdb/parser/token-table.rb +0 -149
  45. data/lib/sportdb/parser/token_helpers.rb +0 -92
@@ -18,58 +18,42 @@ module SportDb
18
18
  class Tokens
19
19
  def initialize( tokens )
20
20
  @tokens = tokens
21
- @pos = 0
21
+ @pos = 0
22
22
  end
23
23
 
24
24
  def pos() @pos; end
25
25
  def eos?() @pos >= @tokens.size; end
26
26
 
27
27
 
28
- def include?( *types )
29
- pos = @pos
30
- ## puts " starting include? #{types.inspect} @ #{pos}"
31
- while pos < @tokens.size do
32
- return true if types.include?( @tokens[pos][0] )
33
- pos +=1
34
- end
35
- false
36
- end
28
+
37
29
 
38
30
  ## pattern e.g. [:TEXT, [:VS,:SCORE], :TEXT]
39
31
  def match?( *pattern )
40
32
  ## puts " starting match? #{pattern.inspect} @ #{@pos}"
41
33
  pattern.each_with_index do |types,offset|
34
+ tok = peek(offset)
35
+ return false if tok.nil? ## no more tokens (cannot match)
36
+
42
37
  ## if single symbol wrap in array
43
38
  types = types.is_a?(Array) ? types : [types]
44
- return false unless types.include?( peek(offset) )
39
+ return false unless types.include?( tok.type )
45
40
  end
46
41
  true
47
42
  end
48
43
 
49
44
 
50
- ## return token type (e.g. :TEXT, :NUM, etc.)
51
- def cur() peek(0); end
52
- ## return content (assumed to be text)
53
- def text(offset=0)
54
- ## raise error - why? why not?
55
- ## return nil?
56
- if peek( offset ) != :text
57
- raise ArgumentError, "text(#{offset}) - token not a text type"
58
- end
59
- @tokens[@pos+offset][1]
60
- end
61
45
 
46
+ def cur() peek(0); end
62
47
 
63
48
  def peek(offset=1)
64
49
  ## return nil if eos
65
50
  if @pos+offset >= @tokens.size
66
51
  nil
67
52
  else
68
- @tokens[@pos+offset][0]
53
+ @tokens[@pos+offset]
69
54
  end
70
55
  end
71
56
 
72
- ## note - returns complete token
73
57
  def next
74
58
  # if @pos >= @tokens.size
75
59
  # raise ArgumentError, "end of array - #{@pos} >= #{@tokens.size}"
@@ -80,18 +64,5 @@ class Tokens
80
64
  @pos += 1
81
65
  t
82
66
  end
83
-
84
- def collect( &blk )
85
- tokens = []
86
- loop do
87
- break if eos?
88
- tokens << if block_given?
89
- blk.call( self.next )
90
- else
91
- self.next
92
- end
93
- end
94
- tokens
95
- end
96
67
  end # class Tokens
97
68
  end # module SportDb
@@ -0,0 +1,126 @@
1
+ module SportDb
2
+ class Lexer
3
+
4
+
5
+
6
+ class Token
7
+
8
+ ## Token.newline( lineno: 1, offset: [1,2] )
9
+ ## maps to =>
10
+ ## Token.new( :NEWLINE, "\n", lineno: 1, offset: [1,2])
11
+ ##
12
+ ## use self.nl ?
13
+ def self.newline( lineno:, offset: [])
14
+ new( :NEWLINE, "\n", lineno: lineno, offset: offset )
15
+ end
16
+
17
+ ## Token.literal( ",", lineno: 4, offset: [5,6])
18
+ ## # maps to =>
19
+ ## Token.new( ",", ",", lineno: 4, offset: [5,6])
20
+ ##
21
+ ## use self.lit?
22
+ def self.literal( literal, lineno:, offset: [])
23
+ new( literal, literal, lineno: lineno, offset: offset )
24
+ end
25
+
26
+ ## or use virt or pseudo - why? why not?
27
+ def self.virtual( type, lineno:, offset: [])
28
+ ## note - offset (start/end) should be same number (zero-width assertions!!)
29
+ ## e.g. :GOALS_COMPAT, "<|GOALS_COMPAT|>"
30
+ new( type, '', lineno: lineno, offset: offset )
31
+ end
32
+
33
+
34
+ attr_reader :type, :text,
35
+ :lineno, :offset
36
+
37
+ def initialize( type, text='',
38
+ lineno:, offset: [],
39
+ value: nil )
40
+ @type = type
41
+ @text = text # note - lexeme (string from source)
42
+ @lineno = lineno # note - lineno (integer number - not line as string) !!!
43
+
44
+ raise TypeError, "type Array required for offset; got #{offset.inspect}" unless offset.is_a?( Array )
45
+ @offset = offset # note - for now char offset [start,end] in line (NOT absolute!!)
46
+ # maybe latter add MatchData#byteoffset instead - why? why not?
47
+ @value = value # might be (union of) string/array/hash
48
+ end
49
+
50
+ def value
51
+ ## note - if value is not set (nil) return text (lexeme)
52
+ ## no need to duplicate text as value
53
+ @value.nil? ? @text : @value
54
+ end
55
+
56
+
57
+ ## note: do NOT use as_text/text to avoid confusion with (raw) text (lexeme)
58
+ ##
59
+ ## use
60
+ ## as_str -- value (as String)
61
+ ## as_int -- value (as Integer)
62
+ ## as_hash -- value (as Hash)
63
+ ## as_ary -- value (as Array)
64
+
65
+ def as_str
66
+ raise TypeError, "token value #{@value.inspect} is #{@value.class.name} NOT string; sorry" if @value && !@value.is_a?(String)
67
+ ## note - if value is not set (nil) return text (lexeme)
68
+ ## no need to duplicate text as value
69
+ @value.nil? ? @text : @value
70
+ end
71
+
72
+ def as_int
73
+ raise TypeError, "token value #{@value.inspect} is #{@value.class.name} NOT integer; sorry" if !@value.is_a?(Integer)
74
+ @value
75
+ end
76
+
77
+ def as_hash
78
+ raise TypeError, "token value #{@value.inspect} is #{@value.class.name} NOT hash; sorry" if !@value.is_a?(Hash)
79
+ @value
80
+ end
81
+
82
+ def as_ary
83
+ raise TypeError, "token value #{@value.inspect} is #{@value.class.name} NOT array; sorry" if !@value.is_a?(Array)
84
+ @value
85
+ end
86
+
87
+
88
+ def to_legacy
89
+ ## return old "legacy" array format
90
+ if @value.nil?
91
+ [@type, @text]
92
+ else
93
+ [@type, [@text, @value]]
94
+ end
95
+ end
96
+
97
+
98
+ ## pretty print
99
+ def pretty_print( printer )
100
+ ## check for literal e.g. "," etc.
101
+ if @type.is_a?( String ) && @type == @text && @value.nil?
102
+ printer.text( "[#{@type.inspect}" )
103
+ elsif @type.is_a?( Symbol ) && @text == '' && @value.nil?
104
+ ## assume virtual token (zero-width)
105
+ ## use <!...!> style
106
+ printer.text( "[<|#{@type}|>" )
107
+ else
108
+ printer.text( "[#{@type.inspect} #{@text.inspect}" )
109
+ printer.text( ", #{value.inspect}") if @value
110
+ end
111
+
112
+
113
+ printer.text( " @#{@lineno}" )
114
+ ## note - for now print only start_offset (offset[0])
115
+ ## to keep dump/output shorter
116
+ ## note - start counting columns at one (NOT zero), thus, add +1 !!
117
+ printer.text( ":#{@offset[0]+1}" ) if @offset.is_a?(Array) && @offset.size == 2
118
+ printer.text( "]" )
119
+ end
120
+
121
+ end # class Token
122
+
123
+
124
+
125
+ end # class Lexer
126
+ end # module SportDb