How to print debug message?
How can I print debug messages to the console?
Asked by Sven Stork on 15 January 2013 at 22:58
3 Answers
In Spoofax/Stratego the command to write to the console is called debug. The following example demonstrate the usage of the debug function to trace the execution of the type checking rule for a while loop:
Answered by Sven Stork on 15 January 2013 at 23:01constraint-error: While(exp, body) -> (exp, ["While loop conditions needs to have to bool and not ", texp] ) where <debug> $[type checking of while loop [<id>]]; texp := <type-of> exp; <debug> $[ type of condition is [texp]]; <not(eq)>(texp, Bool())
The debug(s) strategy can be used, but it has some pitfalls. Normally it prints the message produced by the strategy s, followed by the current term. The first issue is that the argument is usually just a static String, however, if you forget the
!
build operator, it implicitly becomes a match against the String. In that case the debug strategy fails and prints nothing unless the current term is equivalent to the text message, which is very confusing. The second issue I’ve found is that in some cases it doesn’t print the annotation, e.g. when printing a String term the annotation is hidden.Here is the variant I use in WebDSL that addresses these issues ( https://svn.strategoxt.org/repos/WebDSL/webdsls/trunk/src/org/webdsl/dsl/utils/debug.str ):
Answered by Danny Groenewegen on 16 January 2013 at 11:46debug(|t) = where(if is-string then write-to-string end; debug(!t))
Another useful technique is trace:
trace(s|m) = where(indent := <trace-indent <+ !"">()) ; ((debug(!$[[indent]>>>([m]): ]) ; {|trace-indent: rules(trace-indent:() -> <conc-strings>(" ",indent)) ; s |} ; debug(!$[[indent]<<<([m]): ])) <+ (debug(!$[[indent]!!!([m]): ]); fail))
This prints the input term, the output term, and whether the result was success or failure. You can wrap it around any strategy or partial strategy, like:
Answered by Dobes Vandermeer on 16 January 2013 at 18:30tracing-desugar = trace(desugar | "desugar")