1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
|
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <iostream>
#include <sstream>
#include "testing/gtest/include/gtest/gtest.h"
#include "tools/gn/input_file.h"
#include "tools/gn/parser.h"
#include "tools/gn/tokenizer.h"
namespace {
bool GetTokens(const InputFile* input, std::vector<Token>* result) {
result->clear();
Err err;
*result = Tokenizer::Tokenize(input, &err);
return !err.has_error();
}
bool IsIdentifierEqual(const ParseNode* node, const char* val) {
if (!node)
return false;
const IdentifierNode* ident = node->AsIdentifier();
if (!ident)
return false;
return ident->value().value() == val;
}
bool IsLiteralEqual(const ParseNode* node, const char* val) {
if (!node)
return false;
const LiteralNode* lit = node->AsLiteral();
if (!lit)
return false;
return lit->value().value() == val;
}
// Returns true if the given node as a simple assignment to a given value.
bool IsAssignment(const ParseNode* node, const char* ident, const char* value) {
if (!node)
return false;
const BinaryOpNode* binary = node->AsBinaryOp();
if (!binary)
return false;
return binary->op().IsOperatorEqualTo("=") &&
IsIdentifierEqual(binary->left(), ident) &&
IsLiteralEqual(binary->right(), value);
}
// Returns true if the given node is a block with one assignment statement.
bool IsBlockWithAssignment(const ParseNode* node,
const char* ident, const char* value) {
if (!node)
return false;
const BlockNode* block = node->AsBlock();
if (!block)
return false;
if (block->statements().size() != 1)
return false;
return IsAssignment(block->statements()[0], ident, value);
}
void DoParserPrintTest(const char* input, const char* expected) {
std::vector<Token> tokens;
InputFile input_file(SourceFile("/test"));
input_file.SetContents(input);
ASSERT_TRUE(GetTokens(&input_file, &tokens));
Err err;
scoped_ptr<ParseNode> result = Parser::Parse(tokens, &err);
ASSERT_TRUE(result);
std::ostringstream collector;
result->Print(collector, 0);
EXPECT_EQ(expected, collector.str());
}
// Expects the tokenizer or parser to identify an error at the given line and
// character.
void DoParserErrorTest(const char* input, int err_line, int err_char) {
InputFile input_file(SourceFile("/test"));
input_file.SetContents(input);
Err err;
std::vector<Token> tokens = Tokenizer::Tokenize(&input_file, &err);
if (!err.has_error()) {
scoped_ptr<ParseNode> result = Parser::Parse(tokens, &err);
ASSERT_FALSE(result);
ASSERT_TRUE(err.has_error());
}
EXPECT_EQ(err_line, err.location().line_number());
EXPECT_EQ(err_char, err.location().char_offset());
}
} // namespace
TEST(Parser, BinaryOp) {
std::vector<Token> tokens;
// Simple set expression.
InputFile expr_input(SourceFile("/test"));
expr_input.SetContents("a=2");
ASSERT_TRUE(GetTokens(&expr_input, &tokens));
Err err;
Parser set(tokens, &err);
scoped_ptr<ParseNode> expr = set.ParseExpression();
ASSERT_TRUE(expr);
const BinaryOpNode* binary_op = expr->AsBinaryOp();
ASSERT_TRUE(binary_op);
EXPECT_TRUE(binary_op->left()->AsIdentifier());
EXPECT_TRUE(binary_op->op().type() == Token::OPERATOR);
EXPECT_TRUE(binary_op->op().value() == "=");
EXPECT_TRUE(binary_op->right()->AsLiteral());
}
TEST(Parser, Condition) {
std::vector<Token> tokens;
InputFile cond_input(SourceFile("/test"));
cond_input.SetContents("if(1) { a = 2 }");
ASSERT_TRUE(GetTokens(&cond_input, &tokens));
Err err;
Parser simple_if(tokens, &err);
scoped_ptr<ConditionNode> cond = simple_if.ParseCondition();
ASSERT_TRUE(cond);
EXPECT_TRUE(IsLiteralEqual(cond->condition(), "1"));
EXPECT_FALSE(cond->if_false()); // No else block.
EXPECT_TRUE(IsBlockWithAssignment(cond->if_true(), "a", "2"));
// Now try a complicated if/else if/else one.
InputFile complex_if_input(SourceFile("/test"));
complex_if_input.SetContents(
"if(1) { a = 2 } else if (0) { a = 3 } else { a = 4 }");
ASSERT_TRUE(GetTokens(&complex_if_input, &tokens));
Parser complex_if(tokens, &err);
cond = complex_if.ParseCondition();
ASSERT_TRUE(cond);
EXPECT_TRUE(IsLiteralEqual(cond->condition(), "1"));
EXPECT_TRUE(IsBlockWithAssignment(cond->if_true(), "a", "2"));
ASSERT_TRUE(cond->if_false());
const ConditionNode* nested_cond = cond->if_false()->AsConditionNode();
ASSERT_TRUE(nested_cond);
EXPECT_TRUE(IsLiteralEqual(nested_cond->condition(), "0"));
EXPECT_TRUE(IsBlockWithAssignment(nested_cond->if_true(), "a", "3"));
EXPECT_TRUE(IsBlockWithAssignment(nested_cond->if_false(), "a", "4"));
}
TEST(Parser, FunctionCall) {
const char* input = "foo(a, 1, 2,) bar()";
const char* expected =
"BLOCK\n"
" FUNCTION(foo)\n"
" LIST\n"
" IDENTIFIER(a)\n"
" LITERAL(1)\n"
" LITERAL(2)\n"
" FUNCTION(bar)\n"
" LIST\n";
DoParserPrintTest(input, expected);
}
TEST(Parser, ParenExpression) {
const char* input = "(foo(1)) + (a + b)";
const char* expected =
"BLOCK\n"
" BINARY(+)\n"
" FUNCTION(foo)\n"
" LIST\n"
" LITERAL(1)\n"
" BINARY(+)\n"
" IDENTIFIER(a)\n"
" IDENTIFIER(b)\n";
DoParserPrintTest(input, expected);
DoParserErrorTest("(a +", 1, 4);
}
TEST(Parser, UnaryOp) {
std::vector<Token> tokens;
InputFile ident_input(SourceFile("/test"));
ident_input.SetContents("!foo");
ASSERT_TRUE(GetTokens(&ident_input, &tokens));
Err err;
Parser ident(tokens, &err);
scoped_ptr<UnaryOpNode> op = ident.ParseUnaryOp();
ASSERT_TRUE(op);
EXPECT_TRUE(op->op().type() == Token::OPERATOR);
EXPECT_TRUE(op->op().value() == "!");
}
TEST(Parser, CompleteFunction) {
const char* input =
"cc_test(\"foo\") {\n"
" sources = [\n"
" \"foo.cc\",\n"
" \"foo.h\"\n"
" ]\n"
" dependencies = [\n"
" \"base\"\n"
" ]\n"
"}\n";
const char* expected =
"BLOCK\n"
" FUNCTION(cc_test)\n"
" LIST\n"
" LITERAL(\"foo\")\n"
" BLOCK\n"
" BINARY(=)\n"
" IDENTIFIER(sources)\n"
" LIST\n"
" LITERAL(\"foo.cc\")\n"
" LITERAL(\"foo.h\")\n"
" BINARY(=)\n"
" IDENTIFIER(dependencies)\n"
" LIST\n"
" LITERAL(\"base\")\n";
DoParserPrintTest(input, expected);
}
TEST(Parser, FunctionWithConditional) {
const char* input =
"cc_test(\"foo\") {\n"
" sources = [\"foo.cc\"]\n"
" if (OS == \"mac\") {\n"
" sources += \"bar.cc\"\n"
" } else if (OS == \"win\") {\n"
" sources -= [\"asd.cc\", \"foo.cc\"]\n"
" } else {\n"
" dependencies += [\"bar.cc\"]\n"
" }\n"
"}\n";
const char* expected =
"BLOCK\n"
" FUNCTION(cc_test)\n"
" LIST\n"
" LITERAL(\"foo\")\n"
" BLOCK\n"
" BINARY(=)\n"
" IDENTIFIER(sources)\n"
" LIST\n"
" LITERAL(\"foo.cc\")\n"
" CONDITION\n"
" BINARY(==)\n"
" IDENTIFIER(OS)\n"
" LITERAL(\"mac\")\n"
" BLOCK\n"
" BINARY(+=)\n"
" IDENTIFIER(sources)\n"
" LITERAL(\"bar.cc\")\n"
" CONDITION\n"
" BINARY(==)\n"
" IDENTIFIER(OS)\n"
" LITERAL(\"win\")\n"
" BLOCK\n"
" BINARY(-=)\n"
" IDENTIFIER(sources)\n"
" LIST\n"
" LITERAL(\"asd.cc\")\n"
" LITERAL(\"foo.cc\")\n"
" BLOCK\n"
" BINARY(+=)\n"
" IDENTIFIER(dependencies)\n"
" LIST\n"
" LITERAL(\"bar.cc\")\n";
DoParserPrintTest(input, expected);
}
TEST(Parser, NestedBlocks) {
const char* input = "{cc_test(\"foo\") {{foo=1}{}}}";
const char* expected =
"BLOCK\n"
" BLOCK\n"
" FUNCTION(cc_test)\n"
" LIST\n"
" LITERAL(\"foo\")\n"
" BLOCK\n"
" BLOCK\n"
" BINARY(=)\n"
" IDENTIFIER(foo)\n"
" LITERAL(1)\n"
" BLOCK\n";
DoParserPrintTest(input, expected);
}
TEST(Parser, List) {
const char* input = "[] a = [1,asd,] b = [1, 2+3 - foo]";
const char* expected =
"BLOCK\n"
" LIST\n"
" BINARY(=)\n"
" IDENTIFIER(a)\n"
" LIST\n"
" LITERAL(1)\n"
" IDENTIFIER(asd)\n"
" BINARY(=)\n"
" IDENTIFIER(b)\n"
" LIST\n"
" LITERAL(1)\n"
" BINARY(+)\n"
" LITERAL(2)\n"
" BINARY(-)\n"
" LITERAL(3)\n"
" IDENTIFIER(foo)\n";
DoParserPrintTest(input, expected);
DoParserErrorTest("[a, 2+,]", 1, 7);
DoParserErrorTest("[,]", 1, 2);
DoParserErrorTest("[a,,]", 1, 4);
}
TEST(Parser, UnterminatedBlock) {
DoParserErrorTest("hello {", 1, 7);
}
TEST(Parser, BadlyTerminatedNumber) {
DoParserErrorTest("1234z", 1, 5);
}
|