lib/qed/parser.rb in qed-2.2.1 vs lib/qed/parser.rb in qed-2.2.2

- old
+ new

@@ -12,50 +12,63 @@ def initialize(file) @lines = File.readlines(file).to_a @ast = [] end - # + # Abstract Syntax Tree attr :ast + # Parse the demo into an abstract syntax tree. # + # TODO: I know there has to be a faster way to do this. def parse - state = :text - linein = 0 - - text = '' - + blocks = [[]] + state = :none @lines.each_with_index do |line, lineno| - if /^\S/ =~ line - if state == :code - add_section(:code, text, linein) - linein = lineno - text = '' + case line + when /^$/ + case state + when :code + blocks.last << line + when :blank + blocks.last << line + else + blocks.last << line + state = :blank end + when /^\s+/ + blocks << [] if state != :code + blocks.last << line + state = :code + else + blocks << [] if state != :text + blocks.last << line state = :text - text << line + end + end + blocks.shift if blocks.first.empty? + + line_cnt = 1 + blocks.each do |block| + text = block.join + case text + when /\A\s+/ + add_section(:code, text, line_cnt) else - if state == :text - next if text.strip.empty? - add_section(:text, text, linein) - linein = lineno - text = '' - end - state = :code - text << line + add_section(:text, text, line_cnt) end + line_cnt += block.size end - add_section(state, text, linein) - @ast.reject!{ |sect| sect.type == :code && sect.text.strip.empty? } + #@ast.reject!{ |sect| sect.type == :code && sect.text.strip.empty? } return @ast end # def add_section(state, text, lineno) case state when :code - if ast.last.raw? + if ast.last && ast.last.cont? @ast.last << text #clean_quote(text) else @ast << CodeSection.new(text, lineno) end else @@ -83,26 +96,34 @@ end end # class TextSection < Section + attr :args + attr :cont + def initialize(text, line, *args) @text = text @line = line @args = args @cont = [] end + + # def <<(text) @cont << clean_continuation(text) @args << block_continuation(text) end + + # def type :text end + # TODO: Use ':' or '...' ? - def raw? + def cont? #/\:\s*\Z/m =~ text /\.\.\.\s*\Z/m =~ text end # Clean up the text, removing unccesseary white lines and triple