2 /// Provides the debugging functions invoked by a recognizer
3 /// built using the debug generator mode of the antlr tool.
4 /// See antlr3debugeventlistener.h for documentation.
8 // Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC
9 // http://www.temporal-wave.com
10 // http://www.linkedin.com/in/jimidle
12 // All rights reserved.
14 // Redistribution and use in source and binary forms, with or without
15 // modification, are permitted provided that the following conditions
17 // 1. Redistributions of source code must retain the above copyright
18 // notice, this list of conditions and the following disclaimer.
19 // 2. Redistributions in binary form must reproduce the above copyright
20 // notice, this list of conditions and the following disclaimer in the
21 // documentation and/or other materials provided with the distribution.
22 // 3. The name of the author may not be used to endorse or promote products
23 // derived from this software without specific prior written permission.
25 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 // Not everyone wishes to include the debugger stuff in their final deployment because
39 // it will then rely on being linked with the socket libraries. Hence if the programmer turns
40 // off the debugging, we do some dummy stuff that satifies compilers etc but means there is
41 // no debugger and no reliance on the socket librarires. If you set this flag, then using the -debug
42 // option to generate your code will produce code that just crashes, but then I presme you are smart
43 // enough to realize that building the libraries without debugger support means you can't call the
46 #ifdef ANTLR3_NODEBUGGER
47 ANTLR3_API pANTLR3_DEBUG_EVENT_LISTENER
48 antlr3DebugListenerNew()
50 ANTLR3_PRINTF("C runtime was compiled without debugger support. This program will crash!!");
55 static ANTLR3_BOOLEAN handshake (pANTLR3_DEBUG_EVENT_LISTENER delboy);
56 static void enterRule (pANTLR3_DEBUG_EVENT_LISTENER delboy, const char * grammarFileName, const char * ruleName);
57 static void enterAlt (pANTLR3_DEBUG_EVENT_LISTENER delboy, int alt);
58 static void exitRule (pANTLR3_DEBUG_EVENT_LISTENER delboy, const char * grammarFileName, const char * ruleName);
59 static void enterSubRule (pANTLR3_DEBUG_EVENT_LISTENER delboy, int decisionNumber);
60 static void exitSubRule (pANTLR3_DEBUG_EVENT_LISTENER delboy, int decisionNumber);
61 static void enterDecision (pANTLR3_DEBUG_EVENT_LISTENER delboy, int decisionNumber);
62 static void exitDecision (pANTLR3_DEBUG_EVENT_LISTENER delboy, int decisionNumber);
63 static void consumeToken (pANTLR3_DEBUG_EVENT_LISTENER delboy, pANTLR3_COMMON_TOKEN t);
64 static void consumeHiddenToken (pANTLR3_DEBUG_EVENT_LISTENER delboy, pANTLR3_COMMON_TOKEN t);
65 static void LT (pANTLR3_DEBUG_EVENT_LISTENER delboy, int i, pANTLR3_COMMON_TOKEN t);
66 static void mark (pANTLR3_DEBUG_EVENT_LISTENER delboy, ANTLR3_MARKER marker);
67 static void rewindMark (pANTLR3_DEBUG_EVENT_LISTENER delboy, ANTLR3_MARKER marker);
68 static void rewindLast (pANTLR3_DEBUG_EVENT_LISTENER delboy);
69 static void beginBacktrack (pANTLR3_DEBUG_EVENT_LISTENER delboy, int level);
70 static void endBacktrack (pANTLR3_DEBUG_EVENT_LISTENER delboy, int level, ANTLR3_BOOLEAN successful);
71 static void location (pANTLR3_DEBUG_EVENT_LISTENER delboy, int line, int pos);
72 static void recognitionException (pANTLR3_DEBUG_EVENT_LISTENER delboy, pANTLR3_EXCEPTION e);
73 static void beginResync (pANTLR3_DEBUG_EVENT_LISTENER delboy);
74 static void endResync (pANTLR3_DEBUG_EVENT_LISTENER delboy);
75 static void semanticPredicate (pANTLR3_DEBUG_EVENT_LISTENER delboy, ANTLR3_BOOLEAN result, const char * predicate);
76 static void commence (pANTLR3_DEBUG_EVENT_LISTENER delboy);
77 static void terminate (pANTLR3_DEBUG_EVENT_LISTENER delboy);
78 static void consumeNode (pANTLR3_DEBUG_EVENT_LISTENER delboy, pANTLR3_BASE_TREE t);
79 static void LTT (pANTLR3_DEBUG_EVENT_LISTENER delboy, int i, pANTLR3_BASE_TREE t);
80 static void nilNode (pANTLR3_DEBUG_EVENT_LISTENER delboy, pANTLR3_BASE_TREE t);
81 static void errorNode (pANTLR3_DEBUG_EVENT_LISTENER delboy, pANTLR3_BASE_TREE t);
82 static void createNode (pANTLR3_DEBUG_EVENT_LISTENER delboy, pANTLR3_BASE_TREE t);
83 static void createNodeTok (pANTLR3_DEBUG_EVENT_LISTENER delboy, pANTLR3_BASE_TREE node, pANTLR3_COMMON_TOKEN token);
84 static void becomeRoot (pANTLR3_DEBUG_EVENT_LISTENER delboy, pANTLR3_BASE_TREE newRoot, pANTLR3_BASE_TREE oldRoot);
85 static void addChild (pANTLR3_DEBUG_EVENT_LISTENER delboy, pANTLR3_BASE_TREE root, pANTLR3_BASE_TREE child);
86 static void setTokenBoundaries (pANTLR3_DEBUG_EVENT_LISTENER delboy, pANTLR3_BASE_TREE t, ANTLR3_MARKER tokenStartIndex, ANTLR3_MARKER tokenStopIndex);
87 static void ack (pANTLR3_DEBUG_EVENT_LISTENER delboy);
89 /// Create and initialize a new debug event listener that can be connected to
90 /// by ANTLRWorks and any other debugger via a socket.
92 ANTLR3_API pANTLR3_DEBUG_EVENT_LISTENER
93 antlr3DebugListenerNew()
95 pANTLR3_DEBUG_EVENT_LISTENER delboy;
97 delboy = ANTLR3_CALLOC(1, sizeof(ANTLR3_DEBUG_EVENT_LISTENER));
104 // Initialize the API
106 delboy->addChild = addChild;
107 delboy->becomeRoot = becomeRoot;
108 delboy->beginBacktrack = beginBacktrack;
109 delboy->beginResync = beginResync;
110 delboy->commence = commence;
111 delboy->consumeHiddenToken = consumeHiddenToken;
112 delboy->consumeNode = consumeNode;
113 delboy->consumeToken = consumeToken;
114 delboy->createNode = createNode;
115 delboy->createNodeTok = createNodeTok;
116 delboy->endBacktrack = endBacktrack;
117 delboy->endResync = endResync;
118 delboy->enterAlt = enterAlt;
119 delboy->enterDecision = enterDecision;
120 delboy->enterRule = enterRule;
121 delboy->enterSubRule = enterSubRule;
122 delboy->exitDecision = exitDecision;
123 delboy->exitRule = exitRule;
124 delboy->exitSubRule = exitSubRule;
125 delboy->handshake = handshake;
126 delboy->location = location;
130 delboy->nilNode = nilNode;
131 delboy->recognitionException = recognitionException;
132 delboy->rewind = rewindMark;
133 delboy->rewindLast = rewindLast;
134 delboy->semanticPredicate = semanticPredicate;
135 delboy->setTokenBoundaries = setTokenBoundaries;
136 delboy->terminate = terminate;
137 delboy->errorNode = errorNode;
139 delboy->PROTOCOL_VERSION = 2; // ANTLR 3.1 is at protocol version 2
141 delboy->port = DEFAULT_DEBUGGER_PORT;
146 pANTLR3_DEBUG_EVENT_LISTENER
147 antlr3DebugListenerNewPort(ANTLR3_UINT32 port)
149 pANTLR3_DEBUG_EVENT_LISTENER delboy;
151 delboy = antlr3DebugListenerNew();
161 //--------------------------------------------------------------------------------
162 // Support functions for sending stuff over the socket interface
165 sockSend(SOCKET sock, const char * ptr, int len)
174 // Send as many bytes as we can
176 thisSend = send(sock, ptr, len - sent, 0);
178 // Check for errors and tell the user if we got one
185 // Increment our offset by how many we were able to send
193 static ANTLR3_BOOLEAN
194 handshake (pANTLR3_DEBUG_EVENT_LISTENER delboy)
196 /// Connection structure with which to wait and accept a connection from
201 // Connection structures to deal with the client after we accept the connection
202 // and the server while we accept a connection.
204 ANTLR3_SOCKADDRT client;
205 ANTLR3_SOCKADDRT server;
207 // Buffer to construct our message in
211 // Specifies the length of the connection structure to accept()
212 // Windows use int, everyone else uses size_t
214 ANTLR3_SALENT sockaddr_len;
216 // Option holder for setsockopt()
220 if (delboy->initialized == ANTLR3_FALSE)
222 // Windows requires us to initialize WinSock.
224 #ifdef ANTLR3_WINDOWS
226 WORD wVersionRequested;
228 int err; // Return code from WSAStartup
230 // We must initialise the Windows socket system when the DLL is loaded.
231 // We are asking for Winsock 1.1 or better as we don't need anything
232 // too complicated for this.
234 wVersionRequested = MAKEWORD( 1, 1);
236 err = WSAStartup( wVersionRequested, &wsaData );
240 // Tell the user that we could not find a usable
248 // Create the server socket, we are the server because we just wait until
249 // a debugger connects to the port we are listening on.
251 serverSocket = socket(AF_INET, SOCK_STREAM, 0);
253 if (serverSocket == INVALID_SOCKET)
258 // Set the listening port
260 server.sin_port = htons((unsigned short)delboy->port);
261 server.sin_family = AF_INET;
262 server.sin_addr.s_addr = htonl (INADDR_ANY);
264 // We could allow a rebind on the same addr/port pair I suppose, but
265 // I imagine that most people will just want to start debugging one parser at once.
266 // Maybe change this at some point, but rejecting the bind at this point will ensure
267 // that people realize they have left something running in the background.
269 if (bind(serverSocket, (pANTLR3_SOCKADDRC)&server, sizeof(server)) == -1)
274 // We have bound the socket to the port and address so we now ask the TCP subsystem
275 // to start listening on that address/port
277 if (listen(serverSocket, 1) == -1)
279 // Some error, just fail
284 // Now we can try to accept a connection on the port
286 sockaddr_len = sizeof(client);
287 delboy->socket = accept(serverSocket, (pANTLR3_SOCKADDRC)&client, &sockaddr_len);
289 // Having accepted a connection, we can stop listening and close down the socket
291 shutdown (serverSocket, 0x02);
292 ANTLR3_CLOSESOCKET (serverSocket);
294 if (delboy->socket == -1)
299 // Disable Nagle as this is essentially a chat exchange
302 setsockopt(delboy->socket, SOL_SOCKET, TCP_NODELAY, (const void *)&optVal, sizeof(optVal));
306 // We now have a good socket connection with the debugging client, so we
307 // send it the protocol version we are using and what the name of the grammar
308 // is that we represent.
310 sprintf (message, "ANTLR %d\n", delboy->PROTOCOL_VERSION);
311 sockSend (delboy->socket, message, (int)strlen(message));
312 sprintf (message, "grammar \"%s\n", delboy->grammarFileName->chars);
313 sockSend (delboy->socket, message, (int)strlen(message));
316 delboy->initialized = ANTLR3_TRUE;
321 // Send the supplied text and wait for an ack from the client
323 transmit(pANTLR3_DEBUG_EVENT_LISTENER delboy, const char * ptr)
325 sockSend(delboy->socket, ptr, (int)strlen(ptr));
330 ack (pANTLR3_DEBUG_EVENT_LISTENER delboy)
332 // Local buffer to read the next character in to
337 // Ack terminates in a line feed, so we just wait for
338 // one of those. Speed is not of the essence so we don't need
339 // to buffer the input or anything.
343 rCount = recv(delboy->socket, &buffer, 1, 0);
345 while (rCount == 1 && buffer != '\n');
347 // If the socket ws closed on us, then we will get an error or
348 // (with a graceful close), 0. We can assume the the debugger stopped for some reason
349 // (such as Java crashing again). Therefore we just exit the program
350 // completely if we don't get the terminating '\n' for the ack.
354 ANTLR3_PRINTF("Exiting debugger as remote client closed the socket\n");
355 ANTLR3_PRINTF("Received char count was %d, and last char received was %02X\n", rCount, buffer);
360 // Given a buffer string and a source string, serialize the
361 // text, escaping any newlines and linefeeds. We have no need
362 // for speed here, this is the debugger.
365 serializeText(pANTLR3_STRING buffer, pANTLR3_STRING text)
368 ANTLR3_UCHAR character;
370 // strings lead in with a "
372 buffer->append(buffer, " \"");
379 // Now we replace linefeeds, newlines and the escape
380 // leadin character '%' with their hex equivalents
383 for (c = 0; c < text->len; c++)
385 switch (character = text->charAt(text, c))
389 buffer->append(buffer, "%0A");
394 buffer->append(buffer, "%0D");
399 buffer->append(buffer, "%25");
402 // Other characters: The Song Remains the Same.
406 buffer->addc(buffer, character);
412 // Given a token, create a stringified version of it, in the supplied
413 // buffer. We create a string for this in the debug 'object', if there
414 // is not one there already, and then reuse it here if asked to do this
418 serializeToken(pANTLR3_DEBUG_EVENT_LISTENER delboy, pANTLR3_COMMON_TOKEN t)
420 // Do we already have a serialization buffer?
422 if (delboy->tokenString == NULL)
424 // No, so create one, using the string factory that
425 // the grammar name used, which is guaranteed to exist.
426 // 64 bytes will do us here for starters.
428 delboy->tokenString = delboy->grammarFileName->factory->newSize(delboy->grammarFileName->factory, 64);
433 delboy->tokenString->set(delboy->tokenString, (const char *)"");
435 // Now we serialize the elements of the token.Note that the debugger only
438 delboy->tokenString->addi(delboy->tokenString, (ANTLR3_INT32)(t->getTokenIndex(t)));
439 delboy->tokenString->addc(delboy->tokenString, ' ');
440 delboy->tokenString->addi(delboy->tokenString, (ANTLR3_INT32)(t->getType(t)));
441 delboy->tokenString->addc(delboy->tokenString, ' ');
442 delboy->tokenString->addi(delboy->tokenString, (ANTLR3_INT32)(t->getChannel(t)));
443 delboy->tokenString->addc(delboy->tokenString, ' ');
444 delboy->tokenString->addi(delboy->tokenString, (ANTLR3_INT32)(t->getLine(t)));
445 delboy->tokenString->addc(delboy->tokenString, ' ');
446 delboy->tokenString->addi(delboy->tokenString, (ANTLR3_INT32)(t->getCharPositionInLine(t)));
448 // Now send the text that the token represents.
450 serializeText(delboy->tokenString, t->getText(t));
452 // Finally, as the debugger is a Java program it will expect to get UTF-8
453 // encoded strings. We don't use UTF-8 internally to the C runtime, so we
454 // must force encode it. We have a method to do this in the string class, but
455 // it returns malloc space that we must free afterwards.
457 return delboy->tokenString->toUTF8(delboy->tokenString);
460 // Given a tree node, create a stringified version of it in the supplied
464 serializeNode(pANTLR3_DEBUG_EVENT_LISTENER delboy, pANTLR3_BASE_TREE node)
466 pANTLR3_COMMON_TOKEN token;
469 // Do we already have a serialization buffer?
471 if (delboy->tokenString == NULL)
473 // No, so create one, using the string factory that
474 // the grammar name used, which is guaranteed to exist.
475 // 64 bytes will do us here for starters.
477 delboy->tokenString = delboy->grammarFileName->factory->newSize(delboy->grammarFileName->factory, 64);
482 delboy->tokenString->set(delboy->tokenString, (const char *)"");
484 // Protect against bugs/errors etc
488 return delboy->tokenString;
491 // Now we serialize the elements of the node.Note that the debugger only
494 delboy->tokenString->addc(delboy->tokenString, ' ');
498 delboy->tokenString->addi(delboy->tokenString, delboy->adaptor->getUniqueID(delboy->adaptor, node));
499 delboy->tokenString->addc(delboy->tokenString, ' ');
501 // Type of the current token (which may be imaginary)
503 delboy->tokenString->addi(delboy->tokenString, delboy->adaptor->getType(delboy->adaptor, node));
505 // See if we have an actual token or just an imaginary
507 token = delboy->adaptor->getToken(delboy->adaptor, node);
509 delboy->tokenString->addc(delboy->tokenString, ' ');
514 delboy->tokenString->addi(delboy->tokenString, (ANTLR3_INT32)(token->getLine(token)));
515 delboy->tokenString->addc(delboy->tokenString, ' ');
516 delboy->tokenString->addi(delboy->tokenString, (ANTLR3_INT32)(token->getCharPositionInLine(token)));
520 // Imaginary tokens have no location
522 delboy->tokenString->addi(delboy->tokenString, -1);
523 delboy->tokenString->addc(delboy->tokenString, ' ');
524 delboy->tokenString->addi(delboy->tokenString, -1);
527 // Start Index of the node
529 delboy->tokenString->addc(delboy->tokenString, ' ');
530 delboy->tokenString->addi(delboy->tokenString, (ANTLR3_UINT32)(delboy->adaptor->getTokenStartIndex(delboy->adaptor, node)));
532 // Now send the text that the node represents.
534 serializeText(delboy->tokenString, delboy->adaptor->getText(delboy->adaptor, node));
536 // Finally, as the debugger is a Java program it will expect to get UTF-8
537 // encoded strings. We don't use UTF-8 internally to the C runtime, so we
538 // must force encode it. We have a method to do this in the string class, but
539 // there is no utf8 string implementation as of yet
541 return delboy->tokenString->toUTF8(delboy->tokenString);
544 //------------------------------------------------------------------------------------------------------------------
548 enterRule (pANTLR3_DEBUG_EVENT_LISTENER delboy, const char * grammarFileName, const char * ruleName)
552 // Create the message (speed is not of the essence)
554 sprintf(buffer, "enterRule %s %s\n", grammarFileName, ruleName);
555 transmit(delboy, buffer);
559 enterAlt (pANTLR3_DEBUG_EVENT_LISTENER delboy, int alt)
563 // Create the message (speed is not of the essence)
565 sprintf(buffer, "enterAlt %d\n", alt);
566 transmit(delboy, buffer);
570 exitRule (pANTLR3_DEBUG_EVENT_LISTENER delboy, const char * grammarFileName, const char * ruleName)
574 // Create the message (speed is not of the essence)
576 sprintf(buffer, "enterRule %s %s\n", grammarFileName, ruleName);
577 transmit(delboy, buffer);
581 enterSubRule (pANTLR3_DEBUG_EVENT_LISTENER delboy, int decisionNumber)
585 // Create the message (speed is not of the essence)
587 sprintf(buffer, "enterSubRule %d\n", decisionNumber);
588 transmit(delboy, buffer);
592 exitSubRule (pANTLR3_DEBUG_EVENT_LISTENER delboy, int decisionNumber)
596 // Create the message (speed is not of the essence)
598 sprintf(buffer, "exitSubRule %d\n", decisionNumber);
599 transmit(delboy, buffer);
603 enterDecision (pANTLR3_DEBUG_EVENT_LISTENER delboy, int decisionNumber)
607 // Create the message (speed is not of the essence)
609 sprintf(buffer, "enterDecision %d\n", decisionNumber);
610 transmit(delboy, buffer);
615 exitDecision (pANTLR3_DEBUG_EVENT_LISTENER delboy, int decisionNumber)
619 // Create the message (speed is not of the essence)
621 sprintf(buffer, "exitDecision %d\n", decisionNumber);
622 transmit(delboy, buffer);
626 consumeToken (pANTLR3_DEBUG_EVENT_LISTENER delboy, pANTLR3_COMMON_TOKEN t)
630 // Create the serialized token
632 msg = serializeToken(delboy, t);
634 // Insert the debug event indicator
636 msg->insert8(msg, 0, "consumeToken ");
638 msg->addc(msg, '\n');
640 // Transmit the message and wait for ack
642 transmit(delboy, (const char *)(msg->chars));
646 consumeHiddenToken (pANTLR3_DEBUG_EVENT_LISTENER delboy, pANTLR3_COMMON_TOKEN t)
650 // Create the serialized token
652 msg = serializeToken(delboy, t);
654 // Insert the debug event indicator
656 msg->insert8(msg, 0, "consumeHiddenToken ");
658 msg->addc(msg, '\n');
660 // Transmit the message and wait for ack
662 transmit(delboy, (const char *)(msg->chars));
665 // Looking at the next token event.
668 LT (pANTLR3_DEBUG_EVENT_LISTENER delboy, int i, pANTLR3_COMMON_TOKEN t)
674 // Create the serialized token
676 msg = serializeToken(delboy, t);
678 // Insert the index parameter
680 msg->insert8(msg, 0, " ");
681 msg->inserti(msg, 0, i);
683 // Insert the debug event indicator
685 msg->insert8(msg, 0, "LT ");
687 msg->addc(msg, '\n');
689 // Transmit the message and wait for ack
691 transmit(delboy, (const char *)(msg->chars));
696 mark (pANTLR3_DEBUG_EVENT_LISTENER delboy, ANTLR3_MARKER marker)
700 sprintf(buffer, "mark %d\n", (ANTLR3_UINT32)(marker & 0xFFFFFFFF));
702 // Transmit the message and wait for ack
704 transmit(delboy, buffer);
708 rewindMark (pANTLR3_DEBUG_EVENT_LISTENER delboy, ANTLR3_MARKER marker)
712 sprintf(buffer, "rewind %d\n", (ANTLR3_UINT32)(marker & 0xFFFFFFFF));
714 // Transmit the message and wait for ack
716 transmit(delboy, buffer);
721 rewindLast (pANTLR3_DEBUG_EVENT_LISTENER delboy)
723 transmit(delboy, (const char *)"rewind\n");
727 beginBacktrack (pANTLR3_DEBUG_EVENT_LISTENER delboy, int level)
731 sprintf(buffer, "beginBacktrack %d\n", (ANTLR3_UINT32)(level & 0xFFFFFFFF));
733 // Transmit the message and wait for ack
735 transmit(delboy, buffer);
739 endBacktrack (pANTLR3_DEBUG_EVENT_LISTENER delboy, int level, ANTLR3_BOOLEAN successful)
743 sprintf(buffer, "endBacktrack %d %d\n", level, successful);
745 // Transmit the message and wait for ack
747 transmit(delboy, buffer);
751 location (pANTLR3_DEBUG_EVENT_LISTENER delboy, int line, int pos)
755 sprintf(buffer, "location %d %d\n", line, pos);
757 // Transmit the message and wait for ack
759 transmit(delboy, buffer);
763 recognitionException (pANTLR3_DEBUG_EVENT_LISTENER delboy, pANTLR3_EXCEPTION e)
767 sprintf(buffer, "exception %s %d %d %d\n", (char *)(e->name), (ANTLR3_INT32)(e->index), e->line, e->charPositionInLine);
769 // Transmit the message and wait for ack
771 transmit(delboy, buffer);
775 beginResync (pANTLR3_DEBUG_EVENT_LISTENER delboy)
777 transmit(delboy, (const char *)"beginResync\n");
781 endResync (pANTLR3_DEBUG_EVENT_LISTENER delboy)
783 transmit(delboy, (const char *)"endResync\n");
787 semanticPredicate (pANTLR3_DEBUG_EVENT_LISTENER delboy, ANTLR3_BOOLEAN result, const char * predicate)
789 unsigned char * buffer;
792 if (predicate != NULL)
794 buffer = (unsigned char *)ANTLR3_MALLOC(64 + 2*strlen(predicate));
798 out = buffer + sprintf((char *)buffer, "semanticPredicate %s ", result == ANTLR3_TRUE ? "true" : "false");
800 while (*predicate != '\0')
838 // Send it and wait for the ack
840 transmit(delboy, (const char *)buffer);
844 #ifdef ANTLR3_WINDOWS
845 #pragma warning (push)
846 #pragma warning (disable : 4100)
850 commence (pANTLR3_DEBUG_EVENT_LISTENER delboy)
852 // Nothing to see here
856 #ifdef ANTLR3_WINDOWS
857 #pragma warning (pop)
861 terminate (pANTLR3_DEBUG_EVENT_LISTENER delboy)
863 // Terminate sequence
865 sockSend(delboy->socket, "terminate\n", 10); // Send out the command
868 //----------------------------------------------------------------
869 // Tree parsing events
872 consumeNode (pANTLR3_DEBUG_EVENT_LISTENER delboy, pANTLR3_BASE_TREE t)
874 pANTLR3_STRING buffer;
876 buffer = serializeNode (delboy, t);
878 // Now prepend the command
880 buffer->insert8 (buffer, 0, "consumeNode ");
881 buffer->addc (buffer, '\n');
883 // Send to the debugger and wait for the ack
885 transmit (delboy, (const char *)(delboy->tokenString->toUTF8(delboy->tokenString)->chars));
889 LTT (pANTLR3_DEBUG_EVENT_LISTENER delboy, int i, pANTLR3_BASE_TREE t)
891 pANTLR3_STRING buffer;
893 buffer = serializeNode (delboy, t);
895 // Now prepend the command
897 buffer->insert8 (buffer, 0, " ");
898 buffer->inserti (buffer, 0, i);
899 buffer->insert8 (buffer, 0, "LN ");
900 buffer->addc (buffer, '\n');
902 // Send to the debugger and wait for the ack
904 transmit (delboy, (const char *)(delboy->tokenString->toUTF8(delboy->tokenString)->chars));
908 nilNode (pANTLR3_DEBUG_EVENT_LISTENER delboy, pANTLR3_BASE_TREE t)
911 sprintf(buffer, "nilNode %d\n", delboy->adaptor->getUniqueID(delboy->adaptor, t));
912 transmit(delboy, buffer);
916 createNode (pANTLR3_DEBUG_EVENT_LISTENER delboy, pANTLR3_BASE_TREE t)
918 // Do we already have a serialization buffer?
920 if (delboy->tokenString == NULL)
922 // No, so create one, using the string factory that
923 // the grammar name used, which is guaranteed to exist.
924 // 64 bytes will do us here for starters.
926 delboy->tokenString = delboy->grammarFileName->factory->newSize(delboy->grammarFileName->factory, 64);
931 delboy->tokenString->set8(delboy->tokenString, (const char *)"createNodeFromTokenElements ");
933 // Now we serialize the elements of the node.Note that the debugger only
938 delboy->tokenString->addi(delboy->tokenString, delboy->adaptor->getUniqueID(delboy->adaptor, t));
939 delboy->tokenString->addc(delboy->tokenString, ' ');
941 // Type of the current token (which may be imaginary)
943 delboy->tokenString->addi(delboy->tokenString, delboy->adaptor->getType(delboy->adaptor, t));
945 // The text that this node represents
947 serializeText(delboy->tokenString, delboy->adaptor->getText(delboy->adaptor, t));
948 delboy->tokenString->addc(delboy->tokenString, '\n');
950 // Finally, as the debugger is a Java program it will expect to get UTF-8
951 // encoded strings. We don't use UTF-8 internally to the C runtime, so we
952 // must force encode it. We have a method to do this in the string class, but
953 // there is no utf8 string implementation as of yet
955 transmit(delboy, (const char *)(delboy->tokenString->toUTF8(delboy->tokenString)->chars));
959 errorNode (pANTLR3_DEBUG_EVENT_LISTENER delboy, pANTLR3_BASE_TREE t)
961 // Do we already have a serialization buffer?
963 if (delboy->tokenString == NULL)
965 // No, so create one, using the string factory that
966 // the grammar name used, which is guaranteed to exist.
967 // 64 bytes will do us here for starters.
969 delboy->tokenString = delboy->grammarFileName->factory->newSize(delboy->grammarFileName->factory, 64);
974 delboy->tokenString->set8(delboy->tokenString, (const char *)"errorNode ");
976 // Now we serialize the elements of the node.Note that the debugger only
981 delboy->tokenString->addi(delboy->tokenString, delboy->adaptor->getUniqueID(delboy->adaptor, t));
982 delboy->tokenString->addc(delboy->tokenString, ' ');
984 // Type of the current token (which is an error)
986 delboy->tokenString->addi(delboy->tokenString, ANTLR3_TOKEN_INVALID);
988 // The text that this node represents
990 serializeText(delboy->tokenString, delboy->adaptor->getText(delboy->adaptor, t));
991 delboy->tokenString->addc(delboy->tokenString, '\n');
993 // Finally, as the debugger is a Java program it will expect to get UTF-8
994 // encoded strings. We don't use UTF-8 internally to the C runtime, so we
995 // must force encode it. We have a method to do this in the string class, but
996 // there is no utf8 string implementation as of yet
998 transmit(delboy, (const char *)(delboy->tokenString->toUTF8(delboy->tokenString)->chars));
1003 createNodeTok (pANTLR3_DEBUG_EVENT_LISTENER delboy, pANTLR3_BASE_TREE node, pANTLR3_COMMON_TOKEN token)
1007 sprintf(buffer, "createNode %d %d\n", delboy->adaptor->getUniqueID(delboy->adaptor, node), (ANTLR3_UINT32)token->getTokenIndex(token));
1009 transmit(delboy, buffer);
1013 becomeRoot (pANTLR3_DEBUG_EVENT_LISTENER delboy, pANTLR3_BASE_TREE newRoot, pANTLR3_BASE_TREE oldRoot)
1017 sprintf(buffer, "becomeRoot %d %d\n", delboy->adaptor->getUniqueID(delboy->adaptor, newRoot),
1018 delboy->adaptor->getUniqueID(delboy->adaptor, oldRoot)
1020 transmit(delboy, buffer);
1025 addChild (pANTLR3_DEBUG_EVENT_LISTENER delboy, pANTLR3_BASE_TREE root, pANTLR3_BASE_TREE child)
1029 sprintf(buffer, "addChild %d %d\n", delboy->adaptor->getUniqueID(delboy->adaptor, root),
1030 delboy->adaptor->getUniqueID(delboy->adaptor, child)
1032 transmit(delboy, buffer);
1036 setTokenBoundaries (pANTLR3_DEBUG_EVENT_LISTENER delboy, pANTLR3_BASE_TREE t, ANTLR3_MARKER tokenStartIndex, ANTLR3_MARKER tokenStopIndex)
1040 sprintf(buffer, "becomeRoot %d %d %d\n", delboy->adaptor->getUniqueID(delboy->adaptor, t),
1041 (ANTLR3_UINT32)tokenStartIndex,
1042 (ANTLR3_UINT32)tokenStopIndex
1044 transmit(delboy, buffer);