1/*
2 * [The "BSD licence"]
3 * Copyright (c) 2005-2008 Terence Parr
4 * All rights reserved.
5 *
6 * Conversion to C#:
7 * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc.
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. The name of the author may not be used to endorse or promote products
19 *    derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33namespace Antlr.Runtime.Tree
34{
35    using Antlr.Runtime.Misc;
36
37    public class TreeFilter : TreeParser
38    {
39        protected ITokenStream originalTokenStream;
40        protected ITreeAdaptor originalAdaptor;
41
42        public TreeFilter( ITreeNodeStream input )
43            : this( input, new RecognizerSharedState() )
44        {
45        }
46        public TreeFilter( ITreeNodeStream input, RecognizerSharedState state )
47            : base( input, state )
48        {
49            originalAdaptor = input.TreeAdaptor;
50            originalTokenStream = input.TokenStream;
51        }
52
53        public virtual void ApplyOnce( object t, Action whichRule )
54        {
55            if ( t == null )
56                return;
57
58            try
59            {
60                // share TreeParser object but not parsing-related state
61                state = new RecognizerSharedState();
62                input = new CommonTreeNodeStream( originalAdaptor, t );
63                ( (CommonTreeNodeStream)input ).TokenStream = originalTokenStream;
64                BacktrackingLevel = 1;
65                whichRule();
66                BacktrackingLevel = 0;
67            }
68            catch ( RecognitionException )
69            {
70            }
71        }
72
73        public virtual void Downup( object t )
74        {
75            TreeVisitor v = new TreeVisitor( new CommonTreeAdaptor() );
76            Func<object, object> pre = ( o ) =>
77            {
78                ApplyOnce( o, Topdown );
79                return o;
80            };
81            Func<object, object> post = ( o ) =>
82            {
83                ApplyOnce( o, Bottomup );
84                return o;
85            };
86            v.Visit( t, pre, post );
87        }
88
89        // methods the downup strategy uses to do the up and down rules.
90        // to override, just define tree grammar rule topdown and turn on
91        // filter=true.
92        protected virtual void Topdown()
93        {
94        }
95        protected virtual void Bottomup()
96        {
97        }
98    }
99}
100