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
|
IndentLevel: 0, // TODO: deal with this
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: actually partition the native item tokens
|
remain := within
|
||||||
// For now we'll just return it all as one big Unstructured.
|
for _, nativeItem := range nativeItems {
|
||||||
unstructured := &Unstructured{
|
beforeItem, item, afterItem := parseBodyItem(nativeItem, remain)
|
||||||
AllTokens: within.Seq(),
|
|
||||||
|
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
|
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
|
// writerTokens takes a sequence of tokens as produced by the main zclsyntax
|
||||||
// package and transforms it into an equivalent sequence of tokens using
|
// package and transforms it into an equivalent sequence of tokens using
|
||||||
// this package's own token model.
|
// this package's own token model.
|
||||||
|
@ -19,12 +19,8 @@ func TestParse(t *testing.T) {
|
|||||||
{
|
{
|
||||||
"",
|
"",
|
||||||
&Body{
|
&Body{
|
||||||
Items: []Node{
|
Items: nil,
|
||||||
&Unstructured{
|
AllTokens: nil,
|
||||||
AllTokens: &TokenSeq{Tokens{}},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
AllTokens: &TokenSeq{&TokenSeq{Tokens{}}},
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user