Class: QED::Parser

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

Overview

The parser breaks down a demonstandum into structured object to passed thru the script evaluator.

Technically is defines it’s own markup language but for interoperability sake it …

Defined Under Namespace

Classes: CodeSection, Section, TextSection

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Parser

Returns a new instance of Parser.



12
13
14
15
# File 'lib/qed/parser.rb', line 12

def initialize(file)
  @lines = File.readlines(file).to_a
  @ast = []
end

Instance Attribute Details

#astObject (readonly)

Returns the value of attribute ast.



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

def ast
  @ast
end

Instance Method Details

#add_section(state, text, lineno) ⇒ Object



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

def add_section(state, text, lineno)
  case state
  when :code
    if ast.last.raw?
      @ast.last << text #clean_quote(text)
    else
      @ast << CodeSection.new(text, lineno)
    end
  else
    @ast << TextSection.new(text, lineno)
    #cont = (/\.\.\.\s*^/ =~ text ? true : false)
  end
end

#parseObject



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
# File 'lib/qed/parser.rb', line 21

def parse
  state = :text
  linein = 0

  text  = ''

  @lines.each_with_index do |line, lineno|
    if /^\S/ =~ line
      if state == :code
        add_section(:code, text, linein)
        linein = lineno
        text = ''
      end
      state = :text
      text << line          
    else
      if state == :text
        next if text.strip.empty?
        add_section(:text, text, linein)
        linein = lineno
        text = ''
      end
      state = :code
      text << line          
    end
  end
  add_section(state, text, linein)
  @ast.reject!{ |sect| sect.type == :code && sect.text.strip.empty? }
  return @ast
end