source: trunk/yao/share/antlr-2.7.7/lib/csharp/antlr.runtime/antlr/InputBuffer.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: 4.2 KB
Line 
1namespace antlr
2{
3        using System;
4        using ArrayList         = System.Collections.ArrayList;
5        using StringBuilder     = System.Text.StringBuilder;
6
7        /*ANTLR Translator Generator
8        * Project led by Terence Parr at http://www.jGuru.com
9        * Software rights: http://www.antlr.org/license.html
10        */
11
12        //
13        // ANTLR C# Code Generator by Micheal Jordan
14        //                            Kunle Odutola       : kunle UNDERSCORE odutola AT hotmail DOT com
15        //                            Anthony Oguntimehin
16        //
17        // With many thanks to Eric V. Smith from the ANTLR list.
18        //
19
20        // SAS: Added this class to genericise the input buffers for scanners
21        //      This allows a scanner to use a binary (FileInputStream) or
22        //      text (FileReader) stream of data; the generated scanner
23        //      subclass will define the input stream
24        //      There are two subclasses to this: CharBuffer and ByteBuffer
25       
26        /// <summary>
27        /// Represents a stream of characters fed to the lexer from that can be rewound
28        /// via mark()/rewind() methods.
29        /// </summary>
30        /// <remarks>
31        /// <para>
32        /// A dynamic array is used to buffer up all the input characters.  Normally,
33        /// "k" characters are stored in the buffer.  More characters may be stored
34        /// during guess mode (testing syntactic predicate), or when LT(i>k) is referenced.
35        /// Consumption of characters is deferred.  In other words, reading the next
36        /// character is not done by conume(), but deferred until needed by LA or LT.
37        /// </para>
38        /// </remarks>
39        public abstract class InputBuffer
40        {
41                // Number of active markers
42                protected internal int nMarkers = 0;
43               
44                // Additional offset used when markers are active
45                protected internal int markerOffset = 0;
46               
47                // Number of calls to consume() since last LA() or LT() call
48                protected internal int numToConsume = 0;
49               
50                // Circular queue
51                protected ArrayList     queue;
52               
53                /*Create an input buffer */
54                public InputBuffer()
55                {
56                        queue = new ArrayList();
57                }
58               
59                /*This method updates the state of the input buffer so that
60                *  the text matched since the most recent mark() is no longer
61                *  held by the buffer.  So, you either do a mark/rewind for
62                *  failed predicate or mark/commit to keep on parsing without
63                *  rewinding the input.
64                */
65                public virtual void  commit()
66                {
67                        nMarkers--;
68                }
69               
70                /*Mark another character for deferred consumption */
71                public virtual char consume()
72                {
73                        numToConsume++;
74                        return LA(1);
75                }
76               
77                /*Ensure that the input buffer is sufficiently full */
78                public abstract void  fill(int amount);
79               
80                public virtual string getLAChars()
81                {
82                        StringBuilder la = new StringBuilder();
83
84                        // copy buffer contents to array before looping thru contents (it's usually faster)
85                        char[] fastBuf = new char[queue.Count-markerOffset];
86                        queue.CopyTo(fastBuf, markerOffset);
87
88                        la.Append(fastBuf);
89                        return la.ToString();
90                }
91               
92                public virtual string getMarkedChars()
93                {
94                        StringBuilder marked = new StringBuilder();
95
96                        // copy buffer contents to array before looping thru contents (it's usually faster)
97                        char[] fastBuf = new char[queue.Count-markerOffset];
98                        queue.CopyTo(fastBuf, markerOffset);
99
100                        marked.Append(fastBuf);
101                        return marked.ToString();
102                }
103               
104                public virtual bool isMarked()
105                {
106                        return (nMarkers != 0);
107                }
108               
109                /*Get a lookahead character */
110                public virtual char LA(int i)
111                {
112                        fill(i);
113                        return (char) queue[markerOffset + i - 1];
114                }
115               
116                /*Return an integer marker that can be used to rewind the buffer to
117                * its current state.
118                */
119                public virtual int mark()
120                {
121                        syncConsume();
122                        nMarkers++;
123                        return markerOffset;
124                }
125               
126                /*Rewind the character buffer to a marker.
127                * @param mark Marker returned previously from mark()
128                */
129                public virtual void  rewind(int mark)
130                {
131                        syncConsume();
132                        markerOffset = mark;
133                        nMarkers--;
134                }
135               
136                /*Reset the input buffer
137                */
138                public virtual void  reset()
139                {
140                        nMarkers = 0;
141                        markerOffset = 0;
142                        numToConsume = 0;
143                        queue.Clear();
144                }
145               
146                /*Sync up deferred consumption */
147                protected internal virtual void  syncConsume()
148                {
149                        if (numToConsume > 0)
150                        {
151                                if (nMarkers > 0)
152                                {
153                                        // guess mode -- leave leading characters and bump offset.
154                                        markerOffset += numToConsume;
155                                }
156                                else
157                                {
158                                        // normal mode -- remove "consumed" characters from buffer
159                                        queue.RemoveRange(0, numToConsume);
160                                }
161                                numToConsume = 0;
162                        }
163                }
164        }
165}
Note: See TracBrowser for help on using the repository browser.