Browse Source

Clang-Format

Sam Jaffe 7 years ago
parent
commit
56143608b1
8 changed files with 193 additions and 82 deletions
  1. 108 0
      .clang-format
  2. 5 7
      include/die.h
  3. 2 1
      include/exception.h
  4. 1 0
      include/random.h
  5. 1 1
      include/roll.h
  6. 55 54
      src/die.cxx
  7. 1 1
      src/main.cxx
  8. 20 18
      src/roll.cxx

+ 108 - 0
.clang-format

@@ -0,0 +1,108 @@
+---
+Language:        Cpp
+# BasedOnStyle:  LLVM
+AccessModifierOffset: -2
+AlignAfterOpenBracket: Align
+AlignConsecutiveAssignments: false
+AlignConsecutiveDeclarations: false
+AlignEscapedNewlines: Right
+AlignOperands:   true
+AlignTrailingComments: true
+AllowAllParametersOfDeclarationOnNextLine: true
+AllowShortBlocksOnASingleLine: true
+AllowShortCaseLabelsOnASingleLine: false
+AllowShortFunctionsOnASingleLine: All
+AllowShortIfStatementsOnASingleLine: true
+AllowShortLoopsOnASingleLine: false
+AlwaysBreakAfterDefinitionReturnType: None
+AlwaysBreakAfterReturnType: None
+AlwaysBreakBeforeMultilineStrings: false
+AlwaysBreakTemplateDeclarations: false
+BinPackArguments: true
+BinPackParameters: true
+BraceWrapping:   
+  AfterClass:      false
+  AfterControlStatement: false
+  AfterEnum:       false
+  AfterFunction:   false
+  AfterNamespace:  false
+  AfterObjCDeclaration: false
+  AfterStruct:     false
+  AfterUnion:      false
+  BeforeCatch:     false
+  BeforeElse:      false
+  IndentBraces:    false
+  SplitEmptyFunction: true
+  SplitEmptyRecord: true
+  SplitEmptyNamespace: true
+BreakBeforeBinaryOperators: None
+BreakBeforeBraces: Attach
+BreakBeforeInheritanceComma: false
+BreakBeforeTernaryOperators: true
+BreakConstructorInitializersBeforeComma: false
+BreakConstructorInitializers: BeforeColon
+BreakAfterJavaFieldAnnotations: false
+BreakStringLiterals: true
+ColumnLimit:     80
+CommentPragmas:  '^ IWYU pragma:'
+CompactNamespaces: true
+ConstructorInitializerAllOnOneLineOrOnePerLine: false
+ConstructorInitializerIndentWidth: 4
+ContinuationIndentWidth: 4
+Cpp11BracedListStyle: true
+DerivePointerAlignment: false
+DisableFormat:   false
+ExperimentalAutoDetectBinPacking: false
+FixNamespaceComments: false
+ForEachMacros:   
+  - foreach
+  - Q_FOREACH
+  - BOOST_FOREACH
+IncludeCategories: 
+  - Regex:           '^"(llvm|llvm-c|clang|clang-c)/'
+    Priority:        2
+  - Regex:           '^(<|"(gtest|gmock|isl|json)/)'
+    Priority:        3
+  - Regex:           '.*'
+    Priority:        1
+IncludeIsMainRegex: '(Test)?$'
+IndentCaseLabels: false
+IndentWidth:     2
+IndentWrappedFunctionNames: false
+JavaScriptQuotes: Leave
+JavaScriptWrapImports: true
+KeepEmptyLinesAtTheStartOfBlocks: true
+MacroBlockBegin: ''
+MacroBlockEnd:   ''
+MaxEmptyLinesToKeep: 1
+NamespaceIndentation: All
+ObjCBlockIndentWidth: 2
+ObjCSpaceAfterProperty: false
+ObjCSpaceBeforeProtocolList: true
+PenaltyBreakAssignment: 2
+PenaltyBreakBeforeFirstCallParameter: 19
+PenaltyBreakComment: 300
+PenaltyBreakFirstLessLess: 120
+PenaltyBreakString: 1000
+PenaltyExcessCharacter: 1000000
+PenaltyReturnTypeOnItsOwnLine: 60
+PointerAlignment: Middle
+ReflowComments:  true
+SortIncludes:    true
+SortUsingDeclarations: true
+SpaceAfterCStyleCast: false
+SpaceAfterTemplateKeyword: true
+SpaceBeforeAssignmentOperators: true
+SpaceBeforeParens: ControlStatements
+SpaceInEmptyParentheses: false
+SpacesBeforeTrailingComments: 1
+SpacesInAngles:  false
+SpacesInContainerLiterals: true
+SpacesInCStyleCastParentheses: false
+SpacesInParentheses: false
+SpacesInSquareBrackets: false
+Standard:        Cpp11
+TabWidth:        8
+UseTab:          Never
+...
+

+ 5 - 7
include/die.h

@@ -13,33 +13,31 @@
 #include <vector>
 
 namespace dice {
-  enum sign {
-    PLUS = 1, MINUS = -1, ZERO = 0
-  };
+  enum sign { PLUS = 1, MINUS = -1, ZERO = 0 };
   template <typename T> static sign sgn(T val) {
     return sign((T(0) < val) - (val < T(0)));
   }
   int sgn(sign);
   std::string str(sign);
-  
+
   struct die {
     sign sgn;
     int num, sides;
   };
-  
+
   struct mod {
     operator int() const;
     sign sign;
     int value;
   };
-  
+
   // Default value: 1{+0}
   struct dice {
     int num{1};
     std::vector<die> of{};
     std::vector<mod> modifier{+0};
   };
-    
+
   std::ostream & operator<<(std::ostream & out, dice const & d);
   std::istream & operator>>(std::istream & out, dice & d);
 }

+ 2 - 1
include/exception.h

@@ -20,7 +20,7 @@ namespace dice {
      * or -1(EOF) if the error was a 'we ran out of buffer' type.
      */
     unexpected_token(std::string const & reason, long long position);
-    
+
     /**
      * @brief Create a string matching the regex '~*^', that points to the error
      * covered by this exception class.
@@ -31,6 +31,7 @@ namespace dice {
      * "<END>" if position and backup_length are both -1
      */
     std::string pointer(long long backup_length = -1) const;
+
   private:
     long long position;
   };

+ 1 - 0
include/random.h

@@ -13,6 +13,7 @@
 namespace dice { namespace engine {
   class random : private ::engine::random_number_generator {
     using super = ::engine::random_number_generator;
+
   public:
     using super::random_number_generator;
     /**

+ 1 - 1
include/roll.h

@@ -23,7 +23,7 @@ namespace dice {
     // of rolled is within the integer range [1, M].
     std::vector<int> rolled;
   };
-  
+
   // Describe the actual result of rolling an arbitrary set of dice with mods
   struct dice_roll {
     // Collapse this roll into its actual value

+ 55 - 54
src/die.cxx

@@ -14,7 +14,9 @@
 
 static void advance_over_whitespace(std::istream & in, char const * also = "") {
   if (strchr(also, in.peek())) { in.get(); }
-  while (isspace(in.peek())) { in.get(); }
+  while (isspace(in.peek())) {
+    in.get();
+  }
 }
 
 static std::string carrot(long long pos) {
@@ -27,34 +29,29 @@ static std::string carrot(long long pos) {
 }
 
 namespace dice {
-  int sgn(sign s) {
-    return s == MINUS ? -1 : 1;
-  }
+  int sgn(sign s) { return s == MINUS ? -1 : 1; }
   std::string str(sign s) {
     switch (s) {
-      case PLUS:  return "+";
-      case MINUS: return "-";
-      default:    return "";
+    case PLUS:
+      return "+";
+    case MINUS:
+      return "-";
+    default:
+      return "";
     }
   }
-  
-  mod::operator int() const {
-    return sgn(sign) * value;
-  }
+
+  mod::operator int() const { return sgn(sign) * value; }
 }
 
 namespace dice {
-  unexpected_token::unexpected_token(std::string const & reason,
-                                     long long pos) :
-    std::runtime_error(reason),
-    position(pos) {
-    
-  }
-  
+  unexpected_token::unexpected_token(std::string const & reason, long long pos)
+      : std::runtime_error(reason), position(pos) {}
+
   std::string unexpected_token::pointer(long long backup_length) const {
     return carrot(position == -1 ? backup_length : position);
   }
-    
+
   std::ostream & operator<<(std::ostream & out, dice const & d) {
     if (d.num != 1) out << d.num << '{';
     for (die const & di : d.of) {
@@ -73,11 +70,11 @@ namespace dice { namespace {
     void parse(sign s);
     void parse_dN(sign s, int value);
     void parse_const(sign s, int value);
-    
+
     std::istream & in;
     dice & d;
   };
-  
+
   /**
    * @sideeffect This function advances the input stream over a single numeric
    * token. This token represents the number of sides in the die roll.
@@ -102,7 +99,7 @@ namespace dice { namespace {
     in >> d.of.back().sides;
     parse(ZERO);
   }
-  
+
   /**
    * @param s The arithmatic sign attached to this numeric constant. Because
    * value is non-negative, this token contains the +/- effect.
@@ -114,7 +111,7 @@ namespace dice { namespace {
       d.modifier.push_back({s, std::abs(value)});
     }
   }
-  
+
   /**
    * Main dispatch function for parsing a dice roll.
    * @param s The current +/- sign attached to the parse sequence. s is ZERO
@@ -134,23 +131,26 @@ namespace dice { namespace {
     // By defaulting this to zero, we can write a more elegant handling of
     // expressions like 1d4+1d6+5+1
     int value = 0;
-    if (isnumber(in.peek())) { in >> value; }
-    else if (in.peek() == EOF && s != ZERO) {
+    if (isnumber(in.peek())) {
+      in >> value;
+    } else if (in.peek() == EOF && s != ZERO) {
       throw unexpected_token("Unexpected EOF while parsing", -1);
     }
     advance_over_whitespace(in);
     switch (in.peek()) {
-      case 'd': case 'D':
-        return parse_dN(s, value);
-      case '+': case '-':
-        // Handle 5+... cases
-        parse_const(s, value);
-        // Add another token
-        parse((in.get() == '+') ? PLUS : MINUS);
-        break;
-      default:
-        parse_const(s, value);
-        break;
+    case 'd':
+    case 'D':
+      return parse_dN(s, value);
+    case '+':
+    case '-':
+      // Handle 5+... cases
+      parse_const(s, value);
+      // Add another token
+      parse((in.get() == '+') ? PLUS : MINUS);
+      break;
+    default:
+      parse_const(s, value);
+      break;
     }
   }
 }}
@@ -161,24 +161,25 @@ namespace dice {
     advance_over_whitespace(in);
     if (isnumber(in.peek())) { in >> value; }
     switch (in.peek()) {
-      case 'd': case 'D':
-        parser{in, d}.parse(ZERO);
-        d.of.front().num = value;
-        break;
-      case '{':
-        in.get();
-        d.num = value;
-        parser{in, d}.parse(ZERO);
-        if (in.get() != '}') {
-          throw unexpected_token("Expected closing '}' in repeated roll",
-                                 in.tellg());
-        }
-        break;
-      case EOF:
-        throw unexpected_token("No dice in expression", in.tellg());
-      default:
-        throw unexpected_token("Unexpected token", in.tellg());
+    case 'd':
+    case 'D':
+      parser{in, d}.parse(ZERO);
+      d.of.front().num = value;
+      break;
+    case '{':
+      in.get();
+      d.num = value;
+      parser{in, d}.parse(ZERO);
+      if (in.get() != '}') {
+        throw unexpected_token("Expected closing '}' in repeated roll",
+                               in.tellg());
+      }
+      break;
+    case EOF:
+      throw unexpected_token("No dice in expression", in.tellg());
+    default:
+      throw unexpected_token("Unexpected token", in.tellg());
     }
-   return in;
+    return in;
   }
 }

+ 1 - 1
src/main.cxx

@@ -57,7 +57,7 @@ void eval(std::string const & str) {
     print(rs);
   } catch (dice::unexpected_token const & ut) {
     std::cerr << "Error in roll: '" << str << "': " << ut.what() << "\n";
-    std::cerr << "                " << ut.pointer(str.size()+1) << std::endl;
+    std::cerr << "                " << ut.pointer(str.size() + 1) << std::endl;
   }
 }
 

+ 20 - 18
src/roll.cxx

@@ -21,27 +21,29 @@ namespace dice {
 
   dice_roll::operator int() const {
     return std::accumulate(sub_rolls.begin(), sub_rolls.end(), 0) +
-        std::accumulate(modifiers.begin(), modifiers.end(), 0);
+           std::accumulate(modifiers.begin(), modifiers.end(), 0);
   }
-  
+
   std::ostream & operator<<(std::ostream & out, die_roll const & r) {
     out << str(r.sign);
     switch (r.rolled.size()) {
-        // Prevent crashes if we somehow get a 0dM expression
-      case 0: return out << "0";
-        // Don't bother with braces if there's only a single roll,
-        // the braces are for grouping purposes.
-      case 1: return out << r.rolled[0];
-      default:
-        out << "[ ";
-        out << r.rolled[0];
-        for (int i = 1; i < r.rolled.size(); ++i) {
-          out << ", " << r.rolled[i];
-        }
-        return out << " ]";
+      // Prevent crashes if we somehow get a 0dM expression
+    case 0:
+      return out << "0";
+      // Don't bother with braces if there's only a single roll,
+      // the braces are for grouping purposes.
+    case 1:
+      return out << r.rolled[0];
+    default:
+      out << "[ ";
+      out << r.rolled[0];
+      for (int i = 1; i < r.rolled.size(); ++i) {
+        out << ", " << r.rolled[i];
+      }
+      return out << " ]";
     }
   }
-  
+
   std::ostream & operator<<(std::ostream & out, dice_roll const & r) {
     for (die_roll const & dr : r.sub_rolls) {
       out << dr;
@@ -51,7 +53,7 @@ namespace dice {
     }
     return out;
   }
-  
+
   die_roll roll_impl(die const & d, engine::random & gen) {
     std::vector<int> hits;
     for (int i = 0; i < d.num; ++i) {
@@ -59,7 +61,7 @@ namespace dice {
     }
     return {d.sgn, hits};
   }
-  
+
   dice_roll roll_impl(dice const & d, engine::random & gen) {
     std::vector<die_roll> hits;
     for (die const & di : d.of) {
@@ -67,7 +69,7 @@ namespace dice {
     }
     return {hits, d.modifier};
   }
-  
+
   std::vector<dice_roll> roll(dice const & d) {
     std::vector<dice_roll> out;
     engine::random gen;