forked from carbon-language/carbon-lang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lex_scan_helper.cpp
68 lines (59 loc) · 2.23 KB
/
lex_scan_helper.cpp
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
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "explorer/syntax/lex_scan_helper.h"
#include "common/string_helpers.h"
#include "explorer/syntax/lex_helper.h"
#include "llvm/Support/FormatVariadic.h"
namespace Carbon {
auto StringLexHelper::Advance() -> bool {
CARBON_CHECK(is_eof_ == false);
const char c = YyinputWrapper(yyscanner_);
if (c <= 0) {
context_.RecordSyntaxError("Unexpected end of file");
is_eof_ = true;
return false;
}
str_.push_back(c);
return true;
}
auto ReadHashTags(Carbon::StringLexHelper& scan_helper,
const size_t hashtag_num) -> bool {
for (size_t i = 0; i < hashtag_num; ++i) {
if (!scan_helper.Advance() || scan_helper.last_char() != '#') {
return false;
}
}
return true;
}
auto ProcessSingleLineString(llvm::StringRef str,
Carbon::ParseAndLexContext& context,
const size_t hashtag_num)
-> Carbon::Parser::symbol_type {
std::string hashtags(hashtag_num, '#');
const auto str_with_quote = str;
CARBON_CHECK(str.consume_front(hashtags + "\"") &&
str.consume_back("\"" + hashtags));
std::optional<std::string> unescaped =
Carbon::UnescapeStringLiteral(str, hashtag_num);
if (unescaped == std::nullopt) {
return context.RecordSyntaxError(
llvm::formatv("Invalid escaping in string: {0}", str_with_quote));
}
return CARBON_ARG_TOKEN(string_literal, *unescaped);
}
auto ProcessMultiLineString(llvm::StringRef str,
Carbon::ParseAndLexContext& context,
const size_t hashtag_num)
-> Carbon::Parser::symbol_type {
std::string hashtags(hashtag_num, '#');
CARBON_CHECK(str.consume_front(hashtags) && str.consume_back(hashtags));
Carbon::ErrorOr<std::string> block_string =
Carbon::ParseBlockStringLiteral(str, hashtag_num);
if (!block_string.ok()) {
return context.RecordSyntaxError(llvm::formatv(
"Invalid block string: {0}", block_string.error().message()));
}
return CARBON_ARG_TOKEN(string_literal, *block_string);
}
} // namespace Carbon