Class: XRT::Parser

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

Instance Method Summary collapse

Constructor Details

#initialize(source = '') ⇒ Parser

Returns a new instance of Parser.



5
6
7
# File 'lib/xrt/parser.rb', line 5

def initialize(source='')
  @source = source
end

Instance Method Details

#documentObject



9
10
11
12
13
14
15
16
# File 'lib/xrt/parser.rb', line 9

def document
  doc = XRT::Statement::Document.new

  tokenized = self.tokens

  parse_contents(tokenized, doc)
  doc
end

#parse_contents(tokenized, node) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/xrt/parser.rb', line 18

def parse_contents(tokenized, node)
  while tokenized.length > 0
    statement = XRT::Statement::Factory.new_from_content(tokenized.shift)
    case statement
    when XRT::Statement::Block
      parse_contents(tokenized, statement)
      node << statement
    when XRT::Statement::End
      node << statement
      break
    when XRT::Statement::Text
      node << statement
    when XRT::Statement::Directive
      node << statement
    end
  end

  node
end

#read_directive(source) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/xrt/parser.rb', line 53

def read_directive source
  return nil unless source[0...2] == '[%'

  buffer = ''
  while source[0...2] != '%]'
    buffer << source.slice!(0)
  end
  buffer << source.slice!(0, 2)
  buffer
end

#read_text(source) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/xrt/parser.rb', line 64

def read_text source
  return nil if source[0...2] == '[%'

  buffer = ''
  while true
    return buffer if source[0...2] == '[%'
    break if source.length < 2
    buffer << source.slice!(0)
  end

  buffer << source.slice!(0, source.length)

  buffer
end

#tokensObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/xrt/parser.rb', line 38

def tokens
  reading = @source.dup
  result = []

  while reading.length > 0
    got = read_directive(reading) || read_text(reading)
    unless got
      raise "failed to parse #{@source}"
    end
    result << got
  end

  result
end