Address review comments

This commit is contained in:
Stanisław Barzowski 2019-08-14 18:51:20 +02:00 committed by Dave Cunningham
parent 82f949e7fe
commit e6c74ca7a6
14 changed files with 55 additions and 69 deletions

View File

@ -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",
],
)

View File

@ -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)

View File

@ -16,7 +16,6 @@ go_library(
"internal.h",
"json.cpp",
"json.h",
"libgojsonnet.h",
"libjsonnet.cpp",
],
cdeps = [

View File

@ -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",
],
)

View File

@ -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("<std>", string(buf))
node, err := program.SnippetToAST("<std>", string(buf))
if err != nil {
panic(err)
}

View File

@ -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",

View File

@ -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

View File

@ -14,4 +14,4 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package transformations
package program

View File

@ -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
}

View File

@ -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"))
}

View File

@ -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)
}

View File

@ -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
}

View File

@ -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 {

4
vm.go
View File

@ -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.