2016-11-15 20:30:34 +01:00
|
|
|
// Copyright 2016 Marapongo, Inc. All rights reserved.
|
|
|
|
|
|
|
|
package compiler
|
|
|
|
|
|
|
|
import (
|
2016-11-16 17:19:26 +01:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2016-11-15 20:30:34 +01:00
|
|
|
"github.com/golang/glog"
|
|
|
|
|
|
|
|
"github.com/marapongo/mu/pkg/diag"
|
|
|
|
"github.com/marapongo/mu/pkg/errors"
|
|
|
|
"github.com/marapongo/mu/pkg/workspace"
|
|
|
|
)
|
|
|
|
|
2016-11-16 20:09:45 +01:00
|
|
|
// Phase represents a compiler phase.
|
|
|
|
type Phase interface {
|
2016-11-16 18:29:44 +01:00
|
|
|
// Diag fetches the diagnostics sink used by this compiler pass.
|
|
|
|
Diag() diag.Sink
|
|
|
|
}
|
|
|
|
|
2016-11-15 20:30:34 +01:00
|
|
|
// Compiler provides an interface into the many phases of the Mu compilation process.
|
|
|
|
type Compiler interface {
|
2016-11-16 20:09:45 +01:00
|
|
|
Phase
|
2016-11-16 18:29:44 +01:00
|
|
|
|
2016-11-16 02:42:22 +01:00
|
|
|
// Context returns the current compiler context.
|
|
|
|
Context() *Context
|
|
|
|
|
2016-11-15 20:30:34 +01:00
|
|
|
// Build detects and compiles inputs from the given location, storing build artifacts in the given destination.
|
|
|
|
Build(inp string, outp string)
|
|
|
|
}
|
|
|
|
|
|
|
|
// compiler is the canonical implementation of the Mu compiler.
|
|
|
|
type compiler struct {
|
2016-11-16 02:42:22 +01:00
|
|
|
ctx *Context
|
2016-11-15 20:30:34 +01:00
|
|
|
opts Options
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewCompiler creates a new instance of the Mu compiler, with the given initialization settings.
|
|
|
|
func NewCompiler(opts Options) Compiler {
|
2016-11-16 02:42:22 +01:00
|
|
|
return &compiler{
|
|
|
|
ctx: &Context{},
|
|
|
|
opts: opts,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *compiler) Context() *Context {
|
|
|
|
return c.ctx
|
2016-11-15 20:30:34 +01:00
|
|
|
}
|
|
|
|
|
2016-11-16 01:30:10 +01:00
|
|
|
func (c *compiler) Diag() diag.Sink {
|
|
|
|
return c.opts.Diag
|
2016-11-15 20:30:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *compiler) Build(inp string, outp string) {
|
2016-11-16 01:30:10 +01:00
|
|
|
glog.Infof("Building target '%v' (out='%v')", inp, outp)
|
|
|
|
if glog.V(2) {
|
|
|
|
defer func() {
|
|
|
|
glog.V(2).Infof("Building target '%v' completed w/ %v warnings and %v errors",
|
|
|
|
inp, c.Diag().Warnings(), c.Diag().Errors())
|
|
|
|
}()
|
|
|
|
}
|
2016-11-15 20:30:34 +01:00
|
|
|
|
|
|
|
// First find the root of the current package based on the location of its Mufile.
|
2016-11-16 17:19:26 +01:00
|
|
|
mufile := c.detectMufile(inp)
|
|
|
|
if mufile == "" {
|
2016-11-16 01:30:10 +01:00
|
|
|
c.Diag().Errorf(errors.MissingMufile, inp)
|
2016-11-15 20:30:34 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-11-16 02:42:22 +01:00
|
|
|
// Read in the contents of the document and make it available to subsequent stages.
|
|
|
|
doc, err := diag.ReadDocument(mufile)
|
|
|
|
if err != nil {
|
|
|
|
c.Diag().Errorf(errors.CouldNotReadMufile.WithFile(mufile), err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-11-15 20:30:34 +01:00
|
|
|
// To build the Mu package, first parse the input file.
|
|
|
|
p := NewParser(c)
|
2016-11-16 02:42:22 +01:00
|
|
|
stack := p.Parse(doc)
|
|
|
|
if p.Diag().Errors() > 0 {
|
2016-11-16 18:29:44 +01:00
|
|
|
// If any errors happened during parsing, exit.
|
2016-11-15 20:30:34 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-11-16 02:42:22 +01:00
|
|
|
// Do a pass over the parse tree to ensure that all is well.
|
|
|
|
ptAnalyzer := NewPTAnalyzer(c)
|
|
|
|
ptAnalyzer.Analyze(doc, stack)
|
|
|
|
if p.Diag().Errors() > 0 {
|
2016-11-16 18:29:44 +01:00
|
|
|
// If any errors happened during parse tree analysis, exit.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: load dependencies.
|
|
|
|
|
|
|
|
binder := NewBinder(c)
|
|
|
|
binder.Bind(doc, stack)
|
|
|
|
if p.Diag().Errors() > 0 {
|
|
|
|
// If any errors happened during binding, exit.
|
2016-11-16 02:42:22 +01:00
|
|
|
return
|
|
|
|
}
|
2016-11-15 20:30:34 +01:00
|
|
|
|
2016-11-16 18:29:44 +01:00
|
|
|
// TODO: perform semantic analysis on the bound tree.
|
2016-11-17 16:00:52 +01:00
|
|
|
|
2016-11-16 18:29:44 +01:00
|
|
|
// TODO: select a target backend (including reading in a Muclusters file if needed).
|
|
|
|
// TODO: lower the ASTs to the target backend's representation, emit it.
|
|
|
|
// TODO: delta generation, deployment, etc.
|
2016-11-15 20:30:34 +01:00
|
|
|
}
|
2016-11-16 17:19:26 +01:00
|
|
|
|
|
|
|
// detectMufile locates the closest Mufile-looking file from the given path, searching "upwards" in the directory
|
|
|
|
// hierarchy. If no Mufile is found, an empty path is returned.
|
|
|
|
func (c *compiler) detectMufile(from string) string {
|
|
|
|
abs, err := filepath.Abs(from)
|
|
|
|
if err != nil {
|
|
|
|
glog.Fatalf("An IO error occurred while searching for a Mufile: %v", err)
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// It's possible the target is already the file we seek; if so, return right away.
|
|
|
|
if c.isMufile(abs) {
|
|
|
|
return abs
|
|
|
|
}
|
|
|
|
|
|
|
|
curr := abs
|
|
|
|
for {
|
|
|
|
stop := false
|
|
|
|
|
|
|
|
// If the target is a directory, enumerate its files, checking each to see if it's a Mufile.
|
|
|
|
files, err := ioutil.ReadDir(curr)
|
|
|
|
if err != nil {
|
|
|
|
glog.Fatalf("An IO error occurred while searching for a Mufile: %v", err)
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
for _, file := range files {
|
|
|
|
name := file.Name()
|
|
|
|
path := filepath.Join(curr, name)
|
|
|
|
if c.isMufile(path) {
|
|
|
|
return path
|
|
|
|
} else if name == workspace.Muspace {
|
|
|
|
// If we hit a .muspace file, stop looking.
|
|
|
|
stop = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we encountered a stop condition, break out of the loop.
|
|
|
|
if stop {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// If neither succeeded, keep looking in our parent directory.
|
|
|
|
curr = filepath.Dir(curr)
|
|
|
|
if os.IsPathSeparator(curr[len(curr)-1]) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// isMufile returns true if the path references what appears to be a valid Mufile.
|
|
|
|
func (c *compiler) isMufile(path string) bool {
|
|
|
|
info, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Directories can't be Mufiles.
|
|
|
|
if info.IsDir() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the base name is expected.
|
|
|
|
name := info.Name()
|
|
|
|
ext := filepath.Ext(name)
|
|
|
|
base := strings.TrimSuffix(name, ext)
|
|
|
|
if base != workspace.MufileBase {
|
|
|
|
if strings.EqualFold(base, workspace.MufileBase) {
|
|
|
|
// If the strings aren't equal, but case-insensitively match, issue a warning.
|
|
|
|
c.Diag().Warningf(errors.WarnIllegalMufileCasing.WithFile(name))
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check all supported extensions.
|
|
|
|
for _, mufileExt := range workspace.MufileExts {
|
|
|
|
if name == workspace.MufileBase+mufileExt {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we got here, it means the base name matched, but not the extension. Warn and return.
|
|
|
|
c.Diag().Warningf(errors.WarnIllegalMufileExt.WithFile(name), ext)
|
|
|
|
return false
|
|
|
|
}
|