Class: Ruty::ParserController

Inherits:
Object
  • Object
show all
Defined in:
lib/ruty/parser.rb

Overview

parser controller. does the actual parsing, used by the tags to control it.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stream, loader, name) ⇒ ParserController

Returns a new instance of ParserController.



203
204
205
206
207
208
209
# File 'lib/ruty/parser.rb', line 203

def initialize stream, loader, name
  @loader = loader
  @name = name
  @tokenstream = stream
  @first = true
  @storage = {}
end

Instance Attribute Details

#firstObject (readonly)

Returns the value of attribute first.



201
202
203
# File 'lib/ruty/parser.rb', line 201

def first
  @first
end

#storageObject (readonly)

Returns the value of attribute storage.



201
202
203
# File 'lib/ruty/parser.rb', line 201

def storage
  @storage
end

#tokenstreamObject (readonly)

Returns the value of attribute tokenstream.



201
202
203
# File 'lib/ruty/parser.rb', line 201

def tokenstream
  @tokenstream
end

Instance Method Details

#fail(msg) ⇒ Object

fail with an template syntax error exception.



212
213
214
# File 'lib/ruty/parser.rb', line 212

def fail msg
  raise TemplateSyntaxError, msg
end

#load_local(name) ⇒ Object

use the loader to load a subtemplate



223
224
225
226
# File 'lib/ruty/parser.rb', line 223

def load_local name
  raise TemplateRuntimeError, 'no loader defined' if not @loader
  @loader.load_local(name, @name)
end

#parse_allObject

parse everything and return the nodelist for it



279
280
281
# File 'lib/ruty/parser.rb', line 279

def parse_all
  parse_until { false }
end

#parse_arguments(arguments) ⇒ Object

alias for the method with the same name on the parser class



218
219
220
# File 'lib/ruty/parser.rb', line 218

def parse_arguments arguments
  Parser::parse_arguments(arguments)
end

#parse_until(&block) ⇒ Object

parse everything until the block returns true



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/ruty/parser.rb', line 229

def parse_until &block
  result = Datastructure::NodeStream.new(self)
  while not @tokenstream.eos?
    token, value = @tokenstream.next

    # text tokens are returned just if the arn't empty
    if token == :text
      @first = false if @first and not value.strip.empty?
      result << Datastructure::TextNode.new(value) \
                if not value.empty?

    # variables leave the parser just if they have just
    # one name and some optional filters on it.
    elsif token == :var
      @first = false
      names = []
      filters = []
      Parser::parse_arguments(value).each do |arg|
        if arg.is_a?(Array)
          filters << arg
        else
          names << arg
        end
      end

      fail('Invalid syntax for variable node') if names.size != 1
      result << Datastructure::VariableNode.new(names[0], filters)

    # blocks are a bit more complicated. first they can act as
    # needle tokens for other blocks, on the other hand blocks
    # can have their own subprogram
    elsif token == :block
      p = value.split(/\s+/, 2)
      name = p[0].to_sym
      args = p[1] || ''
      if block.call(name, args)
        @first = false
        return result.to_nodelist
      end

      tag = Tags[name]
      fail("Unknown tag #{name.inspect}") if tag.nil?
      result << tag.new(self, args)
      @first = false
    end
  end
  result.to_nodelist
end