Skip to content

Commit

Permalink
refactor strongSpaceB with an enum (#20809)
Browse files Browse the repository at this point in the history
refactor strongSpaceB
  • Loading branch information
ringabout authored Nov 10, 2022
1 parent 31be01d commit a15872b
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 23 deletions.
4 changes: 2 additions & 2 deletions compiler/layouter.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
37 changes: 20 additions & 17 deletions compiler/lexer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions compiler/parser.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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{=})
Expand Down
2 changes: 1 addition & 1 deletion nimpretty/tests/exhaustive.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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(" ")
Expand Down
2 changes: 1 addition & 1 deletion nimpretty/tests/expected/exhaustive.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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(" ")
Expand Down

0 comments on commit a15872b

Please sign in to comment.