1 | using System; |
---|
2 | using System.Runtime.InteropServices; |
---|
3 | using Stream = System.IO.Stream; |
---|
4 | using BinaryReader = System.IO.BinaryReader; |
---|
5 | using IOException = System.IO.IOException; |
---|
6 | |
---|
7 | namespace 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: added this class to handle Binary input w/ FileInputStream |
---|
36 | |
---|
37 | public class ByteBuffer:InputBuffer |
---|
38 | { |
---|
39 | |
---|
40 | // char source |
---|
41 | [NonSerialized()] |
---|
42 | internal Stream input; |
---|
43 | |
---|
44 | private const int BUF_SIZE = 16; |
---|
45 | /// <summary> |
---|
46 | /// Small buffer used to avoid reading individual chars |
---|
47 | /// </summary> |
---|
48 | private byte[] buf = new byte[BUF_SIZE]; |
---|
49 | |
---|
50 | |
---|
51 | /*Create a character buffer */ |
---|
52 | public ByteBuffer(Stream input_) : base() |
---|
53 | { |
---|
54 | input = input_; |
---|
55 | } |
---|
56 | |
---|
57 | /*Ensure that the character buffer is sufficiently full */ |
---|
58 | override public void fill(int amount) |
---|
59 | { |
---|
60 | // try |
---|
61 | // { |
---|
62 | syncConsume(); |
---|
63 | // Fill the buffer sufficiently to hold needed characters |
---|
64 | int bytesToRead = (amount + markerOffset) - queue.Count; |
---|
65 | int c; |
---|
66 | |
---|
67 | while (bytesToRead > 0) |
---|
68 | { |
---|
69 | // Read a few characters |
---|
70 | c = input.Read(buf, 0, BUF_SIZE); |
---|
71 | for (int i = 0; i < c; i++) |
---|
72 | { |
---|
73 | // Append the next character |
---|
74 | queue.Add(unchecked((char) buf[i])); |
---|
75 | } |
---|
76 | if (c < BUF_SIZE) |
---|
77 | { |
---|
78 | while ((bytesToRead-- > 0) && (queue.Count < BUF_SIZE)) |
---|
79 | { |
---|
80 | queue.Add(CharScanner.EOF_CHAR); |
---|
81 | } |
---|
82 | break; |
---|
83 | } |
---|
84 | bytesToRead -= c; |
---|
85 | } |
---|
86 | // } |
---|
87 | // catch (IOException io) |
---|
88 | // { |
---|
89 | // throw new CharStreamIOException(io); |
---|
90 | // } |
---|
91 | } |
---|
92 | } |
---|
93 | } |
---|