Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1 | //===--- UnwrappedLineParser.cpp - Format C++ code ------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | /// |
| 10 | /// \file |
| 11 | /// \brief This file contains the implementation of the UnwrappedLineParser, |
| 12 | /// which turns a stream of tokens into UnwrappedLines. |
| 13 | /// |
| 14 | /// This is EXPERIMENTAL code under heavy development. It is not in a state yet, |
| 15 | /// where it can be used to format real code. |
| 16 | /// |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | |
| 19 | #include "UnwrappedLineParser.h" |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
| 21 | |
| 22 | namespace clang { |
| 23 | namespace format { |
| 24 | |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 25 | UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style, |
| 26 | FormatTokenSource &Tokens, |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 27 | UnwrappedLineConsumer &Callback) |
Daniel Jasper | d7610b8 | 2012-12-24 16:51:15 +0000 | [diff] [blame^] | 28 | : Style(Style), Tokens(Tokens), Callback(Callback) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 29 | } |
| 30 | |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 31 | bool UnwrappedLineParser::parse() { |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 32 | FormatTok = Tokens.getNextToken(); |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 33 | return parseLevel(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 36 | bool UnwrappedLineParser::parseLevel() { |
| 37 | bool Error = false; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 38 | do { |
| 39 | switch (FormatTok.Tok.getKind()) { |
| 40 | case tok::hash: |
| 41 | parsePPDirective(); |
| 42 | break; |
| 43 | case tok::comment: |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 44 | nextToken(); |
| 45 | addUnwrappedLine(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 46 | break; |
| 47 | case tok::l_brace: |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 48 | Error |= parseBlock(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 49 | addUnwrappedLine(); |
| 50 | break; |
| 51 | case tok::r_brace: |
Alexander Kornienko | a3a2b3a | 2012-12-06 17:49:17 +0000 | [diff] [blame] | 52 | // Stray '}' is an error. |
| 53 | return true; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 54 | default: |
| 55 | parseStatement(); |
| 56 | break; |
| 57 | } |
| 58 | } while (!eof()); |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 59 | return Error; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 62 | bool UnwrappedLineParser::parseBlock(unsigned AddLevels) { |
Alexander Kornienko | a3a2b3a | 2012-12-06 17:49:17 +0000 | [diff] [blame] | 63 | assert(FormatTok.Tok.is(tok::l_brace) && "'{' expected"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 64 | nextToken(); |
| 65 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 66 | addUnwrappedLine(); |
| 67 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 68 | Line.Level += AddLevels; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 69 | parseLevel(); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 70 | Line.Level -= AddLevels; |
| 71 | |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 72 | // FIXME: Add error handling. |
| 73 | if (!FormatTok.Tok.is(tok::r_brace)) |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 74 | return true; |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 75 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 76 | nextToken(); |
| 77 | if (FormatTok.Tok.is(tok::semi)) |
| 78 | nextToken(); |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 79 | return false; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | void UnwrappedLineParser::parsePPDirective() { |
| 83 | while (!eof()) { |
| 84 | nextToken(); |
| 85 | if (FormatTok.NewlinesBefore > 0) { |
| 86 | addUnwrappedLine(); |
| 87 | return; |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 92 | void UnwrappedLineParser::parseComments() { |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 93 | // Consume leading line comments, e.g. for branches without compounds. |
| 94 | while (FormatTok.Tok.is(tok::comment)) { |
| 95 | nextToken(); |
| 96 | addUnwrappedLine(); |
| 97 | } |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | void UnwrappedLineParser::parseStatement() { |
| 101 | parseComments(); |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 102 | |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 103 | switch (FormatTok.Tok.getKind()) { |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 104 | case tok::kw_namespace: |
| 105 | parseNamespace(); |
| 106 | return; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 107 | case tok::kw_public: |
| 108 | case tok::kw_protected: |
| 109 | case tok::kw_private: |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 110 | parseAccessSpecifier(); |
| 111 | return; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 112 | case tok::kw_if: |
| 113 | parseIfThenElse(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 114 | return; |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 115 | case tok::kw_for: |
| 116 | case tok::kw_while: |
| 117 | parseForOrWhileLoop(); |
| 118 | return; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 119 | case tok::kw_do: |
| 120 | parseDoWhile(); |
| 121 | return; |
| 122 | case tok::kw_switch: |
| 123 | parseSwitch(); |
| 124 | return; |
| 125 | case tok::kw_default: |
| 126 | nextToken(); |
| 127 | parseLabel(); |
| 128 | return; |
| 129 | case tok::kw_case: |
| 130 | parseCaseLabel(); |
| 131 | return; |
| 132 | default: |
| 133 | break; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 134 | } |
| 135 | int TokenNumber = 0; |
| 136 | do { |
| 137 | ++TokenNumber; |
| 138 | switch (FormatTok.Tok.getKind()) { |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 139 | case tok::kw_enum: |
| 140 | parseEnum(); |
| 141 | return; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 142 | case tok::semi: |
| 143 | nextToken(); |
| 144 | addUnwrappedLine(); |
| 145 | return; |
| 146 | case tok::l_paren: |
| 147 | parseParens(); |
| 148 | break; |
| 149 | case tok::l_brace: |
| 150 | parseBlock(); |
| 151 | addUnwrappedLine(); |
| 152 | return; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 153 | case tok::identifier: |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 154 | nextToken(); |
| 155 | if (TokenNumber == 1 && FormatTok.Tok.is(tok::colon)) { |
| 156 | parseLabel(); |
| 157 | return; |
| 158 | } |
| 159 | break; |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 160 | case tok::equal: |
| 161 | nextToken(); |
| 162 | // Skip initializers as they will be formatted by a later step. |
| 163 | if (FormatTok.Tok.is(tok::l_brace)) |
| 164 | nextToken(); |
| 165 | break; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 166 | default: |
| 167 | nextToken(); |
| 168 | break; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 169 | } |
| 170 | } while (!eof()); |
| 171 | } |
| 172 | |
| 173 | void UnwrappedLineParser::parseParens() { |
| 174 | assert(FormatTok.Tok.is(tok::l_paren) && "'(' expected."); |
| 175 | nextToken(); |
| 176 | do { |
| 177 | switch (FormatTok.Tok.getKind()) { |
| 178 | case tok::l_paren: |
| 179 | parseParens(); |
| 180 | break; |
| 181 | case tok::r_paren: |
| 182 | nextToken(); |
| 183 | return; |
| 184 | default: |
| 185 | nextToken(); |
| 186 | break; |
| 187 | } |
| 188 | } while (!eof()); |
| 189 | } |
| 190 | |
| 191 | void UnwrappedLineParser::parseIfThenElse() { |
| 192 | assert(FormatTok.Tok.is(tok::kw_if) && "'if' expected"); |
| 193 | nextToken(); |
| 194 | parseParens(); |
| 195 | bool NeedsUnwrappedLine = false; |
| 196 | if (FormatTok.Tok.is(tok::l_brace)) { |
| 197 | parseBlock(); |
| 198 | NeedsUnwrappedLine = true; |
| 199 | } else { |
| 200 | addUnwrappedLine(); |
| 201 | ++Line.Level; |
| 202 | parseStatement(); |
| 203 | --Line.Level; |
| 204 | } |
| 205 | if (FormatTok.Tok.is(tok::kw_else)) { |
| 206 | nextToken(); |
| 207 | if (FormatTok.Tok.is(tok::l_brace)) { |
| 208 | parseBlock(); |
| 209 | addUnwrappedLine(); |
| 210 | } else if (FormatTok.Tok.is(tok::kw_if)) { |
| 211 | parseIfThenElse(); |
| 212 | } else { |
| 213 | addUnwrappedLine(); |
| 214 | ++Line.Level; |
| 215 | parseStatement(); |
| 216 | --Line.Level; |
| 217 | } |
| 218 | } else if (NeedsUnwrappedLine) { |
| 219 | addUnwrappedLine(); |
| 220 | } |
| 221 | } |
| 222 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 223 | void UnwrappedLineParser::parseNamespace() { |
| 224 | assert(FormatTok.Tok.is(tok::kw_namespace) && "'namespace' expected"); |
| 225 | nextToken(); |
| 226 | if (FormatTok.Tok.is(tok::identifier)) |
| 227 | nextToken(); |
| 228 | if (FormatTok.Tok.is(tok::l_brace)) { |
| 229 | parseBlock(0); |
| 230 | addUnwrappedLine(); |
| 231 | } |
| 232 | // FIXME: Add error handling. |
| 233 | } |
| 234 | |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 235 | void UnwrappedLineParser::parseForOrWhileLoop() { |
| 236 | assert((FormatTok.Tok.is(tok::kw_for) || FormatTok.Tok.is(tok::kw_while)) && |
| 237 | "'for' or 'while' expected"); |
| 238 | nextToken(); |
| 239 | parseParens(); |
| 240 | if (FormatTok.Tok.is(tok::l_brace)) { |
| 241 | parseBlock(); |
| 242 | addUnwrappedLine(); |
| 243 | } else { |
| 244 | addUnwrappedLine(); |
| 245 | ++Line.Level; |
| 246 | parseStatement(); |
| 247 | --Line.Level; |
| 248 | } |
| 249 | } |
| 250 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 251 | void UnwrappedLineParser::parseDoWhile() { |
| 252 | assert(FormatTok.Tok.is(tok::kw_do) && "'do' expected"); |
| 253 | nextToken(); |
| 254 | if (FormatTok.Tok.is(tok::l_brace)) { |
| 255 | parseBlock(); |
| 256 | } else { |
| 257 | addUnwrappedLine(); |
| 258 | ++Line.Level; |
| 259 | parseStatement(); |
| 260 | --Line.Level; |
| 261 | } |
| 262 | |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 263 | // FIXME: Add error handling. |
| 264 | if (!FormatTok.Tok.is(tok::kw_while)) { |
| 265 | addUnwrappedLine(); |
| 266 | return; |
| 267 | } |
| 268 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 269 | nextToken(); |
| 270 | parseStatement(); |
| 271 | } |
| 272 | |
| 273 | void UnwrappedLineParser::parseLabel() { |
| 274 | // FIXME: remove all asserts. |
| 275 | assert(FormatTok.Tok.is(tok::colon) && "':' expected"); |
| 276 | nextToken(); |
| 277 | unsigned OldLineLevel = Line.Level; |
| 278 | if (Line.Level > 0) |
| 279 | --Line.Level; |
| 280 | if (FormatTok.Tok.is(tok::l_brace)) { |
| 281 | parseBlock(); |
| 282 | } |
| 283 | addUnwrappedLine(); |
| 284 | Line.Level = OldLineLevel; |
| 285 | } |
| 286 | |
| 287 | void UnwrappedLineParser::parseCaseLabel() { |
| 288 | assert(FormatTok.Tok.is(tok::kw_case) && "'case' expected"); |
| 289 | // FIXME: fix handling of complex expressions here. |
| 290 | do { |
| 291 | nextToken(); |
| 292 | } while (!eof() && !FormatTok.Tok.is(tok::colon)); |
| 293 | parseLabel(); |
| 294 | } |
| 295 | |
| 296 | void UnwrappedLineParser::parseSwitch() { |
| 297 | assert(FormatTok.Tok.is(tok::kw_switch) && "'switch' expected"); |
| 298 | nextToken(); |
| 299 | parseParens(); |
| 300 | if (FormatTok.Tok.is(tok::l_brace)) { |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 301 | parseBlock(Style.IndentCaseLabels ? 2 : 1); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 302 | addUnwrappedLine(); |
| 303 | } else { |
| 304 | addUnwrappedLine(); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 305 | Line.Level += (Style.IndentCaseLabels ? 2 : 1); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 306 | parseStatement(); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 307 | Line.Level -= (Style.IndentCaseLabels ? 2 : 1); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 308 | } |
| 309 | } |
| 310 | |
| 311 | void UnwrappedLineParser::parseAccessSpecifier() { |
| 312 | nextToken(); |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 313 | // Otherwise, we don't know what it is, and we'd better keep the next token. |
| 314 | if (FormatTok.Tok.is(tok::colon)) |
| 315 | nextToken(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 316 | addUnwrappedLine(); |
| 317 | } |
| 318 | |
| 319 | void UnwrappedLineParser::parseEnum() { |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 320 | bool HasContents = false; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 321 | do { |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 322 | switch (FormatTok.Tok.getKind()) { |
| 323 | case tok::l_brace: |
| 324 | nextToken(); |
| 325 | addUnwrappedLine(); |
| 326 | ++Line.Level; |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 327 | parseComments(); |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 328 | break; |
| 329 | case tok::l_paren: |
| 330 | parseParens(); |
| 331 | break; |
| 332 | case tok::comma: |
| 333 | nextToken(); |
| 334 | addUnwrappedLine(); |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 335 | parseComments(); |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 336 | break; |
| 337 | case tok::r_brace: |
| 338 | if (HasContents) |
| 339 | addUnwrappedLine(); |
| 340 | --Line.Level; |
| 341 | nextToken(); |
| 342 | break; |
| 343 | case tok::semi: |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 344 | nextToken(); |
| 345 | addUnwrappedLine(); |
| 346 | return; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 347 | default: |
| 348 | HasContents = true; |
| 349 | nextToken(); |
| 350 | break; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 351 | } |
| 352 | } while (!eof()); |
| 353 | } |
| 354 | |
| 355 | void UnwrappedLineParser::addUnwrappedLine() { |
| 356 | // Consume trailing comments. |
| 357 | while (!eof() && FormatTok.NewlinesBefore == 0 && |
| 358 | FormatTok.Tok.is(tok::comment)) { |
| 359 | nextToken(); |
| 360 | } |
Alexander Kornienko | 720ffb6 | 2012-12-05 13:56:52 +0000 | [diff] [blame] | 361 | Callback.consumeUnwrappedLine(Line); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 362 | Line.Tokens.clear(); |
| 363 | } |
| 364 | |
| 365 | bool UnwrappedLineParser::eof() const { |
| 366 | return FormatTok.Tok.is(tok::eof); |
| 367 | } |
| 368 | |
| 369 | void UnwrappedLineParser::nextToken() { |
| 370 | if (eof()) |
| 371 | return; |
| 372 | Line.Tokens.push_back(FormatTok); |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 373 | FormatTok = Tokens.getNextToken(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | } // end namespace format |
| 377 | } // end namespace clang |