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
commit a15872ba9e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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) wr(em, $tok.tokType, ltOther)
if not em.inquote: wrSpace(em) if not em.inquote: wrSpace(em)
of tkOpr, tkDotDot: 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 ["<", ">", "<=", ">=", "==", "!="]): tok.ident.s notin ["<", ">", "<=", ">=", "==", "!="]):
# bug #9504: remember to not spacify a keyword: # bug #9504: remember to not spacify a keyword:
lastTokWasTerse = true lastTokWasTerse = true
@ -538,7 +538,7 @@ proc emitTok*(em: var Emitter; L: Lexer; tok: Token) =
if not em.endsInWhite: wrSpace(em) if not em.endsInWhite: wrSpace(em)
wr(em, tok.ident.s, ltOpr) wr(em, tok.ident.s, ltOpr)
template isUnary(tok): bool = template isUnary(tok): bool =
tok.strongSpaceB == 0 and tok.strongSpaceA tok.strongSpaceB == tsNone and tok.strongSpaceA
if not isUnary(tok): if not isUnary(tok):
rememberSplit(splitBinary) rememberSplit(splitBinary)

View file

@ -93,19 +93,22 @@ type
# so that it is the correct default value # so that it is the correct default value
base2, base8, base16 base2, base8, base16
Token* = object # a Nim token TokenSpacing* = enum
tokType*: TokType # the type of the token tsNone, tsTrailing, tsEof
indent*: int # the indentation; != -1 if the token has been
# preceded with indentation Token* = object # a Nim token
ident*: PIdent # the parsed identifier tokType*: TokType # the type of the token
iNumber*: BiggestInt # the parsed integer literal indent*: int # the indentation; != -1 if the token has been
fNumber*: BiggestFloat # the parsed floating point literal # preceded with indentation
base*: NumericalBase # the numerical base; only valid for int ident*: PIdent # the parsed identifier
# or float literals iNumber*: BiggestInt # the parsed integer literal
strongSpaceA*: bool # leading spaces of an operator fNumber*: BiggestFloat # the parsed floating point literal
strongSpaceB*: int8 # trailing spaces of an operator base*: NumericalBase # the numerical base; only valid for int
literal*: string # the parsed (string) literal; and # or float literals
# documentation comments are here too 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 line*, col*: int
when defined(nimpretty): when defined(nimpretty):
offsetA*, offsetB*: int # used for pretty printing so that literals 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) tokenEnd(tok, pos-1)
# advance pos but don't store it in L.bufpos so the next token (which might # advance pos but don't store it in L.bufpos so the next token (which might
# be an operator too) gets the preceding spaces: # be an operator too) gets the preceding spaces:
tok.strongSpaceB = 0 tok.strongSpaceB = tsNone
while L.buf[pos] == ' ': while L.buf[pos] == ' ':
inc pos inc pos
if tok.strongSpaceB < 1: if tok.strongSpaceB != tsTrailing:
inc(tok.strongSpaceB) tok.strongSpaceB = tsTrailing
if L.buf[pos] in {CR, LF, nimlexbase.EndOfFile}: if L.buf[pos] in {CR, LF, nimlexbase.EndOfFile}:
tok.strongSpaceB = -1 tok.strongSpaceB = tsEof
proc getPrecedence*(tok: Token): int = proc getPrecedence*(tok: Token): int =
## Calculates the precedence of the given token. ## Calculates the precedence of the given token.

View file

@ -300,14 +300,14 @@ proc isRightAssociative(tok: Token): bool {.inline.} =
proc isUnary(tok: Token): bool = proc isUnary(tok: Token): bool =
## Check if the given token is a unary operator ## Check if the given token is a unary operator
tok.tokType in {tkOpr, tkDotDot} and tok.tokType in {tkOpr, tkDotDot} and
tok.strongSpaceB == 0 and tok.strongSpaceB == tsNone and
tok.strongSpaceA tok.strongSpaceA
proc checkBinary(p: Parser) {.inline.} = proc checkBinary(p: Parser) {.inline.} =
## Check if the current parser token is a binary operator. ## Check if the current parser token is a binary operator.
# we don't check '..' here as that's too annoying # we don't check '..' here as that's too annoying
if p.tok.tokType == tkOpr: 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)) parMessage(p, warnInconsistentSpacing, prettyTok(p.tok))
#| module = stmt ^* (';' / IND{=}) #| module = stmt ^* (';' / IND{=})

View file

@ -267,7 +267,7 @@ proc emitTok*(em: var Emitter; L: TLexer; tok: TToken) =
if not em.endsInWhite: wr(" ") if not em.endsInWhite: wr(" ")
wr(tok.ident.s) wr(tok.ident.s)
template isUnary(tok): bool = 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}: if not isUnary(tok) or em.lastTok in {tkOpr, tkDotDot}:
wr(" ") wr(" ")

View file

@ -272,7 +272,7 @@ proc emitTok*(em: var Emitter; L: TLexer; tok: TToken) =
if not em.endsInWhite: wr(" ") if not em.endsInWhite: wr(" ")
wr(tok.ident.s) wr(tok.ident.s)
template isUnary(tok): bool = 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}: if not isUnary(tok) or em.lastTok in {tkOpr, tkDotDot}:
wr(" ") wr(" ")