Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve readability of lexer debug outputs #1240

Merged
merged 2 commits into from
Jul 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions lib/rouge/regex_lexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,29 +102,29 @@ def rule(re, tok=nil, next_state=nil, &callback)
callback ||= case next_state
when :pop!
proc do |stream|
puts " yielding #{tok.qualname}, #{stream[0].inspect}" if @debug
puts " yielding: #{tok.qualname}, #{stream[0].inspect}" if @debug
@output_stream.call(tok, stream[0])
puts " popping stack: 1" if @debug
@stack.pop or raise 'empty stack!'
end
when :push
proc do |stream|
puts " yielding #{tok.qualname}, #{stream[0].inspect}" if @debug
puts " yielding: #{tok.qualname}, #{stream[0].inspect}" if @debug
@output_stream.call(tok, stream[0])
puts " pushing :#{@stack.last.name}" if @debug
@stack.push(@stack.last)
end
when Symbol
proc do |stream|
puts " yielding #{tok.qualname}, #{stream[0].inspect}" if @debug
puts " yielding: #{tok.qualname}, #{stream[0].inspect}" if @debug
@output_stream.call(tok, stream[0])
state = @states[next_state] || self.class.get_state(next_state)
puts " pushing :#{state.name}" if @debug
@stack.push(state)
end
when nil
proc do |stream|
puts " yielding #{tok.qualname}, #{stream[0].inspect}" if @debug
puts " yielding: #{tok.qualname}, #{stream[0].inspect}" if @debug
@output_stream.call(tok, stream[0])
end
else
Expand Down Expand Up @@ -263,6 +263,7 @@ def stream_tokens(str, &b)

until stream.eos?
if @debug
puts
puts "lexer: #{self.class.tag}"
puts "stack: #{stack.map(&:name).map(&:to_sym).inspect}"
puts "stream: #{stream.peek(20).inspect}"
Expand All @@ -289,11 +290,11 @@ def stream_tokens(str, &b)
def step(state, stream)
state.rules.each do |rule|
if rule.is_a?(State)
puts " entering mixin #{rule.name}" if @debug
puts " entering: mixin :#{rule.name}" if @debug
return true if step(rule, stream)
puts " exiting mixin #{rule.name}" if @debug
puts " exiting: mixin :#{rule.name}" if @debug
else
puts " trying #{rule.inspect}" if @debug
puts " trying: #{rule.inspect}" if @debug

# XXX HACK XXX
# StringScanner's implementation of ^ is b0rken.
Expand All @@ -303,14 +304,14 @@ def step(state, stream)
next if rule.beginning_of_line && !stream.beginning_of_line?

if (size = stream.skip(rule.re))
puts " got #{stream[0].inspect}" if @debug
puts " got: #{stream[0].inspect}" if @debug

instance_exec(stream, &rule.callback)

if size.zero?
@null_steps += 1
if @null_steps > MAX_NULL_SCANS
puts " too many scans without consuming the string!" if @debug
puts " warning: too many scans without consuming the string!" if @debug
return false
end
else
Expand Down Expand Up @@ -362,7 +363,7 @@ def groups(*tokens)
# @param [String] text
# The text to delegate. This defaults to the last matched string.
def delegate(lexer, text=nil)
puts " delegating to #{lexer.inspect}" if @debug
puts " delegating to: #{lexer.inspect}" if @debug
text ||= @current_stream[0]

lexer.continue_lex(text) do |tok, val|
Expand All @@ -388,7 +389,7 @@ def push(state_name=nil, &b)
self.state
end

puts " pushing :#{push_state.name}" if @debug
puts " pushing: :#{push_state.name}" if @debug
stack.push(push_state)
end

Expand All @@ -408,7 +409,7 @@ def pop!(times=1)
def goto(state_name)
raise 'empty stack!' if stack.empty?

puts " going to state :#{state_name} " if @debug
puts " going to: state :#{state_name} " if @debug
stack[-1] = get_state(state_name)
end

Expand All @@ -435,7 +436,7 @@ def state?(state_name)
private
def yield_token(tok, val)
return if val.nil? || val.empty?
puts " yielding #{tok.qualname}, #{val.inspect}" if @debug
puts " yielding: #{tok.qualname}, #{val.inspect}" if @debug
@output_stream.yield(tok, val)
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/rouge/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ def reset!
def starts_block(block_state)
@block_state = block_state
@block_indentation = @last_indentation || ''
puts " starts_block #{block_state.inspect}" if @debug
puts " starts_block: #{block_state.inspect}" if @debug
puts " block_indentation: #{@block_indentation.inspect}" if @debug
end

# handle a single indented line
def indentation(indent_str)
puts " indentation #{indent_str.inspect}" if @debug
puts " indentation: #{indent_str.inspect}" if @debug
puts " block_indentation: #{@block_indentation.inspect}" if @debug
@last_indentation = indent_str

Expand Down