@@ -11,6 +11,9 @@ class Parser
1111 BRANCHING = [ "(" , ")" ]
1212 PROPERTY = [ "[" , "]" ]
1313 NODE_DELIMITERS = [ NEW_NODE ] . concat BRANCHING
14+ LIST_IDENTITIES = [ "AW" , "AB" , "AE" , "AR" , "CR" , "DD" ,
15+ "LB" , "LN" , "MA" , "SL" , "SQ" , "TR" , "VW" ,
16+ "TB" , "TW" ]
1417
1518 def initialize strict_parsing = true
1619 @strict_parsing = strict_parsing
@@ -31,12 +34,16 @@ def parse sgf
3134 @stream = streamable sgf
3235 until @stream . eof?
3336 case next_character
34- when ";" then create_new_node
35- when "(" then open_branch
36- when ")" then close_branch
37+ when ";" then
38+ create_new_node
39+ parse_node_data
40+ add_properties_to_current_node
41+ when "(" then
42+ open_branch
43+ when ")" then
44+ close_branch
45+ else next
3746 end
38- parse_node_data
39- add_properties_to_current_node
4047 end
4148 @tree
4249 end
@@ -87,18 +94,56 @@ def still_inside_node?
8794 def parse_identity
8895 @identity = ""
8996 while char = next_character and char != "["
90- @identity << char
97+ @identity << char unless char == " \n "
9198 end
9299 end
93100
94101 def parse_property
95102 @property = ""
103+ case @identity . upcase
104+ when "C" then
105+ parse_comment
106+ when *LIST_IDENTITIES then
107+ parse_multi_property
108+ else
109+ parse_generic_property
110+ end
111+ end
112+
113+ def parse_comment
114+ while char = next_character and still_inside_comment? char
115+ @property << char
116+ end
117+ @property . gsub! "\\ ]" , "]"
118+ end
119+
120+ def parse_multi_property
121+ while char = next_character and still_inside_multi_property? char
122+ @property << char
123+ end
124+ @property = @property . gsub ( "][" , "," ) . split ( "," )
125+ end
126+
127+ def parse_generic_property
96128 while char = next_character and char != "]"
97129 @property << char
98130 end
99131 end
100132
133+ def still_inside_comment? char
134+ char != "]" || ( char == "]" && @property [ -1 ..-1 ] == "\\ " )
135+ end
136+
137+ def still_inside_multi_property? char
138+ return true if char != "]"
139+ char = next_character
140+ @stream . pos -= 1
141+ return true if char == "["
142+ false
143+ end
144+
101145 def add_properties_to_current_node
146+ p @node_properties
102147 @current_node . add_properties @node_properties
103148 end
104149
0 commit comments