From eddf9abd13f4db6acd71ccda1f8ccfe1232ace53 Mon Sep 17 00:00:00 2001 From: Andreas Rumpf Date: Thu, 5 Oct 2017 08:43:10 +0200 Subject: beginnings of the new nimpretty tool; still unusable --- compiler/renderer.nim | 308 ++++++++++++++++++++++++++------------------------ 1 file changed, 159 insertions(+), 149 deletions(-) (limited to 'compiler/renderer.nim') diff --git a/compiler/renderer.nim b/compiler/renderer.nim index bbe81fe37..c0bea793d 100644 --- a/compiler/renderer.nim +++ b/compiler/renderer.nim @@ -17,12 +17,12 @@ type renderNone, renderNoBody, renderNoComments, renderDocComments, renderNoPragmas, renderIds, renderNoProcDefs TRenderFlags* = set[TRenderFlag] - TRenderTok*{.final.} = object + TRenderTok* = object kind*: TTokType length*: int16 TRenderTokSeq* = seq[TRenderTok] - TSrcGen*{.final.} = object + TSrcGen* = object indent*: int lineLen*: int pos*: int # current position for iteration over the buffer @@ -37,15 +37,10 @@ type inGenericParams: bool checkAnon: bool # we're in a context that can contain sfAnon inPragma: int + when defined(nimpretty): + origContent: string -proc renderModule*(n: PNode, filename: string, renderFlags: TRenderFlags = {}) -proc renderTree*(n: PNode, renderFlags: TRenderFlags = {}): string -proc initTokRender*(r: var TSrcGen, n: PNode, renderFlags: TRenderFlags = {}) -proc getNextTok*(r: var TSrcGen, kind: var TTokType, literal: var string) - -proc `$`*(n: PNode): string = n.renderTree -# implementation # We render the source code in a two phases: The first # determines how long the subtree will likely be, the second # phase appends to a buffer that will be the output. @@ -284,8 +279,8 @@ proc gcoms(g: var TSrcGen) = for i in countup(0, high(g.comStack)): gcom(g, g.comStack[i]) popAllComs(g) -proc lsub(n: PNode): int -proc litAux(n: PNode, x: BiggestInt, size: int): string = +proc lsub(g: TSrcGen; n: PNode): int +proc litAux(g: TSrcGen; n: PNode, x: BiggestInt, size: int): string = proc skip(t: PType): PType = result = t while result.kind in {tyGenericInst, tyRange, tyVar, tyDistinct, @@ -302,14 +297,17 @@ proc litAux(n: PNode, x: BiggestInt, size: int): string = elif nfBase16 in n.flags: result = "0x" & toHex(x, size * 2) else: result = $x -proc ulitAux(n: PNode, x: BiggestInt, size: int): string = +proc ulitAux(g: TSrcGen; n: PNode, x: BiggestInt, size: int): string = if nfBase2 in n.flags: result = "0b" & toBin(x, size * 8) elif nfBase8 in n.flags: result = "0o" & toOct(x, size * 3) elif nfBase16 in n.flags: result = "0x" & toHex(x, size * 2) else: result = $x # XXX proper unsigned output! -proc atom(n: PNode): string = +proc atom(g: TSrcGen; n: PNode): string = + when defined(nimpretty): + if true: + return substr(g.origContent, n.info.offsetA, n.info.offsetB) var f: float32 case n.kind of nkEmpty: result = "" @@ -319,30 +317,30 @@ proc atom(n: PNode): string = of nkRStrLit: result = "r\"" & replace(n.strVal, "\"", "\"\"") & '\"' of nkTripleStrLit: result = "\"\"\"" & n.strVal & "\"\"\"" of nkCharLit: result = '\'' & toNimChar(chr(int(n.intVal))) & '\'' - of nkIntLit: result = litAux(n, n.intVal, 4) - of nkInt8Lit: result = litAux(n, n.intVal, 1) & "\'i8" - of nkInt16Lit: result = litAux(n, n.intVal, 2) & "\'i16" - of nkInt32Lit: result = litAux(n, n.intVal, 4) & "\'i32" - of nkInt64Lit: result = litAux(n, n.intVal, 8) & "\'i64" - of nkUIntLit: result = ulitAux(n, n.intVal, 4) & "\'u" - of nkUInt8Lit: result = ulitAux(n, n.intVal, 1) & "\'u8" - of nkUInt16Lit: result = ulitAux(n, n.intVal, 2) & "\'u16" - of nkUInt32Lit: result = ulitAux(n, n.intVal, 4) & "\'u32" - of nkUInt64Lit: result = ulitAux(n, n.intVal, 8) & "\'u64" + of nkIntLit: result = litAux(g, n, n.intVal, 4) + of nkInt8Lit: result = litAux(g, n, n.intVal, 1) & "\'i8" + of nkInt16Lit: result = litAux(g, n, n.intVal, 2) & "\'i16" + of nkInt32Lit: result = litAux(g, n, n.intVal, 4) & "\'i32" + of nkInt64Lit: result = litAux(g, n, n.intVal, 8) & "\'i64" + of nkUIntLit: result = ulitAux(g, n, n.intVal, 4) & "\'u" + of nkUInt8Lit: result = ulitAux(g, n, n.intVal, 1) & "\'u8" + of nkUInt16Lit: result = ulitAux(g, n, n.intVal, 2) & "\'u16" + of nkUInt32Lit: result = ulitAux(g, n, n.intVal, 4) & "\'u32" + of nkUInt64Lit: result = ulitAux(g, n, n.intVal, 8) & "\'u64" of nkFloatLit: if n.flags * {nfBase2, nfBase8, nfBase16} == {}: result = $(n.floatVal) - else: result = litAux(n, (cast[PInt64](addr(n.floatVal)))[] , 8) + else: result = litAux(g, n, (cast[PInt64](addr(n.floatVal)))[] , 8) of nkFloat32Lit: if n.flags * {nfBase2, nfBase8, nfBase16} == {}: result = $n.floatVal & "\'f32" else: f = n.floatVal.float32 - result = litAux(n, (cast[PInt32](addr(f)))[], 4) & "\'f32" + result = litAux(g, n, (cast[PInt32](addr(f)))[], 4) & "\'f32" of nkFloat64Lit: if n.flags * {nfBase2, nfBase8, nfBase16} == {}: result = $n.floatVal & "\'f64" else: - result = litAux(n, (cast[PInt64](addr(n.floatVal)))[], 8) & "\'f64" + result = litAux(g, n, (cast[PInt64](addr(n.floatVal)))[], 8) & "\'f64" of nkNilLit: result = "nil" of nkType: if (n.typ != nil) and (n.typ.sym != nil): result = n.typ.sym.name.s @@ -351,21 +349,21 @@ proc atom(n: PNode): string = internalError("rnimsyn.atom " & $n.kind) result = "" -proc lcomma(n: PNode, start: int = 0, theEnd: int = - 1): int = +proc lcomma(g: TSrcGen; n: PNode, start: int = 0, theEnd: int = - 1): int = assert(theEnd < 0) result = 0 for i in countup(start, sonsLen(n) + theEnd): - inc(result, lsub(n.sons[i])) + inc(result, lsub(g, n.sons[i])) inc(result, 2) # for ``, `` if result > 0: dec(result, 2) # last does not get a comma! -proc lsons(n: PNode, start: int = 0, theEnd: int = - 1): int = +proc lsons(g: TSrcGen; n: PNode, start: int = 0, theEnd: int = - 1): int = assert(theEnd < 0) result = 0 - for i in countup(start, sonsLen(n) + theEnd): inc(result, lsub(n.sons[i])) + for i in countup(start, sonsLen(n) + theEnd): inc(result, lsub(g, n.sons[i])) -proc lsub(n: PNode): int = +proc lsub(g: TSrcGen; n: PNode): int = # computes the length of a tree if isNil(n): return 0 if n.comment != nil: return MaxLineLen + 1 @@ -373,108 +371,108 @@ proc lsub(n: PNode): int = of nkEmpty: result = 0 of nkTripleStrLit: if containsNL(n.strVal): result = MaxLineLen + 1 - else: result = len(atom(n)) + else: result = len(atom(g, n)) of succ(nkEmpty)..pred(nkTripleStrLit), succ(nkTripleStrLit)..nkNilLit: - result = len(atom(n)) + result = len(atom(g, n)) of nkCall, nkBracketExpr, nkCurlyExpr, nkConv, nkPattern, nkObjConstr: - result = lsub(n.sons[0]) + lcomma(n, 1) + 2 - of nkHiddenStdConv, nkHiddenSubConv, nkHiddenCallConv: result = lsub(n[1]) - of nkCast: result = lsub(n.sons[0]) + lsub(n.sons[1]) + len("cast[]()") - of nkAddr: result = (if n.len>0: lsub(n.sons[0]) + len("addr()") else: 4) - of nkStaticExpr: result = lsub(n.sons[0]) + len("static_") - of nkHiddenAddr, nkHiddenDeref: result = lsub(n.sons[0]) - of nkCommand: result = lsub(n.sons[0]) + lcomma(n, 1) + 1 - of nkExprEqExpr, nkAsgn, nkFastAsgn: result = lsons(n) + 3 - of nkPar, nkCurly, nkBracket, nkClosure: result = lcomma(n) + 2 - of nkArgList: result = lcomma(n) + result = lsub(g, n.sons[0]) + lcomma(g, n, 1) + 2 + of nkHiddenStdConv, nkHiddenSubConv, nkHiddenCallConv: result = lsub(g, n[1]) + of nkCast: result = lsub(g, n.sons[0]) + lsub(g, n.sons[1]) + len("cast[]()") + of nkAddr: result = (if n.len>0: lsub(g, n.sons[0]) + len("addr()") else: 4) + of nkStaticExpr: result = lsub(g, n.sons[0]) + len("static_") + of nkHiddenAddr, nkHiddenDeref: result = lsub(g, n.sons[0]) + of nkCommand: result = lsub(g, n.sons[0]) + lcomma(g, n, 1) + 1 + of nkExprEqExpr, nkAsgn, nkFastAsgn: result = lsons(g, n) + 3 + of nkPar, nkCurly, nkBracket, nkClosure: result = lcomma(g, n) + 2 + of nkArgList: result = lcomma(g, n) of nkTableConstr: - result = if n.len > 0: lcomma(n) + 2 else: len("{:}") + result = if n.len > 0: lcomma(g, n) + 2 else: len("{:}") of nkClosedSymChoice, nkOpenSymChoice: - result = lsons(n) + len("()") + sonsLen(n) - 1 - of nkTupleTy: result = lcomma(n) + len("tuple[]") + result = lsons(g, n) + len("()") + sonsLen(n) - 1 + of nkTupleTy: result = lcomma(g, n) + len("tuple[]") of nkTupleClassTy: result = len("tuple") - of nkDotExpr: result = lsons(n) + 1 - of nkBind: result = lsons(n) + len("bind_") - of nkBindStmt: result = lcomma(n) + len("bind_") - of nkMixinStmt: result = lcomma(n) + len("mixin_") - of nkCheckedFieldExpr: result = lsub(n.sons[0]) - of nkLambda: result = lsons(n) + len("proc__=_") - of nkDo: result = lsons(n) + len("do__:_") + of nkDotExpr: result = lsons(g, n) + 1 + of nkBind: result = lsons(g, n) + len("bind_") + of nkBindStmt: result = lcomma(g, n) + len("bind_") + of nkMixinStmt: result = lcomma(g, n) + len("mixin_") + of nkCheckedFieldExpr: result = lsub(g, n.sons[0]) + of nkLambda: result = lsons(g, n) + len("proc__=_") + of nkDo: result = lsons(g, n) + len("do__:_") of nkConstDef, nkIdentDefs: - result = lcomma(n, 0, - 3) + result = lcomma(g, n, 0, - 3) var L = sonsLen(n) - if n.sons[L - 2].kind != nkEmpty: result = result + lsub(n.sons[L - 2]) + 2 - if n.sons[L - 1].kind != nkEmpty: result = result + lsub(n.sons[L - 1]) + 3 - of nkVarTuple: result = lcomma(n, 0, - 3) + len("() = ") + lsub(lastSon(n)) - of nkChckRangeF: result = len("chckRangeF") + 2 + lcomma(n) - of nkChckRange64: result = len("chckRange64") + 2 + lcomma(n) - of nkChckRange: result = len("chckRange") + 2 + lcomma(n) + if n.sons[L - 2].kind != nkEmpty: result = result + lsub(g, n.sons[L - 2]) + 2 + if n.sons[L - 1].kind != nkEmpty: result = result + lsub(g, n.sons[L - 1]) + 3 + of nkVarTuple: result = lcomma(g, n, 0, - 3) + len("() = ") + lsub(g, lastSon(n)) + of nkChckRangeF: result = len("chckRangeF") + 2 + lcomma(g, n) + of nkChckRange64: result = len("chckRange64") + 2 + lcomma(g, n) + of nkChckRange: result = len("chckRange") + 2 + lcomma(g, n) of nkObjDownConv, nkObjUpConv, nkStringToCString, nkCStringToString: result = 2 - if sonsLen(n) >= 1: result = result + lsub(n.sons[0]) - result = result + lcomma(n, 1) - of nkExprColonExpr: result = lsons(n) + 2 - of nkInfix: result = lsons(n) + 2 + if sonsLen(n) >= 1: result = result + lsub(g, n.sons[0]) + result = result + lcomma(g, n, 1) + of nkExprColonExpr: result = lsons(g, n) + 2 + of nkInfix: result = lsons(g, n) + 2 of nkPrefix: - result = lsons(n)+1+(if n.len > 0 and n.sons[1].kind == nkInfix: 2 else: 0) - of nkPostfix: result = lsons(n) - of nkCallStrLit: result = lsons(n) - of nkPragmaExpr: result = lsub(n.sons[0]) + lcomma(n, 1) - of nkRange: result = lsons(n) + 2 - of nkDerefExpr: result = lsub(n.sons[0]) + 2 - of nkAccQuoted: result = lsons(n) + 2 + result = lsons(g, n)+1+(if n.len > 0 and n.sons[1].kind == nkInfix: 2 else: 0) + of nkPostfix: result = lsons(g, n) + of nkCallStrLit: result = lsons(g, n) + of nkPragmaExpr: result = lsub(g, n.sons[0]) + lcomma(g, n, 1) + of nkRange: result = lsons(g, n) + 2 + of nkDerefExpr: result = lsub(g, n.sons[0]) + 2 + of nkAccQuoted: result = lsons(g, n) + 2 of nkIfExpr: - result = lsub(n.sons[0].sons[0]) + lsub(n.sons[0].sons[1]) + lsons(n, 1) + + result = lsub(g, n.sons[0].sons[0]) + lsub(g, n.sons[0].sons[1]) + lsons(g, n, 1) + len("if_:_") - of nkElifExpr: result = lsons(n) + len("_elif_:_") - of nkElseExpr: result = lsub(n.sons[0]) + len("_else:_") # type descriptions - of nkTypeOfExpr: result = (if n.len > 0: lsub(n.sons[0]) else: 0)+len("type()") - of nkRefTy: result = (if n.len > 0: lsub(n.sons[0])+1 else: 0) + len("ref") - of nkPtrTy: result = (if n.len > 0: lsub(n.sons[0])+1 else: 0) + len("ptr") - of nkVarTy: result = (if n.len > 0: lsub(n.sons[0])+1 else: 0) + len("var") + of nkElifExpr: result = lsons(g, n) + len("_elif_:_") + of nkElseExpr: result = lsub(g, n.sons[0]) + len("_else:_") # type descriptions + of nkTypeOfExpr: result = (if n.len > 0: lsub(g, n.sons[0]) else: 0)+len("type()") + of nkRefTy: result = (if n.len > 0: lsub(g, n.sons[0])+1 else: 0) + len("ref") + of nkPtrTy: result = (if n.len > 0: lsub(g, n.sons[0])+1 else: 0) + len("ptr") + of nkVarTy: result = (if n.len > 0: lsub(g, n.sons[0])+1 else: 0) + len("var") of nkDistinctTy: - result = len("distinct") + (if n.len > 0: lsub(n.sons[0])+1 else: 0) + result = len("distinct") + (if n.len > 0: lsub(g, n.sons[0])+1 else: 0) if n.len > 1: result += (if n[1].kind == nkWith: len("_with_") else: len("_without_")) - result += lcomma(n[1]) - of nkStaticTy: result = (if n.len > 0: lsub(n.sons[0]) else: 0) + + result += lcomma(g, n[1]) + of nkStaticTy: result = (if n.len > 0: lsub(g, n.sons[0]) else: 0) + len("static[]") - of nkTypeDef: result = lsons(n) + 3 - of nkOfInherit: result = lsub(n.sons[0]) + len("of_") - of nkProcTy: result = lsons(n) + len("proc_") - of nkIteratorTy: result = lsons(n) + len("iterator_") - of nkSharedTy: result = lsons(n) + len("shared_") + of nkTypeDef: result = lsons(g, n) + 3 + of nkOfInherit: result = lsub(g, n.sons[0]) + len("of_") + of nkProcTy: result = lsons(g, n) + len("proc_") + of nkIteratorTy: result = lsons(g, n) + len("iterator_") + of nkSharedTy: result = lsons(g, n) + len("shared_") of nkEnumTy: if sonsLen(n) > 0: - result = lsub(n.sons[0]) + lcomma(n, 1) + len("enum_") + result = lsub(g, n.sons[0]) + lcomma(g, n, 1) + len("enum_") else: result = len("enum") - of nkEnumFieldDef: result = lsons(n) + 3 + of nkEnumFieldDef: result = lsons(g, n) + 3 of nkVarSection, nkLetSection: if sonsLen(n) > 1: result = MaxLineLen + 1 - else: result = lsons(n) + len("var_") + else: result = lsons(g, n) + len("var_") of nkUsingStmt: if sonsLen(n) > 1: result = MaxLineLen + 1 - else: result = lsons(n) + len("using_") - of nkReturnStmt: result = lsub(n.sons[0]) + len("return_") - of nkRaiseStmt: result = lsub(n.sons[0]) + len("raise_") - of nkYieldStmt: result = lsub(n.sons[0]) + len("yield_") - of nkDiscardStmt: result = lsub(n.sons[0]) + len("discard_") - of nkBreakStmt: result = lsub(n.sons[0]) + len("break_") - of nkContinueStmt: result = lsub(n.sons[0]) + len("continue_") - of nkPragma: result = lcomma(n) + 4 + else: result = lsons(g, n) + len("using_") + of nkReturnStmt: result = lsub(g, n.sons[0]) + len("return_") + of nkRaiseStmt: result = lsub(g, n.sons[0]) + len("raise_") + of nkYieldStmt: result = lsub(g, n.sons[0]) + len("yield_") + of nkDiscardStmt: result = lsub(g, n.sons[0]) + len("discard_") + of nkBreakStmt: result = lsub(g, n.sons[0]) + len("break_") + of nkContinueStmt: result = lsub(g, n.sons[0]) + len("continue_") + of nkPragma: result = lcomma(g, n) + 4 of nkCommentStmt: result = if n.comment.isNil: 0 else: len(n.comment) - of nkOfBranch: result = lcomma(n, 0, - 2) + lsub(lastSon(n)) + len("of_:_") - of nkImportAs: result = lsub(n.sons[0]) + len("_as_") + lsub(n.sons[1]) - of nkElifBranch: result = lsons(n) + len("elif_:_") - of nkElse: result = lsub(n.sons[0]) + len("else:_") - of nkFinally: result = lsub(n.sons[0]) + len("finally:_") - of nkGenericParams: result = lcomma(n) + 2 + of nkOfBranch: result = lcomma(g, n, 0, - 2) + lsub(g, lastSon(n)) + len("of_:_") + of nkImportAs: result = lsub(g, n.sons[0]) + len("_as_") + lsub(g, n.sons[1]) + of nkElifBranch: result = lsons(g, n) + len("elif_:_") + of nkElse: result = lsub(g, n.sons[0]) + len("else:_") + of nkFinally: result = lsub(g, n.sons[0]) + len("finally:_") + of nkGenericParams: result = lcomma(g, n) + 2 of nkFormalParams: - result = lcomma(n, 1) + 2 - if n.sons[0].kind != nkEmpty: result = result + lsub(n.sons[0]) + 2 + result = lcomma(g, n, 1) + 2 + if n.sons[0].kind != nkEmpty: result = result + lsub(g, n.sons[0]) + 2 of nkExceptBranch: - result = lcomma(n, 0, -2) + lsub(lastSon(n)) + len("except_:_") + result = lcomma(g, n, 0, -2) + lsub(g, lastSon(n)) + len("except_:_") else: result = MaxLineLen + 1 proc fits(g: TSrcGen, x: int): bool = @@ -517,7 +515,7 @@ proc gcommaAux(g: var TSrcGen, n: PNode, ind: int, start: int = 0, theEnd: int = - 1, separator = tkComma) = for i in countup(start, sonsLen(n) + theEnd): var c = i < sonsLen(n) + theEnd - var sublen = lsub(n.sons[i]) + ord(c) + var sublen = lsub(g, n.sons[i]) + ord(c) if not fits(g, sublen) and (ind + sublen < MaxLineLen): optNL(g, ind) let oldLen = g.tokens.len gsub(g, n.sons[i]) @@ -564,12 +562,12 @@ proc gsection(g: var TSrcGen, n: PNode, c: TContext, kind: TTokType, gcoms(g) dedent(g) -proc longMode(n: PNode, start: int = 0, theEnd: int = - 1): bool = +proc longMode(g: TSrcGen; n: PNode, start: int = 0, theEnd: int = - 1): bool = result = n.comment != nil if not result: # check further for i in countup(start, sonsLen(n) + theEnd): - if (lsub(n.sons[i]) > MaxLineLen): + if (lsub(g, n.sons[i]) > MaxLineLen): result = true break @@ -597,7 +595,7 @@ proc gif(g: var TSrcGen, n: PNode) = gsub(g, n.sons[0].sons[0]) initContext(c) putWithSpace(g, tkColon, ":") - if longMode(n) or (lsub(n.sons[0].sons[1]) + g.lineLen > MaxLineLen): + if longMode(g, n) or (lsub(g, n.sons[0].sons[1]) + g.lineLen > MaxLineLen): incl(c.flags, rfLongMode) gcoms(g) # a good place for comments gstmts(g, n.sons[0].sons[1], c) @@ -612,7 +610,7 @@ proc gwhile(g: var TSrcGen, n: PNode) = gsub(g, n.sons[0]) putWithSpace(g, tkColon, ":") initContext(c) - if longMode(n) or (lsub(n.sons[1]) + g.lineLen > MaxLineLen): + if longMode(g, n) or (lsub(g, n.sons[1]) + g.lineLen > MaxLineLen): incl(c.flags, rfLongMode) gcoms(g) # a good place for comments gstmts(g, n.sons[1], c) @@ -621,7 +619,7 @@ proc gpattern(g: var TSrcGen, n: PNode) = var c: TContext put(g, tkCurlyLe, "{") initContext(c) - if longMode(n) or (lsub(n.sons[0]) + g.lineLen > MaxLineLen): + if longMode(g, n) or (lsub(g, n.sons[0]) + g.lineLen > MaxLineLen): incl(c.flags, rfLongMode) gcoms(g) # a good place for comments gstmts(g, n, c) @@ -632,7 +630,7 @@ proc gpragmaBlock(g: var TSrcGen, n: PNode) = gsub(g, n.sons[0]) putWithSpace(g, tkColon, ":") initContext(c) - if longMode(n) or (lsub(n.sons[1]) + g.lineLen > MaxLineLen): + if longMode(g, n) or (lsub(g, n.sons[1]) + g.lineLen > MaxLineLen): incl(c.flags, rfLongMode) gcoms(g) # a good place for comments gstmts(g, n.sons[1], c) @@ -642,7 +640,7 @@ proc gtry(g: var TSrcGen, n: PNode) = put(g, tkTry, "try") putWithSpace(g, tkColon, ":") initContext(c) - if longMode(n) or (lsub(n.sons[0]) + g.lineLen > MaxLineLen): + if longMode(g, n) or (lsub(g, n.sons[0]) + g.lineLen > MaxLineLen): incl(c.flags, rfLongMode) gcoms(g) # a good place for comments gstmts(g, n.sons[0], c) @@ -653,8 +651,8 @@ proc gfor(g: var TSrcGen, n: PNode) = var length = sonsLen(n) putWithSpace(g, tkFor, "for") initContext(c) - if longMode(n) or - (lsub(n.sons[length - 1]) + lsub(n.sons[length - 2]) + 6 + g.lineLen > + if longMode(g, n) or + (lsub(g, n.sons[length - 1]) + lsub(g, n.sons[length - 2]) + 6 + g.lineLen > MaxLineLen): incl(c.flags, rfLongMode) gcomma(g, n, c, 0, - 3) @@ -670,7 +668,7 @@ proc gcase(g: var TSrcGen, n: PNode) = initContext(c) var length = sonsLen(n) var last = if n.sons[length-1].kind == nkElse: -2 else: -1 - if longMode(n, 0, last): incl(c.flags, rfLongMode) + if longMode(g, n, 0, last): incl(c.flags, rfLongMode) putWithSpace(g, tkCase, "case") gsub(g, n.sons[0]) gcoms(g) @@ -678,7 +676,7 @@ proc gcase(g: var TSrcGen, n: PNode) = gsons(g, n, c, 1, last) if last == - 2: initContext(c) - if longMode(n.sons[length - 1]): incl(c.flags, rfLongMode) + if longMode(g, n.sons[length - 1]): incl(c.flags, rfLongMode) gsub(g, n.sons[length - 1], c) proc gproc(g: var TSrcGen, n: PNode) = @@ -740,7 +738,7 @@ proc gblock(g: var TSrcGen, n: PNode) = else: put(g, tkBlock, "block") putWithSpace(g, tkColon, ":") - if longMode(n) or (lsub(n.sons[1]) + g.lineLen > MaxLineLen): + if longMode(g, n) or (lsub(g, n.sons[1]) + g.lineLen > MaxLineLen): incl(c.flags, rfLongMode) gcoms(g) # XXX I don't get why this is needed here! gstmts should already handle this! @@ -753,7 +751,7 @@ proc gstaticStmt(g: var TSrcGen, n: PNode) = putWithSpace(g, tkStatic, "static") putWithSpace(g, tkColon, ":") initContext(c) - if longMode(n) or (lsub(n.sons[0]) + g.lineLen > MaxLineLen): + if longMode(g, n) or (lsub(g, n.sons[0]) + g.lineLen > MaxLineLen): incl(c.flags, rfLongMode) gcoms(g) # a good place for comments gstmts(g, n.sons[0], c) @@ -771,7 +769,7 @@ proc gident(g: var TSrcGen, n: PNode) = (n.typ != nil and tfImplicitTypeParam in n.typ.flags): return var t: TTokType - var s = atom(n) + var s = atom(g, n) if (s[0] in lexer.SymChars): if (n.kind == nkIdent): if (n.ident.id < ord(tokKeywordLow) - ord(tkSymbol)) or @@ -818,26 +816,26 @@ proc gsub(g: var TSrcGen, n: PNode, c: TContext) = case n.kind # atoms: of nkTripleStrLit: putRawStr(g, tkTripleStrLit, n.strVal) of nkEmpty: discard - of nkType: put(g, tkInvalid, atom(n)) + of nkType: put(g, tkInvalid, atom(g, n)) of nkSym, nkIdent: gident(g, n) - of nkIntLit: put(g, tkIntLit, atom(n)) - of nkInt8Lit: put(g, tkInt8Lit, atom(n)) - of nkInt16Lit: put(g, tkInt16Lit, atom(n)) - of nkInt32Lit: put(g, tkInt32Lit, atom(n)) - of nkInt64Lit: put(g, tkInt64Lit, atom(n)) - of nkUIntLit: put(g, tkUIntLit, atom(n)) - of nkUInt8Lit: put(g, tkUInt8Lit, atom(n)) - of nkUInt16Lit: put(g, tkUInt16Lit, atom(n)) - of nkUInt32Lit: put(g, tkUInt32Lit, atom(n)) - of nkUInt64Lit: put(g, tkUInt64Lit, atom(n)) - of nkFloatLit: put(g, tkFloatLit, atom(n)) - of nkFloat32Lit: put(g, tkFloat32Lit, atom(n)) - of nkFloat64Lit: put(g, tkFloat64Lit, atom(n)) - of nkFloat128Lit: put(g, tkFloat128Lit, atom(n)) - of nkStrLit: put(g, tkStrLit, atom(n)) - of nkRStrLit: put(g, tkRStrLit, atom(n)) - of nkCharLit: put(g, tkCharLit, atom(n)) - of nkNilLit: put(g, tkNil, atom(n)) # complex expressions + of nkIntLit: put(g, tkIntLit, atom(g, n)) + of nkInt8Lit: put(g, tkInt8Lit, atom(g, n)) + of nkInt16Lit: put(g, tkInt16Lit, atom(g, n)) + of nkInt32Lit: put(g, tkInt32Lit, atom(g, n)) + of nkInt64Lit: put(g, tkInt64Lit, atom(g, n)) + of nkUIntLit: put(g, tkUIntLit, atom(g, n)) + of nkUInt8Lit: put(g, tkUInt8Lit, atom(g, n)) + of nkUInt16Lit: put(g, tkUInt16Lit, atom(g, n)) + of nkUInt32Lit: put(g, tkUInt32Lit, atom(g, n)) + of nkUInt64Lit: put(g, tkUInt64Lit, atom(g, n)) + of nkFloatLit: put(g, tkFloatLit, atom(g, n)) + of nkFloat32Lit: put(g, tkFloat32Lit, atom(g, n)) + of nkFloat64Lit: put(g, tkFloat64Lit, atom(g, n)) + of nkFloat128Lit: put(g, tkFloat128Lit, atom(g, n)) + of nkStrLit: put(g, tkStrLit, atom(g, n)) + of nkRStrLit: put(g, tkRStrLit, atom(g, n)) + of nkCharLit: put(g, tkCharLit, atom(g, n)) + of nkNilLit: put(g, tkNil, atom(g, n)) # complex expressions of nkCall, nkConv, nkDotCall, nkPattern, nkObjConstr: if n.len > 0 and isBracket(n[0]): gsub(g, n, 1) @@ -1003,7 +1001,7 @@ proc gsub(g: var TSrcGen, n: PNode, c: TContext) = gsub(g, n, 1) put(g, tkSpaces, Space) gsub(g, n, 0) # binary operator - if not fits(g, lsub(n.sons[2]) + lsub(n.sons[0]) + 1): + if not fits(g, lsub(g, n.sons[2]) + lsub(g, n.sons[0]) + 1): optNL(g, g.indent + longIndentWid) else: put(g, tkSpaces, Space) @@ -1011,7 +1009,8 @@ proc gsub(g: var TSrcGen, n: PNode, c: TContext) = of nkPrefix: gsub(g, n, 0) if n.len > 1: - put(g, tkSpaces, Space) + if n[1].kind == nkPrefix: + put(g, tkSpaces, Space) if n.sons[1].kind == nkInfix: put(g, tkParLe, "(") gsub(g, n.sons[1]) @@ -1316,8 +1315,11 @@ proc gsub(g: var TSrcGen, n: PNode, c: TContext) = gstmts(g, n.sons[0], c) of nkExceptBranch: optNL(g) - putWithSpace(g, tkExcept, "except") - gcomma(g, n, 0, - 2) + if n.len != 1: + putWithSpace(g, tkExcept, "except") + else: + put(g, tkExcept, "except") + gcomma(g, n, 0, -2) putWithSpace(g, tkColon, ":") gcoms(g) gstmts(g, lastSon(n), c) @@ -1363,7 +1365,7 @@ proc gsub(g: var TSrcGen, n: PNode, c: TContext) = #nkNone, nkExplicitTypeListCall: internalError(n.info, "rnimsyn.gsub(" & $n.kind & ')') -proc renderTree(n: PNode, renderFlags: TRenderFlags = {}): string = +proc renderTree*(n: PNode, renderFlags: TRenderFlags = {}): string = var g: TSrcGen initSrcGen(g, renderFlags) # do not indent the initial statement list so that @@ -1375,12 +1377,20 @@ proc renderTree(n: PNode, renderFlags: TRenderFlags = {}): string = gsub(g, n) result = g.buf -proc renderModule(n: PNode, filename: string, - renderFlags: TRenderFlags = {}) = +proc `$`*(n: PNode): string = n.renderTree + +proc renderModule*(n: PNode, filename: string, + renderFlags: TRenderFlags = {}) = var f: File g: TSrcGen initSrcGen(g, renderFlags) + when defined(nimpretty): + try: + g.origContent = readFile(filename) + except IOError: + rawMessage(errCannotOpenFile, filename) + for i in countup(0, sonsLen(n) - 1): gsub(g, n.sons[i]) optNL(g) @@ -1397,11 +1407,11 @@ proc renderModule(n: PNode, filename: string, else: rawMessage(errCannotOpenFile, filename) -proc initTokRender(r: var TSrcGen, n: PNode, renderFlags: TRenderFlags = {}) = +proc initTokRender*(r: var TSrcGen, n: PNode, renderFlags: TRenderFlags = {}) = initSrcGen(r, renderFlags) gsub(r, n) -proc getNextTok(r: var TSrcGen, kind: var TTokType, literal: var string) = +proc getNextTok*(r: var TSrcGen, kind: var TTokType, literal: var string) = if r.idx < len(r.tokens): kind = r.tokens[r.idx].kind var length = r.tokens[r.idx].length.int -- cgit 1.4.1-2-gfad0 From 125ccd303ee980deda1061e346e29414658d9ae6 Mon Sep 17 00:00:00 2001 From: Andreas Rumpf Date: Fri, 6 Oct 2017 08:20:56 +0200 Subject: nimpretty bugfix --- compiler/renderer.nim | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'compiler/renderer.nim') diff --git a/compiler/renderer.nim b/compiler/renderer.nim index c0bea793d..fba4dc9ea 100644 --- a/compiler/renderer.nim +++ b/compiler/renderer.nim @@ -306,7 +306,9 @@ proc ulitAux(g: TSrcGen; n: PNode, x: BiggestInt, size: int): string = proc atom(g: TSrcGen; n: PNode): string = when defined(nimpretty): - if true: + if n.info.offsetA <= n.info.offsetB: + # for some constructed tokens this can not be the case and we're better + # off to not mess with the offset then. return substr(g.origContent, n.info.offsetA, n.info.offsetB) var f: float32 case n.kind -- cgit 1.4.1-2-gfad0 From ac3e3cf2b000ad30fad81c957cb9d73d887e01d2 Mon Sep 17 00:00:00 2001 From: Andreas Rumpf Date: Tue, 10 Oct 2017 11:15:18 +0200 Subject: fixes a renderer regression that affects c2nim --- compiler/renderer.nim | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'compiler/renderer.nim') diff --git a/compiler/renderer.nim b/compiler/renderer.nim index fba4dc9ea..4fbac45ab 100644 --- a/compiler/renderer.nim +++ b/compiler/renderer.nim @@ -1011,7 +1011,10 @@ proc gsub(g: var TSrcGen, n: PNode, c: TContext) = of nkPrefix: gsub(g, n, 0) if n.len > 1: - if n[1].kind == nkPrefix: + let opr = if n[0].kind == nkIdent: n[0].ident + elif n[0].kind == nkSym: n[0].sym.name + else: nil + if n[1].kind == nkPrefix or (opr != nil and renderer.isKeyword(opr)): put(g, tkSpaces, Space) if n.sons[1].kind == nkInfix: put(g, tkParLe, "(") -- cgit 1.4.1-2-gfad0 From 604a15c0aaf6d3df28b3236b9aeb32acea7d653f Mon Sep 17 00:00:00 2001 From: Araq Date: Mon, 16 Oct 2017 00:59:51 +0200 Subject: some progress on the nimpretty tool; still not ready --- compiler/lexer.nim | 22 +++++++++++++++++ compiler/msgs.nim | 1 + compiler/renderer.nim | 68 +++++++++++++++++++++++++++++++++++++++------------ tools/nimpretty.nim | 5 ++-- 4 files changed, 79 insertions(+), 17 deletions(-) (limited to 'compiler/renderer.nim') diff --git a/compiler/lexer.nim b/compiler/lexer.nim index 9b37b499b..895848e77 100644 --- a/compiler/lexer.nim +++ b/compiler/lexer.nim @@ -129,6 +129,7 @@ type when defined(nimpretty): offsetA*, offsetB*: int # used for pretty printing so that literals # like 0b01 or r"\L" are unaffected + commentOffsetA*, commentOffsetB*: int TErrorHandler* = proc (info: TLineInfo; msg: TMsgKind; arg: string) TLexer* = object of TBaseLexer @@ -144,6 +145,10 @@ type when defined(nimsuggest): previousToken: TLineInfo +when defined(nimpretty): + var + gIndentationWidth*: int + var gLinesCompiled*: int # all lines that have been compiled proc getLineInfo*(L: TLexer, tok: TToken): TLineInfo {.inline.} = @@ -151,6 +156,8 @@ proc getLineInfo*(L: TLexer, tok: TToken): TLineInfo {.inline.} = when defined(nimpretty): result.offsetA = tok.offsetA result.offsetB = tok.offsetB + result.commentOffsetA = tok.commentOffsetA + result.commentOffsetB = tok.commentOffsetB proc isKeyword*(kind: TTokType): bool = result = (kind >= tokKeywordLow) and (kind <= tokKeywordHigh) @@ -198,6 +205,9 @@ proc initToken*(L: var TToken) = L.fNumber = 0.0 L.base = base10 L.ident = nil + when defined(nimpretty): + L.commentOffsetA = 0 + L.commentOffsetB = 0 proc fillToken(L: var TToken) = L.tokType = tkInvalid @@ -208,6 +218,9 @@ proc fillToken(L: var TToken) = L.fNumber = 0.0 L.base = base10 L.ident = nil + when defined(nimpretty): + L.commentOffsetA = 0 + L.commentOffsetB = 0 proc openLexer*(lex: var TLexer, fileIdx: int32, inputstream: PLLStream; cache: IdentCache) = @@ -996,18 +1009,27 @@ proc skip(L: var TLexer, tok: var TToken) = of '#': # do not skip documentation comment: if buf[pos+1] == '#': break + when defined(nimpretty): + tok.commentOffsetA = L.offsetBase + pos if buf[pos+1] == '[': skipMultiLineComment(L, tok, pos+2, false) pos = L.bufpos buf = L.buf + when defined(nimpretty): + tok.commentOffsetB = L.offsetBase + pos else: tokenBegin(tok, pos) while buf[pos] notin {CR, LF, nimlexbase.EndOfFile}: inc(pos) tokenEndIgnore(tok, pos+1) + when defined(nimpretty): + tok.commentOffsetB = L.offsetBase + pos + 1 else: break # EndOfFile also leaves the loop tokenEndPrevious(tok, pos-1) L.bufpos = pos + when defined(nimpretty): + if gIndentationWidth <= 0: + gIndentationWidth = tok.indent proc rawGetTok*(L: var TLexer, tok: var TToken) = template atTokenEnd() {.dirty.} = diff --git a/compiler/msgs.nim b/compiler/msgs.nim index c988141e5..8d43103db 100644 --- a/compiler/msgs.nim +++ b/compiler/msgs.nim @@ -500,6 +500,7 @@ type fileIndex*: int32 when defined(nimpretty): offsetA*, offsetB*: int + commentOffsetA*, commentOffsetB*: int TErrorOutput* = enum eStdOut diff --git a/compiler/renderer.nim b/compiler/renderer.nim index 4fbac45ab..f3b4527df 100644 --- a/compiler/renderer.nim +++ b/compiler/renderer.nim @@ -37,6 +37,7 @@ type inGenericParams: bool checkAnon: bool # we're in a context that can contain sfAnon inPragma: int + pendingNewlineCount: int when defined(nimpretty): origContent: string @@ -62,9 +63,31 @@ proc renderDefinitionName*(s: PSym, noQuotes = false): string = else: result = '`' & x & '`' +when not defined(nimpretty): + const + IndentWidth = 2 + longIndentWid = IndentWidth * 2 +else: + template IndentWidth: untyped = lexer.gIndentationWidth + template longIndentWid: untyped = IndentWidth() * 2 + +proc minmaxLine(n: PNode): (int, int) = + case n.kind + of nkTripleStrLit: + result = (n.info.line.int, n.info.line.int + countLines(n.strVal)) + of nkCommentStmt: + result = (n.info.line.int, n.info.line.int + countLines(n.comment)) + else: + result = (n.info.line.int, n.info.line.int) + for i in 0 ..< safeLen(n): + let (currMin, currMax) = minmaxLine(n[i]) + if currMin < result[0]: result[0] = currMin + if currMax > result[1]: result[1] = currMax + +proc lineDiff(a, b: PNode): int = + result = minmaxLine(b)[0] - minmaxLine(a)[1] + const - IndentWidth = 2 - longIndentWid = 4 MaxLineLen = 80 LineCommentColumn = 30 @@ -90,7 +113,8 @@ proc addTok(g: var TSrcGen, kind: TTokType, s: string) = proc addPendingNL(g: var TSrcGen) = if g.pendingNL >= 0: - addTok(g, tkSpaces, "\n" & spaces(g.pendingNL)) + let newlines = repeat("\n", clamp(g.pendingNewlineCount, 1, 3)) + addTok(g, tkSpaces, newlines & spaces(g.pendingNL)) g.lineLen = g.pendingNL g.pendingNL = - 1 g.pendingWhitespace = -1 @@ -114,11 +138,17 @@ proc putNL(g: var TSrcGen) = proc optNL(g: var TSrcGen, indent: int) = g.pendingNL = indent - g.lineLen = indent # BUGFIX + g.lineLen = indent + g.pendingNewlineCount = 0 proc optNL(g: var TSrcGen) = optNL(g, g.indent) +proc optNL(g: var TSrcGen; a, b: PNode) = + g.pendingNL = g.indent + g.lineLen = g.indent + g.pendingNewlineCount = lineDiff(a, b) + proc indentNL(g: var TSrcGen) = inc(g.indent, IndentWidth) g.pendingNL = g.indent @@ -306,10 +336,14 @@ proc ulitAux(g: TSrcGen; n: PNode, x: BiggestInt, size: int): string = proc atom(g: TSrcGen; n: PNode): string = when defined(nimpretty): + let comment = if n.info.commentOffsetA < n.info.commentOffsetB: + " " & substr(g.origContent, n.info.commentOffsetA, n.info.commentOffsetB) + else: + "" if n.info.offsetA <= n.info.offsetB: # for some constructed tokens this can not be the case and we're better # off to not mess with the offset then. - return substr(g.origContent, n.info.offsetA, n.info.offsetB) + return substr(g.origContent, n.info.offsetA, n.info.offsetB) & comment var f: float32 case n.kind of nkEmpty: result = "" @@ -577,12 +611,16 @@ proc gstmts(g: var TSrcGen, n: PNode, c: TContext, doIndent=true) = if n.kind == nkEmpty: return if n.kind in {nkStmtList, nkStmtListExpr, nkStmtListType}: if doIndent: indentNL(g) - for i in countup(0, sonsLen(n) - 1): - optNL(g) - if n.sons[i].kind in {nkStmtList, nkStmtListExpr, nkStmtListType}: - gstmts(g, n.sons[i], c, doIndent=false) + let L = n.len + for i in 0 .. L-1: + if i > 0: + optNL(g, n[i-1], n[i]) else: - gsub(g, n.sons[i]) + optNL(g) + if n[i].kind in {nkStmtList, nkStmtListExpr, nkStmtListType}: + gstmts(g, n[i], c, doIndent=false) + else: + gsub(g, n[i]) gcoms(g) if doIndent: dedent(g) else: @@ -1384,7 +1422,7 @@ proc renderTree*(n: PNode, renderFlags: TRenderFlags = {}): string = proc `$`*(n: PNode): string = n.renderTree -proc renderModule*(n: PNode, filename: string, +proc renderModule*(n: PNode, infile, outfile: string, renderFlags: TRenderFlags = {}) = var f: File @@ -1392,9 +1430,9 @@ proc renderModule*(n: PNode, filename: string, initSrcGen(g, renderFlags) when defined(nimpretty): try: - g.origContent = readFile(filename) + g.origContent = readFile(infile) except IOError: - rawMessage(errCannotOpenFile, filename) + rawMessage(errCannotOpenFile, infile) for i in countup(0, sonsLen(n) - 1): gsub(g, n.sons[i]) @@ -1406,11 +1444,11 @@ proc renderModule*(n: PNode, filename: string, gcoms(g) if optStdout in gGlobalOptions: write(stdout, g.buf) - elif open(f, filename, fmWrite): + elif open(f, outfile, fmWrite): write(f, g.buf) close(f) else: - rawMessage(errCannotOpenFile, filename) + rawMessage(errCannotOpenFile, outfile) proc initTokRender*(r: var TSrcGen, n: PNode, renderFlags: TRenderFlags = {}) = initSrcGen(r, renderFlags) diff --git a/tools/nimpretty.nim b/tools/nimpretty.nim index 2c967b1e8..36d1382cf 100644 --- a/tools/nimpretty.nim +++ b/tools/nimpretty.nim @@ -42,7 +42,8 @@ proc writeVersion() = proc prettyPrint(infile: string) = let fileIdx = fileInfoIdx(infile) let tree = parseFile(fileIdx, newIdentCache()) - renderModule(tree, infile, {}) + let outfile = changeFileExt(infile, ".pretty.nim") + renderModule(tree, infile, outfile, {}) proc main = var infile: string @@ -50,7 +51,7 @@ proc main = for kind, key, val in getopt(): case kind of cmdArgument: - infile = key + infile = key.addFileExt(".nim") of cmdLongoption, cmdShortOption: case normalize(key) of "help", "h": writeHelp() -- cgit 1.4.1-2-gfad0 From 186e7d49d3629c7e701e6aa2e2507b6410dd2b1f Mon Sep 17 00:00:00 2001 From: Andreas Rumpf Date: Thu, 19 Oct 2017 13:29:05 +0200 Subject: fixes c2nim regression; do not produce more newlines --- compiler/renderer.nim | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) (limited to 'compiler/renderer.nim') diff --git a/compiler/renderer.nim b/compiler/renderer.nim index f3b4527df..51aecae2b 100644 --- a/compiler/renderer.nim +++ b/compiler/renderer.nim @@ -37,8 +37,8 @@ type inGenericParams: bool checkAnon: bool # we're in a context that can contain sfAnon inPragma: int - pendingNewlineCount: int when defined(nimpretty): + pendingNewlineCount: int origContent: string @@ -71,21 +71,21 @@ else: template IndentWidth: untyped = lexer.gIndentationWidth template longIndentWid: untyped = IndentWidth() * 2 -proc minmaxLine(n: PNode): (int, int) = - case n.kind - of nkTripleStrLit: - result = (n.info.line.int, n.info.line.int + countLines(n.strVal)) - of nkCommentStmt: - result = (n.info.line.int, n.info.line.int + countLines(n.comment)) - else: - result = (n.info.line.int, n.info.line.int) - for i in 0 ..< safeLen(n): - let (currMin, currMax) = minmaxLine(n[i]) - if currMin < result[0]: result[0] = currMin - if currMax > result[1]: result[1] = currMax + proc minmaxLine(n: PNode): (int, int) = + case n.kind + of nkTripleStrLit: + result = (n.info.line.int, n.info.line.int + countLines(n.strVal)) + of nkCommentStmt: + result = (n.info.line.int, n.info.line.int + countLines(n.comment)) + else: + result = (n.info.line.int, n.info.line.int) + for i in 0 ..< safeLen(n): + let (currMin, currMax) = minmaxLine(n[i]) + if currMin < result[0]: result[0] = currMin + if currMax > result[1]: result[1] = currMax -proc lineDiff(a, b: PNode): int = - result = minmaxLine(b)[0] - minmaxLine(a)[1] + proc lineDiff(a, b: PNode): int = + result = minmaxLine(b)[0] - minmaxLine(a)[1] const MaxLineLen = 80 @@ -113,7 +113,10 @@ proc addTok(g: var TSrcGen, kind: TTokType, s: string) = proc addPendingNL(g: var TSrcGen) = if g.pendingNL >= 0: - let newlines = repeat("\n", clamp(g.pendingNewlineCount, 1, 3)) + when defined(nimpretty): + let newlines = repeat("\n", clamp(g.pendingNewlineCount, 1, 3)) + else: + const newlines = "\n" addTok(g, tkSpaces, newlines & spaces(g.pendingNL)) g.lineLen = g.pendingNL g.pendingNL = - 1 @@ -139,7 +142,7 @@ proc putNL(g: var TSrcGen) = proc optNL(g: var TSrcGen, indent: int) = g.pendingNL = indent g.lineLen = indent - g.pendingNewlineCount = 0 + when defined(nimpretty): g.pendingNewlineCount = 0 proc optNL(g: var TSrcGen) = optNL(g, g.indent) @@ -147,7 +150,7 @@ proc optNL(g: var TSrcGen) = proc optNL(g: var TSrcGen; a, b: PNode) = g.pendingNL = g.indent g.lineLen = g.indent - g.pendingNewlineCount = lineDiff(a, b) + when defined(nimpretty): g.pendingNewlineCount = lineDiff(a, b) proc indentNL(g: var TSrcGen) = inc(g.indent, IndentWidth) -- cgit 1.4.1-2-gfad0 From 70ea45cdbaa9d26a7196ab2718f60c9ca77e2d12 Mon Sep 17 00:00:00 2001 From: Andreas Rumpf Date: Sun, 29 Oct 2017 08:37:13 +0100 Subject: deprecated unary '<' --- changelog.md | 2 ++ compiler/ast.nim | 4 +-- compiler/canonicalizer.nim | 6 ++-- compiler/ccgcalls.nim | 6 ++-- compiler/ccgexprs.nim | 4 +-- compiler/ccgstmts.nim | 14 ++++---- compiler/ccgtypes.nim | 2 +- compiler/ccgutils.nim | 4 +-- compiler/cgen.nim | 2 +- compiler/dfa.nim | 4 +-- compiler/docgen.nim | 6 ++-- compiler/evalffi.nim | 4 +-- compiler/evaltempl.nim | 2 +- compiler/forloops.nim | 4 +-- compiler/guards.nim | 2 +- compiler/hlo.nim | 4 +-- compiler/jsgen.nim | 6 ++-- compiler/jstypes.nim | 2 +- compiler/lexer.nim | 2 +- compiler/lookups.nim | 2 +- compiler/lowerings.nim | 4 +-- compiler/parampatterns.nim | 6 ++-- compiler/patterns.nim | 18 +++++----- compiler/pragmas.nim | 2 +- compiler/renderer.nim | 2 +- compiler/reorder.nim | 2 +- compiler/rodread.nim | 2 +- compiler/sem.nim | 2 +- compiler/semasgn.nim | 6 ++-- compiler/semcall.nim | 4 +-- compiler/semdestruct.nim | 2 +- compiler/semexprs.nim | 20 +++++------ compiler/semfields.nim | 2 +- compiler/seminst.nim | 8 ++--- compiler/semmacrosanity.nim | 4 +-- compiler/semobjconstr.nim | 10 +++--- compiler/semparallel.nim | 24 ++++++------- compiler/sempass2.nim | 48 +++++++++++++------------- compiler/semstmts.nim | 14 ++++---- compiler/semtempl.nim | 8 ++--- compiler/semtypes.nim | 8 ++--- compiler/semtypinst.nim | 16 ++++----- compiler/sighashes.nim | 6 ++-- compiler/sigmatch.nim | 16 ++++----- compiler/transf.nim | 10 +++--- compiler/trees.nim | 2 +- compiler/types.nim | 4 +-- compiler/typesrenderer.nim | 20 +++++------ compiler/vm.nim | 23 ++++++------- compiler/vmdeps.nim | 6 ++-- compiler/vmgen.nim | 22 ++++++------ compiler/vmmarshal.nim | 4 +-- compiler/writetracking.nim | 2 +- lib/pure/collections/sequtils.nim | 34 +++++++++---------- lib/system.nim | 71 ++++++++++++++++++++------------------- todo.txt | 3 -- 56 files changed, 259 insertions(+), 258 deletions(-) (limited to 'compiler/renderer.nim') diff --git a/changelog.md b/changelog.md index ebd454ab7..6dea3ee63 100644 --- a/changelog.md +++ b/changelog.md @@ -20,3 +20,5 @@ recursive types can be created across module boundaries. See [package level objects](https://nim-lang.org/docs/manual.html#package-level-objects) for more information. +- The **unary** ``<`` is now deprecated, for ``.. <`` use ``..<`` for other usages + use the ``pred`` proc. diff --git a/compiler/ast.nim b/compiler/ast.nim index aa6af7e3f..43aa3e484 100644 --- a/compiler/ast.nim +++ b/compiler/ast.nim @@ -1604,10 +1604,10 @@ proc hasPattern*(s: PSym): bool {.inline.} = result = isRoutine(s) and s.ast.sons[patternPos].kind != nkEmpty iterator items*(n: PNode): PNode = - for i in 0.. = nkNone and n.kind <= nkNilLit diff --git a/compiler/canonicalizer.nim b/compiler/canonicalizer.nim index 6972f5acf..d1669a06c 100644 --- a/compiler/canonicalizer.nim +++ b/compiler/canonicalizer.nim @@ -102,7 +102,7 @@ proc hashTree(c: var MD5Context, n: PNode) = of nkStrLit..nkTripleStrLit: c &= n.strVal else: - for i in 0.. = 1: result = shallowCopy(n) - for i in 0 .. < n.len: + for i in 0 ..< n.len: result.sons[i] = canon(n.sons[i]) elif n.kind == nkSym and n.sym.kind == skLet and n.sym.ast.getMagic in (someEq + someAdd + someMul + someMin + diff --git a/compiler/hlo.nim b/compiler/hlo.nim index 9491eef83..2bffaa173 100644 --- a/compiler/hlo.nim +++ b/compiler/hlo.nim @@ -36,7 +36,7 @@ proc applyPatterns(c: PContext, n: PNode): PNode = # we apply the last pattern first, so that pattern overriding is possible; # however the resulting AST would better not trigger the old rule then # anymore ;-) - for i in countdown( 0: add(r.res, ", ") genOtherArg(p, n, k, typ, generated, r) inc i @@ -1528,7 +1528,7 @@ proc createVar(p: PProc, typ: PType, indirect: bool): Rope = of tyTuple: if p.target == targetJS: result = rope("{") - for i in 0.. 0: add(result, ", ") addf(result, "Field$1: $2", [i.rope, createVar(p, t.sons[i], false)]) @@ -1536,7 +1536,7 @@ proc createVar(p: PProc, typ: PType, indirect: bool): Rope = if indirect: result = "[$1]" % [result] else: result = rope("array(") - for i in 0.. 0: add(result, ", ") add(result, createVar(p, t.sons[i], false)) add(result, ")") diff --git a/compiler/jstypes.nim b/compiler/jstypes.nim index 0d5b29ace..d9df04e4b 100644 --- a/compiler/jstypes.nim +++ b/compiler/jstypes.nim @@ -84,7 +84,7 @@ proc genObjectInfo(p: PProc, typ: PType, name: Rope) = proc genTupleFields(p: PProc, typ: PType): Rope = var s: Rope = nil - for i in 0 .. 0: add(s, ", " & tnl) s.addf("{kind: 1, offset: \"Field$1\", len: 0, " & "typ: $2, name: \"Field$1\", sons: null}", diff --git a/compiler/lexer.nim b/compiler/lexer.nim index 895848e77..2ae2176de 100644 --- a/compiler/lexer.nim +++ b/compiler/lexer.nim @@ -693,7 +693,7 @@ proc getEscapedChar(L: var TLexer, tok: var TToken) = proc newString(s: cstring, len: int): string = ## XXX, how come there is no support for this? result = newString(len) - for i in 0 .. = 2: - for i in 1.. MaxStackSize-1: internalError(p.info, "parameter pattern too complex") @@ -152,7 +152,7 @@ proc checkForSideEffects*(n: PNode): TSideEffectAnalysis = # indirect call: assume side effect: return seSideEffect # we need to check n[0] too: (FwithSideEffectButReturnsProcWithout)(args) - for i in 0 .. = 2: - for i in 1 .. < params.len: + for i in 1 ..< params.len: let param = params.sons[i].sym if whichAlias(param) != aqNone: return true @@ -237,7 +237,7 @@ proc addToArgList(result, n: PNode) = if n.typ != nil and n.typ.kind != tyStmt: if n.kind != nkArgList: result.add(n) else: - for i in 0 .. 0: gsub(g, n.sons[0]) - for i in 1 .. PRodReader but I'll leave # this for later versions if benchmarking shows the linear search causes # problems: - for i in 0 .. 1: - for i in 1 .. 1: resetIdTable(cl.symMap) diff --git a/compiler/semmacrosanity.nim b/compiler/semmacrosanity.nim index a6024a42f..fe9bb6c8d 100644 --- a/compiler/semmacrosanity.nim +++ b/compiler/semmacrosanity.nim @@ -42,7 +42,7 @@ proc annotateType*(n: PNode, t: PType) = of nkObjConstr: let x = t.skipTypes(abstractPtrs) n.typ = t - for i in 1 .. = x.len: globalError n.info, "invalid field at index " & $i else: annotateType(n.sons[i], x.sons[i]) elif x.kind == tyProc and x.callConv == ccClosure: diff --git a/compiler/semobjconstr.nim b/compiler/semobjconstr.nim index b331d05a1..c73b042fe 100644 --- a/compiler/semobjconstr.nim +++ b/compiler/semobjconstr.nim @@ -42,7 +42,7 @@ proc mergeInitStatus(existing: var InitStatus, newStatus: InitStatus) = proc locateFieldInInitExpr(field: PSym, initExpr: PNode): PNode = # Returns the assignment nkExprColonExpr node or nil let fieldId = field.name.id - for i in 1 .. = 0 and c.locals[s].stride != nil: localError(n.info, "invalid usage of counter after increment") else: - for i in 0 .. = 0 and c.locals[s].stride != nil: result = c.locals[s].stride.intVal else: - for i in 0 .. 0: result = shallowCopy(n) - for i in 0 .. 1: addFact(c.guards, canon(branch.sons[0])) - for i in 0 .. 0: result = shallowCopy(n) - for i in 0 .. < n.len: + for i in 0 ..< n.len: result.sons[i] = transformSlices(n.sons[i]) else: result = n @@ -415,7 +415,7 @@ proc transformSlices(n: PNode): PNode = proc transformSpawn(owner: PSym; n, barrier: PNode): PNode proc transformSpawnSons(owner: PSym; n, barrier: PNode): PNode = result = shallowCopy(n) - for i in 0 .. < n.len: + for i in 0 ..< n.len: result.sons[i] = transformSpawn(owner, n.sons[i], barrier) proc transformSpawn(owner: PSym; n, barrier: PNode): PNode = diff --git a/compiler/sempass2.nim b/compiler/sempass2.nim index 17d9c9840..5add78268 100644 --- a/compiler/sempass2.nim +++ b/compiler/sempass2.nim @@ -248,7 +248,7 @@ type TIntersection = seq[tuple[id, count: int]] # a simple count table proc addToIntersection(inter: var TIntersection, s: int) = - for j in 0.. 1: addFact(tracked.guards, branch.sons[0]) setLen(tracked.init, oldState) - for i in 0 .. = 3): diff --git a/compiler/trees.nim b/compiler/trees.nim index c77dab349..7efefdc2e 100644 --- a/compiler/trees.nim +++ b/compiler/trees.nim @@ -98,7 +98,7 @@ proc isDeepConstExpr*(n: PNode): bool = of nkExprEqExpr, nkExprColonExpr, nkHiddenStdConv, nkHiddenSubConv: result = isDeepConstExpr(n.sons[1]) of nkCurly, nkBracket, nkPar, nkObjConstr, nkClosure, nkRange: - for i in ord(n.kind == nkObjConstr) .. 0 result = "proc(" - for i in 1 .. = 2 result = renderType(n[0]) & '[' - for i in 1 .. n.safeLen and sym.typ.len > 1: globalError(n.info, "in call '$#' got $#, but expected $# argument(s)" % [ - n.renderTree, - $ 0 and i < sonsLen(fntyp): # let paramType = fntyp.n.sons[i] # if paramType.typ.isCompileTimeOnly: continue @@ -995,7 +995,7 @@ proc genMagic(c: PCtx; n: PNode; dest: var TDest; m: TMagic) = let n = n[1].skipConv let x = c.getTempRange(n.len, slotTempUnknown) internalAssert n.kind == nkBracket - for i in 0.. 0: s.add(", ") s.add("\"Field" & $i) s.add("\": ") @@ -90,7 +90,7 @@ proc storeAny(s: var string; t: PType; a: PNode; stored: var IntSet) = s.add("}") of tySet: s.add("[") - for i in 0.. 0: s.add(", ") if a[i].kind == nkRange: var x = copyNode(a[i][0]) diff --git a/compiler/writetracking.nim b/compiler/writetracking.nim index fe71e5b31..577db613d 100644 --- a/compiler/writetracking.nim +++ b/compiler/writetracking.nim @@ -123,7 +123,7 @@ proc returnsNewExpr*(n: PNode): NewLocation = of nkCurly, nkBracket, nkPar, nkObjConstr, nkClosure, nkIfExpr, nkIfStmt, nkWhenStmt, nkCaseStmt, nkTryStmt: result = newLit - for i in ord(n.kind == nkObjConstr) .. 0: inc(stride) - for i in 0 .. 0: extra -= 1 inc(last) result[i] = newSeq[T]() - for g in first .. ["142", "242", "342", "442"] ## **Deprecated since version 0.12.0:** Use the ``apply`` proc instead. - for i in 0 .. ["142", "242", "342", "442"] ## - for i in 0 .. ["142", "242", "342", "442"] ## - for i in 0 .. 10) ## assert floats == @[13.0, 12.5, 10.1] var pos = 0 - for i in 0 .. 0 1 2 3 4 5 6 7 8 9 ## ## Semantically this is the same as ``pred``. + ## + ## **Deprecated since version 0.18.0**. For the common excluding range + ## write ``0 ..< 10`` instead of ``0 .. < 10`` (look at the spacing). + ## For `` requires 'mixin' annotation for procs! - make 'nil' work for 'add': - resizeString - incrSeq -- cgit 1.4.1-2-gfad0