Skip to content

Commit 9a19c6b

Browse files
committed
Fix #172
1 parent 4a2a781 commit 9a19c6b

File tree

7 files changed

+28
-14
lines changed

7 files changed

+28
-14
lines changed

core/ast.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -432,21 +432,21 @@ struct Function : public AST {
432432
}
433433
};
434434

435-
struct LiteralString;
436-
437435
/** Represents import "file". */
438436
struct Import : public AST {
439-
LiteralString *file;
440-
Import(const LocationRange &lr, const Fodder &open_fodder, LiteralString *file)
437+
// Must be a LiteralString.
438+
AST *file;
439+
Import(const LocationRange &lr, const Fodder &open_fodder, AST *file)
441440
: AST(lr, AST_IMPORT, open_fodder), file(file)
442441
{
443442
}
444443
};
445444

446445
/** Represents importstr "file". */
447446
struct Importstr : public AST {
448-
LiteralString *file;
449-
Importstr(const LocationRange &lr, const Fodder &open_fodder, LiteralString *file)
447+
// Must be a LiteralString.
448+
AST *file;
449+
Importstr(const LocationRange &lr, const Fodder &open_fodder, AST *file)
450450
: AST(lr, AST_IMPORTSTR, open_fodder), file(file)
451451
{
452452
}

core/desugarer.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -643,11 +643,11 @@ class Desugarer {
643643
desugar(ast->body, obj_level);
644644
desugarParams(ast->params, obj_level);
645645

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

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

652652
} else if (auto *ast = dynamic_cast<InSuper *>(ast_)) {
653653
desugar(ast->element, obj_level);

core/formatter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2031,7 +2031,7 @@ class SortImports {
20312031
/// Get the value by which the imports should be sorted.
20322032
UString sortingKey(Import *import)
20332033
{
2034-
return import->file->value;
2034+
return dynamic_cast<const LiteralString*>(import->file)->value;
20352035
}
20362036

20372037
/// Check if `local` expression is used for importing,

core/string_utils.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ limitations under the License.
2222
/** Unparse the string. */
2323
UString jsonnet_string_unparse(const UString &str, bool single);
2424

25+
26+
// Note that the following two functions do not handle the quoting of ' and "
27+
// inside verbatim strings because that quoting is reversible. Thus, that
28+
// quoting is done at lexing time and undone again at pretty-printing time.
29+
2530
/** Escape special characters. */
2631
UString jsonnet_string_escape(const UString &str, bool single);
2732

core/vm.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1509,7 +1509,8 @@ class Interpreter {
15091509

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

15221523
case AST_IMPORTSTR: {
15231524
const auto &ast = *static_cast<const Importstr *>(ast_);
1524-
const ImportCacheValue *value = importString(ast.location, ast.file);
1525+
const auto *file = static_cast<const LiteralString *>(ast.file);
1526+
const ImportCacheValue *value = importString(ast.location, file);
15251527
scratch = makeString(decode_utf8(value->content));
15261528
} break;
15271529

doc/language/spec.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,8 @@ <h2 id="lexing">Lexing</h2>
138138
first subsequent line that does not begin with <i>W</i>, and it is an error if this line does not
139139
contain some optional whitespace followed by <code>|||</code>. The content of the string is the
140140
concatenation of all the lines that began with <i>W</i> but with that prefix stripped. The line
141-
ending style in the file is preserved in the string.</li>
141+
ending style in the file is preserved in the string. This form cannot be used in
142+
<code>import</code> statements.</li>
142143
</ul>
143144

144145
<p>Double- and single-quoted strings are allowed to span multiple lines, in which case whatever

test_suite/import.jsonnet

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,20 @@ limitations under the License.
1717
// Can capture variables from another file.
1818
std.assertEqual((import "lib/A_20_func.libsonnet")(), 20) &&
1919

20+
// Ensure string is quoted.
21+
std.assertEqual((import "lib/\u0041_20_func.libsonnet")(), 20) &&
22+
2023
# Test single quoted string.
2124
std.assertEqual((import 'lib/A_20_func.libsonnet')(), 20) &&
2225
# The block string is hard to test because the filename would include a terminating \n
2326

2427
// Each import has its own environment, can't be overidden.
2528
std.assertEqual(local A = 7; local lib = import "lib/A_20.libsonnet"; lib, 20) &&
2629
std.assertEqual(local A = 7, lib = import "lib/A_20.libsonnet"; lib, 20) &&
30+
2731
std.assertEqual(importstr "lib/some_file.txt", "Hello World!\n") &&
32+
std.assertEqual(importstr "lib/\u0073ome_file.txt", "Hello World!\n") &&
33+
2834
std.assertEqual(import "lib/rel_path.libsonnet", "rel_path") &&
2935
std.assertEqual(import "lib/rel_path4.libsonnet", "rel_path") &&
3036

0 commit comments

Comments
 (0)