Class: PseudoHiki::InlineParser

Inherits:
TreeStack show all
Includes:
InlineElement
Defined in:
lib/pseudohiki/inlineparser.rb

Direct Known Subclasses

TableRowParser

Defined Under Namespace

Modules: InlineElement

Class Attribute Summary collapse

Attributes inherited from TreeStack

#current_node, #last_leaf, #node_end

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from TreeStack

#accept, #pop, #push, #push_as_child_node, #push_as_leaf, #push_as_sibling, #remove_current_node, #tree

Constructor Details

#initialize(str) ⇒ InlineParser

Returns a new instance of InlineParser.



73
74
75
76
# File 'lib/pseudohiki/inlineparser.rb', line 73

def initialize(str)
  @tokens = PseudoHiki.split_into_tokens(str, self.class.token_pat)
  super()
end

Class Attribute Details

.token_patObject (readonly)

Returns the value of attribute token_pat.



45
46
47
# File 'lib/pseudohiki/inlineparser.rb', line 45

def token_pat
  @token_pat
end

Class Method Details

.parse(str) ⇒ Object



105
106
107
# File 'lib/pseudohiki/inlineparser.rb', line 105

def self.parse(str)
  new(str).parse.tree # parser = new(str)
end

Instance Method Details

#convert_last_node_into_leafObject



78
79
80
81
82
83
# File 'lib/pseudohiki/inlineparser.rb', line 78

def convert_last_node_into_leaf
  last_node = remove_current_node
  tag_head = NodeTypeToHead[last_node.class]
  push InlineLeaf.create(tag_head)
  last_node.each {|leaf| push_as_leaf leaf }
end

#node_in_ancestors?(node_class) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/pseudohiki/inlineparser.rb', line 85

def node_in_ancestors?(node_class)
  not @stack.select {|node| node_class == node.class }.empty?
end

#parseObject



96
97
98
99
100
101
102
103
# File 'lib/pseudohiki/inlineparser.rb', line 96

def parse
  while token = @tokens.shift
    next if TAIL[token] and treated_as_node_end(token)
    next if HEAD[token] and push HEAD[token].new
    push InlineLeaf.create(token)
  end
  self
end

#treated_as_node_end(token) ⇒ Object



89
90
91
92
93
94
# File 'lib/pseudohiki/inlineparser.rb', line 89

def treated_as_node_end(token)
  return pop if current_node.class == TAIL[token]
  return nil unless node_in_ancestors?(TAIL[token])
  convert_last_node_into_leaf until current_node.class == TAIL[token]
  pop
end