Class: Fbtxt::Parser

Inherits:
Racc::Parser show all
Includes:
Debuggable
Defined in:
lib/fbtxt/parser/parser.rb,
lib/fbtxt/parser/parse_tree.rb,
lib/fbtxt/parser/parser-top.rb,
lib/fbtxt/parser/parse_tree-match.rb,
lib/fbtxt/parser/parse_tree-props.rb

Defined Under Namespace

Classes: AttendanceLine, BlankLine, Booking, Card, CardsLine, DateHeader, DateHeaderLegs, Goal, GoalAlt, GoalCompat, GoalLine, GoalLineAlt, GoalLineCompat, GoalMinute, GoalType, GroupDef, Heading1, Heading2, Heading3, Lineup, LineupLine, MatchLine, MatchLineBye, MatchLineLegs, Minute, NotaBene, NoteLine, PenaltiesLine, Penalty, Referee, RefereeLine, RoundDef, RoundOutline, 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",
"H1",
"NEWLINE",
"H2",
"H3",
"NOTE",
"NOTA_BENE",
"\"|\"",
"\":\"",
"GROUP_DEF",
"TEAM",
"\",\"",
"ROUND_OUTLINE",
"ROUND_DEF",
"DATE",
"DURATION",
"YEAR",
"DATETIME",
"TIME",
"DATE_LEGS",
"\"@\"",
"GEO",
"\"-\"",
"VS",
"INLINE_NP",
"INLINE_CANC",
"INLINE_WO",
"INLINE_PPD",
"SCORE_FULL",
"SCORE_FULLER",
"SCORE",
"SCORE_AWD",
"SCORE_ABD",
"SCORE_FULLER_MORE",
"INLINE_AWD",
"INLINE_ABD",
"INLINE_VOID",
"INLINE_SUSP",
"TEAM_HOME",
"TEAM_AWAY",
"TEAM_NEUTRAL",
"ORD",
"STATUS",
"INLINE_ROUND_SHORT",
"INLINE_ROUND_BIG",
"INLINE_ATTENDANCE",
"INLINE_ATTENDANCE_SEP",
"INLINE_GOALS",
"GOALS_END",
"INLINE_BYE",
"SCORE_LEGS",
"GOALS",
"\";\"",
"GOAL_SEP_ALT",
"PLAYER",
"GOAL_COUNT",
"GOAL_MINUTE_SEP",
"GOAL_MINUTE",
"GOALS_ALT",
"GOAL_TYPE",
"GOALS_COMPAT",
"MINUTE",
"PROP_ATTENDANCE",
"PROP_NUM",
"PROP_END",
"PROP_REFEREE",
"PROP_NAME",
"ENCLOSED_NAME",
"ATTENDANCE",
"PROP_YELLOWCARDS",
"PROP_REDCARDS",
"PROP_YELLOWREDCARDS",
"PROP_SENTOFF",
"CARDS_NONE_RIGHT",
"CARDS_NONE_LEFT",
"CARDS_SEP_ALT",
"PROP_PENALTIES",
"PROP",
"COACH",
"INLINE_CAPTAIN",
"\"(\"",
"\")\"",
"INLINE_YELLOW",
"INLINE_YELLOW_RED",
"INLINE_RED",
"$start",
"document",
"elements",
"element",
"heading",
"group_def",
"round_def",
"round_outline",
"date_header",
"match_line_with_header",
"match_line",
"date_header_legs",
"match_line_legs",
"note_line",
"nota_bene",
"goal_lines",
"goal_lines_alt",
"goal_lines_compat",
"lineup_lines",
"penalties_lines",
"referee_line",
"attendance_line",
"yellowcard_line",
"redcard_line",
"yellowredcard_line",
"sentoff_line",
"opt_blank_lines",
"blank_lines",
"group_def_sep",
"team_values",
"round_def_sep",
"round_date_opts",
"date",
"datetime",
"time",
"date_datetime",
"geo",
"geo_names",
"match_sep",
"match_fixture",
"match_fixture_not_played",
"match_fixture_postponed",
"SCORE_FULL_OR_FULLER",
"match_result",
"match_fixture_base",
"match_result_base",
"ord",
"opt_ord",
"status",
"note",
"opt_inline_note",
"inline_round_short",
"inline_round_big",
"inline_attendance",
"opt_inline_attendance",
"match_header",
"match_line_header",
"match",
"more_match_header_opts",
"pre_match_opts",
"more_match_opts",
"match_bye",
"goal_lines_body",
"pre_match1",
"pre_match2",
"goals",
"goals_sep",
"opt_goal_sep",
"goal",
"goal_minutes",
"opt_goal_minute_sep",
"goal_minute",
"goals_alt",
"goal_alt",
"goals_compat",
"goal_compat",
"referees",
"attendance_opt",
"referee",
"card_body",
"cards",
"cards_sep",
"player_w_minute",
"opt_card_sep",
"penalties_body",
"penalty_sep",
"penalty",
"lineup",
"opt_coach",
"lineup_sep",
"lineup_name",
"opt_captain",
"opt_inline_cards",
"opt_lineup_sub",
"lineup_sub_props",
"inline_cards",
"lineup_sub",
"lineup_sub_contents",
"inline_yellow",
"inline_yellow_red",
"inline_red" ]
Racc_debug_parser =
false

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Debuggable

#_error, #_info, #_trace, #_warn, #debug?, included

Methods inherited from Racc::Parser

#_racc_do_parse_rb, #_racc_do_reduce, #_racc_evalact, #_racc_init_sysvars, #_racc_setup, #do_parse, #racc_accept, #racc_e_pop, #racc_next_state, #racc_print_stacks, #racc_print_states, #racc_read_token, #racc_reduce, #racc_shift, #racc_token2str, #token_to_str, #yyaccept, #yyerrok, #yyerror

Constructor Details

#initialize(txt) ⇒ Parser

Returns a new instance of Parser.



19
20
21
22
23
24
25
26
27
# File 'lib/fbtxt/parser/parser-top.rb', line 19

def initialize( txt )
  @tree   = []
  @errors = []

  lexer = Lexer.new( txt )
  ##  note - use tokenize_with_errors and add/collect tokenize errors
  @tokens, @errors = lexer.tokenize_with_errors
  ## pp @tokens
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



67
68
69
# File 'lib/fbtxt/parser/parser-top.rb', line 67

def errors
  @errors
end

Instance Method Details

#_reduce_none(val, _values, result) ⇒ Object



2338
2339
2340
# File 'lib/fbtxt/parser/parser.rb', line 2338

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

#errors?Boolean

Returns:

  • (Boolean)


68
69
70
71
# File 'lib/fbtxt/parser/parser-top.rb', line 68

def errors?
   puts "[DEPRECATED] errors?; use new (porcelain/public) Fbtxt.parse api!!"
   @errors.size > 0;
end

#next_tokenObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/fbtxt/parser/parser-top.rb', line 30

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

  ##  convert to racc format single char literal tokens e.g. '@' etc.
  ##                  note - literal token MUST be string (NOT symbol)
  ##    note - racc expects array with to items
  ##               -  item[0] is the token id
  ##               -  item[1] is the token value

  ## note - returns nil for end-of-file !!!
  tok = [tok.type, tok]   if tok

  tok
end

#on_error(error_token_id, error_value, value_stack) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/fbtxt/parser/parser-top.rb', line 74

def on_error(error_token_id, error_value, value_stack)
  ## note - get  error_token (as string) e.g. MINUTE, DATE, etc.
  error_token = Racc_token_to_s_table[error_token_id]

  msg = "parse error on token: #{error_token}"

  ## add error_value  - might be Lexer::Token or value
  if error_value.is_a?( Lexer::Token)
    if error_value.type == :NEWLINE
       ## skip print of "literal" newline
    else
       msg << " >#{error_value.text}<"
    end
    msg << " @#{error_value.lineno}"
  else
    msg << " >#{error_value.inspect}"
  end

  ## add stack - why? why not?
  ## "stack: #{value_stack.inspect}"

  _error( msg )

  @errors << msg
end

#parseObject

convenience shortcut (ignores errors)



61
62
63
64
65
# File 'lib/fbtxt/parser/parser-top.rb', line 61

def parse  ## convenience shortcut (ignores errors)
   puts "[DEPRECATED] parse; use new (porcelain/public) Fbtxt.parse api!!"
   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



51
52
53
54
55
56
57
58
# File 'lib/fbtxt/parser/parser-top.rb', line 51

def parse_with_errors
   _trace( "start parse:" )
   do_parse

   _trace( "#{@tree.size} parse tree node(s): " +  @tree.pretty_inspect)

   [@tree, @errors]
end