Support for various kinds of strings in imports

This commit is contained in:
Stanisław Barzowski 2017-09-25 16:36:13 -04:00 committed by Dave Cunningham
parent 121a77c66f
commit 0b52ea4d40
23 changed files with 71 additions and 10 deletions

View File

@ -300,7 +300,7 @@ type Parameters struct {
// Import represents import "file".
type Import struct {
NodeBase
File string
File *LiteralString
}
// ---------------------------------------------------------------------------
@ -308,7 +308,7 @@ type Import struct {
// ImportStr represents importstr "file".
type ImportStr struct {
NodeBase
File string
File *LiteralString
}
// ---------------------------------------------------------------------------

View File

@ -438,10 +438,18 @@ func desugar(astPtr *ast.Node, objLevel int) (err error) {
}
case *ast.Import:
// Nothing to do.
var file ast.Node = node.File
err = desugar(&file, objLevel)
if err != nil {
return
}
case *ast.ImportStr:
// Nothing to do.
var file ast.Node = node.File
err = desugar(&file, objLevel)
if err != nil {
return
}
case *ast.Index:
err = desugar(&node.Target, objLevel)

View File

@ -379,11 +379,11 @@ func (i *interpreter) evaluate(a ast.Node, context *TraceContext) (value, error)
case *ast.Import:
codeDir := path.Dir(ast.Loc().FileName)
return i.importCache.ImportCode(codeDir, ast.File, e)
return i.importCache.ImportCode(codeDir, ast.File.Value, e)
case *ast.ImportStr:
codeDir := path.Dir(ast.Loc().FileName)
return i.importCache.ImportString(codeDir, ast.File, e)
return i.importCache.ImportString(codeDir, ast.File.Value, e)
case *ast.LiteralBoolean:
return makeValueBoolean(ast.Value), nil

View File

@ -715,7 +715,7 @@ func (p *parser) parseTerminal() (ast.Node, error) {
return &ast.LiteralString{
NodeBase: ast.NewNodeBaseLoc(tok.loc),
Value: tok.data,
Kind: ast.StringDouble,
Kind: ast.StringBlock,
BlockIndent: tok.stringBlockIndent,
}, nil
case tokenVerbatimStringDouble:
@ -902,9 +902,12 @@ func (p *parser) parse(prec precedence) (ast.Node, error) {
return nil, err
}
if lit, ok := body.(*ast.LiteralString); ok {
if lit.Kind == ast.StringBlock {
return nil, MakeStaticError("Block string literals not allowed in imports", *body.Loc())
}
return &ast.Import{
NodeBase: ast.NewNodeBaseLoc(locFromTokenAST(begin, body)),
File: lit.Value,
File: lit,
}, nil
}
return nil, MakeStaticError("Computed imports are not allowed", *body.Loc())
@ -916,9 +919,12 @@ func (p *parser) parse(prec precedence) (ast.Node, error) {
return nil, err
}
if lit, ok := body.(*ast.LiteralString); ok {
if lit.Kind == ast.StringBlock {
return nil, MakeStaticError("Block string literals not allowed in imports", *body.Loc())
}
return &ast.ImportStr{
NodeBase: ast.NewNodeBaseLoc(locFromTokenAST(begin, body)),
File: lit.Value,
File: lit,
}, nil
}
return nil, MakeStaticError("Computed imports are not allowed", *body.Loc())

1
testdata/".golden vendored Normal file
View File

@ -0,0 +1 @@
testdata/":2:1 Unexpected end of file.

1
testdata/".jsonnet vendored Normal file
View File

@ -0,0 +1 @@
// This file is there only for its filename: to test escaping in imports

1
testdata/'.golden vendored Normal file
View File

@ -0,0 +1 @@
testdata/':2:1 Unexpected end of file.

1
testdata/'.jsonnet vendored Normal file
View File

@ -0,0 +1 @@
// This file is there only for its filename: to test escaping in imports

1
testdata/import_block_literal.golden vendored Normal file
View File

@ -0,0 +1 @@
testdata/import_block_literal:(1:8)-(3:4) Block string literals not allowed in imports

3
testdata/import_block_literal.jsonnet vendored Normal file
View File

@ -0,0 +1,3 @@
import |||
block_literals_for_imports_are_not_allowed_and_make_exactly_zero_sense
|||

1
testdata/import_computed.golden vendored Normal file
View File

@ -0,0 +1 @@
testdata/import_computed:1:9-16 Computed imports are not allowed

1
testdata/import_computed.jsonnet vendored Normal file
View File

@ -0,0 +1 @@
import "a" + "b"

View File

@ -0,0 +1,6 @@
[
true,
true,
true,
true
]

View File

@ -0,0 +1,6 @@
[
import "true.jsonnet",
import 'true.jsonnet',
import @"true.jsonnet",
import @'true.jsonnet',
]

View File

@ -0,0 +1 @@
testdata/".jsonnet:2:1 Unexpected end of file.

View File

@ -0,0 +1,6 @@
[
import "\u0074rue.jsonnet",
import '\u0074rue.jsonnet',
import @""".jsonnet",
import @'''.jsonnet',
]

View File

@ -0,0 +1 @@
testdata/importstr_block_literal:(1:11)-(3:4) Block string literals not allowed in imports

View File

@ -0,0 +1,3 @@
importstr |||
block_literals_for_imports_are_not_allowed_and_make_exactly_zero_sense
|||

1
testdata/importstr_computed.golden vendored Normal file
View File

@ -0,0 +1 @@
testdata/importstr_computed:1:12-19 Computed imports are not allowed

1
testdata/importstr_computed.jsonnet vendored Normal file
View File

@ -0,0 +1 @@
importstr "a" + "b"

View File

@ -0,0 +1,6 @@
[
"true\n",
"true\n",
"// This file is there only for its filename: to test escaping in imports\n",
"// This file is there only for its filename: to test escaping in imports\n"
]

View File

@ -0,0 +1,6 @@
[
importstr "\u0074rue.jsonnet",
importstr '\u0074rue.jsonnet',
importstr @""".jsonnet",
importstr @'''.jsonnet',
]

View File

@ -1 +1 @@
RUNTIME ERROR: Couldn't open import "ąęółńśćźż \\\" \\' \\n\\n\\t\\t": No match locally or in the Jsonnet library paths.
RUNTIME ERROR: Couldn't open import "ąęółńśćźż \" ' \n\n\t\t": No match locally or in the Jsonnet library paths.