Skip to content

Commit

Permalink
Fix #172 by desugaring Import filenames.
Browse files Browse the repository at this point in the history
  • Loading branch information
sparkprime committed Aug 15, 2017
1 parent 4a2a781 commit 294b8a3
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 6 deletions.
16 changes: 11 additions & 5 deletions core/desugarer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -643,11 +643,17 @@ class Desugarer {
desugar(ast->body, obj_level);
desugarParams(ast->params, obj_level);

} else if (dynamic_cast<const Import *>(ast_)) {
// Nothing to do.

} else if (dynamic_cast<const Importstr *>(ast_)) {
// Nothing to do.
} else if (auto *ast = dynamic_cast<Import *>(ast_)) {
// TODO(dcunnin): Abstract this into a template function if it becomes more common.
AST *file = ast->file;
desugar(file, obj_level);
ast->file = dynamic_cast<LiteralString*>(file);

} else if (auto *ast = dynamic_cast<Importstr *>(ast_)) {
// TODO(dcunnin): Abstract this into a template function if it becomes more common.
AST *file = ast->file;
desugar(file, obj_level);
ast->file = dynamic_cast<LiteralString*>(file);

} else if (auto *ast = dynamic_cast<InSuper *>(ast_)) {
desugar(ast->element, obj_level);
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
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
20 changes: 20 additions & 0 deletions test_suite/error.parse.import_text_block.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
Copyright 2017 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import |||
This is a paragraph of text, which is being used in the place of
a filename. That is quite unusual, and probably not intended.
|||
1 change: 1 addition & 0 deletions test_suite/error.parse.import_text_block.jsonnet.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
STATIC ERROR: error.parse.import_text_block.jsonnet:(17:8)-(20:3): Cannot use text blocks in import statements.
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 294b8a3

Please sign in to comment.