Class: PrisonParser::Utils::Parser

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeParser

Returns a new instance of Parser.



7
8
9
# File 'lib/prison_parser/utils/parser.rb', line 7

def initialize
  @tokens = []
end

Instance Attribute Details

#tokensObject (readonly)

Returns the value of attribute tokens.



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

def tokens
  @tokens
end

Instance Method Details

#load(filepath) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/prison_parser/utils/parser.rb', line 11

def load(filepath)
  line_num = 0
  nodes = []
  @tokens = []
  currentNode = Prison.new

  file = File.open(filepath, "r")

  while !file.eof? do
    line = file.readline.strip
    line_num += 1
    tokenize(line)

    next if tokens.size == 0

    # start a new node
    if "BEGIN" == tokens[0]
      nodes.push(currentNode)

      label = tokens[1]
      currentNode = currentNode.create_node(label)

      if tokens.size > 2
        # inline node
        if tokens.size % 2 != 1
          raise "Unexpected number of tokens in an inline node definition on line #{line_num}"
        end

        unless "END" == tokens[tokens.size - 1]
          raise "Unexpected end of inline node definition on line #{line_num}"
        end
        
        tokens.each_slice(2) do |parts|
          # the first one is bogus :/
          currentNode.add_property(parts[0], parts[1])
        end
        
        upperNode = nodes.pop
        upperNode.finished_reading_node(currentNode)
        currentNode = upperNode
      else
        currentNode.allow_inline = false
      end
    elsif "END" == tokens[0]
      # end of multi-line section
      upperNode = nodes.pop
      upperNode.finished_reading_node(currentNode)
      currentNode = upperNode
    else
      # inside a multi-line section
      currentNode.add_property(tokens[0], tokens[1])
    end
  end

  if nodes.size != 0
    raise "Unexpected end of file!"
  end

  currentNode
end

#tokenize(line) ⇒ Object

Splits a line into a series of tokens



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/prison_parser/utils/parser.rb', line 73

def tokenize(line)
  return if line.size == 0
  
  @tokens.clear
  
  token_start = 0
  i = 0
  chars = line.chars

  while i < line.size do
    c = chars[i]
    if c == ' '
      # eat the spaces!
      if token_start != i
        @tokens << line[token_start, i - token_start]
      end
      token_start = i + 1
    elsif c == '"'
      # skip ahead to the next quote
      end_quotes = line.index('"', i + 1)

      end_quotes = -1 if end_quotes.nil?

      @tokens << line[i + 1, end_quotes - i - 1]
      i = end_quotes
      token_start = i + 1
    else
      # skip ahead to the next space
      next_space = line.index(' ', i)
      next_space = -1 if next_space.nil?
      i = next_space - 1
      break if (i < 0)
    end
    i += 1
  end
  
  if token_start < line.size
    # append the remainder of the string, after we ran out of spaces
    @tokens << line[token_start, line.size - token_start]
  end

  return @tokens
end