lib/luobo/token.rb in luobo-0.0.2 vs lib/luobo/token.rb in luobo-0.0.5

- old
+ new

@@ -1,28 +1,42 @@ -module Luobo - ## this class holds a block of carrot source code tokenized by the parser. - class Token - attr_accessor :ln, :line, :indent_level, :processor_name, :line_code, :block_code +## this class holds a block of carrot source code tokenized by the parser. +class Token + attr_accessor :ln, :line, :indent_level, # raw line and line number + :processor_name, :line_code, :blocks, :indent_level - def initialize ln, line, indent_level, processor_name, line_code, block_code - @ln, @line, @indent_level, @processor_name, @line_code, @block_code = ln, line, indent_level, processor_name, line_code, block_code - end - - # add a line to current block args, separate each line with "\n" - def add_block_code line - line.chomp! - if self.block_code - self.block_code += "\n" + line - else - self.block_code = line - end - end + def initialize ln, line, indent_level, processor_name, line_code = '', block_open = false + @ln, @line, @indent_level, @processor_name, @line_code = ln, line, indent_level, processor_name, line_code + @blocks = Array.new if block_open + end - def block_args - YAML.load(block_code) - end + def indent + " " * indent_level + end - def line_args - YAML.load(line_code) + # add a line to current block args, separate each line with "\n" + def add_block_code line + raise "block not opened in line #{:ln}" unless block_open? + line.chomp.split("\n").each do |ln| + @blocks << ln end + end + + def has_block? + (@blocks and @blocks.size > 0) ? true : false + end + + def block_open? + @blocks.is_a?(Array) + end + + def block_code + has_block? ? @blocks.join("\n") : "" + end + + def block_args + YAML.load(block_code) + end + + def line_args + YAML.load(line_code) end end