source code renderer doesn't produce trailing whitespace anymore

This commit is contained in:
Araq 2015-07-12 13:32:27 +02:00
commit 7da62f9833

View file

@ -31,6 +31,7 @@ type
buf*: string buf*: string
pendingNL*: int # negative if not active; else contains the pendingNL*: int # negative if not active; else contains the
# indentation value # indentation value
pendingWhitespace: int
comStack*: seq[PNode] # comment stack comStack*: seq[PNode] # comment stack
flags*: TRenderFlags flags*: TRenderFlags
checkAnon: bool # we're in a context that can contain sfAnon checkAnon: bool # we're in a context that can contain sfAnon
@ -83,6 +84,7 @@ proc initSrcGen(g: var TSrcGen, renderFlags: TRenderFlags) =
g.buf = "" g.buf = ""
g.flags = renderFlags g.flags = renderFlags
g.pendingNL = -1 g.pendingNL = -1
g.pendingWhitespace = -1
g.checkAnon = false g.checkAnon = false
proc addTok(g: var TSrcGen, kind: TTokType, s: string) = proc addTok(g: var TSrcGen, kind: TTokType, s: string) =
@ -97,12 +99,21 @@ proc addPendingNL(g: var TSrcGen) =
addTok(g, tkSpaces, "\n" & spaces(g.pendingNL)) addTok(g, tkSpaces, "\n" & spaces(g.pendingNL))
g.lineLen = g.pendingNL g.lineLen = g.pendingNL
g.pendingNL = - 1 g.pendingNL = - 1
g.pendingWhitespace = -1
elif g.pendingWhitespace >= 0:
addTok(g, tkSpaces, spaces(g.pendingWhitespace))
g.pendingWhitespace = -1
proc putNL(g: var TSrcGen, indent: int) = proc putNL(g: var TSrcGen, indent: int) =
if g.pendingNL >= 0: addPendingNL(g) if g.pendingNL >= 0: addPendingNL(g)
else: addTok(g, tkSpaces, "\n") else: addTok(g, tkSpaces, "\n")
g.pendingNL = indent g.pendingNL = indent
g.lineLen = indent g.lineLen = indent
g.pendingWhitespace = -1
proc previousNL(g: TSrcGen): bool =
result = g.pendingNL >= 0 or (g.tokens.len > 0 and
g.tokens[^1].kind == tkSpaces)
proc putNL(g: var TSrcGen) = proc putNL(g: var TSrcGen) =
putNL(g, g.indent) putNL(g, g.indent)
@ -127,10 +138,13 @@ proc dedent(g: var TSrcGen) =
dec(g.lineLen, IndentWidth) dec(g.lineLen, IndentWidth)
proc put(g: var TSrcGen, kind: TTokType, s: string) = proc put(g: var TSrcGen, kind: TTokType, s: string) =
if kind != tkSpaces:
addPendingNL(g) addPendingNL(g)
if len(s) > 0: if len(s) > 0:
addTok(g, kind, s) addTok(g, kind, s)
inc(g.lineLen, len(s)) inc(g.lineLen, len(s))
else:
g.pendingWhitespace = s.len
proc putLong(g: var TSrcGen, kind: TTokType, s: string, lineLen: int) = proc putLong(g: var TSrcGen, kind: TTokType, s: string, lineLen: int) =
# use this for tokens over multiple lines. # use this for tokens over multiple lines.
@ -1196,6 +1210,7 @@ proc gsub(g: var TSrcGen, n: PNode, c: TContext) =
if renderNoPragmas notin g.flags: if renderNoPragmas notin g.flags:
if g.inPragma <= 0: if g.inPragma <= 0:
inc g.inPragma inc g.inPragma
#if not previousNL(g):
put(g, tkSpaces, Space) put(g, tkSpaces, Space)
put(g, tkCurlyDotLe, "{.") put(g, tkCurlyDotLe, "{.")
gcomma(g, n, emptyContext) gcomma(g, n, emptyContext)