Skip to content

Commit

Permalink
Merge pull request #340 from sparkprime/fix_verbatim_strings
Browse files Browse the repository at this point in the history
Fix handling of verbatim strings as object fields
  • Loading branch information
sparkprime authored Aug 14, 2017
2 parents 51d657d + 411afd7 commit 4a2a781
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 1 deletion.
20 changes: 19 additions & 1 deletion core/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,9 @@ class Parser {
case Token::IDENTIFIER:
case Token::STRING_DOUBLE:
case Token::STRING_SINGLE:
case Token::STRING_BLOCK: {
case Token::STRING_BLOCK:
case Token::VERBATIM_STRING_DOUBLE:
case Token::VERBATIM_STRING_SINGLE: {
ObjectField::Kind kind;
AST *expr1 = nullptr;
const Identifier *id = nullptr;
Expand Down Expand Up @@ -353,6 +355,22 @@ class Parser {
LiteralString::BLOCK,
next.stringBlockIndent,
next.stringBlockTermIndent);
} else if (next.kind == Token::VERBATIM_STRING_SINGLE) {
kind = ObjectField::FIELD_STR;
expr1 = alloc->make<LiteralString>(next.location,
next.fodder,
next.data32(),
LiteralString::VERBATIM_SINGLE,
"",
"");
} else if (next.kind == Token::VERBATIM_STRING_DOUBLE) {
kind = ObjectField::FIELD_STR;
expr1 = alloc->make<LiteralString>(next.location,
next.fodder,
next.data32(),
LiteralString::VERBATIM_DOUBLE,
"",
"");
} else {
kind = ObjectField::FIELD_EXPR;
fodder1 = next.fodder;
Expand Down
22 changes: 22 additions & 0 deletions test_suite/error.verbatim_import.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
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.
*/


// The only way to verify that verbatim strings are actually verbatim is to put a \ in them.
// However, we can't make that work on all platforms. However by importing a file that does
// not exist, we get the same behavior everywhere and can check the correct filename was
// attemted in the error message.
import @'C:\can''t possibly exist~'
2 changes: 2 additions & 0 deletions test_suite/error.verbatim_import.jsonnet.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
RUNTIME ERROR: Couldn't open import "C:\can't possibly exist~": No match locally or in the Jsonnet library paths.
error.verbatim_import.jsonnet:22:1-35
2 changes: 2 additions & 0 deletions test_suite/refresh_fmt_golden.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

set -e

source "tests.source"

if [ $# -eq 0 ] ; then
echo "Usage: $0 <filename.jsonnet>" 2>&1
exit 1
Expand Down
2 changes: 2 additions & 0 deletions test_suite/refresh_golden.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

set -e

source "tests.source"

if [ $# -eq 0 ] ; then
echo "Usage: $0 <filename.jsonnet>" 2>&1
exit 1
Expand Down
24 changes: 24 additions & 0 deletions test_suite/verbatim_strings.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
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.
*/


std.assertEqual(@"c:\negative""", 'c:\\negative"') &&
std.assertEqual(@'c:\negative''', "c:\\negative'") &&

std.assertEqual({ @"c:\negative""": 1 }, { 'c:\\negative"': 1 }) &&
std.assertEqual({ @'c:\negative''': 1 }, { "c:\\negative'": 1 }) &&

true

0 comments on commit 4a2a781

Please sign in to comment.