[bugfix] nimpretty: fixes #11468

This commit is contained in:
Araq 2019-06-13 10:42:37 +02:00
commit af39f26a3a
5 changed files with 39 additions and 19 deletions

View file

@ -173,7 +173,7 @@ proc isNimIdentifier*(s: string): bool =
inc(i) inc(i)
result = true result = true
proc tokToStr*(tok: TToken): string = proc `$`*(tok: TToken): string =
case tok.tokType case tok.tokType
of tkIntLit..tkInt64Lit: result = $tok.iNumber of tkIntLit..tkInt64Lit: result = $tok.iNumber
of tkFloatLit..tkFloat64Lit: result = $tok.fNumber of tkFloatLit..tkFloat64Lit: result = $tok.fNumber
@ -188,11 +188,11 @@ proc tokToStr*(tok: TToken): string =
proc prettyTok*(tok: TToken): string = proc prettyTok*(tok: TToken): string =
if isKeyword(tok.tokType): result = "keyword " & tok.ident.s if isKeyword(tok.tokType): result = "keyword " & tok.ident.s
else: result = tokToStr(tok) else: result = $tok
proc printTok*(conf: ConfigRef; tok: TToken) = proc printTok*(conf: ConfigRef; tok: TToken) =
msgWriteln(conf, $tok.line & ":" & $tok.col & "\t" & msgWriteln(conf, $tok.line & ":" & $tok.col & "\t" &
TokTypeToStr[tok.tokType] & " " & tokToStr(tok)) TokTypeToStr[tok.tokType] & " " & $tok)
proc initToken*(L: var TToken) = proc initToken*(L: var TToken) =
L.tokType = tkInvalid L.tokType = tkInvalid

View file

@ -123,31 +123,31 @@ proc parseDirective(L: var TLexer, tok: var TToken; config: ConfigRef; condStack
of wEnd: doEnd(L, tok, condStack) of wEnd: doEnd(L, tok, condStack)
of wWrite: of wWrite:
ppGetTok(L, tok) ppGetTok(L, tok)
msgs.msgWriteln(config, strtabs.`%`(tokToStr(tok), config.configVars, msgs.msgWriteln(config, strtabs.`%`($tok, config.configVars,
{useEnvironment, useKey})) {useEnvironment, useKey}))
ppGetTok(L, tok) ppGetTok(L, tok)
else: else:
case tok.ident.s.normalize case tok.ident.s.normalize
of "putenv": of "putenv":
ppGetTok(L, tok) ppGetTok(L, tok)
var key = tokToStr(tok) var key = $tok
ppGetTok(L, tok) ppGetTok(L, tok)
os.putEnv(key, tokToStr(tok)) os.putEnv(key, $tok)
ppGetTok(L, tok) ppGetTok(L, tok)
of "prependenv": of "prependenv":
ppGetTok(L, tok) ppGetTok(L, tok)
var key = tokToStr(tok) var key = $tok
ppGetTok(L, tok) ppGetTok(L, tok)
os.putEnv(key, tokToStr(tok) & os.getEnv(key)) os.putEnv(key, $tok & os.getEnv(key))
ppGetTok(L, tok) ppGetTok(L, tok)
of "appendenv": of "appendenv":
ppGetTok(L, tok) ppGetTok(L, tok)
var key = tokToStr(tok) var key = $tok
ppGetTok(L, tok) ppGetTok(L, tok)
os.putEnv(key, os.getEnv(key) & tokToStr(tok)) os.putEnv(key, os.getEnv(key) & $tok)
ppGetTok(L, tok) ppGetTok(L, tok)
else: else:
lexMessage(L, errGenerated, "invalid directive: '$1'" % tokToStr(tok)) lexMessage(L, errGenerated, "invalid directive: '$1'" % $tok)
proc confTok(L: var TLexer, tok: var TToken; config: ConfigRef; condStack: var seq[bool]) = proc confTok(L: var TLexer, tok: var TToken; config: ConfigRef; condStack: var seq[bool]) =
ppGetTok(L, tok) ppGetTok(L, tok)
@ -156,7 +156,7 @@ proc confTok(L: var TLexer, tok: var TToken; config: ConfigRef; condStack: var s
proc checkSymbol(L: TLexer, tok: TToken) = proc checkSymbol(L: TLexer, tok: TToken) =
if tok.tokType notin {tkSymbol..tkInt64Lit, tkStrLit..tkTripleStrLit}: if tok.tokType notin {tkSymbol..tkInt64Lit, tkStrLit..tkTripleStrLit}:
lexMessage(L, errGenerated, "expected identifier, but got: " & tokToStr(tok)) lexMessage(L, errGenerated, "expected identifier, but got: " & $tok)
proc parseAssignment(L: var TLexer, tok: var TToken; proc parseAssignment(L: var TLexer, tok: var TToken;
config: ConfigRef; condStack: var seq[bool]) = config: ConfigRef; condStack: var seq[bool]) =
@ -164,21 +164,21 @@ proc parseAssignment(L: var TLexer, tok: var TToken;
confTok(L, tok, config, condStack) # skip unnecessary prefix confTok(L, tok, config, condStack) # skip unnecessary prefix
var info = getLineInfo(L, tok) # save for later in case of an error var info = getLineInfo(L, tok) # save for later in case of an error
checkSymbol(L, tok) checkSymbol(L, tok)
var s = tokToStr(tok) var s = $tok
confTok(L, tok, config, condStack) # skip symbol confTok(L, tok, config, condStack) # skip symbol
var val = "" var val = ""
while tok.tokType == tkDot: while tok.tokType == tkDot:
add(s, '.') add(s, '.')
confTok(L, tok, config, condStack) confTok(L, tok, config, condStack)
checkSymbol(L, tok) checkSymbol(L, tok)
add(s, tokToStr(tok)) add(s, $tok)
confTok(L, tok, config, condStack) confTok(L, tok, config, condStack)
if tok.tokType == tkBracketLe: if tok.tokType == tkBracketLe:
# BUGFIX: val, not s! # BUGFIX: val, not s!
confTok(L, tok, config, condStack) confTok(L, tok, config, condStack)
checkSymbol(L, tok) checkSymbol(L, tok)
add(val, '[') add(val, '[')
add(val, tokToStr(tok)) add(val, $tok)
confTok(L, tok, config, condStack) confTok(L, tok, config, condStack)
if tok.tokType == tkBracketRi: confTok(L, tok, config, condStack) if tok.tokType == tkBracketRi: confTok(L, tok, config, condStack)
else: lexMessage(L, errGenerated, "expected closing ']'") else: lexMessage(L, errGenerated, "expected closing ']'")
@ -188,12 +188,12 @@ proc parseAssignment(L: var TLexer, tok: var TToken;
if len(val) > 0: add(val, ':') if len(val) > 0: add(val, ':')
confTok(L, tok, config, condStack) # skip ':' or '=' or '%' confTok(L, tok, config, condStack) # skip ':' or '=' or '%'
checkSymbol(L, tok) checkSymbol(L, tok)
add(val, tokToStr(tok)) add(val, $tok)
confTok(L, tok, config, condStack) # skip symbol confTok(L, tok, config, condStack) # skip symbol
while tok.ident != nil and tok.ident.s == "&": while tok.ident != nil and tok.ident.s == "&":
confTok(L, tok, config, condStack) confTok(L, tok, config, condStack)
checkSymbol(L, tok) checkSymbol(L, tok)
add(val, tokToStr(tok)) add(val, $tok)
confTok(L, tok, config, condStack) confTok(L, tok, config, condStack)
if percent: if percent:
processSwitch(s, strtabs.`%`(val, config.configVars, processSwitch(s, strtabs.`%`(val, config.configVars,

View file

@ -343,13 +343,13 @@ proc parseSymbol(p: var TParser, mode = smNormal): PNode =
var accm = "" var accm = ""
while p.tok.tokType in {tkOpr, tkDot, tkDotDot, tkEquals, while p.tok.tokType in {tkOpr, tkDot, tkDotDot, tkEquals,
tkParLe..tkParDotRi}: tkParLe..tkParDotRi}:
accm.add(tokToStr(p.tok)) accm.add($p.tok)
getTok(p) getTok(p)
let node = newNodeI(nkIdent, lineinfo) let node = newNodeI(nkIdent, lineinfo)
node.ident = p.lex.cache.getIdent(accm) node.ident = p.lex.cache.getIdent(accm)
result.add(node) result.add(node)
of tokKeywordLow..tokKeywordHigh, tkSymbol, tkIntLit..tkCharLit: of tokKeywordLow..tokKeywordHigh, tkSymbol, tkIntLit..tkCharLit:
result.add(newIdentNodeP(p.lex.cache.getIdent(tokToStr(p.tok)), p)) result.add(newIdentNodeP(p.lex.cache.getIdent($p.tok), p))
getTok(p) getTok(p)
else: else:
parMessage(p, errIdentifierExpected, p.tok) parMessage(p, errIdentifierExpected, p.tok)
@ -903,6 +903,8 @@ proc parsePragma(p: var TParser): PNode =
#| pragma = '{.' optInd (exprColonExpr comma?)* optPar ('.}' | '}') #| pragma = '{.' optInd (exprColonExpr comma?)* optPar ('.}' | '}')
result = newNodeP(nkPragma, p) result = newNodeP(nkPragma, p)
inc p.inPragma inc p.inPragma
when defined(nimpretty):
inc p.em.keepIndents
getTok(p) getTok(p)
optInd(p, result) optInd(p, result)
while p.tok.tokType notin {tkCurlyDotRi, tkCurlyRi, tkEof}: while p.tok.tokType notin {tkCurlyDotRi, tkCurlyRi, tkEof}:
@ -921,6 +923,8 @@ proc parsePragma(p: var TParser): PNode =
else: else:
parMessage(p, "expected '.}'") parMessage(p, "expected '.}'")
dec p.inPragma dec p.inPragma
when defined(nimpretty):
dec p.em.keepIndents
proc identVis(p: var TParser; allowDot=false): PNode = proc identVis(p: var TParser; allowDot=false): PNode =
#| identVis = symbol opr? # postfix position #| identVis = symbol opr? # postfix position

View file

@ -409,3 +409,11 @@ type
ccFastCall, # fastcall (pass parameters in registers) ccFastCall, # fastcall (pass parameters in registers)
ccClosure, # proc has a closure ccClosure, # proc has a closure
ccNoConvention # needed for generating proper C procs sometimes ccNoConvention # needed for generating proper C procs sometimes
proc isValid1*[A](s: HashSet[A]): bool {.deprecated:
"Deprecated since v0.20; sets are initialized by default".} =
## Returns `true` if the set has been initialized (with `initHashSet proc
## <#initHashSet,int>`_ or `init proc <#init,HashSet[A],int>`_).
result = s.data.len > 0
# bug #11468

View file

@ -419,3 +419,11 @@ type
ccFastCall, # fastcall (pass parameters in registers) ccFastCall, # fastcall (pass parameters in registers)
ccClosure, # proc has a closure ccClosure, # proc has a closure
ccNoConvention # needed for generating proper C procs sometimes ccNoConvention # needed for generating proper C procs sometimes
proc isValid1*[A](s: HashSet[A]): bool {.deprecated:
"Deprecated since v0.20; sets are initialized by default".} =
## Returns `true` if the set has been initialized (with `initHashSet proc
## <#initHashSet,int>`_ or `init proc <#init,HashSet[A],int>`_).
result = s.data.len > 0
# bug #11468