Class: Eggshell::ParseTree
- Inherits:
-
Object
- Object
- Eggshell::ParseTree
- Defined in:
- lib/eggshell/parse-tree.rb
Overview
Holds a hierarchical collection of blocks and macros. The tree follows this structure:
pre. [ String*, [:block, ‘block_name’, [], line_start]* # last entry is array of Eggshell::Line, [:macro, ‘macro_name’, {}, [ __repeat_of_parse_tree ] ], line_start* ]
Constant Summary collapse
- BH =
Eggshell::BlockHandler
- IDX_TYPE =
0
- IDX_NAME =
1
- IDX_ARGS =
2
- IDX_LINES =
3
- IDX_LINES_START =
4
- IDX_LINES_END =
5
Class Method Summary collapse
-
.walk(struct = nil, indent = 0, out = nil) ⇒ Object
Does basic output of parse tree structure to visually inspect parsed info.
Instance Method Summary collapse
- #collect(line_obj) ⇒ Object
-
#initialize ⇒ ParseTree
constructor
A new instance of ParseTree.
- #macro_delim_match(line_obj, line_num) ⇒ Object
- #mode ⇒ Object
- #new_block(handler, type, line_obj, consume_mode, line_start) ⇒ Object
- #new_macro(line_obj, line_start) ⇒ Object
- #push_block ⇒ Object
- #raw_line(line_obj) ⇒ Object
- #tree ⇒ Object
Constructor Details
#initialize ⇒ ParseTree
Returns a new instance of ParseTree.
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/eggshell/parse-tree.rb', line 22 def initialize #@eggshell = eggshell @modes = [:nil] @tree = [] @lines = [] @macro_delims = [] @macro_open = [] @macro_ptr = [] @ptr = @tree end |
Class Method Details
.walk(struct = nil, indent = 0, out = nil) ⇒ Object
Does basic output of parse tree structure to visually inspect parsed info.
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/eggshell/parse-tree.rb', line 131 def self.walk(struct = nil, indent = 0, out = nil) out = $stdout if !out struct.each do |row| if row.is_a?(Eggshell::Line) out.write "#{' '*indent}#{row.to_s.inspect} ##{row.line_num}\n" elsif row.is_a?(Array) if row[0] == :macro out.write "#{' '*indent}@#{row[1]}(#{row[2].inspect}) | end=#{row[-1]}\n" walk(row[3], indent + 1, out) elsif row[0] == :block out.write "#{' '*indent}#{row[1]}(#{row[2].inspect}) | end=#{row[-1]}\n" walk(row[3], indent + 1, out) end else out.write "#{' '*indent}#{row.inspect} ?\n" end end end |
Instance Method Details
#collect(line_obj) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/eggshell/parse-tree.rb', line 103 def collect(line_obj) more = @cur_block[0].continue_with(line_obj.line) if more != BH::RETRY @lines << line_obj if more == BH::DONE push_block end else push_block end more end |
#macro_delim_match(line_obj, line_num) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/eggshell/parse-tree.rb', line 53 def macro_delim_match(line_obj, line_num) if @macro_delims[-1] == line_obj.line if @modes[-1] == :block push_block end @macro_delims.pop @macro_open.pop @ptr = @macro_ptr.pop @ptr[-1] << line_num last_mode = @modes.pop return true end return false end |
#mode ⇒ Object
TODO:
return macro_open
126 127 128 |
# File 'lib/eggshell/parse-tree.rb', line 126 def mode @modes[-1] end |
#new_block(handler, type, line_obj, consume_mode, line_start) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/eggshell/parse-tree.rb', line 68 def new_block(handler, type, line_obj, consume_mode, line_start) block_type, args, line = Eggshell::Processor.parse_block_start(line_obj.line) nline = Eggshell::Line.new(line, line_obj.tab_str, line_obj.indent_lvl, line_obj.line_num) if consume_mode != BH::DONE @modes << :block if line != '' @lines << nline line_start -= 1 end @cur_block = [handler, type, args, line_start] if consume_mode == BH::COLLECT_RAW mode = :raw else consume_mode == BH::COLLECT mode = :block end else @ptr << [:block, type, args, [nline], line_start] end end |
#new_macro(line_obj, line_start) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/eggshell/parse-tree.rb', line 33 def new_macro(line_obj, line_start) line = line_obj.line macro, args, delim = Eggshell::Processor.parse_macro_start(line) push_block if delim @modes << :macro @macro_delims << delim.reverse.gsub('[', ']').gsub('(', ')').gsub('{', '}') @macro_open << line @macro_ptr << @ptr # set ptr to entry's tree entry = [:macro, macro, args, [], line_start] @ptr << entry @ptr = entry[IDX_LINES] else @ptr << [:macro, macro, args, [], line_start] end end |
#push_block ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/eggshell/parse-tree.rb', line 89 def push_block if @modes[-1] == :block if @cur_block line_end = @cur_block[3] line_end = @lines[-1].line_num if @lines[-1] @ptr << [:block, @cur_block[1], @cur_block[2], @lines, @cur_block[3], line_end] @lines = [] @cur_block[0].reset @cur_block = nil end @modes.pop end end |
#raw_line(line_obj) ⇒ Object
116 117 118 |
# File 'lib/eggshell/parse-tree.rb', line 116 def raw_line(line_obj) @ptr << line_obj end |
#tree ⇒ Object
120 121 122 |
# File 'lib/eggshell/parse-tree.rb', line 120 def tree @tree.clone end |