pulumi/pkg/compiler/parser.go
joeduffy 094d3a0817 Perform more real compilation activities
This change includes some progress on actual compilation (albeit with several
TODOs remaining before we can actually spit out a useful artifact).  There are
also some general cleanups sprinkled throughout.  In a nutshell:

* Add a compiler.Context object that will be available during template expansion.

* Introduce a diag.Document abstraction.  This is better than passing raw filenames
  around, and lets us embellish diagnostics as we go.  In particular, we will be
  in a better position to provide line/column error information.

* Move IO out of the Parser and into the Compiler, where it can cache and reuse
  Documents.  This will become important as we start to load up dependencies.

* Rename PosRange to Location.  This reads nicer with the new Document terminology.

* Rename the mu/api package to mu/schema.  It's likely we will need to introduce a
  true AST that is decoupled from the serialization format and contains bound nodes.
  As a result, treating the existing types as "schema" is more honest.

* Add in a big section of TODOs at the end of the compiler.Compiler.Build function.
2016-11-15 17:42:22 -08:00

77 lines
2.1 KiB
Go

// Copyright 2016 Marapongo, Inc. All rights reserved.
package compiler
import (
"encoding/json"
"github.com/ghodss/yaml"
"github.com/golang/glog"
"github.com/marapongo/mu/pkg/diag"
"github.com/marapongo/mu/pkg/errors"
"github.com/marapongo/mu/pkg/schema"
)
type Parser interface {
// Diag fetches the diagnostics sink used by this parser.
Diag() diag.Sink
// Parse detects and parses input from the given path. If an error occurs, the return value will be nil. It is
// expected that errors are conveyed using the diag.Sink interface.
Parse(doc *diag.Document) *schema.Stack
}
func NewParser(c Compiler) Parser {
return &parser{c}
}
type parser struct {
c Compiler
}
func (p *parser) Diag() diag.Sink {
return p.c.Diag()
}
func (p *parser) Parse(doc *diag.Document) *schema.Stack {
glog.Infof("Parsing Mufile: %v (len(body)=%v)", doc.File, len(doc.Body))
if glog.V(2) {
defer func() {
glog.V(2).Infof("Parsing Mufile '%v' completed w/ %v warnings and %v errors",
doc.File, p.Diag().Warnings(), p.Diag().Errors())
}()
}
// We support both JSON and YAML as a file format. Detect the file extension and deserialize the contents.
// TODO: we need to expand templates as part of the parsing process.
switch doc.Ext() {
case ".json":
return p.parseFromJSON(doc)
case ".yaml":
return p.parseFromYAML(doc)
default:
p.Diag().Errorf(errors.IllegalMufileExt.WithDocument(doc), doc.Ext())
return nil
}
}
func (p *parser) parseFromJSON(doc *diag.Document) *schema.Stack {
var stack schema.Stack
if err := json.Unmarshal(doc.Body, &stack); err != nil {
p.Diag().Errorf(errors.IllegalMufileSyntax.WithDocument(doc), err)
// TODO: it would be great if we issued an error per issue found in the file with line/col numbers.
return nil
}
return &stack
}
func (p *parser) parseFromYAML(doc *diag.Document) *schema.Stack {
var stack schema.Stack
if err := yaml.Unmarshal(doc.Body, &stack); err != nil {
p.Diag().Errorf(errors.IllegalMufileSyntax.WithDocument(doc), err)
// TODO: it would be great if we issued an error per issue found in the file with line/col numbers.
return nil
}
return &stack
}