2016-09-19 16:50:29 +00:00
|
|
|
// Copyright 2016 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
// Tests v8::internal::Scanner. Note that presently most unit tests for the
|
|
|
|
// Scanner are in cctest/test-parsing.cc, rather than here.
|
|
|
|
|
|
|
|
#include "src/handles-inl.h"
|
2017-01-09 13:43:28 +00:00
|
|
|
#include "src/objects-inl.h"
|
2016-09-19 16:50:29 +00:00
|
|
|
#include "src/parsing/scanner-character-streams.h"
|
|
|
|
#include "src/parsing/scanner.h"
|
|
|
|
#include "src/unicode-cache.h"
|
|
|
|
#include "test/cctest/cctest.h"
|
|
|
|
|
2017-08-11 11:22:28 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
2016-09-19 16:50:29 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
const char src_simple[] = "function foo() { var x = 2 * a() + b; }";
|
|
|
|
|
2016-11-24 14:28:53 +00:00
|
|
|
struct ScannerTestHelper {
|
|
|
|
ScannerTestHelper() = default;
|
|
|
|
ScannerTestHelper(ScannerTestHelper&& other)
|
|
|
|
: unicode_cache(std::move(other.unicode_cache)),
|
|
|
|
stream(std::move(other.stream)),
|
|
|
|
scanner(std::move(other.scanner)) {}
|
|
|
|
|
|
|
|
std::unique_ptr<UnicodeCache> unicode_cache;
|
|
|
|
std::unique_ptr<Utf16CharacterStream> stream;
|
|
|
|
std::unique_ptr<Scanner> scanner;
|
2017-09-29 19:52:15 +00:00
|
|
|
int use_counts[v8::Isolate::kUseCounterFeatureCount];
|
2016-11-24 14:28:53 +00:00
|
|
|
|
|
|
|
Scanner* operator->() const { return scanner.get(); }
|
|
|
|
Scanner* get() const { return scanner.get(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
ScannerTestHelper make_scanner(const char* src) {
|
|
|
|
ScannerTestHelper helper;
|
|
|
|
helper.unicode_cache = std::unique_ptr<UnicodeCache>(new UnicodeCache);
|
|
|
|
helper.stream = ScannerStream::ForTesting(src);
|
2017-09-29 19:52:15 +00:00
|
|
|
for (int i = 0; i < v8::Isolate::kUseCounterFeatureCount; i++) {
|
|
|
|
helper.use_counts[i] = 0;
|
|
|
|
}
|
|
|
|
helper.scanner = std::unique_ptr<Scanner>(
|
|
|
|
new Scanner(helper.unicode_cache.get(), helper.use_counts));
|
2017-05-04 16:37:54 +00:00
|
|
|
helper.scanner->Initialize(helper.stream.get(), false);
|
2016-11-24 14:28:53 +00:00
|
|
|
return helper;
|
2016-09-19 16:50:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2017-03-28 12:13:28 +00:00
|
|
|
// CHECK_TOK checks token equality, but by checking for equality of the token
|
2016-09-23 13:15:57 +00:00
|
|
|
// names. That should have the same result, but has much nicer error messaages.
|
2017-03-28 12:13:28 +00:00
|
|
|
#define CHECK_TOK(a, b) CHECK_EQ(Token::Name(a), Token::Name(b))
|
2016-09-23 13:15:57 +00:00
|
|
|
|
2016-09-19 16:50:29 +00:00
|
|
|
TEST(Bookmarks) {
|
|
|
|
// Scan through the given source and record the tokens for use as reference
|
|
|
|
// below.
|
|
|
|
std::vector<Token::Value> tokens;
|
|
|
|
{
|
2016-11-24 14:28:53 +00:00
|
|
|
auto scanner = make_scanner(src_simple);
|
2016-09-19 16:50:29 +00:00
|
|
|
do {
|
|
|
|
tokens.push_back(scanner->Next());
|
|
|
|
} while (scanner->current_token() != Token::EOS);
|
|
|
|
}
|
|
|
|
|
|
|
|
// For each position:
|
|
|
|
// - Scan through file,
|
|
|
|
// - set a bookmark once the position is reached,
|
|
|
|
// - scan a bit more,
|
|
|
|
// - reset to the bookmark, and
|
|
|
|
// - scan until the end.
|
|
|
|
// At each step, compare to the reference token sequence generated above.
|
|
|
|
for (size_t bookmark_pos = 0; bookmark_pos < tokens.size(); bookmark_pos++) {
|
2016-11-24 14:28:53 +00:00
|
|
|
auto scanner = make_scanner(src_simple);
|
2016-09-19 16:50:29 +00:00
|
|
|
Scanner::BookmarkScope bookmark(scanner.get());
|
|
|
|
|
|
|
|
for (size_t i = 0; i < std::min(bookmark_pos + 10, tokens.size()); i++) {
|
|
|
|
if (i == bookmark_pos) {
|
|
|
|
bookmark.Set();
|
|
|
|
}
|
2017-03-28 12:13:28 +00:00
|
|
|
CHECK_TOK(tokens[i], scanner->Next());
|
2016-09-19 16:50:29 +00:00
|
|
|
}
|
|
|
|
|
2016-09-20 13:47:31 +00:00
|
|
|
bookmark.Apply();
|
2016-09-19 16:50:29 +00:00
|
|
|
for (size_t i = bookmark_pos; i < tokens.size(); i++) {
|
2017-03-28 12:13:28 +00:00
|
|
|
CHECK_TOK(tokens[i], scanner->Next());
|
2016-09-23 13:15:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(AllThePushbacks) {
|
|
|
|
const struct {
|
|
|
|
const char* src;
|
|
|
|
const Token::Value tokens[5]; // Large enough for any of the test cases.
|
|
|
|
} test_cases[] = {
|
|
|
|
{"<-x", {Token::LT, Token::SUB, Token::IDENTIFIER, Token::EOS}},
|
|
|
|
{"<!x", {Token::LT, Token::NOT, Token::IDENTIFIER, Token::EOS}},
|
|
|
|
{"<!-x",
|
|
|
|
{Token::LT, Token::NOT, Token::SUB, Token::IDENTIFIER, Token::EOS}},
|
|
|
|
{"<!-- xx -->\nx", {Token::IDENTIFIER, Token::EOS}},
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const auto& test_case : test_cases) {
|
2016-11-24 14:28:53 +00:00
|
|
|
auto scanner = make_scanner(test_case.src);
|
2016-09-23 13:15:57 +00:00
|
|
|
for (size_t i = 0; test_case.tokens[i] != Token::EOS; i++) {
|
2017-03-28 12:13:28 +00:00
|
|
|
CHECK_TOK(test_case.tokens[i], scanner->Next());
|
2016-09-19 16:50:29 +00:00
|
|
|
}
|
2017-03-28 12:13:28 +00:00
|
|
|
CHECK_TOK(Token::EOS, scanner->Next());
|
2016-09-19 16:50:29 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-28 12:13:28 +00:00
|
|
|
|
|
|
|
TEST(ContextualKeywordTokens) {
|
|
|
|
auto scanner = make_scanner("function of get bla");
|
|
|
|
|
|
|
|
// function (regular keyword)
|
|
|
|
scanner->Next();
|
|
|
|
CHECK_TOK(Token::FUNCTION, scanner->current_token());
|
|
|
|
CHECK_TOK(Token::UNINITIALIZED, scanner->current_contextual_token());
|
|
|
|
|
|
|
|
// of (contextual keyword)
|
|
|
|
scanner->Next();
|
|
|
|
CHECK_TOK(Token::IDENTIFIER, scanner->current_token());
|
|
|
|
CHECK_TOK(Token::OF, scanner->current_contextual_token());
|
|
|
|
|
|
|
|
// get (contextual keyword)
|
|
|
|
scanner->Next();
|
|
|
|
CHECK_TOK(Token::IDENTIFIER, scanner->current_token());
|
|
|
|
CHECK_TOK(Token::GET, scanner->current_contextual_token());
|
|
|
|
|
|
|
|
// bla (identfier, not any sort of keyword)
|
|
|
|
scanner->Next();
|
|
|
|
CHECK_TOK(Token::IDENTIFIER, scanner->current_token());
|
|
|
|
CHECK_TOK(Token::UNINITIALIZED, scanner->current_contextual_token());
|
|
|
|
}
|
2017-08-11 11:22:28 +00:00
|
|
|
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|