Class: RaccMatchParser

Inherits:
Racc::Parser
  • Object
show all
Defined in:
lib/sportdb/parser/racc_tree.rb,
lib/sportdb/parser/parser.rb,
lib/sportdb/parser/racc_parser.rb

Overview

RaccMatchParser support machinery (incl. node classes/abstract syntax tree)

Defined Under Namespace

Classes: Card, DateHeader, Goal, GoalLine, GroupDef, GroupHeader, Lineup, LineupLine, MatchLine, Minute, RoundDef, RoundHeader, Sub

Constant Summary collapse

Racc_arg =
[
racc_action_table,
racc_action_check,
racc_action_default,
racc_action_pointer,
racc_goto_table,
racc_goto_check,
racc_goto_default,
racc_goto_pointer,
racc_nt_base,
racc_reduce_table,
racc_token_table,
racc_shift_n,
racc_reduce_n,
racc_use_result_var ]
Racc_token_to_s_table =
[
"$end",
"error",
"BLANK",
"TEAMS",
"NEWLINE",
"\"---\"",
"TEXT",
"\"--\"",
"\"-\"",
"PROP",
"PROP_END",
"\",\"",
"PROP_NAME",
"\"(\"",
"MINUTE",
"\")\"",
"\"[\"",
"\"]\"",
"YELLOW_CARD",
"RED_CARD",
"GROUP_DEF",
"\"|\"",
"TEAM",
"ROUND_DEF",
"DATE",
"DURATION",
"DATETIME",
"GROUP",
"\"/\"",
"ROUND",
"ORD",
"TIME",
"WDAY",
"STATUS",
"NOTE",
"\"@\"",
"TIMEZONE",
"VS",
"SCORE",
"SCORE_MORE",
"PLAYER",
"\";\"",
"OG",
"PEN",
"$start",
"document",
"elements",
"element",
"date_header",
"group_header",
"round_header",
"group_def",
"round_def",
"match_line",
"goal_lines",
"goal_lines_alt",
"teams_list",
"lineup_lines",
"list",
"list_item",
"lineup",
"lineup_name",
"lineup_sep",
"lineup_name_opts",
"card",
"lineup_sub",
"card_body",
"card_type",
"team_values",
"round_date_opts",
"date_header_body",
"date_header_date",
"geo_opts",
"round_values",
"group_sep",
"round_sep",
"match_opts",
"match",
"more_match_opts",
"more_matches",
"date_opts",
"geo_values",
"match_result",
"match_fixture",
"match_sep",
"score",
"goals_alt",
"goal_alt",
"minute",
"goal_lines_body",
"goals",
"goal_sep",
"goal",
"minutes",
"minute_opts" ]
Racc_debug_parser =
false

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(txt, debug: false) ⇒ RaccMatchParser

Returns a new instance of RaccMatchParser.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/sportdb/parser/racc_parser.rb', line 8

def initialize( txt,  debug: false )
  ## puts "==> txt:"
  ## puts txt
 
  @tree   = [] 
  @errors = []

  ### todo:
  ##  -  pass along debug flag
  lexer = SportDb::Lexer.new( txt, debug: debug )
  ##  note - use tokenize_with_errors and add/collect tokenize errors
  @tokens, @errors = lexer.tokenize_with_errors
  ## pp @tokens
  
  ## quick hack - convert to racc format single char literal tokens e.g. '@' etc.
  @tokens = @tokens.map do |tok|
               if tok.size == 1
                 [tok[0].to_s, tok[0].to_s]
               else 
                 tok
               end
             end
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



66
67
68
# File 'lib/sportdb/parser/racc_parser.rb', line 66

def errors
  @errors
end

Instance Method Details

#_reduce_none(val, _values, result) ⇒ Object



1147
1148
1149
# File 'lib/sportdb/parser/parser.rb', line 1147

def _reduce_none(val, _values, result)
  val[0]
end

#debug(value) ⇒ Object



33
# File 'lib/sportdb/parser/racc_parser.rb', line 33

def debug( value ) @debug = value; end

#debug?Boolean

Returns:

  • (Boolean)


34
# File 'lib/sportdb/parser/racc_parser.rb', line 34

def debug?()  @debug == true; end

#errors?Boolean

Returns:

  • (Boolean)


67
# File 'lib/sportdb/parser/racc_parser.rb', line 67

def errors?()   @errors.size > 0; end

#next_tokenObject



44
45
46
47
48
# File 'lib/sportdb/parser/racc_parser.rb', line 44

def next_token
  tok = @tokens.shift
  trace( "next_token => #{tok.pretty_inspect}" )
  tok
end

#on_error(error_token_id, error_value, value_stack) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/sportdb/parser/racc_parser.rb', line 70

def on_error(error_token_id, error_value, value_stack)
  ## auto-add error_token (as string)
  error_token = Racc_token_to_s_table[error_token_id] 
  args = [error_token, error_token_id, error_value, value_stack]
  puts
  puts "!! on parse error:"
  puts "args=#{args.pretty_inspect}"

  @errors << "parse error on token: #{error_token} (#{error_token_id}) with value: #{error_value}, stack: #{value_stack.pretty_inspect}" 
  ## exit 1  ##   exit for now  -  get and print more info about context etc.!!
end

#parseObject

convenience shortcut (ignores errors)



60
61
62
63
# File 'lib/sportdb/parser/racc_parser.rb', line 60

def parse  ## convenience shortcut (ignores errors)
  tree, _ = parse_with_errors
  tree 
end

#parse_with_errorsObject

on_error do |error_token_id, error_value, value_stack|

    puts "Parse error on token: #{error_token_id}, value: #{error_value}"
end


54
55
56
57
58
# File 'lib/sportdb/parser/racc_parser.rb', line 54

def parse_with_errors    
   trace( "start parse:" )
   do_parse
   [@tree, @errors]
end

#trace(msg) ⇒ Object

debug - trace / print message



37
38
39
# File 'lib/sportdb/parser/racc_parser.rb', line 37

def trace( msg )
   puts "  [parse] " + msg    if debug?
end