Class: Rogdl::Parser

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeParser

Returns a new instance of Parser.



23
24
25
26
27
28
29
30
31
# File 'lib/parser.rb', line 23

def initialize
  @spaces = 0
  @lines = [[]]
  @prev_state = nil
  @escape = false
  @state = InLine
  @indenting = true
  @line_count = 0
end

Instance Attribute Details

#escapeObject

Returns the value of attribute escape.



18
19
20
# File 'lib/parser.rb', line 18

def escape
  @escape
end

#indentingObject

Returns the value of attribute indenting.



18
19
20
# File 'lib/parser.rb', line 18

def indenting
  @indenting
end

#line_countObject

Returns the value of attribute line_count.



18
19
20
# File 'lib/parser.rb', line 18

def line_count
  @line_count
end

#linesObject

Returns the value of attribute lines.



18
19
20
# File 'lib/parser.rb', line 18

def lines
  @lines
end

#prev_stateObject

Returns the value of attribute prev_state.



18
19
20
# File 'lib/parser.rb', line 18

def prev_state
  @prev_state
end

#spacesObject

Returns the value of attribute spaces.



18
19
20
# File 'lib/parser.rb', line 18

def spaces
  @spaces
end

#stateObject

Returns the value of attribute state.



18
19
20
# File 'lib/parser.rb', line 18

def state
  @state
end

Class Method Details

.parse(text) ⇒ Object



55
56
57
58
59
# File 'lib/parser.rb', line 55

def Parser.parse(text)
  p = Parser.new
  p.translate(text)
  return p.convert
end

.parse_file(filename) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/parser.rb', line 33

def Parser.parse_file(filename)
  file = File.new(filename)
  contents = ""
  file.each_byte do |ch|
    contents << ch
  end
  first_line = contents.split("\n").first
  schema = nil
  if first_line[0, SchemaString.length] == SchemaString
    schema = Schema.new(Parser.parse_file(first_line[SchemaString.length, first_line.length]))
  end
  node = Parser.parse(contents)
  if !schema.nil?
    messages = schema.validate(node);
    if messages.length > 0 
      puts filename + ' does not match the supplied schema'
      puts messages
    end
  end
  return node
end

Instance Method Details

#add_lineObject



66
67
68
69
# File 'lib/parser.rb', line 66

def add_line
  @lines << []
  @indenting = true
end

#append(char) ⇒ Object



61
62
63
64
# File 'lib/parser.rb', line 61

def append(char)
  @lines.last.last << char
  @indenting = false
end

#begin_stringObject

def end_string

  @lines.last << ''
end


99
100
101
# File 'lib/parser.rb', line 99

def begin_string
  @lines.last << ''
end

#capture(state) ⇒ Object



91
92
93
# File 'lib/parser.rb', line 91

def capture(state)
  @prev_state = state
end

#changeto(state) ⇒ Object



79
80
81
# File 'lib/parser.rb', line 79

def changeto(state)
  @state = state
end

#complain(message) ⇒ Object



83
84
85
# File 'lib/parser.rb', line 83

def complain(message)
  raise message + "  line count: #{@line_count}"
end

#convertObject



343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'lib/parser.rb', line 343

def convert
  
  nodes = convert_nodes
  
  nodes.each_with_index do |line, i|
    first_in_line = nil
    line.each_with_index do |node, j|
      
      if !node.nil?
        
        if first_in_line.nil?
          first_in_line = node
          #find parent here

          nodes[0..i].reverse.each do |array|

            if !array[j-1].nil? && i != 0
              array[j-1].add(node)
              break
            end
          
          end

        else
          first_in_line.add(node)
        end
      end
    end
  end
#        puts parent.flatten.collect {|n| n.gname}
  return nodes[0][0]
end

#convert_nodesObject



376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/parser.rb', line 376

def convert_nodes
  
  nodes = []
  
  @lines.each_with_index do |line, i|
    if line != []
      temp = []
      nodes << temp
      
      line.each_with_index do |name, j|
        temp[j] = name.to_n unless name.nil?
      end
    end
  end
  return nodes
end

#indentObject



71
72
73
# File 'lib/parser.rb', line 71

def indent
  @lines.last << nil
end

#read(char) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/parser.rb', line 104

def read(char)
  case
    when self.state == InLine
      read_line(char)
      
    when self.state == InString
      read_string(char)
      
    when self.state == InSQString
      read_sq_string(char)

    when self.state == InDQString
      read_dq_string(char)

    when self.state == InPhrase
      read_phrase(char)

    when self.state == InComment
      read_comment(char)

    else
      complain "unexpected state #{@state}"
  end #switch state
end

#read_comment(char) ⇒ Object



330
331
332
333
334
335
336
337
338
339
340
# File 'lib/parser.rb', line 330

def read_comment(char)
    case
      when char == "\n" # New Line
        add_line
        changeto(InLine)
      else
        # ignore characters
        
    end # switch
    
end

#read_dq_string(char) ⇒ Object

read_sq_string



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/parser.rb', line 305

def read_dq_string(char)
  case
    when char == '"' # Double Quotes
      if @escape
        append(char)
        @escape = false
      else
        changeto(@prev_state)
      end
      
    when char == '\\' # Escape
      @escape = true

    when char == "\n" # New Line
      complain "new line is unexpected"
      
    else             # Normal Character
      append(char)
      @escape = false
  end # switch
    
end

#read_line(char) ⇒ Object

read



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/parser.rb', line 129

def read_line(char)
    case
      when char == "'" # Single Quotes
        complain 'invalid indentation ' if @spaces != 0
        begin_string
        capture(InLine)
        changeto(InSQString)

      when char == '"' # Double Quotes
        complain 'invalid indentation ' if @spaces != 0
        begin_string
        capture(InLine)
        changeto(InDQString)
      
      when char == ' ' # Whitespace
        if @indenting
          @spaces += 1
          if @spaces == 4
            @spaces = 0
            indent
          end
        else
          # ignore in line whitespace
        end
      
      when char == '#' # Comment
        changeto(InComment)
      
      when char == "\t" # Tab
        complain "invalid number of white spaces" if @spaces != 0
        indent
        
      when char == "\n" # New Line
        add_line
        @line_count += 1
        @spaces = 0

      when char == "(" # Open parentheses
        count = 0
        @lines.last.each {|n| count += 1 if n.nil?}
        add_line
        indent
        count.times {indent}
        changeto(InPhrase)

      when char == ")" # Close parentheses
        complain "close parentheses is unexpected"

      when char == "," # Comma
        complain "comma is unexpected"

      else             # Normal Character
        complain "invalid indentation parsing character: #{char}" if @spaces != 0
        begin_string
        capture(InLine)
        changeto(InString)
        read(char)
        
    end # switch
    
end

#read_phrase(char) ⇒ Object

read_line



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/parser.rb', line 191

def read_phrase(char)

    case
      when char == "'" # Single Quotes
        capture(InPhrase)
        changeto(InSQString)
        begin_string

      when char == '"' # Double Quotes
        capture(InPhrase)
        changeto(InDQString)
        begin_string
      
      when char == ' ' # Whitespace
        # ignore whitespace
      
      when char == '#' # Comment
        complain "unexpected comment '#' in phrase"
      
      when char == "\t" # Tab
        # ignore Tab
        
      when char == "\n" # New Line
#              complain "unexpected new line character in phrase"
#              add_line
        changeto(InLine)
        read(char)

      when char == "(" # Open parentheses
        complain "Invalid open parentheses"

      when char == ")" # Close parentheses
        changeto(InLine)

      when char == "," # Comma
        count = 0
        @lines.last.each {|n| count += 1 if n.nil?}
        add_line              
        count.times {indent}

      else             # Normal Character
        capture(InPhrase)
        changeto(InString)
        begin_string
        read(char)
        
    end # switch
    
end

#read_sq_string(char) ⇒ Object



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/parser.rb', line 282

def read_sq_string(char)
  case
    when char == "'" # Single Quotes
      if @escape
        append(char)
        @escape = false
      else
        changeto(@prev_state)
      end
      
    when char == '\\' # Escape
      @escape = true

    when char == "\n" # New Line
      complain "new line is unexpected"
      
    else             # Normal Character
      append(char)
      @escape = false
  end # switch
    
end

#read_string(char) ⇒ Object

read_phrase



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
277
278
# File 'lib/parser.rb', line 241

def read_string(char)
    case
      when char == "'" # Single Quotes
        complain "single quotes not allowed inside of Node name"
        
      when char == '"' # Double Quotes
        complain "double quotes not allowed inside of Node name"
        
      when char == ' ' # Whitespace
        changeto(@prev_state)
        
      when char == '#' # Comment
        complain "double quotes not allowed inside of Node name"
        
      when char == '\\' # Escape
        complain "escape is not allowed inside of Node name"

      when char == "\t" # Tab
        complain "tabs not allowed inside of Node name"
        
      when char == "\n" # New Line
        changeto(InLine)
        add_line
        @line_count += 1

      when char == "," # Comma
        complain "unexpected comma" unless @prev_state == InPhrase
        changeto(InPhrase)
        read(char)

      when char == ")" # Close parentheses
        changeto(@prev_state)

      else             # Normal Character
        append(char)
    end # switch
    
end

#to_sObject



75
76
77
# File 'lib/parser.rb', line 75

def to_s
  @lines.to_s
end

#translate(string) ⇒ Object



87
88
89
# File 'lib/parser.rb', line 87

def translate(string)
  string.length.times {|i| read(string[i,1])}
end