1 /********************* */
2 /*! \file antlr_tracing.h
4 ** Top contributors (to current version):
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
12 ** \brief [[ Add one-line brief description here ]]
14 ** [[ Add lengthier description here ]]
15 ** \todo document this file
18 #ifndef CVC4__PARSER__ANTLR_TRACING_H
19 #define CVC4__PARSER__ANTLR_TRACING_H
21 // only enable the hack with -DCVC4_TRACE_ANTLR
22 #ifdef CVC4_TRACE_ANTLR
27 #include "base/output.h"
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:
34 * System.out.println("something"+somethingElse+"another thing");
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.
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.
45 /** A "System" object, just like in Java! */
46 static struct __Cvc4System
{
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.
55 JavaPrinter
operator+(const T
& t
) const {
59 };/* struct JavaPrinter */
61 /** A "System.out" object, just like in Java! */
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.
69 void println(JavaPrinter
) { Message() << std::endl
; }
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;}))
80 #define ANTLR3_FPRINTF(args...) {int failed=0;fprintf(args);}
82 #define ANTLR3_PRINTF(args...) {int failed=0;printf(args);}
84 #endif /* CVC4_TRACE_ANTLR */
86 #endif /* CVC4__PARSER__ANTLR_TRACING_H */