source: trunk/yao/share/antlr-2.7.7/lib/csharp/antlr.runtime/antlr/CharBuffer.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: 2.3 KB
Line 
1using System;
2using System.Runtime.InteropServices;
3using TextReader                = System.IO.TextReader;
4using IOException               = System.IO.IOException;
5
6
7namespace antlr
8{
9        /*ANTLR Translator Generator
10        * Project led by Terence Parr at http://www.jGuru.com
11        * Software rights: http://www.antlr.org/license.html
12        *
13        * $Id:$
14        */
15
16        //
17        // ANTLR C# Code Generator by Micheal Jordan
18        //                            Kunle Odutola       : kunle UNDERSCORE odutola AT hotmail DOT com
19        //                            Anthony Oguntimehin
20        //
21        // With many thanks to Eric V. Smith from the ANTLR list.
22        //
23
24        /*A Stream of characters fed to the lexer from a InputStream that can
25        * be rewound via mark()/rewind() methods.
26        * <p>
27        * A dynamic array is used to buffer up all the input characters.  Normally,
28        * "k" characters are stored in the buffer.  More characters may be stored during
29        * guess mode (testing syntactic predicate), or when LT(i>k) is referenced.
30        * Consumption of characters is deferred.  In other words, reading the next
31        * character is not done by conume(), but deferred until needed by LA or LT.
32        * <p>
33        */
34       
35        // SAS: Move most functionality into InputBuffer -- just the file-specific
36        //      stuff is in here
37        public class CharBuffer : InputBuffer
38        {
39                // char source
40                [NonSerialized()]
41                internal TextReader input;
42
43                private const int BUF_SIZE = 16;
44                /// <summary>
45                /// Small buffer used to avoid reading individual chars
46                /// </summary>
47                private char[] buf = new char[BUF_SIZE];
48
49               
50                /*Create a character buffer */
51                public CharBuffer(TextReader input_) : base()
52                { 
53                        input = input_;
54                }
55               
56                /*Ensure that the character buffer is sufficiently full */
57                override public void  fill(int amount)
58                {
59                        try
60                        {
61                                syncConsume();
62                                // Fill the buffer sufficiently to hold needed characters
63                                int charsToRead = (amount + markerOffset) - queue.Count;
64                                int c;
65
66                                while (charsToRead > 0)
67                                {
68                                        // Read a few characters
69                                        c = input.Read(buf, 0, BUF_SIZE);
70                                        for (int i = 0; i < c; i++)
71                                        {
72                                                // Append the next character
73                                                queue.Add(buf[i]);
74                                        }
75                                        if (c < BUF_SIZE)
76                                        {
77                                                while ((charsToRead-- > 0) && (queue.Count < BUF_SIZE))
78                                                {
79                                                        queue.Add(CharScanner.EOF_CHAR);
80                                                }
81                                                break;
82                                        }
83                                        charsToRead -= c;
84                                }
85                        }
86                        catch (IOException io)
87                        {
88                                throw new CharStreamIOException(io);
89                        }
90                }
91        }
92}
Note: See TracBrowser for help on using the repository browser.