2dd8665c46
This change begins to lay the groundwork for doing semantic analysis and lowering to the cloud target's representation. In particular: * Split the mu/schema package. There is now mu/ast which contains the core types and mu/encoding which concerns itself with JSON and YAML serialization. * Notably I am *not* yet introducing a second AST form. Instead, we will keep the parse tree and AST unified for the time being. I envision very little difference between them -- at least for now -- and so this keeps things simpler, at the expense of two downsides: 1) the trees will be mutable (which turns out to be a good thing for performance), and 2) some fields will need to be ignored during de/serialization. We can always revisit this later when and if the need to split them arises. * Add a binder phase. It is currently a no-op.
58 lines
1.5 KiB
Go
58 lines
1.5 KiB
Go
// Copyright 2016 Marapongo, Inc. All rights reserved.
|
|
|
|
package compiler
|
|
|
|
import (
|
|
"github.com/golang/glog"
|
|
|
|
"github.com/marapongo/mu/pkg/ast"
|
|
"github.com/marapongo/mu/pkg/diag"
|
|
"github.com/marapongo/mu/pkg/encoding"
|
|
"github.com/marapongo/mu/pkg/errors"
|
|
)
|
|
|
|
type Parser interface {
|
|
Pass
|
|
|
|
// 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) *ast.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) *ast.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 many file formats. Detect the file extension and deserialize the contents.
|
|
// TODO: we need to expand templates as part of the parsing process
|
|
var stack ast.Stack
|
|
marshaler, has := encoding.Marshalers[doc.Ext()]
|
|
if !has {
|
|
glog.Fatalf("No marshaler registered for this Mufile extension: %v", doc.Ext())
|
|
return nil
|
|
}
|
|
|
|
if err := marshaler.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
|
|
}
|