Skip to content

Commit 32168e6

Browse files
committed
Finish implementation of single quote string literals, fix #72
1 parent 03a7834 commit 32168e6

7 files changed

+41
-3
lines changed

core/desugaring.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ static String desugar_string(const LocationRange &loc, const String &s)
7272
case '\\':
7373
switch (*(++c)) {
7474
case '"':
75+
case '\'':
7576
r += *c;
7677
break;
7778

core/parser.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,8 @@ namespace {
709709
first = false;
710710

711711
switch (next.kind) {
712-
case Token::BRACKET_L: case Token::IDENTIFIER: case Token::STRING_DOUBLE: {
712+
case Token::BRACKET_L: case Token::IDENTIFIER: case Token::STRING_DOUBLE:
713+
case Token::STRING_SINGLE: case Token::STRING_BLOCK: {
713714
ObjectField::Kind kind;
714715
AST *expr1 = nullptr;
715716
const Identifier *id = nullptr;
@@ -720,6 +721,14 @@ namespace {
720721
kind = ObjectField::FIELD_STR;
721722
expr1 = alloc->make<LiteralString>(
722723
next.location, next.data32(), LiteralString::DOUBLE, "");
724+
} else if (next.kind == Token::STRING_SINGLE) {
725+
kind = ObjectField::FIELD_STR;
726+
expr1 = alloc->make<LiteralString>(
727+
next.location, next.data32(), LiteralString::SINGLE, "");
728+
} else if (next.kind == Token::STRING_BLOCK) {
729+
kind = ObjectField::FIELD_STR;
730+
expr1 = alloc->make<LiteralString>(
731+
next.location, next.data32(), LiteralString::BLOCK, "");
723732
} else {
724733
kind = ObjectField::FIELD_EXPR;
725734
expr1 = parse(MAX_PRECEDENCE);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
Copyright 2015 Google Inc. All rights reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
STATIC ERROR: error.parse.string.unfinished2.jsonnet:17:1: Unterminated string

test_suite/import.jsonnet

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

20+
# Test single quoted string.
21+
std.assertEqual((import 'lib/A_20_func.jsonnet')(), 20) &&
22+
# The block string is hard to test because the filename would include a terminating \n
23+
2024
// Each import has its own environment, can't be overidden.
2125
std.assertEqual(local A = 7; local lib = import "lib/A_20.jsonnet"; lib, 20) &&
2226
std.assertEqual(local A = 7, lib = import "lib/A_20.jsonnet"; lib, 20) &&

test_suite/unparse.jsonnet

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ limitations under the License.
2020
number: 1/3,
2121
small_number: 0.00000000000001,
2222
zero: 0,
23-
string: "foo\n bar\n\n\"bar\u0005\"\t \u0050\b\f\r\\",
23+
string: "'foo\n bar\n\n\"bar\u0005\"\'\t \u0050\b\f\r\\",
24+
string2: '"foo\n bar\n\n\'bar\u0005\"\'\t \u0050\b\f\r\\',
25+
"lit_field1": 1,
26+
'lit_field2': 1,
2427
"false": false,
2528
"true": true,
2629
"null": null,

test_suite/unparse.jsonnet.golden

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
{
22
"false": false,
3+
"lit_field1": 1,
4+
"lit_field2": 1,
35
"neg_integer": -1029301293,
46
"null": null,
57
"number": 0.33333333333333331,
68
"pos_integer": 13212381932,
79
"small_number": 1e-14,
8-
"string": "foo\n bar\n\n\"bar\u0005\"\t P\b\f\r\\",
10+
"string": "'foo\n bar\n\n\"bar\u0005\"'\t P\b\f\r\\",
11+
"string2": "\"foo\n bar\n\n'bar\u0005\"'\t P\b\f\r\\",
912
"true": true,
1013
"zero": 0
1114
}

0 commit comments

Comments
 (0)