* removed code duplication
* changelog entry, adopt tests for changes
This commit is contained in:
Arne Döring 2018-04-04 15:37:07 +02:00 • committed by Andreas Rumpf
commit 5d46e1eaa2
4 changed files with 21 additions and 47 deletions

View file

@ -18,6 +18,9 @@
### Library changes ### Library changes
- ``macros.astGenRepr``, ``macros.lispRepr`` and ``macros.treeRepr``
now escapes the content of string literals consistently.
### Language additions ### Language additions
### Language changes ### Language changes

View file

@ -620,7 +620,7 @@ proc treeRepr*(n: NimNode): string {.compileTime, benign.} =
of nnkNilLit: res.add(" nil") of nnkNilLit: res.add(" nil")
of nnkCharLit..nnkInt64Lit: res.add(" " & $n.intVal) of nnkCharLit..nnkInt64Lit: res.add(" " & $n.intVal)
of nnkFloatLit..nnkFloat64Lit: res.add(" " & $n.floatVal) of nnkFloatLit..nnkFloat64Lit: res.add(" " & $n.floatVal)
of nnkStrLit..nnkTripleStrLit: res.add(" " & $n.strVal) of nnkStrLit..nnkTripleStrLit: res.add(" " & $n.strVal.newLit.repr)
of nnkIdent: res.add(" ident\"" & $n.ident & '"') of nnkIdent: res.add(" ident\"" & $n.ident & '"')
of nnkSym: res.add(" \"" & $n.symbol & '"') of nnkSym: res.add(" \"" & $n.symbol & '"')
of nnkNone: assert false of nnkNone: assert false
@ -645,7 +645,7 @@ proc lispRepr*(n: NimNode): string {.compileTime, benign.} =
of nnkNilLit: add(result, "nil") of nnkNilLit: add(result, "nil")
of nnkCharLit..nnkInt64Lit: add(result, $n.intVal) of nnkCharLit..nnkInt64Lit: add(result, $n.intVal)
of nnkFloatLit..nnkFloat64Lit: add(result, $n.floatVal) of nnkFloatLit..nnkFloat64Lit: add(result, $n.floatVal)
of nnkStrLit..nnkTripleStrLit: add(result, $n.strVal) of nnkStrLit..nnkTripleStrLit, nnkCommentStmt: add(result, n.strVal.newLit.repr)
of nnkIdent: add(result, "ident\"" & $n.ident & '"') of nnkIdent: add(result, "ident\"" & $n.ident & '"')
of nnkSym: add(result, $n.symbol) of nnkSym: add(result, $n.symbol)
of nnkNone: assert false of nnkNone: assert false
@ -681,33 +681,6 @@ proc astGenRepr*(n: NimNode): string {.compileTime, benign.} =
NodeKinds = {nnkEmpty, nnkNilLit, nnkIdent, nnkSym, nnkNone, nnkCommentStmt} NodeKinds = {nnkEmpty, nnkNilLit, nnkIdent, nnkSym, nnkNone, nnkCommentStmt}
LitKinds = {nnkCharLit..nnkInt64Lit, nnkFloatLit..nnkFloat64Lit, nnkStrLit..nnkTripleStrLit} LitKinds = {nnkCharLit..nnkInt64Lit, nnkFloatLit..nnkFloat64Lit, nnkStrLit..nnkTripleStrLit}
proc escape(s: string, prefix = "\"", suffix = "\""): string {.noSideEffect.} =
## Functions copied from strutils
proc toHex(x: BiggestInt, len: Positive): string {.noSideEffect, rtl.} =
const
HexChars = "0123456789ABCDEF"
var
t = x
result = newString(len)
for j in countdown(len-1, 0):
result[j] = HexChars[int(t and 0xF)]
t = t shr 4
# handle negative overflow
if t == 0 and x < 0: t = -1
result = newStringOfCap(s.len + s.len shr 2)
result.add(prefix)
for c in items(s):
case c
of '\0'..'\31', '\128'..'\255':
add(result, "\\x")
add(result, toHex(ord(c), 2))
of '\\': add(result, "\\\\")
of '\'': add(result, "\\'")
of '\"': add(result, "\\\"")
else: add(result, c)
add(result, suffix)
proc traverse(res: var string, level: int, n: NimNode) {.benign.} = proc traverse(res: var string, level: int, n: NimNode) {.benign.} =
for i in 0..level-1: res.add " " for i in 0..level-1: res.add " "
if n.kind in NodeKinds: if n.kind in NodeKinds:
@ -723,9 +696,9 @@ proc astGenRepr*(n: NimNode): string {.compileTime, benign.} =
of nnkCharLit: res.add("'" & $chr(n.intVal) & "'") of nnkCharLit: res.add("'" & $chr(n.intVal) & "'")
of nnkIntLit..nnkInt64Lit: res.add($n.intVal) of nnkIntLit..nnkInt64Lit: res.add($n.intVal)
of nnkFloatLit..nnkFloat64Lit: res.add($n.floatVal) of nnkFloatLit..nnkFloat64Lit: res.add($n.floatVal)
of nnkStrLit..nnkTripleStrLit, nnkCommentStmt: res.add($n.strVal.escape()) of nnkStrLit..nnkTripleStrLit, nnkCommentStmt: res.add($n.strVal.newLit.repr)
of nnkIdent: res.add(($n.ident).escape()) of nnkIdent: res.add(($n.ident).newLit.repr())
of nnkSym: res.add(($n.symbol).escape()) of nnkSym: res.add(($n.symbol).newLit.repr())
of nnkNone: assert false of nnkNone: assert false
else: else:
res.add(".newTree(") res.add(".newTree(")
@ -774,7 +747,6 @@ macro dumpTreeImm*(s: untyped): untyped {.deprecated.} = echo s.treeRepr
macro dumpLispImm*(s: untyped): untyped {.deprecated.} = echo s.lispRepr macro dumpLispImm*(s: untyped): untyped {.deprecated.} = echo s.lispRepr
## Deprecated. ## Deprecated.
proc newEmptyNode*(): NimNode {.compileTime, noSideEffect.} = proc newEmptyNode*(): NimNode {.compileTime, noSideEffect.} =
## Create a new empty node ## Create a new empty node
result = newNimNode(nnkEmpty) result = newNimNode(nnkEmpty)

View file

@ -22,26 +22,26 @@ StmtList
Empty Empty
Call Call
Ident ident"foo070" Ident ident"foo070"
StrLit test StrLit "test"
StmtList StmtList
DiscardStmt DiscardStmt
Empty Empty
Call Call
Ident ident"foo080" Ident ident"foo080"
StrLit test StrLit "test"
StmtList StmtList
DiscardStmt DiscardStmt
Empty Empty
Command Command
Ident ident"foo090" Ident ident"foo090"
StrLit test StrLit "test"
StmtList StmtList
DiscardStmt DiscardStmt
Empty Empty
Command Command
Ident ident"foo100" Ident ident"foo100"
Call Call
StrLit test StrLit "test"
StmtList StmtList
DiscardStmt DiscardStmt
Empty Empty
@ -540,4 +540,3 @@ dumpTree:
foo380.add((quote do: foo380.add((quote do:
discard discard
)[0]) )[0])

View file

@ -26,12 +26,12 @@ proc checkNode(arg: NimNode; name: string): void {. compileTime .} =
seqAppend.add(arg) # bit this creates a copy seqAppend.add(arg) # bit this creates a copy
arg.add newCall(ident"echo", newLit("Hello World")) arg.add newCall(ident"echo", newLit("Hello World"))
assertEq arg.lispRepr , """StmtList(DiscardStmt(Empty()), Call(Ident(ident"echo"), StrLit(Hello World)))""" assertEq arg.lispRepr , """StmtList(DiscardStmt(Empty()), Call(Ident(ident"echo"), StrLit("Hello World")))"""
assertEq node.lispRepr , """StmtList(DiscardStmt(Empty()), Call(Ident(ident"echo"), StrLit(Hello World)))""" assertEq node.lispRepr , """StmtList(DiscardStmt(Empty()), Call(Ident(ident"echo"), StrLit("Hello World")))"""
assertEq nodeArray[0].lispRepr , """StmtList(DiscardStmt(Empty()), Call(Ident(ident"echo"), StrLit(Hello World)))""" assertEq nodeArray[0].lispRepr , """StmtList(DiscardStmt(Empty()), Call(Ident(ident"echo"), StrLit("Hello World")))"""
assertEq nodeSeq[0].lispRepr , """StmtList(DiscardStmt(Empty()), Call(Ident(ident"echo"), StrLit(Hello World)))""" assertEq nodeSeq[0].lispRepr , """StmtList(DiscardStmt(Empty()), Call(Ident(ident"echo"), StrLit("Hello World")))"""
assertEq seqAppend[0].lispRepr , """StmtList(DiscardStmt(Empty()), Call(Ident(ident"echo"), StrLit(Hello World)))""" assertEq seqAppend[0].lispRepr , """StmtList(DiscardStmt(Empty()), Call(Ident(ident"echo"), StrLit("Hello World")))"""
assertEq seqAppend[1].lispRepr , """StmtList(DiscardStmt(Empty()), Call(Ident(ident"echo"), StrLit(Hello World)))""" assertEq seqAppend[1].lispRepr , """StmtList(DiscardStmt(Empty()), Call(Ident(ident"echo"), StrLit("Hello World")))"""
echo "OK" echo "OK"