Beispiel #1
0
		void tokenize_file (CompilationUnit file, CompilerContext ctx)
		{
			Stream input;

			try {
				input = File.OpenRead (file.Name);
			} catch {
				Report.Error (2001, "Source file `" + file.Name + "' could not be found");
				return;
			}

			using (input){
				SeekableStreamReader reader = new SeekableStreamReader (input, RootContext.Encoding);
				Tokenizer lexer = new Tokenizer (reader, file, ctx);
				int token, tokens = 0, errors = 0;

				while ((token = lexer.token ()) != Token.EOF){
					tokens++;
					if (token == Token.ERROR)
						errors++;
				}
				Console.WriteLine ("Tokenized: " + tokens + " found " + errors + " errors");
			}
			
			return;
		}
Beispiel #2
0
		void Parse (CompilationUnit file, ModuleContainer module)
		{
			Stream input;

			try {
				input = File.OpenRead (file.Name);
			} catch {
				Report.Error (2001, "Source file `{0}' could not be found", file.Name);
				return;
			}

			// Check 'MZ' header
			if (input.ReadByte () == 77 && input.ReadByte () == 90) {
				Report.Error (2015, "Source file `{0}' is a binary file and not a text file", file.Name);
				input.Close ();
				return;
			}

			input.Position = 0;
			SeekableStreamReader reader = new SeekableStreamReader (input, RootContext.Encoding);

			Parse (reader, file, module);
			reader.Dispose ();
			input.Close ();
		}	
Beispiel #3
0
        public NamespaceEntry(NamespaceEntry parent, CompilationUnit file, string name)
        {
            this.parent = parent;
            this.file   = file;
            entries.Add(this);

            if (parent != null)
            {
                ns = parent.NS.GetNamespace(name, true);
            }
            else if (name != null)
            {
                ns = RootNamespace.Global.GetNamespace(name, true);
            }
            else
            {
                ns = RootNamespace.Global;
            }
            SlaveDeclSpace = new RootDeclSpace(this);
        }
Beispiel #4
0
        public NamespaceEntry(ModuleContainer ctx, NamespaceEntry parent, CompilationUnit file, string name)
        {
            this.ctx    = ctx;
            this.parent = parent;
            this.file   = file;
            entries.Add(this);

            if (parent != null)
            {
                ns = parent.NS.GetNamespace(name, true);
            }
            else if (name != null)
            {
                ns = ctx.GlobalRootNamespace.GetNamespace(name, true);
            }
            else
            {
                ns = ctx.GlobalRootNamespace;
            }

            SlaveDeclSpace = new RootDeclSpace(this);
        }
Beispiel #5
0
		void Parse (SeekableStreamReader reader, CompilationUnit file)
		{
			CSharpParser parser = new CSharpParser (reader, file, ctx);
			parser.parse ();
		}
Beispiel #6
0
		void Parse (SeekableStreamReader reader, CompilationUnit file)
		{
			CSharpParser parser = new CSharpParser (reader, file);
			parser.ErrorOutput = Report.Stderr;
			try {
				parser.parse ();
			} catch (Exception ex) {
				Report.Error(589, parser.Lexer.Location,
					"Compilation aborted in file `{0}', {1}", file.Name, ex);
			}
		}
Beispiel #7
0
		void Parse (CompilationUnit file)
		{
			Stream input;

			try {
				input = File.OpenRead (file.Name);
			} catch {
				Report.Error (2001, "Source file `{0}' could not be found", file.Name);
				return;
			}

			SeekableStreamReader reader = new SeekableStreamReader (input, encoding);

			// Check 'MZ' header
			if (reader.Read () == 77 && reader.Read () == 90) {
				Report.Error (2015, "Source file `{0}' is a binary file and not a text file", file.Name);
				input.Close ();
				return;
			}

			reader.Position = 0;
			Parse (reader, file);
			reader.Dispose ();
			input.Close ();
		}	
		public NamespaceEntry (ModuleContainer ctx, NamespaceEntry parent, CompilationUnit file, string name)
		{
			this.ctx = ctx;
			this.parent = parent;
			this.file = file;
			entries.Add (this);

			if (parent != null)
				ns = parent.NS.GetNamespace (name, true);
			else if (name != null)
				ns = ctx.GlobalRootNamespace.GetNamespace (name, true);
			else
				ns = ctx.GlobalRootNamespace;

			SlaveDeclSpace = new RootDeclSpace (ctx, this);
		}
		private NamespaceEntry (ModuleContainer ctx, NamespaceEntry parent, CompilationUnit file, Namespace ns, bool slave)
		{
			this.ctx = ctx;
			this.parent = parent;
			this.file = file;
			this.IsImplicit = true;
			this.ns = ns;
			this.SlaveDeclSpace = slave ? new RootDeclSpace (ctx, this) : null;
		}
Beispiel #10
0
public CSharpParser (SeekableStreamReader reader, CompilationUnit file, ModuleContainer module, NamespaceEntry ns)
{
	this.file = file;
	this.module = module;
	this.compiler = module.Compiler;
	this.settings = compiler.Settings;
	lang_version = settings.Version;
	doc_support = settings.Documentation != null;
	current_namespace = ns;
	current_class = current_namespace.SlaveDeclSpace;
	current_container = current_class.PartialContainer; // == RootContest.ToplevelTypes
	oob_stack.Clear ();
	lexer = new Tokenizer (reader, file, compiler);
	
	use_global_stacks = true;
}
Beispiel #11
0
public CSharpParser (SeekableStreamReader reader, CompilationUnit file, CompilerContext ctx)
{
	if (RootContext.EvalMode)
		undo = new Undo ();

	this.file = file;
	this.compiler = ctx;
	current_namespace = new NamespaceEntry (ctx, null, file, null);
	current_class = current_namespace.SlaveDeclSpace;
	current_container = current_class.PartialContainer; // == RootContest.ToplevelTypes
	oob_stack.Clear ();
	lexer = new Tokenizer (reader, file, ctx);
	
	use_global_stacks = true;
}
		public NamespaceEntry (NamespaceEntry parent, CompilationUnit file, string name)
		{
			this.parent = parent;
			this.file = file;
			entries.Add (this);

			if (parent != null)
				ns = parent.NS.GetNamespace (name, true);
			else if (name != null)
				ns = GlobalRootNamespace.Instance.GetNamespace (name, true);
			else
				ns = GlobalRootNamespace.Instance;
			SlaveDeclSpace = new RootDeclSpace (this);
		}
Beispiel #13
0
		// <summary>
		//   This must be called before parsing/tokenizing any files.
		// </summary>
		static public void AddFile (string name)
		{
			string path = Path.GetFullPath (name);

			if (source_files.Contains (path)){
				int id = (int) source_files [path];
				string other_name = ((SourceFile) source_list [id - 1]).Name;
				if (name.Equals (other_name))
					Report.Warning (2002, 1, "Source file `{0}' specified multiple times", other_name);
				else
					Report.Warning (2002, 1, "Source filenames `{0}' and `{1}' both refer to the same file: {2}", name, other_name, path);
				return;
			}

			source_files.Add (path, ++source_count);
			CompilationUnit unit = new CompilationUnit (name, path, source_count);
			source_list.Add (unit);
			compile_units.Add (unit);
		}
Beispiel #14
0
		static public void Push (CompilationUnit compile_unit, SourceFile file)
		{
			current_source = file != null ? file.Index : -1;
			current_compile_unit = compile_unit != null ? compile_unit.Index : -1;
			// File is always pushed before being changed.
		}
Beispiel #15
0
        public void Parse(SeekableStreamReader reader, CompilationUnit file, ModuleContainer module)
        {
            CSharpParser parser = new CSharpParser(reader, file, module);

            parser.parse();
        }
Beispiel #16
0
		// <remarks>
		//   This is used when we encounter a #line preprocessing directive.
		// </remarks>
		static public SourceFile LookupFile (CompilationUnit comp_unit, string name)
		{
			string path;
			if (!Path.IsPathRooted (name)) {
				string root = Path.GetDirectoryName (comp_unit.Path);
				path = Path.Combine (root, name);
			} else
				path = name;

			if (!source_files.ContainsKey (path)) {
				if (source_count >= (1 << checkpoint_bits))
					return new SourceFile (name, path, 0, true);

				source_files.Add (path, ++source_count);
				SourceFile retval = new SourceFile (name, path, source_count, true);
				source_list.Add (retval);
				return retval;
			}

			int index = (int) source_files [path];
			return (SourceFile) source_list [index - 1];
		}
Beispiel #17
0
		// <summary>
		//   This must be called before parsing/tokenizing any files.
		// </summary>
		static public void AddFile (Report r, string name)
		{
			string path = Path.GetFullPath (name);
			int id;
			if (source_files.TryGetValue (path, out id)){
				string other_name = source_list [id - 1].Name;
				if (name.Equals (other_name))
					r.Warning (2002, 1, "Source file `{0}' specified multiple times", other_name);
				else
					r.Warning (2002, 1, "Source filenames `{0}' and `{1}' both refer to the same file: {2}", name, other_name, path);
				return;
			}

			source_files.Add (path, ++source_count);
			CompilationUnit unit = new CompilationUnit (name, path, source_count);
			source_list.Add (unit);
			compile_units.Add (unit);
		}
Beispiel #18
0
 static public void Push(CompilationUnit compile_unit, SourceFile file)
 {
     current_source       = file != null ? file.Index : -1;
     current_compile_unit = compile_unit != null ? compile_unit.Index : -1;
     // File is always pushed before being changed.
 }
Beispiel #19
0
		public static CompilerCompilationUnit ParseFile (string[] args, Stream input, string inputFile, ReportPrinter reportPrinter)
		{
			lock (parseLock) {
				try {
					Driver d = Driver.Create (args, false, reportPrinter);
					if (d == null)
						return null;
	
					Location.AddFile (null, inputFile);
					Location.Initialize ();
	
					// TODO: encoding from driver
					SeekableStreamReader reader = new SeekableStreamReader (input, Encoding.Default);
	
					CompilerContext ctx = new CompilerContext (new ReflectionMetaImporter (), new Report (reportPrinter));
					
					RootContext.ToplevelTypes = new ModuleCompiled (ctx, false /* isUnsafe */);
					CompilationUnit unit = null;
					try {
						unit = (CompilationUnit) Location.SourceFiles [0];
					} catch (Exception) {
						string path = Path.GetFullPath (inputFile);
						unit = new CompilationUnit (inputFile, path, 0);
					}
					CSharpParser parser = new CSharpParser (reader, unit, ctx);
					parser.Lexer.TabSize = 1;
					parser.Lexer.sbag = new SpecialsBag ();
					parser.LocationsBag = new LocationsBag ();
					parser.UsingsBag = new UsingsBag ();
					parser.parse ();
					
					return new CompilerCompilationUnit () { ModuleCompiled = RootContext.ToplevelTypes, LocationsBag = parser.LocationsBag, UsingsBag = parser.UsingsBag, SpecialsBag = parser.Lexer.sbag };
				} finally {
					Reset ();
				}
			}
		}
		public Tokenizer (SeekableStreamReader input, CompilationUnit file, CompilerContext ctx)
		{
			this.ref_name = file;
			this.file_name = file;
			this.context = ctx;
			reader = input;
			
			putback_char = -1;

			xml_comment_buffer = new StringBuilder ();

			//
			// FIXME: This could be `Location.Push' but we have to
			// find out why the MS compiler allows this
			//
			Mono.CSharp.Location.Push (file, file);
		}
 public void Parse(SeekableStreamReader reader, CompilationUnit file, ModuleContainer module)
 {
     CSharpParser parser = new CSharpParser (reader, file, module);
     parser.parse ();
 }
Beispiel #22
0
public CSharpParser (SeekableStreamReader reader, CompilationUnit file, ModuleContainer module)
	: this (reader, file, module, new NamespaceEntry (module, null, file, null))
{
}
		private NamespaceEntry (NamespaceEntry parent, CompilationUnit file, Namespace ns, bool slave)
		{
			this.parent = parent;
			this.file = file;
			this.IsImplicit = true;
			this.ns = ns;
			this.SlaveDeclSpace = slave ? new RootDeclSpace (this) : null;
		}
Beispiel #24
0
		public static CompilerCompilationUnit ParseFile (string[] args, Stream input, string inputFile, ReportPrinter reportPrinter)
		{
			lock (parseLock) {
				try {
//                                     Driver d = Driver.Create (args, false, null, reportPrinter);
//                                     if (d == null)
//                                             return null;
       
                                       var r = new Report (reportPrinter);
                                       CommandLineParser cmd = new CommandLineParser (r, Console.Out);
                                       var setting = cmd.ParseArguments (args);
                                       if (setting == null || r.Errors > 0)
						return null;

                                       CompilerContext ctx = new CompilerContext (setting, r);
                                       var d = new Driver (ctx);

					Location.AddFile (null, inputFile);
					Location.Initialize ();
	
					// TODO: encoding from driver
					SeekableStreamReader reader = new SeekableStreamReader (input, Encoding.Default);
					
					RootContext.ToplevelTypes = new ModuleContainer (ctx);
					CompilationUnit unit = null;
					try {
						unit = (CompilationUnit) Location.SourceFiles [0];
					} catch (Exception) {
						string path = Path.GetFullPath (inputFile);
						unit = new CompilationUnit (inputFile, path, 0);
					}
					CSharpParser parser = new CSharpParser (reader, unit, RootContext.ToplevelTypes);
					parser.Lexer.TabSize = 1;
					parser.Lexer.sbag = new SpecialsBag ();
					parser.LocationsBag = new LocationsBag ();
					parser.UsingsBag = new UsingsBag ();
					parser.parse ();
					
					return new CompilerCompilationUnit () { ModuleCompiled = RootContext.ToplevelTypes, LocationsBag = parser.LocationsBag, UsingsBag = parser.UsingsBag, SpecialsBag = parser.Lexer.sbag };
				} finally {
					Reset ();
				}
			}
		}