Class: Textpow::SyntaxNode
- Inherits:
-
Object
- Object
- Textpow::SyntaxNode
- Defined in:
- lib/ver/vendor/textpow.rb
Constant Summary collapse
- LITERAL_KEYS =
[:firstLineMatch, :foldingStartMarker, :foldingStopMarker, :match, :begin, :content, :fileTypes, :name, :contentName, :end, :scopeName, :keyEquivalent]
- @@syntaxes =
{}
Instance Attribute Summary collapse
-
#begin ⇒ Object
Returns the value of attribute begin.
-
#beginCaptures ⇒ Object
Returns the value of attribute beginCaptures.
-
#captures ⇒ Object
Returns the value of attribute captures.
-
#content ⇒ Object
Returns the value of attribute content.
-
#contentName ⇒ Object
Returns the value of attribute contentName.
-
#end ⇒ Object
Returns the value of attribute end.
-
#endCaptures ⇒ Object
Returns the value of attribute endCaptures.
-
#fileTypes ⇒ Object
Returns the value of attribute fileTypes.
-
#firstLineMatch ⇒ Object
Returns the value of attribute firstLineMatch.
-
#foldingStartMarker ⇒ Object
Returns the value of attribute foldingStartMarker.
-
#foldingStopMarker ⇒ Object
Returns the value of attribute foldingStopMarker.
-
#keyEquivalent ⇒ Object
Returns the value of attribute keyEquivalent.
-
#match ⇒ Object
Returns the value of attribute match.
-
#name ⇒ Object
Returns the value of attribute name.
-
#patterns ⇒ Object
Returns the value of attribute patterns.
-
#processor ⇒ Object
Returns the value of attribute processor.
-
#repository ⇒ Object
Returns the value of attribute repository.
-
#scopeName ⇒ Object
Returns the value of attribute scopeName.
-
#syntax ⇒ Object
Returns the value of attribute syntax.
Class Method Summary collapse
Instance Method Summary collapse
- #create_children(patterns) ⇒ Object
-
#initialize(hash, syntax = nil, name_space = :default) ⇒ SyntaxNode
constructor
A new instance of SyntaxNode.
- #match_captures(name, match) ⇒ Object
- #match_end(string, match, position) ⇒ Object
- #match_first(string, position) ⇒ Object
- #match_first_son(string, position) ⇒ Object
- #parse(string, processor = Processor.new) ⇒ Object
- #parse_captures(name, pattern, match, processor) ⇒ Object
- #parse_line(stack, line, processor) ⇒ Object
- #parse_repository(repository) ⇒ Object
- #prepare_scope_name(scopeName) ⇒ Object
- #syntaxes ⇒ Object
Constructor Details
#initialize(hash, syntax = nil, name_space = :default) ⇒ SyntaxNode
Returns a new instance of SyntaxNode.
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/ver/vendor/textpow.rb', line 123 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 *LITERAL_KEYS 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
#begin ⇒ Object
Returns the value of attribute begin.
118 119 120 |
# File 'lib/ver/vendor/textpow.rb', line 118 def begin @begin end |
#beginCaptures ⇒ Object
Returns the value of attribute beginCaptures.
118 119 120 |
# File 'lib/ver/vendor/textpow.rb', line 118 def beginCaptures @beginCaptures end |
#captures ⇒ Object
Returns the value of attribute captures.
118 119 120 |
# File 'lib/ver/vendor/textpow.rb', line 118 def captures @captures end |
#content ⇒ Object
Returns the value of attribute content.
118 119 120 |
# File 'lib/ver/vendor/textpow.rb', line 118 def content @content end |
#contentName ⇒ Object
Returns the value of attribute contentName.
118 119 120 |
# File 'lib/ver/vendor/textpow.rb', line 118 def contentName @contentName end |
#end ⇒ Object
Returns the value of attribute end.
118 119 120 |
# File 'lib/ver/vendor/textpow.rb', line 118 def end @end end |
#endCaptures ⇒ Object
Returns the value of attribute endCaptures.
118 119 120 |
# File 'lib/ver/vendor/textpow.rb', line 118 def endCaptures @endCaptures end |
#fileTypes ⇒ Object
Returns the value of attribute fileTypes.
118 119 120 |
# File 'lib/ver/vendor/textpow.rb', line 118 def fileTypes @fileTypes end |
#firstLineMatch ⇒ Object
Returns the value of attribute firstLineMatch.
118 119 120 |
# File 'lib/ver/vendor/textpow.rb', line 118 def firstLineMatch @firstLineMatch end |
#foldingStartMarker ⇒ Object
Returns the value of attribute foldingStartMarker.
118 119 120 |
# File 'lib/ver/vendor/textpow.rb', line 118 def foldingStartMarker @foldingStartMarker end |
#foldingStopMarker ⇒ Object
Returns the value of attribute foldingStopMarker.
118 119 120 |
# File 'lib/ver/vendor/textpow.rb', line 118 def foldingStopMarker @foldingStopMarker end |
#keyEquivalent ⇒ Object
Returns the value of attribute keyEquivalent.
118 119 120 |
# File 'lib/ver/vendor/textpow.rb', line 118 def keyEquivalent @keyEquivalent end |
#match ⇒ Object
Returns the value of attribute match.
118 119 120 |
# File 'lib/ver/vendor/textpow.rb', line 118 def match @match end |
#name ⇒ Object
Returns the value of attribute name.
118 119 120 |
# File 'lib/ver/vendor/textpow.rb', line 118 def name @name end |
#patterns ⇒ Object
Returns the value of attribute patterns.
118 119 120 |
# File 'lib/ver/vendor/textpow.rb', line 118 def patterns @patterns end |
#processor ⇒ Object
Returns the value of attribute processor.
118 119 120 |
# File 'lib/ver/vendor/textpow.rb', line 118 def processor @processor end |
#repository ⇒ Object
Returns the value of attribute repository.
118 119 120 |
# File 'lib/ver/vendor/textpow.rb', line 118 def repository @repository end |
#scopeName ⇒ Object
Returns the value of attribute scopeName.
118 119 120 |
# File 'lib/ver/vendor/textpow.rb', line 118 def scopeName @scopeName end |
#syntax ⇒ Object
Returns the value of attribute syntax.
118 119 120 |
# File 'lib/ver/vendor/textpow.rb', line 118 def syntax @syntax end |
Class Method Details
.load(filename, name_space = :default) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/ver/vendor/textpow.rb', line 94 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) when /\.rb$/i eval(File.read(filename)) else raise ArgumentError, "Unknown filename extension" end SyntaxNode.new(table, nil, name_space) if table end |
Instance Method Details
#create_children(patterns) ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/ver/vendor/textpow.rb', line 182 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
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/ver/vendor/textpow.rb', line 230 def match_captures(name, match) matches = [] if captures = send(name) captures.each do |key, value| if Symbol === key key = key.to_sym match_to_key = match.to_index(key) matches << [match_to_key, match.offset(key), value[:name]] if match_to_key else key = key.to_i matches << [key, match.offset(key), value[:name]] if key < match.size end end end matches end |
#match_end(string, match, position) ⇒ Object
266 267 268 269 270 271 272 273 274 |
# File 'lib/ver/vendor/textpow.rb', line 266 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
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/ver/vendor/textpow.rb', line 249 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
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
# File 'lib/ver/vendor/textpow.rb', line 276 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
158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/ver/vendor/textpow.rb', line 158 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
195 196 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 |
# File 'lib/ver/vendor/textpow.rb', line 195 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
293 294 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 358 359 360 |
# File 'lib/ver/vendor/textpow.rb', line 293 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 if position >= end_pos # raise "Parser didn't move forward on line: %p" % [line] return else position = end_pos end end end |
#parse_repository(repository) ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/ver/vendor/textpow.rb', line 170 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
146 147 148 149 150 151 152 |
# File 'lib/ver/vendor/textpow.rb', line 146 def prepare_scope_name(scopeName) @@syntaxes[@name_space] ||= {} return unless scopeName @@syntaxes[@name_space][scopeName] = self end |
#syntaxes ⇒ Object
154 155 156 |
# File 'lib/ver/vendor/textpow.rb', line 154 def syntaxes @@syntaxes[@name_space] end |