@@ -2,21 +2,23 @@ module Slang
22 class Parser
33 def initialize (string )
44 @lexer = Lexer .new(string)
5+ @document = Document .new
6+ @current_node = @document
7+ @control_nodes_per_column = {} of Int32 => Nodes ::Control
58 next_token
69 end
710
811 def parse (io_name = Slang ::DEFAULT_BUFFER_NAME )
9- document = Document .new
10- @current_node = document
1112 String .build do |str |
1213 loop do
14+ # puts token.inspect
1315 case token.type
1416 when :EOF
1517 break
1618 when :NEWLINE
1719 next_token
1820 when :DOCTYPE
19- document.nodes << Nodes ::Doctype .new(document, token)
21+ @ document .nodes << Nodes ::Doctype .new(@ document , token)
2022 next_token
2123 when :ELEMENT , :TEXT , :HTML , :COMMENT , :CONTROL , :OUTPUT
2224 parent = @current_node .not_nil!
@@ -35,14 +37,35 @@ module Slang
3537 else
3638 Nodes ::Text .new(parent, token)
3739 end
38- parent.not_nil!.nodes << node
40+
41+ # puts node.inspect
42+ # puts @control_nodes_per_column[node.column_number]?
43+
44+ if node.is_a?(Nodes ::Control )
45+ if @control_nodes_per_column [node.column_number]?
46+ last_control_node = @control_nodes_per_column [node.column_number]
47+ # puts "LAST CONTROL NODE"
48+ # puts last_control_node.inspect
49+ if last_control_node.allow_branch?(node)
50+ last_control_node.branches << node
51+ else
52+ @control_nodes_per_column [node.column_number] = node
53+ parent.not_nil!.nodes << node
54+ end
55+ else
56+ @control_nodes_per_column [node.column_number] = node
57+ parent.not_nil!.nodes << node
58+ end
59+ else
60+ parent.not_nil!.nodes << node
61+ end
3962 @current_node = node
4063 next_token
4164 else
4265 unexpected_token
4366 end
4467 end
45- document.to_s(str, io_name)
68+ @ document .to_s(str, io_name)
4669 end
4770 end
4871
0 commit comments