Add support for str.from_code (#3829)
[cvc5.git] / src / parser / antlr_tracing.h
1 /********************* */
2 /*! \file antlr_tracing.h
3 ** \verbatim
4 ** Top contributors (to current version):
5 ** Morgan Deters
6 ** This file is part of the CVC4 project.
7 ** Copyright (c) 2009-2019 by the authors listed in the file AUTHORS
8 ** in the top-level source directory) and their institutional affiliations.
9 ** All rights reserved. See the file COPYING in the top-level source
10 ** directory for licensing information.\endverbatim
11 **
12 ** \brief [[ Add one-line brief description here ]]
13 **
14 ** [[ Add lengthier description here ]]
15 ** \todo document this file
16 **/
17
18 #ifndef CVC4__PARSER__ANTLR_TRACING_H
19 #define CVC4__PARSER__ANTLR_TRACING_H
20
21 // only enable the hack with -DCVC4_TRACE_ANTLR
22 #ifdef CVC4_TRACE_ANTLR
23
24 #include <iostream>
25 #include <string>
26
27 #include "base/output.h"
28
29 /* The ANTLR lexer generator, as of v3.2, puts Java trace commands
30 * into our beautiful generated C lexer! How awful! This is clearly
31 * a bug (the parser generator traces with printf()), but we have to
32 * do something about it. Basically, the things look like this:
33 *
34 * System.out.println("something"+somethingElse+"another thing");
35 *
36 * What to do to make this C++?! Alas, you cannot
37 * "#define System.out.println", because it has dots in it.
38 * So we do the following. Sigh.
39 *
40 * This is all C++, and the generated lexer/parser is C++, but that's
41 * ok here, we use the C++ compiler anyway! Plus, this is only code
42 * for **debugging** lexers and parsers.
43 */
44
45 /** A "System" object, just like in Java! */
46 static struct __Cvc4System {
47 /**
48 * Helper class just to handle arbitrary string concatenation
49 * with Java syntax. In C++ you cannot do "char*" + "char*",
50 * so instead we fool the type system into calling
51 * JavaPrinter::operator+() for each item successively.
52 */
53 struct JavaPrinter {
54 template <class T>
55 JavaPrinter operator+(const T& t) const {
56 Message() << t;
57 return JavaPrinter();
58 }
59 };/* struct JavaPrinter */
60
61 /** A "System.out" object, just like in Java! */
62 struct {
63 /**
64 * By the time println() is called, we've completely
65 * evaluated (and thus printed) its entire argument, thanks
66 * to the call-by-value semantics of C. All that's left to
67 * do is print the newline.
68 */
69 void println(JavaPrinter) { Message() << std::endl; }
70 } out;
71 } System;
72
73 // Now define println(): this kicks off the successive operator+() calls
74 // Also define "failed" because ANTLR 3.3 uses "failed" improperly.
75 // These are highly dependent on the bugs in a particular ANTLR release.
76 // These seem to work with ANTLR 3.3 as of 4/23/2011. A different trick
77 // works with ANTLR 3.2. EXPECT LOTS OF COMPILER WARNINGS.
78 #define println(x) println(({int failed=0;__Cvc4System::JavaPrinter()+x;}))
79 #undef ANTLR3_FPRINTF
80 #define ANTLR3_FPRINTF(args...) {int failed=0;fprintf(args);}
81 #undef ANTLR3_PRINTF
82 #define ANTLR3_PRINTF(args...) {int failed=0;printf(args);}
83
84 #endif /* CVC4_TRACE_ANTLR */
85
86 #endif /* CVC4__PARSER__ANTLR_TRACING_H */