diff --git a/BUILD.bazel b/BUILD.bazel index fba3863..dfe5022 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -26,7 +26,7 @@ go_library( visibility = ["//visibility:public"], deps = [ "//ast:go_default_library", - "//internal/transformations:go_default_library", + "//internal/program:go_default_library", "//parser:go_default_library", ], ) diff --git a/README.md b/README.md index 5bee80c..f8a2c75 100644 --- a/README.md +++ b/README.md @@ -47,10 +47,7 @@ Windows | _bazel build --platforms=@io_bazel_rules_go//go/toolchain:wind For additional target platform names, see the per-Go release definitions [here](https://github.com/bazelbuild/rules_go/blob/master/go/private/sdk_list.bzl#L21-L31) in the _rules_go_ Bazel package. -Additionally if files were moved around, you may need to run the following command to update Bazel files: -``` -bazel run //:gazelle -``` +Additionally if any files were moved around, see the section [Keeping the Bazel files up to date](#keeping-the-bazel-files-up-to-date). ## Build instructions (go 1.8 - 1.10) diff --git a/c-bindings/BUILD.bazel b/c-bindings/BUILD.bazel index 2ef23fd..9edd4bd 100644 --- a/c-bindings/BUILD.bazel +++ b/c-bindings/BUILD.bazel @@ -16,7 +16,6 @@ go_library( "internal.h", "json.cpp", "json.h", - "libgojsonnet.h", "libjsonnet.cpp", ], cdeps = [ diff --git a/cmd/dumpstdlibast/BUILD.bazel b/cmd/dumpstdlibast/BUILD.bazel index 41e569e..eb2275f 100644 --- a/cmd/dumpstdlibast/BUILD.bazel +++ b/cmd/dumpstdlibast/BUILD.bazel @@ -7,7 +7,7 @@ go_library( visibility = ["//visibility:private"], deps = [ "//internal/dump:go_default_library", - "//internal/transformations:go_default_library", + "//internal/program:go_default_library", ], ) diff --git a/cmd/dumpstdlibast/dumpstdlibast.go b/cmd/dumpstdlibast/dumpstdlibast.go index 387ef04..3ee5f3a 100644 --- a/cmd/dumpstdlibast/dumpstdlibast.go +++ b/cmd/dumpstdlibast/dumpstdlibast.go @@ -20,7 +20,7 @@ import ( "path/filepath" "github.com/google/go-jsonnet/internal/dump" - "github.com/google/go-jsonnet/internal/transformations" + "github.com/google/go-jsonnet/internal/program" ) func main() { @@ -33,7 +33,7 @@ func main() { panic(err) } - node, err := transformations.SnippetToAST("", string(buf)) + node, err := program.SnippetToAST("", string(buf)) if err != nil { panic(err) } diff --git a/internal/transformations/BUILD.bazel b/internal/program/BUILD.bazel similarity index 83% rename from internal/transformations/BUILD.bazel rename to internal/program/BUILD.bazel index f76a494..dcb2bc6 100644 --- a/internal/transformations/BUILD.bazel +++ b/internal/program/BUILD.bazel @@ -4,10 +4,10 @@ go_library( name = "go_default_library", srcs = [ "desugarer.go", + "program.go", "static_analyzer.go", - "transformations.go", ], - importpath = "github.com/google/go-jsonnet/internal/transformations", + importpath = "github.com/google/go-jsonnet/internal/program", visibility = ["//:__subpackages__"], deps = [ "//ast:go_default_library", diff --git a/internal/transformations/desugarer.go b/internal/program/desugarer.go similarity index 96% rename from internal/transformations/desugarer.go rename to internal/program/desugarer.go index ea71db1..3052ab0 100644 --- a/internal/transformations/desugarer.go +++ b/internal/program/desugarer.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package transformations +package program import ( "bytes" @@ -571,18 +571,20 @@ func desugar(astPtr *ast.Node, objLevel int) (err error) { return nil } -// Desugar Jsonnet expressions to reduce the number of constructs the rest of the implementation -// needs to understand. +// desugarAST desugars Jsonnet expressions to reduce the number of constructs +// the rest of the implementation needs to understand. // -// Note that despite the name, desugar() is not idempotent. String literals have their escape -// codes translated to low-level characters during desugaring. +// Note that despite the name, desugar() is not idempotent. String literals +// have their escape codes translated to low-level characters during desugaring. // -// Desugaring should happen immediately after parsing, i.e. before static analysis and execution. -// Temporary variables introduced here should be prefixed with $ to ensure they do not clash with -// variables used in user code. -// TODO(sbarzowski) Actually we may want to do some static analysis before desugaring, e.g. -// warning user about dangerous use of constructs that we desugar. -func Desugar(ast *ast.Node) error { +// Desugaring should happen immediately after parsing, i.e. before static +// analysis and execution. Temporary variables introduced here should be +// prefixed with $ to ensure they do not clash with variables used in user code. +// +// TODO(sbarzowski) Actually we may want to do some static analysis before +// desugaring, e.g. warning user about dangerous use of constructs that we +// desugar. +func desugarAST(ast *ast.Node) error { err := desugar(ast, 0) if err != nil { return err diff --git a/internal/transformations/desugarer_test.go b/internal/program/desugarer_test.go similarity index 96% rename from internal/transformations/desugarer_test.go rename to internal/program/desugarer_test.go index ed3a7a3..3d9026f 100644 --- a/internal/transformations/desugarer_test.go +++ b/internal/program/desugarer_test.go @@ -14,4 +14,4 @@ See the License for the specific language governing permissions and limitations under the License. */ -package transformations +package program diff --git a/internal/program/program.go b/internal/program/program.go new file mode 100644 index 0000000..47591f5 --- /dev/null +++ b/internal/program/program.go @@ -0,0 +1,23 @@ +package program + +import ( + "github.com/google/go-jsonnet/ast" + "github.com/google/go-jsonnet/parser" +) + +// SnippetToAST converts a Jsonnet code snippet to a desugared and analyzed AST. +func SnippetToAST(filename string, snippet string) (ast.Node, error) { + node, err := parser.SnippetToRawAST(filename, snippet) + if err != nil { + return nil, err + } + err = desugarAST(&node) + if err != nil { + return nil, err + } + err = analyze(node) + if err != nil { + return nil, err + } + return node, nil +} diff --git a/internal/transformations/static_analyzer.go b/internal/program/static_analyzer.go similarity index 94% rename from internal/transformations/static_analyzer.go rename to internal/program/static_analyzer.go index 7061aa9..0ae2b32 100644 --- a/internal/transformations/static_analyzer.go +++ b/internal/program/static_analyzer.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package transformations +package program import ( "fmt" @@ -164,9 +164,9 @@ func analyzeVisit(a ast.Node, inObject bool, vars ast.IdentifierSet) error { return s.err } -// Analyze checks variable references (these could be checked statically in Jsonnet). -// It enriches ast with additional information about free variables in every node, -// so it is necessary to always run it before executing AST. -func Analyze(node ast.Node) error { +// analyze checks variable references (these could be checked statically in Jsonnet). +// It enriches the AST with additional information about free variables in every node, +// so it is necessary to always run it before executing the AST. +func analyze(node ast.Node) error { return analyzeVisit(node, false, ast.NewIdentifierSet("std")) } diff --git a/internal/transformations/static_analyzer_test.go b/internal/program/static_analyzer_test.go similarity index 92% rename from internal/transformations/static_analyzer_test.go rename to internal/program/static_analyzer_test.go index 3859672..fae4623 100644 --- a/internal/transformations/static_analyzer_test.go +++ b/internal/program/static_analyzer_test.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package transformations +package program import ( "testing" @@ -22,13 +22,9 @@ import ( "github.com/google/go-jsonnet/ast" ) -// func dummyNodeBase() astNodeBase { -// return astNode -// } - func TestSimpleNull(t *testing.T) { ast := &ast.LiteralNull{} - err := Analyze(ast) + err := analyze(ast) if err != nil { t.Errorf("Unexpected error: %+v", err) } @@ -60,7 +56,7 @@ func TestSimpleLocal(t *testing.T) { Body: &ast.Var{Id: "x"}, } - err := Analyze(node) + err := analyze(node) if err != nil { t.Errorf("Unexpected error: %+v", err) } diff --git a/internal/transformations/transformations.go b/internal/transformations/transformations.go deleted file mode 100644 index 206e3ef..0000000 --- a/internal/transformations/transformations.go +++ /dev/null @@ -1,31 +0,0 @@ -package transformations - -import ( - "github.com/google/go-jsonnet/ast" - "github.com/google/go-jsonnet/parser" -) - -func snippetToRawAST(filename string, snippet string) (ast.Node, error) { - tokens, err := parser.Lex(filename, snippet) - if err != nil { - return nil, err - } - return parser.Parse(tokens) -} - -// SnippetToAST converts Jsonnet code snippet to desugared and analyzed AST -func SnippetToAST(filename string, snippet string) (ast.Node, error) { - node, err := snippetToRawAST(filename, snippet) - if err != nil { - return nil, err - } - err = Desugar(&node) - if err != nil { - return nil, err - } - err = Analyze(node) - if err != nil { - return nil, err - } - return node, nil -} diff --git a/parser/parser.go b/parser/parser.go index 2b33031..6de6f26 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -1192,7 +1192,7 @@ func Parse(t Tokens) (ast.Node, error) { return expr, nil } -// SnippetToRawAST converts Jsonnet code snippet to AST (without any transformations) +// SnippetToRawAST converts a Jsonnet code snippet to an AST (without any transformations). func SnippetToRawAST(filename string, snippet string) (ast.Node, error) { tokens, err := Lex(filename, snippet) if err != nil { diff --git a/vm.go b/vm.go index fc433e4..1afce79 100644 --- a/vm.go +++ b/vm.go @@ -22,7 +22,7 @@ import ( "runtime/debug" "github.com/google/go-jsonnet/ast" - "github.com/google/go-jsonnet/internal/transformations" + "github.com/google/go-jsonnet/internal/program" ) // Note: There are no garbage collection params because we're using the native @@ -201,7 +201,7 @@ func (vm *VM) EvaluateSnippetMulti(filename string, snippet string) (files map[s // SnippetToAST parses a snippet and returns the resulting AST. func SnippetToAST(filename string, snippet string) (ast.Node, error) { - return transformations.SnippetToAST(filename, snippet) + return program.SnippetToAST(filename, snippet) } // Version returns the Jsonnet version number.