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 '[':
174 case ']':
175 case '{': // Used in OpenMP context selector specification
176 case '}': //
177 return true;
178 default:
179 return IsLegalIdentifierStart(ch) || IsDecimalDigit(ch);
180 }
181}
182
184 static constexpr int maxEncodingBytes{6};
185 char buffer[maxEncodingBytes];
186 int bytes{0};
187};
188
189template <Encoding ENCODING> EncodedCharacter EncodeCharacter(char32_t ucs);
190template <> EncodedCharacter EncodeCharacter<Encoding::LATIN_1>(char32_t);
191template <> EncodedCharacter EncodeCharacter<Encoding::UTF_8>(char32_t);
192
193EncodedCharacter EncodeCharacter(Encoding, char32_t ucs);
194
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 &);
201
202// EmitQuotedChar drives callbacks "emit" and "insert" to output the
203// bytes of an encoding for a codepoint.
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)}) {
210 insert('\\');
211 emit(*escape);
212 } else if (useHexadecimalEscapeSequences) {
213 insert('\\');
214 insert('x');
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);
218 } else {
219 // octal escape sequence; always emit 3 digits to avoid ambiguity
220 insert('\\');
221 insert('0' + (ch >> 6));
222 insert('0' + ((ch >> 3) & 7));
223 insert('0' + (ch & 7));
224 }
225 } else if (ch == '\n') { // always escape newlines
226 insert('\\');
227 insert('n');
228 } else {
229 emit(ch);
230 }
231 }};
232 if (ch <= 0x7f) {
233 emitOneByte(ch);
234 } else if (backslashEscapes && useHexadecimalEscapeSequences) {
235 insert('\\');
236 insert('u');
237 if (ch > 0xffff) {
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);
244 }
245 unsigned c1{(ch >> 12) & 0xf}, c2{(ch >> 8) & 0xf}, c3{(ch >> 4) & 0xf},
246 c4{ch & 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);
251 } else {
252 EncodedCharacter encoded{EncodeCharacter(encoding, ch)};
253 for (int j{0}; j < encoded.bytes; ++j) {
254 emitOneByte(encoded.buffer[j]);
255 }
256 }
257}
258
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);
265
266int UTF_8CharacterBytes(const char *);
267
269 char32_t codepoint{0};
270 int bytes{0}; // signifying failure
271};
272
273template <Encoding ENCODING>
274DecodedCharacter DecodeRawCharacter(const char *, std::size_t);
275template <>
276DecodedCharacter DecodeRawCharacter<Encoding::LATIN_1>(
277 const char *, std::size_t);
278
279template <>
280DecodedCharacter DecodeRawCharacter<Encoding::UTF_8>(const char *, std::size_t);
281
282// DecodeCharacter optionally handles backslash escape sequences, too.
283template <Encoding ENCODING>
284DecodedCharacter DecodeCharacter(
285 const char *, std::size_t, bool backslashEscapes);
286extern template DecodedCharacter DecodeCharacter<Encoding::LATIN_1>(
287 const char *, std::size_t, bool);
288extern template DecodedCharacter DecodeCharacter<Encoding::UTF_8>(
289 const char *, std::size_t, bool);
290
291DecodedCharacter DecodeCharacter(
292 Encoding, const char *, std::size_t, bool backslashEscapes);
293
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);
302} // namespace Fortran::parser
303#endif // FORTRAN_PARSER_CHARACTERS_H_
Definition check-expression.h:19
Definition characters.h:268
Definition characters.h:183