parent
31be01d78f
commit
a15872ba9e
5 changed files with 26 additions and 23 deletions
|
|
@ -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)
|
||||||
|
|
|
||||||
|
|
@ -93,6 +93,9 @@ type
|
||||||
# so that it is the correct default value
|
# so that it is the correct default value
|
||||||
base2, base8, base16
|
base2, base8, base16
|
||||||
|
|
||||||
|
TokenSpacing* = enum
|
||||||
|
tsNone, tsTrailing, tsEof
|
||||||
|
|
||||||
Token* = object # a Nim token
|
Token* = object # a Nim token
|
||||||
tokType*: TokType # the type of the token
|
tokType*: TokType # the type of the token
|
||||||
indent*: int # the indentation; != -1 if the token has been
|
indent*: int # the indentation; != -1 if the token has been
|
||||||
|
|
@ -103,7 +106,7 @@ type
|
||||||
base*: NumericalBase # the numerical base; only valid for int
|
base*: NumericalBase # the numerical base; only valid for int
|
||||||
# or float literals
|
# or float literals
|
||||||
strongSpaceA*: bool # leading spaces of an operator
|
strongSpaceA*: bool # leading spaces of an operator
|
||||||
strongSpaceB*: int8 # trailing spaces of an operator
|
strongSpaceB*: TokenSpacing # trailing spaces of an operator
|
||||||
literal*: string # the parsed (string) literal; and
|
literal*: string # the parsed (string) literal; and
|
||||||
# documentation comments are here too
|
# documentation comments are here too
|
||||||
line*, col*: int
|
line*, col*: int
|
||||||
|
|
@ -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.
|
||||||
|
|
|
||||||
|
|
@ -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{=})
|
||||||
|
|
|
||||||
|
|
@ -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(" ")
|
||||||
|
|
|
||||||
|
|
@ -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(" ")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue