class Mustermann::Visualizer::Highlighter::Regular

Provides highlighting for {Mustermann::Regular} @!visibility private

Constants

SPECIAL_ESCAPE

@!visibility private

Attributes

output[R]

@!visibility private

renderer[R]

@!visibility private

scanner[R]

@!visibility private

Public Class Methods

highlight(pattern, renderer) click to toggle source

@!visibility private

# File lib/mustermann/visualizer/highlighter/regular.rb, line 21
def self.highlight(pattern, renderer)
  new(renderer).highlight(pattern)
end
highlight?(pattern) click to toggle source

@!visibility private

# File lib/mustermann/visualizer/highlighter/regular.rb, line 16
def self.highlight?(pattern)
  pattern.class.name == "Mustermann::Regular"
end
new(renderer) click to toggle source

@!visibility private

# File lib/mustermann/visualizer/highlighter/regular.rb, line 29
def initialize(renderer)
  @renderer = renderer
  @output   = String.new
end

Public Instance Methods

char_class() click to toggle source

@!visibility private

# File lib/mustermann/visualizer/highlighter/regular.rb, line 60
def char_class
  if result = scanner.scan(/\[:\w+:\]\]/)
    element(:special, "[#{result}")
  else
    element(:special, ?[)
    element(:special, ?^) if scanner.scan(/\^/)
  end
end
element(type, content = nil) { || ... } click to toggle source

@!visibility private

# File lib/mustermann/visualizer/highlighter/regular.rb, line 96
def element(type, content = nil)
  output << renderer.pre(type)
  output << renderer.escape(content) if content
  yield if block_given?
  output << renderer.post(type)
end
escaped(char) click to toggle source

@!visibility private

# File lib/mustermann/visualizer/highlighter/regular.rb, line 84
def escaped(char)
  case char
  when  *SPECIAL_ESCAPE    then element(:special, "\\#{char}")
  when 'A', 'Z', 'z'       then element(:illegal, "\\#{char}")
  when 'g'                 then element(:special, "\\#{char}#{scanner.scan(/<\w*>/)}")
  when 'p', 'u'            then element(:special, "\\#{char}#{scanner.scan(/\{[^\}]*\}/)}")
  when ?/                  then element(:separator, char)
  else element(:escaped, ?\\) { element(:escaped_char, char) }
  end
end
highlight(pattern) click to toggle source

@!visibility private

# File lib/mustermann/visualizer/highlighter/regular.rb, line 35
def highlight(pattern)
  output << renderer.pre(:root)
  @scanner = ::StringScanner.new(pattern.to_s)
  scan
  output << renderer.post(:root)
end
potential_capture() click to toggle source

@!visibility private

# File lib/mustermann/visualizer/highlighter/regular.rb, line 70
def potential_capture
  if scanner.scan(/\?<(\w+)>/)
    element(:capture, "(?<") do
      element(:name, scanner[1])
      output << ">" << scan(?))
    end
  elsif scanner.scan(/\?(?:(?:-\w+)?:|>|<=|<!|!|=)/)
    element(:special, "(#{scanner[0]}")
  else
    element(:capture, "(") { output << scan(?)) }
  end
end
scan(stop = nil) click to toggle source

@!visibility private

# File lib/mustermann/visualizer/highlighter/regular.rb, line 43
def scan(stop = nil)
  until scanner.eos?
    case char = scanner.getch
    when stop                then return char
    when ?/                  then element(:separator, char)
    when Regexp.escape(char) then element(:char, char)
    when ?\\                 then escaped(scanner.getch)
    when ?(                  then potential_capture
    when ?[                  then char_class
    when ?^, ?$              then element(:illegal, char)
    when ?{                  then element(:special, "\{#{scanner.scan(/[^\}]*\}/)}")
    else element(:special, char)
    end
  end
end