9#ifndef FORTRAN_PARSER_CHARACTERS_H_
10#define FORTRAN_PARSER_CHARACTERS_H_
23extern bool useHexadecimalEscapeSequences;
30enum class Encoding { LATIN_1, UTF_8 };
32inline constexpr bool IsUpperCaseLetter(
char ch) {
33 return ch >=
'A' && ch <=
'Z';
36inline constexpr bool IsLowerCaseLetter(
char ch) {
37 return ch >=
'a' && ch <=
'z';
40inline constexpr bool IsLetter(
char ch) {
41 return IsUpperCaseLetter(ch) || IsLowerCaseLetter(ch);
44inline constexpr bool IsDecimalDigit(
char ch) {
return ch >=
'0' && ch <=
'9'; }
46inline constexpr bool IsHexadecimalDigit(
char ch) {
47 return (ch >=
'0' && ch <=
'9') || (ch >=
'A' && ch <=
'F') ||
48 (ch >=
'a' && ch <=
'f');
51inline constexpr bool IsOctalDigit(
char ch) {
return ch >=
'0' && ch <=
'7'; }
53inline constexpr bool IsLegalIdentifierStart(
char ch) {
54 return IsLetter(ch) || ch ==
'_' || ch ==
'@' || ch ==
'$';
57inline constexpr bool IsLegalInIdentifier(
char ch) {
58 return IsLegalIdentifierStart(ch) || IsDecimalDigit(ch);
61inline constexpr bool IsPrintable(
char ch) {
return ch >=
' ' && ch <=
'~'; }
63inline constexpr bool IsWhiteSpace(
char ch) {
64 return ch ==
' ' || ch ==
'\t' || ch ==
'\n' || ch ==
'\v' || ch ==
'\f' ||
68inline constexpr char ToLowerCaseLetter(
char ch) {
69 return IsUpperCaseLetter(ch) ? ch -
'A' +
'a' : ch;
72inline std::string ToLowerCaseLetters(std::string_view str) {
73 std::string lowered{str};
74 for (
char &ch : lowered) {
75 ch = ToLowerCaseLetter(ch);
80inline constexpr char ToUpperCaseLetter(
char ch) {
81 return IsLowerCaseLetter(ch) ? ch -
'a' +
'A' : ch;
84inline std::string ToUpperCaseLetters(std::string_view str) {
85 std::string raised{str};
86 for (
char &ch : raised) {
87 ch = ToUpperCaseLetter(ch);
92inline constexpr bool IsSameApartFromCase(
char x,
char y) {
93 return ToLowerCaseLetter(x) == ToLowerCaseLetter(y);
96inline constexpr char DecimalDigitValue(
char ch) {
return ch -
'0'; }
98inline constexpr char HexadecimalDigitValue(
char ch) {
99 return IsUpperCaseLetter(ch) ? ch -
'A' + 10
100 : IsLowerCaseLetter(ch) ? ch -
'a' + 10
101 : DecimalDigitValue(ch);
104inline constexpr std::optional<char> BackslashEscapeValue(
char ch) {
129inline constexpr std::optional<char> BackslashEscapeChar(
char ch) {
155inline constexpr bool IsValidFortranTokenCharacter(
char ch) {
180 return IsLegalIdentifierStart(ch) || IsDecimalDigit(ch);
185 static constexpr int maxEncodingBytes{6};
186 char buffer[maxEncodingBytes];
196template <Encoding ENCODING,
typename STRING>
197std::string EncodeString(
const STRING &);
198extern template std::string EncodeString<Encoding::LATIN_1, std::string>(
199 const std::string &);
200extern template std::string EncodeString<Encoding::UTF_8, std::u32string>(
201 const std::u32string &);
205template <
typename NORMAL,
typename INSERTED>
206void EmitQuotedChar(
char32_t ch,
const NORMAL &emit,
const INSERTED &insert,
207 bool backslashEscapes =
true, Encoding encoding = Encoding::UTF_8) {
208 auto emitOneByte{[&](std::uint8_t ch) {
209 if (backslashEscapes && (ch <
' ' || ch >= 0x7f || ch ==
'\\')) {
210 if (std::optional<char> escape{BackslashEscapeChar(ch)}) {
213 }
else if (useHexadecimalEscapeSequences) {
216 int top{ch >> 4}, bottom{ch & 0xf};
217 insert(top > 9 ?
'a' + top - 10 :
'0' + top);
218 insert(bottom > 9 ?
'a' + bottom - 10 :
'0' + bottom);
222 insert(
'0' + (ch >> 6));
223 insert(
'0' + ((ch >> 3) & 7));
224 insert(
'0' + (ch & 7));
226 }
else if (ch ==
'\n') {
235 }
else if (backslashEscapes && useHexadecimalEscapeSequences) {
239 unsigned c1{(ch >> 28) & 0xf}, c2{(ch >> 24) & 0xf}, c3{(ch >> 20) & 0xf},
240 c4{(ch >> 16) & 0xf};
241 insert(c1 > 9 ?
'a' + c1 - 10 :
'0' + c1);
242 insert(c2 > 9 ?
'a' + c2 - 10 :
'0' + c2);
243 insert(c3 > 9 ?
'a' + c3 - 10 :
'0' + c3);
244 insert(c4 > 9 ?
'a' + c4 - 10 :
'0' + c4);
246 unsigned c1{(ch >> 12) & 0xf}, c2{(ch >> 8) & 0xf}, c3{(ch >> 4) & 0xf},
248 insert(c1 > 9 ?
'a' + c1 - 10 :
'0' + c1);
249 insert(c2 > 9 ?
'a' + c2 - 10 :
'0' + c2);
250 insert(c3 > 9 ?
'a' + c3 - 10 :
'0' + c3);
251 insert(c4 > 9 ?
'a' + c4 - 10 :
'0' + c4);
254 for (
int j{0}; j < encoded.bytes; ++j) {
255 emitOneByte(encoded.buffer[j]);
260std::string QuoteCharacterLiteral(
const std::string &,
261 bool backslashEscapes =
true, Encoding = Encoding::LATIN_1);
262std::string QuoteCharacterLiteral(
const std::u16string &,
263 bool backslashEscapes =
true, Encoding = Encoding::UTF_8);
264std::string QuoteCharacterLiteral(
const std::u32string &,
265 bool backslashEscapes =
true, Encoding = Encoding::UTF_8);
267int UTF_8CharacterBytes(
const char *);
270 char32_t codepoint{0};
274template <Encoding ENCODING>
278 const char *, std::size_t);
281DecodedCharacter DecodeRawCharacter<Encoding::UTF_8>(
const char *, std::size_t);
284template <Encoding ENCODING>
286 const char *, std::size_t,
bool backslashEscapes);
288 const char *, std::size_t,
bool);
290 const char *, std::size_t,
bool);
293 Encoding,
const char *, std::size_t,
bool backslashEscapes);
295template <
typename RESULT, Encoding ENCODING>
296RESULT DecodeString(
const std::string &,
bool backslashEscapes);
297extern template std::string DecodeString<std::string, Encoding::LATIN_1>(
298 const std::string &,
bool);
299extern template std::u16string DecodeString<std::u16string, Encoding::UTF_8>(
300 const std::string &,
bool);
301extern template std::u32string DecodeString<std::u32string, Encoding::UTF_8>(
302 const std::string &,
bool);
Definition check-expression.h:19
Definition characters.h:269
Definition characters.h:184