refactor strongSpaceB with an enum (#20809)

refactor strongSpaceB
This commit is contained in:
ringabout
2022-11-10 23:21:52 +08:00
committed by GitHub
parent 31be01d78f
commit a15872ba9e
5 changed files with 26 additions and 23 deletions

View File

@@ -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)

View File

@@ -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.

View File

@@ -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{=})

View File

@@ -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(" ")

View File

@@ -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(" ")