source: trunk/yao/share/antlr-2.7.7/lib/csharp/antlr.runtime/antlr/BaseAST.cs @ 1

Last change on this file since 1 was 1, checked in by lnalod, 15 years ago

Initial import of YAO sources

File size: 13.8 KB
Line 
1using System;
2using StringBuilder             = System.Text.StringBuilder;
3using ISerializable             = System.Runtime.Serialization.ISerializable;
4using TextWriter                = System.IO.TextWriter;
5using ArrayList                 = System.Collections.ArrayList;
6using IEnumerator               = System.Collections.IEnumerator;
7
8using AST                               = antlr.collections.AST;
9
10namespace antlr
11{
12        /*ANTLR Translator Generator
13        * Project led by Terence Parr at http://www.jGuru.com
14        * Software rights: http://www.antlr.org/license.html
15        *
16        * $Id:$
17        */
18       
19        //
20        // ANTLR C# Code Generator by Micheal Jordan
21        //                            Kunle Odutola       : kunle UNDERSCORE odutola AT hotmail DOT com
22        //                            Anthony Oguntimehin
23        //
24        // With many thanks to Eric V. Smith from the ANTLR list.
25        //
26
27        /*
28        * A Child-Sibling Tree.
29        *
30        * A tree with PLUS at the root and with two children 3 and 4 is
31        * structured as:
32        *
33        *               PLUS
34        *                 |
35        *                 3 -- 4
36        *
37        * and can be specified easily in LISP notation as
38        *
39        * (PLUS 3 4)
40        *
41        * where every '(' starts a new subtree.
42        *
43        * These trees are particular useful for translators because of
44        * the flexibility of the children lists.  They are also very easy
45        * to walk automatically, whereas trees with specific children
46        * reference fields can't easily be walked automatically.
47        *
48        * This class contains the basic support for an AST.
49        * Most people will create ASTs that are subclasses of
50        * BaseAST or of CommonAST.
51        */
52        [Serializable()] 
53        public abstract class BaseAST : AST
54        {
55                protected internal BaseAST down;
56                protected internal BaseAST right;
57               
58                private static bool verboseStringConversion = false;
59                private static string[] tokenNames = null;
60
61                /*Add a node to the end of the child list for this node */
62                public virtual void  addChild(AST node)
63                {
64                        if (node == null)
65                                return ;
66                        BaseAST t = this.down;
67                        if (t != null)
68                        {
69                                while (t.right != null)
70                                {
71                                        t = t.right;
72                                }
73                                t.right = (BaseAST) node;
74                        }
75                        else
76                        {
77                                this.down = (BaseAST) node;
78                        }
79                }
80               
81                private void  doWorkForFindAll(ArrayList v, AST target, bool partialMatch)
82                {
83                        AST sibling;
84                       
85                        // Start walking sibling lists, looking for matches.
86//siblingWalk:
87                         for (sibling = this; sibling != null; sibling = sibling.getNextSibling())
88                        {
89                                if ((partialMatch && sibling.EqualsTreePartial(target)) || (!partialMatch && sibling.EqualsTree(target)))
90                                {
91                                        v.Add(sibling);
92                                }
93                                // regardless of match or not, check any children for matches
94                                if (sibling.getFirstChild() != null)
95                                {
96                                        ((BaseAST) sibling.getFirstChild()).doWorkForFindAll(v, target, partialMatch);
97                                }
98                        }
99                }
100               
101                public override bool Equals(object obj) 
102                {     
103                        if (obj == null) 
104                                return false;                           
105                        if (this.GetType() != obj.GetType()) 
106                                return false;                           
107                        return Equals((AST)obj);       
108                }   
109               
110                /*Is node t equal to this in terms of token type and text? */
111                public virtual bool Equals(AST t)
112                {
113                        if (t == null)
114                                return false;
115
116                        return  (Object.Equals(this.getText(), t.getText())) && 
117                                        (this.Type == t.Type);
118                }
119               
120                /*Is t an exact structural and equals() match of this tree.  The
121                *  'this' reference is considered the start of a sibling list.
122                */
123                public virtual bool EqualsList(AST t)
124                {
125                        AST sibling;
126                       
127                        // the empty tree is not a match of any non-null tree.
128                        if (t == null)
129                        {
130                                return false;
131                        }
132                       
133                        // Otherwise, start walking sibling lists.  First mismatch, return false.
134                         for (sibling = this; sibling != null && t != null; sibling = sibling.getNextSibling(), t = t.getNextSibling())
135                        {
136                                // as a quick optimization, check roots first.
137                                if (!sibling.Equals(t))
138                                {
139                                        return false;
140                                }
141                                // if roots match, do full list match test on children.
142                                if (sibling.getFirstChild() != null)
143                                {
144                                        if (!sibling.getFirstChild().EqualsList(t.getFirstChild()))
145                                        {
146                                                return false;
147                                        }
148                                }
149                                else if (t.getFirstChild() != null)
150                                {
151                                        return false;
152                                }
153                        }
154                        if (sibling == null && t == null)
155                        {
156                                return true;
157                        }
158                        // one sibling list has more than the other
159                        return false;
160                }
161               
162                /*Is 'sub' a subtree of this list?
163                *  The siblings of the root are NOT ignored.
164                */
165                public virtual bool EqualsListPartial(AST sub)
166                {
167                        AST sibling;
168                       
169                        // the empty tree is always a subset of any tree.
170                        if (sub == null)
171                        {
172                                return true;
173                        }
174                       
175                        // Otherwise, start walking sibling lists.  First mismatch, return false.
176                         for (sibling = this; sibling != null && sub != null; sibling = sibling.getNextSibling(), sub = sub.getNextSibling())
177                        {
178                                // as a quick optimization, check roots first.
179                                if (!sibling.Equals(sub))
180                                        return false;
181                                // if roots match, do partial list match test on children.
182                                if (sibling.getFirstChild() != null)
183                                {
184                                        if (!sibling.getFirstChild().EqualsListPartial(sub.getFirstChild()))
185                                                return false;
186                                }
187                        }
188                        if (sibling == null && sub != null)
189                        {
190                                // nothing left to match in this tree, but subtree has more
191                                return false;
192                        }
193                        // either both are null or sibling has more, but subtree doesn't
194                        return true;
195                }
196               
197                /*Is tree rooted at 'this' equal to 't'?  The siblings
198                *  of 'this' are ignored.
199                */
200                public virtual bool EqualsTree(AST t)
201                {
202                        // check roots first.
203                        if (!this.Equals(t))
204                                return false;
205                        // if roots match, do full list match test on children.
206                        if (this.getFirstChild() != null)
207                        {
208                                if (!this.getFirstChild().EqualsList(t.getFirstChild()))
209                                        return false;
210                        }
211                        else if (t.getFirstChild() != null)
212                        {
213                                return false;
214                        }
215                        return true;
216                }
217               
218                /*Is 't' a subtree of the tree rooted at 'this'?  The siblings
219                *  of 'this' are ignored.
220                */
221                public virtual bool EqualsTreePartial(AST sub)
222                {
223                        // the empty tree is always a subset of any tree.
224                        if (sub == null)
225                        {
226                                return true;
227                        }
228                       
229                        // check roots first.
230                        if (!this.Equals(sub))
231                                return false;
232                        // if roots match, do full list partial match test on children.
233                        if (this.getFirstChild() != null)
234                        {
235                                if (!this.getFirstChild().EqualsListPartial(sub.getFirstChild()))
236                                        return false;
237                        }
238                        return true;
239                }
240               
241                /*Walk the tree looking for all exact subtree matches.  Return
242                *  an IEnumerator that lets the caller walk the list
243                *  of subtree roots found herein.
244                */
245                public virtual IEnumerator findAll(AST target)
246                {
247                        ArrayList roots = new ArrayList(10);
248                        //AST sibling;
249                       
250                        // the empty tree cannot result in an enumeration
251                        if (target == null)
252                        {
253                                return null;
254                        }
255                       
256                        doWorkForFindAll(roots, target, false); // find all matches recursively
257                       
258                        return roots.GetEnumerator();
259                }
260               
261                /*Walk the tree looking for all subtrees.  Return
262                *  an IEnumerator that lets the caller walk the list
263                *  of subtree roots found herein.
264                */
265                public virtual IEnumerator findAllPartial(AST sub)
266                {
267                        ArrayList roots = new ArrayList(10);
268                        //AST sibling;
269                       
270                        // the empty tree cannot result in an enumeration
271                        if (sub == null)
272                        {
273                                return null;
274                        }
275                       
276                        doWorkForFindAll(roots, sub, true); // find all matches recursively
277                       
278                        return roots.GetEnumerator();
279                }
280               
281                /*Get the first child of this node; null if not children */
282                public virtual AST getFirstChild()
283                {
284                        return down;
285                }
286               
287                /*Get the next sibling in line after this one */
288                public virtual AST getNextSibling()
289                {
290                        return right;
291                }
292               
293                /*Get the token text for this node */
294                public virtual string getText()
295                {
296                        return "";
297                }
298               
299                /*Get the token type for this node */
300                public virtual int Type
301                {
302                        get { return 0; }
303                        set { ; }
304                }
305               
306                /// <summary>
307                /// Get number of children of this node; if leaf, returns 0
308                /// </summary>
309                /// <returns>Number of children</returns>
310                public int getNumberOfChildren() 
311                {
312                        BaseAST t = this.down;
313                        int n = 0;
314                        if (t != null) 
315                        {
316                                n = 1;
317                                while (t.right != null) 
318                                {
319                                        t = t.right;
320                                        n++;
321                                }
322                        }
323                        return n;
324                }
325
326                public abstract void  initialize(int t, string txt);
327               
328                public abstract void  initialize(AST t);
329               
330                public abstract void  initialize(IToken t);
331               
332                /*Remove all children */
333                public virtual void  removeChildren()
334                {
335                        down = null;
336                }
337               
338                public virtual void  setFirstChild(AST c)
339                {
340                        down = (BaseAST) c;
341                }
342               
343                public virtual void  setNextSibling(AST n)
344                {
345                        right = (BaseAST) n;
346                }
347               
348                /*Set the token text for this node */
349                public virtual void  setText(string text)
350                {
351                        ;
352                }
353               
354                /*Set the token type for this node */
355                public virtual void  setType(int ttype)
356                {
357                        this.Type = ttype;
358                }
359               
360                public static void  setVerboseStringConversion(bool verbose, string[] names)
361                {
362                        verboseStringConversion = verbose;
363                        tokenNames = names;
364                }
365               
366                override public string ToString()
367                {
368                        StringBuilder b = new StringBuilder();
369                        // if verbose and type name not same as text (keyword probably)
370                        if (verboseStringConversion && 
371                                        (0 != String.Compare(getText(), (tokenNames[Type]), true)) &&
372                                        (0 != String.Compare(getText(), StringUtils.stripFrontBack(tokenNames[Type], @"""", @""""), true)))
373                        {
374                                b.Append('[');
375                                b.Append(getText());
376                                b.Append(",<");
377                                b.Append(tokenNames[Type]);
378                                b.Append(">]");
379                                return b.ToString();
380                        }
381                        return getText();
382                }
383               
384                /*Print out a child-sibling tree in LISP notation */
385                public virtual string ToStringList()
386                {
387                        AST t = this;
388                        string ts = "";
389                        if (t.getFirstChild() != null)
390                                ts += " (";
391                        ts += " " + this.ToString();
392                        if (t.getFirstChild() != null)
393                        {
394                                ts += ((BaseAST) t.getFirstChild()).ToStringList();
395                        }
396                        if (t.getFirstChild() != null)
397                                ts += " )";
398                        if (t.getNextSibling() != null)
399                        {
400                                ts += ((BaseAST) t.getNextSibling()).ToStringList();
401                        }
402                        return ts;
403                }
404               
405                public virtual string ToStringTree() 
406                {
407                        AST t = this;
408                        string ts = "";
409                        if (t.getFirstChild() != null) 
410                        {
411                                ts += " (";
412                        }
413                        ts += " " + this.ToString();
414                        if (t.getFirstChild() != null)
415                        {
416                                ts += ((BaseAST) t.getFirstChild()).ToStringList();
417                        }
418                        if (t.getFirstChild() != null)
419                        {
420                                ts += " )";
421                        }
422                        return ts;
423                 }
424
425                public virtual string ToTree()
426                {
427                        return ToTree(string.Empty);
428                }
429               
430                public virtual string ToTree(string prefix) 
431                {
432                        StringBuilder sb = new StringBuilder(prefix);
433               
434                        // Replace vertical bar if there is no next sibling.
435                        if ( (getNextSibling() == null) )
436                                sb.Append("+--");
437                        else
438                                sb.Append("|--");
439               
440                        sb.Append( ToString() );
441                        sb.Append( Environment.NewLine );
442
443                        if ( getFirstChild() != null ) 
444                        {
445                                // Replace vertical bar if there is no next sibling.
446                                if ( getNextSibling() == null )
447                                        sb.Append( ((BaseAST) getFirstChild()).ToTree(prefix + "   ") );
448                                else
449                                        sb.Append( ((BaseAST) getFirstChild()).ToTree(prefix + "|  ") );
450                        }
451
452                        if ( getNextSibling() != null )
453                                sb.Append( ((BaseAST) getNextSibling()).ToTree(prefix) );
454
455                        return sb.ToString();
456                }
457
458                public static string decode(string text)
459                {
460                        char c, c1, c2, c3, c4, c5;
461                        StringBuilder n = new StringBuilder();
462                         for (int i = 0; i < text.Length; i++)
463                        {
464                                c = text[i];
465                                if (c == '&')
466                                {
467                                        c1 = text[i + 1];
468                                        c2 = text[i + 2];
469                                        c3 = text[i + 3];
470                                        c4 = text[i + 4];
471                                        c5 = text[i + 5];
472                                       
473                                        if (c1 == 'a' && c2 == 'm' && c3 == 'p' && c4 == ';')
474                                        {
475                                                n.Append("&");
476                                                i += 5;
477                                        }
478                                        else if (c1 == 'l' && c2 == 't' && c3 == ';')
479                                        {
480                                                n.Append("<");
481                                                i += 4;
482                                        }
483                                        else if (c1 == 'g' && c2 == 't' && c3 == ';')
484                                        {
485                                                n.Append(">");
486                                                i += 4;
487                                        }
488                                        else if (c1 == 'q' && c2 == 'u' && c3 == 'o' && c4 == 't' && c5 == ';')
489                                        {
490                                                n.Append("\"");
491                                                i += 6;
492                                        }
493                                        else if (c1 == 'a' && c2 == 'p' && c3 == 'o' && c4 == 's' && c5 == ';')
494                                        {
495                                                n.Append("'");
496                                                i += 6;
497                                        }
498                                        else
499                                                n.Append("&");
500                                }
501                                else
502                                        n.Append(c);
503                        }
504                        return n.ToString();
505                }
506               
507                public static string encode(string text)
508                {
509                        char c;
510                        StringBuilder n = new StringBuilder();
511                         for (int i = 0; i < text.Length; i++)
512                        {
513                                c = text[i];
514                                switch (c)
515                                {
516                                        case '&': 
517                                        {
518                                                n.Append("&amp;");
519                                                break;
520                                        }
521                                       
522                                        case '<': 
523                                        {
524                                                n.Append("&lt;");
525                                                break;
526                                        }
527                                       
528                                        case '>': 
529                                        {
530                                                n.Append("&gt;");
531                                                break;
532                                        }
533                                       
534                                        case '"': 
535                                        {
536                                                n.Append("&quot;");
537                                                break;
538                                        }
539                                       
540                                        case '\'': 
541                                        {
542                                                n.Append("&apos;");
543                                                break;
544                                        }
545                                       
546                                        default: 
547                                        {
548                                                n.Append(c);
549                                                break;
550                                        }
551                                       
552                                }
553                        }
554                        return n.ToString();
555                }
556               
557                public virtual void  xmlSerializeNode(TextWriter outWriter)
558                {
559                        StringBuilder buf = new StringBuilder(100);
560                        buf.Append("<");
561                        buf.Append(GetType().FullName + " ");
562                        buf.Append("text=\"" + encode(getText()) + "\" type=\"" + Type + "\"/>");
563                        outWriter.Write(buf.ToString());
564                }
565               
566                public virtual void  xmlSerializeRootOpen(TextWriter outWriter)
567                {
568                        StringBuilder buf = new StringBuilder(100);
569                        buf.Append("<");
570                        buf.Append(GetType().FullName + " ");
571                        buf.Append("text=\"" + encode(getText()) + "\" type=\"" + Type + "\">\n");
572                        outWriter.Write(buf.ToString());
573                }
574               
575                public virtual void  xmlSerializeRootClose(TextWriter outWriter)
576                {
577                        outWriter.Write("</" + GetType().FullName + ">\n");
578                }
579               
580                public virtual void  xmlSerialize(TextWriter outWriter)
581                {
582                        // print out this node and all siblings
583                         for (AST node = this; node != null; node = node.getNextSibling())
584                        {
585                                if (node.getFirstChild() == null)
586                                {
587                                        // print guts (class name, attributes)
588                                        ((BaseAST) node).xmlSerializeNode(outWriter);
589                                }
590                                else
591                                {
592                                        ((BaseAST) node).xmlSerializeRootOpen(outWriter);
593                                       
594                                        // print children
595                                        ((BaseAST) node.getFirstChild()).xmlSerialize(outWriter);
596                                       
597                                        // print end tag
598                                        ((BaseAST) node).xmlSerializeRootClose(outWriter);
599                                }
600                        }
601                }
602
603                #region Implementation of ICloneable
604                [Obsolete("Deprecated since version 2.7.2. Use ASTFactory.dup() instead.", false)]
605                public virtual object Clone()
606                {
607                        return MemberwiseClone();
608                }
609                #endregion
610
611                public override Int32 GetHashCode() 
612                {
613                        return base.GetHashCode();
614                }       
615        }
616}
Note: See TracBrowser for help on using the repository browser.