class Regextest::Front::Scanner

Constants

LexCodeLiteral

A table for lexical analysis of elements (Not strict analisis here.)

LexTable

Public Class Methods

new() click to toggle source
# File lib/regextest/front/scanner.rb, line 124
def initialize
  @lex_table = LexTable.dup
  whole_lex = @lex_table.map{|lex| "(?<#{lex[0]}>" + lex[1].source + ")"}.join('|')
  # puts whole_lex
  @reg = /^#{whole_lex}/mx
end
test(test_string, reg_options = nil) click to toggle source

method for testing

# File lib/regextest/front/scanner.rb, line 156
def self.test(test_string, reg_options = nil)
  puts "String: #{test_string.inspect}"
  results = Regextest::Front::Scanner.new().scan(test_string)
  pp results
end

Public Instance Methods

scan(test_string) click to toggle source
# File lib/regextest/front/scanner.rb, line 132
def scan(test_string)
  results = []
  match_string = test_string
  match_offset = 0
  while (md = @reg.match(match_string))
    scan_offset = @lex_table.index{|elem| md[elem[0]]}
    name = @lex_table[scan_offset][0]
    if(name != :LEX_ERROR)
      lex_word = md[name]
      match_length = md.end(0)
      results.push [name, [lex_word, match_offset, match_length]]
      match_string = md.post_match
      match_offset += match_length
    else
      offset = test_string.index(match_string)
      raise "Regexp syntax error, offset #{offset}, \n" + test_string + "\n" + (" "*offset) + "^"
    end
  end
  results.push [false, nil]
  TstLog("Scanned elements:\n#{results}")
  results
end