From 107aa28930f5f3d74622c4af5e7c3f0656eb3bfe Mon Sep 17 00:00:00 2001 From: peter klausler Date: Wed, 18 Apr 2018 16:39:28 -0700 Subject: [PATCH] [flang] More repackaging Original-commit: flang-compiler/f18@55f4f0505e57b750c3da0dc3fc9e2bbd0c99d831 Reviewed-on: https://github.com/flang-compiler/f18/pull/61 Tree-same-pre-rewrite: false --- flang/lib/parser/char-set.cc | 2 +- flang/lib/parser/char-set.h | 51 ++++++++++++++++------------------------ flang/lib/parser/message.cc | 2 +- flang/lib/parser/message.h | 2 +- flang/lib/parser/token-parsers.h | 5 ++-- 5 files changed, 25 insertions(+), 37 deletions(-) diff --git a/flang/lib/parser/char-set.cc b/flang/lib/parser/char-set.cc index 6054e4a..174667c 100644 --- a/flang/lib/parser/char-set.cc +++ b/flang/lib/parser/char-set.cc @@ -8,7 +8,7 @@ std::string SetOfChars::ToString() const { std::uint64_t set{bits_}; for (char ch{' '}; set != 0; ++ch) { if (IsCharInSet(set, ch)) { - set -= EncodeChar(ch); + set -= SetOfChars{ch}.bits_; result += ch; } } diff --git a/flang/lib/parser/char-set.h b/flang/lib/parser/char-set.h index f6f398b..5ee739d 100644 --- a/flang/lib/parser/char-set.h +++ b/flang/lib/parser/char-set.h @@ -14,6 +14,25 @@ namespace parser { struct SetOfChars { constexpr SetOfChars() {} + constexpr SetOfChars(char c) { + if (c <= 32 /*space*/) { + // map control characters, incl. LF (newline), to '?' + c = '?'; + } else if (c >= 127) { + // map DEL and 8-bit characters to '^' + c = '^'; + } else if (c >= 96) { + // map lower-case letters to upper-case + c -= 32; + } + // range is now [32..95]; reduce to [0..63] and use as a shift count + bits_ = static_cast(1) << (c - 32); + } + constexpr SetOfChars(const char str[], std::size_t n = 256) { + for (std::size_t j{0}; j < n; ++j) { + bits_ |= SetOfChars{str[j]}.bits_; + } + } constexpr SetOfChars(std::uint64_t b) : bits_{b} {} constexpr SetOfChars(const SetOfChars &) = default; constexpr SetOfChars(SetOfChars &&) = default; @@ -23,39 +42,9 @@ struct SetOfChars { std::uint64_t bits_{0}; }; -static constexpr std::uint64_t EncodeChar(char c) { - if (c <= 32 /*space*/) { - // map control characters, incl. LF (newline), to '?' - c = '?'; - } else if (c >= 127) { - // map DEL and 8-bit characters to '^' - c = '^'; - } else if (c >= 96) { - // map lower-case letters to upper-case - c -= 32; - } - // range is now [32..95]; reduce to [0..63] and use as a shift count - return static_cast(1) << (c - 32); -} - -static constexpr SetOfChars SingletonChar(char c) { return {EncodeChar(c)}; } - -static constexpr SetOfChars CharsToSet(const char str[], std::size_t n = 256) { - SetOfChars chars; - for (std::size_t j{0}; j < n; ++j) { - if (str[j] == '\0') { - break; - } - chars.bits_ |= EncodeChar(str[j]); - } - return chars; -} - static inline constexpr bool IsCharInSet(SetOfChars set, char c) { - return (set.bits_ & EncodeChar(c)) != 0; + return (set.bits_ & SetOfChars{c}.bits_) != 0; } - -std::string SetOfCharsToString(SetOfChars); } // namespace parser } // namespace Fortran #endif // FORTRAN_PARSER_CHAR_SET_H_ diff --git a/flang/lib/parser/message.cc b/flang/lib/parser/message.cc index 3e19004..d4b015e 100644 --- a/flang/lib/parser/message.cc +++ b/flang/lib/parser/message.cc @@ -58,7 +58,7 @@ std::string Message::ToString() const { } else { SetOfChars expect{expected_}; if (IsCharInSet(expect, '\n')) { - expect.bits_ &= ~SingletonChar('\n').bits_; + expect.bits_ &= ~SetOfChars{'\n'}.bits_; if (expect.bits_ == 0) { return "expected end of line"_err_en_US.ToString(); } else { diff --git a/flang/lib/parser/message.h b/flang/lib/parser/message.h index c705c5b..da1b97b 100644 --- a/flang/lib/parser/message.h +++ b/flang/lib/parser/message.h @@ -77,7 +77,7 @@ public: } } MessageExpectedText(MessageExpectedText &&) = default; - explicit MessageExpectedText(char ch) : set_{SingletonChar(ch)} {} + explicit MessageExpectedText(char ch) : set_{ch} {} explicit MessageExpectedText(SetOfChars set) : set_{set} {} const char *str() const { return str_; } diff --git a/flang/lib/parser/token-parsers.h b/flang/lib/parser/token-parsers.h index cef6751..b5899dd 100644 --- a/flang/lib/parser/token-parsers.h +++ b/flang/lib/parser/token-parsers.h @@ -44,7 +44,7 @@ private: }; constexpr AnyOfChars operator""_ch(const char str[], std::size_t n) { - return AnyOfChars{CharsToSet(str, n)}; + return AnyOfChars{SetOfChars(str, n)}; } constexpr auto letter = "abcdefghijklmnopqrstuvwxyz"_ch; @@ -266,8 +266,7 @@ template struct CharLiteral { static constexpr auto nextch = attempt(CharLiteralChar{}); while (std::optional ch{nextch.Parse(state)}) { if (ch->ch == quote && !ch->wasEscaped) { - static constexpr auto doubled = - attempt(AnyOfChars{SingletonChar(quote)}); + static constexpr auto doubled = attempt(AnyOfChars{SetOfChars{quote}}); if (!doubled.Parse(state).has_value()) { return {str}; } -- 2.7.4