lib/regextest/front/letter.rb in regextest-0.1.5 vs lib/regextest/front/letter.rb in regextest-0.1.6

- old
+ new

@@ -36,21 +36,26 @@ @data_type = :LEX_CHAR @obj = CharClass.new([ TRange.new(val)]) when :LEX_SIMPLE_ESCAPE @data_type = :LEX_CHAR @obj = CharClass.new([ TRange.new(val[1..1])]) - when :LEX_CODE_LITERAL, :LEX_ESCAPED_LETTER, :LEX_UNICODE, :LEX_CONTROL_LETTER, :LEX_META_LETTER, :LEX_OCTET + when :LEX_CODE_LITERAL, :LEX_ESCAPED_LETTER, :LEX_UNICODE, :LEX_OCTET @data_type = :LEX_CHAR @obj = CharClass.new([ TRange.new(eval('"'+ val + '"'))]) # convert using ruby's eval + when :LEX_CONTROL_LETTER, :LEX_META_LETTER + @data_type = :LEX_CHAR + @obj = generate_control_letter(val, type) when :LEX_BRACKET @obj = Regextest::Front::Bracket.new(val) when :LEX_SIMPLIFIED_CLASS @obj = generate_simplified_class(val) when :LEX_POSIX_CHAR_CLASS @obj = generate_char_class(val) when :LEX_UNICODE_CLASS - @obj = generate_unicode_char(val) + @obj = generate_unicode_char(val, type) + when :LEX_UNICODE_CLASS_BRACKET + @obj = generate_unicode_char(val, type) when :LEX_ANY_LETTER @obj = generate_any_char(val) when :LEX_SPECIAL_LETTER @obj = generate_special_char(val) when :LEX_AND_AND @@ -59,18 +64,44 @@ else raise "Error: internal error, type:#{type} not implemented" end end + # generate control letter \c-x, \m-x + def generate_control_letter(val, type) + suffix = val[-1..-1] + codepoint = suffix.unpack("U*")[0] + case type + when :LEX_CONTROL_LETTER + if ('0'..'?').include?(suffix) + result = codepoint - 0x20 + elsif ('@'..'_').include?(suffix) + result = codepoint - 0x40 + elsif ('`'..'~').include?(suffix) + result = codepoint - 0x60 + else + raise "Internal error: invalid control letter (#{val})" + end + when :LEX_META_LETTER + result = codepoint + 0x80 + pp [result].pack("U*") + else + raise "Internal error: invalid type #{type}" + end + @obj = CharClass.new([ TRange.new([result].pack("U*"))]) + end + # generate whole set of letters (depends on option) def generate_any_char(val) if @options[:reg_options].is_unicode? obj = CharClass.new(TstConstUnicodeCharSet) else - obj = CharClass.new( [ TRange.new("\x20", "\x7e") ] ) + obj = CharClass.new(TstConstUnicodeCharSet) + # obj = CharClass.new( [ TRange.new("\x20", "\x7e") ] ) end + # add new-line if multi-line option specified if( @options[:reg_options].is_multiline? ) obj.add_ranges( [ TRange.new("\n") ] ) end obj end @@ -175,16 +206,16 @@ end obj end # generate Unicode class (ie. \p{...} | \P{...}) - def generate_unicode_char(val) + def generate_unicode_char(val, type) if(md = val.match(/(p|P)\{(\^?)(\w+)\}/)) class_name = md[3].downcase reverse = (md[2] && md[2]=="^")?true:false - obj = CharClass.new(class_name) + obj = CharClass.new(class_name, type) else raise "Internal error, inconsistent Unicode class #{val}" end # ¥P{^...} is equivalent to \p{...} @@ -295,12 +326,14 @@ end # transform to json format def json @@id += 1 + charset = @options[:reg_options].charset "{" + "\"type\": \"#{@data_type}\", \"id\": \"L#{@@id}\", \"value\": #{@obj.json}, " + - "\"offset\": #{@offset}, \"length\": #{@length}" + + "\"offset\": #{@offset}, \"length\": #{@length}, " + + "\"charset\": \"#{charset}\"" + "}" end end end