Class: Rley::Parser::ParseTracer

Inherits:
Object
  • Object
show all
Defined in:
lib/rley/parser/parse_tracer.rb

Overview

Utility class used to trace the parsing of a token sequence.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(aTraceLevel, anIO, aTokenSequence) ⇒ ParseTracer

Returns a new instance of ParseTracer.



17
18
19
20
21
22
23
24
# File 'lib/rley/parser/parse_tracer.rb', line 17

def initialize(aTraceLevel, anIO, aTokenSequence)
  @level = aTraceLevel <= 0 ? 0 : [aTraceLevel, 2].min
  @ostream = anIO
  @lexemes = aTokenSequence.map(&:lexeme)

  emit_tokens
  emit_heading
end

Instance Attribute Details

#col_widthObject (readonly)

Returns the value of attribute col_width.



15
16
17
# File 'lib/rley/parser/parse_tracer.rb', line 15

def col_width
  @col_width
end

#levelObject (readonly)

The trace level



13
14
15
# File 'lib/rley/parser/parse_tracer.rb', line 13

def level
  @level
end

#lexemesObject (readonly)

Returns the value of attribute lexemes.



14
15
16
# File 'lib/rley/parser/parse_tracer.rb', line 14

def lexemes
  @lexemes
end

#ostreamObject (readonly)

The stream where the trace output is sent



10
11
12
# File 'lib/rley/parser/parse_tracer.rb', line 10

def ostream
  @ostream
end

Instance Method Details

Emit the trace text to the output IO if the given trace level is equal or greater to the trace level of the tracer instance.



29
30
31
# File 'lib/rley/parser/parse_tracer.rb', line 29

def print_if(aLevel, text)
  ostream.print(text) if level >= aLevel
end

#trace_completion(aStatesetIndex, aParseState) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rley/parser/parse_tracer.rb', line 49

def trace_completion(aStatesetIndex, aParseState)
  return unless level

  if aStatesetIndex == lexemes.size && aParseState.origin.zero? &&
     aParseState.complete?
    picture = '=' * (col_width * lexemes.size - 1)
  else
    count = col_width * (aStatesetIndex - aParseState.origin) - 1
    picture = '-' * count
  end
  completion_picture = "[#{picture}#{aParseState.complete? ? ']' : '>'}"
  trace_diagram(aStatesetIndex, aParseState, completion_picture)
end

#trace_prediction(aStatesetIndex, aParseState) ⇒ Object



43
44
45
46
47
# File 'lib/rley/parser/parse_tracer.rb', line 43

def trace_prediction(aStatesetIndex, aParseState)
  return unless level

  trace_diagram(aStatesetIndex, aParseState, '>')
end

#trace_scanning(aStatesetIndex, aParseState) ⇒ Object

Emit the trace of a scanning step.



34
35
36
37
38
39
40
41
# File 'lib/rley/parser/parse_tracer.rb', line 34

def trace_scanning(aStatesetIndex, aParseState)
  return unless level

  scan_picture = "[#{'-' * (col_width - 1)}]"
  org = OpenStruct.new(origin: aStatesetIndex - 1,
                       dotted_rule: aParseState.dotted_rule)
  trace_diagram(aStatesetIndex, org, scan_picture)
end