Example #1
0
		public void Simple ()
		{
			//string content = @"class A { }";
			string content = @"

class Foo
{
	void Bar ()
	{
completionList.Add (""delegate"" + sb, ""md-keyword"", GettextCatalog.GetString (""Creates anonymous delegate.""), ""delegate"" + sb + "" {"" + Document.Editor.EolMarker + stateTracker.Engine.ThisLineIndent + TextEditorProperties.IndentString + ""|"" + Document.Editor.EolMarker + stateTracker.Engine.ThisLineIndent +""};"");
	}
}"
	;


			var stream = new MemoryStream (Encoding.UTF8.GetBytes (content));

			var ctx = new CompilerContext (new CompilerSettings (), new AssertReportPrinter ());

			ModuleContainer module = new ModuleContainer (ctx);
			var file = new SourceFile ("test", "asdfas", 0);
			CSharpParser parser = new CSharpParser (
				new SeekableStreamReader (stream, Encoding.UTF8),
				new CompilationSourceFile (module, file),
				ctx.Report,
				new ParserSession ());

			RootContext.ToplevelTypes = module;
			Location.Initialize (new List<SourceFile> { file });
			parser.parse ();

			Assert.AreEqual (0, ctx.Report.Errors);

			module.Accept (new TestVisitor ());
		}
Example #2
0
		void tokenize_file (SourceFile sourceFile, ModuleContainer module)
		{
			Stream input;

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

			using (input){
				SeekableStreamReader reader = new SeekableStreamReader (input, ctx.Settings.Encoding);
				var file = new CompilationSourceFile (module, sourceFile);

				Tokenizer lexer = new Tokenizer (reader, file);
				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;
		}
        public static void Parse(SeekableStreamReader reader, SourceFile sourceFile, ModuleContainer module, ParserSession session, Report report)
        {
            var file = new CompilationSourceFile(module, sourceFile);
            module.AddTypeContainer(file);

            CSharpParser parser = new CSharpParser(reader, file, report, session);
            parser.parse();
        }
Example #4
0
		public void AddFile (SourceFile file)
		{
			if (include_files == null)
				include_files = new Hashtable ();

			if (!include_files.Contains (file.Path))
				include_files.Add (file.Path, file);
		}
Example #5
0
		void tokenize_file (SourceFile sourceFile, ModuleContainer module, ParserSession session)
		{
			Stream input;

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

			using (input) {
				SeekableStreamReader reader = new SeekableStreamReader (input, ctx.Settings.Encoding);
				var file = new CompilationSourceFile (module, sourceFile);

				if (sourceFile.FileType == SourceFileType.CSharp) {
					Tokenizer lexer = new Tokenizer (reader, file, session);
					int token, tokens = 0, errors = 0;
	
					while ((token = lexer.token ()) != Token.EOF){
						tokens++;
						if (token == Token.ERROR)
							errors++;
					}
				} else {
					Mono.PlayScript.Tokenizer lexer = new Mono.PlayScript.Tokenizer (reader, file, session);
					lexer.ParsingPlayScript = sourceFile.PsExtended;
					int token, tokens = 0, errors = 0;
	
					while ((token = lexer.token ()) != Mono.PlayScript.Token.EOF){
						tokens++;
						if (token == Mono.PlayScript.Token.ERROR)
							errors++;
					}
				}
			}
			
			return;
		}
Example #6
0
		public static void GenerateDynamicPartialClasses(ModuleContainer module, ParserSession session, Report report)
		{
			List<Class> classes = new List<Class>();
			FindDynamicClasses(module, classes);

			if (classes.Count == 0)
				return;

			var os = new StringWriter();

			os.Write (@"
// Generated dynamic class partial classes

");

			foreach (var cl in classes) {
				os.Write (@"
namespace {1} {{

	partial class {2} : PlayScript.IDynamicClass {{

		private PlayScript.IDynamicClass __dynamicProps;

		dynamic PlayScript.IDynamicClass.__GetDynamicValue(string name) {{
			object value = null;
			if (__dynamicProps != null) {{
				value = __dynamicProps.__GetDynamicValue(name);
			}}
			return value;
		}}

		bool PlayScript.IDynamicClass.__TryGetDynamicValue(string name, out object value) {{
			if (__dynamicProps != null) {{
				return __dynamicProps.__TryGetDynamicValue(name, out value);
			}} else {{
				value = null;
				return false;
			}}
		}}
			
		void PlayScript.IDynamicClass.__SetDynamicValue(string name, object value) {{
			if (__dynamicProps == null) {{
				__dynamicProps = new PlayScript.DynamicProperties(this);
			}}
			__dynamicProps.__SetDynamicValue(name, value);
		}}

		bool PlayScript.IDynamicClass.__DeleteDynamicValue(object name) {{
			if (__dynamicProps != null) {{
				return __dynamicProps.__DeleteDynamicValue(name);
			}}
			return false;
		}}
			
		bool PlayScript.IDynamicClass.__HasDynamicValue(string name) {{
			if (__dynamicProps != null) {{
				return __dynamicProps.__HasDynamicValue(name);
			}}
			return false;
		}}

		System.Collections.IEnumerable PlayScript.IDynamicClass.__GetDynamicNames() {{
			if (__dynamicProps != null) {{
				return __dynamicProps.__GetDynamicNames();
			}}
			return null;
		}}
	}}
}}

", PsConsts.PsRootNamespace, ((ITypeDefinition)cl).Namespace, cl.MemberName.Basename);
			}

			string fileStr = os.ToString();
			var path = System.IO.Path.Combine (System.IO.Path.GetDirectoryName(System.IO.Path.GetFullPath(module.Compiler.Settings.OutputFile)), "dynamic.g.cs");
			System.IO.File.WriteAllText(path, fileStr);

			byte[] byteArray = Encoding.ASCII.GetBytes( fileStr );
			var input = new MemoryStream( byteArray, false );
			var reader = new SeekableStreamReader (input, System.Text.Encoding.UTF8);

			SourceFile file = new SourceFile(path, path, 0);
			file.FileType = SourceFileType.CSharp;

			Driver.Parse (reader, file, module, session, report);

		}
Example #7
0
		public static void GenerateEmbedClasses(ModuleContainer module, ParserSession session, Report report)
		{
			List<EmbedData> embeds = new List<EmbedData>();
			FindEmbedClasses(module, module, embeds);
			if (embeds.Count == 0)
				return;

			var os = new StringWriter();
			
			os.Write (@"
// Generated embed loader classes

");

			foreach (var e in embeds) {

				var loc = e._field.Location;

				e._field.Initializer = new TypeOf(new MemberAccess(new SimpleName("_embed_loaders", loc), e._className), loc);

				os.Write (@"
namespace _embed_loaders {{

	internal class {1} : PlayScript.EmbedLoader {{

		public {1}() : base({2}, {3}, {4}, {5}, {6}) {{
		}}
	}}
}}

", PsConsts.PsRootNamespace, e._className, e.source, e.mimeType, e.embedAsCFF, e.fontFamily, e.symbol);
			}
			
			string fileStr = os.ToString();
			var path = System.IO.Path.Combine (System.IO.Path.GetDirectoryName(System.IO.Path.GetFullPath(module.Compiler.Settings.OutputFile)), "embed.g.cs");
			System.IO.File.WriteAllText(path, fileStr);
			
			byte[] byteArray = Encoding.ASCII.GetBytes( fileStr );
			var input = new MemoryStream( byteArray, false );
			var reader = new SeekableStreamReader (input, System.Text.Encoding.UTF8);
			
			SourceFile file = new SourceFile(path, path, 0);
			file.FileType = SourceFileType.CSharp;
			
			Driver.Parse (reader, file, module, session, report);
		}
Example #8
0
 public static void AddFile(SourceFile file)
 {
     source_list.Add(file);
 }
		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);
		}
Example #10
0
        // Summary:
        //     Compiles an assembly from the specified array of strings containing source code,
        //     using the specified compiler settings.
        //
        // Parameters:
        //   options:
        //     A System.CodeDom.Compiler.CompilerParameters object that indicates the settings
        //     for compilation.
        //
        //   sources:
        //     The source code strings to compile.
        //
        // Returns:
        //     A System.CodeDom.Compiler.CompilerResults object that indicates the results of
        //     compilation.
        public CompilerResults CompileAssemblyFromSourceBatch(CompilerParameters options, string[] sources)
        {
            var settings = ParamsToSettings(options);

            int i = 0;
            foreach (var _source in sources)
            {
                var source = _source;
                Func<Stream> getStream = () => { return new MemoryStream(Encoding.UTF8.GetBytes(source ?? "")); };
                var fileName = i.ToString();
                var unit = new SourceFile(fileName, fileName, settings.SourceFiles.Count + 1, getStream);
                settings.SourceFiles.Add(unit);
                i++;
            }

            return CompileFromCompilerSettings(settings, options.GenerateInMemory);
        }
Example #11
0
 public CompilationSourceFile(ModuleContainer parent, SourceFile sourceFile)
     : this(parent)
 {
     this.file = sourceFile;
 }
Example #12
0
		public void Parse (SourceFile 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, ctx.Settings.Encoding);

			Parse (reader, file, module);

			if (ctx.Settings.GenerateDebugInfo && ctx.Report.Errors == 0 && !file.HasChecksum) {
				input.Position = 0;
				if (md5 == null)
					md5 = MD5.Create ();

				file.SetChecksum (md5.ComputeHash (input));
			}

			reader.Dispose ();
			input.Close ();
		}
Example #13
0
		public void Parse (SourceFile 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, ctx.Settings.Encoding);

			Parse (reader, file, module);
			reader.Dispose ();
			input.Close ();
		}
Example #14
0
		public CSharpParser Parse (SeekableStreamReader reader, SourceFile sourceFile, ModuleContainer module)
		{
			var file = new CompilationSourceFile (module, sourceFile);
			module.AddTypeContainer (file);

			CSharpParser parser = new CSharpParser (reader, file);
			parser.Lexer.sbag = new SpecialsBag ();
			parser.parse ();
			return parser;
		}
Example #15
0
		public static void Parse (SeekableStreamReader reader, SourceFile sourceFile, ModuleContainer module, ParserSession session, Report report)
		{
			var file = new CompilationSourceFile (module, sourceFile);
			module.AddTypeContainer (file);

			if (sourceFile.FileType == SourceFileType.CSharp) {
				CSharpParser parser = new CSharpParser (reader, file, session);
				parser.parse ();
			} else {
				PlayScriptParser parser = new PlayScriptParser (reader, file, session);
				parser.parsing_playscript = sourceFile.PsExtended;
				parser.parse ();
			}
		}
Example #16
0
		public void AddFile (SourceFile file)
		{
			if (file == this)
				return;
			
			if (include_files == null)
				include_files = new Dictionary<string, SourceFile> ();

			if (!include_files.ContainsKey (file.Path))
				include_files.Add (file.Path, file);
		}
Example #17
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.
		}
Example #18
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];
		}
Example #19
0
		public void Parse (SourceFile file, ModuleContainer module, ParserSession session, Report report)
		{
			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, ctx.Settings.Encoding, session.StreamReaderBuffer);

			Parse (reader, file, module, session, report);

			if (ctx.Settings.GenerateDebugInfo && report.Errors == 0 && !file.HasChecksum) {
				input.Position = 0;
				var checksum = session.GetChecksumAlgorithm ();
				file.SetChecksum (checksum.ComputeHash (input));
			}

			reader.Dispose ();
			input.Close ();
		}
Example #20
0
		SyntaxTree Parse(ITextSource program, string fileName, int initialLine, int initialColumn)
		{
			lock (parseLock) {
				errorReportPrinter = new ErrorReportPrinter("");
				var ctx = new CompilerContext(compilerSettings.ToMono(), errorReportPrinter);
				ctx.Settings.TabSize = 1;
				var reader = new SeekableStreamReader(program);
				var file = new SourceFile(fileName, fileName, 0);
				Location.Initialize(new List<SourceFile>(new [] { file }));
				var module = new ModuleContainer(ctx);
				var session = new ParserSession();
				session.LocationsBag = new LocationsBag();
				var report = new Report(ctx, errorReportPrinter);
				var parser = Driver.Parse(reader, file, module, session, report, initialLine - 1, initialColumn - 1);
				var top = new CompilerCompilationUnit {
					ModuleCompiled = module,
					LocationsBag = session.LocationsBag,
					SpecialsBag = parser.Lexer.sbag,
					Conditionals = parser.Lexer.SourceFile.Conditionals
				};
				var unit = Parse(top, fileName);
				unit.Errors.AddRange(errorReportPrinter.Errors);
				CompilerCallableEntryPoint.Reset();
				return unit;
			}
		}
Example #21
0
 static public void Push(CompilationSourceFile 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.
 }
Example #22
0
		//
		// This is used when we encounter a #line preprocessing directive during parsing
		// to register additional source file names
		//
		public SourceFile LookupFile (CompilationSourceFile comp_unit, string name)
		{
			if (all_source_files == null) {
				all_source_files = new Dictionary<string, SourceFile> ();
				foreach (var source in SourceFiles)
					all_source_files[source.FullPathName] = source;
			}

			string path;
			if (!Path.IsPathRooted (name)) {
				string root = Path.GetDirectoryName (comp_unit.SourceFile.FullPathName);
				path = Path.Combine (root, name);
			} else
				path = name;

			SourceFile retval;
			if (all_source_files.TryGetValue (path, out retval))
				return retval;

			retval = new SourceFile (name, path, all_source_files.Count + 1);
			Location.AddFile (retval);
			all_source_files.Add (path, retval);
			return retval;
		}
Example #23
0
		public CompilationUnit Parse(Stream stream, string fileName, int lineModifier = 0)
		{
			lock (parseLock) {
				errorReportPrinter = new ErrorReportPrinter ("");
				var ctx = new CompilerContext (CompilerSettings, errorReportPrinter);
				ctx.Settings.TabSize = 1;
				var reader = new SeekableStreamReader (stream, Encoding.UTF8);
				var file = new SourceFile (fileName, fileName, 0);
				Location.Initialize (new List<SourceFile> (new [] { file }));
				var module = new ModuleContainer (ctx);
				var parser = Driver.Parse (reader, file, module, lineModifier);
				
				var top = new CompilerCompilationUnit () { 
					ModuleCompiled = module,
					LocationsBag = parser.LocationsBag,
					SpecialsBag = parser.Lexer.sbag
				};
				var unit = Parse (top, fileName, lineModifier);		
				unit.Errors.AddRange (errorReportPrinter.Errors);
				CompilerCallableEntryPoint.Reset ();
				return unit;
			}
		}
Example #24
0
 static public void Push(SourceFile file)
 {
     current_source = file != null ? file.Index : -1;
     // File is always pushed before being changed.
 }
Example #25
0
        public void AddIncludeFile(SourceFile file)
        {
            if (file == this.file)
                return;

            if (include_files == null)
                include_files = new Dictionary<string, SourceFile> ();

            if (!include_files.ContainsKey (file.FullPathName))
                include_files.Add (file.FullPathName, file);
        }
Example #26
0
        // Summary:
        //     Compiles an assembly from the source code contained within the specified files,
        //     using the specified compiler settings.
        //
        // Parameters:
        //   options:
        //     A System.CodeDom.Compiler.CompilerParameters object that indicates the settings
        //     for compilation.
        //
        //   fileNames:
        //     The file names of the files to compile.
        //
        // Returns:
        //     A System.CodeDom.Compiler.CompilerResults object that indicates the results of
        //     compilation.
        public CompilerResults CompileAssemblyFromFileBatch(CompilerParameters options, string[] fileNames)
        {
            var settings = ParamsToSettings(options);

            foreach (var fileName in fileNames)
            {
                string path = Path.GetFullPath(fileName);
                var unit = new SourceFile(fileName, path, settings.SourceFiles.Count + 1);
                settings.SourceFiles.Add(unit);
            }

            return CompileFromCompilerSettings(settings, options.GenerateInMemory);
        }
Example #27
0
		/*
		/// <summary>
		/// Parses a file snippet; guessing what the code snippet represents (whole file, type members, block, type reference, expression).
		/// </summary>
		public AstNode ParseSnippet (string code)
		{
			// TODO: add support for parsing a part of a file
			throw new NotImplementedException ();
		}
		 */
		public DocumentationReference ParseDocumentationReference(string cref)
		{
			// see Mono.CSharp.DocumentationBuilder.HandleXrefCommon
			if (cref == null)
				throw new ArgumentNullException("cref");
			
			// Additional symbols for < and > are allowed for easier XML typing
			cref = cref.Replace('{', '<').Replace('}', '>');
			
			lock (parseLock) {
				errorReportPrinter = new ErrorReportPrinter("");
				var ctx = new CompilerContext(compilerSettings.ToMono(), errorReportPrinter);
				ctx.Settings.TabSize = 1;
				var reader = new SeekableStreamReader(new StringTextSource(cref));
				var file = new SourceFile("", "", 0);
				Location.Initialize(new List<SourceFile>(new [] { file }));
				var module = new ModuleContainer(ctx);
				module.DocumentationBuilder = new DocumentationBuilder(module);
				var source_file = new CompilationSourceFile(module);
				var report = new Report(ctx, errorReportPrinter);
				var session = new ParserSession();
				session.LocationsBag = new LocationsBag();
				var parser = new Mono.CSharp.CSharpParser(reader, source_file, report, session);
				parser.Lexer.Line += initialLocation.Line - 1;
				parser.Lexer.Column += initialLocation.Column - 1;
				parser.Lexer.putback_char = Tokenizer.DocumentationXref;
				parser.Lexer.parsing_generic_declaration_doc = true;
				parser.parse();
				if (report.Errors > 0) {
//					Report.Warning (1584, 1, mc.Location, "XML comment on `{0}' has syntactically incorrect cref attribute `{1}'",
//					                mc.GetSignatureForError (), cref);
				}
				
				var conversionVisitor = new ConversionVisitor(false, session.LocationsBag);
				var docRef = conversionVisitor.ConvertXmlDoc(module.DocumentationBuilder);
				CompilerCallableEntryPoint.Reset();
				return docRef;
			}
		}
Example #28
0
        public static CSharpParser Parse(SeekableStreamReader reader, SourceFile sourceFile, ModuleContainer module, ParserSession session, Report report, int lineModifier = 0, int colModifier = 0)
        {
            var file = new CompilationSourceFile (module, sourceFile);
            module.AddTypeContainer(file);

            CSharpParser parser = new CSharpParser (reader, file, report, session);
            parser.Lexer.Line += lineModifier;
            parser.Lexer.Column += colModifier;
            parser.Lexer.sbag = new SpecialsBag ();
            parser.parse ();
            return parser;
        }
Example #29
0
		void AddSourceFile (string fileName, List<SourceFile> sourceFiles)
		{
			string path = Path.GetFullPath (fileName);

			int index;
			if (source_file_index.TryGetValue (path, out index)) {
				string other_name = sourceFiles[index - 1].Name;
				if (fileName.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}", fileName, other_name, path);

				return;
			}

			var unit = new SourceFile (fileName, path, sourceFiles.Count + 1);
			sourceFiles.Add (unit);
			source_file_index.Add (path, unit.Index);
		}
		//
		// Handles the #line directive
		//
		bool PreProcessLine (string arg)
		{
			if (arg.Length == 0)
				return false;

			if (arg == "default"){
				ref_line = line;
				ref_name = file_name;
				hidden = false;
				Location.Push (file_name, ref_name);
				return true;
			} else if (arg == "hidden"){
				hidden = true;
				return true;
			}
			
			try {
				int pos;

				if ((pos = arg.IndexOf (' ')) != -1 && pos != 0){
					ref_line = System.Int32.Parse (arg.Substring (0, pos));
					pos++;
					
					char [] quotes = { '\"' };
					
					string name = arg.Substring (pos). Trim (quotes);
					ref_name = Location.LookupFile (file_name, name);
					file_name.AddFile (ref_name);
					hidden = false;
					Location.Push (file_name, ref_name);
				} else {
					ref_line = System.Int32.Parse (arg);
					hidden = false;
				}
			} catch {
				return false;
			}
			
			return true;
		}