zclwrite: File-level AllTokens

This captures any leftover tokens that aren't considered part of the
file's main body, such as the trailing EOF token.
This commit is contained in:
Martin Atkins 2017-06-07 07:37:56 -07:00
parent fa8a707c7f
commit 363d08ed0d
2 changed files with 9 additions and 5 deletions

View File

@ -16,12 +16,13 @@ type File struct {
Name string
SrcBytes []byte
Body *Body
Body *Body
AllTokens *TokenSeq
}
// WriteTo writes the tokens underlying the receiving file to the given writer.
func (f *File) WriteTo(wr io.Writer) (int, error) {
return f.Body.AllTokens.WriteTo(wr)
return f.AllTokens.WriteTo(wr)
}
// Bytes returns a buffer containing the source code resulting from the

View File

@ -48,15 +48,18 @@ func parse(src []byte, filename string, start zcl.Pos) (*File, zcl.Diagnostics)
writerTokens: writerTokens,
}
// we ignore "before" and "after" at the root, because the root body covers
// the entire input.
_, root, _ := parseBody(file.Body.(*zclsyntax.Body), from)
before, root, after := parseBody(file.Body.(*zclsyntax.Body), from)
return &File{
Name: filename,
SrcBytes: src,
Body: root,
AllTokens: &TokenSeq{
before.Seq(),
root.AllTokens,
after.Seq(),
},
}, nil
}