parser: add Parse() and make it compatible with printer
This commit is contained in:
parent
de7241ebe5
commit
71105156e2
@ -20,12 +20,18 @@ type Parser struct {
|
||||
n int // buffer size (max = 1)
|
||||
}
|
||||
|
||||
func New(src []byte) *Parser {
|
||||
func newParser(src []byte) *Parser {
|
||||
return &Parser{
|
||||
sc: scanner.New(src),
|
||||
}
|
||||
}
|
||||
|
||||
// Parse returns the fully parsed source and returns the abstract syntax tree.
|
||||
func Parse(src []byte) (ast.Node, error) {
|
||||
p := newParser(src)
|
||||
return p.Parse()
|
||||
}
|
||||
|
||||
var errEofToken = errors.New("EOF token found")
|
||||
|
||||
// Parse returns the fully parsed source and returns the abstract syntax tree.
|
||||
@ -78,7 +84,7 @@ func (p *Parser) next() (ast.Node, error) {
|
||||
Text: tok.Text,
|
||||
}, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("expected: IDENT | STRING got: %+v", tok.Type)
|
||||
return nil, fmt.Errorf("expected: IDENT | STRING | COMMENT got: %+v", tok.Type)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -25,7 +25,7 @@ func TestType(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, l := range literals {
|
||||
p := New([]byte(l.src))
|
||||
p := newParser([]byte(l.src))
|
||||
item, err := p.parseObjectItem()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
@ -66,7 +66,7 @@ func TestListType(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, l := range literals {
|
||||
p := New([]byte(l.src))
|
||||
p := newParser([]byte(l.src))
|
||||
item, err := p.parseObjectItem()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
@ -140,7 +140,7 @@ func TestObjectType(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, l := range literals {
|
||||
p := New([]byte(l.src))
|
||||
p := newParser([]byte(l.src))
|
||||
// p.enableTrace = true
|
||||
item, err := p.parseObjectItem()
|
||||
if err != nil {
|
||||
@ -184,7 +184,7 @@ func TestObjectKey(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, k := range keys {
|
||||
p := New([]byte(k.src))
|
||||
p := newParser([]byte(k.src))
|
||||
keys, err := p.parseObjectKey()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@ -208,7 +208,7 @@ func TestObjectKey(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, k := range errKeys {
|
||||
p := New([]byte(k.src))
|
||||
p := newParser([]byte(k.src))
|
||||
_, err := p.parseObjectKey()
|
||||
if err == nil {
|
||||
t.Errorf("case '%s' should give an error", k.src)
|
||||
@ -284,9 +284,7 @@ func TestParse(t *testing.T) {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
p := New(d)
|
||||
// p.enableTrace = true
|
||||
_, err = p.Parse()
|
||||
_, err = Parse(d)
|
||||
if (err != nil) != tc.Err {
|
||||
t.Fatalf("Input: %s\n\nError: %s", tc.Name, err)
|
||||
}
|
||||
|
1
printer/nodes.go
Normal file
1
printer/nodes.go
Normal file
@ -0,0 +1 @@
|
||||
package printer
|
@ -6,17 +6,23 @@ import (
|
||||
"io"
|
||||
"text/tabwriter"
|
||||
|
||||
"github.com/hashicorp/hcl/hcl"
|
||||
"github.com/fatih/hcl/ast"
|
||||
)
|
||||
|
||||
type printer struct {
|
||||
cfg Config
|
||||
obj *hcl.Object
|
||||
cfg Config
|
||||
node ast.Node
|
||||
}
|
||||
|
||||
func (p *printer) output() []byte {
|
||||
var buf bytes.Buffer
|
||||
fmt.Println("STARTING OUTPUT")
|
||||
|
||||
ast.Walk(p.node, func(n ast.Node) bool {
|
||||
fmt.Printf("n = %+v\n", n)
|
||||
return true
|
||||
})
|
||||
|
||||
return buf.Bytes()
|
||||
}
|
||||
|
||||
@ -36,10 +42,10 @@ type Config struct {
|
||||
Indent int // default: 0 (all code is indented at least by this much)
|
||||
}
|
||||
|
||||
func (c *Config) fprint(output io.Writer, obj *hcl.Object) error {
|
||||
func (c *Config) fprint(output io.Writer, node ast.Node) error {
|
||||
p := &printer{
|
||||
cfg: *c,
|
||||
obj: obj,
|
||||
cfg: *c,
|
||||
node: node,
|
||||
}
|
||||
|
||||
// TODO(arslan): implement this
|
||||
@ -81,12 +87,12 @@ func (c *Config) fprint(output io.Writer, obj *hcl.Object) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *Config) Fprint(output io.Writer, obj *hcl.Object) error {
|
||||
return c.fprint(output, obj)
|
||||
func (c *Config) Fprint(output io.Writer, node ast.Node) error {
|
||||
return c.fprint(output, node)
|
||||
}
|
||||
|
||||
// Fprint "pretty-prints" an HCL object to output
|
||||
// Fprint "pretty-prints" an HCL node to output
|
||||
// It calls Config.Fprint with default settings.
|
||||
func Fprint(output io.Writer, obj *hcl.Object) error {
|
||||
return (&Config{Tabwidth: 8}).Fprint(output, obj)
|
||||
func Fprint(output io.Writer, node ast.Node) error {
|
||||
return (&Config{Tabwidth: 8}).Fprint(output, node)
|
||||
}
|
||||
|
@ -4,7 +4,7 @@ import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/hcl/hcl"
|
||||
"github.com/fatih/hcl/parser"
|
||||
)
|
||||
|
||||
var complexHcl = `// This comes from Terraform, as a test
|
||||
@ -52,12 +52,12 @@ output "web_ip" {
|
||||
`
|
||||
|
||||
func TestPrint(t *testing.T) {
|
||||
obj, err := hcl.Parse(complexHcl)
|
||||
node, err := parser.Parse([]byte(complexHcl))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err := Fprint(os.Stdout, obj); err != nil {
|
||||
if err := Fprint(os.Stdout, node); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user