2 // Mono.MonoBASIC.Tokenizer.cs: The Tokenizer for the MonoBASIC compiler
4 // Author: A Rafael D Teixeira (rafaelteixeirabr@hotmail.com)
6 // Based on cs-tokenizer.cs by Miguel de Icaza (miguel@gnu.org)
8 // Licensed under the terms of the GNU GPL
10 // Copyright (C) 2001 A Rafael D Teixeira
13 namespace Mono.MonoBASIC
17 using System.Collections;
19 using System.Globalization;
24 /// Tokenizer for MonoBASIC source code.
27 public class Tokenizer : yyParser.yyInput
30 public string ref_name;
31 public int ref_line = 1;
34 public int current_token;
35 bool handle_get_set = false;
37 public int ExpandedTabsSize = 4;
39 public string location {
43 if (current_token == Token.ERROR)
44 det = "detail: " + error_details;
48 return "Line: "+line+" Col: "+col + "\n" +
49 "VirtLine: "+ref_line +
50 " Token: "+current_token + " " + det;
54 public bool properties {
56 return handle_get_set;
60 handle_get_set = value;
67 static Hashtable keywords;
68 static NumberStyles styles;
69 static NumberFormatInfo csharp_format_info;
72 // Values for the associated token returned
74 System.Text.StringBuilder number;
79 // Details about the error encoutered by the tokenizer
101 static void initTokens ()
103 keywords = new Hashtable ();
105 keywords.Add ("addhandler", Token.ADDHANDLER);
106 keywords.Add ("addressof", Token.ADDRESSOF);
107 keywords.Add ("alias", Token.ALIAS);
108 keywords.Add ("and", Token.AND);
109 keywords.Add ("andalso", Token.ANDALSO);
110 keywords.Add ("ansi", Token.ANSI);
111 keywords.Add ("as", Token.AS);
112 keywords.Add ("assembly", Token.ASSEMBLY);
113 keywords.Add ("auto", Token.AUTO);
114 keywords.Add ("boolean", Token.BOOLEAN);
115 keywords.Add ("byref", Token.BYREF);
116 keywords.Add ("byte", Token.BYTE);
117 keywords.Add ("byval", Token.BYVAL);
118 keywords.Add ("call", Token.CALL);
119 keywords.Add ("case", Token.CASE);
120 keywords.Add ("catch", Token.CATCH);
121 keywords.Add ("cbool", Token.CBOOL);
122 keywords.Add ("cbyte", Token.CBYTE);
123 keywords.Add ("cchar", Token.CCHAR);
124 keywords.Add ("cdate", Token.CDATE);
125 keywords.Add ("cdec", Token.CDEC);
126 keywords.Add ("cdbl", Token.CDBL);
127 keywords.Add ("char", Token.CHAR);
128 keywords.Add ("cint", Token.CINT);
129 keywords.Add ("class", Token.CLASS);
130 keywords.Add ("clng", Token.CLNG);
131 keywords.Add ("cobj", Token.COBJ);
132 //keywords.Add ("compare", Token.COMPARE);
133 keywords.Add ("const", Token.CONST);
134 keywords.Add ("cshort", Token.CSHORT);
135 keywords.Add ("csng", Token.CSNG);
136 keywords.Add ("cstr", Token.CSTR);
137 keywords.Add ("ctype", Token.CTYPE);
138 keywords.Add ("date", Token.DATE);
139 keywords.Add ("decimal", Token.DECIMAL);
140 keywords.Add ("declare", Token.DECLARE);
141 keywords.Add ("default", Token.DEFAULT);
142 keywords.Add ("delegate", Token.DELEGATE);
143 keywords.Add ("dim", Token.DIM);
144 keywords.Add ("do", Token.DO);
145 keywords.Add ("double", Token.DOUBLE);
146 keywords.Add ("each", Token.EACH);
147 keywords.Add ("else", Token.ELSE);
148 keywords.Add ("elseif", Token.ELSEIF);
149 keywords.Add ("end", Token.END);
150 keywords.Add ("enum", Token.ENUM);
151 keywords.Add ("erase", Token.ERASE);
152 keywords.Add ("error", Token.ERROR);
153 keywords.Add ("event", Token.EVENT);
154 keywords.Add ("exit", Token.EXIT);
155 //keywords.Add ("explicit", Token.EXPLICIT);
156 keywords.Add ("false", Token.FALSE);
157 keywords.Add ("finally", Token.FINALLY);
158 keywords.Add ("for", Token.FOR);
159 keywords.Add ("friend", Token.FRIEND);
160 keywords.Add ("function", Token.FUNCTION);
161 keywords.Add ("get", Token.GET);
162 keywords.Add ("gettype", Token.GETTYPE);
163 keywords.Add ("goto", Token.GOTO);
164 keywords.Add ("handles", Token.HANDLES);
165 keywords.Add ("if", Token.IF);
166 keywords.Add ("implements", Token.IMPLEMENTS);
167 keywords.Add ("imports", Token.IMPORTS);
168 keywords.Add ("in", Token.IN);
169 keywords.Add ("inherits", Token.INHERITS);
170 keywords.Add ("integer", Token.INTEGER);
171 keywords.Add ("interface", Token.INTERFACE);
172 keywords.Add ("is", Token.IS);
173 keywords.Add ("let ", Token.LET );
174 keywords.Add ("lib ", Token.LIB );
175 keywords.Add ("like ", Token.LIKE );
176 keywords.Add ("long", Token.LONG);
177 keywords.Add ("loop", Token.LOOP);
178 keywords.Add ("me", Token.ME);
179 keywords.Add ("mod", Token.MOD);
180 keywords.Add ("module", Token.MODULE);
181 keywords.Add ("mustinherit", Token.MUSTINHERIT);
182 keywords.Add ("mustoverride", Token.MUSTOVERRIDE);
183 keywords.Add ("mybase", Token.MYBASE);
184 keywords.Add ("myclass", Token.MYCLASS);
185 keywords.Add ("namespace", Token.NAMESPACE);
186 keywords.Add ("new", Token.NEW);
187 keywords.Add ("next", Token.NEXT);
188 keywords.Add ("not", Token.NOT);
189 keywords.Add ("nothing", Token.NOTHING);
190 keywords.Add ("notinheritable", Token.NOTINHERITABLE);
191 keywords.Add ("notoverridable", Token.NOTOVERRIDABLE);
192 keywords.Add ("object", Token.OBJECT);
193 keywords.Add ("on", Token.ON);
194 keywords.Add ("option", Token.OPTION);
195 keywords.Add ("optional", Token.OPTIONAL);
196 keywords.Add ("or", Token.OR);
197 keywords.Add ("orelse", Token.ORELSE);
198 keywords.Add ("overloads", Token.OVERLOADS);
199 keywords.Add ("overridable", Token.OVERRIDABLE);
200 keywords.Add ("overrides", Token.OVERRIDES);
201 keywords.Add ("paramarray", Token.PARAM_ARRAY);
202 keywords.Add ("preserve", Token.PRESERVE);
203 keywords.Add ("private", Token.PRIVATE);
204 keywords.Add ("property", Token.PROPERTY);
205 keywords.Add ("protected", Token.PROTECTED);
206 keywords.Add ("public", Token.PUBLIC);
207 keywords.Add ("raiseevent", Token.RAISEEVENT);
208 keywords.Add ("readonly", Token.READONLY);
209 keywords.Add ("redim", Token.REDIM);
210 keywords.Add ("rem", Token.REM);
211 keywords.Add ("removehandler", Token.REMOVEHANDLER);
212 keywords.Add ("resume", Token.RESUME);
213 keywords.Add ("return", Token.RETURN);
214 keywords.Add ("select", Token.SELECT);
215 keywords.Add ("set", Token.SET);
216 keywords.Add ("shadows", Token.SHADOWS);
217 keywords.Add ("shared", Token.SHARED);
218 keywords.Add ("short", Token.SHORT);
219 keywords.Add ("single", Token.SINGLE);
220 keywords.Add ("sizeof", Token.SIZEOF);
221 keywords.Add ("static", Token.STATIC);
222 keywords.Add ("step", Token.STEP);
223 keywords.Add ("stop", Token.STOP);
224 keywords.Add ("string", Token.STRING);
225 keywords.Add ("structure", Token.STRUCTURE);
226 keywords.Add ("sub", Token.SUB);
227 keywords.Add ("synclock", Token.SYNCLOCK);
228 keywords.Add ("then", Token.THEN);
229 keywords.Add ("throw", Token.THROW);
230 keywords.Add ("to", Token.TO);
231 keywords.Add ("true", Token.TRUE);
232 keywords.Add ("try", Token.TRY);
233 keywords.Add ("typeof", Token.TYPEOF);
234 keywords.Add ("unicode", Token.UNICODE);
235 keywords.Add ("until", Token.UNTIL);
236 keywords.Add ("variant", Token.VARIANT);
237 keywords.Add ("when", Token.WHEN);
238 keywords.Add ("while", Token.WHILE);
239 keywords.Add ("with", Token.WITH);
240 keywords.Add ("withevents", Token.WITHEVENTS);
241 keywords.Add ("writeonly", Token.WRITEONLY);
242 keywords.Add ("xor", Token.XOR);
251 csharp_format_info = new NumberFormatInfo ();
252 csharp_format_info.CurrencyDecimalSeparator = ".";
253 styles = NumberStyles.AllowExponent | NumberStyles.AllowDecimalPoint;
256 bool is_keyword (string name)
260 res = keywords.Contains(name.ToLower());
261 if ((name == "get" || name == "set") && handle_get_set == false)
266 int getKeyword (string name)
268 return (int) (keywords [name.ToLower()]);
271 public Location Location {
273 return new Location (ref_line);
277 public bool PropertyParsing {
279 return handle_get_set;
283 handle_get_set = value;
287 bool is_identifier_start_character (char c)
289 return Char.IsLetter (c) || c == '_' ;
292 bool is_identifier_part_character (char c)
294 return (Char.IsLetter (c) || Char.IsDigit (c) || c == '_');
297 int is_punct (char c, ref bool doread)
299 int idx = "{}[](),:;~+-*/%&|^!=<>?".IndexOf (c);
307 // return Token.OPEN_BRACKET;
309 // return Token.CLOSE_BRACKET;
311 return Token.OPEN_PARENS;
313 return Token.CLOSE_PARENS;
328 t = Token.OP_ADD_ASSIGN;
336 t = Token.OP_SUB_ASSIGN;
354 return Token.OP_MULT_ASSIGN;
362 return Token.OP_DIV_ASSIGN;
370 return Token.OP_IDIV_ASSIGN;
372 return Token.OP_IDIV;
378 return Token.OP_EXP_ASSIGN;
406 bool decimal_digits (int c)
409 bool seen_digits = false;
412 number.Append ((char) c);
414 while ((d = peekChar ()) != -1){
415 if (Char.IsDigit ((char)d)){
416 number.Append ((char) d);
425 void hex_digits (int c)
430 number.Append ((char) c);
431 while ((d = peekChar ()) != -1){
432 char e = Char.ToUpper ((char) d);
434 if (Char.IsDigit (e) ||
435 (e >= 'A' && e <= 'F')){
436 number.Append ((char) e);
443 int real_type_suffix (int c)
449 t = Token.LITERAL_SINGLE;
452 t = Token.LITERAL_DOUBLE;
455 t= Token.LITERAL_DECIMAL;
464 int integer_type_suffix (int c)
466 // FIXME: Handle U and L suffixes.
467 // We also need to see in which kind of
468 // Int the thing fits better according to the spec.
469 return Token.LITERAL_INTEGER;
472 void adjust_int (int t)
474 val = new System.Int32();
475 val = System.Int32.Parse (number.ToString (), 0);
478 int adjust_real (int t)
480 string s = number.ToString ();
482 Console.WriteLine (s);
484 case Token.LITERAL_DECIMAL:
485 val = new System.Decimal ();
486 val = System.Decimal.Parse (
487 s, styles, csharp_format_info);
489 case Token.LITERAL_DOUBLE:
490 val = new System.Double ();
491 val = System.Double.Parse (
492 s, styles, csharp_format_info);
494 case Token.LITERAL_SINGLE:
495 val = new System.Double ();
496 val = (float) System.Double.Parse (
497 s, styles, csharp_format_info);
501 val = new System.Double ();
502 val = System.Double.Parse (
503 s, styles, csharp_format_info);
504 t = Token.LITERAL_DOUBLE;
511 // Invoked if we know we have .digits or digits
513 int is_number (int c)
515 bool is_real = false;
516 number = new System.Text.StringBuilder ();
521 if (Char.IsDigit ((char)c)){
522 if (c == '0' && peekChar () == 'x' || peekChar () == 'X'){
525 val = new System.Int32 ();
526 val = System.Int32.Parse (number.ToString (), NumberStyles.HexNumber);
527 return integer_type_suffix (peekChar ());
534 // We need to handle the case of
535 // "1.1" vs "1.string" (LITERAL_SINGLE vs NUMBER DOT IDENTIFIER)
538 if (decimal_digits ('.')){
544 adjust_int (Token.LITERAL_INTEGER);
545 return Token.LITERAL_INTEGER;
549 if (c == 'e' || c == 'E'){
556 number.Append ((char) c);
559 } else if (c == '-'){
560 number.Append ((char) c);
568 type = real_type_suffix (c);
569 if (type == Token.NONE && !is_real){
570 type = integer_type_suffix (c);
578 return adjust_real (type);
580 Console.WriteLine ("This should not be reached");
581 throw new Exception ("Is Number should never reach this point");
617 error_details = "cs1009: Unrecognized escape sequence " + (char)d;
626 if (putback_char != -1){
627 int x = putback_char;
632 return reader.Read ();
637 if (putback_char != -1)
639 return reader.Peek ();
644 if (putback_char != -1)
645 throw new Exception ("This should not happen putback on putback");
649 public bool advance ()
651 return current_token != Token.EOF ;
654 public Object Value {
660 public Object value ()
665 private bool IsEOL(int currentChar)
667 if (currentChar == 0x0D)
669 if (peekChar() == 0x0A) // if it is a CR-LF pair consume LF also
674 return (currentChar == -1 || currentChar == 0x0A || currentChar == 0x2028 || currentChar == 0x2029);
677 private int DropComments()
680 while (!IsEOL(d = getChar ()))
691 int lastToken = current_token;
694 current_token = xtoken ();
695 if (current_token == 0)
697 if (current_token == Token.REM)
698 current_token = DropComments();
699 } while (lastToken == Token.EOL && current_token == Token.EOL);
701 return current_token;
704 private string GetIdentifier()
707 if (is_identifier_start_character ((char) c))
708 return GetIdentifier(c);
713 private string GetIdentifier(int c)
715 System.Text.StringBuilder id = new System.Text.StringBuilder ();
717 id.Append ((char) c);
719 while ((c = peekChar ()) != -1)
721 if (is_identifier_part_character ((char) c))
723 id.Append ((char)getChar ());
730 return id.ToString ();
740 for (;(c = getChar ()) != -1; col++) {
742 // Handle line comments.
752 if (current_token == Token.EOL) // if last token was also EOL keep skipping
757 // Handle escaped identifiers
760 if ((val = GetIdentifier()) == null)
762 if ((c = getChar()) != ']')
764 return Token.IDENTIFIER;
767 // Handle unescaped identifiers
768 if (is_identifier_start_character ((char) c))
771 if ((id = GetIdentifier(c)) == null)
774 return getKeyword(id);
776 return Token.IDENTIFIER;
779 // handle numeric literals
781 if (Char.IsDigit ((char) peekChar ()))
782 return is_number (c);
786 if (Char.IsDigit ((char) c))
787 return is_number (c);
789 /* For now, limited support for pre-processor commands */
790 if (col == 1 && c == '#'){
791 System.Text.StringBuilder s = new System.Text.StringBuilder ();
793 while ((c = getChar ()) != -1 && (c != '\n')){
796 if (String.Compare (s.ToString (), 0, "line", 0, 4) == 0){
797 string arg = s.ToString ().Substring (5);
800 if ((pos = arg.IndexOf (' ')) != -1 && pos != 0){
801 ref_line = System.Int32.Parse (arg.Substring (0, pos));
804 char [] quotes = { '\"' };
806 ref_name = arg.Substring (pos);
807 ref_name.TrimStart (quotes);
808 ref_name.TrimEnd (quotes);
810 ref_line = System.Int32.Parse (arg);
817 if ((t = is_punct ((char)c, ref doread)) != Token.ERROR){
825 // Treat string literals
827 System.Text.StringBuilder s = new System.Text.StringBuilder ();
829 while ((c = getChar ()) != -1){
830 if (c == '"'){ // TODO: treat double-doublequotes
832 return Token.LITERAL_STRING;
842 // expand tabs for location and ignore it as whitespace
845 col = (((col + ExpandedTabsSize) / ExpandedTabsSize) * ExpandedTabsSize) - 1;
850 if (c == ' ' || c == '\f' || c == '\v')
853 error_details = ((char)c).ToString ();
858 if (current_token != Token.EOL) // if last token wasn´t EOL send it before EOF
864 public void cleanup ()
\r
866 /* borrowed from mcs - have to work it to have preprocessing in mbas
868 if (ifstack != null && ifstack.Count >= 1) {
\r
869 int state = (int) ifstack.Pop ();
\r
870 if ((state & REGION) != 0)
\r
871 Report.Error (1038, "#endregion directive expected");
\r
873 Report.Error (1027, "#endif directive expected");
\r
878 public Tokenizer (System.IO.TextReader input, string fname, ArrayList defines)
880 this.ref_name = fname;
884 Location.Push (fname);