Skip to content

Commit

Permalink
[CHECKPOINT]
Browse files Browse the repository at this point in the history
  • Loading branch information
haxscramper committed Jan 26, 2022
1 parent 431c4ea commit b42b2c4
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 62 deletions.
5 changes: 2 additions & 3 deletions compiler/front/cli_reporter.nim
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ import
],
vm/[
vm_enums
],
./reporter_common
]

import front/options as compiler_options

Expand Down Expand Up @@ -123,7 +122,7 @@ proc toStr(conf: ConfigRef, loc: TLineInfo, dropExt: bool = false): string =
## Convert location to printable string
conf.wrap(
"$1($2, $3)" % [
conf.formatPath(loc.fileIndex).dropExt(dropExt),
conf.toMsgFilename(loc.fileIndex).dropExt(dropExt),
$loc.line,
$(loc.col + ColOffset)
],
Expand Down
41 changes: 0 additions & 41 deletions compiler/front/reporter_common.nim

This file was deleted.

22 changes: 9 additions & 13 deletions compiler/front/sexp_reporter.nim
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
## Implementation of the CLI message generator
## Implementation of the structured CLI message generator. Using
## `--msgFormat=sexp` will make compiler switch to the report hook
## implemented in this module.

import
experimental/[
sexp,
diff,
colortext
colortext,
sexp_diff
],
ast/[
lineinfos,
Expand All @@ -17,8 +20,7 @@ import
],
std/[
strutils
],
./reporter_common.nim
]


var writeConf: ConfigRef
Expand Down Expand Up @@ -50,14 +52,8 @@ proc sexp*(s: cstring): SexpNode = sexp($s)

proc sexp*(v: SomeInteger): SexpNode = newSInt(BiggestInt(v))
proc sexp*(id: FileIndex): SexpNode =
sexp(writeConf.formatPath(id))

sexp(writeConf.toMsgFilename(id))

# proc sexp*(id: ReportLineInfo): SexpNode =
# sexp([
# line = sexp(id.line),
# filename =
# ])

iterator sexpFields[T](obj: T, ignore: seq[string] = @[]): SexpNode =
for name, value in fieldPairs(obj):
Expand Down Expand Up @@ -183,7 +179,7 @@ proc reportHook*(conf: ConfigRef, r: Report): TErrorHandling =
of repExternal: s.addFields(r.externalReport)

if wkind == writeForceEnabled:
echo s.toLine()
echo s.toLine().toString(conf.useColor)

else:
conf.writeln(s.toLine())
conf.writeln(s.toLine().toString(conf.useColor))
6 changes: 3 additions & 3 deletions lib/experimental/colordiff.nim
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ proc formatInlineDiff*(

proc formatDiffed*(
shifted: ShiftedDiff,
oldSeq, newSeq: openarray[string],
oldSeq, newSeq: openArray[string],
conf: DiffFormatConf = diffFormatter()
): ColText =
## Format shifted multiline diff
Expand Down Expand Up @@ -579,7 +579,7 @@ proc formatDiffed*(
lhsMax = max(oldText[^1].text.len, lhsMax)

var first = true
for (lhs, rhs) in zip(oldtext, newtext):
for (lhs, rhs) in zip(oldText, newText):
if not first:
# Avoid trailing newline of the diff formatting.
result.add "\n"
Expand All @@ -596,7 +596,7 @@ proc formatDiffed*(
result.add rhs.text

proc formatDiffed*[T](
oldSeq, newSeq: openarray[T],
oldSeq, newSeq: openArray[T],
conf: DiffFormatConf,
eqCmp: proc(a, b: T): bool = (proc(a, b: T): bool = a == b),
strConv: proc(a: T): string = (proc(a: T): string = $a)
Expand Down
4 changes: 2 additions & 2 deletions lib/experimental/colortext.nim
Original file line number Diff line number Diff line change
Expand Up @@ -922,12 +922,12 @@ func addf*(
of addfVar, addfExpr:
assert false, "var/expr formatting is not supported for colored text yet"

of addfPositional, addfIndexed, addfbackIndexed:
of addfPositional, addfIndexed, addfBackIndexed:
let idx = if fr.kind == addfBackIndexed: len(colored) - fr.idx else: fr.idx
assert (0 <= idx and idx < colored.len)
text.add colored[idx]

func `%`*(format: string, interpolate: openarray[ColText]): ColText =
func `%`*(format: string, interpolate: openArray[ColText]): ColText =
## Shorthand for colored text interpolation
result.addf(format, interpolate)

Expand Down
114 changes: 114 additions & 0 deletions tests/stdlib/tsexp_diff.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import experimental/[sexp, sexp_diff]
import std/[tables, algorithm]

proc sortmatches(s1, s2: seq[SexpNode], dir: SortOrder = Ascending):
seq[tuple[key: (int, int), diff: seq[SexpMismatch]]] =

var diffMap = TableRef[(int, int), seq[SexpMismatch]]()

proc reportCmp(a, b: int): int =
let diff = diff(s1[a], s2[b])
diffMap[(a, b)] = diff
return diff.len

let (expected, given, sorted) = sortedStablematch(
s1.len, s2.len, reportCmp, dir)

for (key, val) in sorted:
result.add((key, diffMap[key]))

proc sortmatches(
s1, s2: seq[string],
dir: SortOrder = Ascending
): seq[tuple[key: (int, int), diff: seq[SexpMismatch]]] =
var ss1, ss2: seq[SexpNode]
for item in s1: ss1.add parseSexp(item)
for item in s2: ss2.add parseSexp(item)
return sortmatches(ss1, ss2, dir)


proc matches(s1, s2: string): seq[SexpMismatch] =
diff(s1.parseSexp(), s2.parseSexp())

proc assertEq[T](a, b: T) =
doAssert a == b, "a was " & $a & ", b was " & $b

block literal:
let d = matches("(1)", "(2)")
assertEq d.len, 1
assertEq(d[0].kind, smDifferentLiteral)

block symbol:
let d = matches("(A)", "(B)")
assertEq d.len, 1
assertEq d[0].kind, smDifferentSymbol

block match_all:
let d = matches("(_)", "(Q)")
assertEq d.len, 0

block match_keys:
block ordered_unordered:
for (m1, m2) in @[
("(:a b :c d)", "(:a q :c z)"),
("(:a b :c d)", "(:c q :a z)")
]:
let d = matches(m1, m2)
assertEq d.len, 2
# Mismatches are placed in the same order as they are used in the input
# text
assertEq d[0].path[0].key, "a"
assertEq d[1].path[0].key, "c"
assertEq d[0].kind, smDifferentSymbol
assertEq d[1].kind, smDifferentSymbol

block elements:
let d = matches("(User :key 12)", "(Azer :id 14 :key 3)")
assertEq d.len, 2
assertEq d[0].kind, smDifferentLiteral
assertEq d[1].kind, smDifferentSymbol

block weighed_matching:
let d = sortmatches(@["(User)"], @["(User2)"])
assertEq d[0].key, (0, 0)
assertEq d[0].diff.len, 1
assertEq d[0].diff[0].kind, smDifferentSymbol

block hint_matching:
let d = sortmatches(@[
"""(User :location ("tfile.nim" 11 7) :severity Hint :str "Another hint")""",
"""(User :location ("tfile.nim" 8 _) :str "User Hint")"""
], @[
"""(User :location ("tfile.nim" 8 _) :str "User Hint")""",
"""(User :location ("tfile.nim" 9 6) :severity Hint :str "User hint")"""
])

echo d

# @[(
# key: (0, 1),
# diff: @[
# (path: @[(kind: spKey, key: "location"), (kind: spIndex, index: 1)],
# kind: smDifferentLiteral,
# expected: 11,
# found: 9,
# arraydiff: (target: @[], input: @[])),
# (path: @[(kind: spKey, key: "location"), (kind: spIndex, index: 2)],
# kind: smDifferentLiteral,
# expected: 7,
# found: 6,
# arraydiff: (target: @[], input: @[])),
# (path: @[(kind: spKey, key: "str")],
# kind: smDifferentLiteral,
# expected: "Another hint",
# found: "User hint",
# arraydiff: (target: @[], input: @[]))
# ]),
# (
# key: (0, 0),
# diff: @[
# (path: @[(kind: spKey, key: "location"), (kind: spIndex, index: 1)], kind: smDifferentLiteral, expected: 11, found: 8, arraydiff: (target: @[], input: @[])),
# (path: @[(kind: spKey, key: "location"), (kind: spIndex, index: 2)], kind: smKindMismatch, expected: 7, found: _, arraydiff: (target: @[], input: @[])),
# (path: @[], kind: smMissingKey, key: "severity"), (path: @[(kind: spKey, key: "str")], kind: smDifferentLiteral, expected: "Another hint", found: "User Hint", arraydiff: (target: @[], input: @[]))
# ])
# ]

0 comments on commit b42b2c4

Please sign in to comment.