~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/Tools/SharpCoco/src/Scanner.frame

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
using System;
 
2
using System.Drawing;
 
3
using System.IO;
 
4
using System.Collections;
 
5
using System.Text;
 
6
 
 
7
-->namespace
 
8
 
 
9
public class Token {
 
10
        public int kind;    // token kind
 
11
        public int pos;     // token position in the source text (starting at 0)
 
12
        public int col;     // token column (starting at 0)
 
13
        public int line;    // token line (starting at 1)
 
14
        public string val;  // token value
 
15
        public Token next;  // AW 2003-03-07 Tokens are kept in linked list
 
16
        
 
17
        public Point Location {
 
18
                get {
 
19
                        return new Point(line, col);
 
20
                }
 
21
        }
 
22
 
 
23
        public Token () { }
 
24
        public Token (int kind) { this.kind = kind; }
 
25
}
 
26
 
 
27
public class Buffer {
 
28
        public const int eof = '\uffff';
 
29
 
 
30
        static byte[] buf;
 
31
        static int bufLen;
 
32
        static int pos;
 
33
        
 
34
        public static void Fill (string fileName) {
 
35
                FileStream s = null;
 
36
                try {
 
37
                        s = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
 
38
                        Fill(s);
 
39
                } catch (IOException) {
 
40
                        Console.WriteLine("--- Cannot open file {0}", fileName);
 
41
                        System.Environment.Exit(0);
 
42
                } finally {
 
43
                        if (s != null) s.Close();
 
44
                }
 
45
        }
 
46
        
 
47
        public static void Fill (Stream s) {
 
48
                bufLen = (int) s.Length;
 
49
                buf = new byte[bufLen];
 
50
                s.Read(buf, 0, bufLen); 
 
51
                pos = 0;
 
52
        }
 
53
                
 
54
        public static int Read () {
 
55
                if (pos < bufLen) return buf[pos++];
 
56
                else return 0;
 
57
        }
 
58
 
 
59
        public static int Peek () {
 
60
                if (pos < bufLen) return buf[pos];
 
61
                else return 0;
 
62
        }
 
63
        
 
64
        /* AW 2003-03-10 moved this from ParserGen.cs */
 
65
        public static string GetString (int beg, int end) {
 
66
                StringBuilder s = new StringBuilder(64);
 
67
                int oldPos = Buffer.Pos;
 
68
                Buffer.Pos = beg;
 
69
                while (beg < end) { s.Append((char)Buffer.Read()); beg++; }
 
70
                Buffer.Pos = oldPos;
 
71
                return s.ToString();
 
72
        }
 
73
 
 
74
        public static int Pos {
 
75
                get { return pos; }
 
76
                set {
 
77
                        if (value < 0) pos = 0; 
 
78
                        else if (value >= bufLen) pos = bufLen; 
 
79
                        else pos = value;
 
80
                }
 
81
        }
 
82
}
 
83
 
 
84
public class Scanner {
 
85
        const char EOF = '\0';
 
86
        const char EOL = '\n';
 
87
        const char CR  = '\n';
 
88
 
 
89
-->constants
 
90
        
 
91
-->declarations
 
92
 
 
93
        static Token t;          // current token
 
94
        static char ch;          // current input character
 
95
        static int pos;          // column number of current character
 
96
        static int line;         // line number of current character
 
97
        static int lineStart;    // start position of current line
 
98
        static int oldEols;    // EOLs that appeared in a comment;
 
99
        static BitArray ignore;  // set of characters to be ignored by the scanner
 
100
 
 
101
        /* ML ----- begin */
 
102
        static Token tokens;  // the complete input token stream
 
103
        static Token pt;      // current peek token
 
104
        
 
105
        static int peekCount = 0;
 
106
        
 
107
        public static int PeekCount { get { return peekCount; } }
 
108
        
 
109
        static void Init()
 
110
        {
 
111
                pos = -1; line = 1; lineStart = 0;
 
112
                oldEols = 0;
 
113
                NextCh();
 
114
-->initialization
 
115
 
 
116
                /* AW 2003-03-07 fill token list */
 
117
                tokens = new Token();  // first token is a dummy
 
118
                Token node = tokens;
 
119
                do {
 
120
                        node.next = NextToken();
 
121
                        node = node.next;
 
122
                } while (node.kind != 0);       /* AW: 0 => EOF */
 
123
                t = pt = tokens;
 
124
        }
 
125
 
 
126
        public static void Init(String fileName) {
 
127
                Buffer.Fill(fileName);
 
128
                Init();
 
129
        }
 
130
        
 
131
        public static void Init(Stream s) {
 
132
                Buffer.Fill(s);
 
133
                Init();
 
134
        }
 
135
 
 
136
        static void NextCh() {
 
137
                if (oldEols > 0) { ch = EOL; oldEols--; } 
 
138
                else {
 
139
                        ch = (char)Buffer.Read(); pos++;
 
140
                        // replace isolated '\r' by '\n' in order to make
 
141
                        // eol handling uniform across Windows, Unix and Mac
 
142
                        if (ch == '\r' && Buffer.Peek() != '\n') ch = EOL;
 
143
                        else if (ch > '\u007f') ch = '?';
 
144
                        if (ch == EOL) { line++; lineStart = pos + 1; }
 
145
                }
 
146
        }
 
147
        
 
148
-->comment
 
149
        
 
150
        static void CheckLiteral() {
 
151
                switch (t.val) {
 
152
-->literals
 
153
                }
 
154
        }
 
155
 
 
156
        /* AW Scan() renamed to NextToken() */
 
157
        static Token NextToken() {
 
158
                while (ignore[ch]) NextCh();
 
159
-->scan1
 
160
                t = new Token();
 
161
                t.pos = pos; t.col = pos - lineStart + 1; t.line = line; 
 
162
                int state = start[ch];
 
163
                StringBuilder buf = new StringBuilder(16);
 
164
                buf.Append(ch); NextCh();
 
165
                
 
166
                switch (state) {
 
167
                        case 0: { t.kind = noSym; goto done; }  // NextCh already done
 
168
-->scan2
 
169
                }
 
170
                done: 
 
171
                t.val = buf.ToString();
 
172
                return t;
 
173
        }
 
174
        
 
175
        /* AW 2003-03-07 get the next token, move on and synch peek token with current */
 
176
        public static Token Scan () {
 
177
                t = pt = t.next;
 
178
                return t;
 
179
        }
 
180
 
 
181
        /* AW 2003-03-07 get the next token, ignore pragmas */
 
182
        public static Token Peek () {
 
183
                do {                      // skip pragmas while peeking
 
184
                        pt = pt.next;
 
185
                } while (pt != null && pt.kind > maxT);
 
186
                return pt;
 
187
        }
 
188
        
 
189
        /* AW 2003-03-11 to make sure peek start at current scan position */
 
190
        public static void StartPeek () { pt = t; }
 
191
} // end Scanner
 
192
 
 
193
 
 
194
public delegate void ErrorCodeProc (int line, int col, int n);
 
195
public delegate void ErrorMsgProc (int line, int col, string msg);
 
196
 
 
197
public class Errors {
 
198
        public static int count = 0;                                               // number of errors detected
 
199
        public static ErrorCodeProc SynErr = new ErrorCodeProc(DefaultCodeError);  // syntactic errors
 
200
        public static ErrorCodeProc SemErr = new ErrorCodeProc(DefaultCodeError);  // semantic errors
 
201
        public static ErrorMsgProc Error = new ErrorMsgProc(DefaultMsgError);      // user defined string based errors
 
202
        public static StringBuilder errorText = new StringBuilder();
 
203
        
 
204
        public static void Exception (string s) {
 
205
                Console.WriteLine(s); 
 
206
                System.Environment.Exit(0);
 
207
        }
 
208
 
 
209
        static void DefaultCodeError (int line, int col, int n) {
 
210
                errorText.Append(String.Format("-- line {0} col {1}: error {2}", line, col, n));
 
211
                errorText.Append("\n");
 
212
                count++;
 
213
        }
 
214
 
 
215
        static void DefaultMsgError (int line, int col, string s) {
 
216
                errorText.Append(String.Format("-- line {0} col {1}: {2}", line, col, s));
 
217
                errorText.Append("\n");
 
218
                count++;
 
219
        }
 
220
} // Errors
 
221
 
 
222
$$$