2020-02-25 23:11:52 +08:00
|
|
|
//===-- lib/Parser/characters.cpp -----------------------------------------===//
|
2018-05-02 03:50:34 +08:00
|
|
|
//
|
2019-12-21 04:52:07 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-05-02 03:50:34 +08:00
|
|
|
//
|
2020-01-11 04:12:03 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2018-05-02 03:50:34 +08:00
|
|
|
|
2020-02-25 23:11:52 +08:00
|
|
|
#include "flang/Parser/characters.h"
|
|
|
|
#include "flang/Common/idioms.h"
|
2019-06-18 01:35:34 +08:00
|
|
|
#include <algorithm>
|
2018-03-21 01:59:07 +08:00
|
|
|
#include <cstddef>
|
|
|
|
#include <optional>
|
2019-06-14 05:16:04 +08:00
|
|
|
#include <type_traits>
|
2018-03-01 08:56:10 +08:00
|
|
|
|
2018-05-03 04:48:12 +08:00
|
|
|
namespace Fortran::parser {
|
2018-03-01 08:56:10 +08:00
|
|
|
|
2020-01-04 05:15:18 +08:00
|
|
|
bool useHexadecimalEscapeSequences{false};
|
|
|
|
|
2019-06-15 06:44:32 +08:00
|
|
|
int UTF_8CharacterBytes(const char *p) {
|
2018-03-01 08:56:10 +08:00
|
|
|
if ((*p & 0x80) == 0) {
|
2019-06-12 01:34:58 +08:00
|
|
|
return 1;
|
2018-03-01 08:56:10 +08:00
|
|
|
} else if ((*p & 0xe0) == 0xc0) {
|
2019-06-15 06:44:32 +08:00
|
|
|
return 2;
|
|
|
|
} else if ((*p & 0xf0) == 0xe0) {
|
|
|
|
return 3;
|
|
|
|
} else if ((*p & 0xf8) == 0xf0) {
|
|
|
|
return 4;
|
|
|
|
} else if ((*p & 0xfc) == 0xf8) {
|
|
|
|
return 5;
|
2019-06-12 01:34:58 +08:00
|
|
|
} else {
|
2019-06-15 06:44:32 +08:00
|
|
|
return 6;
|
2019-06-12 01:34:58 +08:00
|
|
|
}
|
2018-03-01 08:56:10 +08:00
|
|
|
}
|
2018-05-03 04:48:12 +08:00
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <typename STRING>
|
2019-06-14 05:16:04 +08:00
|
|
|
std::string QuoteCharacterLiteralHelper(
|
|
|
|
const STRING &str, bool backslashEscapes, Encoding encoding) {
|
2018-07-07 06:12:33 +08:00
|
|
|
std::string result{'"'};
|
2018-07-11 08:09:07 +08:00
|
|
|
const auto emit{[&](char ch) { result += ch; }};
|
2018-11-02 02:18:12 +08:00
|
|
|
for (auto ch : str) {
|
2019-05-28 17:15:05 +08:00
|
|
|
using CharT = std::decay_t<decltype(ch)>;
|
2019-06-14 05:16:04 +08:00
|
|
|
char32_t ch32{static_cast<std::make_unsigned_t<CharT>>(ch)};
|
|
|
|
if (ch32 == static_cast<unsigned char>('"')) {
|
2020-03-29 12:00:16 +08:00
|
|
|
emit('"'); // double the " when it appears in the text
|
2019-05-28 17:15:05 +08:00
|
|
|
}
|
2019-06-14 05:16:04 +08:00
|
|
|
EmitQuotedChar(ch32, emit, emit, backslashEscapes, encoding);
|
2018-07-07 06:12:33 +08:00
|
|
|
}
|
|
|
|
result += '"';
|
|
|
|
return result;
|
|
|
|
}
|
2018-11-02 02:18:12 +08:00
|
|
|
|
2019-06-14 05:16:04 +08:00
|
|
|
std::string QuoteCharacterLiteral(
|
|
|
|
const std::string &str, bool backslashEscapes, Encoding encoding) {
|
|
|
|
return QuoteCharacterLiteralHelper(str, backslashEscapes, encoding);
|
2019-06-12 01:34:58 +08:00
|
|
|
}
|
|
|
|
|
2019-06-14 05:16:04 +08:00
|
|
|
std::string QuoteCharacterLiteral(
|
|
|
|
const std::u16string &str, bool backslashEscapes, Encoding encoding) {
|
|
|
|
return QuoteCharacterLiteralHelper(str, backslashEscapes, encoding);
|
2019-06-12 01:34:58 +08:00
|
|
|
}
|
|
|
|
|
2019-06-14 05:16:04 +08:00
|
|
|
std::string QuoteCharacterLiteral(
|
|
|
|
const std::u32string &str, bool backslashEscapes, Encoding encoding) {
|
|
|
|
return QuoteCharacterLiteralHelper(str, backslashEscapes, encoding);
|
2019-06-12 01:34:58 +08:00
|
|
|
}
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <> EncodedCharacter EncodeCharacter<Encoding::LATIN_1>(char32_t ucs) {
|
2019-06-15 06:44:32 +08:00
|
|
|
CHECK(ucs <= 0xff);
|
2019-06-12 01:34:58 +08:00
|
|
|
EncodedCharacter result;
|
2019-06-15 06:44:32 +08:00
|
|
|
result.buffer[0] = ucs;
|
2019-06-12 01:34:58 +08:00
|
|
|
result.bytes = 1;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <> EncodedCharacter EncodeCharacter<Encoding::UTF_8>(char32_t ucs) {
|
2019-06-12 01:34:58 +08:00
|
|
|
// N.B. char32_t is unsigned
|
|
|
|
EncodedCharacter result;
|
2019-06-19 03:34:23 +08:00
|
|
|
if (ucs <= 0x7f) {
|
|
|
|
result.buffer[0] = ucs;
|
2019-06-12 01:34:58 +08:00
|
|
|
result.bytes = 1;
|
2019-06-19 03:34:23 +08:00
|
|
|
} else if (ucs <= 0x7ff) {
|
|
|
|
result.buffer[0] = 0xc0 | (ucs >> 6);
|
|
|
|
result.buffer[1] = 0x80 | (ucs & 0x3f);
|
2019-06-12 01:34:58 +08:00
|
|
|
result.bytes = 2;
|
2019-06-19 03:34:23 +08:00
|
|
|
} else if (ucs <= 0xffff) {
|
|
|
|
result.buffer[0] = 0xe0 | (ucs >> 12);
|
|
|
|
result.buffer[1] = 0x80 | ((ucs >> 6) & 0x3f);
|
|
|
|
result.buffer[2] = 0x80 | (ucs & 0x3f);
|
2019-06-12 01:34:58 +08:00
|
|
|
result.bytes = 3;
|
2019-06-19 03:34:23 +08:00
|
|
|
} else if (ucs <= 0x1fffff) {
|
2019-06-15 06:44:32 +08:00
|
|
|
// UCS actually only goes up to 0x10ffff, but the
|
|
|
|
// UTF-8 encoding can handle 32 bits.
|
2019-06-19 03:34:23 +08:00
|
|
|
result.buffer[0] = 0xf0 | (ucs >> 18);
|
|
|
|
result.buffer[1] = 0x80 | ((ucs >> 12) & 0x3f);
|
|
|
|
result.buffer[2] = 0x80 | ((ucs >> 6) & 0x3f);
|
|
|
|
result.buffer[3] = 0x80 | (ucs & 0x3f);
|
2019-06-12 01:34:58 +08:00
|
|
|
result.bytes = 4;
|
2019-06-19 03:34:23 +08:00
|
|
|
} else if (ucs <= 0x3ffffff) {
|
|
|
|
result.buffer[0] = 0xf8 | (ucs >> 24);
|
|
|
|
result.buffer[1] = 0x80 | ((ucs >> 18) & 0x3f);
|
|
|
|
result.buffer[2] = 0x80 | ((ucs >> 12) & 0x3f);
|
|
|
|
result.buffer[3] = 0x80 | ((ucs >> 6) & 0x3f);
|
|
|
|
result.buffer[4] = 0x80 | (ucs & 0x3f);
|
2019-06-15 06:44:32 +08:00
|
|
|
result.bytes = 5;
|
|
|
|
} else {
|
2019-06-19 03:34:23 +08:00
|
|
|
result.buffer[0] = 0xfc | (ucs >> 30);
|
|
|
|
result.buffer[1] = 0x80 | ((ucs >> 24) & 0x3f);
|
|
|
|
result.buffer[2] = 0x80 | ((ucs >> 18) & 0x3f);
|
|
|
|
result.buffer[3] = 0x80 | ((ucs >> 12) & 0x3f);
|
|
|
|
result.buffer[4] = 0x80 | ((ucs >> 6) & 0x3f);
|
|
|
|
result.buffer[5] = 0x80 | (ucs & 0x3f);
|
2019-06-15 06:44:32 +08:00
|
|
|
result.bytes = 6;
|
2019-06-12 01:34:58 +08:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-06-15 06:44:32 +08:00
|
|
|
EncodedCharacter EncodeCharacter(Encoding encoding, char32_t ucs) {
|
2019-06-12 01:34:58 +08:00
|
|
|
switch (encoding) {
|
2019-10-03 05:24:44 +08:00
|
|
|
SWITCH_COVERS_ALL_CASES
|
2020-03-29 12:00:16 +08:00
|
|
|
case Encoding::LATIN_1:
|
|
|
|
return EncodeCharacter<Encoding::LATIN_1>(ucs);
|
|
|
|
case Encoding::UTF_8:
|
|
|
|
return EncodeCharacter<Encoding::UTF_8>(ucs);
|
2019-06-12 01:34:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <Encoding ENCODING, typename STRING>
|
2019-06-19 03:34:23 +08:00
|
|
|
std::string EncodeString(const STRING &str) {
|
|
|
|
std::string result;
|
|
|
|
for (auto ch : str) {
|
|
|
|
char32_t uch{static_cast<std::make_unsigned_t<decltype(ch)>>(ch)};
|
|
|
|
EncodedCharacter encoded{EncodeCharacter<ENCODING>(uch)};
|
|
|
|
result.append(encoded.buffer, static_cast<std::size_t>(encoded.bytes));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
template std::string EncodeString<Encoding::LATIN_1, std::string>(
|
|
|
|
const std::string &);
|
2019-06-29 02:16:37 +08:00
|
|
|
template std::string EncodeString<Encoding::UTF_8, std::u16string>(
|
2019-06-19 03:34:23 +08:00
|
|
|
const std::u16string &);
|
|
|
|
template std::string EncodeString<Encoding::UTF_8, std::u32string>(
|
|
|
|
const std::u32string &);
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <>
|
2019-06-19 03:34:23 +08:00
|
|
|
DecodedCharacter DecodeRawCharacter<Encoding::LATIN_1>(
|
|
|
|
const char *cp, std::size_t bytes) {
|
|
|
|
if (bytes >= 1) {
|
|
|
|
return {*reinterpret_cast<const std::uint8_t *>(cp), 1};
|
|
|
|
} else {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <>
|
2019-06-19 03:34:23 +08:00
|
|
|
DecodedCharacter DecodeRawCharacter<Encoding::UTF_8>(
|
|
|
|
const char *cp, std::size_t bytes) {
|
2019-06-12 01:34:58 +08:00
|
|
|
auto p{reinterpret_cast<const std::uint8_t *>(cp)};
|
|
|
|
char32_t ch{*p};
|
2019-06-13 06:26:37 +08:00
|
|
|
if (ch <= 0x7f) {
|
|
|
|
return {ch, 1};
|
|
|
|
} else if ((ch & 0xf8) == 0xf0 && bytes >= 4 && ch > 0xf0 &&
|
|
|
|
((p[1] | p[2] | p[3]) & 0xc0) == 0x80) {
|
|
|
|
ch = ((ch & 7) << 6) | (p[1] & 0x3f);
|
|
|
|
ch = (ch << 6) | (p[2] & 0x3f);
|
|
|
|
ch = (ch << 6) | (p[3] & 0x3f);
|
|
|
|
return {ch, 4};
|
|
|
|
} else if ((ch & 0xf0) == 0xe0 && bytes >= 3 && ch > 0xe0 &&
|
|
|
|
((p[1] | p[2]) & 0xc0) == 0x80) {
|
|
|
|
ch = ((ch & 0xf) << 6) | (p[1] & 0x3f);
|
|
|
|
ch = (ch << 6) | (p[2] & 0x3f);
|
|
|
|
return {ch, 3};
|
|
|
|
} else if ((ch & 0xe0) == 0xc0 && bytes >= 2 && ch > 0xc0 &&
|
|
|
|
(p[1] & 0xc0) == 0x80) {
|
|
|
|
ch = ((ch & 0x1f) << 6) | (p[1] & 0x3f);
|
|
|
|
return {ch, 2};
|
|
|
|
} else {
|
2020-03-29 12:00:16 +08:00
|
|
|
return {}; // not valid UTF-8
|
2019-06-12 01:34:58 +08:00
|
|
|
}
|
2018-11-02 02:18:12 +08:00
|
|
|
}
|
|
|
|
|
2019-06-13 06:26:37 +08:00
|
|
|
static DecodedCharacter DecodeEscapedCharacter(
|
|
|
|
const char *cp, std::size_t bytes) {
|
2019-06-14 05:16:04 +08:00
|
|
|
if (cp[0] == '\\' && bytes >= 2) {
|
2019-06-13 06:26:37 +08:00
|
|
|
if (std::optional<char> escChar{BackslashEscapeValue(cp[1])}) {
|
2019-06-14 05:16:04 +08:00
|
|
|
return {static_cast<unsigned char>(*escChar), 2};
|
2019-06-18 01:35:34 +08:00
|
|
|
} else if (IsOctalDigit(cp[1])) {
|
|
|
|
std::size_t maxLen{std::min(std::size_t{4}, bytes)};
|
2019-06-19 03:34:23 +08:00
|
|
|
char32_t code{static_cast<char32_t>(DecimalDigitValue(cp[1]))};
|
2020-03-29 12:00:16 +08:00
|
|
|
std::size_t len{2}; // so far
|
2019-06-18 01:35:34 +08:00
|
|
|
for (; code <= 037 && len < maxLen && IsOctalDigit(cp[len]); ++len) {
|
2019-06-13 06:26:37 +08:00
|
|
|
code = 8 * code + DecimalDigitValue(cp[len]);
|
|
|
|
}
|
|
|
|
return {code, static_cast<int>(len)};
|
|
|
|
} else if (bytes >= 4 && ToLowerCaseLetter(cp[1]) == 'x' &&
|
|
|
|
IsHexadecimalDigit(cp[2]) && IsHexadecimalDigit(cp[3])) {
|
|
|
|
return {static_cast<char32_t>(16 * HexadecimalDigitValue(cp[2]) +
|
|
|
|
HexadecimalDigitValue(cp[3])),
|
|
|
|
4};
|
2019-06-18 03:29:20 +08:00
|
|
|
} else if (IsLetter(cp[1])) {
|
|
|
|
// Unknown escape - ignore the '\' (PGI compatibility)
|
2019-06-18 01:35:34 +08:00
|
|
|
return {static_cast<unsigned char>(cp[1]), 2};
|
2019-06-18 03:29:20 +08:00
|
|
|
} else {
|
|
|
|
// Not an escape character.
|
|
|
|
return {'\\', 1};
|
2019-06-13 06:26:37 +08:00
|
|
|
}
|
|
|
|
}
|
2019-06-14 05:16:04 +08:00
|
|
|
return {static_cast<unsigned char>(cp[0]), 1};
|
2019-06-13 06:26:37 +08:00
|
|
|
}
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <Encoding ENCODING>
|
2019-06-13 06:26:37 +08:00
|
|
|
static DecodedCharacter DecodeEscapedCharacters(
|
2019-06-19 03:34:23 +08:00
|
|
|
const char *cp, std::size_t bytes) {
|
2019-06-15 06:44:32 +08:00
|
|
|
char buffer[EncodedCharacter::maxEncodingBytes];
|
|
|
|
int count[EncodedCharacter::maxEncodingBytes];
|
2019-06-13 06:26:37 +08:00
|
|
|
std::size_t at{0}, len{0};
|
2019-06-15 06:44:32 +08:00
|
|
|
for (; len < EncodedCharacter::maxEncodingBytes && at < bytes; ++len) {
|
2019-06-13 06:26:37 +08:00
|
|
|
DecodedCharacter code{DecodeEscapedCharacter(cp + at, bytes - at)};
|
2019-06-14 05:16:04 +08:00
|
|
|
buffer[len] = code.codepoint;
|
2019-06-13 06:26:37 +08:00
|
|
|
at += code.bytes;
|
|
|
|
count[len] = at;
|
|
|
|
}
|
2019-06-19 03:34:23 +08:00
|
|
|
DecodedCharacter code{DecodeCharacter<ENCODING>(buffer, len, false)};
|
2019-06-13 06:26:37 +08:00
|
|
|
if (code.bytes > 0) {
|
|
|
|
code.bytes = count[code.bytes - 1];
|
2019-06-14 05:16:04 +08:00
|
|
|
} else {
|
2019-06-15 06:44:32 +08:00
|
|
|
code.codepoint = buffer[0] & 0xff;
|
2019-06-14 05:16:04 +08:00
|
|
|
code.bytes = count[0];
|
2019-06-13 06:26:37 +08:00
|
|
|
}
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <Encoding ENCODING>
|
2019-06-19 03:34:23 +08:00
|
|
|
DecodedCharacter DecodeCharacter(
|
|
|
|
const char *cp, std::size_t bytes, bool backslashEscapes) {
|
2019-06-14 05:16:04 +08:00
|
|
|
if (backslashEscapes && bytes >= 2 && *cp == '\\') {
|
2019-06-19 03:34:23 +08:00
|
|
|
return DecodeEscapedCharacters<ENCODING>(cp, bytes);
|
2019-06-13 06:26:37 +08:00
|
|
|
} else {
|
2019-06-19 03:34:23 +08:00
|
|
|
return DecodeRawCharacter<ENCODING>(cp, bytes);
|
2019-06-12 01:34:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-19 03:34:23 +08:00
|
|
|
template DecodedCharacter DecodeCharacter<Encoding::LATIN_1>(
|
|
|
|
const char *, std::size_t, bool);
|
|
|
|
template DecodedCharacter DecodeCharacter<Encoding::UTF_8>(
|
|
|
|
const char *, std::size_t, bool);
|
|
|
|
|
|
|
|
DecodedCharacter DecodeCharacter(Encoding encoding, const char *cp,
|
|
|
|
std::size_t bytes, bool backslashEscapes) {
|
|
|
|
switch (encoding) {
|
2019-10-03 06:48:20 +08:00
|
|
|
SWITCH_COVERS_ALL_CASES
|
2019-06-19 03:34:23 +08:00
|
|
|
case Encoding::LATIN_1:
|
|
|
|
return DecodeCharacter<Encoding::LATIN_1>(cp, bytes, backslashEscapes);
|
|
|
|
case Encoding::UTF_8:
|
|
|
|
return DecodeCharacter<Encoding::UTF_8>(cp, bytes, backslashEscapes);
|
2018-11-06 05:48:00 +08:00
|
|
|
}
|
|
|
|
}
|
2019-06-12 01:34:58 +08:00
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <typename RESULT, Encoding ENCODING>
|
2019-06-29 02:16:37 +08:00
|
|
|
RESULT DecodeString(const std::string &s, bool backslashEscapes) {
|
|
|
|
RESULT result;
|
2019-06-12 01:34:58 +08:00
|
|
|
const char *p{s.c_str()};
|
|
|
|
for (auto bytes{s.size()}; bytes != 0;) {
|
2019-06-19 03:34:23 +08:00
|
|
|
DecodedCharacter decoded{
|
|
|
|
DecodeCharacter<ENCODING>(p, bytes, backslashEscapes)};
|
2019-06-12 01:34:58 +08:00
|
|
|
if (decoded.bytes > 0) {
|
|
|
|
if (static_cast<std::size_t>(decoded.bytes) <= bytes) {
|
2019-06-14 05:16:04 +08:00
|
|
|
result.append(1, decoded.codepoint);
|
2019-06-12 01:34:58 +08:00
|
|
|
bytes -= decoded.bytes;
|
|
|
|
p += decoded.bytes;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result.append(1, static_cast<uint8_t>(*p));
|
|
|
|
++p;
|
|
|
|
--bytes;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-06-29 02:16:37 +08:00
|
|
|
template std::string DecodeString<std::string, Encoding::LATIN_1>(
|
|
|
|
const std::string &, bool);
|
|
|
|
template std::u16string DecodeString<std::u16string, Encoding::UTF_8>(
|
2019-06-19 03:34:23 +08:00
|
|
|
const std::string &, bool);
|
2019-06-29 02:16:37 +08:00
|
|
|
template std::u32string DecodeString<std::u32string, Encoding::UTF_8>(
|
2019-06-19 03:34:23 +08:00
|
|
|
const std::string &, bool);
|
2020-03-29 12:00:16 +08:00
|
|
|
} // namespace Fortran::parser
|