Class: AdHocTemplate::Parser

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

Defined Under Namespace

Classes: FallbackNode, IterationNode, Leaf, TagNode, TagType, UserDefinedTagTypeConfigError, ValueNode

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, tag) ⇒ Parser

Returns a new instance of Parser.



302
303
304
305
306
# File 'lib/ad_hoc_template/parser.rb', line 302

def initialize(source, tag)
  @tag = tag
  @tokens = PseudoHiki.split_into_tokens(source, @tag.token_pat)
  super()
end

Class Method Details

.parse(str, tag_name = :default) ⇒ Object



234
235
236
237
# File 'lib/ad_hoc_template/parser.rb', line 234

def self.parse(str, tag_name=:default)
  str = remove_indents_and_newlines_if_necessary(str, tag_name)
  new(str, TagType[tag_name]).parse!.tree
end

.register_user_defined_tag_type(config_source) ⇒ Object



239
240
241
242
243
244
245
246
247
248
# File 'lib/ad_hoc_template/parser.rb', line 239

def self.register_user_defined_tag_type(config_source)
  config = YAML.safe_load(config_source, [Symbol])
  check_validity_of_config(config)
  TagType.register(registered_tag_name = config['tag_name'].to_sym,
                   config['tag'],
                   config['iteration_tag'],
                   config['fallback_tag'],
                   config['remove_indent'] || false)
  registered_tag_name
end

Instance Method Details

#parse!Object



308
309
310
311
312
313
314
315
316
317
# File 'lib/ad_hoc_template/parser.rb', line 308

def parse!
  @tokens.each do |token|
    next if @tag.tail[token] == current_node.class && pop
    next if @tag.head[token] && push(@tag.head[token].new)
    push Leaf.create(token)
  end

  @tokens = nil
  self
end