2016-11-16 22:11:58 +01:00
|
|
|
// Copyright 2016 Marapongo, Inc. All rights reserved.
|
|
|
|
|
|
|
|
package ast
|
|
|
|
|
|
|
|
import (
|
2016-11-21 21:06:32 +01:00
|
|
|
"regexp"
|
2016-11-16 22:11:58 +01:00
|
|
|
"strings"
|
2016-11-21 21:06:32 +01:00
|
|
|
|
|
|
|
"github.com/marapongo/mu/pkg/util"
|
2016-11-16 22:11:58 +01:00
|
|
|
)
|
|
|
|
|
2016-11-23 02:07:10 +01:00
|
|
|
// NameDelimiter is what delimits Namespace and Name parts.
|
2016-11-16 22:11:58 +01:00
|
|
|
const NameDelimiter = "/"
|
|
|
|
|
2016-12-02 22:23:18 +01:00
|
|
|
var NameRegexp = regexp.MustCompile(NameRegexps)
|
|
|
|
var NameRegexps = "(" + NamePartRegexps + "\\" + NameDelimiter + ")*" + NamePartRegexps
|
|
|
|
var NamePartRegexps = "[A-Za-z_][A-Za-z0-9_]*"
|
2016-11-21 21:06:32 +01:00
|
|
|
|
|
|
|
// IsName checks whether a string is a legal Name.
|
|
|
|
func IsName(s string) bool {
|
2016-12-02 22:23:18 +01:00
|
|
|
return NameRegexp.FindString(s) == s
|
2016-11-21 21:06:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// AsName converts a given string to a Name, asserting its validity.
|
|
|
|
func AsName(s string) Name {
|
2016-12-02 23:33:22 +01:00
|
|
|
util.AssertMF(IsName(s), "Expected string '%v' to be a name (%v)", s, NameRegexps)
|
2016-11-21 21:06:32 +01:00
|
|
|
return Name(s)
|
|
|
|
}
|
|
|
|
|
Implement dependency resolution
This change includes logic to resolve dependencies declared by stacks. The design
is described in https://github.com/marapongo/mu/blob/master/docs/deps.md.
In summary, each stack may declare dependencies, which are name/semver pairs. A
new structure has been introduced, ast.Ref, to distinguish between ast.Names and
dependency names. An ast.Ref includes a protocol, base part, and a name part (the
latter being an ast.Name); for example, in "https://hub.mu.com/mu/container/",
"https://" is the protocol, "hub.mu.com/" is the base, and "mu/container" is the
name. This is used to resolve URL-like names to package manager-like artifacts.
The dependency resolution phase happens after parsing, but before semantic analysis.
This is because dependencies are "source-like" in that we must load and parse all
dependency metadata files. We stick the full transitive closure of dependencies
into a map attached to the compiler to avoid loading dependencies multiple times.
Note that, although dependencies prohibit cycles, this forms a DAG, meaning multiple
inbound edges to a single stack may come from multiple places.
From there, we rely on ordinary visitation to deal with dependencies further.
This includes inserting symbol entries into the symbol table, mapping names to the
loaded stacks, during the first phase of binding so that they may be found
subsequently when typechecking during the second phase and beyond.
2016-11-21 20:19:25 +01:00
|
|
|
// Simple extracts the name portion of a Name (dropping any Namespace).
|
|
|
|
func (nm Name) Simple() Name {
|
2016-11-16 22:11:58 +01:00
|
|
|
ix := strings.LastIndex(string(nm), NameDelimiter)
|
|
|
|
if ix == -1 {
|
|
|
|
return nm
|
|
|
|
}
|
|
|
|
return nm[ix+1:]
|
|
|
|
}
|
|
|
|
|
Implement dependency resolution
This change includes logic to resolve dependencies declared by stacks. The design
is described in https://github.com/marapongo/mu/blob/master/docs/deps.md.
In summary, each stack may declare dependencies, which are name/semver pairs. A
new structure has been introduced, ast.Ref, to distinguish between ast.Names and
dependency names. An ast.Ref includes a protocol, base part, and a name part (the
latter being an ast.Name); for example, in "https://hub.mu.com/mu/container/",
"https://" is the protocol, "hub.mu.com/" is the base, and "mu/container" is the
name. This is used to resolve URL-like names to package manager-like artifacts.
The dependency resolution phase happens after parsing, but before semantic analysis.
This is because dependencies are "source-like" in that we must load and parse all
dependency metadata files. We stick the full transitive closure of dependencies
into a map attached to the compiler to avoid loading dependencies multiple times.
Note that, although dependencies prohibit cycles, this forms a DAG, meaning multiple
inbound edges to a single stack may come from multiple places.
From there, we rely on ordinary visitation to deal with dependencies further.
This includes inserting symbol entries into the symbol table, mapping names to the
loaded stacks, during the first phase of binding so that they may be found
subsequently when typechecking during the second phase and beyond.
2016-11-21 20:19:25 +01:00
|
|
|
// Namespace extracts the namespace portion of a Name (dropping the Name); this may be empty.
|
|
|
|
func (nm Name) Namespace() Name {
|
2016-11-16 22:11:58 +01:00
|
|
|
ix := strings.LastIndex(string(nm), NameDelimiter)
|
|
|
|
if ix == -1 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return nm[:ix]
|
|
|
|
}
|