swissChili | 923bd53 | 2021-12-08 22:48:58 -0800 | [diff] [blame] | 1 | #include "PPrint.h" |
swissChili | 323883d | 2022-02-20 16:35:23 -0800 | [diff] [blame] | 2 | |
| 3 | #include <QTextStream> |
| 4 | |
| 5 | void sout(QString string) |
| 6 | { |
| 7 | QTextStream(stdout) << string << '\n'; |
| 8 | } |
| 9 | |
swissChili | ece1ac8 | 2022-02-25 11:20:42 -0800 | [diff] [blame] | 10 | QString pprint(ParseResult val, const Parser &parser, PPrint::Style style) |
swissChili | 323883d | 2022-02-20 16:35:23 -0800 | [diff] [blame] | 11 | { |
| 12 | if (val) |
| 13 | { |
| 14 | return "\033[32mOK\033[0m"; |
| 15 | } |
| 16 | |
swissChili | ece1ac8 | 2022-02-25 11:20:42 -0800 | [diff] [blame] | 17 | QString newline = (style == PPrint::ANSI ? "\n" : "<br>"); |
| 18 | |
| 19 | QString highlighted = parser.line(val.pos().line - 1) + newline; |
swissChili | 323883d | 2022-02-20 16:35:23 -0800 | [diff] [blame] | 20 | |
| 21 | for (int i = 1; i < val.pos().lineOffset; i++) |
| 22 | { |
swissChili | ece1ac8 | 2022-02-25 11:20:42 -0800 | [diff] [blame] | 23 | if (style == PPrint::ANSI) |
| 24 | highlighted += " "; |
| 25 | else |
| 26 | highlighted += " "; |
swissChili | 323883d | 2022-02-20 16:35:23 -0800 | [diff] [blame] | 27 | } |
swissChili | 323883d | 2022-02-20 16:35:23 -0800 | [diff] [blame] | 28 | |
swissChili | ece1ac8 | 2022-02-25 11:20:42 -0800 | [diff] [blame] | 29 | if (style == PPrint::ANSI) |
| 30 | { |
| 31 | highlighted += "\033[31m^~~\033[0m"; |
| 32 | } |
| 33 | else if (style == PPrint::HTML) |
| 34 | { |
| 35 | highlighted += "<font color=\"red\">^~~</font>"; |
| 36 | } |
| 37 | |
| 38 | return val.message() + " at " + val.pos() + newline + highlighted; |
swissChili | 323883d | 2022-02-20 16:35:23 -0800 | [diff] [blame] | 39 | } |
swissChili | 918557c | 2022-02-20 20:16:34 -0800 | [diff] [blame] | 40 | |
| 41 | void eout(QString string) |
| 42 | { |
| 43 | QTextStream(stderr) << string << '\n'; |
| 44 | } |
swissChili | d845a14 | 2022-02-25 15:46:35 -0800 | [diff] [blame] | 45 | |
| 46 | QString pprint(QList<Token> val) |
| 47 | { |
| 48 | return pprintDense(val); |
| 49 | } |
| 50 | |
| 51 | QString pprint(QList<AstNode> val) |
| 52 | { |
| 53 | return pprintDense(val); |
| 54 | } |