| 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 |
#include "parser.h" |
| 18 |
|
| 19 |
#include <list> |
| 20 |
#include "ast.h" |
| 21 |
#include "gtest/gtest.h" |
| 22 |
#include "lexer.h" |
| 23 |
|
| 24 |
namespace jsonnet::internal { |
| 25 |
namespace { |
| 26 |
|
| 27 |
// Checks whether the provided snippet parses successfully. |
| 28 |
// TODO(dzc): Update this test to check the parsed AST against an expected AST. |
| 29 |
void testParse(const char* snippet) |
| 30 |
{ |
| 31 |
try { |
| 32 |
std::list<Token> tokens = jsonnet_lex("test", snippet); |
| 33 |
Allocator allocator; |
| 34 |
AST* ast = jsonnet_parse(&allocator, tokens); |
| 35 |
(void)ast; |
| 36 |
} catch (StaticError& e) { |
| 37 |
ASSERT_TRUE(false) << "Static error: " << e.toString() << std::endl |
| 38 |
<< "Snippet:" << std::endl |
| 39 |
<< snippet << std::endl; |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
TEST(Parser, TestLiterals) |
| 44 |
{ |
| 45 |
testParse("true"); |
| 46 |
testParse("1"); |
| 47 |
testParse("1.2e3"); |
| 48 |
testParse("!true"); |
| 49 |
testParse("null"); |
| 50 |
testParse(R"("world")"); |
| 51 |
testParse(R"("world")"); |
| 52 |
testParse("|||\n world\n|||"); |
| 53 |
} |
| 54 |
|
| 55 |
TEST(Parser, TestExpressions) |
| 56 |
{ |
| 57 |
testParse("$.foo.bar"); |
| 58 |
testParse("self.foo.bar"); |
| 59 |
testParse("super.foo.bar"); |
| 60 |
testParse("super[1]"); |
| 61 |
testParse(R"(error "Error!")"); |
| 62 |
|
| 63 |
testParse("foo(bar)"); |
| 64 |
testParse("foo(bar) tailstrict"); |
| 65 |
testParse("foo.bar"); |
| 66 |
testParse("foo[bar]"); |
| 67 |
|
| 68 |
testParse("true || false"); |
| 69 |
testParse("0 && 1 || 0"); |
| 70 |
testParse("0 && (1 || 0)"); |
| 71 |
} |
| 72 |
|
| 73 |
TEST(Parser, TestLocal) |
| 74 |
{ |
| 75 |
testParse(R"(local foo = "bar"; foo)"); |
| 76 |
testParse("local foo(bar) = bar; foo(1)"); |
| 77 |
testParse(R"({ local foo = "bar", baz: 1})"); |
| 78 |
testParse("{ local foo(bar) = bar, baz: foo(1)}"); |
| 79 |
} |
| 80 |
|
| 81 |
TEST(Parser, TestArray) |
| 82 |
{ |
| 83 |
testParse("[]"); |
| 84 |
testParse("[a, b, c]"); |
| 85 |
testParse("[x for x in [1,2,3] ]"); |
| 86 |
testParse("[x for x in [1,2,3] if x <= 2]"); |
| 87 |
testParse("[x+y for x in [1,2,3] if x <= 2 for y in [4, 5, 6]]"); |
| 88 |
} |
| 89 |
|
| 90 |
TEST(Parser, TestTuple) |
| 91 |
{ |
| 92 |
testParse("{ foo(bar, baz): bar+baz }"); |
| 93 |
|
| 94 |
testParse(R"({ ["foo" + "bar"]: 3 })"); |
| 95 |
testParse(R"({ ["field" + x]: x for x in [1, 2, 3] })"); |
| 96 |
testParse(R"({ local y = x, ["field" + x]: x for x in [1, 2, 3] })"); |
| 97 |
testParse(R"({ ["field" + x]: x for x in [1, 2, 3] if x <= 2 })"); |
| 98 |
testParse(R"({ ["field" + x + y]:)" |
| 99 |
R"( x + y for x in [1, 2, 3] if x <= 2 for y in [4, 5, 6]})"); |
| 100 |
|
| 101 |
testParse("{}"); |
| 102 |
testParse(R"({ hello: "world" })"); |
| 103 |
testParse(R"({ hello +: "world" })"); |
| 104 |
testParse(R"({ |
| 105 |
hello: "world", |
| 106 |
"name":: joe, |
| 107 |
'mood'::: "happy", |
| 108 |
||| |
| 109 |
key type |
| 110 |
|||: "block", |
| 111 |
})"); |
| 112 |
|
| 113 |
testParse("assert true: 'woah!'; true"); |
| 114 |
testParse("{ assert true: 'woah!', foo: bar }"); |
| 115 |
|
| 116 |
testParse("if n > 1 then 'foos' else 'foo'"); |
| 117 |
|
| 118 |
testParse("local foo = function(x) x + 1; true"); |
| 119 |
|
| 120 |
testParse("import 'foo.jsonnet'"); |
| 121 |
testParse("importstr 'foo.text'"); |
| 122 |
testParse("importbin 'foo.text'"); |
| 123 |
|
| 124 |
testParse("{a: b} + {c: d}"); |
| 125 |
testParse("{a: b}{c: d}"); |
| 126 |
} |
| 127 |
|
| 128 |
void testParseError(const char* snippet, const std::string& expectedError) |
| 129 |
{ |
| 130 |
try { |
| 131 |
std::list<Token> tokens = jsonnet_lex("test", snippet); |
| 132 |
Allocator allocator; |
| 133 |
AST* ast = jsonnet_parse(&allocator, tokens); |
| 134 |
(void)ast; |
| 135 |
} catch (StaticError& e) { |
| 136 |
ASSERT_EQ(expectedError, e.toString()) << "Snippet:" << std::endl << snippet << std::endl; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
TEST(Parser, TestInvalidFunctionCall) |
| 141 |
{ |
| 142 |
testParseError("function(a, b c)", |
| 143 |
"test:1:15: expected a comma before next function parameter."); |
| 144 |
testParseError("function(a, 1)", "test:1:13: could not parse parameter here."); |
| 145 |
testParseError("a b", R"(test:1:3: did not expect: (IDENTIFIER, "b"))"); |
| 146 |
testParseError("foo(a, bar(a b))", |
| 147 |
"test:1:14: expected a comma before next function argument."); |
| 148 |
} |
| 149 |
|
| 150 |
TEST(Parser, TestInvalidLocal) |
| 151 |
{ |
| 152 |
testParseError("local", "test:1:6: expected token IDENTIFIER but got end of file"); |
| 153 |
testParseError("local foo = 1, foo = 2; true", "test:1:16-19: duplicate local var: foo"); |
| 154 |
testParseError("local foo(a b) = a; true", |
| 155 |
"test:1:13: expected a comma before next function parameter."); |
| 156 |
testParseError("local foo(a): a; true", "test:1:13: expected operator = but got :"); |
| 157 |
testParseError("local foo(a) = bar(a b); true", |
| 158 |
"test:1:22: expected a comma before next function argument."); |
| 159 |
testParseError("local foo: 1; true", "test:1:10: expected operator = but got :"); |
| 160 |
testParseError("local foo = bar(a b); true", |
| 161 |
"test:1:19: expected a comma before next function argument."); |
| 162 |
|
| 163 |
testParseError("local a = b ()", "test:1:15: expected , or ; but got end of file"); |
| 164 |
testParseError("local a = b; (a b)", |
| 165 |
R"_(test:1:17: expected token ")" but got (IDENTIFIER, "b"))_"); |
| 166 |
} |
| 167 |
|
| 168 |
TEST(Parser, TestInvalidTuple) |
| 169 |
{ |
| 170 |
testParseError("{a b}", |
| 171 |
R"(test:1:4: expected token OPERATOR but got (IDENTIFIER, "b"))"); |
| 172 |
testParseError("{a = b}", "test:1:2: expected one of :, ::, :::, +:, +::, +:::, got: ="); |
| 173 |
testParseError("{a :::: b}", "test:1:2: expected one of :, ::, :::, +:, +::, +:::, got: ::::"); |
| 174 |
} |
| 175 |
|
| 176 |
TEST(Parser, TestInvalidComprehension) |
| 177 |
{ |
| 178 |
testParseError("{assert x for x in [1, 2, 3]}", |
| 179 |
"test:1:11-14: object comprehension cannot have asserts."); |
| 180 |
testParseError("{['foo' + x]: true, [x]: x for x in [1, 2, 3]}", |
| 181 |
"test:1:28-31: object comprehension can only have one field."); |
| 182 |
testParseError("{foo: x for x in [1, 2, 3]}", |
| 183 |
"test:1:9-12: object comprehensions can only have [e] fields."); |
| 184 |
testParseError("{[x]:: true for x in [1, 2, 3]}", |
| 185 |
"test:1:13-16: object comprehensions cannot have hidden fields."); |
| 186 |
testParseError("{[x]: true for 1 in [1, 2, 3]}", |
| 187 |
R"(test:1:16: expected token IDENTIFIER but got (NUMBER, "1"))"); |
| 188 |
testParseError("{[x]: true for x at [1, 2, 3]}", |
| 189 |
R"(test:1:18-20: expected token in but got (IDENTIFIER, "at"))"); |
| 190 |
testParseError("{[x]: true for x in [1, 2 3]}", |
| 191 |
"test:1:27: expected a comma before next array element."); |
| 192 |
testParseError("{[x]: true for x in [1, 2, 3] if (a b)}", |
| 193 |
R"_(test:1:37: expected token ")" but got (IDENTIFIER, "b"))_"); |
| 194 |
testParseError("{[x]: true for x in [1, 2, 3] if a b}", |
| 195 |
R"(test:1:36: expected for, if or "}" after for clause,)" |
| 196 |
R"( got: (IDENTIFIER, "b"))"); |
| 197 |
} |
| 198 |
|
| 199 |
TEST(Parser, TestInvalidNoComma) |
| 200 |
{ |
| 201 |
testParseError("{a: b c:d}", "test:1:7: expected a comma before next field."); |
| 202 |
} |
| 203 |
|
| 204 |
TEST(Parser, TestInvalidArrayKey) |
| 205 |
{ |
| 206 |
testParseError("{[(x y)]: z}", R"_(test:1:6: expected token ")" but got (IDENTIFIER, "y"))_"); |
| 207 |
testParseError("{[x y]: z}", R"(test:1:5: expected token "]" but got (IDENTIFIER, "y"))"); |
| 208 |
} |
| 209 |
|
| 210 |
TEST(Parser, TestInvalidFields) |
| 211 |
{ |
| 212 |
testParseError("{foo(x y): z}", "test:1:8: expected a comma before next method parameter."); |
| 213 |
testParseError("{foo(x)+: z}", "test:1:2-5: cannot use +: syntax sugar in a method: foo"); |
| 214 |
testParseError("{foo: 1, foo: 2}", "test:1:10-13: duplicate field: foo"); |
| 215 |
testParseError("{foo: (1 2)}", R"_(test:1:10: expected token ")" but got (NUMBER, "2"))_"); |
| 216 |
} |
| 217 |
|
| 218 |
TEST(Parser, TestInvalidLocalInTuple) |
| 219 |
{ |
| 220 |
testParseError("{local 1 = 3, true}", |
| 221 |
R"(test:1:8: expected token IDENTIFIER but got (NUMBER, "1"))"); |
| 222 |
testParseError("{local foo = 1, local foo = 2, true}", |
| 223 |
"test:1:23-26: duplicate local var: foo"); |
| 224 |
testParseError("{local foo(a b) = 1, a: true}", |
| 225 |
"test:1:14: expected a comma before next function parameter."); |
| 226 |
testParseError("{local foo(a): 1, a: true}", "test:1:14: expected operator = but got :"); |
| 227 |
testParseError("{local foo(a) = (a b), a: true}", |
| 228 |
R"_(test:1:20: expected token ")" but got (IDENTIFIER, "b"))_"); |
| 229 |
} |
| 230 |
|
| 231 |
TEST(Parser, TestInvalidAssertInTuple) |
| 232 |
{ |
| 233 |
testParseError("{assert (a b), a: true}", |
| 234 |
R"_(test:1:12: expected token ")" but got (IDENTIFIER, "b"))_"); |
| 235 |
testParseError("{assert a: (a b), a: true}", |
| 236 |
R"_(test:1:15: expected token ")" but got (IDENTIFIER, "b"))_"); |
| 237 |
} |
| 238 |
|
| 239 |
TEST(Parser, TestInvalidUnexpectedFunction) |
| 240 |
{ |
| 241 |
// TODO(jsonnet-team): The following error output differs from the Go |
| 242 |
// implementation, which is: |
| 243 |
// test:1:2-10 Unexpected: (function, "function") while parsing field |
| 244 |
// definition. |
| 245 |
testParseError("{function(a, b) a+b: true}", |
| 246 |
"test:1:2-10: unexpected: function while parsing field definition"); |
| 247 |
} |
| 248 |
|
| 249 |
TEST(Parser, TestInvalidArray) |
| 250 |
{ |
| 251 |
testParseError("[(a b), 2, 3]", |
| 252 |
R"_(test:1:5: expected token ")" but got (IDENTIFIER, "b"))_"); |
| 253 |
testParseError("[1, (a b), 2, 3]", |
| 254 |
R"_(test:1:8: expected token ")" but got (IDENTIFIER, "b"))_"); |
| 255 |
testParseError("[a for b in [1 2 3]]", |
| 256 |
"test:1:16: expected a comma before next array element."); |
| 257 |
} |
| 258 |
|
| 259 |
TEST(Parser, TestInvalidExpression) |
| 260 |
{ |
| 261 |
// TODO(jsonnet-team): The error output of the following differs from the Go |
| 262 |
// implementation, which is: |
| 263 |
// test:1:1-4 Unexpected: (for, "for") while parsing terminal) |
| 264 |
testParseError("for", "test:1:1-4: unexpected: for while parsing terminal"); |
| 265 |
testParseError("", "test:1:1: unexpected end of file."); |
| 266 |
testParseError("((a b))", R"_(test:1:5: expected token ")" but got (IDENTIFIER, "b"))_"); |
| 267 |
testParseError("a.1", R"(test:1:3: expected token IDENTIFIER but got (NUMBER, "1"))"); |
| 268 |
testParseError("super.1", R"(test:1:7: expected token IDENTIFIER but got (NUMBER, "1"))"); |
| 269 |
testParseError("super[(a b)]", R"_(test:1:10: expected token ")" but got (IDENTIFIER, "b"))_"); |
| 270 |
testParseError("super[a b]", R"(test:1:9: expected token "]" but got (IDENTIFIER, "b"))"); |
| 271 |
testParseError("super", "test:1:1-6: expected . or [ after super."); |
| 272 |
} |
| 273 |
|
| 274 |
TEST(Parser, TestInvalidAssert) |
| 275 |
{ |
| 276 |
testParseError("assert (a b); true", |
| 277 |
R"_(test:1:11: expected token ")" but got (IDENTIFIER, "b"))_"); |
| 278 |
testParseError("assert a: (a b); true", |
| 279 |
R"_(test:1:14: expected token ")" but got (IDENTIFIER, "b"))_"); |
| 280 |
// TODO(jsonnet-team): The error output of this differs from the Go |
| 281 |
// implementation, which is: |
| 282 |
// test:1:16: expected token ";" but got (",", ",") |
| 283 |
testParseError("assert a: 'foo', true", |
| 284 |
R"(test:1:16: expected token ";" but got ",")"); |
| 285 |
testParseError("assert a: 'foo'; (a b)", |
| 286 |
R"_(test:1:21: expected token ")" but got (IDENTIFIER, "b"))_"); |
| 287 |
} |
| 288 |
|
| 289 |
TEST(Parser, TestInvalidError) |
| 290 |
{ |
| 291 |
testParseError("error (a b)", R"_(test:1:10: expected token ")" but got (IDENTIFIER, "b"))_"); |
| 292 |
} |
| 293 |
|
| 294 |
TEST(Parser, TestInvalidIf) |
| 295 |
{ |
| 296 |
testParseError("if (a b) then c", |
| 297 |
R"_(test:1:7: expected token ")" but got (IDENTIFIER, "b"))_"); |
| 298 |
testParseError("if a b c", |
| 299 |
R"(test:1:6: expected token then but got (IDENTIFIER, "b"))"); |
| 300 |
testParseError("if a then (b c)", |
| 301 |
R"_(test:1:14: expected token ")" but got (IDENTIFIER, "c"))_"); |
| 302 |
testParseError("if a then b else (c d)", |
| 303 |
R"_(test:1:21: expected token ")" but got (IDENTIFIER, "d"))_"); |
| 304 |
} |
| 305 |
|
| 306 |
TEST(Parser, TestInvalidFunction) |
| 307 |
{ |
| 308 |
testParseError("function(a) (a b)", |
| 309 |
R"_(test:1:16: expected token ")" but got (IDENTIFIER, "b"))_"); |
| 310 |
testParseError("function a a", R"(test:1:10: expected ( but got (IDENTIFIER, "a"))"); |
| 311 |
} |
| 312 |
|
| 313 |
TEST(Parser, TestInvalidImport) |
| 314 |
{ |
| 315 |
testParseError("import (a b)", R"_(test:1:11: expected token ")" but got (IDENTIFIER, "b"))_"); |
| 316 |
testParseError("import (a+b)", "test:1:8-13: computed imports are not allowed."); |
| 317 |
testParseError("importstr (a b)", |
| 318 |
R"_(test:1:14: expected token ")" but got (IDENTIFIER, "b"))_"); |
| 319 |
testParseError("importstr (a+b)", "test:1:11-16: computed imports are not allowed."); |
| 320 |
testParseError("importbin (a b)", |
| 321 |
R"_(test:1:14: expected token ")" but got (IDENTIFIER, "b"))_"); |
| 322 |
testParseError("importbin (a+b)", "test:1:11-16: computed imports are not allowed."); |
| 323 |
} |
| 324 |
|
| 325 |
TEST(Parser, TestInvalidOperator) |
| 326 |
{ |
| 327 |
testParseError("1+ <<", "test:1:4-6: not a unary operator: <<"); |
| 328 |
testParseError("-(a b)", R"_(test:1:5: expected token ")" but got (IDENTIFIER, "b"))_"); |
| 329 |
testParseError("1~2", "test:1:2: not a binary operator: ~"); |
| 330 |
} |
| 331 |
|
| 332 |
TEST(Parser, TestInvalidArrayAccess) |
| 333 |
{ |
| 334 |
testParseError("a[(b c)]", R"_(test:1:6: expected token ")" but got (IDENTIFIER, "c"))_"); |
| 335 |
// TODO(jsonnet-team): The error output of this differs from the Go |
| 336 |
// implementation, which is: |
| 337 |
// test:1:5: expected token "]" but got (IDENTIFIER, "c") |
| 338 |
testParseError("a[b c]", "test:1:5: unexpected: IDENTIFIER while parsing slice"); |
| 339 |
} |
| 340 |
|
| 341 |
TEST(Parser, TestInvalidOverride) |
| 342 |
{ |
| 343 |
testParseError("a{b c}", R"(test:1:5: expected token OPERATOR but got (IDENTIFIER, "c"))"); |
| 344 |
} |
| 345 |
|
| 346 |
} // namespace |
| 347 |
} // namespace jsonnet::internal |