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



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

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.



17
18
19
# File 'lib/rley/parser/parse_tracer.rb', line 17

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.



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

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.



31
32
33
# File 'lib/rley/parser/parse_tracer.rb', line 31

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

#trace_completion(aStatesetIndex, aParseState) ⇒ Object



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

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



45
46
47
48
49
# File 'lib/rley/parser/parse_tracer.rb', line 45

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.



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

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