FLANG
characters.h
1//===-- include/flang/Parser/characters.h -----------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef FORTRAN_PARSER_CHARACTERS_H_
10#define FORTRAN_PARSER_CHARACTERS_H_
11
12// Define some character classification predicates and
13// conversions here to avoid dependences upon <cctype> and
14// also to accomodate Fortran tokenization.
15
16#include <cstddef>
17#include <cstdint>
18#include <optional>
19#include <string>
20
21namespace Fortran::parser {
22
23extern bool useHexadecimalEscapeSequences;
24
25// We can easily support Fortran program source in any character
26// set whose first 128 code points correspond to ASCII codes 0-127 (ISO/IEC646).
27// The specific encodings that we can handle include:
28// LATIN_1: ISO 8859-1 Latin-1
29// UTF_8: Multi-byte encoding of Unicode (ISO/IEC 10646)
30enum class Encoding { LATIN_1, UTF_8 };
31
32inline constexpr bool IsUpperCaseLetter(char ch) {
33 return ch >= 'A' && ch <= 'Z';
34}
35
36inline constexpr bool IsLowerCaseLetter(char ch) {
37 return ch >= 'a' && ch <= 'z';
38}
39
40inline constexpr bool IsLetter(char ch) {
41 return IsUpperCaseLetter(ch) || IsLowerCaseLetter(ch);
42}
43
44inline constexpr bool IsDecimalDigit(char ch) { return ch >= '0' && ch <= '9'; }
45
46inline constexpr bool IsHexadecimalDigit(char ch) {
47 return (ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'F') ||
48 (ch >= 'a' && ch <= 'f');
49}
50
51inline constexpr bool IsOctalDigit(char ch) { return ch >= '0' && ch <= '7'; }
52
53inline constexpr bool IsLegalIdentifierStart(char ch) {
54 return IsLetter(ch) || ch == '_' || ch == '@' || ch == '$';
55}
56
57inline constexpr bool IsLegalInIdentifier(char ch) {
58 return IsLegalIdentifierStart(ch) || IsDecimalDigit(ch);
59}
60
61inline constexpr bool IsPrintable(char ch) { return ch >= ' ' && ch <= '~'; }
62
63inline constexpr bool IsWhiteSpace(char ch) {
64 return ch == ' ' || ch == '\t' || ch == '\n' || ch == '\v' || ch == '\f' ||
65 ch == '\r';
66}
67
68inline constexpr char ToLowerCaseLetter(char ch) {
69 return IsUpperCaseLetter(ch) ? ch - 'A' + 'a' : ch;
70}
71
72inline std::string ToLowerCaseLetters(std::string_view str) {
73 std::string lowered{str};
74 for (char &ch : lowered) {
75 ch = ToLowerCaseLetter(ch);
76 }
77 return lowered;
78}
79
80inline constexpr char ToUpperCaseLetter(char ch) {
81 return IsLowerCaseLetter(ch) ? ch - 'a' + 'A' : ch;
82}
83
84inline std::string ToUpperCaseLetters(std::string_view str) {
85 std::string raised{str};
86 for (char &ch : raised) {
87 ch = ToUpperCaseLetter(ch);
88 }
89 return raised;
90}
91
92inline constexpr bool IsSameApartFromCase(char x, char y) {
93 return ToLowerCaseLetter(x) == ToLowerCaseLetter(y);
94}
95
96inline constexpr char DecimalDigitValue(char ch) { return ch - '0'; }
97
98inline constexpr char HexadecimalDigitValue(char ch) {
99 return IsUpperCaseLetter(ch) ? ch - 'A' + 10
100 : IsLowerCaseLetter(ch) ? ch - 'a' + 10
101 : DecimalDigitValue(ch);
102}
103
104inline constexpr std::optional<char> BackslashEscapeValue(char ch) {
105 switch (ch) {
106 case 'a':
107 return std::nullopt; // '\a'; PGF90 doesn't know \a
108 case 'b':
109 return '\b';
110 case 'f':
111 return '\f';
112 case 'n':
113 return '\n';
114 case 'r':
115 return '\r';
116 case 't':
117 return '\t';
118 case 'v':
119 return '\v';
120 case '"':
121 case '\'':
122 case '\\':
123 return ch;
124 default:
125 return std::nullopt;
126 }
127}
128
129inline constexpr std::optional<char> BackslashEscapeChar(char ch) {
130 switch (ch) {
131 case '\a':
132 return std::nullopt; // 'a'; PGF90 doesn't know \a
133 case '\b':
134 return 'b';
135 case '\f':
136 return 'f';
137 case '\n':
138 return 'n';
139 case '\r':
140 return 'r';
141 case '\t':
142 return 't';
143 case '\v':
144 return 'v';
145 case '"':
146 case '\'':
147 case '\\':
148 return ch;
149 default:
150 return std::nullopt;
151 }
152}
153
154// Does not include spaces or line ending characters.
155inline constexpr bool IsValidFortranTokenCharacter(char ch) {
156 switch (ch) {
157 case '"':
158 case '%':
159 case '\'':
160 case '(':
161 case ')':
162 case '*':
163 case '+':
164 case ',':
165 case '-':
166 case '.':
167 case '/':
168 case ':':
169 case ';':
170 case '<':
171 case '=':
172 case '>':
173 case '?': // Used in conditional expressions (Fortran 2023)
174 case '[':
175 case ']':
176 case '{': // Used in OpenMP context selector specification
177 case '}': //
178 return true;
179 default:
180 return IsLegalIdentifierStart(ch) || IsDecimalDigit(ch);
181 }
182}
183
185 static constexpr int maxEncodingBytes{6};
186 char buffer[maxEncodingBytes];
187 int bytes{0};
188};
189
190template <Encoding ENCODING> EncodedCharacter EncodeCharacter(char32_t ucs);
191template <> EncodedCharacter EncodeCharacter<Encoding::LATIN_1>(char32_t);
192template <> EncodedCharacter EncodeCharacter<Encoding::UTF_8>(char32_t);
193
194EncodedCharacter EncodeCharacter(Encoding, char32_t ucs);
195
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 &);
202
203// EmitQuotedChar drives callbacks "emit" and "insert" to output the
204// bytes of an encoding for a codepoint.
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)}) {
211 insert('\\');
212 emit(*escape);
213 } else if (useHexadecimalEscapeSequences) {
214 insert('\\');
215 insert('x');
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);
219 } else {
220 // octal escape sequence; always emit 3 digits to avoid ambiguity
221 insert('\\');
222 insert('0' + (ch >> 6));
223 insert('0' + ((ch >> 3) & 7));
224 insert('0' + (ch & 7));
225 }
226 } else if (ch == '\n') { // always escape newlines
227 insert('\\');
228 insert('n');
229 } else {
230 emit(ch);
231 }
232 }};
233 if (ch <= 0x7f) {
234 emitOneByte(ch);
235 } else if (backslashEscapes && useHexadecimalEscapeSequences) {
236 insert('\\');
237 insert('u');
238 if (ch > 0xffff) {
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);
245 }
246 unsigned c1{(ch >> 12) & 0xf}, c2{(ch >> 8) & 0xf}, c3{(ch >> 4) & 0xf},
247 c4{ch & 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);
252 } else {
253 EncodedCharacter encoded{EncodeCharacter(encoding, ch)};
254 for (int j{0}; j < encoded.bytes; ++j) {
255 emitOneByte(encoded.buffer[j]);
256 }
257 }
258}
259
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);
266
267int UTF_8CharacterBytes(const char *);
268
270 char32_t codepoint{0};
271 int bytes{0}; // signifying failure
272};
273
274template <Encoding ENCODING>
275DecodedCharacter DecodeRawCharacter(const char *, std::size_t);
276template <>
277DecodedCharacter DecodeRawCharacter<Encoding::LATIN_1>(
278 const char *, std::size_t);
279
280template <>
281DecodedCharacter DecodeRawCharacter<Encoding::UTF_8>(const char *, std::size_t);
282
283// DecodeCharacter optionally handles backslash escape sequences, too.
284template <Encoding ENCODING>
285DecodedCharacter DecodeCharacter(
286 const char *, std::size_t, bool backslashEscapes);
287extern template DecodedCharacter DecodeCharacter<Encoding::LATIN_1>(
288 const char *, std::size_t, bool);
289extern template DecodedCharacter DecodeCharacter<Encoding::UTF_8>(
290 const char *, std::size_t, bool);
291
292DecodedCharacter DecodeCharacter(
293 Encoding, const char *, std::size_t, bool backslashEscapes);
294
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);
303} // namespace Fortran::parser
304#endif // FORTRAN_PARSER_CHARACTERS_H_
Definition check-expression.h:19
Definition characters.h:269
Definition characters.h:184