Class: SublimeDSL::TextMate::Grammar::BeginEndRule

Inherits:
Rule
  • Object
show all
Defined in:
lib/sublime_dsl/textmate/grammar.rb

Overview

A ‘begin/end’ rule.

Instance Attribute Summary collapse

Attributes inherited from Rule

#comment, #disabled, #scope

Instance Method Summary collapse

Constructor Details

#initializeBeginEndRule

Returns a new instance of BeginEndRule.



152
153
154
155
# File 'lib/sublime_dsl/textmate/grammar.rb', line 152

def initialize
  @patterns = []
  @captures = {}
end

Instance Attribute Details

#capturesObject (readonly)

common captures, a hash { number => scope }



149
150
151
# File 'lib/sublime_dsl/textmate/grammar.rb', line 149

def captures
  @captures
end

#content_scopeObject

Returns the value of attribute content_scope.



145
146
147
# File 'lib/sublime_dsl/textmate/grammar.rb', line 145

def content_scope
  @content_scope
end

#fromObject

Match object



146
147
148
# File 'lib/sublime_dsl/textmate/grammar.rb', line 146

def from
  @from
end

#patternsObject

Returns the value of attribute patterns.



150
151
152
# File 'lib/sublime_dsl/textmate/grammar.rb', line 150

def patterns
  @patterns
end

#toObject

Match object



147
148
149
# File 'lib/sublime_dsl/textmate/grammar.rb', line 147

def to
  @to
end

#to_lastObject

Returns the value of attribute to_last.



148
149
150
# File 'lib/sublime_dsl/textmate/grammar.rb', line 148

def to_last
  @to_last
end

Instance Method Details

#add_capture(scope, index, captures) ⇒ Object



208
209
210
211
212
213
214
215
216
# File 'lib/sublime_dsl/textmate/grammar.rb', line 208

def add_capture(scope, index, captures)
  captures[index] = scope
  h = {}
  captures.keys.sort.each do |i|
    h[i] = captures[i]
  end
  captures.clear
  captures.merge! h
end

#complete!(grammar) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/sublime_dsl/textmate/grammar.rb', line 157

def complete!(grammar)
  captures.each_pair do |index, scope|
    fscope = from.captures[index]
    tscope = to.captures[index]
    if fscope
      if tscope == fscope
        if fscope == scope
          # from scope == to scope == common scope => just keep common
          from.captures.delete(index)
          to.captures.delete(index)
        else
          # from scope == to scope != common scope => set common = from/to
          warn "grammar #{grammar}: 'both' capture #{index} => #{scope.inspect} replaced by 'from/to' capture #{index} => #{fscope.inspect}"
          captures[index] = fscope
          from.captures.delete index
          to.captures.delete index
        end
      elsif tscope.nil?
        if fscope == scope
          # from scope == common scope, no 'to' scope => just keep common
          from.captures.delete(index)
        else
          # from scope != common scope, no 'to' scope => common become 'to'
          warn "grammar #{grammar}: 'both' capture #{index} => #{scope.inspect} moved to 'to' ('from' has #{index} => #{fscope.inspect})"
          add_capture captures.delete(index), index, to.captures
        end
      else
        # from scope != to scope => ignore common
        warn "grammar #{grammar}: 'both' capture #{index} => #{scope.inspect} ignored: 'from' and 'to' already given"
        captures.delete(index)
      end
    elsif tscope.nil?
      # both fscope & tscope nil: ok
    else
      if tscope == scope
        # to scope == common scope, no 'from' scope => just keep common
        to.captures.delete(index)
      else
        # to scope != common scope, no 'from' scope => common become 'from'
        warn "grammar #{grammar}: 'both' capture #{index} => #{scope.inspect} moved to 'from' ('to' has #{index} => #{tscope.inspect})"
        add_capture captures.delete(index), index, from.captures
      end
    end
  end
  if !from.captures.empty? && from.captures == to.captures
    captures.merge! from.captures
    from.captures.clear
    to.captures.clear
  end
end