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) {
179 return IsLegalIdentifierStart(ch) || IsDecimalDigit(ch);
184 static constexpr int maxEncodingBytes{6};
185 char buffer[maxEncodingBytes];
195template <Encoding ENCODING,
typename STRING>
196std::string EncodeString(
const STRING &);
197extern template std::string EncodeString<Encoding::LATIN_1, std::string>(
198 const std::string &);
199extern template std::string EncodeString<Encoding::UTF_8, std::u32string>(
200 const std::u32string &);
204template <
typename NORMAL,
typename INSERTED>
205void EmitQuotedChar(
char32_t ch,
const NORMAL &emit,
const INSERTED &insert,
206 bool backslashEscapes =
true, Encoding encoding = Encoding::UTF_8) {
207 auto emitOneByte{[&](std::uint8_t ch) {
208 if (backslashEscapes && (ch <
' ' || ch >= 0x7f || ch ==
'\\')) {
209 if (std::optional<char> escape{BackslashEscapeChar(ch)}) {
212 }
else if (useHexadecimalEscapeSequences) {
215 int top{ch >> 4}, bottom{ch & 0xf};
216 insert(top > 9 ?
'a' + top - 10 :
'0' + top);
217 insert(bottom > 9 ?
'a' + bottom - 10 :
'0' + bottom);
221 insert(
'0' + (ch >> 6));
222 insert(
'0' + ((ch >> 3) & 7));
223 insert(
'0' + (ch & 7));
225 }
else if (ch ==
'\n') {
234 }
else if (backslashEscapes && useHexadecimalEscapeSequences) {
238 unsigned c1{(ch >> 28) & 0xf}, c2{(ch >> 24) & 0xf}, c3{(ch >> 20) & 0xf},
239 c4{(ch >> 16) & 0xf};
240 insert(c1 > 9 ?
'a' + c1 - 10 :
'0' + c1);
241 insert(c2 > 9 ?
'a' + c2 - 10 :
'0' + c2);
242 insert(c3 > 9 ?
'a' + c3 - 10 :
'0' + c3);
243 insert(c4 > 9 ?
'a' + c4 - 10 :
'0' + c4);
245 unsigned c1{(ch >> 12) & 0xf}, c2{(ch >> 8) & 0xf}, c3{(ch >> 4) & 0xf},
247 insert(c1 > 9 ?
'a' + c1 - 10 :
'0' + c1);
248 insert(c2 > 9 ?
'a' + c2 - 10 :
'0' + c2);
249 insert(c3 > 9 ?
'a' + c3 - 10 :
'0' + c3);
250 insert(c4 > 9 ?
'a' + c4 - 10 :
'0' + c4);
253 for (
int j{0}; j < encoded.bytes; ++j) {
254 emitOneByte(encoded.buffer[j]);
259std::string QuoteCharacterLiteral(
const std::string &,
260 bool backslashEscapes =
true, Encoding = Encoding::LATIN_1);
261std::string QuoteCharacterLiteral(
const std::u16string &,
262 bool backslashEscapes =
true, Encoding = Encoding::UTF_8);
263std::string QuoteCharacterLiteral(
const std::u32string &,
264 bool backslashEscapes =
true, Encoding = Encoding::UTF_8);
266int UTF_8CharacterBytes(
const char *);
269 char32_t codepoint{0};
273template <Encoding ENCODING>
277 const char *, std::size_t);
280DecodedCharacter DecodeRawCharacter<Encoding::UTF_8>(
const char *, std::size_t);
283template <Encoding ENCODING>
285 const char *, std::size_t,
bool backslashEscapes);
287 const char *, std::size_t,
bool);
289 const char *, std::size_t,
bool);
292 Encoding,
const char *, std::size_t,
bool backslashEscapes);
294template <
typename RESULT, Encoding ENCODING>
295RESULT DecodeString(
const std::string &,
bool backslashEscapes);
296extern template std::string DecodeString<std::string, Encoding::LATIN_1>(
297 const std::string &,
bool);
298extern template std::u16string DecodeString<std::u16string, Encoding::UTF_8>(
299 const std::string &,
bool);
300extern template std::u32string DecodeString<std::u32string, Encoding::UTF_8>(
301 const std::string &,
bool);
Definition check-expression.h:19
Definition characters.h:268
Definition characters.h:183