diff --git a/compiler/layouter.nim b/compiler/layouter.nim index 1e36b0f9ea3a8..6e8280e674d9a 100644 --- a/compiler/layouter.nim +++ b/compiler/layouter.nim @@ -528,7 +528,7 @@ proc emitTok*(em: var Emitter; L: Lexer; tok: Token) = wr(em, $tok.tokType, ltOther) if not em.inquote: wrSpace(em) of tkOpr, tkDotDot: - if em.inquote or (((not tok.strongSpaceA) and tok.strongSpaceB == 0) and + if em.inquote or (((not tok.strongSpaceA) and tok.strongSpaceB == tsNone) and tok.ident.s notin ["<", ">", "<=", ">=", "==", "!="]): # bug #9504: remember to not spacify a keyword: lastTokWasTerse = true @@ -538,7 +538,7 @@ proc emitTok*(em: var Emitter; L: Lexer; tok: Token) = if not em.endsInWhite: wrSpace(em) wr(em, tok.ident.s, ltOpr) template isUnary(tok): bool = - tok.strongSpaceB == 0 and tok.strongSpaceA + tok.strongSpaceB == tsNone and tok.strongSpaceA if not isUnary(tok): rememberSplit(splitBinary) diff --git a/compiler/lexer.nim b/compiler/lexer.nim index a82e99a474c4a..a9e370b5b80aa 100644 --- a/compiler/lexer.nim +++ b/compiler/lexer.nim @@ -93,19 +93,22 @@ type # so that it is the correct default value base2, base8, base16 - Token* = object # a Nim token - tokType*: TokType # the type of the token - indent*: int # the indentation; != -1 if the token has been - # preceded with indentation - ident*: PIdent # the parsed identifier - iNumber*: BiggestInt # the parsed integer literal - fNumber*: BiggestFloat # the parsed floating point literal - base*: NumericalBase # the numerical base; only valid for int - # or float literals - strongSpaceA*: bool # leading spaces of an operator - strongSpaceB*: int8 # trailing spaces of an operator - literal*: string # the parsed (string) literal; and - # documentation comments are here too + TokenSpacing* = enum + tsNone, tsTrailing, tsEof + + Token* = object # a Nim token + tokType*: TokType # the type of the token + indent*: int # the indentation; != -1 if the token has been + # preceded with indentation + ident*: PIdent # the parsed identifier + iNumber*: BiggestInt # the parsed integer literal + fNumber*: BiggestFloat # the parsed floating point literal + base*: NumericalBase # the numerical base; only valid for int + # or float literals + strongSpaceA*: bool # leading spaces of an operator + strongSpaceB*: TokenSpacing # trailing spaces of an operator + literal*: string # the parsed (string) literal; and + # documentation comments are here too line*, col*: int when defined(nimpretty): offsetA*, offsetB*: int # used for pretty printing so that literals @@ -955,13 +958,13 @@ proc getOperator(L: var Lexer, tok: var Token) = tokenEnd(tok, pos-1) # advance pos but don't store it in L.bufpos so the next token (which might # be an operator too) gets the preceding spaces: - tok.strongSpaceB = 0 + tok.strongSpaceB = tsNone while L.buf[pos] == ' ': inc pos - if tok.strongSpaceB < 1: - inc(tok.strongSpaceB) + if tok.strongSpaceB != tsTrailing: + tok.strongSpaceB = tsTrailing if L.buf[pos] in {CR, LF, nimlexbase.EndOfFile}: - tok.strongSpaceB = -1 + tok.strongSpaceB = tsEof proc getPrecedence*(tok: Token): int = ## Calculates the precedence of the given token. diff --git a/compiler/parser.nim b/compiler/parser.nim index 19600f6868eb4..22d3b8595d808 100644 --- a/compiler/parser.nim +++ b/compiler/parser.nim @@ -300,14 +300,14 @@ proc isRightAssociative(tok: Token): bool {.inline.} = proc isUnary(tok: Token): bool = ## Check if the given token is a unary operator tok.tokType in {tkOpr, tkDotDot} and - tok.strongSpaceB == 0 and + tok.strongSpaceB == tsNone and tok.strongSpaceA proc checkBinary(p: Parser) {.inline.} = ## Check if the current parser token is a binary operator. # we don't check '..' here as that's too annoying if p.tok.tokType == tkOpr: - if p.tok.strongSpaceB > 0 and not p.tok.strongSpaceA: + if p.tok.strongSpaceB == tsTrailing and not p.tok.strongSpaceA: parMessage(p, warnInconsistentSpacing, prettyTok(p.tok)) #| module = stmt ^* (';' / IND{=}) diff --git a/nimpretty/tests/exhaustive.nim b/nimpretty/tests/exhaustive.nim index 80bf8b3460f90..53ff0ea4d5f63 100644 --- a/nimpretty/tests/exhaustive.nim +++ b/nimpretty/tests/exhaustive.nim @@ -267,7 +267,7 @@ proc emitTok*(em: var Emitter; L: TLexer; tok: TToken) = if not em.endsInWhite: wr(" ") wr(tok.ident.s) template isUnary(tok): bool = - tok.strongSpaceB == 0 and tok.strongSpaceA + tok.strongSpaceB == tsNone and tok.strongSpaceA if not isUnary(tok) or em.lastTok in {tkOpr, tkDotDot}: wr(" ") diff --git a/nimpretty/tests/expected/exhaustive.nim b/nimpretty/tests/expected/exhaustive.nim index 22bc22a7d8bdb..266bcae06b575 100644 --- a/nimpretty/tests/expected/exhaustive.nim +++ b/nimpretty/tests/expected/exhaustive.nim @@ -272,7 +272,7 @@ proc emitTok*(em: var Emitter; L: TLexer; tok: TToken) = if not em.endsInWhite: wr(" ") wr(tok.ident.s) template isUnary(tok): bool = - tok.strongSpaceB == 0 and tok.strongSpaceA + tok.strongSpaceB == tsNone and tok.strongSpaceA if not isUnary(tok) or em.lastTok in {tkOpr, tkDotDot}: wr(" ")