2018-05-22 19:43:36 +00:00
|
|
|
// Copyright 2016-2018, Pulumi Corporation.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2016-11-21 20:06:32 +00:00
|
|
|
|
Begin overhauling semantic phases
This change further merges the new AST and MuPack/MuIL formats and
abstractions into the core of the compiler. A good amount of the old
code is gone now; I decided against ripping it all out in one fell
swoop so that I can methodically check that we are preserving all
relevant decisions and/or functionality we had in the old model.
The changes are too numerous to outline in this commit message,
however, here are the noteworthy ones:
* Split up the notion of symbols and tokens, resulting in:
- pkg/symbols for true compiler symbols (bound nodes)
- pkg/tokens for name-based tokens, identifiers, constants
* Several packages move underneath pkg/compiler:
- pkg/ast becomes pkg/compiler/ast
- pkg/errors becomes pkg/compiler/errors
- pkg/symbols becomes pkg/compiler/symbols
* pkg/ast/... becomes pkg/compiler/legacy/ast/...
* pkg/pack/ast becomes pkg/compiler/ast.
* pkg/options goes away, merged back into pkg/compiler.
* All binding functionality moves underneath a dedicated
package, pkg/compiler/binder. The legacy.go file contains
cruft that will eventually go away, while the other files
represent a halfway point between new and old, but are
expected to stay roughly in the current shape.
* All parsing functionality is moved underneath a new
pkg/compiler/metadata namespace, and we adopt new terminology
"metadata reading" since real parsing happens in the MetaMu
compilers. Hence, Parser has become metadata.Reader.
* In general phases of the compiler no longer share access to
the actual compiler.Compiler object. Instead, shared state is
moved to the core.Context object underneath pkg/compiler/core.
* Dependency resolution during binding has been rewritten to
the new model, including stashing bound package symbols in the
context object, and detecting import cycles.
* Compiler construction does not take a workspace object. Instead,
creation of a workspace is entirely hidden inside of the compiler's
constructor logic.
* There are three Compile* functions on the Compiler interface, to
support different styles of invoking compilation: Compile() auto-
detects a Mu package, based on the workspace; CompilePath(string)
loads the target as a Mu package and compiles it, regardless of
the workspace settings; and, CompilePackage(*pack.Package) will
compile a pre-loaded package AST, again regardless of workspace.
* Delete the _fe, _sema, and parsetree phases. They are no longer
relevant and the functionality is largely subsumed by the above.
...and so very much more. I'm surprised I ever got this to compile again!
2017-01-18 20:18:37 +00:00
|
|
|
package tokens
|
2016-11-21 20:06:32 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestIsAsName(t *testing.T) {
|
2017-06-01 21:01:26 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2023-03-03 16:36:39 +00:00
|
|
|
goodNames := []string{
|
2022-03-11 17:06:18 +00:00
|
|
|
"simple", // all alpha.
|
|
|
|
"SiMplE", // mixed-case alpha.
|
|
|
|
"simple0", // alphanumeric.
|
|
|
|
"SiMpLe0", // mixed-case alphanumeric.
|
|
|
|
"_", // permit underscore.
|
|
|
|
"s1MPl3_", // mixed-case alphanumeric/underscore.
|
|
|
|
"_s1MPl3", // ditto.
|
|
|
|
"hy-phy", // permit hyphens.
|
|
|
|
".dotstart", // start with .
|
|
|
|
"-hyphenstart", // start with -
|
|
|
|
"0num", // start with numbers
|
|
|
|
"9num", // start with numbers
|
2017-01-21 19:04:03 +00:00
|
|
|
}
|
|
|
|
for _, nm := range goodNames {
|
|
|
|
assert.True(t, IsName(nm), "IsName expected to be true: %v", nm)
|
|
|
|
}
|
|
|
|
|
2023-03-03 16:36:39 +00:00
|
|
|
goodQNames := []string{
|
2016-11-21 20:06:32 +00:00
|
|
|
"namespace/complex", // multi-part name.
|
|
|
|
"_naMeSpace0/coMpl3x32", // multi-part, alphanumeric, etc. name.
|
|
|
|
"n_ameSpace3/moRenam3sp4ce/_Complex5", // even more complex parts.
|
|
|
|
}
|
2017-01-21 19:04:03 +00:00
|
|
|
for _, nm := range goodQNames {
|
|
|
|
assert.True(t, IsQName(nm), "IsQName expected to be true: %v", nm)
|
|
|
|
assert.False(t, IsName(nm), "IsName expected to be false: %v", nm)
|
2016-11-21 20:06:32 +00:00
|
|
|
}
|
|
|
|
|
2023-03-03 16:36:39 +00:00
|
|
|
badNames := []string{
|
2022-03-11 17:06:18 +00:00
|
|
|
"s!mple", // bad characters.
|
|
|
|
"namesp@ce/complex", // ditto.
|
|
|
|
"namespace/morenamespace/compl#x", // ditto.
|
2016-11-21 20:06:32 +00:00
|
|
|
}
|
|
|
|
for _, nm := range badNames {
|
2017-01-21 19:04:03 +00:00
|
|
|
assert.False(t, IsName(nm), "IsName expected to be false: %v", nm)
|
|
|
|
assert.False(t, IsQName(nm), "IsQName expected to be false: %v", nm)
|
2016-11-21 20:06:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNameSimple(t *testing.T) {
|
2017-06-01 21:01:26 +00:00
|
|
|
t.Parallel()
|
2022-07-15 13:56:54 +00:00
|
|
|
assert.Equal(t, "simple", string(Name("simple")))
|
|
|
|
assert.Equal(t, "complex", string(QName("namespace/complex").Name()))
|
|
|
|
assert.Equal(t, "complex", string(QName("ns1/ns2/ns3/ns4/complex").Name()))
|
|
|
|
assert.Equal(t, "c0Mpl3x_", string(QName("_/_/_/_/a0/c0Mpl3x_").Name()))
|
2016-11-21 20:06:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNameNamespace(t *testing.T) {
|
2017-06-01 21:01:26 +00:00
|
|
|
t.Parallel()
|
2022-07-15 13:56:54 +00:00
|
|
|
assert.Equal(t, "namespace", string(QName("namespace/complex").Namespace()))
|
|
|
|
assert.Equal(t, "ns1/ns2/ns3/ns4", string(QName("ns1/ns2/ns3/ns4/complex").Namespace()))
|
|
|
|
assert.Equal(t, "_/_/_/_/a0", string(QName("_/_/_/_/a0/c0Mpl3x_").Namespace()))
|
2016-11-21 20:06:32 +00:00
|
|
|
}
|
2021-12-17 22:52:01 +00:00
|
|
|
|
|
|
|
func TestIntoQName(t *testing.T) {
|
2022-03-04 08:17:41 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2021-12-17 22:52:01 +00:00
|
|
|
cases := []struct {
|
|
|
|
input string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{"foo/bar", "foo/bar"},
|
|
|
|
{input: "https:", expected: "https_"},
|
2023-03-03 16:36:39 +00:00
|
|
|
{
|
|
|
|
"https://github.com/pulumi/pulumi/blob/master/pkg/resource/deploy/providers/provider.go#L61-L86",
|
|
|
|
"https_/github.com/pulumi/pulumi/blob/master/pkg/resource/deploy/providers/provider.go_L61-L86",
|
|
|
|
},
|
2021-12-17 22:52:01 +00:00
|
|
|
{"", "_"},
|
|
|
|
{"///", "_"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
2022-03-04 08:17:41 +00:00
|
|
|
c := c
|
2021-12-17 22:52:01 +00:00
|
|
|
t.Run(c.input, func(t *testing.T) {
|
2022-03-04 08:17:41 +00:00
|
|
|
t.Parallel()
|
2022-07-15 13:56:54 +00:00
|
|
|
assert.Equal(t, QName(c.expected), IntoQName(c.input))
|
2021-12-17 22:52:01 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|