aboutsummaryrefslogtreecommitdiff
path: root/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/RemoteDebugEventSocketListener.cs
blob: e390065fa7e99a2dc0a0f3ab3c74b12980445c8a (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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
/*
 * [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 Antlr.Runtime.JavaExtensions;

    using Array = System.Array;
    using BaseTree = Antlr.Runtime.Tree.BaseTree;
    using Console = System.Console;
    using Exception = System.Exception;
    using IOException = System.IO.IOException;
    using ITree = Antlr.Runtime.Tree.ITree;
    using Math = System.Math;
    using Socket = System.Net.Sockets.Socket;
    using SocketException = System.Net.Sockets.SocketException;
    using TextReader = System.IO.TextReader;
    using TextWriter = System.IO.TextWriter;

    public class RemoteDebugEventSocketListener
    {
        const int MAX_EVENT_ELEMENTS = 8;
        IDebugEventListener listener;
        string machine;
        int port;
        Socket channel = null;
        TextWriter @out;
        TextReader @in;
        string @event;
        /** <summary>Version of ANTLR (dictates events)</summary> */
        public string version;
        public string grammarFileName;
        /** <summary>
         *  Track the last token index we saw during a consume.  If same, then
         *  set a flag that we have a problem.
         *  </summary>
         */
        int previousTokenIndex = -1;
        bool tokenIndexesInvalid = false;

        public class ProxyToken : IToken
        {
            int index;
            int type;
            int channel;
            int line;
            int charPos;
            string text;
            public ProxyToken( int index )
            {
                this.index = index;
            }
            public ProxyToken( int index, int type, int channel,
                              int line, int charPos, string text )
            {
                this.index = index;
                this.type = type;
                this.channel = channel;
                this.line = line;
                this.charPos = charPos;
                this.text = text;
            }

            #region IToken Members
            public string Text
            {
                get
                {
                    return text;
                }
                set
                {
                    text = value;
                }
            }

            public int Type
            {
                get
                {
                    return type;
                }
                set
                {
                    type = value;
                }
            }

            public int Line
            {
                get
                {
                    return line;
                }
                set
                {
                    line = value;
                }
            }

            public int CharPositionInLine
            {
                get
                {
                    return charPos;
                }
                set
                {
                    charPos = value;
                }
            }

            public int Channel
            {
                get
                {
                    return channel;
                }
                set
                {
                    channel = value;
                }
            }

            public int StartIndex
            {
                get
                {
                    return -1;
                }
                set
                {
                }
            }

            public int StopIndex
            {
                get
                {
                    return -1;
                }
                set
                {
                }
            }

            public int TokenIndex
            {
                get
                {
                    return index;
                }
                set
                {
                    index = value;
                }
            }

            public ICharStream InputStream
            {
                get
                {
                    return null;
                }
                set
                {
                }
            }

            #endregion

            public override string ToString()
            {
                string channelStr = "";
                if ( channel != TokenChannels.Default )
                {
                    channelStr = ",channel=" + channel;
                }
                return "[" + Text + "/<" + type + ">" + channelStr + "," + line + ":" + CharPositionInLine + ",@" + index + "]";
            }
        }

        public class ProxyTree : BaseTree
        {
            public int ID;
            int type;
            int line = 0;
            public int charPos = -1;
            public int tokenIndex = -1;
            string text;

            public ProxyTree( int ID, int type, int line, int charPos, int tokenIndex, string text )
            {
                this.ID = ID;
                this.type = type;
                this.line = line;
                this.charPos = charPos;
                this.tokenIndex = tokenIndex;
                this.text = text;
            }

            public ProxyTree( int ID )
            {
                this.ID = ID;
            }

            #region Properties
            public override string Text
            {
                get
                {
                    return text;
                }
                set
                {
                }
            }
            public override int TokenStartIndex
            {
                get
                {
                    return tokenIndex;
                }
                set
                {
                }
            }
            public override int TokenStopIndex
            {
                get
                {
                    return 0;
                }
                set
                {
                }
            }
            public override int Type
            {
                get
                {
                    return type;
                }
                set
                {
                }
            }
            #endregion

            public override ITree DupNode()
            {
                return null;
            }

            public override string ToString()
            {
                return "fix this";
            }
        }

        public RemoteDebugEventSocketListener( IDebugEventListener listener,
                                              string machine,
                                              int port )
        {
            this.listener = listener;
            this.machine = machine;
            this.port = port;

            if ( !OpenConnection() )
            {
                throw new SocketException();
            }
        }

        protected virtual void EventHandler()
        {
            try
            {
                Handshake();
                @event = @in.ReadLine();
                while ( @event != null )
                {
                    Dispatch( @event );
                    Ack();
                    @event = @in.ReadLine();
                }
            }
            catch ( Exception e )
            {
                Console.Error.WriteLine( e );
                ExceptionExtensions.PrintStackTrace( e, Console.Error );
            }
            finally
            {
                CloseConnection();
            }
        }

        protected virtual bool OpenConnection()
        {
            bool success = false;
            try
            {
                throw new System.NotImplementedException();
                //channel = new Socket( machine, port );
                //channel.setTcpNoDelay( true );
                //OutputStream os = channel.getOutputStream();
                //OutputStreamWriter osw = new OutputStreamWriter( os, "UTF8" );
                //@out = new PrintWriter( new BufferedWriter( osw ) );
                //InputStream @is = channel.getInputStream();
                //InputStreamReader isr = new InputStreamReader( @is, "UTF8" );
                //@in = new BufferedReader( isr );
                //success = true;
            }
            catch ( Exception e )
            {
                Console.Error.WriteLine( e );
            }
            return success;
        }

        protected virtual void CloseConnection()
        {
            try
            {
                @in.Close();
                @in = null;
                @out.Close();
                @out = null;
                channel.Close();
                channel = null;
            }
            catch ( Exception e )
            {
                Console.Error.WriteLine( e );
                ExceptionExtensions.PrintStackTrace( e, Console.Error );
            }
            finally
            {
                if ( @in != null )
                {
                    try
                    {
                        @in.Close();
                    }
                    catch ( IOException ioe )
                    {
                        Console.Error.WriteLine( ioe );
                    }
                }
                if ( @out != null )
                {
                    @out.Close();
                }
                if ( channel != null )
                {
                    try
                    {
                        channel.Close();
                    }
                    catch ( IOException ioe )
                    {
                        Console.Error.WriteLine( ioe );
                    }
                }
            }

        }

        protected virtual void Handshake()
        {
            string antlrLine = @in.ReadLine();
            string[] antlrElements = GetEventElements( antlrLine );
            version = antlrElements[1];
            string grammarLine = @in.ReadLine();
            string[] grammarElements = GetEventElements( grammarLine );
            grammarFileName = grammarElements[1];
            Ack();
            listener.Commence(); // inform listener after handshake
        }

        protected virtual void Ack()
        {
            @out.WriteLine( "ack" );
            @out.Flush();
        }

        protected virtual void Dispatch( string line )
        {
            //JSystem.@out.println( "event: " + line );
            string[] elements = GetEventElements( line );
            if ( elements == null || elements[0] == null )
            {
                Console.Error.WriteLine( "unknown debug event: " + line );
                return;
            }
            if ( elements[0].Equals( "enterRule" ) )
            {
                listener.EnterRule( elements[1], elements[2] );
            }
            else if ( elements[0].Equals( "exitRule" ) )
            {
                listener.ExitRule( elements[1], elements[2] );
            }
            else if ( elements[0].Equals( "enterAlt" ) )
            {
                listener.EnterAlt( int.Parse( elements[1] ) );
            }
            else if ( elements[0].Equals( "enterSubRule" ) )
            {
                listener.EnterSubRule( int.Parse( elements[1] ) );
            }
            else if ( elements[0].Equals( "exitSubRule" ) )
            {
                listener.ExitSubRule( int.Parse( elements[1] ) );
            }
            else if ( elements[0].Equals( "enterDecision" ) )
            {
                listener.EnterDecision(int.Parse(elements[1]), elements[2].Equals("true"));
            }
            else if ( elements[0].Equals( "exitDecision" ) )
            {
                listener.ExitDecision( int.Parse( elements[1] ) );
            }
            else if ( elements[0].Equals( "location" ) )
            {
                listener.Location( int.Parse( elements[1] ),
                                  int.Parse( elements[2] ) );
            }
            else if ( elements[0].Equals( "consumeToken" ) )
            {
                ProxyToken t = DeserializeToken( elements, 1 );
                if ( t.TokenIndex == previousTokenIndex )
                {
                    tokenIndexesInvalid = true;
                }
                previousTokenIndex = t.TokenIndex;
                listener.ConsumeToken( t );
            }
            else if ( elements[0].Equals( "consumeHiddenToken" ) )
            {
                ProxyToken t = DeserializeToken( elements, 1 );
                if ( t.TokenIndex == previousTokenIndex )
                {
                    tokenIndexesInvalid = true;
                }
                previousTokenIndex = t.TokenIndex;
                listener.ConsumeHiddenToken( t );
            }
            else if ( elements[0].Equals( "LT" ) )
            {
                IToken t = DeserializeToken( elements, 2 );
                listener.LT( int.Parse( elements[1] ), t );
            }
            else if ( elements[0].Equals( "mark" ) )
            {
                listener.Mark( int.Parse( elements[1] ) );
            }
            else if ( elements[0].Equals( "rewind" ) )
            {
                if ( elements[1] != null )
                {
                    listener.Rewind( int.Parse( elements[1] ) );
                }
                else
                {
                    listener.Rewind();
                }
            }
            else if ( elements[0].Equals( "beginBacktrack" ) )
            {
                listener.BeginBacktrack( int.Parse( elements[1] ) );
            }
            else if ( elements[0].Equals( "endBacktrack" ) )
            {
                int level = int.Parse( elements[1] );
                int successI = int.Parse( elements[2] );
                listener.EndBacktrack( level, successI == DebugEventListenerConstants.True );
            }
            else if ( elements[0].Equals( "exception" ) )
            {
#if true
                throw new System.NotImplementedException();
#else
                string excName = elements[1];
                string indexS = elements[2];
                string lineS = elements[3];
                string posS = elements[4];
                Class excClass = null;
                try
                {
                    excClass = Class.forName( excName );
                    RecognitionException e =
                        (RecognitionException)excClass.newInstance();
                    e.index = int.Parse( indexS );
                    e.line = int.Parse( lineS );
                    e.charPositionInLine = int.Parse( posS );
                    listener.recognitionException( e );
                }
                catch ( ClassNotFoundException cnfe )
                {
                    Console.Error.println( "can't find class " + cnfe );
                    cnfe.printStackTrace( Console.Error );
                }
                catch ( InstantiationException ie )
                {
                    Console.Error.println( "can't instantiate class " + ie );
                    ie.printStackTrace( Console.Error );
                }
                catch ( IllegalAccessException iae )
                {
                    Console.Error.println( "can't access class " + iae );
                    iae.printStackTrace( Console.Error );
                }
#endif
            }
            else if ( elements[0].Equals( "beginResync" ) )
            {
                listener.BeginResync();
            }
            else if ( elements[0].Equals( "endResync" ) )
            {
                listener.EndResync();
            }
            else if ( elements[0].Equals( "terminate" ) )
            {
                listener.Terminate();
            }
            else if ( elements[0].Equals( "semanticPredicate" ) )
            {
                bool result = bool.Parse( elements[1] );
                string predicateText = elements[2];
                predicateText = UnEscapeNewlines( predicateText );
                listener.SemanticPredicate( result,
                                           predicateText );
            }
            else if ( elements[0].Equals( "consumeNode" ) )
            {
                ProxyTree node = DeserializeNode( elements, 1 );
                listener.ConsumeNode( node );
            }
            else if ( elements[0].Equals( "LN" ) )
            {
                int i = int.Parse( elements[1] );
                ProxyTree node = DeserializeNode( elements, 2 );
                listener.LT( i, node );
            }
            else if ( elements[0].Equals( "createNodeFromTokenElements" ) )
            {
                int ID = int.Parse( elements[1] );
                int type = int.Parse( elements[2] );
                string text = elements[3];
                text = UnEscapeNewlines( text );
                ProxyTree node = new ProxyTree( ID, type, -1, -1, -1, text );
                listener.CreateNode( node );
            }
            else if ( elements[0].Equals( "createNode" ) )
            {
                int ID = int.Parse( elements[1] );
                int tokenIndex = int.Parse( elements[2] );
                // create dummy node/token filled with ID, tokenIndex
                ProxyTree node = new ProxyTree( ID );
                ProxyToken token = new ProxyToken( tokenIndex );
                listener.CreateNode( node, token );
            }
            else if ( elements[0].Equals( "nilNode" ) )
            {
                int ID = int.Parse( elements[1] );
                ProxyTree node = new ProxyTree( ID );
                listener.NilNode( node );
            }
            else if ( elements[0].Equals( "errorNode" ) )
            {
                // TODO: do we need a special tree here?
                int ID = int.Parse( elements[1] );
                int type = int.Parse( elements[2] );
                string text = elements[3];
                text = UnEscapeNewlines( text );
                ProxyTree node = new ProxyTree( ID, type, -1, -1, -1, text );
                listener.ErrorNode( node );
            }
            else if ( elements[0].Equals( "becomeRoot" ) )
            {
                int newRootID = int.Parse( elements[1] );
                int oldRootID = int.Parse( elements[2] );
                ProxyTree newRoot = new ProxyTree( newRootID );
                ProxyTree oldRoot = new ProxyTree( oldRootID );
                listener.BecomeRoot( newRoot, oldRoot );
            }
            else if ( elements[0].Equals( "addChild" ) )
            {
                int rootID = int.Parse( elements[1] );
                int childID = int.Parse( elements[2] );
                ProxyTree root = new ProxyTree( rootID );
                ProxyTree child = new ProxyTree( childID );
                listener.AddChild( root, child );
            }
            else if ( elements[0].Equals( "setTokenBoundaries" ) )
            {
                int ID = int.Parse( elements[1] );
                ProxyTree node = new ProxyTree( ID );
                listener.SetTokenBoundaries(
                    node,
                    int.Parse( elements[2] ),
                    int.Parse( elements[3] ) );
            }
            else
            {
                Console.Error.WriteLine( "unknown debug event: " + line );
            }
        }

        protected virtual ProxyTree DeserializeNode( string[] elements, int offset )
        {
            int ID = int.Parse( elements[offset + 0] );
            int type = int.Parse( elements[offset + 1] );
            int tokenLine = int.Parse( elements[offset + 2] );
            int charPositionInLine = int.Parse( elements[offset + 3] );
            int tokenIndex = int.Parse( elements[offset + 4] );
            string text = elements[offset + 5];
            text = UnEscapeNewlines( text );
            return new ProxyTree( ID, type, tokenLine, charPositionInLine, tokenIndex, text );
        }

        protected virtual ProxyToken DeserializeToken( string[] elements,
                                              int offset )
        {
            string indexS = elements[offset + 0];
            string typeS = elements[offset + 1];
            string channelS = elements[offset + 2];
            string lineS = elements[offset + 3];
            string posS = elements[offset + 4];
            string text = elements[offset + 5];
            text = UnEscapeNewlines( text );
            int index = int.Parse( indexS );
            ProxyToken t =
                new ProxyToken( index,
                               int.Parse( typeS ),
                               int.Parse( channelS ),
                               int.Parse( lineS ),
                               int.Parse( posS ),
                               text );
            return t;
        }

        /** <summary>Create a thread to listen to the remote running recognizer</summary> */
        public virtual void Start()
        {
            System.Threading.Thread t = new System.Threading.Thread( Run );
            t.Start();
        }

        public virtual void Run()
        {
            EventHandler();
        }

        #region Misc

        public virtual string[] GetEventElements( string @event )
        {
            if ( @event == null )
            {
                return null;
            }
            string[] elements = new string[MAX_EVENT_ELEMENTS];
            string str = null; // a string element if present (must be last)
            try
            {
                int firstQuoteIndex = @event.IndexOf( '"' );
                if ( firstQuoteIndex >= 0 )
                {
                    // treat specially; has a string argument like "a comment\n
                    // Note that the string is terminated by \n not end quote.
                    // Easier to parse that way.
                    string eventWithoutString = @event.Substring( 0, firstQuoteIndex );
                    str = @event.Substring( firstQuoteIndex + 1 );
                    @event = eventWithoutString;
                }

                string[] tokens = @event.Split('\t');
                Array.Copy(tokens, elements, Math.Min(tokens.Length, MAX_EVENT_ELEMENTS));
                if (tokens.Length >= MAX_EVENT_ELEMENTS)
                    return elements;

                if ( str != null )
                {
                    elements[tokens.Length] = str;
                }
            }
            catch ( Exception e )
            {
                ExceptionExtensions.PrintStackTrace( e, Console.Error );
            }
            return elements;
        }

        protected virtual string UnEscapeNewlines( string txt )
        {
            // this unescape is slow but easy to understand
            txt = txt.Replace( "%0A", "\n" );  // unescape \n
            txt = txt.Replace( "%0D", "\r" );  // unescape \r
            txt = txt.Replace( "%25", "%" );   // undo escaped escape chars
            return txt;
        }

        public virtual bool TokenIndexesAreInvalid()
        {
            return tokenIndexesInvalid;
        }

        #endregion

    }
}