zclwrite: start to partition body items

So far they are still just all unstructured, but each item is a separate
node.
This commit is contained in:
Martin Atkins 2017-06-07 07:38:44 -07:00
parent 363d08ed0d
commit 69a87c73b4
2 changed files with 44 additions and 11 deletions

View File

@ -118,16 +118,53 @@ func parseBody(nativeBody *zclsyntax.Body, from inputTokens) (inputTokens, *Body
IndentLevel: 0, // TODO: deal with this
}
// TODO: actually partition the native item tokens
// For now we'll just return it all as one big Unstructured.
unstructured := &Unstructured{
AllTokens: within.Seq(),
remain := within
for _, nativeItem := range nativeItems {
beforeItem, item, afterItem := parseBodyItem(nativeItem, remain)
if beforeItem.Len() > 0 {
body.AppendItem(&Unstructured{
AllTokens: beforeItem.Seq(),
})
}
body.AppendItem(item)
remain = afterItem
}
if remain.Len() > 0 {
body.AppendItem(&Unstructured{
AllTokens: remain.Seq(),
})
}
body.AppendItem(unstructured)
return before, body, after
}
func parseBodyItem(nativeItem zclsyntax.Node, from inputTokens) (inputTokens, Node, inputTokens) {
before, within, after := from.Partition(nativeItem.Range())
var item Node
switch nativeItem.(type) {
case *zclsyntax.Attribute:
// TODO: actually deconstruct the attribute parts
item = &Unstructured{
AllTokens: within.Seq(),
}
case *zclsyntax.Block:
// TODO: actually deconstruct the block parts
item = &Unstructured{
AllTokens: within.Seq(),
}
default:
// should never happen if caller is behaving
panic("unsupported native item type")
}
return before, item, after
}
// writerTokens takes a sequence of tokens as produced by the main zclsyntax
// package and transforms it into an equivalent sequence of tokens using
// this package's own token model.

View File

@ -19,12 +19,8 @@ func TestParse(t *testing.T) {
{
"",
&Body{
Items: []Node{
&Unstructured{
AllTokens: &TokenSeq{Tokens{}},
},
},
AllTokens: &TokenSeq{&TokenSeq{Tokens{}}},
Items: nil,
AllTokens: nil,
},
},
{