formatter/formatter.spice
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // Copyright (c) 2021-2026 ChilliBits. All rights reserved. | ||
| 2 | |||
| 3 | // Std imports | ||
| 4 | import "std/data/vector"; | ||
| 5 | import "std/io/filepath"; | ||
| 6 | import "std/text/analysis"; | ||
| 7 | |||
| 8 | // Own imports | ||
| 9 | import "@/ast/ast-nodes"; | ||
| 10 | import "@/driver"; | ||
| 11 | import "@/exception/compiler-error"; | ||
| 12 | import "@/exception/fatal-error"; | ||
| 13 | import "@/formatter/format-config"; | ||
| 14 | import "@/global/global-resource-manager"; | ||
| 15 | import "@/lexer/lexer"; | ||
| 16 | import "@/lexer/token"; | ||
| 17 | import "@/parser/parser"; | ||
| 18 | import "@/source-file"; | ||
| 19 | |||
| 20 | /** | ||
| 21 | * Kind of a bracket pair. The formatter tracks the open bracket pairs to compute the indentation of the lines | ||
| 22 | */ | ||
| 23 | type FrameKind enum { | ||
| 24 | BLOCK, // Braces of a block or a type body, which contain statements or similar units | ||
| 25 | LIST // Parentheses, square brackets and the braces of a struct instantiation, which contain a list of items | ||
| 26 | } | ||
| 27 | |||
| 28 | /** | ||
| 29 | * Open bracket pair, whose content is indented relative to the line of the opening bracket | ||
| 30 | */ | ||
| 31 | type Frame struct { | ||
| 32 | FrameKind kind | ||
| 33 | long openerIdx // Index of the opening token. -1 for the frame of the whole file | ||
| 34 | unsigned long closerCol // Indentation of a line, that starts with the closing token | ||
| 35 | unsigned long baseCol // Indentation of the lines within the bracket pair | ||
| 36 | unsigned long unitCol // Blocks: Indentation of the line, where the current statement started | ||
| 37 | // Lists, whose first item is on the line of the opening bracket: Column of the first item. Otherwise -1. The items in the | ||
| 38 | // following lines are either aligned to the first item or indented by one level, depending on what the original code does | ||
| 39 | long alignCol | ||
| 40 | bool alignDecided // Whether it is decided if the items are aligned to the first one | ||
| 41 | bool isAligned // Whether the items are aligned to the first one | ||
| 42 | } | ||
| 43 | |||
| 44 | /** | ||
| 45 | * Token with all information the formatter needs about it | ||
| 46 | */ | ||
| 47 | 9376 | type FmtToken struct { | |
| 48 | TokenType tokenType | ||
| 49 | String text // Source code of the token. Line comments without the line break at the end | ||
| 50 | unsigned long startOffset // Offset of the first char within the original source code | ||
| 51 | unsigned long endOffset // Offset after the last char within the original source code | ||
| 52 | unsigned long startLine // Line of the first char in the original source code | ||
| 53 | unsigned long endLine // Line of the last char in the original source code | ||
| 54 | unsigned long origStartCol // Column of the first char within its line in the original source code, starting at 0 | ||
| 55 | unsigned long origEndCol // Column after the last char within its line in the original source code | ||
| 56 | ASTNodeKind ownerKind // Kind of the innermost syntax tree node, that covers the token | ||
| 57 | bool isUnitStart // Whether the token starts a statement, a top level definition, a field or a similar unit | ||
| 58 | unsigned long lineBreaksBefore // Number of line breaks before the token in the formatted source code | ||
| 59 | } | ||
| 60 | |||
| 61 | /** | ||
| 62 | * The source formatter brings Spice source code into the canonical layout. It only changes whitespace: | ||
| 63 | * | ||
| 64 | * - Indentation with four spaces per level (configurable), computed from the bracket pairs. Continuation lines of a statement get one more | ||
| 65 | * level. The items of a list, whose first item is on the line of the opening bracket, stay aligned to the first item, if they | ||
| 66 | * are aligned in the original code. Otherwise, they are indented by one level as well | ||
| 67 | * - Canonical spacing between the tokens of a line, e.g. around binary operators, after commas and keywords | ||
| 68 | * - Opening braces of blocks on the line of their construct (K&R style), 'else' on the line of the preceding '}' | ||
| 69 | * - At most one blank line in a row (configurable), no blank lines at the start or end of a bracket pair | ||
| 70 | * - Comments at the end of a line and aligned declarations, e.g. the names of consecutive fields, keep their original column, | ||
| 71 | * if it can be reached, so that they stay aligned | ||
| 72 | * | ||
| 73 | * Line breaks within statements are kept as written. The formatter works on the token stream of the lexer, which contains | ||
| 74 | * the comments, and uses the syntax tree to tell apart tokens with multiple meanings, e.g. '<' as generic bracket or as | ||
| 75 | * comparison, '*' as pointer type modifier or as multiplication and '{' as block or as struct instantiation. | ||
| 76 | */ | ||
| 77 | 19 | type SourceFormatter struct { | |
| 78 | FormatConfig config | ||
| 79 | Vector<FmtToken> tokens // All tokens, except for the EOF token | ||
| 80 | Vector<long> nextCodeIdx // For each token: Index of the next token, that is no comment. -1 if there is none | ||
| 81 | Vector<long> prevCodeIdx // For each token: Index of the previous token, that is no comment. -1 if there is none | ||
| 82 | } | ||
| 83 | |||
| 84 | /** | ||
| 85 | * Create a formatter for the given tokens of the given source code | ||
| 86 | * | ||
| 87 | * @param sourceCode Source code, that was lexed | ||
| 88 | * @param lexerTokens Tokens of the source code, including the comments and the terminating EOF token | ||
| 89 | * @param config Formatter configuration, e.g. from the manifest | ||
| 90 | */ | ||
| 91 | 19 | p SourceFormatter.ctor(const String& sourceCode, const Vector<Token>& lexerTokens, const FormatConfig& config) { | |
| 92 | 19 | this.config = config; | |
| 93 | // Walk through the source code once to compute the line and column of each token | ||
| 94 | 19 | unsigned long offset = 0l; | |
| 95 | 19 | unsigned long line = 1l; | |
| 96 | 19 | unsigned long col = 0l; | |
| 97 | 19 | const unsigned long tokenCount = lexerTokens.getSize(); | |
| 98 | 2363 | for unsigned long i = 0l; i < tokenCount; i++ { | |
| 99 | 2363 | const Token& lexerToken = lexerTokens.get(i); | |
| 100 | 2363 | if lexerToken.tokenType == TokenType::EOF { | |
| 101 | 19 | break; | |
| 102 | } | ||
| 103 | |||
| 104 | 2344 | FmtToken token; | |
| 105 | 2344 | token.tokenType = lexerToken.tokenType; | |
| 106 | 2344 | token.startOffset = lexerToken.startOffset; | |
| 107 | 2344 | token.endOffset = lexerToken.endOffset; | |
| 108 | 2344 | token.ownerKind = ASTNodeKind::INVALID; | |
| 109 | 2344 | token.isUnitStart = false; | |
| 110 | 2344 | token.lineBreaksBefore = 0l; | |
| 111 | 2344 | String text = sourceCode.getSubstring(lexerToken.startOffset, cast<long>(lexerToken.endOffset - lexerToken.startOffset)); | |
| 112 | 2344 | if lexerToken.tokenType == TokenType::LINE_COMMENT { | |
| 113 | 47 | text = trimEnd(text); | |
| 114 | 47 | } | |
| 115 | 2344 | token.text = text; | |
| 116 | |||
| 117 | // Start of the token | ||
| 118 | 5015 | while offset < lexerToken.startOffset { | |
| 119 | 2671 | advancePosition(sourceCode[offset], line, col); | |
| 120 | 2671 | offset++; | |
| 121 | 2671 | } | |
| 122 | 2344 | token.startLine = line; | |
| 123 | 2344 | token.origStartCol = col; | |
| 124 | |||
| 125 | // End of the token | ||
| 126 | 2344 | const unsigned long textEndOffset = lexerToken.startOffset + text.getLength(); | |
| 127 | 10974 | while offset < textEndOffset { | |
| 128 | 8630 | advancePosition(sourceCode[offset], line, col); | |
| 129 | 8630 | offset++; | |
| 130 | 8630 | } | |
| 131 | 2344 | token.endLine = line; | |
| 132 | 2344 | token.origEndCol = col; | |
| 133 | |||
| 134 | 2344 | this.tokens.pushBack(token); | |
| 135 | 2344 | } | |
| 136 | |||
| 137 | // Index the neighboring code tokens of each token once, so that runs of comments are not scanned over and over | ||
| 138 | 19 | const unsigned long count = this.tokens.getSize(); | |
| 139 | 19 | long prevCode = -1l; | |
| 140 | 2363 | for unsigned long i = 0l; i < count; i++ { | |
| 141 | 2344 | this.prevCodeIdx.pushBack(prevCode); | |
| 142 | 2344 | this.nextCodeIdx.pushBack(-1l); | |
| 143 | 2344 | if !isComment(this.tokens.get(i).tokenType) { | |
| 144 | 2279 | prevCode = cast<long>(i); | |
| 145 | 2279 | } | |
| 146 | 2344 | } | |
| 147 | 19 | long nextCode = -1l; | |
| 148 | 2363 | for long i = cast<long>(count) - 1l; i >= 0l; i-- { | |
| 149 | 2344 | long& nextCodeOfToken = this.nextCodeIdx.get(cast<unsigned long>(i)); | |
| 150 | 2344 | nextCodeOfToken = nextCode; | |
| 151 | 2344 | if !isComment(this.tokens.get(cast<unsigned long>(i)).tokenType) { | |
| 152 | 2279 | nextCode = i; | |
| 153 | 2279 | } | |
| 154 | 2344 | } | |
| 155 | 19 | } | |
| 156 | |||
| 157 | /** | ||
| 158 | * Advance the given line and column by the given char | ||
| 159 | * | ||
| 160 | * @param c Char to advance by | ||
| 161 | * @param line Line to update | ||
| 162 | * @param col Column to update | ||
| 163 | */ | ||
| 164 | 11301 | p advancePosition(char c, unsigned long& line, unsigned long& col) { | |
| 165 | 11301 | if c == '\n' { | |
| 166 | 537 | line++; | |
| 167 | 537 | col = 0l; | |
| 168 | 11301 | } else if !isUtf8ContinuationByte(c) { | |
| 169 | // Count code points instead of bytes | ||
| 170 | 10764 | col++; | |
| 171 | 10764 | } | |
| 172 | 11301 | } | |
| 173 | |||
| 174 | /** | ||
| 175 | * Check if the given char is a continuation byte of a multibyte UTF-8 sequence (0b10xxxxxx) | ||
| 176 | * | ||
| 177 | * @param c Char to check | ||
| 178 | * @return Continuation byte or not | ||
| 179 | */ | ||
| 180 | 19322 | f<bool> isUtf8ContinuationByte(char c) { | |
| 181 | 19322 | const int byteValue = cast<int>(cast<byte>(c)); | |
| 182 | 19322 | return (byteValue & 0xC0) == 0x80; | |
| 183 | } | ||
| 184 | |||
| 185 | /** | ||
| 186 | * Get the number of code points of the given UTF-8 string | ||
| 187 | * | ||
| 188 | * @param text Text to measure | ||
| 189 | * @return Number of code points | ||
| 190 | */ | ||
| 191 | 2380 | f<unsigned long> getCodePointCount(const String& text) { | |
| 192 | 2380 | unsigned long count = 0l; | |
| 193 | 2380 | const unsigned long length = text.getLength(); | |
| 194 | 10938 | for unsigned long i = 0l; i < length; i++ { | |
| 195 | 8558 | if !isUtf8ContinuationByte(text[i]) { | |
| 196 | 8558 | count++; | |
| 197 | 8558 | } | |
| 198 | 8558 | } | |
| 199 | 2380 | return count; | |
| 200 | } | ||
| 201 | |||
| 202 | /** | ||
| 203 | * Remove the whitespace at the end of the given text | ||
| 204 | * | ||
| 205 | * @param text Text to trim | ||
| 206 | * @return Trimmed text | ||
| 207 | */ | ||
| 208 | 93 | f<String> trimEnd(const String& text) { | |
| 209 | 93 | unsigned long length = text.getLength(); | |
| 210 | 140 | while length > 0l && isWhitespace(text[length - 1l]) { | |
| 211 | 47 | length--; | |
| 212 | 47 | } | |
| 213 | 93 | return text.getSubstring(0ul, cast<long>(length)); | |
| 214 | } | ||
| 215 | |||
| 216 | // ====================================================== Syntax tree analysis ====================================================== | ||
| 217 | |||
| 218 | /** | ||
| 219 | * Attach the information from the syntax tree to the tokens: the kind of the innermost node, that covers each token, and | ||
| 220 | * whether a token starts a statement or a similar unit | ||
| 221 | * | ||
| 222 | * @param node Root node of the syntax tree | ||
| 223 | */ | ||
| 224 | 2007 | p SourceFormatter.analyzeSyntaxTree(ASTNode* node) { | |
| 225 | 2007 | const long start = node.codeLoc.sourceIntervalStart; | |
| 226 | 2007 | const long stop = node.codeLoc.sourceIntervalStop; | |
| 227 | 2007 | if start >= 0l && stop >= start { | |
| 228 | 2007 | const long firstIdx = this.findFirstTokenAtOrAfter(cast<unsigned long>(start)); | |
| 229 | 2007 | const long lastIdx = this.findLastTokenAtOrBefore(cast<unsigned long>(stop)); | |
| 230 | 2007 | if firstIdx >= 0l && lastIdx >= firstIdx { | |
| 231 | // The children are visited afterwards, so they override the kind for the tokens they cover | ||
| 232 | 14804 | for long i = firstIdx; i <= lastIdx; i++ { | |
| 233 | 12797 | this.tokens.get(cast<unsigned long>(i)).ownerKind = node.kind; | |
| 234 | 12797 | } | |
| 235 | 2007 | if isUnitNode(node) { | |
| 236 | 299 | this.tokens.get(cast<unsigned long>(firstIdx)).isUnitStart = true; | |
| 237 | 299 | } | |
| 238 | // The definition after an attribute like '#[test]' starts in the next line | ||
| 239 | 2007 | if node.kind == ASTNodeKind::TOP_LEVEL_DEF_ATTR { | |
| 240 | 2 | const long nextIdx = this.findNextCodeToken(lastIdx); | |
| 241 | 2 | if nextIdx >= 0l { | |
| 242 | 2 | this.tokens.get(cast<unsigned long>(nextIdx)).isUnitStart = true; | |
| 243 | 2 | } | |
| 244 | 2 | } | |
| 245 | 2007 | } | |
| 246 | 2007 | } | |
| 247 | |||
| 248 | 3995 | foreach ASTNode* child : node.children { | |
| 249 | 1988 | this.analyzeSyntaxTree(child); | |
| 250 | 1988 | } | |
| 251 | 2007 | } | |
| 252 | |||
| 253 | /** | ||
| 254 | * Check if the given node is a statement or a similar unit, whose first token starts a new line, that is not a continuation | ||
| 255 | * | ||
| 256 | * @param node Node to check | ||
| 257 | * @return Unit or not | ||
| 258 | */ | ||
| 259 | 2007 | f<bool> isUnitNode(ASTNode* node) { | |
| 260 | 2007 | switch node.kind { | |
| 261 | case ASTNodeKind::FIELD, ASTNodeKind::SIGNATURE, ASTNodeKind::ENUM_ITEM, ASTNodeKind::CASE_BRANCH, ASTNodeKind::DEFAULT_BRANCH: { | ||
| 262 | 44 | return true; | |
| 263 | } | ||
| 264 | } | ||
| 265 | 1963 | ASTNode* parent = node.parent; | |
| 266 | 1963 | return parent != nil<ASTNode*> && (parent.kind == ASTNodeKind::ENTRY || parent.kind == ASTNodeKind::STMT_LST); | |
| 267 | } | ||
| 268 | |||
| 269 | /** | ||
| 270 | * Find the index of the first token, that starts at or after the given offset | ||
| 271 | * | ||
| 272 | * @param offset Offset within the source code | ||
| 273 | * @return Token index or -1 if there is none | ||
| 274 | */ | ||
| 275 | 2007 | f<long> SourceFormatter.findFirstTokenAtOrAfter(unsigned long offset) { | |
| 276 | 2007 | long low = 0l; | |
| 277 | 2007 | long high = cast<long>(this.tokens.getSize()); | |
| 278 | 17170 | while low < high { | |
| 279 | 15163 | const long mid = (low + high) / 2l; | |
| 280 | 15163 | if this.tokens.get(cast<unsigned long>(mid)).startOffset < offset { | |
| 281 | 7045 | low = mid + 1l; | |
| 282 | 7045 | } else { | |
| 283 | 8118 | high = mid; | |
| 284 | 8118 | } | |
| 285 | 15163 | } | |
| 286 | 2007 | return low < cast<long>(this.tokens.getSize()) ? low : -1l; | |
| 287 | } | ||
| 288 | |||
| 289 | /** | ||
| 290 | * Find the index of the last token, that starts at or before the given offset | ||
| 291 | * | ||
| 292 | * @param offset Offset within the source code | ||
| 293 | * @return Token index or -1 if there is none | ||
| 294 | */ | ||
| 295 | 2007 | f<long> SourceFormatter.findLastTokenAtOrBefore(unsigned long offset) { | |
| 296 | 2007 | long low = 0l; | |
| 297 | 2007 | long high = cast<long>(this.tokens.getSize()); | |
| 298 | 17070 | while low < high { | |
| 299 | 15063 | const long mid = (low + high) / 2l; | |
| 300 | 15063 | if this.tokens.get(cast<unsigned long>(mid)).startOffset <= offset { | |
| 301 | 7608 | low = mid + 1l; | |
| 302 | 7608 | } else { | |
| 303 | 7455 | high = mid; | |
| 304 | 7455 | } | |
| 305 | 15063 | } | |
| 306 | 2007 | return low - 1l; | |
| 307 | } | ||
| 308 | |||
| 309 | /** | ||
| 310 | * Find the next token after the given one, that is no comment | ||
| 311 | * | ||
| 312 | * @param idx Index of the token to start after | ||
| 313 | * @return Token index or -1 if there is none | ||
| 314 | */ | ||
| 315 | 35 | f<long> SourceFormatter.findNextCodeToken(long idx) { | |
| 316 | 35 | return this.nextCodeIdx.get(cast<unsigned long>(idx)); | |
| 317 | } | ||
| 318 | |||
| 319 | /** | ||
| 320 | * Find the previous token before the given one, that is no comment | ||
| 321 | * | ||
| 322 | * @param idx Index of the token to start before | ||
| 323 | * @return Token index or -1 if there is none | ||
| 324 | */ | ||
| 325 | 1487 | f<long> SourceFormatter.findPrevCodeToken(long idx) { | |
| 326 | 1487 | return this.prevCodeIdx.get(cast<unsigned long>(idx)); | |
| 327 | } | ||
| 328 | |||
| 329 | // =========================================================== Formatting =========================================================== | ||
| 330 | |||
| 331 | /** | ||
| 332 | * Format the tokens. The syntax tree has to be analyzed before | ||
| 333 | * | ||
| 334 | * @return Formatted source code | ||
| 335 | */ | ||
| 336 | 19 | f<String> SourceFormatter.format() { | |
| 337 | 19 | this.computeLineBreaks(); | |
| 338 | 19 | return this.render(); | |
| 339 | } | ||
| 340 | |||
| 341 | /** | ||
| 342 | * Compute the number of line breaks before each token. Most line breaks are kept as they are in the original source code | ||
| 343 | */ | ||
| 344 | 19 | p SourceFormatter.computeLineBreaks() { | |
| 345 | 19 | const unsigned long tokenCount = this.tokens.getSize(); | |
| 346 | 2344 | for unsigned long i = 1l; i < tokenCount; i++ { | |
| 347 | 2325 | const FmtToken& prev = this.tokens.get(i - 1l); | |
| 348 | 2325 | FmtToken& token = this.tokens.get(i); | |
| 349 | 2325 | unsigned long lineBreaks = token.startLine - prev.endLine; | |
| 350 | |||
| 351 | // K&R style. A comment in between keeps the line break, because a line comment has to end its line | ||
| 352 | 2325 | if lineBreaks > 0l && !isComment(prev.tokenType) && this.isJoinedWithPrevLine(i) { | |
| 353 | 11 | lineBreaks = 0l; | |
| 354 | 11 | } | |
| 355 | // Collapse multiple blank lines to the maximum number of blank lines. One line break more makes the last blank line | ||
| 356 | 2325 | const unsigned long maxLineBreaks = this.config.maxBlankLines + 1l; | |
| 357 | 2325 | if lineBreaks > maxLineBreaks { | |
| 358 | 10 | lineBreaks = maxLineBreaks; | |
| 359 | 10 | } | |
| 360 | // No blank lines at the start or end of a bracket pair | ||
| 361 | 2325 | if lineBreaks > 1l && (isOpener(prev.tokenType) || isCloser(token.tokenType)) { | |
| 362 | 4 | lineBreaks = 1l; | |
| 363 | 4 | } | |
| 364 | |||
| 365 | 2325 | token.lineBreaksBefore = lineBreaks; | |
| 366 | 2325 | } | |
| 367 | 19 | } | |
| 368 | |||
| 369 | /** | ||
| 370 | * Check if the given token belongs on the line of the previous token: the opening brace of a block, 'else' after the closing | ||
| 371 | * brace of the if branch and 'while' after the closing brace of the body of a do-while loop | ||
| 372 | * | ||
| 373 | * @param idx Index of the token to check | ||
| 374 | * @return Joined with the previous line or not | ||
| 375 | */ | ||
| 376 | 352 | f<bool> SourceFormatter.isJoinedWithPrevLine(unsigned long idx) { | |
| 377 | 352 | const FmtToken& prev = this.tokens.get(idx - 1l); | |
| 378 | 352 | const FmtToken& token = this.tokens.get(idx); | |
| 379 | 352 | if token.tokenType == TokenType::LBRACE { | |
| 380 | // An anonymous block statement starts on its own line | ||
| 381 | 8 | if !isBlockBrace(token) || token.isUnitStart { | |
| 382 | ✗ | return false; | |
| 383 | } | ||
| 384 | 8 | const bool isAfterBlock = prev.tokenType == TokenType::RBRACE && isBlockBrace(prev); | |
| 385 | 8 | return prev.tokenType != TokenType::SEMICOLON && prev.tokenType != TokenType::LBRACE && !isAfterBlock; | |
| 386 | } | ||
| 387 | 344 | if token.tokenType == TokenType::ELSE { | |
| 388 | 2 | return prev.tokenType == TokenType::RBRACE; | |
| 389 | } | ||
| 390 | 342 | if token.tokenType == TokenType::WHILE && token.ownerKind == ASTNodeKind::DO_WHILE_LOOP { | |
| 391 | 1 | return prev.tokenType == TokenType::RBRACE; | |
| 392 | } | ||
| 393 | 341 | return false; | |
| 394 | } | ||
| 395 | |||
| 396 | /** | ||
| 397 | * Assemble the formatted source code. The indentation of each line is computed on the way, from the open bracket pairs | ||
| 398 | * | ||
| 399 | * @return Formatted source code | ||
| 400 | */ | ||
| 401 | 19 | f<String> SourceFormatter.render() { | |
| 402 | 19 | Vector<Frame> frames; | |
| 403 | 19 | const unsigned long indentWidth = this.config.indentWidth; | |
| 404 | 19 | Frame fileFrame = createFrame(FrameKind::BLOCK, -1l, 0l, indentWidth); | |
| 405 | 19 | fileFrame.baseCol = 0l; | |
| 406 | 19 | fileFrame.unitCol = 0l; | |
| 407 | 19 | frames.pushBack(fileFrame); | |
| 408 | |||
| 409 | 19 | String output; | |
| 410 | 19 | unsigned long col = 0l; // Column in the current line of the output in code points | |
| 411 | 19 | unsigned long lineIndentCol = 0l; // Indentation of the current line of the output | |
| 412 | 19 | const unsigned long tokenCount = this.tokens.getSize(); | |
| 413 | 2363 | for unsigned long i = 0l; i < tokenCount; i++ { | |
| 414 | 2344 | const FmtToken& token = this.tokens.get(i); | |
| 415 | |||
| 416 | // Whitespace before the token | ||
| 417 | 2344 | const bool startsLine = i == 0l || token.lineBreaksBefore > 0l; | |
| 418 | 2344 | if startsLine { | |
| 419 | 879 | for unsigned long lineBreak = 0l; lineBreak < token.lineBreaksBefore; lineBreak++ { | |
| 420 | 464 | output += '\n'; | |
| 421 | 464 | } | |
| 422 | 415 | lineIndentCol = this.computeLineIndentCol(i, frames.back()); | |
| 423 | 415 | appendSpaces(output, lineIndentCol); | |
| 424 | 415 | col = lineIndentCol; | |
| 425 | 415 | } else { | |
| 426 | 1929 | const unsigned long spaces = this.getSpacesBefore(i, col); | |
| 427 | 1929 | appendSpaces(output, spaces); | |
| 428 | 1929 | col += spaces; | |
| 429 | 1929 | } | |
| 430 | |||
| 431 | // Remember the column of the first item of a list, if it is on the line of the opening bracket | ||
| 432 | 2344 | Frame& top = frames.back(); | |
| 433 | 2344 | const bool isFirstItem = cast<long>(i) == top.openerIdx + 1l && !startsLine && token.tokenType != TokenType::LINE_COMMENT; | |
| 434 | 2344 | if top.kind == FrameKind::LIST && isFirstItem { | |
| 435 | 136 | top.alignCol = cast<long>(col); | |
| 436 | 136 | } | |
| 437 | |||
| 438 | // The token itself | ||
| 439 | 2344 | if isComment(token.tokenType) && token.endLine > token.startLine { | |
| 440 | 10 | col = appendMultiLineComment(output, token, col); | |
| 441 | 10 | } else { | |
| 442 | 2334 | output += token.text; | |
| 443 | 2334 | col = advanceColumn(col, token.text); | |
| 444 | 2334 | } | |
| 445 | 2344 | if isComment(token.tokenType) { | |
| 446 | 65 | continue; | |
| 447 | } | ||
| 448 | |||
| 449 | // Remember the indentation of the line, where the current statement started. Blocks, that are opened by the | ||
| 450 | // statement, are indented relative to it, even if the opening brace is on a continuation line | ||
| 451 | 2279 | if top.kind == FrameKind::BLOCK && this.startsUnit(i, top) { | |
| 452 | 403 | top.unitCol = lineIndentCol; | |
| 453 | 403 | } | |
| 454 | |||
| 455 | // Open and close bracket pairs | ||
| 456 | 2279 | const TokenType tokenType = token.tokenType; | |
| 457 | 2279 | if tokenType == TokenType::LBRACE && isBlockBrace(token) { | |
| 458 | 101 | const unsigned long closerCol = top.kind == FrameKind::BLOCK ? top.unitCol : lineIndentCol; | |
| 459 | 101 | frames.pushBack(createFrame(FrameKind::BLOCK, cast<long>(i), closerCol, indentWidth)); | |
| 460 | 2279 | } else if isOpener(tokenType) { | |
| 461 | 138 | frames.pushBack(createFrame(FrameKind::LIST, cast<long>(i), lineIndentCol, indentWidth)); | |
| 462 | 2178 | } else if isCloser(tokenType) && frames.getSize() > 1l { | |
| 463 | 239 | frames.popBack(); | |
| 464 | 2178 | } | |
| 465 | 2279 | } | |
| 466 | |||
| 467 | // End the file with a line break | ||
| 468 | 19 | if tokenCount > 0l { | |
| 469 | 19 | output += '\n'; | |
| 470 | 19 | } | |
| 471 | 38 | return output; | |
| 472 | 19 | } | |
| 473 | |||
| 474 | /** | ||
| 475 | * Create a frame for a bracket pair | ||
| 476 | * | ||
| 477 | * @param kind Kind of the bracket pair | ||
| 478 | * @param openerIdx Index of the opening token | ||
| 479 | * @param closerCol Indentation of a line, that starts with the closing token | ||
| 480 | * @param indentWidth Number of spaces per indentation level | ||
| 481 | * @return Frame | ||
| 482 | */ | ||
| 483 | 258 | f<Frame> createFrame(FrameKind kind, long openerIdx, unsigned long closerCol, unsigned long indentWidth) { | |
| 484 | 258 | Frame frame; | |
| 485 | 258 | frame.kind = kind; | |
| 486 | 258 | frame.openerIdx = openerIdx; | |
| 487 | 258 | frame.closerCol = closerCol; | |
| 488 | 258 | frame.baseCol = closerCol + indentWidth; | |
| 489 | 258 | frame.unitCol = closerCol + indentWidth; | |
| 490 | 258 | frame.alignCol = -1l; | |
| 491 | 258 | frame.alignDecided = false; | |
| 492 | 258 | frame.isAligned = false; | |
| 493 | 258 | return frame; | |
| 494 | } | ||
| 495 | |||
| 496 | /** | ||
| 497 | * Get the indentation of the items of the given bracket pair | ||
| 498 | * | ||
| 499 | * @param frame Bracket pair | ||
| 500 | * @return Indentation | ||
| 501 | */ | ||
| 502 | 27 | f<unsigned long> getContentCol(const Frame& frame) { | |
| 503 | 27 | return frame.alignCol >= 0l && frame.isAligned ? cast<unsigned long>(frame.alignCol) : frame.baseCol; | |
| 504 | } | ||
| 505 | |||
| 506 | /** | ||
| 507 | * Compute the indentation of a line, that starts with the given token | ||
| 508 | * | ||
| 509 | * @param idx Index of the first token of the line | ||
| 510 | * @param top Innermost open bracket pair | ||
| 511 | * @return Indentation | ||
| 512 | */ | ||
| 513 | 415 | f<unsigned long> SourceFormatter.computeLineIndentCol(unsigned long idx, Frame& top) { | |
| 514 | 415 | const FmtToken& token = this.tokens.get(idx); | |
| 515 | 415 | const bool isCommentLine = isComment(token.tokenType); | |
| 516 | |||
| 517 | // A comment is indented like the code after it | ||
| 518 | 415 | long codeIdx = cast<long>(idx); | |
| 519 | 415 | if isCommentLine { | |
| 520 | 33 | codeIdx = this.findNextCodeToken(cast<long>(idx)); | |
| 521 | 33 | if codeIdx < 0l { | |
| 522 | 2 | return getContentCol(top); | |
| 523 | } | ||
| 524 | 31 | } | |
| 525 | |||
| 526 | // A closing bracket is indented like the line of the opening bracket | ||
| 527 | 413 | const bool isCloserLine = !isCommentLine && isCloser(this.tokens.get(cast<unsigned long>(codeIdx)).tokenType); | |
| 528 | 413 | if isCloserLine { | |
| 529 | 84 | return top.closerCol; | |
| 530 | } | ||
| 531 | |||
| 532 | // The first line with items of a list decides, whether the items are aligned to the first one, which is on the line of the | ||
| 533 | // opening bracket. They are, if they are aligned in the original code. Only the code items count, comments do not | ||
| 534 | 329 | const FmtToken& code = this.tokens.get(cast<unsigned long>(codeIdx)); | |
| 535 | 329 | if top.alignCol >= 0l && !top.alignDecided && !isCloser(code.tokenType) { | |
| 536 | 13 | const FmtToken& firstItem = this.tokens.get(cast<unsigned long>(top.openerIdx + 1l)); | |
| 537 | 13 | const FmtToken& lineStart = this.tokens.get(this.findOrigLineStart(cast<unsigned long>(codeIdx))); | |
| 538 | 13 | top.isAligned = lineStart.origStartCol == firstItem.origStartCol; | |
| 539 | 13 | top.alignDecided = true; | |
| 540 | 13 | } | |
| 541 | |||
| 542 | // A comment before a closing bracket belongs to the content | ||
| 543 | 329 | if isCloser(this.tokens.get(cast<unsigned long>(codeIdx)).tokenType) { | |
| 544 | 2 | return getContentCol(top); | |
| 545 | } | ||
| 546 | |||
| 547 | // All items of a list are indented alike. Within blocks, continuation lines of a statement get one more level | ||
| 548 | 327 | if top.kind == FrameKind::LIST { | |
| 549 | 23 | return getContentCol(top); | |
| 550 | } | ||
| 551 | 304 | return this.startsUnit(cast<unsigned long>(codeIdx), top) ? top.baseCol : top.baseCol + this.config.indentWidth; | |
| 552 | } | ||
| 553 | |||
| 554 | /** | ||
| 555 | * Find the first token on the original line of the given token, e.g. an inline comment before it | ||
| 556 | * | ||
| 557 | * @param idx Index of the token | ||
| 558 | * @return Index of the first token on the line | ||
| 559 | */ | ||
| 560 | 13 | f<unsigned long> SourceFormatter.findOrigLineStart(unsigned long idx) { | |
| 561 | 13 | const unsigned long line = this.tokens.get(idx).startLine; | |
| 562 | 13 | unsigned long startIdx = idx; | |
| 563 | 15 | while startIdx > 0l && this.tokens.get(startIdx - 1l).startLine == line { | |
| 564 | 2 | startIdx--; | |
| 565 | 2 | } | |
| 566 | 13 | return startIdx; | |
| 567 | } | ||
| 568 | |||
| 569 | /** | ||
| 570 | * Check if the given token starts a statement or a similar unit within a block, opposed to continuing the previous one | ||
| 571 | * | ||
| 572 | * @param idx Index of the token to check. Must be no comment | ||
| 573 | * @param top Innermost open bracket pair | ||
| 574 | * @return Starts a unit or not | ||
| 575 | */ | ||
| 576 | 2084 | f<bool> SourceFormatter.startsUnit(unsigned long idx, const Frame& top) { | |
| 577 | 2084 | if this.tokens.get(idx).isUnitStart { | |
| 578 | 597 | return true; | |
| 579 | } | ||
| 580 | 1487 | const long prevIdx = this.findPrevCodeToken(cast<long>(idx)); | |
| 581 | 1487 | if prevIdx < 0l || prevIdx == top.openerIdx { | |
| 582 | ✗ | return true; | |
| 583 | } | ||
| 584 | 1487 | const FmtToken& prev = this.tokens.get(cast<unsigned long>(prevIdx)); | |
| 585 | 1487 | if prev.tokenType == TokenType::RBRACE { | |
| 586 | 36 | return isBlockBrace(prev); | |
| 587 | } | ||
| 588 | 1451 | return prev.tokenType == TokenType::SEMICOLON; | |
| 589 | } | ||
| 590 | |||
| 591 | /** | ||
| 592 | * Get the column after the given text, if it is appended at the given column. The text can contain line breaks, e.g. a | ||
| 593 | * string literal with an escaped line break | ||
| 594 | * | ||
| 595 | * @param col Column, where the text starts | ||
| 596 | * @param text Text | ||
| 597 | * @return Column after the text | ||
| 598 | */ | ||
| 599 | 2334 | f<unsigned long> advanceColumn(unsigned long col, const String& text) { | |
| 600 | 2334 | const unsigned long length = text.getLength(); | |
| 601 | 2334 | long lastLineBreak = -1l; | |
| 602 | 9736 | for unsigned long i = 0l; i < length; i++ { | |
| 603 | 7402 | if text[i] == '\n' { | |
| 604 | 2 | lastLineBreak = cast<long>(i); | |
| 605 | 2 | } | |
| 606 | 7402 | } | |
| 607 | 2334 | if lastLineBreak < 0l { | |
| 608 | 2332 | return col + getCodePointCount(text); | |
| 609 | } | ||
| 610 | 4 | return getCodePointCount(text.getSubstring(cast<unsigned long>(lastLineBreak + 1l))); | |
| 611 | 2 | } | |
| 612 | |||
| 613 | /** | ||
| 614 | * Append the given number of spaces to the output | ||
| 615 | * | ||
| 616 | * @param output Output to append to | ||
| 617 | * @param count Number of spaces | ||
| 618 | */ | ||
| 619 | 2378 | p appendSpaces(String& output, unsigned long count) { | |
| 620 | 4685 | for unsigned long i = 0l; i < count; i++ { | |
| 621 | 2307 | output += ' '; | |
| 622 | 2307 | } | |
| 623 | 2378 | } | |
| 624 | |||
| 625 | /** | ||
| 626 | * Append a block comment, that spans multiple lines. The lines after the first one are shifted by the same number of columns | ||
| 627 | * as the first one, so that e.g. the asterisks of a doc comment stay aligned | ||
| 628 | * | ||
| 629 | * @param output Output to append to | ||
| 630 | * @param token Comment token | ||
| 631 | * @param startCol Column of the comment in the output | ||
| 632 | * @return Column after the comment in the output | ||
| 633 | */ | ||
| 634 | 10 | f<unsigned long> appendMultiLineComment(String& output, const FmtToken& token, unsigned long startCol) { | |
| 635 | 10 | const long shift = cast<long>(startCol) - cast<long>(token.origStartCol); | |
| 636 | 10 | const String& text = token.text; | |
| 637 | 10 | const unsigned long length = text.getLength(); | |
| 638 | 10 | unsigned long col = startCol; | |
| 639 | 10 | unsigned long lineStart = 0l; | |
| 640 | 10 | bool isFirstLine = true; | |
| 641 | 56 | while lineStart <= length { | |
| 642 | // Find the end of the line | ||
| 643 | 46 | unsigned long lineEnd = lineStart; | |
| 644 | 1238 | while lineEnd < length && text[lineEnd] != '\n' { | |
| 645 | 1192 | lineEnd++; | |
| 646 | 1192 | } | |
| 647 | 46 | const String line = trimEnd(text.getSubstring(lineStart, cast<long>(lineEnd - lineStart))); | |
| 648 | |||
| 649 | 46 | if isFirstLine { | |
| 650 | 10 | output += line; | |
| 651 | 10 | col += getCodePointCount(line); | |
| 652 | 10 | isFirstLine = false; | |
| 653 | 10 | } else { | |
| 654 | 36 | output += '\n'; | |
| 655 | 36 | col = 0l; | |
| 656 | 36 | if !line.isEmpty() { | |
| 657 | // Shift the line by removing or adding leading spaces | ||
| 658 | 36 | unsigned long skip = 0l; | |
| 659 | 36 | if shift < 0l { | |
| 660 | 2 | const unsigned long maxSkip = cast<unsigned long>(-shift); | |
| 661 | 10 | while skip < maxSkip && skip < line.getLength() && line[skip] == ' ' { | |
| 662 | 8 | skip++; | |
| 663 | 8 | } | |
| 664 | 2 | } else { | |
| 665 | 34 | appendSpaces(output, cast<unsigned long>(shift)); | |
| 666 | 34 | col += cast<unsigned long>(shift); | |
| 667 | 34 | } | |
| 668 | 36 | const String shiftedLine = line.getSubstring(skip); | |
| 669 | 36 | output += shiftedLine; | |
| 670 | 36 | col += getCodePointCount(shiftedLine); | |
| 671 | 36 | } | |
| 672 | 36 | } | |
| 673 | |||
| 674 | 46 | lineStart = lineEnd + 1l; | |
| 675 | 46 | } | |
| 676 | 10 | return col; | |
| 677 | } | ||
| 678 | |||
| 679 | /** | ||
| 680 | * Compute the number of spaces between the given token and the previous one, which are on the same line | ||
| 681 | * | ||
| 682 | * @param idx Index of the token | ||
| 683 | * @param col Column after the previous token in the output | ||
| 684 | * @return Number of spaces | ||
| 685 | */ | ||
| 686 | 1929 | f<unsigned long> SourceFormatter.getSpacesBefore(unsigned long idx, unsigned long col) { | |
| 687 | 1929 | const FmtToken& prev = this.tokens.get(idx - 1l); | |
| 688 | 1929 | const FmtToken& token = this.tokens.get(idx); | |
| 689 | |||
| 690 | // Spaces between the tokens in the original source code. Tokens, that were joined from multiple lines, count as one space | ||
| 691 | 1929 | long origGap = 1l; | |
| 692 | 1929 | if prev.endLine == token.startLine { | |
| 693 | 1918 | origGap = cast<long>(token.origStartCol) - cast<long>(prev.origEndCol); | |
| 694 | 1918 | } | |
| 695 | |||
| 696 | // Comments keep the original spacing. Comments at the end of a line keep their original column, if it can be reached, so | ||
| 697 | // that the comments of consecutive lines stay aligned | ||
| 698 | 1929 | if isComment(token.tokenType) { | |
| 699 | 32 | const bool endsLine = idx + 1l == this.tokens.getSize() || this.tokens.get(idx + 1l).lineBreaksBefore > 0l; | |
| 700 | 32 | if endsLine || token.tokenType == TokenType::LINE_COMMENT { | |
| 701 | 28 | return getAlignedSpaces(col, token.origStartCol, origGap); | |
| 702 | } | ||
| 703 | 4 | return origGap > 0l ? 1l : 0l; | |
| 704 | } | ||
| 705 | 1897 | if isComment(prev.tokenType) { | |
| 706 | 8 | return origGap > 0l ? 1l : 0l; | |
| 707 | } | ||
| 708 | |||
| 709 | 1889 | if !this.needsSpace(prev, token) { | |
| 710 | 1021 | return 0l; | |
| 711 | } | ||
| 712 | // Aligned declarations, e.g. the names of consecutive fields or the '=' of consecutive constants, stay aligned | ||
| 713 | 868 | if origGap > 1l && isAlignable(prev, token) { | |
| 714 | 20 | return getAlignedSpaces(col, token.origStartCol, origGap); | |
| 715 | } | ||
| 716 | 848 | return 1l; | |
| 717 | } | ||
| 718 | |||
| 719 | /** | ||
| 720 | * Get the number of spaces, that move a token to its original column. If that is not possible, it gets a single space | ||
| 721 | * | ||
| 722 | * @param col Current column in the output | ||
| 723 | * @param origCol Original column of the token | ||
| 724 | * @param origGap Number of spaces before the token in the original source code | ||
| 725 | * @return Number of spaces | ||
| 726 | */ | ||
| 727 | 48 | f<unsigned long> getAlignedSpaces(unsigned long col, unsigned long origCol, long origGap) { | |
| 728 | 48 | if origGap > 1l && origCol > col + 1l { | |
| 729 | 28 | return origCol - col; | |
| 730 | } | ||
| 731 | 20 | return 1l; | |
| 732 | } | ||
| 733 | |||
| 734 | /** | ||
| 735 | * Check if the given token can be aligned with the same kind of token in the lines around it. This applies to the names of | ||
| 736 | * fields and type aliases, the '=' of declarations and the values after it, the items of tables and the bodies of one-line | ||
| 737 | * definitions | ||
| 738 | * | ||
| 739 | * @param prev Previous token | ||
| 740 | * @param token Token to check | ||
| 741 | * @return Alignable or not | ||
| 742 | */ | ||
| 743 | 20 | f<bool> isAlignable(const FmtToken& prev, const FmtToken& token) { | |
| 744 | 20 | if token.tokenType == TokenType::ASSIGN || prev.tokenType == TokenType::ASSIGN || prev.tokenType == TokenType::COMMA { | |
| 745 | 10 | return true; | |
| 746 | } | ||
| 747 | 10 | if token.tokenType == TokenType::IDENTIFIER && token.ownerKind == ASTNodeKind::FIELD { | |
| 748 | 4 | return true; | |
| 749 | } | ||
| 750 | 6 | if token.tokenType == TokenType::LBRACE && isBlockBrace(token) { | |
| 751 | 2 | return true; | |
| 752 | } | ||
| 753 | 4 | const bool isTypeDefName = prev.ownerKind == ASTNodeKind::GENERIC_TYPE_DEF || prev.ownerKind == ASTNodeKind::ALIAS_DEF; | |
| 754 | 4 | return prev.tokenType == TokenType::TYPE_IDENTIFIER && isTypeDefName; | |
| 755 | } | ||
| 756 | |||
| 757 | /** | ||
| 758 | * Check if there has to be a space between the two given tokens, which are no comments and are on the same line | ||
| 759 | * | ||
| 760 | * @param left Left token | ||
| 761 | * @param right Right token | ||
| 762 | * @return Space needed or not | ||
| 763 | */ | ||
| 764 | 1889 | f<bool> SourceFormatter.needsSpace(const FmtToken& left, const FmtToken& right) { | |
| 765 | 1889 | const TokenType lt = left.tokenType; | |
| 766 | 1889 | const TokenType rt = right.tokenType; | |
| 767 | |||
| 768 | // Tokens, that would be read as a different token without whitespace in between, have to stay apart | ||
| 769 | 1889 | if mustSeparate(left, right) { | |
| 770 | 250 | return true; | |
| 771 | } | ||
| 772 | |||
| 773 | // Function names, including operator names like 'operator==', are directly followed by their template types or params | ||
| 774 | 1639 | if left.ownerKind == ASTNodeKind::FCT_NAME { | |
| 775 | 49 | return false; | |
| 776 | } | ||
| 777 | |||
| 778 | // Punctuation | ||
| 779 | 1590 | if rt == TokenType::COMMA || rt == TokenType::SEMICOLON || rt == TokenType::RPAREN || rt == TokenType::RBRACKET { | |
| 780 | 378 | return false; | |
| 781 | } | ||
| 782 | 1212 | if rt == TokenType::DOT || rt == TokenType::SCOPE_ACCESS || lt == TokenType::DOT || lt == TokenType::SCOPE_ACCESS { | |
| 783 | 124 | return false; | |
| 784 | } | ||
| 785 | 1088 | if lt == TokenType::LPAREN || lt == TokenType::LBRACKET { | |
| 786 | 80 | return false; | |
| 787 | } | ||
| 788 | 1008 | if lt == TokenType::FCT_ATTR_PREAMBLE || lt == TokenType::MOD_ATTR_PREAMBLE { | |
| 789 | 4 | return false; | |
| 790 | } | ||
| 791 | |||
| 792 | // Generic type lists, e.g. 'Vector<int>', 'f<bool>' or 'cast<long>(x)' | ||
| 793 | 1004 | if isGenericOpen(left) || isGenericOpen(right) || isGenericClose(right) { | |
| 794 | 206 | return false; | |
| 795 | } | ||
| 796 | |||
| 797 | // Unary operators, e.g. '-x', '!x', '*ptr', '&x', '++i' and 'i++' | ||
| 798 | 798 | if isPrefixOp(left) || isPostfixOp(right) { | |
| 799 | 32 | return false; | |
| 800 | } | ||
| 801 | |||
| 802 | // Type modifiers, e.g. 'int*', 'T&' or 'int[3]' | ||
| 803 | 766 | if isTypeModifier(right) { | |
| 804 | 17 | return false; | |
| 805 | } | ||
| 806 | |||
| 807 | // Colon of a case branch | ||
| 808 | 749 | if rt == TokenType::COLON && (right.ownerKind == ASTNodeKind::CASE_BRANCH || right.ownerKind == ASTNodeKind::DEFAULT_BRANCH) { | |
| 809 | 12 | return false; | |
| 810 | } | ||
| 811 | |||
| 812 | // Type alternatives, e.g. 'int|long' | ||
| 813 | 737 | if (lt == TokenType::BITWISE_OR && left.ownerKind == ASTNodeKind::TYPE_ALTS_LST) || (rt == TokenType::BITWISE_OR && right.ownerKind == ASTNodeKind::TYPE_ALTS_LST) { | |
| 814 | 12 | return false; | |
| 815 | } | ||
| 816 | |||
| 817 | // Opening brackets. Subscripts and attributes have no space before, array initializations and lambda attributes have one | ||
| 818 | 725 | if rt == TokenType::LBRACKET { | |
| 819 | 10 | return right.ownerKind == ASTNodeKind::ARRAY_INITIALIZATION || right.ownerKind == ASTNodeKind::LAMBDA_ATTR; | |
| 820 | } | ||
| 821 | |||
| 822 | // Opening parentheses. Calls, definitions and function types have no space before, e.g. 'foo(', 'p(' or 'f<int>(' | ||
| 823 | 715 | if rt == TokenType::LPAREN { | |
| 824 | 81 | const bool isCallOrDef = lt == TokenType::IDENTIFIER || lt == TokenType::TYPE_IDENTIFIER || lt == TokenType::MAIN || lt == TokenType::P; | |
| 825 | 81 | return !isCallOrDef && !isGenericClose(left) && lt != TokenType::RPAREN && lt != TokenType::RBRACKET; | |
| 826 | } | ||
| 827 | |||
| 828 | // Braces. Blocks have spaces around their content, struct instantiations like 'Pair{1, 2}' not | ||
| 829 | 634 | if rt == TokenType::LBRACE { | |
| 830 | 109 | return right.ownerKind != ASTNodeKind::STRUCT_INSTANTIATION; | |
| 831 | } | ||
| 832 | 525 | if lt == TokenType::LBRACE { | |
| 833 | 27 | return left.ownerKind != ASTNodeKind::STRUCT_INSTANTIATION && rt != TokenType::RBRACE; | |
| 834 | } | ||
| 835 | 498 | if rt == TokenType::RBRACE { | |
| 836 | 27 | return right.ownerKind != ASTNodeKind::STRUCT_INSTANTIATION; | |
| 837 | } | ||
| 838 | |||
| 839 | // Everything else, e.g. keywords, identifiers and binary operators, is separated by a space | ||
| 840 | 471 | return true; | |
| 841 | } | ||
| 842 | |||
| 843 | /** | ||
| 844 | * Check if the two given tokens would be read as different tokens, if there was no whitespace between them | ||
| 845 | * | ||
| 846 | * @param left Left token | ||
| 847 | * @param right Right token | ||
| 848 | * @return Have to be separated or not | ||
| 849 | */ | ||
| 850 | 1889 | f<bool> mustSeparate(const FmtToken& left, const FmtToken& right) { | |
| 851 | 1889 | return mustSeparateTexts(left.text, right.text, isNumberLiteral(left.tokenType)); | |
| 852 | } | ||
| 853 | |||
| 854 | /** | ||
| 855 | * Check if the two given token texts would be read as different tokens, if there was no whitespace between them | ||
| 856 | * | ||
| 857 | * @param left Text of the left token | ||
| 858 | * @param right Text of the right token | ||
| 859 | * @param leftIsNumber Whether the left token is a number literal | ||
| 860 | * @return Have to be separated or not | ||
| 861 | */ | ||
| 862 | 1898 | f<bool> mustSeparateTexts(const String& left, const String& right, bool leftIsNumber) { | |
| 863 | 1898 | if left.isEmpty() || right.isEmpty() { | |
| 864 | ✗ | return false; | |
| 865 | } | ||
| 866 | 1898 | const char l = left[left.getLength() - 1l]; | |
| 867 | 1898 | const char r = right[0ul]; | |
| 868 | |||
| 869 | // Identifiers, keywords and number literals | ||
| 870 | 1898 | if isWordChar(l) && isWordChar(r) { | |
| 871 | 251 | return true; | |
| 872 | } | ||
| 873 | // A dot next to a number would be read as part of a double literal | ||
| 874 | 1647 | if (leftIsNumber && r == '.') || (l == '.' && isDigit(r)) { | |
| 875 | 1 | return true; | |
| 876 | } | ||
| 877 | // Operators, that would be read as a longer operator or as the start of a comment. The lexer reads the longest operator, | ||
| 878 | // so e.g. '>>' followed by '>' is read as '>>' and '>' again | ||
| 879 | 1646 | switch l { | |
| 880 | 8 | case '|': { return r == '|' || r == '='; } | |
| 881 | 17 | case '&': { return r == '&' || r == '='; } | |
| 882 | 17 | case '+': { return r == '+' || r == '='; } | |
| 883 | 13 | case '-': { return r == '-' || r == '=' || r == '>'; } | |
| 884 | 1 | case '/': { return r == '/' || r == '*' || r == '='; } | |
| 885 | 98 | case '<': { return r == '=' || (r == '<' && left != "<<"); } | |
| 886 | 76 | case '>': { return r == '=' || (r == '>' && left != ">>"); } | |
| 887 | 39 | case ':': { return r == ':'; } | |
| 888 | 54 | case '.': { return r == '.'; } | |
| 889 | 2 | case '#': { return r == '!'; } | |
| 890 | 117 | case '^', '!', '*', '%', '=': { return r == '='; } | |
| 891 | } | ||
| 892 | 1204 | return false; | |
| 893 | } | ||
| 894 | |||
| 895 | /** | ||
| 896 | * Check if the given token type is a number literal | ||
| 897 | * | ||
| 898 | * @param tokenType Token type to check | ||
| 899 | * @return Number literal or not | ||
| 900 | */ | ||
| 901 | 1889 | f<bool> isNumberLiteral(TokenType tokenType) { | |
| 902 | 1889 | switch tokenType { | |
| 903 | case TokenType::DOUBLE_LIT, TokenType::INT_LIT, TokenType::SHORT_LIT, TokenType::LONG_LIT: { | ||
| 904 | 131 | return true; | |
| 905 | } | ||
| 906 | } | ||
| 907 | 1758 | return false; | |
| 908 | } | ||
| 909 | |||
| 910 | /** | ||
| 911 | * Check if the given char can be part of an identifier, a keyword or a number literal | ||
| 912 | * | ||
| 913 | * @param c Char to check | ||
| 914 | * @return Word char or not | ||
| 915 | */ | ||
| 916 | 2923 | f<bool> isWordChar(char c) { | |
| 917 | 2923 | return isAlphaNum(c) || c == '_'; | |
| 918 | } | ||
| 919 | |||
| 920 | /** | ||
| 921 | * Check if the given token type is a comment | ||
| 922 | * | ||
| 923 | * @param tokenType Token type to check | ||
| 924 | * @return Comment or not | ||
| 925 | */ | ||
| 926 | 16398 | f<bool> isComment(TokenType tokenType) { | |
| 927 | 16398 | return tokenType == TokenType::LINE_COMMENT || tokenType == TokenType::BLOCK_COMMENT || tokenType == TokenType::DOC_COMMENT; | |
| 928 | } | ||
| 929 | |||
| 930 | /** | ||
| 931 | * Check if the given token type is an opening bracket | ||
| 932 | * | ||
| 933 | * @param tokenType Token type to check | ||
| 934 | * @return Opening bracket or not | ||
| 935 | */ | ||
| 936 | 2245 | f<bool> isOpener(TokenType tokenType) { | |
| 937 | 2245 | return tokenType == TokenType::LBRACE || tokenType == TokenType::LPAREN || tokenType == TokenType::LBRACKET; | |
| 938 | } | ||
| 939 | |||
| 940 | /** | ||
| 941 | * Check if the given token type is a closing bracket | ||
| 942 | * | ||
| 943 | * @param tokenType Token type to check | ||
| 944 | * @return Closing bracket or not | ||
| 945 | */ | ||
| 946 | 2829 | f<bool> isCloser(TokenType tokenType) { | |
| 947 | 2829 | return tokenType == TokenType::RBRACE || tokenType == TokenType::RPAREN || tokenType == TokenType::RBRACKET; | |
| 948 | } | ||
| 949 | |||
| 950 | /** | ||
| 951 | * Check if the given token is a brace of a block or a type body, opposed to a struct instantiation | ||
| 952 | * | ||
| 953 | * @param token Token to check | ||
| 954 | * @return Block brace or not | ||
| 955 | */ | ||
| 956 | 156 | f<bool> isBlockBrace(const FmtToken& token) { | |
| 957 | 156 | if token.tokenType != TokenType::LBRACE && token.tokenType != TokenType::RBRACE { | |
| 958 | ✗ | return false; | |
| 959 | } | ||
| 960 | 156 | switch token.ownerKind { | |
| 961 | case ASTNodeKind::STMT_LST, ASTNodeKind::STRUCT_DEF, ASTNodeKind::INTERFACE_DEF, ASTNodeKind::UNION_DEF, ASTNodeKind::ENUM_DEF, ASTNodeKind::SWITCH_STMT: { | ||
| 962 | 141 | return true; | |
| 963 | } | ||
| 964 | } | ||
| 965 | 15 | return false; | |
| 966 | } | ||
| 967 | |||
| 968 | /** | ||
| 969 | * Check if the given token is an operator of a binary expression, whose token can be a generic bracket as well | ||
| 970 | * | ||
| 971 | * @param token Token to check | ||
| 972 | * @return Binary operator or not | ||
| 973 | */ | ||
| 974 | 243 | f<bool> isBinaryExprToken(const FmtToken& token) { | |
| 975 | 243 | return token.ownerKind == ASTNodeKind::RELATIONAL_EXPR || token.ownerKind == ASTNodeKind::SHIFT_EXPR; | |
| 976 | } | ||
| 977 | |||
| 978 | /** | ||
| 979 | * Check if the given token opens a generic type list | ||
| 980 | * | ||
| 981 | * @param token Token to check | ||
| 982 | * @return Generic opening bracket or not | ||
| 983 | */ | ||
| 984 | 1937 | f<bool> isGenericOpen(const FmtToken& token) { | |
| 985 | 1937 | return token.tokenType == TokenType::LESS && !isBinaryExprToken(token) && token.ownerKind != ASTNodeKind::FCT_NAME; | |
| 986 | } | ||
| 987 | |||
| 988 | /** | ||
| 989 | * Check if the given token closes a generic type list. Two nested lists can be closed by a single '>>' token | ||
| 990 | * | ||
| 991 | * @param token Token to check | ||
| 992 | * @return Generic closing bracket or not | ||
| 993 | */ | ||
| 994 | 879 | f<bool> isGenericClose(const FmtToken& token) { | |
| 995 | 879 | const bool isGreater = token.tokenType == TokenType::GREATER || token.tokenType == TokenType::SHR; | |
| 996 | 879 | return isGreater && !isBinaryExprToken(token) && token.ownerKind != ASTNodeKind::FCT_NAME; | |
| 997 | } | ||
| 998 | |||
| 999 | /** | ||
| 1000 | * Check if the given token is a type modifier: a pointer, a reference or an array | ||
| 1001 | * | ||
| 1002 | * @param token Token to check | ||
| 1003 | * @return Type modifier or not | ||
| 1004 | */ | ||
| 1005 | 766 | f<bool> isTypeModifier(const FmtToken& token) { | |
| 1006 | 766 | if token.ownerKind != ASTNodeKind::DATA_TYPE { | |
| 1007 | 749 | return false; | |
| 1008 | } | ||
| 1009 | 17 | const TokenType tokenType = token.tokenType; | |
| 1010 | 17 | return tokenType == TokenType::MUL || tokenType == TokenType::BITWISE_AND || tokenType == TokenType::LBRACKET; | |
| 1011 | } | ||
| 1012 | |||
| 1013 | /** | ||
| 1014 | * Check if the given token is a prefix operator, e.g. the '-' of '-x' or the '&' of '&x' | ||
| 1015 | * | ||
| 1016 | * @param token Token to check | ||
| 1017 | * @return Prefix operator or not | ||
| 1018 | */ | ||
| 1019 | 798 | f<bool> isPrefixOp(const FmtToken& token) { | |
| 1020 | 798 | const TokenType tokenType = token.tokenType; | |
| 1021 | // The minus of a negative number literal belongs to the constant | ||
| 1022 | 798 | if token.ownerKind == ASTNodeKind::CONSTANT { | |
| 1023 | 50 | return tokenType == TokenType::MINUS; | |
| 1024 | } | ||
| 1025 | 748 | if token.ownerKind != ASTNodeKind::PREFIX_UNARY_EXPR { | |
| 1026 | 733 | return false; | |
| 1027 | } | ||
| 1028 | 15 | switch tokenType { | |
| 1029 | case TokenType::MINUS, TokenType::PLUS_PLUS, TokenType::MINUS_MINUS, TokenType::NOT, TokenType::BITWISE_NOT, TokenType::MUL, TokenType::BITWISE_AND: { | ||
| 1030 | 15 | return true; | |
| 1031 | } | ||
| 1032 | } | ||
| 1033 | ✗ | return false; | |
| 1034 | } | ||
| 1035 | |||
| 1036 | /** | ||
| 1037 | * Check if the given token is a postfix operator, e.g. the '++' of 'i++' or the '!' of 'getResult()!' | ||
| 1038 | * | ||
| 1039 | * @param token Token to check | ||
| 1040 | * @return Postfix operator or not | ||
| 1041 | */ | ||
| 1042 | 780 | f<bool> isPostfixOp(const FmtToken& token) { | |
| 1043 | 780 | if token.ownerKind != ASTNodeKind::POSTFIX_UNARY_EXPR { | |
| 1044 | 762 | return false; | |
| 1045 | } | ||
| 1046 | 18 | const TokenType tokenType = token.tokenType; | |
| 1047 | 18 | return tokenType == TokenType::PLUS_PLUS || tokenType == TokenType::MINUS_MINUS || tokenType == TokenType::NOT; | |
| 1048 | } | ||
| 1049 | |||
| 1050 | // ============================================================ Entry point ============================================================ | ||
| 1051 | |||
| 1052 | /** | ||
| 1053 | * Format the given Spice source code. The code has to be syntactically valid. Otherwise, the compilation is aborted with the | ||
| 1054 | * parser error. | ||
| 1055 | * | ||
| 1056 | * @param resourceManager Global resource manager, that owns the syntax tree | ||
| 1057 | * @param sourceCode Source code to format | ||
| 1058 | * @param filePath Path of the source file, which errors refer to. Has to stay valid, until the formatting is done | ||
| 1059 | * @param config Formatter configuration, e.g. from the manifest | ||
| 1060 | * @return Formatted source code | ||
| 1061 | */ | ||
| 1062 | 20 | public f<String> formatSourceCode(GlobalResourceManager& resourceManager, const String& sourceCode, const FilePath& filePath, const FormatConfig& config) { | |
| 1063 | // The formatted code is assembled from strings, which end at a NUL char. So source code with NUL chars cannot be formatted | ||
| 1064 | 20 | if containsNulChar(sourceCode) { | |
| 1065 | ✗ | const String message = "Source file '" + String(filePath.toString()) + "' contains a NUL char and cannot be formatted"; | |
| 1066 | ✗ | const CompilerError error = CompilerError(CompilerErrorType::IO_ERROR, message.getRaw()); | |
| 1067 | ✗ | abortWithError(error.getMessage()); | |
| 1068 | ✗ | } | |
| 1069 | |||
| 1070 | 20 | Lexer lexer = Lexer(sourceCode, filePath); | |
| 1071 | 20 | Parser parser = Parser(resourceManager, nil<SourceFile*>, lexer); | |
| 1072 | 20 | ASTEntryNode* ast = parser.parse(); | |
| 1073 | |||
| 1074 | 20 | SourceFormatter formatter = SourceFormatter(sourceCode, lexer.getTokens(), config); | |
| 1075 | 20 | formatter.analyzeSyntaxTree(cast<ASTNode*>(ast)); | |
| 1076 | 20 | const String formattedCode = formatter.format(); | |
| 1077 | |||
| 1078 | // The formatter only changes whitespace. Make sure that the formatted code consists of the same tokens, before anyone | ||
| 1079 | // replaces the original code with it | ||
| 1080 | 20 | Lexer formattedLexer = Lexer(formattedCode, filePath); | |
| 1081 | 20 | if !haveSameTokens(lexer.getTokens(), sourceCode, formattedLexer.getTokens(), formattedCode) { | |
| 1082 | ✗ | const String message = "Formatting changed the tokens of '" + String(filePath.toString()) + "'. Please report this as a bug"; | |
| 1083 | ✗ | const CompilerError error = CompilerError(CompilerErrorType::INTERNAL_ERROR, message.getRaw()); | |
| 1084 | ✗ | abortWithError(error.getMessage()); | |
| 1085 | ✗ | } | |
| 1086 | 40 | return formattedCode; | |
| 1087 | 20 | } | |
| 1088 | |||
| 1089 | /** | ||
| 1090 | * Check if the given text contains a NUL char | ||
| 1091 | * | ||
| 1092 | * @param text Text to check | ||
| 1093 | * @return Contains a NUL char or not | ||
| 1094 | */ | ||
| 1095 | 20 | f<bool> containsNulChar(const String& text) { | |
| 1096 | 20 | const unsigned long length = text.getLength(); | |
| 1097 | 11370 | for unsigned long i = 0l; i < length; i++ { | |
| 1098 | 11350 | if text[i] == '\0' { | |
| 1099 | ✗ | return true; | |
| 1100 | } | ||
| 1101 | 11350 | } | |
| 1102 | 20 | return false; | |
| 1103 | } | ||
| 1104 | |||
| 1105 | /** | ||
| 1106 | * Check if the two given token streams consist of the same tokens. Only the whitespace within comments does not count, because | ||
| 1107 | * comments may be reindented | ||
| 1108 | * | ||
| 1109 | * @param origTokens Tokens of the original source code | ||
| 1110 | * @param origCode Original source code | ||
| 1111 | * @param newTokens Tokens of the formatted source code | ||
| 1112 | * @param newCode Formatted source code | ||
| 1113 | * @return Same tokens or not | ||
| 1114 | */ | ||
| 1115 | 21 | f<bool> haveSameTokens(const Vector<Token>& origTokens, const String& origCode, const Vector<Token>& newTokens, const String& newCode) { | |
| 1116 | 21 | const unsigned long tokenCount = origTokens.getSize(); | |
| 1117 | 21 | if newTokens.getSize() != tokenCount { | |
| 1118 | ✗ | return false; | |
| 1119 | } | ||
| 1120 | 2394 | for unsigned long i = 0l; i < tokenCount; i++ { | |
| 1121 | 2374 | const Token& origToken = origTokens.get(i); | |
| 1122 | 2374 | const Token& newToken = newTokens.get(i); | |
| 1123 | 2374 | if origToken.tokenType != newToken.tokenType { | |
| 1124 | ✗ | return false; | |
| 1125 | } | ||
| 1126 | 2374 | const String origText = getTokenSource(origCode, origToken); | |
| 1127 | 2374 | const String newText = getTokenSource(newCode, newToken); | |
| 1128 | 2374 | if isComment(origToken.tokenType) { | |
| 1129 | 66 | if removeWhitespace(origText) != removeWhitespace(newText) { | |
| 1130 | ✗ | return false; | |
| 1131 | } | ||
| 1132 | 2374 | } else if origText != newText { | |
| 1133 | 1 | return false; | |
| 1134 | 2307 | } | |
| 1135 | 2374 | } | |
| 1136 | 20 | return true; | |
| 1137 | } | ||
| 1138 | |||
| 1139 | /** | ||
| 1140 | * Get the source code of the given token | ||
| 1141 | * | ||
| 1142 | * @param sourceCode Source code, that contains the token | ||
| 1143 | * @param token Token | ||
| 1144 | * @return Source code of the token | ||
| 1145 | */ | ||
| 1146 | 4748 | f<String> getTokenSource(const String& sourceCode, const Token& token) { | |
| 1147 | 4748 | return sourceCode.getSubstring(token.startOffset, cast<long>(token.endOffset - token.startOffset)); | |
| 1148 | } | ||
| 1149 | |||
| 1150 | /** | ||
| 1151 | * Remove all whitespace from the given text | ||
| 1152 | * | ||
| 1153 | * @param text Text | ||
| 1154 | * @return Text without whitespace | ||
| 1155 | */ | ||
| 1156 | 132 | f<String> removeWhitespace(const String& text) { | |
| 1157 | 132 | String textWithoutWhitespace; | |
| 1158 | 132 | const unsigned long length = text.getLength(); | |
| 1159 | 5252 | for unsigned long i = 0l; i < length; i++ { | |
| 1160 | 5120 | const char c = text[i]; | |
| 1161 | 5120 | if !isWhitespace(c) { | |
| 1162 | 4136 | textWithoutWhitespace += c; | |
| 1163 | 4136 | } | |
| 1164 | 5120 | } | |
| 1165 | 132 | return textWithoutWhitespace; | |
| 1166 | } | ||
| 1167 | |||
| 1168 | // ============================================================== Tests ============================================================== | ||
| 1169 | |||
| 1170 | /** | ||
| 1171 | * Format the given source code with a fresh resource manager | ||
| 1172 | * | ||
| 1173 | * @param sourceCode Source code to format | ||
| 1174 | * @param config Formatter configuration | ||
| 1175 | * @return Formatted source code | ||
| 1176 | */ | ||
| 1177 | 5 | f<String> formatForTest(string sourceCode, const FormatConfig& config) { | |
| 1178 | 5 | String formattedCode; | |
| 1179 | { | ||
| 1180 | 5 | CliOptions cliOptions; | |
| 1181 | 5 | GlobalResourceManager resourceManager = GlobalResourceManager(cliOptions); | |
| 1182 | 5 | const FilePath filePath = FilePath("test.spice"); | |
| 1183 | 5 | formattedCode = formatSourceCode(resourceManager, String(sourceCode), filePath, config); | |
| 1184 | 5 | } | |
| 1185 | 5 | return formattedCode; | |
| 1186 | } | ||
| 1187 | |||
| 1188 | /** | ||
| 1189 | * Format the given source code with a fresh resource manager and the default configuration | ||
| 1190 | * | ||
| 1191 | * @param sourceCode Source code to format | ||
| 1192 | * @return Formatted source code | ||
| 1193 | */ | ||
| 1194 | 3 | f<String> formatForTest(string sourceCode) { | |
| 1195 | 3 | const FormatConfig config; | |
| 1196 | 3 | return formatForTest(sourceCode, config); | |
| 1197 | } | ||
| 1198 | |||
| 1199 | 1 | #[test] | |
| 1200 | f<bool> testFormatterSpacing() { | ||
| 1201 | 1 | const String formatted = formatForTest("f<int>main(){int x=-1+2*3;int* ptr=&x;Vector<Vector<int>>v;return cast<int>(*ptr)<x?x:-x;}"); | |
| 1202 | 1 | assert formatted == "f<int> main() { int x = -1 + 2 * 3; int* ptr = &x; Vector<Vector<int>> v; return cast<int>(*ptr) < x ? x : -x; }\n"; | |
| 1203 | 1 | return true; | |
| 1204 | 1 | } | |
| 1205 | |||
| 1206 | 1 | #[test] | |
| 1207 | f<bool> testFormatterIndentation() { | ||
| 1208 | 1 | const String formatted = formatForTest("p foo()\n{\n if true\n {\n bar(1,\n 2);\n }\n else {\n baz();\n }\n}\n"); | |
| 1209 | 1 | assert formatted == "p foo() {\n if true {\n bar(1,\n 2);\n } else {\n baz();\n }\n}\n"; | |
| 1210 | 1 | return true; | |
| 1211 | 1 | } | |
| 1212 | |||
| 1213 | 1 | #[test] | |
| 1214 | f<bool> testFormatterBlankLinesAndComments() { | ||
| 1215 | 1 | const String formatted = formatForTest("// Header\n\n\n\nimport \"std/io/file\";\n\nf<int> main() {\n\n int a = 1; // One\n int bb = 2; // Two\n\n}\n"); | |
| 1216 | 1 | assert formatted == "// Header\n\nimport \"std/io/file\";\n\nf<int> main() {\n int a = 1; // One\n int bb = 2; // Two\n}\n"; | |
| 1217 | 1 | return true; | |
| 1218 | 1 | } | |
| 1219 | |||
| 1220 | 1 | #[test] | |
| 1221 | f<bool> testFormatterConfig() { | ||
| 1222 | 1 | const string sourceCode = "p foo() {\n if true {\n a();\n\n\n\n b();\n }\n}\n"; | |
| 1223 | // Two spaces per level and no blank lines | ||
| 1224 | 1 | FormatConfig compactConfig; | |
| 1225 | 1 | compactConfig.indentWidth = 2l; | |
| 1226 | 1 | compactConfig.maxBlankLines = 0l; | |
| 1227 | 1 | const String compact = formatForTest(sourceCode, compactConfig); | |
| 1228 | // Two blank lines in a row | ||
| 1229 | 1 | FormatConfig looseConfig; | |
| 1230 | 1 | looseConfig.maxBlankLines = 2l; | |
| 1231 | 1 | const String loose = formatForTest(sourceCode, looseConfig); | |
| 1232 | 1 | assert compact == "p foo() {\n if true {\n a();\n b();\n }\n}\n"; | |
| 1233 | 1 | assert loose == "p foo() {\n if true {\n a();\n\n\n b();\n }\n}\n"; | |
| 1234 | 1 | return true; | |
| 1235 | 1 | } | |
| 1236 | |||
| 1237 | 1 | #[test] | |
| 1238 | f<bool> testFormatterComparesTokensExactly() { | ||
| 1239 | 1 | const FilePath filePath = FilePath("test.spice"); | |
| 1240 | 1 | const String original = String("string s = \"a b\"; // A comment\n"); | |
| 1241 | 1 | const String changedLiteral = String("string s = \"ab\"; // A comment\n"); | |
| 1242 | 1 | const String reindentedComment = String("string s = \"a b\"; // A comment\n"); | |
| 1243 | 1 | Lexer originalLexer = Lexer(original, filePath); | |
| 1244 | 1 | Lexer changedLiteralLexer = Lexer(changedLiteral, filePath); | |
| 1245 | 1 | Lexer reindentedCommentLexer = Lexer(reindentedComment, filePath); | |
| 1246 | 1 | assert !haveSameTokens(originalLexer.getTokens(), original, changedLiteralLexer.getTokens(), changedLiteral); | |
| 1247 | 1 | assert haveSameTokens(originalLexer.getTokens(), original, reindentedCommentLexer.getTokens(), reindentedComment); | |
| 1248 | 1 | return true; | |
| 1249 | 1 | } | |
| 1250 | |||
| 1251 | 1 | #[test] | |
| 1252 | f<bool> testFormatterKeepsTokensApart() { | ||
| 1253 | 1 | assert mustSeparateTexts(String("-"), String("-"), false); | |
| 1254 | 1 | assert mustSeparateTexts(String("/"), String("/"), false); | |
| 1255 | 1 | assert mustSeparateTexts(String("int"), String("x"), false); | |
| 1256 | 1 | assert mustSeparateTexts(String(">"), String(">"), false); | |
| 1257 | 1 | assert mustSeparateTexts(String("1"), String("."), true); | |
| 1258 | 1 | assert !mustSeparateTexts(String("v1"), String("."), false); | |
| 1259 | 1 | assert !mustSeparateTexts(String(">>"), String(">"), false); | |
| 1260 | 1 | assert !mustSeparateTexts(String("("), String("x"), false); | |
| 1261 | 1 | assert !mustSeparateTexts(String("x"), String(")"), false); | |
| 1262 | 1 | return true; | |
| 1263 | 1 | } | |
| 1264 |