aboutsummaryrefslogtreecommitdiff
path: root/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/DebugEventSocketProxy.cs
blob: 64b0fd22943ade59d39f616bc9ff1fdba4a7e3cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/*
 * [The "BSD licence"]
 * Copyright (c) 2005-2008 Terence Parr
 * All rights reserved.
 *
 * Conversion to C#:
 * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

namespace Antlr.Runtime.Debug
{
    using System;
    using Antlr.Runtime.JavaExtensions;

    using IOException = System.IO.IOException;
    using ITreeAdaptor = Antlr.Runtime.Tree.ITreeAdaptor;
    using Socket = System.Net.Sockets.Socket;
    using StringBuilder = System.Text.StringBuilder;
    using TcpListener = System.Net.Sockets.TcpListener;

    /** <summary>
     *  A proxy debug event listener that forwards events over a socket to
     *  a debugger (or any other listener) using a simple text-based protocol;
     *  one event per line.  ANTLRWorks listens on server socket with a
     *  RemoteDebugEventSocketListener instance.  These two objects must therefore
     *  be kept in sync.  New events must be handled on both sides of socket.
     *  </summary>
     */
    public class DebugEventSocketProxy : BlankDebugEventListener
    {
        public const int DefaultDebuggerPort = 49100;
        protected int port = DefaultDebuggerPort;
        protected TcpListener serverSocket;
        protected Socket socket;
        protected string grammarFileName;
        //protected PrintWriter @out;
        //protected BufferedReader @in;

        /** <summary>Who am i debugging?</summary> */
        protected BaseRecognizer recognizer;

        /** <summary>
         *  Almost certainly the recognizer will have adaptor set, but
         *  we don't know how to cast it (Parser or TreeParser) to get
         *  the adaptor field.  Must be set with a constructor. :(
         *  </summary>
         */
        protected ITreeAdaptor adaptor;

        public DebugEventSocketProxy( BaseRecognizer recognizer, ITreeAdaptor adaptor ) :
            this( recognizer, DefaultDebuggerPort, adaptor )
        {
        }

        public DebugEventSocketProxy( BaseRecognizer recognizer, int port, ITreeAdaptor adaptor )
        {
            this.grammarFileName = recognizer.GrammarFileName;
            this.adaptor = adaptor;
            this.port = port;
        }

        #region Properties
        public virtual ITreeAdaptor TreeAdaptor
        {
            get
            {
                return adaptor;
            }
            set
            {
                adaptor = value;
            }
        }
        #endregion

        public virtual void Handshake()
        {
            if ( serverSocket == null )
            {
                System.Net.IPHostEntry hostInfo = System.Net.Dns.GetHostEntry( "localhost" );
                System.Net.IPAddress ipAddress = hostInfo.AddressList[0];
                serverSocket = new TcpListener( ipAddress, port );
                socket = serverSocket.AcceptSocket();
                socket.NoDelay = true;

                System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
                socket.Send( encoding.GetBytes( "ANTLR " + DebugEventListenerConstants.ProtocolVersion + "\n" ) );
                socket.Send( encoding.GetBytes( "grammar \"" + grammarFileName + "\n" ) );
                Ack();

                //serverSocket = new ServerSocket( port );
                //socket = serverSocket.accept();
                //socket.setTcpNoDelay( true );
                //OutputStream os = socket.getOutputStream();
                //OutputStreamWriter osw = new OutputStreamWriter( os, "UTF8" );
                //@out = new PrintWriter( new BufferedWriter( osw ) );
                //InputStream @is = socket.getInputStream();
                //InputStreamReader isr = new InputStreamReader( @is, "UTF8" );
                //@in = new BufferedReader( isr );
                //@out.println( "ANTLR " + DebugEventListenerConstants.PROTOCOL_VERSION );
                //@out.println( "grammar \"" + grammarFileName );
                //@out.flush();
                //ack();
            }
        }

        public override void Commence()
        {
            // don't bother sending event; listener will trigger upon connection
        }

        public override void Terminate()
        {
            Transmit( "terminate" );
            //@out.close();
            try
            {
                socket.Close();
            }
            catch ( IOException ioe )
            {
                ExceptionExtensions.PrintStackTrace( ioe, Console.Error );
            }
        }

        protected virtual void Ack()
        {
            try
            {
                throw new NotImplementedException();
                //@in.readLine();
            }
            catch ( IOException ioe )
            {
                ExceptionExtensions.PrintStackTrace( ioe, Console.Error );
            }
        }

        protected virtual void Transmit( string @event )
        {
            socket.Send( new System.Text.UTF8Encoding().GetBytes( @event + "\n" ) );
            //@out.println( @event );
            //@out.flush();
            Ack();
        }

        public override void EnterRule( string grammarFileName, string ruleName )
        {
            Transmit( "enterRule\t" + grammarFileName + "\t" + ruleName );
        }

        public override void EnterAlt( int alt )
        {
            Transmit( "enterAlt\t" + alt );
        }

        public override void ExitRule( string grammarFileName, string ruleName )
        {
            Transmit( "exitRule\t" + grammarFileName + "\t" + ruleName );
        }

        public override void EnterSubRule( int decisionNumber )
        {
            Transmit( "enterSubRule\t" + decisionNumber );
        }

        public override void ExitSubRule( int decisionNumber )
        {
            Transmit( "exitSubRule\t" + decisionNumber );
        }

        public override void EnterDecision(int decisionNumber, bool couldBacktrack)
        {
            Transmit( "enterDecision\t" + decisionNumber );
        }

        public override void ExitDecision( int decisionNumber )
        {
            Transmit( "exitDecision\t" + decisionNumber );
        }

        public override void ConsumeToken( IToken t )
        {
            string buf = SerializeToken( t );
            Transmit( "consumeToken\t" + buf );
        }

        public override void ConsumeHiddenToken( IToken t )
        {
            string buf = SerializeToken( t );
            Transmit( "consumeHiddenToken\t" + buf );
        }

        public override void LT( int i, IToken t )
        {
            if ( t != null )
                Transmit( "LT\t" + i + "\t" + SerializeToken( t ) );
        }

        public override void Mark( int i )
        {
            Transmit( "mark\t" + i );
        }

        public override void Rewind( int i )
        {
            Transmit( "rewind\t" + i );
        }

        public override void Rewind()
        {
            Transmit( "rewind" );
        }

        public override void BeginBacktrack( int level )
        {
            Transmit( "beginBacktrack\t" + level );
        }

        public override void EndBacktrack( int level, bool successful )
        {
            Transmit( "endBacktrack\t" + level + "\t" + ( successful ? DebugEventListenerConstants.True : DebugEventListenerConstants.False ) );
        }

        public override void Location( int line, int pos )
        {
            Transmit( "location\t" + line + "\t" + pos );
        }

        public override void RecognitionException( RecognitionException e )
        {
            StringBuilder buf = new StringBuilder( 50 );
            buf.Append( "exception\t" );
            buf.Append( e.GetType().Name );
            // dump only the data common to all exceptions for now
            buf.Append( "\t" );
            buf.Append( e.Index );
            buf.Append( "\t" );
            buf.Append( e.Line );
            buf.Append( "\t" );
            buf.Append( e.CharPositionInLine );
            Transmit( buf.ToString() );
        }

        public override void BeginResync()
        {
            Transmit( "beginResync" );
        }

        public override void EndResync()
        {
            Transmit( "endResync" );
        }

        public override void SemanticPredicate( bool result, string predicate )
        {
            StringBuilder buf = new StringBuilder( 50 );
            buf.Append( "semanticPredicate\t" );
            buf.Append( result );
            SerializeText( buf, predicate );
            Transmit( buf.ToString() );
        }

        #region AST Parsing Events

        public override void ConsumeNode( object t )
        {
            StringBuilder buf = new StringBuilder( 50 );
            buf.Append( "consumeNode" );
            SerializeNode( buf, t );
            Transmit( buf.ToString() );
        }

        public override void LT( int i, object t )
        {
            int ID = adaptor.GetUniqueID( t );
            string text = adaptor.GetText( t );
            int type = adaptor.GetType( t );
            StringBuilder buf = new StringBuilder( 50 );
            buf.Append( "LN\t" ); // lookahead node; distinguish from LT in protocol
            buf.Append( i );
            SerializeNode( buf, t );
            Transmit( buf.ToString() );
        }

        protected virtual void SerializeNode( StringBuilder buf, object t )
        {
            int ID = adaptor.GetUniqueID( t );
            string text = adaptor.GetText( t );
            int type = adaptor.GetType( t );
            buf.Append( "\t" );
            buf.Append( ID );
            buf.Append( "\t" );
            buf.Append( type );
            IToken token = adaptor.GetToken( t );
            int line = -1;
            int pos = -1;
            if ( token != null )
            {
                line = token.Line;
                pos = token.CharPositionInLine;
            }
            buf.Append( "\t" );
            buf.Append( line );
            buf.Append( "\t" );
            buf.Append( pos );
            int tokenIndex = adaptor.GetTokenStartIndex( t );
            buf.Append( "\t" );
            buf.Append( tokenIndex );
            SerializeText( buf, text );
        }

        #endregion


        #region AST Events

        public override void NilNode( object t )
        {
            int ID = adaptor.GetUniqueID( t );
            Transmit( "nilNode\t" + ID );
        }

        public override void ErrorNode( object t )
        {
            int ID = adaptor.GetUniqueID( t );
            string text = t.ToString();
            StringBuilder buf = new StringBuilder( 50 );
            buf.Append( "errorNode\t" );
            buf.Append( ID );
            buf.Append( "\t" );
            buf.Append( TokenTypes.Invalid );
            SerializeText( buf, text );
            Transmit( buf.ToString() );
        }

        public override void CreateNode( object t )
        {
            int ID = adaptor.GetUniqueID( t );
            string text = adaptor.GetText( t );
            int type = adaptor.GetType( t );
            StringBuilder buf = new StringBuilder( 50 );
            buf.Append( "createNodeFromTokenElements\t" );
            buf.Append( ID );
            buf.Append( "\t" );
            buf.Append( type );
            SerializeText( buf, text );
            Transmit( buf.ToString() );
        }

        public override void CreateNode( object node, IToken token )
        {
            int ID = adaptor.GetUniqueID( node );
            int tokenIndex = token.TokenIndex;
            Transmit( "createNode\t" + ID + "\t" + tokenIndex );
        }

        public override void BecomeRoot( object newRoot, object oldRoot )
        {
            int newRootID = adaptor.GetUniqueID( newRoot );
            int oldRootID = adaptor.GetUniqueID( oldRoot );
            Transmit( "becomeRoot\t" + newRootID + "\t" + oldRootID );
        }

        public override void AddChild( object root, object child )
        {
            int rootID = adaptor.GetUniqueID( root );
            int childID = adaptor.GetUniqueID( child );
            Transmit( "addChild\t" + rootID + "\t" + childID );
        }

        public override void SetTokenBoundaries( object t, int tokenStartIndex, int tokenStopIndex )
        {
            int ID = adaptor.GetUniqueID( t );
            Transmit( "setTokenBoundaries\t" + ID + "\t" + tokenStartIndex + "\t" + tokenStopIndex );
        }

        #endregion


        #region Support

        protected virtual string SerializeToken( IToken t )
        {
            StringBuilder buf = new StringBuilder( 50 );
            buf.Append( t.TokenIndex );
            buf.Append( '\t' );
            buf.Append( t.Type );
            buf.Append( '\t' );
            buf.Append( t.Channel );
            buf.Append( '\t' );
            buf.Append( t.Line );
            buf.Append( '\t' );
            buf.Append( t.CharPositionInLine );
            SerializeText( buf, t.Text );
            return buf.ToString();
        }

        protected virtual void SerializeText( StringBuilder buf, string text )
        {
            buf.Append( "\t\"" );
            if ( text == null )
            {
                text = "";
            }
            // escape \n and \r all text for token appears to exist on one line
            // this escape is slow but easy to understand
            text = EscapeNewlines( text );
            buf.Append( text );
        }

        protected virtual string EscapeNewlines( string txt )
        {
            txt = txt.Replace( "%", "%25" );   // escape all escape char ;)
            txt = txt.Replace( "\n", "%0A" );  // escape \n
            txt = txt.Replace( "\r", "%0D" );  // escape \r
            return txt;
        }

        #endregion
    }
}