[flang] More repackaging
authorpeter klausler <pklausler@nvidia.com>
Wed, 18 Apr 2018 23:39:28 +0000 (16:39 -0700)
committerpeter klausler <pklausler@nvidia.com>
Thu, 19 Apr 2018 18:21:07 +0000 (11:21 -0700)
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
flang/lib/parser/char-set.h
flang/lib/parser/message.cc
flang/lib/parser/message.h
flang/lib/parser/token-parsers.h

index 6054e4a..174667c 100644 (file)
@@ -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;
     }
   }
index f6f398b..5ee739d 100644 (file)
@@ -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<std::uint64_t>(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<std::uint64_t>(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_
index 3e19004..d4b015e 100644 (file)
@@ -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 {
index c705c5b..da1b97b 100644 (file)
@@ -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_; }
index cef6751..b5899dd 100644 (file)
@@ -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<char quote> struct CharLiteral {
     static constexpr auto nextch = attempt(CharLiteralChar{});
     while (std::optional<CharLiteralChar::Result> 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};
         }