Class: Textpow::SyntaxNode

Inherits:
Object
  • Object
show all
Defined in:
lib/ver/vendor/textpow.rb

Constant Summary collapse

OPTIONS =

:options => Oniguruma::OPTION_CAPTURE_GROUP}

{}
@@syntaxes =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash, syntax = nil, name_space = :default) ⇒ SyntaxNode

Returns a new instance of SyntaxNode.



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/ver/vendor/textpow.rb', line 119

def initialize(hash, syntax = nil, name_space = :default)
  @name_space = name_space

  prepare_scope_name(hash['scopeName'])

  @syntax = syntax || self

  hash.each do |key, value|
    case key
    when "firstLineMatch", "foldingStartMarker", "foldingStopMarker", "match", "begin"
      begin
        send("#{key}=", Regexp.new(value))
      rescue ArgumentError, RegexpError => exception
        warn "Parsing error in %p => %p: %s" % [key, value, exception]
      end
    when "content", "fileTypes", "name", "contentName", "end", "scopeName", "keyEquivalent"
      send("#{key}=", value)
    when "captures", "beginCaptures", "endCaptures"
      send("#{key}=", value.sort)
    when "repository"
      parse_repository value
    when "patterns"
      create_children value
    else
      $stderr.puts "Ignoring: #{key} => #{value.to_s.gsub("\n", "\n>>")}" if $DEBUG
    end
  end
end

Instance Attribute Details

#beginObject

Returns the value of attribute begin.



96
97
98
# File 'lib/ver/vendor/textpow.rb', line 96

def begin
  @begin
end

#beginCapturesObject

Returns the value of attribute beginCaptures.



96
97
98
# File 'lib/ver/vendor/textpow.rb', line 96

def beginCaptures
  @beginCaptures
end

#capturesObject

Returns the value of attribute captures.



96
97
98
# File 'lib/ver/vendor/textpow.rb', line 96

def captures
  @captures
end

#contentObject

Returns the value of attribute content.



96
97
98
# File 'lib/ver/vendor/textpow.rb', line 96

def content
  @content
end

#contentNameObject

Returns the value of attribute contentName.



96
97
98
# File 'lib/ver/vendor/textpow.rb', line 96

def contentName
  @contentName
end

#endObject

Returns the value of attribute end.



96
97
98
# File 'lib/ver/vendor/textpow.rb', line 96

def end
  @end
end

#endCapturesObject

Returns the value of attribute endCaptures.



96
97
98
# File 'lib/ver/vendor/textpow.rb', line 96

def endCaptures
  @endCaptures
end

#fileTypesObject

Returns the value of attribute fileTypes.



96
97
98
# File 'lib/ver/vendor/textpow.rb', line 96

def fileTypes
  @fileTypes
end

#firstLineMatchObject

Returns the value of attribute firstLineMatch.



96
97
98
# File 'lib/ver/vendor/textpow.rb', line 96

def firstLineMatch
  @firstLineMatch
end

#foldingStartMarkerObject

Returns the value of attribute foldingStartMarker.



96
97
98
# File 'lib/ver/vendor/textpow.rb', line 96

def foldingStartMarker
  @foldingStartMarker
end

#foldingStopMarkerObject

Returns the value of attribute foldingStopMarker.



96
97
98
# File 'lib/ver/vendor/textpow.rb', line 96

def foldingStopMarker
  @foldingStopMarker
end

#keyEquivalentObject

Returns the value of attribute keyEquivalent.



96
97
98
# File 'lib/ver/vendor/textpow.rb', line 96

def keyEquivalent
  @keyEquivalent
end

#matchObject

Returns the value of attribute match.



96
97
98
# File 'lib/ver/vendor/textpow.rb', line 96

def match
  @match
end

#nameObject

Returns the value of attribute name.



96
97
98
# File 'lib/ver/vendor/textpow.rb', line 96

def name
  @name
end

#patternsObject

Returns the value of attribute patterns.



96
97
98
# File 'lib/ver/vendor/textpow.rb', line 96

def patterns
  @patterns
end

#processorObject

Returns the value of attribute processor.



96
97
98
# File 'lib/ver/vendor/textpow.rb', line 96

def processor
  @processor
end

#repositoryObject

Returns the value of attribute repository.



96
97
98
# File 'lib/ver/vendor/textpow.rb', line 96

def repository
  @repository
end

#scopeNameObject

Returns the value of attribute scopeName.



96
97
98
# File 'lib/ver/vendor/textpow.rb', line 96

def scopeName
  @scopeName
end

#syntaxObject

Returns the value of attribute syntax.



96
97
98
# File 'lib/ver/vendor/textpow.rb', line 96

def syntax
  @syntax
end

Class Method Details

.load(filename, name_space = :default) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/ver/vendor/textpow.rb', line 101

def self.load(filename, name_space = :default)
  filename = filename.to_s

  table =
    case filename
    when /(\.tmSyntax|\.plist)$/
      Plist::parse_xml(filename)
    when /\.json$/i
      JSON.load(File.read(filename))
    when /\.ya?ml$/i
      YAML.load_file(filename)
    else
      raise ArgumentError, "Unknown filename extension"
    end

  SyntaxNode.new(table, nil, name_space) if table
end

Instance Method Details

#create_children(patterns) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/ver/vendor/textpow.rb', line 184

def create_children(patterns)
  @patterns = []
  syntax = self.syntax

  patterns.each do |pattern|
    if include = pattern["include"]
      @patterns << SyntaxProxy.new(include, syntax)
    else
      @patterns << SyntaxNode.new(pattern, syntax, @name_space)
    end
  end
end

#match_captures(name, match) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/ver/vendor/textpow.rb', line 232

def match_captures(name, match)
  matches = []

  if captures = send(name)
    captures.each do |key, value|
      if key =~ /^\d*$/
        key = key.to_i
        matches << [key, match.offset(key), value["name"]] if key < match.size
      else
        key = key.to_sym
        match_to_key = match.to_index(key)
        matches << [match_to_key, match.offset(key), value["name"]] if match_to_key
      end
    end
  end

  matches
end

#match_end(string, match, position) ⇒ Object



268
269
270
271
272
273
274
275
276
# File 'lib/ver/vendor/textpow.rb', line 268

def match_end(string, match, position)
  regstring = self.end.clone

  regstring.gsub!(/\\([1-9])/){ match[$1.to_i] }
  regstring.gsub!(/\\k<(.*?)>/){ match[$1.to_sym] }
  regstring = '\\\\' if regstring == '\\'

  Regexp.new(regstring).match(string, position)
end

#match_first(string, position) ⇒ Object



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/ver/vendor/textpow.rb', line 251

def match_first(string, position)
  if self.match
    if match = self.match.match(string, position)
      return [self, match]
    end
  elsif self_begin = self.begin
    if match = self_begin.match(string, position)
      return [self, match]
    end
  elsif self.end
  else
    return match_first_son(string, position)
  end

  nil
end

#match_first_son(string, position) ⇒ Object



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/ver/vendor/textpow.rb', line 278

def match_first_son(string, position)
  return unless patterns
  match = nil

  patterns.each do |pattern|
    tmatch = pattern.match_first(string, position)

    next unless tmatch

    if !match || match[1].offset(0).first > tmatch[1].offset(0).first
      match = tmatch
    end
  end

  return match
end

#parse(string, processor = Processor.new) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
# File 'lib/ver/vendor/textpow.rb', line 160

def parse(string, processor = Processor.new)
  processor.start_parsing scopeName

  stack = [[self, nil]]
  string.each_line do |line|
    parse_line(stack, line, processor)
  end

  processor.end_parsing self.scopeName
  processor
end

#parse_captures(name, pattern, match, processor) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/ver/vendor/textpow.rb', line 197

def parse_captures(name, pattern, match, processor)
  all_starts = []
  all_ends = []

  pattern.match_captures(name, match).each do |group, range, match_name|
    range_first = range.first
    next unless range_first

    range_last = range.last
    next if range_first == range_last

    all_starts << [range_first, group, match_name]
    all_ends   << [range_last, -group, match_name]
  end

  starts = all_starts.sort.reverse
  ends = all_ends.sort.reverse

  until starts.empty? && ends.empty?
    if starts.empty?
      pos, key, name = ends.pop
      processor.close_tag name, pos
    elsif ends.empty?
      pos, key, name = starts.pop
      processor.open_tag name, pos
    elsif ends.last[1].abs < starts.last[1]
      pos, key, name = ends.pop
      processor.close_tag name, pos
    else
      pos, key, name = starts.pop
      processor.open_tag name, pos
    end
  end
end

#parse_line(stack, line, processor) ⇒ Object



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# File 'lib/ver/vendor/textpow.rb', line 295

def parse_line(stack, line, processor)
  processor.new_line(line)
  top, match = stack.last
  position = 0

  while true
    if top.patterns
      pattern, pattern_match = top.match_first_son(line, position)
    else
      pattern, pattern_match = nil
    end

    end_match = nil

    if top.end
      end_match = top.match_end(line, match, position)
    end

    if end_match && ( !pattern_match || pattern_match.offset(0).first >= end_match.offset(0).first )
      pattern_match = end_match
      pattern_match_first_offset = pattern_match.offset(0)
      start_pos = pattern_match_first_offset.first
      end_pos = pattern_match_first_offset.last

      top_contentName = top.contentName
      processor.close_tag top_contentName, start_pos if top_contentName

      parse_captures "captures", top, pattern_match, processor
      parse_captures "endCaptures", top, pattern_match, processor

      top_name = top.name
      processor.close_tag top_name, end_pos if top_name

      stack.pop
      top, match = stack.last
    else
      break unless pattern

      start_pos = pattern_match.offset(0).first
      end_pos = pattern_match.offset(0).last
      pattern_name = pattern.name

      if pattern.begin
        processor.open_tag pattern_name, start_pos if pattern_name
        parse_captures "captures", pattern, pattern_match, processor
        parse_captures "beginCaptures", pattern, pattern_match, processor

        pattern_contentName = pattern.contentName
        processor.open_tag pattern_contentName, end_pos if pattern_contentName

        top = pattern
        match = pattern_match
        stack << [top, match]
      elsif pattern.match
        processor.open_tag pattern_name, start_pos if pattern_name
        parse_captures "captures", pattern, pattern_match, processor
        processor.close_tag pattern_name, end_pos if pattern_name
      end
    end

    position = end_pos
  end
end

#parse_repository(repository) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
# File 'lib/ver/vendor/textpow.rb', line 172

def parse_repository(repository)
  @repository = {}

  repository.each do |key, value|
    if include = value["include"]
      @repository[key] = SyntaxProxy.new(include, self.syntax)
    else
      @repository[key] = SyntaxNode.new(value, self.syntax, @name_space)
    end
  end
end

#prepare_scope_name(scopeName) ⇒ Object



148
149
150
151
152
153
154
# File 'lib/ver/vendor/textpow.rb', line 148

def prepare_scope_name(scopeName)
  @@syntaxes[@name_space] ||= {}

  return unless scopeName

  @@syntaxes[@name_space][scopeName] = self
end

#syntaxesObject



156
157
158
# File 'lib/ver/vendor/textpow.rb', line 156

def syntaxes
  @@syntaxes[@name_space]
end