-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Testament diff rework - unstructured, structured #206
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
## 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, | ||
sexp_diff | ||
], | ||
ast/[ | ||
lineinfos, | ||
ast, | ||
reports | ||
], | ||
front/[ | ||
options, | ||
msgs | ||
], | ||
std/[ | ||
strutils | ||
] | ||
|
||
import std/options as std_options | ||
|
||
var writeConf: ConfigRef | ||
|
||
|
||
proc addFields[T](s: var SexpNode, r: T, ignore: seq[string] = @[]) | ||
|
||
|
||
|
||
proc sexpItems*[T](s: T): SexpNode = | ||
result = newSList() | ||
for item in items(s): | ||
result.add sexp(item) | ||
|
||
proc sexp*[T: object | tuple](obj: T): SexpNode = | ||
result = newSList() | ||
addFields(result, obj) | ||
|
||
proc sexp*[T: object | tuple](obj: ref T): SexpNode = sexp(obj[]) | ||
proc sexp*[E: enum](e: E): SexpNode = newSSymbol($e) | ||
proc sexp*[T](s: seq[T]): SexpNode = sexpItems(s) | ||
proc sexp*[R, T](s: array[R, T]): SexpNode = sexpItems(s) | ||
proc sexp*[I](s: set[I]): SexpNode = sexpItems(s) | ||
proc sexp*(s: cstring): SexpNode = sexp($s) | ||
|
||
proc sexp*(v: SomeInteger): SexpNode = newSInt(BiggestInt(v)) | ||
proc sexp*(id: FileIndex): SexpNode = | ||
sexp(writeConf.toMsgFilename(id)) | ||
|
||
|
||
iterator sexpFields[T: object | tuple](obj: T, ignore: seq[string] = @[]): SexpNode = | ||
for name, value in fieldPairs(obj): | ||
var pass = true | ||
when value is ref or value is ptr: | ||
if isNil(value): | ||
pass = false | ||
|
||
when value is seq or value is string: | ||
if len(value) == 0: | ||
pass = false | ||
|
||
when value is TLineInfo: | ||
if pass and value == unknownLineInfo: | ||
pass = false | ||
|
||
when value is ReportLineInfo: | ||
if pass and not value.isValid(): | ||
pass = false | ||
|
||
if pass and name in ignore: | ||
pass = false | ||
|
||
if pass: | ||
yield newSKeyword(name, sexp(value)) | ||
|
||
|
||
proc add*(self: var SexpNode, str: string, expr: SexpNode) = | ||
self.add newSSymbol(":" & str) | ||
self.add expr | ||
|
||
proc sexp*[T](o: Option[T]): SexpNode = | ||
if o.isNone: newSNil() else: sexp(o.get()) | ||
haxscramper marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
proc addFields[T](s: var SexpNode, r: T, ignore: seq[string] = @[]) = | ||
for item in sexpFields(r, ignore): | ||
s.add item | ||
|
||
proc sexp*(i: ReportLineInfo): SexpNode = | ||
convertSexp([ | ||
writeConf.formatPath(i.file).sexp(), | ||
sexp(i.line), | ||
sexp(i.col) | ||
]) | ||
|
||
proc sexp*(i: TLineInfo): SexpNode = | ||
convertSexp([sexp(i.fileIndex), sexp(i.line), sexp(i.col)]) | ||
|
||
proc sexp*(e: StackTraceEntry): SexpNode = | ||
result = newSList() | ||
result.addFields(e, @["filename"]) | ||
result.add newSKeyword( | ||
"filename", writeConf.formatPath($e.filename).sexp()) | ||
|
||
|
||
proc sexp*(typ: PType): SexpNode = | ||
if typ.isNil: return newSNil() | ||
result = newSList() | ||
result.add newSSymbol(($typ.kind)[2 ..^ 1]) | ||
if typ.sons.len > 0: | ||
result.add("sons", sexp(typ.sons)) | ||
|
||
proc sexp*(node: PNode): SexpNode = | ||
if node.isNil: return newSNil() | ||
|
||
result = newSList() | ||
result.add newSSymbol(($node.kind)[2 ..^ 1]) | ||
case node.kind: | ||
of nkCharLit..nkUInt64Lit: result.add sexp(node.intVal) | ||
of nkFloatLit..nkFloat128Lit: result.add sexp(node.floatVal) | ||
of nkStrLit..nkTripleStrLit: result.add sexp(node.strVal) | ||
of nkSym: result.add newSSymbol(node.sym.name.s) | ||
of nkIdent: result.add newSSymbol(node.ident.s) | ||
else: | ||
for node in node.sons: | ||
result.add sexp(node) | ||
|
||
proc sexp*(t: PSym): SexpNode = | ||
convertSexp([ | ||
substr($t.kind, 2).newSSymbol(), | ||
name = sexp(t.name.s), | ||
info = sexp(t.info) | ||
]) | ||
|
||
|
||
proc reportHook*(conf: ConfigRef, r: Report): TErrorHandling = | ||
writeConf = conf | ||
let wkind = conf.writabilityKind(r) | ||
|
||
if wkind == writeDisabled: | ||
return | ||
|
||
else: | ||
var s = newSList() | ||
s.add newSSymbol(multiReplace($r.kind, { | ||
"rsem": "Sem", | ||
"rpar": "Par", | ||
"rlex": "Lex", | ||
"rint": "Int", | ||
"rext": "Ext", | ||
"rdbg": "Dbg", | ||
"rback": "Bck", | ||
})) | ||
s.add newSSymbol(":severity") | ||
s.add sexp(conf.severity(r)) | ||
|
||
let f = @["kind"] | ||
|
||
case r.category: | ||
of repLexer: s.addFields(r.lexReport, f) | ||
of repParser: s.addFields(r.parserReport, f) | ||
of repCmd: s.addFields(r.cmdReport, f) | ||
of repSem: | ||
if r.kind == rsemProcessingStmt: | ||
s.addFields(r.semReport, f & "node") | ||
|
||
else: | ||
s.addFields(r.semReport, f) | ||
|
||
of repDebug: s.addFields(r.debugReport, f) | ||
of repInternal: s.addFields(r.internalReport, f) | ||
of repBackend: s.addFields(r.backendReport, f) | ||
of repExternal: s.addFields(r.externalReport, f) | ||
|
||
if wkind == writeForceEnabled: | ||
echo s.toLine().toString(conf.useColor) | ||
|
||
else: | ||
conf.writeln(s.toLine().toString(conf.useColor)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we move this below the other s-exp procs, it flows better and if the T is one of the ones below it'll not be confusing if some declares a new one but this doesn't take it into account up here.
Also, seriously this probably shouldn't compile.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what 'process' are you talking about and why it shouldn't compile?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo, yeah it's strange that it's allowed by the compiler.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
again, why?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because a type
T
is not guaranteed to have anitems
, yes?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It won't compile when I instantiate the proc like every other generic, so I guess this is not allowed? I just don't get why this remark is attached to a definition. sexp two lines above can have $ defined as {.error.} and it would've been an instantiation error as well