Class: SublimeDSL::Tools::RegexpWannabe
- Inherits:
-
Object
- Object
- SublimeDSL::Tools::RegexpWannabe
- Defined in:
- lib/sublime_dsl/tools/regexp_wannabe.rb
Overview
A string that would like to be a Regexp. May contain back-references to another RegexpWannabe, stored as ‘§1’, ‘§2’, etc.
Instance Attribute Summary collapse
-
#backref ⇒ Object
readonly
Returns the value of attribute backref.
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#regexp ⇒ Object
readonly
may be nil if error.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
-
#warnings ⇒ Object
readonly
Returns the value of attribute warnings.
Instance Method Summary collapse
-
#fixme_comments(indent = ' ') ⇒ Object
Returns the fixme comments for this regexp.
-
#fixmes ⇒ Object
Returns an array of fixme comments.
-
#initialize(source, backref = nil) ⇒ RegexpWannabe
constructor
FIXME: 3 possible sources: PList, JSON, Ruby Regexp - when from PList, the current way of doing things seems ok - when from JSON, t & n should be converted to \t and \n and back to t & n on output (they don’t use (?x)) - when from Ruby, source should not be altered on input, but the output depends on JSON vs PList.
-
#inspect(rform = false) ⇒ Object
Returns the ruby syntax.
-
#to_s(for_json = false) ⇒ Object
Returns the string syntax.
Constructor Details
#initialize(source, backref = nil) ⇒ RegexpWannabe
FIXME: 3 possible sources: PList, JSON, Ruby Regexp
-
when from PList, the current way of doing things seems ok
-
when from JSON, t & n should be converted to \t and \n and back to t & n on output (they don’t use (?x))
-
when from Ruby, source should not be altered on input, but the output depends on JSON vs PList
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/sublime_dsl/tools/regexp_wannabe.rb', line 28 def initialize(source, backref = nil) # encode in utf-8, remove escaped newlines @source = source.encode('utf-8').gsub(/(^|[^\\](\\\\)*+)\\\n/, '\1') @backref = backref @regexp = nil @error = nil @warnings = [] try_conversion @error = (@error) if @error @source.gsub!("\t", " ") if extended? end |
Instance Attribute Details
#backref ⇒ Object (readonly)
Returns the value of attribute backref.
15 16 17 |
# File 'lib/sublime_dsl/tools/regexp_wannabe.rb', line 15 def backref @backref end |
#error ⇒ Object (readonly)
Returns the value of attribute error.
16 17 18 |
# File 'lib/sublime_dsl/tools/regexp_wannabe.rb', line 16 def error @error end |
#regexp ⇒ Object (readonly)
may be nil if error
14 15 16 |
# File 'lib/sublime_dsl/tools/regexp_wannabe.rb', line 14 def regexp @regexp end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
13 14 15 |
# File 'lib/sublime_dsl/tools/regexp_wannabe.rb', line 13 def source @source end |
#warnings ⇒ Object (readonly)
Returns the value of attribute warnings.
17 18 19 |
# File 'lib/sublime_dsl/tools/regexp_wannabe.rb', line 17 def warnings @warnings end |
Instance Method Details
#fixme_comments(indent = ' ') ⇒ Object
Returns the fixme comments for this regexp. If no fixme, returns an empty string.
91 92 93 |
# File 'lib/sublime_dsl/tools/regexp_wannabe.rb', line 91 def fixme_comments(indent = ' ') fixmes.map { |c| "#{indent}#{c}\n" }.join end |
#fixmes ⇒ Object
Returns an array of fixme comments.
78 79 80 81 82 83 84 85 86 |
# File 'lib/sublime_dsl/tools/regexp_wannabe.rb', line 78 def fixmes comments = [] error and comments << "# FIXME: (error) #{error}" source =~ /#[@${]/ and comments << "# FIXME: (error) '#$&' will be interpreted as interpolation: escape '#' as '\\#'" comments.concat warnings.map { |w| "# FIXME: (warning) #{w}" } comments end |
#inspect(rform = false) ⇒ Object
Returns the ruby syntax.
If #source contains a ‘/’, tries to return an r-form %r'...' or %r:...: or %r!...!. If it does not, or if the #source contains all 3 characters ':!, return /.../ if rform is false (the default), or %r/.../ if rform is true. (The latter syntax avoids warnings when the regexp is the first argument to a method and parentheses are not used around arguments.)
48 49 50 51 52 53 54 55 56 |
# File 'lib/sublime_dsl/tools/regexp_wannabe.rb', line 48 def inspect(rform = false) str = extended? ? source : source.gsub("\t", '\t').gsub("\n", '\n') s = rform ? '%r' : '' return "#{s}/#{str}/" unless str.include?('/') %w(' : !).each { |c| return "%r#{c}#{str}#{c}" unless str.include?(c) } # replace / with \/ unless already escaped escaped = str.gsub( %r"(^|[^\\](\\\\)*+)(?=/)", '\1\\') "#{s}/#{escaped}/" end |
#to_s(for_json = false) ⇒ Object
Returns the string syntax.
-
If
json== false, the string is suitable for PList. -
If
json== true, the string is suitable for JSON output with String#inspect.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/sublime_dsl/tools/regexp_wannabe.rb', line 62 def to_s(for_json = false) if regexp # remove \/ escapes str = regexp.source.gsub( %r"(^|[^\\](\\\\)*+)\\(?=/)", '\1') str = str.gsub("\t", '\t').gsub("\n", '\n') unless extended? if for_json str.gsub!( %r"(^|[^\\](\\\\)*+)\\n", "\\1\n") str.gsub!( %r"(^|[^\\](\\\\)*+)\\t", "\\1\t") end else str = source end str.gsub(/§(?=[1-9])/, '\\') end |