sportdb-parser 0.5.5 → 0.5.7

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.
@@ -24,7 +24,9 @@ require_relative 'parser/token-status'
24
24
  require_relative 'parser/token'
25
25
  require_relative 'parser/tokenizer'
26
26
 
27
- require_relative 'parser/parser'
27
+ require_relative 'parser/parser' ## auto-generated by racc (from parser.y)
28
+ require_relative 'parser/racc_parser'
29
+ require_relative 'parser/racc_tree'
28
30
 
29
31
 
30
32
 
@@ -44,9 +46,11 @@ end # module SportDb
44
46
 
45
47
 
46
48
  module SportDb
47
-
48
-
49
-
49
+ ###
50
+ ## todo/fix - use LangHelper or such
51
+ ## e.g. class Parser
52
+ ## include LangHelper
53
+ ## end
50
54
  class Parser
51
55
  ## keep "old" access to checking for group, round & friends
52
56
  ## for now for compatibility
@@ -54,302 +58,9 @@ class Parser
54
58
  def is_round?( text ) Lang.is_round?( text ); end
55
59
  def is_leg?( text ) Lang.is_leg?( text ); end
56
60
  end
61
+ end # module SportDb
57
62
 
58
63
 
59
64
 
60
- class Tokenizer
61
-
62
- attr_reader :tokens
63
-
64
- def initialize( txt )
65
- parser = Parser.new
66
-
67
- tree = []
68
-
69
- lines = txt.split( "\n" )
70
- lines.each_with_index do |line,i|
71
- next if line.strip.empty? || line.strip.start_with?( '#' )
72
- ## support for inline (end-of-line) comments
73
- line = line.sub( /#.*/, '' ).strip
74
-
75
- puts "line >#{line}<"
76
- tokens = parser.tokenize( line )
77
- pp tokens
78
-
79
- tree << tokens
80
- end
81
-
82
-
83
- =begin
84
- ## quick hack
85
- ## turn all text tokens followed by minute token
86
- ## into player tokens!!!
87
- ##
88
- ## also auto-convert text tokens into team tokens - why? why not?
89
- tree.each do |tokens|
90
- tokens.each_with_index do |t0,idx|
91
- t1 = tokens[idx+1]
92
- if t1 && t1[0] == :minute && t0[0] == :text
93
- t0[0] = :player
94
- end
95
- end
96
- end
97
- =end
98
-
99
- =begin
100
- ## auto-add/insert start tokens for known line patterns
101
- ## START_GOALS for goals_line
102
- ## why? why not?
103
- =end
104
-
105
- ## flatten
106
- @tokens = []
107
- tree.each do |tokens|
108
- @tokens += tokens
109
- @tokens << [:NEWLINE, "\n"] ## auto-add newlines
110
- end
111
-
112
-
113
- ## convert to racc format
114
- @tokens = @tokens.map do |tok|
115
- if tok.size == 1
116
- [tok[0].to_s, tok[0].to_s]
117
- elsif tok.size == 2
118
- #############
119
- ## pass 1
120
- ## replace all texts with keyword matches (e.g. group, round, leg, etc.)
121
- if tok[0] == :TEXT
122
- text = tok[1]
123
- tok = if parser.is_group?( text )
124
- [:GROUP, text]
125
- elsif parser.is_round?( text ) || parser.is_leg?( text )
126
- [:ROUND, text]
127
- else
128
- tok ## pass through as-is (1:1)
129
- end
130
- end
131
- ## pass 2
132
- tok
133
- else
134
- raise ArgumentError, "tokens of size 1|2 expected; got #{tok.pretty_inspect}"
135
- end
136
- end
137
- end
138
-
139
-
140
-
141
- def next_token
142
- @tokens.shift
143
- end
144
- end # class Tokenizer
145
- end # module SportDb
146
-
147
-
148
-
149
- ####
150
- # RaccMatchParser support machinery (incl. node classes/abstract syntax tree)
151
-
152
- class RaccMatchParser
153
-
154
-
155
- LineupLine = Struct.new( :team, :lineup ) do
156
- def pretty_print( printer )
157
- printer.text( "<LineupLine " )
158
- printer.text( self.team )
159
- printer.text( " lineup=" + self.lineup.pretty_inspect )
160
- printer.text( ">" )
161
- end
162
- end
163
-
164
- Lineup = Struct.new( :name, :card, :sub ) do
165
- def pretty_print( printer )
166
- buf = String.new
167
- buf << self.name
168
- buf << " card=" + self.card.pretty_inspect if card
169
- buf << " sub=" + self.sub.pretty_inspect if sub
170
- printer.text( buf )
171
- end
172
- end
173
-
174
-
175
- Card = Struct.new( :name, :minute ) do
176
- def to_s
177
- buf = String.new
178
- buf << "#{self.name}"
179
- buf << " #{self.minute.to_s}" if self.minute
180
- buf
181
- end
182
-
183
- def pretty_print( printer )
184
- printer.text( to_s )
185
- end
186
- end
187
-
188
-
189
- Sub = Struct.new( :minute, :sub ) do
190
- def pretty_print( printer )
191
- buf = String.new
192
- buf << "(#{self.minute.to_s} "
193
- buf << self.sub.pretty_inspect
194
- buf << ")"
195
- printer.text( buf )
196
- end
197
- end
198
-
199
-
200
-
201
- GroupDef = Struct.new( :name, :teams ) do
202
- def pretty_print( printer )
203
- printer.text( "<GroupDef " )
204
- printer.text( self.name )
205
- printer.text( " teams=" + self.teams.pretty_inspect )
206
- printer.text( ">" )
207
- end
208
- end
209
-
210
-
211
- RoundDef = Struct.new( :name, :date, :duration ) do
212
- def pretty_print( printer )
213
- printer.text( "<RoundDef " )
214
- printer.text( self.name )
215
- printer.text( " date=" + self.date.pretty_inspect ) if date
216
- printer.text( " durattion=" + self.duration.pretty_inspect ) if duration
217
- printer.text( ">" )
218
- end
219
- end
220
-
221
- DateHeader = Struct.new( :date ) do
222
- def pretty_print( printer )
223
- printer.text( "<DateHeader " )
224
- printer.text( "#{self.date.pretty_inspect}>" )
225
- end
226
- end
227
-
228
- GroupHeader = Struct.new( :name ) do
229
- def pretty_print( printer )
230
- printer.text( "<GroupHeader " )
231
- printer.text( "#{self.name}>" )
232
- end
233
- end
234
-
235
- RoundHeader = Struct.new( :names ) do
236
- def pretty_print( printer )
237
- printer.text( "<RoundHeader " )
238
- printer.text( "#{self.names.join(', ')}>" )
239
- end
240
- end
241
-
242
- MatchLine = Struct.new( :ord, :date, :time,
243
- :team1, :team2, :score,
244
- :status,
245
- :geo,
246
- :timezone ) do ## change to geos - why? why not?
247
-
248
- def pretty_print( printer )
249
- printer.text( "<MatchLine " )
250
- printer.text( "#{self.team1} v #{self.team2}")
251
- printer.breakable
252
-
253
- members.zip(values) do |name, value|
254
- next if [:team1, :team2].include?( name )
255
- next if value.nil?
256
-
257
- printer.text( "#{name}=#{value.pretty_inspect}" )
258
- end
259
-
260
- printer.text( ">" )
261
- end
262
-
263
- end
264
-
265
- GoalLine = Struct.new( :goals1, :goals2 ) do
266
- def pretty_print( printer )
267
- printer.text( "<GoalLine " )
268
- printer.text( "goals1=" + self.goals1.pretty_inspect + "," )
269
- printer.breakable
270
- printer.text( "goals2=" + self.goals2.pretty_inspect + ">" )
271
- end
272
- end
273
-
274
- Goal = Struct.new( :player, :minutes ) do
275
- def to_s
276
- buf = String.new
277
- buf << "#{self.player}"
278
- buf << " "
279
- buf << minutes.map { |min| min.to_s }.join(' ')
280
- buf
281
- end
282
-
283
- def pretty_print( printer )
284
- printer.text( to_s )
285
- end
286
-
287
- end
288
-
289
-
290
- ##
291
- ## fix - move :og, :pen to Goal if possible - why? why not?
292
- ## or change to GoalMinute ???
293
- Minute = Struct.new( :m, :offset, :og, :pen ) do
294
- def to_s
295
- buf = String.new
296
- buf << "#{self.m}"
297
- buf << "+#{self.offset}" if self.offset
298
- buf << "'"
299
- buf << "(og)" if self.og
300
- buf << "(pen)" if self.pen
301
- buf
302
- end
303
-
304
- def pretty_print( printer )
305
- printer.text( to_s )
306
- end
307
- end
308
-
309
-
310
-
311
-
312
- def initialize(input)
313
- puts "==> input:"
314
- puts input
315
- @tokenizer = SportDb::Tokenizer.new(input)
316
- end
317
-
318
-
319
- def next_token
320
- tok = @tokenizer.next_token
321
- puts "next_token => #{tok.pretty_inspect}"
322
- tok
323
- end
324
-
325
- # on_error do |error_token_id, error_value, value_stack|
326
- # puts "Parse error on token: #{error_token_id}, value: #{error_value}"
327
- # end
328
-
329
- def parse
330
- puts "parse:"
331
- @tree = []
332
- do_parse
333
- @tree
334
- end
335
-
336
-
337
- def on_error(*args)
338
- puts
339
- puts "!! on parse error:"
340
- puts "args=#{args.pretty_inspect}"
341
- exit 1 ## exit for now - get and print more info about context etc.!!
342
- end
343
-
344
-
345
- =begin
346
- on_error do |error_token_id, error_value, value_stack|
347
- puts "Parse error on token: #{error_token_id}, value: #{error_value}"
348
- end
349
- =end
350
-
351
- end
352
-
353
-
354
65
  puts SportDb::Module::Parser.banner # say hello
355
66
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sportdb-parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.5
4
+ version: 0.5.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gerald Bauer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-01-19 00:00:00.000000000 Z
11
+ date: 2025-01-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cocos
@@ -98,6 +98,8 @@ files:
98
98
  - lib/sportdb/parser.rb
99
99
  - lib/sportdb/parser/lang.rb
100
100
  - lib/sportdb/parser/parser.rb
101
+ - lib/sportdb/parser/racc_parser.rb
102
+ - lib/sportdb/parser/racc_tree.rb
101
103
  - lib/sportdb/parser/token-date.rb
102
104
  - lib/sportdb/parser/token-score.rb
103
105
  - lib/sportdb/parser/token-status.rb