Skip to content

Commit

Permalink
Fix #172
Browse files Browse the repository at this point in the history
  • Loading branch information
sparkprime committed Aug 14, 2017
1 parent 4a2a781 commit 9a19c6b
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 14 deletions.
12 changes: 6 additions & 6 deletions core/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -432,21 +432,21 @@ struct Function : public AST {
}
};

struct LiteralString;

/** Represents import "file". */
struct Import : public AST {
LiteralString *file;
Import(const LocationRange &lr, const Fodder &open_fodder, LiteralString *file)
// Must be a LiteralString.
AST *file;
Import(const LocationRange &lr, const Fodder &open_fodder, AST *file)
: AST(lr, AST_IMPORT, open_fodder), file(file)
{
}
};

/** Represents importstr "file". */
struct Importstr : public AST {
LiteralString *file;
Importstr(const LocationRange &lr, const Fodder &open_fodder, LiteralString *file)
// Must be a LiteralString.
AST *file;
Importstr(const LocationRange &lr, const Fodder &open_fodder, AST *file)
: AST(lr, AST_IMPORTSTR, open_fodder), file(file)
{
}
Expand Down
8 changes: 4 additions & 4 deletions core/desugarer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -643,11 +643,11 @@ class Desugarer {
desugar(ast->body, obj_level);
desugarParams(ast->params, obj_level);

} else if (dynamic_cast<const Import *>(ast_)) {
// Nothing to do.
} else if (auto *ast = dynamic_cast<Import *>(ast_)) {
desugar(ast->file, obj_level);

} else if (dynamic_cast<const Importstr *>(ast_)) {
// Nothing to do.
} else if (auto *ast = dynamic_cast<Importstr *>(ast_)) {
desugar(ast->file, obj_level);

} else if (auto *ast = dynamic_cast<InSuper *>(ast_)) {
desugar(ast->element, obj_level);
Expand Down
2 changes: 1 addition & 1 deletion core/formatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2031,7 +2031,7 @@ class SortImports {
/// Get the value by which the imports should be sorted.
UString sortingKey(Import *import)
{
return import->file->value;
return dynamic_cast<const LiteralString*>(import->file)->value;
}

/// Check if `local` expression is used for importing,
Expand Down
5 changes: 5 additions & 0 deletions core/string_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ limitations under the License.
/** Unparse the string. */
UString jsonnet_string_unparse(const UString &str, bool single);


// Note that the following two functions do not handle the quoting of ' and "
// inside verbatim strings because that quoting is reversible. Thus, that
// quoting is done at lexing time and undone again at pretty-printing time.

/** Escape special characters. */
UString jsonnet_string_escape(const UString &str, bool single);

Expand Down
6 changes: 4 additions & 2 deletions core/vm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1509,7 +1509,8 @@ class Interpreter {

case AST_IMPORT: {
const auto &ast = *static_cast<const Import *>(ast_);
HeapThunk *thunk = import(ast.location, ast.file);
const auto *file = static_cast<const LiteralString *>(ast.file);
HeapThunk *thunk = import(ast.location, file);
if (thunk->filled) {
scratch = thunk->content;
} else {
Expand All @@ -1521,7 +1522,8 @@ class Interpreter {

case AST_IMPORTSTR: {
const auto &ast = *static_cast<const Importstr *>(ast_);
const ImportCacheValue *value = importString(ast.location, ast.file);
const auto *file = static_cast<const LiteralString *>(ast.file);
const ImportCacheValue *value = importString(ast.location, file);
scratch = makeString(decode_utf8(value->content));
} break;

Expand Down
3 changes: 2 additions & 1 deletion doc/language/spec.html
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ <h2 id="lexing">Lexing</h2>
first subsequent line that does not begin with <i>W</i>, and it is an error if this line does not
contain some optional whitespace followed by <code>|||</code>. The content of the string is the
concatenation of all the lines that began with <i>W</i> but with that prefix stripped. The line
ending style in the file is preserved in the string.</li>
ending style in the file is preserved in the string. This form cannot be used in
<code>import</code> statements.</li>
</ul>

<p>Double- and single-quoted strings are allowed to span multiple lines, in which case whatever
Expand Down
6 changes: 6 additions & 0 deletions test_suite/import.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,20 @@ limitations under the License.
// Can capture variables from another file.
std.assertEqual((import "lib/A_20_func.libsonnet")(), 20) &&

// Ensure string is quoted.
std.assertEqual((import "lib/\u0041_20_func.libsonnet")(), 20) &&

# Test single quoted string.
std.assertEqual((import 'lib/A_20_func.libsonnet')(), 20) &&
# The block string is hard to test because the filename would include a terminating \n

// Each import has its own environment, can't be overidden.
std.assertEqual(local A = 7; local lib = import "lib/A_20.libsonnet"; lib, 20) &&
std.assertEqual(local A = 7, lib = import "lib/A_20.libsonnet"; lib, 20) &&

std.assertEqual(importstr "lib/some_file.txt", "Hello World!\n") &&
std.assertEqual(importstr "lib/\u0073ome_file.txt", "Hello World!\n") &&

std.assertEqual(import "lib/rel_path.libsonnet", "rel_path") &&
std.assertEqual(import "lib/rel_path4.libsonnet", "rel_path") &&

Expand Down

0 comments on commit 9a19c6b

Please sign in to comment.