Skip to content

Commit

Permalink
Bring up-to-date with cpp version
Browse files Browse the repository at this point in the history
  • Loading branch information
sparkprime committed May 22, 2019
1 parent 6fa1db7 commit df7753e
Show file tree
Hide file tree
Showing 11 changed files with 16,026 additions and 14,334 deletions.
29,987 changes: 15,857 additions & 14,130 deletions ast/stdast.go

Large diffs are not rendered by default.

311 changes: 138 additions & 173 deletions cmd/jsonnet/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,12 @@ func version(o io.Writer) {
func usage(o io.Writer) {
version(o)
fmt.Fprintln(o)
fmt.Fprintln(o, "General commandline:")
fmt.Fprintln(o, "jsonnet [<cmd>] {<option>} { <filename> }")
fmt.Fprintln(o, "Note: <cmd> defaults to \"eval\"")
fmt.Fprintln(o, "jsonnet {<option>} <filename>")
fmt.Fprintln(o)
fmt.Fprintln(o, "The eval command:")
fmt.Fprintln(o, "jsonnet eval {<option>} <filename>")
fmt.Fprintln(o, "Note: Only one filename is supported")
fmt.Fprintln(o)
fmt.Fprintln(o, "Available eval options:")
fmt.Fprintln(o, "Available options:")
fmt.Fprintln(o, " -h / --help This message")
fmt.Fprintln(o, " -e / --exec Treat filename as code")
fmt.Fprintln(o, " -J / --jpath <dir> Specify an additional library search dir")
fmt.Fprintln(o, " -J / --jpath <dir> Specify an additional library search dir (right-most wins)")
fmt.Fprintln(o, " -o / --output-file <file> Write to the output file rather than stdout")
fmt.Fprintln(o, " -m / --multi <dir> Write multiple files to the directory, list files on stdout")
fmt.Fprintln(o, " -c / --create-output-dirs Automatically creates all parent directories for files")
Expand All @@ -106,9 +100,12 @@ func usage(o io.Writer) {
fmt.Fprintln(o, "Provide a value as Jsonnet code:")
fmt.Fprintln(o, " --tla-code <var>[=<code>] If <code> is omitted, get from environment var <var>")
fmt.Fprintln(o, " --tla-code-file <var>=<file> Read the code from the file")
fmt.Fprintln(o)
fmt.Fprintln(o, "The fmt command:")
fmt.Fprintln(o, "jsonnet fmt is currently not available in the Go implementation")
fmt.Fprintln(o, "Environment variables:")
fmt.Fprintln(o, "JSONNET_PATH is a colon (semicolon on Windows) separated list of directories added")
fmt.Fprintln(o, "in reverse order before the paths specified by --jpath (i.e. left-most wins)")
fmt.Fprintln(o, "E.g. JSONNET_PATH=a:b jsonnet -J c -J d is equivalent to:")
fmt.Fprintln(o, "JSONNET_PATH=d:c:a:b jsonnet")
fmt.Fprintln(o, "jsonnet -J b -J a -J c -J d")
fmt.Fprintln(o)
fmt.Fprintln(o, "In all cases:")
fmt.Fprintln(o, "<filename> can be - (stdin)")
Expand All @@ -129,31 +126,20 @@ func safeStrToInt(str string) (i int) {

type command int

const (
commandEval = iota
commandFmt = iota
)

type config struct {
cmd command
inputFiles []string
outputFile string
filenameIsCode bool

// commandEval flags
evalMulti bool
evalStream bool
evalMultiOutputDir string
evalCreateOutputDirs bool
evalJpath []string

// commandFmt flags
// commandFmt is currently unsupported.
}

func makeConfig() config {
return config{
cmd: commandEval,
filenameIsCode: false,
evalMulti: false,
evalStream: false,
Expand Down Expand Up @@ -197,13 +183,6 @@ func processArgs(givenArgs []string, config *config, vm *jsonnet.VM) (processArg
args := simplifyArgs(givenArgs)
remainingArgs := make([]string, 0, 0)
i := 0
if len(args) > 0 && args[i] == "fmt" {
config.cmd = commandFmt
i++
} else if len(args) > 0 && args[i] == "eval" {
config.cmd = commandEval
i++
}

handleVarVal := func(handle func(key string, val string)) error {
next := nextArg(&i, args)
Expand Down Expand Up @@ -247,83 +226,79 @@ func processArgs(givenArgs []string, config *config, vm *jsonnet.VM) (processArg
remainingArgs = append(remainingArgs, args[i])
}
break
} else if config.cmd == commandEval {
if arg == "-s" || arg == "--max-stack" {
l := safeStrToInt(nextArg(&i, args))
if l < 1 {
return processArgsStatusFailure, fmt.Errorf("invalid --max-stack value: %d", l)
}
vm.MaxStack = l
} else if arg == "-J" || arg == "--jpath" {
dir := nextArg(&i, args)
if len(dir) == 0 {
return processArgsStatusFailure, fmt.Errorf("-J argument was empty string")
}
if dir[len(dir)-1] != '/' {
dir += "/"
}
config.evalJpath = append(config.evalJpath, dir)
} else if arg == "-V" || arg == "--ext-str" {
if err := handleVarVal(vm.ExtVar); err != nil {
return processArgsStatusFailure, err
}
} else if arg == "--ext-str-file" {
if err := handleVarFile(vm.ExtCode, "importstr"); err != nil {
return processArgsStatusFailure, err
}
} else if arg == "--ext-code" {
if err := handleVarVal(vm.ExtCode); err != nil {
return processArgsStatusFailure, err
}
} else if arg == "--ext-code-file" {
if err := handleVarFile(vm.ExtCode, "import"); err != nil {
return processArgsStatusFailure, err
}
} else if arg == "-A" || arg == "--tla-str" {
if err := handleVarVal(vm.TLAVar); err != nil {
return processArgsStatusFailure, err
}
} else if arg == "--tla-str-file" {
if err := handleVarFile(vm.TLACode, "importstr"); err != nil {
return processArgsStatusFailure, err
}
} else if arg == "--tla-code" {
if err := handleVarVal(vm.TLACode); err != nil {
return processArgsStatusFailure, err
}
} else if arg == "--tla-code-file" {
if err := handleVarFile(vm.TLACode, "import"); err != nil {
return processArgsStatusFailure, err
}
} else if arg == "-t" || arg == "--max-trace" {
l := safeStrToInt(nextArg(&i, args))
if l < 0 {
return processArgsStatusFailure, fmt.Errorf("invalid --max-trace value: %d", l)
}
vm.ErrorFormatter.SetMaxStackTraceSize(l)
} else if arg == "-m" || arg == "--multi" {
config.evalMulti = true
outputDir := nextArg(&i, args)
if len(outputDir) == 0 {
return processArgsStatusFailure, fmt.Errorf("-m argument was empty string")
}
if outputDir[len(outputDir)-1] != '/' {
outputDir += "/"
}
config.evalMultiOutputDir = outputDir
} else if arg == "-c" || arg == "--create-output-dirs" {
config.evalCreateOutputDirs = true
} else if arg == "-y" || arg == "--yaml-stream" {
config.evalStream = true
} else if arg == "-S" || arg == "--string" {
vm.StringOutput = true
} else if len(arg) > 1 && arg[0] == '-' {
return processArgsStatusFailure, fmt.Errorf("unrecognized argument: %s", arg)
} else {
remainingArgs = append(remainingArgs, arg)
} else if arg == "-s" || arg == "--max-stack" {
l := safeStrToInt(nextArg(&i, args))
if l < 1 {
return processArgsStatusFailure, fmt.Errorf("invalid --max-stack value: %d", l)
}
vm.MaxStack = l
} else if arg == "-J" || arg == "--jpath" {
dir := nextArg(&i, args)
if len(dir) == 0 {
return processArgsStatusFailure, fmt.Errorf("-J argument was empty string")
}
if dir[len(dir)-1] != '/' {
dir += "/"
}
config.evalJpath = append(config.evalJpath, dir)
} else if arg == "-V" || arg == "--ext-str" {
if err := handleVarVal(vm.ExtVar); err != nil {
return processArgsStatusFailure, err
}
} else if arg == "--ext-str-file" {
if err := handleVarFile(vm.ExtCode, "importstr"); err != nil {
return processArgsStatusFailure, err
}
} else if arg == "--ext-code" {
if err := handleVarVal(vm.ExtCode); err != nil {
return processArgsStatusFailure, err
}
} else if arg == "--ext-code-file" {
if err := handleVarFile(vm.ExtCode, "import"); err != nil {
return processArgsStatusFailure, err
}
} else if arg == "-A" || arg == "--tla-str" {
if err := handleVarVal(vm.TLAVar); err != nil {
return processArgsStatusFailure, err
}
} else if arg == "--tla-str-file" {
if err := handleVarFile(vm.TLACode, "importstr"); err != nil {
return processArgsStatusFailure, err
}
} else if arg == "--tla-code" {
if err := handleVarVal(vm.TLACode); err != nil {
return processArgsStatusFailure, err
}
} else if arg == "--tla-code-file" {
if err := handleVarFile(vm.TLACode, "import"); err != nil {
return processArgsStatusFailure, err
}
} else if arg == "-t" || arg == "--max-trace" {
l := safeStrToInt(nextArg(&i, args))
if l < 0 {
return processArgsStatusFailure, fmt.Errorf("invalid --max-trace value: %d", l)
}
vm.ErrorFormatter.SetMaxStackTraceSize(l)
} else if arg == "-m" || arg == "--multi" {
config.evalMulti = true
outputDir := nextArg(&i, args)
if len(outputDir) == 0 {
return processArgsStatusFailure, fmt.Errorf("-m argument was empty string")
}
if outputDir[len(outputDir)-1] != '/' {
outputDir += "/"
}
config.evalMultiOutputDir = outputDir
} else if arg == "-c" || arg == "--create-output-dirs" {
config.evalCreateOutputDirs = true
} else if arg == "-y" || arg == "--yaml-stream" {
config.evalStream = true
} else if arg == "-S" || arg == "--string" {
vm.StringOutput = true
} else if len(arg) > 1 && arg[0] == '-' {
return processArgsStatusFailure, fmt.Errorf("unrecognized argument: %s", arg)
} else {
return processArgsStatusFailure, fmt.Errorf("the Go implementation currently does not support jsonnet fmt")
remainingArgs = append(remainingArgs, arg)
}
}

Expand Down Expand Up @@ -543,86 +518,76 @@ func main() {
JPaths: config.evalJpath,
})

if config.cmd == commandEval {
if len(config.inputFiles) != 1 {
// Should already have been caught by processArgs.
panic(fmt.Sprintf("Internal error: expected a single input file."))
}
filename := config.inputFiles[0]
input, err := readInput(config, &filename)
if err != nil {
var op string
switch typedErr := err.(type) {
case *os.PathError:
op = typedErr.Op
err = typedErr.Err
}
if op == "open" {
fmt.Fprintf(os.Stderr, "Opening input file: %s: %s\n", filename, err.Error())
} else if op == "read" {
fmt.Fprintf(os.Stderr, "Reading input file: %s: %s\n", filename, err.Error())
} else {
fmt.Fprintf(os.Stderr, err.Error())
}
os.Exit(1)
if len(config.inputFiles) != 1 {
// Should already have been caught by processArgs.
panic(fmt.Sprintf("Internal error: expected a single input file."))
}
filename := config.inputFiles[0]
input, err := readInput(config, &filename)
if err != nil {
var op string
switch typedErr := err.(type) {
case *os.PathError:
op = typedErr.Op
err = typedErr.Err
}
var output string
var outputArray []string
var outputDict map[string]string
if config.evalMulti {
outputDict, err = vm.EvaluateSnippetMulti(filename, input)
} else if config.evalStream {
outputArray, err = vm.EvaluateSnippetStream(filename, input)
if op == "open" {
fmt.Fprintf(os.Stderr, "Opening input file: %s: %s\n", filename, err.Error())
} else if op == "read" {
fmt.Fprintf(os.Stderr, "Reading input file: %s: %s\n", filename, err.Error())
} else {
output, err = vm.EvaluateSnippet(filename, input)
fmt.Fprintf(os.Stderr, err.Error())
}
os.Exit(1)
}
var output string
var outputArray []string
var outputDict map[string]string
if config.evalMulti {
outputDict, err = vm.EvaluateSnippetMulti(filename, input)
} else if config.evalStream {
outputArray, err = vm.EvaluateSnippetStream(filename, input)
} else {
output, err = vm.EvaluateSnippet(filename, input)
}

var memprofile = os.Getenv("JSONNET_MEM_PROFILE")
if memprofile != "" {
f, err := os.Create(memprofile)
if err != nil {
log.Fatal("could not create memory profile: ", err)
}
runtime.GC() // get up-to-date statistics
if err := pprof.WriteHeapProfile(f); err != nil {
log.Fatal("could not write memory profile: ", err)
}
f.Close()
var memprofile = os.Getenv("JSONNET_MEM_PROFILE")
if memprofile != "" {
f, err := os.Create(memprofile)
if err != nil {
log.Fatal("could not create memory profile: ", err)
}
runtime.GC() // get up-to-date statistics
if err := pprof.WriteHeapProfile(f); err != nil {
log.Fatal("could not write memory profile: ", err)
}
f.Close()
}

if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}

// Write output JSON.
if config.evalMulti {
err := writeMultiOutputFiles(outputDict, config.evalMultiOutputDir, config.outputFile, config.evalCreateOutputDirs)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}

// Write output JSON.
if config.evalMulti {
err := writeMultiOutputFiles(outputDict, config.evalMultiOutputDir, config.outputFile, config.evalCreateOutputDirs)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
} else if config.evalStream {
err := writeOutputStream(outputArray, config.outputFile)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
} else {
err := writeOutputFile(output, config.outputFile, config.evalCreateOutputDirs)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
} else if config.evalStream {
err := writeOutputStream(outputArray, config.outputFile)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}

} else if config.cmd == commandFmt {
// Should already have been caught by processArgs.
panic(fmt.Sprintf("Internal error: No jsonnet fmt."))

} else {
panic(fmt.Sprintf("Internal error (please report this): Bad cmd value: %d\n", config.cmd))

err := writeOutputFile(output, config.outputFile, config.evalCreateOutputDirs)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
}

}
2 changes: 1 addition & 1 deletion cpp-jsonnet
Submodule cpp-jsonnet updated 78 files
+3 −0 .gitignore
+3 −2 CMakeLists.txt
+13 −7 Makefile
+33 −7 README.md
+6 −21 WORKSPACE
+1 −6 benchmarks/bench.06.jsonnet
+20 −1 cmd/BUILD
+11 −3 cmd/CMakeLists.txt
+186 −529 cmd/jsonnet.cpp
+326 −0 cmd/jsonnetfmt.cpp
+147 −0 cmd/utils.cpp
+50 −0 cmd/utils.h
+6 −6 core/BUILD
+8 −2 core/CMakeLists.txt
+3 −4 core/desugarer.cpp
+4 −0 core/vm.cpp
+1 −1 cpp/BUILD
+2 −2 doc/_includes/examples/mixins.jsonnet
+36 −48 doc/_includes/examples/mixins.jsonnet.golden
+1 −1 doc/_layouts/base.html
+1 −0 doc/articles/comparisons.html
+1 −0 doc/articles/design.html
+1 −0 doc/articles/fractal.1.html
+1 −0 doc/articles/fractal.2.html
+1 −0 doc/articles/fractal.3.html
+2 −1 doc/articles/kubernetes.html
+1 −0 doc/articles/output-formats.html
+1 −0 doc/index.html
+4 −4 doc/js/demo.js
+1 −0 doc/learning/community.html
+1 −0 doc/learning/getting_started.html
+2 −1 doc/learning/tools.html
+1 −0 doc/learning/tutorial.html
+1 −0 doc/ref/bindings.html
+1 −0 doc/ref/language.html
+1 −0 doc/ref/spec.html
+37 −5 doc/ref/stdlib.html
+0 −605 gmock.BUILD
+2 −1 include/CMakeLists.txt
+28 −19 stdlib/std.jsonnet
+23 −5 test_cmd/cmd_tests.source
+0 −73 test_cmd/eval.golden.stderr.cpp
+0 −48 test_cmd/eval.golden.stderr.golang
+0 −5 test_cmd/exec2.golden.stdout
+1 −0 test_cmd/fmt1.golden.stdout
+1 −0 test_cmd/fmt_bad_out.golden.stderr
+1 −0 test_cmd/fmt_double_dash.golden.stdout
+28 −0 test_cmd/fmt_help.golden.stdout.cpp
+30 −0 test_cmd/fmt_no_args.golden.stderr
+1 −0 test_cmd/fmt_out.golden.custom_output
+5 −0 test_cmd/fmt_simple1.golden.stdout
+5 −0 test_cmd/fmt_simple3.golden.stdout
+5 −0 test_cmd/fmt_simple3.stdin
+1 −0 test_cmd/fmt_simple4.golden.stderr
+1 −0 test_cmd/fmt_simple5.golden.stderr
+5 −0 test_cmd/fmt_simple_out.golden.custom_output
+1 −0 test_cmd/fmt_version1.golden.stdout
+1 −0 test_cmd/fmt_version2.golden.stdout
+2 −29 test_cmd/help.golden.stdout.cpp
+9 −12 test_cmd/help.golden.stdout.golang
+2 −29 test_cmd/no_args.golden.stderr.cpp
+9 −12 test_cmd/no_args.golden.stderr.golang
+0 −1 test_cmd/nosuchcommand.golden.stderr.cpp
+0 −1 test_cmd/nosuchcommand.golden.stderr.golang
+25 −6 test_cmd/run_cmd_tests.sh
+1 −1 test_suite/error.equality_function.jsonnet.golden
+5 −5 test_suite/error.inside_equals_array.jsonnet.golden
+5 −5 test_suite/error.inside_equals_object.jsonnet.golden
+4 −4 test_suite/error.invariant.equality.jsonnet.golden
+4 −4 test_suite/error.obj_assert.fail1.jsonnet.golden
+4 −4 test_suite/error.obj_assert.fail2.jsonnet.golden
+1 −1 test_suite/error.sanity.jsonnet.golden
+6 −0 test_suite/format.jsonnet
+3 −3 test_suite/run_fmt_idempotence_tests.sh
+2 −2 test_suite/run_fmt_tests.sh
+30 −0 test_suite/stdlib.jsonnet
+1 −0 test_suite/tests.source
+1 −0 tools/scripts/serve_docs.sh
2 changes: 1 addition & 1 deletion testdata/assert_equal4.golden
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
RUNTIME ERROR: Assertion failed. {"x": 1} != {"x": 2}
-------------------------------------------------
<std>:782:7-50 function <anonymous>
<std>:787:7-50 function <anonymous>

error 'Assertion failed. ' + a + ' != ' + b,

Expand Down
Loading

0 comments on commit df7753e

Please sign in to comment.