3 // Mono.MonoBASIC.Parser.cs (from .jay): The Parser for the MonoBASIC compiler
5 // Author: A Rafael D Teixeira (rafaelteixeirabr@hotmail.com)
7 // Licensed under the terms of the GNU GPL
9 // Copyright (C) 2001 A Rafael D Teixeira
15 namespace Mono.MonoBASIC
19 using System.Collections;
24 /// The MonoBASIC Parser
26 public class Parser : GenericParser
31 /// Current block is used to add statements as we find
37 /// Current interface is used by the various declaration
38 /// productions in the interface declaration to "add"
39 /// the interfaces as we find them.
41 Interface current_interface;
44 /// This is used by the unary_expression code to resolve
45 /// a name against a parameter.
47 Parameters current_local_parameters;
50 /// Using during property parsing to describe the implicit
51 /// value parameter that is passed to the "set" accessor
54 Parameter [] implicit_value_parameters;
57 bool UseExtendedSyntax; // for ".mbs" files
59 public override string[] extensions()
61 string [] list = { ".vb", ".mbs" };
68 %token NONE /* This token is never returned by our lexer */
69 %token ERROR // This is used not by the parser, but by the tokenizer.
73 *These are the MonoBASIC keywords
113 %token DESCRIPTION // MonoBASIC extension
162 %token NOTINHERITABLE
163 %token NOTOVERRIDABLE
173 %token PARAMETER // MonoBASIC extension
200 %token SUMMARY // MonoBASIC extension
218 /* MonoBASIC single character operators/punctuation. */
219 %token OPEN_BRACKET "["
220 %token CLOSE_BRACKET "]"
221 %token OPEN_PARENS "("
222 %token CLOSE_PARENS ")"
240 /* MonoBASIC multi-character operators. */
245 %token OP_AND //"and"
247 %token OP_XOR //"xor"
248 %token OP_MODULUS //"mod"
249 %token OP_MULT_ASSIGN "*="
250 %token OP_DIV_ASSIGN "/="
251 %token OP_IDIV_ASSIGN "\\="
252 %token OP_ADD_ASSIGN "+="
253 %token OP_SUB_ASSIGN "-="
254 %token OP_CONCAT_ASSIGN "&="
255 %token OP_EXP_ASSIGN "^="
258 %token LITERAL_INTEGER "int literal"
259 %token LITERAL_SINGLE "float literal"
260 %token LITERAL_DOUBLE "double literal"
261 %token LITERAL_DECIMAL "decimal literal"
262 %token LITERAL_CHARACTER "character literal"
263 %token LITERAL_STRING "string literal"
267 /* Add precedence rules to solve dangling else s/r conflict */
276 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
278 %left STAR DIV PERCENT
279 %right BITWISE_NOT CARRET UMINUS
280 %nonassoc OP_INC OP_DEC
282 %left OPEN_BRACKET OPEN_BRACE
286 %start compilation_unit
290 : opt_imports_directives
306 | declarations declaration
310 : namespace_declaration
317 Class c = (Class) $1;
318 mod_flags = c.ModFlags;
320 } else if ($1 is Struct){
321 Struct s = (Struct) $1;
322 mod_flags = s.ModFlags;
327 if ((mod_flags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
329 1527, lexer.Location,
330 "Namespace elements cannot be explicitly " +
331 "declared private or protected in '" + name + "'");
338 | qualified_identifier DOT IDENTIFIER
340 $$ = (($1).ToString ()) + "." + ($3.ToString ());
343 opt_imports_directives
350 | imports_directives imports_directive
354 : /* imports_alias_directive
355 | */ imports_namespace_directive
358 imports_namespace_directive
359 : IMPORTS qualified_identifier EOL
361 current_namespace.Using ((string) $2, lexer.Location);
369 namespace_declaration
370 : NAMESPACE qualified_identifier EOL
372 current_namespace = RootContext.Tree.RecordNamespace(current_namespace, name, (string)$2);
374 opt_imports_directives
378 current_namespace = current_namespace.Parent;
385 // | struct_declaration
386 // | interface_declaration
387 // | enum_declaration
388 // | delegate_declaration
392 : /* opt_attributes opt_modifiers */
393 CLASS IDENTIFIER /* opt_class_interfaces */ EOL
396 opt_class_member_declarations
403 : /* empty */ { $$ = (int) 0; }
404 | PUBLIC { $$ = Modifiers.PUBLIC; }
405 | FRIEND { $$ = Modifiers.INTERNAL; }
409 : opt_attributes opt_module_modifiers
410 MODULE IDENTIFIER EOL
415 name = MakeName((string) $4);
416 new_module = new Module(current_container,
421 current_container = new_module;
422 current_container.Namespace = current_namespace;
423 RootContext.Tree.RecordDecl(name, new_module);
425 opt_class_member_declarations
428 Module new_module = (Module)current_container;
430 current_container = current_container.Parent;
431 CheckDef (current_container.AddClass(new_module), new_module.Name, new_module.Location);
437 opt_class_member_declarations
439 | class_member_declarations
442 class_member_declarations
443 : class_member_declaration
444 | class_member_declarations class_member_declaration
447 class_member_declaration
448 : /* type_declaration
453 : SUB qualified_identifier OPEN_PARENS opt_formal_parameters CLOSE_PARENS EOL
460 | qualified_identifier OPEN_PARENS opt_actual_parameters CLOSE_PARENS EOL
463 opt_formal_parameters
465 | qualified_identifier AS qualified_identifier
468 opt_actual_parameters
470 | qualified_identifier
479 public Tokenizer Lexer {
485 public override int parse ()
487 current_namespace = new Namespace (null, "");
488 current_container = RootContext.Tree.Types;
489 current_container.Namespace = current_namespace;
491 UseExtendedSyntax = name.EndsWith(".mbs");
493 lexer = new Tokenizer (input, name, defines);
494 StringBuilder value = new StringBuilder ();
498 if (yacc_verbose_flag)
499 yyparse (lexer, new yydebug.yyDebugSimple ());
505 Console.WriteLine (lexer.location + " : Parsing error ");
506 Console.WriteLine (e);
509 return Report.Errors;