From 294b8a37a95f932090401eb769887d8254de88dc Mon Sep 17 00:00:00 2001 From: Dave Cunningham Date: Mon, 14 Aug 2017 16:12:04 -0400 Subject: [PATCH] Fix #172 by desugaring Import filenames. --- core/desugarer.cpp | 16 ++++++++++----- core/string_utils.h | 5 +++++ doc/language/spec.html | 3 ++- .../error.parse.import_text_block.jsonnet | 20 +++++++++++++++++++ ...ror.parse.import_text_block.jsonnet.golden | 1 + test_suite/import.jsonnet | 6 ++++++ 6 files changed, 45 insertions(+), 6 deletions(-) create mode 100644 test_suite/error.parse.import_text_block.jsonnet create mode 100644 test_suite/error.parse.import_text_block.jsonnet.golden diff --git a/core/desugarer.cpp b/core/desugarer.cpp index 771d108a6..37dace3b7 100644 --- a/core/desugarer.cpp +++ b/core/desugarer.cpp @@ -643,11 +643,17 @@ class Desugarer { desugar(ast->body, obj_level); desugarParams(ast->params, obj_level); - } else if (dynamic_cast(ast_)) { - // Nothing to do. - - } else if (dynamic_cast(ast_)) { - // Nothing to do. + } else if (auto *ast = dynamic_cast(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(file); + + } else if (auto *ast = dynamic_cast(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(file); } else if (auto *ast = dynamic_cast(ast_)) { desugar(ast->element, obj_level); diff --git a/core/string_utils.h b/core/string_utils.h index fd9d7731c..7f7ed5c51 100644 --- a/core/string_utils.h +++ b/core/string_utils.h @@ -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); diff --git a/doc/language/spec.html b/doc/language/spec.html index 9cecb79d4..a47a8a934 100644 --- a/doc/language/spec.html +++ b/doc/language/spec.html @@ -138,7 +138,8 @@

Lexing

first subsequent line that does not begin with W, and it is an error if this line does not contain some optional whitespace followed by |||. The content of the string is the concatenation of all the lines that began with W but with that prefix stripped. The line -ending style in the file is preserved in the string. +ending style in the file is preserved in the string. This form cannot be used in +import statements.

Double- and single-quoted strings are allowed to span multiple lines, in which case whatever diff --git a/test_suite/error.parse.import_text_block.jsonnet b/test_suite/error.parse.import_text_block.jsonnet new file mode 100644 index 000000000..d68b77519 --- /dev/null +++ b/test_suite/error.parse.import_text_block.jsonnet @@ -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. +||| diff --git a/test_suite/error.parse.import_text_block.jsonnet.golden b/test_suite/error.parse.import_text_block.jsonnet.golden new file mode 100644 index 000000000..6142cc9d3 --- /dev/null +++ b/test_suite/error.parse.import_text_block.jsonnet.golden @@ -0,0 +1 @@ +STATIC ERROR: error.parse.import_text_block.jsonnet:(17:8)-(20:3): Cannot use text blocks in import statements. diff --git a/test_suite/import.jsonnet b/test_suite/import.jsonnet index 800143fa9..f1cf9038a 100644 --- a/test_suite/import.jsonnet +++ b/test_suite/import.jsonnet @@ -17,6 +17,9 @@ 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 @@ -24,7 +27,10 @@ std.assertEqual((import 'lib/A_20_func.libsonnet')(), 20) && // 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") &&