Sha256: edc36f7a6044089e20f058ae261d2e73c912c1f1e1481930c12aae3446826cb6

Contents?: true

Size: 1.87 KB

Versions: 2

Compression:

Stored size: 1.87 KB

Contents

# encoding: utf-8

# Copyright (C) 2016 Mikio Ikoma

# This routine defines front-end of Regextest
class Regextest::Front; end
require 'regextest/common'
require 'regextest/front/scanner'        # scanner class (for splitting the string)
require 'regextest/front/parser'         # parser class (using racc)
require 'regextest/front/manage-parentheses'   # management class of parentheses
require "json"

# Front end processes
class Regextest::Front
  include Regextest::Common
  def initialize(reg_string, options)
    @options = options
  
    # Json option as a workaround for ruby 1.9.*
    @json_option = { max_nesting: 999 }  
    
    # scanning the string
    scanner = Regextest::Front::Scanner.new
    lex_words = scanner.scan(reg_string)
    
    # initialize management class of parentheses
    @options[:parens] = Regextest::Front::ManageParentheses.new()

    # Prepare parsers (for whole regex parser and bracket parser)
    @parser = RegextestFrontParser.new
    
    # Do parse
    @parse_result = @parser.parse(lex_words, @options)
    
    # process options
    @parse_result.set_options(options)
    
    # sort parentheses, since number of parenthesis is by offset-order (not by parsing-order)
    @options[:parens].sort
    
    @parse_result    
  end

  # Output JSON format parse result of the regex
  def get_object
    json_obj = JSON.parse(@parse_result.json, @json_option)
    parsed_object = {
      "regex"  => json_obj,
      "source" => @options[:reg_source]
    }
    TstLog("JSON param:\n" + JSON.pretty_generate(parsed_object, @json_option))
    parsed_object
  end

  # Return JSON string
  def get_json_string
    JSON.pretty_generate(get_object, @json_option)
  end

  # Return JSON of regex
  def get_json_regex
    JSON.pretty_generate(get_object["regex"], @json_option)
  end

end

# Test suite (execute when this file is specified in command line)
if __FILE__ == $0 
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
regextest-0.1.8 lib/regextest/front.rb
regextest-0.1.6 lib/regextest/front.rb