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:
parent
363d08ed0d
commit
69a87c73b4
@ -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.
|
||||
|
@ -19,12 +19,8 @@ func TestParse(t *testing.T) {
|
||||
{
|
||||
"",
|
||||
&Body{
|
||||
Items: []Node{
|
||||
&Unstructured{
|
||||
AllTokens: &TokenSeq{Tokens{}},
|
||||
},
|
||||
},
|
||||
AllTokens: &TokenSeq{&TokenSeq{Tokens{}}},
|
||||
Items: nil,
|
||||
AllTokens: nil,
|
||||
},
|
||||
},
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user