Class: SublimeDSL::TextMate::Grammar::DSLReader

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

Instance Method Summary collapse

Constructor Details

#initialize(file = nil) ⇒ DSLReader

Returns a new instance of DSLReader.



9
10
11
12
13
# File 'lib/sublime_dsl/textmate/grammar/dsl_reader.rb', line 9

def initialize(file = nil)
  @grammars = []
  @context_stack = []
  instance_eval File.read(file, encoding: 'utf-8'), file if file
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object

Raises:



27
28
29
# File 'lib/sublime_dsl/textmate/grammar/dsl_reader.rb', line 27

def method_missing(sym, *args, &block)
  raise Error, "'#{sym}' is not a valid grammar DSL statement"
end

Instance Method Details

#_contextObject



19
20
21
# File 'lib/sublime_dsl/textmate/grammar/dsl_reader.rb', line 19

def _context
  @context_stack.last
end

#_grammarObject



23
24
25
# File 'lib/sublime_dsl/textmate/grammar/dsl_reader.rb', line 23

def _grammar
  @context_stack.first
end

#_grammarsObject



15
16
17
# File 'lib/sublime_dsl/textmate/grammar/dsl_reader.rb', line 15

def _grammars
  @grammars
end

#both(captures) ⇒ Object



125
126
127
128
# File 'lib/sublime_dsl/textmate/grammar/dsl_reader.rb', line 125

def both(captures)
  ensure_reader
  _context.both captures
end

#bundle_uuid(str) ⇒ Object



77
78
79
80
# File 'lib/sublime_dsl/textmate/grammar/dsl_reader.rb', line 77

def bundle_uuid(str)
  ensure_writer
  _grammar.bundle_uuid = str
end

#content_scope(scope) ⇒ Object



130
131
132
133
# File 'lib/sublime_dsl/textmate/grammar/dsl_reader.rb', line 130

def content_scope(scope)
  ensure_writer
  _context.content_scope = scope
end

#disabled(value) ⇒ Object



140
141
142
143
# File 'lib/sublime_dsl/textmate/grammar/dsl_reader.rb', line 140

def disabled(value)
  ensure_writer
  _context.disabled = value
end

#ensure_reader(method = nil) ⇒ Object



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

def ensure_reader(method = nil)
  name = caller[0].gsub(/.*`(.*)'$/, '\1')
  method ||= name
  _context.respond_to? method or raise invalid_context(name)
end

#ensure_writer(method = nil) ⇒ Object



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

def ensure_writer(method = nil)
  name = caller[0].gsub(/.*`(.*)'$/, '\1')
  method ||= name + '='
  _context.respond_to? method or raise invalid_context(name)
end

#file_types(array) ⇒ Object



47
48
49
50
# File 'lib/sublime_dsl/textmate/grammar/dsl_reader.rb', line 47

def file_types(array)
  ensure_writer
  _grammar.file_types = array
end

#first_line_match(re) ⇒ Object



52
53
54
55
# File 'lib/sublime_dsl/textmate/grammar/dsl_reader.rb', line 52

def first_line_match(re)
  ensure_writer
  _grammar.first_line_match = Tools::RegexpWannabe.new(re.source)
end

#folding_start_marker(re) ⇒ Object



57
58
59
60
# File 'lib/sublime_dsl/textmate/grammar/dsl_reader.rb', line 57

def folding_start_marker(re)
  ensure_writer
  _grammar.folding_start_marker = Tools::RegexpWannabe.new(re.source)
end

#folding_stop_marker(re) ⇒ Object



62
63
64
65
# File 'lib/sublime_dsl/textmate/grammar/dsl_reader.rb', line 62

def folding_stop_marker(re)
  ensure_writer
  _grammar.folding_stop_marker = Tools::RegexpWannabe.new(re.source)
end

#fragment(name) {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



91
92
93
94
95
96
97
98
# File 'lib/sublime_dsl/textmate/grammar/dsl_reader.rb', line 91

def fragment(name)
  ensure_reader :fragments
  f = Fragment.new(name.to_s)   # FIXME pas la peine de sortir des :sym pour faire du to_s!
  _grammar.fragments << f
  @context_stack.push f
  yield self # block.call
  @context_stack.pop
end

#from(re, captures = {}) ⇒ Object



115
116
117
118
# File 'lib/sublime_dsl/textmate/grammar/dsl_reader.rb', line 115

def from(re, captures = {})
  ensure_reader
  _context.from re, captures
end

#include(name_or_module) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/sublime_dsl/textmate/grammar/dsl_reader.rb', line 82

def include(name_or_module)
  if name_or_module.is_a?(Module)
    self.class.send :include, name_or_module
  else
    ensure_reader :patterns
    _context.patterns << Include.new(name_or_module)
  end
end

#invalid_context(name) ⇒ Object



157
158
159
160
161
162
163
164
165
# File 'lib/sublime_dsl/textmate/grammar/dsl_reader.rb', line 157

def invalid_context(name)
  if _context
    c = _context.class.name.split('::').last.snake_case
    c = 'rule' if c == 'rule_builder'
    "#{name} is invalid inside '#{c}'"
  else
    "#{name} is invalid outside 'language'"
  end
end

#key_equivalent(str) ⇒ Object



67
68
69
70
# File 'lib/sublime_dsl/textmate/grammar/dsl_reader.rb', line 67

def key_equivalent(str)
  ensure_writer
  _grammar.key_equivalent = str
end

#language(options = {}, &block) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/sublime_dsl/textmate/grammar/dsl_reader.rb', line 31

def language(options = {}, &block)
  _context and raise Error, "'language' blocks cannot be nested"
  base = options.delete(:file)
  options.length == 0 and raise Error, 'missing name & scope'
  name = options.keys.first
  scope = options.delete(name)
  options.empty? or warn "invalid options: #{options.inspect}"
  g = Grammar.new(name, scope)
  g.basename = base
  @context_stack.push g
  instance_eval(&block)
  @context_stack.pop
  g.complete!
  @grammars << g
end

#match(re, captures = {}) ⇒ Object



110
111
112
113
# File 'lib/sublime_dsl/textmate/grammar/dsl_reader.rb', line 110

def match(re, captures = {})
  ensure_reader
  _context.match re, captures
end

#rule(scope = nil) {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



100
101
102
103
104
105
106
107
108
# File 'lib/sublime_dsl/textmate/grammar/dsl_reader.rb', line 100

def rule(scope = nil)
  ensure_reader :patterns
  b = RuleBuilder.new(scope)
  @context_stack.push b
  yield self # block.call
  @context_stack.pop
  b.rule.complete! _grammar
  _context.patterns << b.rule
end

#to(re, captures = {}) ⇒ Object



120
121
122
123
# File 'lib/sublime_dsl/textmate/grammar/dsl_reader.rb', line 120

def to(re, captures = {})
  ensure_reader
  _context.to re, captures
end

#to_last(value) ⇒ Object



135
136
137
138
# File 'lib/sublime_dsl/textmate/grammar/dsl_reader.rb', line 135

def to_last(value)
  ensure_writer
  _context.to_last = value
end

#uuid(str) ⇒ Object



72
73
74
75
# File 'lib/sublime_dsl/textmate/grammar/dsl_reader.rb', line 72

def uuid(str)
  ensure_writer
  _grammar.uuid = str
end