private Token GetTokenFor(string terminator) { Guid dummy = Parser.dummyGuid; Document document = new Document(null, 1, terminator, dummy, dummy, dummy); Scanner scanner = new Scanner(document, new ErrorNodeList(), null); this.currentToken = Token.None; return scanner.GetNextToken(); }
public CompilationUnit ParseCompilationUnit(string source, string fname, CompilerParameters parameters, ErrorNodeList errors, AuthoringSink sink){ Guid dummy = Parser.dummyGuid; Document document = new Document(fname, 1, source, dummy, dummy, dummy); this.errors = errors; this.scanner = new Scanner(document, errors, parameters as SpecSharpCompilerOptions); this.currentToken = Token.None; this.errors = errors; this.ProcessOptions(parameters as SpecSharpCompilerOptions); CompilationUnit cu = new CompilationUnit(Identifier.For(fname)); cu.Compilation = new Compilation(this.module, new CompilationUnitList(cu), parameters, null); cu.SourceContext = new SourceContext(document); this.ParseCompilationUnit(cu, false, true, sink);//This overload is only called for intellisense, not the background error check. cu.PragmaWarnInformation = this.scanner.pragmaInformation; this.errors = null; this.scanner = null; return cu; }
public Parser(Document document, ErrorNodeList errors, Module symbolTable, SpecSharpCompilerOptions options){ this.scanner = new Scanner(document, errors, options); this.ProcessOptions(options); this.errors = errors; this.module = symbolTable; }
public SourceContext(Document document, int startPos, int endPos) { this.Document = document; this.StartPos = startPos; this.EndPos = endPos; }
public SourceContext(Document/*!*/ document, int startLine, int startColumn, int endLine, int endColumn) { this.Document = document; this.Document.GetOffsets(startLine, startColumn, endLine, endColumn, out this.StartPos, out this.EndPos); }
internal Scanner(Document document, ErrorNodeList errors, CompilerOptions options) { this.document = document; this.originalDocument = document; this.sourceText = document.Text; this.endPos = 0; this.maxPos = document.Text.Length; this.errors = errors; this.lastReportedErrorPos = 0; this.PreprocessorDefinedSymbols = new Hashtable(); this.PreprocessorDefinedSymbols["true"] = "true"; this.PreprocessorDefinedSymbols["false"] = null; if (options != null) { StringList syms = options.DefinedPreProcessorSymbols; for (int i = 0, n = syms == null ? 0 : syms.Count; i < n; i++) { string sym = syms[i]; if (sym == null) continue; this.PreprocessorDefinedSymbols[sym] = sym; } } }
public SourceContext(Document document) : this(document, 0, document == null ? 0 : (document.Text == null ? 0 : document.Text.Length)) { }
private bool ScanPreProcessorDirective(bool stopAtEndOfLine, bool insideExcludedBlock, bool atTopLevel) { bool exclude = insideExcludedBlock; int savedStartPos = this.startPos; int i = this.startPos - 1; while (i > 0 && Scanner.IsBlankSpace(this.GetChar(i))) { i--; } if (i > 0 && !this.IsLineTerminator(this.GetChar(i), 0)) { this.HandleError(Error.BadDirectivePlacement); goto skipToEndOfLine; } this.SkipBlanks(); //Check EOL/EOF? this.startPos = this.endPos - 1; this.ScanIdentifier(); switch (this.GetIdentifierString()) { case "define": if (insideExcludedBlock) goto skipToEndOfLine; if (stopAtEndOfLine) break; if (!this.allowPPDefinitions) { this.HandleError(Error.PPDefFollowsToken); goto skipToEndOfLine; } this.startPos = this.endPos; char chr = this.SkipBlanks(); if (this.IsEndLineOrEOF(chr, 0)) { this.HandleError(Error.ExpectedIdentifier); break; } this.identifier.Length = 0; this.endPos--; this.startPos = this.endPos; if (!this.IsIdentifierStartChar(chr)) this.HandleError(Error.ExpectedIdentifier); else { this.ScanIdentifier(); if (this.PreprocessorDefinedSymbols == null) this.PreprocessorDefinedSymbols = new Hashtable(); string s = this.GetIdentifierString(); if (s == "true" || s == "false" || !this.IsIdentifierStartChar(s[0])) { this.HandleError(Error.ExpectedIdentifier); goto skipToEndOfLine; } else this.PreprocessorDefinedSymbols[s] = s; } break; case "undef": if (insideExcludedBlock) goto skipToEndOfLine; if (stopAtEndOfLine) break; if (!this.allowPPDefinitions) { this.HandleError(Error.PPDefFollowsToken); goto skipToEndOfLine; } this.startPos = this.endPos; chr = this.SkipBlanks(); if (this.IsEndLineOrEOF(chr, 0)) { this.HandleError(Error.ExpectedIdentifier); break; } this.identifier.Length = 0; this.endPos--; this.startPos = this.endPos; if (!this.IsIdentifierStartChar(chr)) this.HandleError(Error.ExpectedIdentifier); else { this.ScanIdentifier(); if (this.PreprocessorDefinedSymbols == null) { this.PreprocessorDefinedSymbols = new Hashtable(); this.PreprocessorDefinedSymbols["true"] = "true"; } string s = this.GetIdentifierString(); if (s == "true" || s == "false" || !this.IsIdentifierStartChar(s[0])) { this.HandleError(Error.ExpectedIdentifier); goto skipToEndOfLine; } else this.PreprocessorDefinedSymbols[s] = null; } break; case "if": if (insideExcludedBlock) { this.endIfCount++; goto skipToEndOfLine; } if (stopAtEndOfLine) break; char c = (char)0; exclude = !this.ScanPPExpression(ref c); if (!exclude) this.includeCount++; this.endIfCount++; if (this.IsEndLineOrEOF(c, 0)) return exclude; break; case "elif": if (insideExcludedBlock && !atTopLevel) goto skipToEndOfLine; if (stopAtEndOfLine) break; if (this.elseCount == this.endIfCount) { this.HandleError(Error.UnexpectedDirective); goto skipToEndOfLine; } c = (char)0; exclude = !this.ScanPPExpression(ref c); if (this.includeCount == this.endIfCount) { exclude = true; break; } if (!exclude) this.includeCount++; if (this.IsEndLineOrEOF(c, 0)) return exclude; break; case "else": if (insideExcludedBlock && !atTopLevel) goto skipToEndOfLine; if (stopAtEndOfLine) break; if (this.elseCount == this.endIfCount) { this.HandleError(Error.UnexpectedDirective); goto skipToEndOfLine; } this.elseCount++; if (this.includeCount == this.endIfCount) { exclude = true; break; } exclude = false; this.includeCount++; break; case "endif": if (stopAtEndOfLine) break; if (this.endIfCount <= 0) { this.endIfCount = 0; this.HandleError(Error.UnexpectedDirective); goto skipToEndOfLine; } this.elseCount = this.includeCount = --this.endIfCount; break; case "line": if (insideExcludedBlock) goto skipToEndOfLine; if (stopAtEndOfLine) break; c = this.SkipBlanks(); int lnum = -1; if ('0' <= c && c <= '9') { this.startPos = --this.endPos; while ('0' <= (c = this.GetChar(++this.endPos)) && c <= '9') ; try { lnum = int.Parse(this.GetTokenSource(), CultureInfo.InvariantCulture); if (lnum <= 0) { this.startPos = this.endPos; this.HandleError(Error.InvalidLineNumber); goto skipToEndOfLine; } else if (this.IsEndLineOrEOF(c, 0)) goto setLineInfo; } catch (OverflowException) { this.startPos++; this.HandleError(Error.IntOverflow); goto skipToEndOfLine; } } else { this.startPos = this.endPos - 1; this.ScanIdentifier(); if (this.startPos != this.endPos - 1) { string str = this.GetIdentifierString(); if (str == "default") { this.document = this.originalDocument; break; } if (str == "hidden") { this.document = new Document(this.document.Name, this.document.LineNumber, this.document.Text, this.document.DocumentType, this.document.Language, this.document.LanguageVendor); this.document.Hidden = true; break; } } this.HandleError(Error.InvalidLineNumber); goto skipToEndOfLine; } c = this.SkipBlanks(); this.startPos = this.endPos - 1; if (c == '/') { if (this.GetChar(this.endPos) == '/') { this.endPos--; goto setLineInfo; } else { this.startPos = this.endPos - 1; this.HandleError(Error.EndOfPPLineExpected); goto skipToEndOfLine; } } if (c == '"') { while ((c = this.GetChar(this.endPos++)) != '"' && !this.IsEndLineOrEOF(c, 0)) ; if (c != '"') { this.HandleError(Error.MissingPPFile); goto skipToEndOfLine; } this.startPos++; this.endPos--; string filename = this.GetTokenSource(); this.endPos++; this.document = new Document(filename, 1, this.document.Text, this.document.DocumentType, this.document.Language, this.document.LanguageVendor); } else if (!this.IsEndLineOrEOF(c, 0)) { this.HandleError(Error.MissingPPFile); goto skipToEndOfLine; } setLineInfo: this.document = new Document(this.document.Name, 1, this.document.Text, this.document.DocumentType, this.document.Language, this.document.LanguageVendor); int offset = lnum - this.document.GetLine(this.startPos); this.document.LineNumber = offset; if (this.IsEndLineOrEOF(c, 0)) return exclude; break; case "error": if (insideExcludedBlock) goto skipToEndOfLine; if (stopAtEndOfLine) break; this.SkipBlanks(); this.startPos = --this.endPos; this.ScanString((char)0); this.HandleError(Error.ErrorDirective, this.unescapedString); break; case "warning": if (insideExcludedBlock) goto skipToEndOfLine; if (stopAtEndOfLine) break; this.SkipBlanks(); this.startPos = --this.endPos; this.ScanString((char)0); this.HandleError(Error.WarningDirective, this.unescapedString); break; case "region": if (insideExcludedBlock) goto skipToEndOfLine; if (stopAtEndOfLine) break; this.endRegionCount++; goto skipToEndOfLine; case "endregion": if (insideExcludedBlock) goto skipToEndOfLine; if (stopAtEndOfLine) break; if (this.endRegionCount <= 0) this.HandleError(Error.UnexpectedDirective); else this.endRegionCount--; goto skipToEndOfLine; default: if (insideExcludedBlock) goto skipToEndOfLine; if (stopAtEndOfLine) { this.endPos = this.startPos; break; } this.HandleError(Error.PPDirectiveExpected); goto skipToEndOfLine; } if (stopAtEndOfLine) { this.startPos = savedStartPos; return false; } char ch = this.SkipBlanks(); if (this.IsEndLineOrEOF(ch, 0)) return exclude; if (ch == '/' && (ch = this.GetChar(this.endPos++)) == '/') goto skipToEndOfLine; this.startPos = this.endPos - 1; this.HandleError(Error.EndOfPPLineExpected); skipToEndOfLine: this.SkipSingleLineComment(); return exclude; }
internal Scanner(Document document, ErrorNodeList errors, SpecSharpCompilerOptions options){ this.document = document; this.originalDocument = document; this.sourceText = document.Text; if (document.Text != null) this.sourceString = document.Text.Source; this.endPos = 0; this.maxPos = document.Text.Length; this.errors = errors; this.ignoreDocComments = true; this.SetOptions(options); }
private bool ScanPreProcessorDirective(bool insideExcludedBlock, int nonExcludedEndifCount){ bool exclude = insideExcludedBlock; int savedStartPos = this.startPos; int i = this.startPos-1; while (i > 0 && Scanner.IsBlankSpace(this.GetChar(i))){ i--; } if (i > 0 && !this.IsLineTerminator(this.GetChar(i), 0)){ this.HandleError(Error.BadDirectivePlacement); goto skipToEndOfLine; } this.SkipBlanks(); //Check EOL/EOF? this.startPos = this.endPos-1; this.ScanIdentifier(); switch (this.GetIdentifierString()){ case "define": if (insideExcludedBlock) goto skipToEndOfLine; if (!this.allowPPDefinitions){ this.HandleError(Error.PPDefFollowsToken); goto skipToEndOfLine; } this.startPos = this.endPos; char chr = this.SkipBlanks(); if (this.IsEndLineOrEOF(chr, 0)){ this.HandleError(Error.ExpectedIdentifier); break; } this.identifier.Length = 0; this.endPos--; this.startPos = this.endPos; if (!this.IsIdentifierStartChar(chr)) this.HandleError(Error.ExpectedIdentifier); else{ this.ScanIdentifier(); if (this.PreprocessorDefinedSymbols == null){ this.PreprocessorDefinedSymbols = new Hashtable(); this.PreprocessorDefinedSymbols["true"] = "true"; } string s = this.GetIdentifierString(); if (s == "true" || s == "false" || !this.IsIdentifierStartChar(s[0])){ this.HandleError(Error.ExpectedIdentifier); goto skipToEndOfLine; }else this.PreprocessorDefinedSymbols[s] = s; } break; case "undef": if (insideExcludedBlock) goto skipToEndOfLine; if (!this.allowPPDefinitions){ this.HandleError(Error.PPDefFollowsToken); goto skipToEndOfLine; } this.startPos = this.endPos; chr = this.SkipBlanks(); if (this.IsEndLineOrEOF(chr, 0)){ this.HandleError(Error.ExpectedIdentifier); break; } this.identifier.Length = 0; this.endPos--; this.startPos = this.endPos; if (!this.IsIdentifierStartChar(chr)) this.HandleError(Error.ExpectedIdentifier); else{ this.ScanIdentifier(); if (this.PreprocessorDefinedSymbols == null){ this.PreprocessorDefinedSymbols = new Hashtable(); this.PreprocessorDefinedSymbols["true"] = "true"; } string s = this.GetIdentifierString(); if (s == "true" || s == "false" || !this.IsIdentifierStartChar(s[0])){ this.HandleError(Error.ExpectedIdentifier); goto skipToEndOfLine; }else this.PreprocessorDefinedSymbols[s] = null; } break; case "if": if (insideExcludedBlock){ this.endIfCount++; this.RestartStateHasChanged = true; goto skipToEndOfLine; } char c = (char)0; exclude = !this.ScanPPExpression(ref c); if (this.sink != null) { if (this.regionCtxStack == null) this.regionCtxStack = new Stack(); SourceContext regionCtx = this.CurrentSourceContext; regionCtx.StartPos = this.PositionOfFirstCharacterOfNextLine(this.endPos); regionCtx.EndPos = regionCtx.StartPos; regionCtxStack.Push(regionCtx); } if (!exclude) this.includeCount++; this.endIfCount++; this.nonExcludedEndIfCount++; this.RestartStateHasChanged = true; if (this.IsEndLineOrEOF(c, 0)) return exclude; break; case "elif": if (insideExcludedBlock && (this.endIfCount - nonExcludedEndifCount) > 1) goto skipToEndOfLine; if (this.elseCount == this.endIfCount){ //Already found an else this.HandleError(Error.UnexpectedDirective); goto skipToEndOfLine; } c = (char)0; exclude = !this.ScanPPExpression(ref c); if (this.sink != null && this.regionCtxStack != null && this.regionCtxStack.Count > 0) { SourceContext startRegionCtx = (SourceContext)regionCtxStack.Pop(); startRegionCtx.EndPos = this.PositionOfLastCharacterOfPreviousLine(savedStartPos)+1; if (startRegionCtx.EndPos > startRegionCtx.StartPos) this.sink.AddCollapsibleRegion(startRegionCtx, insideExcludedBlock); SourceContext regionCtx = this.CurrentSourceContext; regionCtx.StartPos = this.endPos; regionCtx.StartPos = this.PositionOfFirstCharacterOfNextLine(this.endPos); regionCtx.EndPos = regionCtx.StartPos; regionCtxStack.Push(regionCtx); } if (this.includeCount == this.endIfCount){ //The #if, or a preceding #elif has already been included, hence this block must be excluded regardless of the expression exclude = true; break; } if (!exclude){ this.includeCount++; this.RestartStateHasChanged = true; } if (this.IsEndLineOrEOF(c, 0)) return exclude; break; case "else": if (insideExcludedBlock && (this.endIfCount - nonExcludedEndifCount) > 1) goto skipToEndOfLine; if (this.elseCount == this.endIfCount){ this.HandleError(Error.UnexpectedDirective); goto skipToEndOfLine; } if (this.sink != null && this.regionCtxStack != null && this.regionCtxStack.Count > 0) { SourceContext startRegionCtx = (SourceContext)regionCtxStack.Pop(); startRegionCtx.EndPos = this.PositionOfLastCharacterOfPreviousLine(savedStartPos)+1; if (startRegionCtx.EndPos > startRegionCtx.StartPos) this.sink.AddCollapsibleRegion(startRegionCtx, insideExcludedBlock); SourceContext regionCtx = this.CurrentSourceContext; regionCtx.StartPos = this.PositionOfFirstCharacterOfNextLine(this.endPos); regionCtx.EndPos = regionCtx.StartPos; regionCtxStack.Push(regionCtx); } this.elseCount++; this.RestartStateHasChanged = true; if (this.includeCount == this.endIfCount){ exclude = true; break; } exclude = false; this.includeCount++; break; case "endif": if (this.endIfCount <= 0){ this.endIfCount = 0; this.nonExcludedEndIfCount = 0; this.RestartStateHasChanged = true; this.HandleError(Error.UnexpectedDirective); goto skipToEndOfLine; } bool collapse = false; this.endIfCount--; if (!insideExcludedBlock || this.nonExcludedEndIfCount > this.endIfCount) this.nonExcludedEndIfCount--; if (this.includeCount > this.endIfCount){ //Can only happen if the #if-#else-#elif-#end block itself appears in an included block and has an included part this.includeCount = this.endIfCount; collapse = true; } if (this.endIfCount > 0) this.elseCount = this.endIfCount-1; else{ this.elseCount = 0; collapse = true; } if (collapse && this.sink != null && this.regionCtxStack != null && this.regionCtxStack.Count > 0) { SourceContext startRegionCtx = (SourceContext)regionCtxStack.Pop(); startRegionCtx.EndPos = this.PositionOfLastCharacterOfPreviousLine(savedStartPos)+1; if (startRegionCtx.EndPos > startRegionCtx.StartPos) this.sink.AddCollapsibleRegion(startRegionCtx, insideExcludedBlock); if (this.regionCtxStack.Count == 0) this.regionCtxStack = null; } this.RestartStateHasChanged = true; break; case "line": if (insideExcludedBlock) goto skipToEndOfLine; c = this.SkipBlanks(); int lnum = -1; if ('0' <= c && c <= '9'){ this.startPos = --this.endPos; while ('0' <= (c = this.GetChar(++this.endPos)) && c <= '9'); try{ lnum = int.Parse(this.GetTokenSource(), CultureInfo.InvariantCulture); if (lnum <= 0){ this.startPos = this.endPos; this.HandleError(Error.InvalidLineNumber); goto skipToEndOfLine; }else if (this.IsEndLineOrEOF(c, 0)) goto setLineInfo; }catch(OverflowException){ this.startPos++; this.HandleError(Error.IntOverflow); goto skipToEndOfLine; } }else{ this.startPos = this.endPos-1; this.ScanIdentifier(); if (this.startPos != this.endPos-1){ string str = this.GetIdentifierString(); if (str == "default"){ this.document = this.originalDocument; break; } if (str == "hidden" && this.document != null){ this.document = new Document(this.document.Name, this.document.LineNumber, this.document.Text, this.document.DocumentType, this.document.Language, this.document.LanguageVendor); this.document.Hidden = true; break; } } this.HandleError(Error.InvalidLineNumber); goto skipToEndOfLine; } c = this.SkipBlanks(); this.startPos = this.endPos-1; if (c == '/'){ if (this.GetChar(this.endPos) == '/'){ this.endPos--; goto setLineInfo; }else{ this.startPos = this.endPos-1; this.HandleError(Error.EndOfPPLineExpected); goto skipToEndOfLine; } } if (c == '"'){ while ((c = this.GetChar(this.endPos++)) != '"' && !this.IsEndLineOrEOF(c, 0)); if (c != '"'){ this.HandleError(Error.MissingPPFile); goto skipToEndOfLine; } this.startPos++; this.endPos--; string filename = this.GetTokenSource(); this.endPos++; if (this.document != null) this.document = new Document(filename, 1, this.document.Text, this.document.DocumentType, this.document.Language, this.document.LanguageVendor); }else if (!this.IsEndLineOrEOF(c, 0)){ this.HandleError(Error.MissingPPFile); goto skipToEndOfLine; }else goto setLineInfo; c = this.SkipBlanks(); this.startPos = this.endPos-1; if (c == '/'){ if (this.GetChar(this.endPos) == '/'){ this.endPos--; goto setLineInfo; }else{ this.startPos = this.endPos-1; this.HandleError(Error.EndOfPPLineExpected); goto skipToEndOfLine; } } setLineInfo: Document doc = this.document; if (doc != null){ this.document = doc = new Document(doc.Name, 1, doc.Text, doc.DocumentType, doc.Language, doc.LanguageVendor); int offset = lnum - doc.GetLine(this.startPos); doc.LineNumber = offset; } if (this.IsEndLineOrEOF(c, 0)) return exclude; break; case "error": if (insideExcludedBlock) goto skipToEndOfLine; this.SkipBlanks(); this.startPos = --this.endPos; this.ScanString((char)0); this.HandleError(Error.ErrorDirective, this.unescapedString); break; case "warning": if (insideExcludedBlock) goto skipToEndOfLine; this.SkipBlanks(); this.startPos = --this.endPos; this.ScanString((char)0); this.HandleError(Error.WarningDirective, this.unescapedString); break; case "region": if (insideExcludedBlock) goto skipToEndOfLine; this.regionCount++; if (this.sink != null){ if (this.regionCtxStack == null) this.regionCtxStack = new Stack(); SourceContext regionCtx = this.CurrentSourceContext; regionCtx.StartPos = this.PositionOfFirstCharacterOfNextLine(this.endPos); regionCtx.StartPos = this.PositionOfLastCharacterOfPreviousLine(regionCtx.StartPos)+1; regionCtxStack.Push(regionCtx); } goto skipToEndOfLine; case "endregion": if (insideExcludedBlock) goto skipToEndOfLine; if (this.regionCount <= 0) this.HandleError(Error.UnexpectedDirective); else this.regionCount--; if (this.sink != null && this.regionCtxStack != null && this.regionCtxStack.Count > 0){ SourceContext startRegionCtx = (SourceContext)regionCtxStack.Pop(); startRegionCtx.EndPos = this.endPos; this.sink.AddCollapsibleRegion(startRegionCtx, true); if (this.regionCtxStack.Count == 0) this.regionCtxStack = null; } goto skipToEndOfLine; case "pragma": this.SkipBlanks(); this.startPos = --this.endPos; this.ScanIdentifier(); string identString = this.GetIdentifierString(); if (identString != "warning") { this.HandleError(Error.UnexpectedDirective); goto skipToEndOfLine; } this.SkipBlanks(); this.startPos = --this.endPos; this.ScanIdentifier(); identString = this.GetIdentifierString(); bool disable; if (identString == "disable") { disable = true; } else if (identString == "restore") { disable = false; } else { this.HandleError(Error.ErrorDirective); goto skipToEndOfLine; } while (true) { this.SkipBlanks(); this.startPos = --this.endPos; c = this.GetChar(this.endPos); if (this.IsEndLineOrEOF(c, 0)) return exclude; if (c < '0' || c > '9') { this.HandleError(Error.IntegralTypeValueExpected); goto skipToEndOfLine; } while ('0' <= (c = this.GetChar(++this.endPos)) && c <= '9') ; int warnNum; try { warnNum = int.Parse(this.GetTokenSource(), CultureInfo.InvariantCulture); if (warnNum <= 0) { this.startPos = this.endPos; this.HandleError(Error.Warning); goto skipToEndOfLine; } } catch (OverflowException) { this.startPos++; this.HandleError(Error.IntOverflow); goto skipToEndOfLine; } PragmaInfo pragmaInfo = (PragmaInfo)this.pragmaInformation[warnNum]; this.pragmaInformation[warnNum] = new PragmaInfo(this.CurrentSourceContext.StartLine, disable, pragmaInfo); if (this.SkipBlanks() != ',') break; } goto skipToEndOfLine; default: if (insideExcludedBlock) goto skipToEndOfLine; this.HandleError(Error.PPDirectiveExpected); goto skipToEndOfLine; } char ch = this.SkipBlanks(); if (this.IsEndLineOrEOF(ch, 0)) return exclude; if (ch == '/' && (ch = this.GetChar(this.endPos++)) == '/') goto skipToEndOfLine; this.startPos = this.endPos-1; this.HandleError(Error.EndOfPPLineExpected); skipToEndOfLine: this.SkipSingleLineComment(); return exclude; }
private static CompilationUnit CompileTemplate(string templateName, params object[] additionalDefines) { CSOPTIONS csOptions = new CSOPTIONS(); csOptions.OutputAssembly = @"c:\temp.dll"; csOptions.AllowUnsafeCode = true; #if REDUCE_LOCAL_ACCESS const bool reduceLocalAccess = true; #else const bool reduceLocalAccess = false; #endif if (additionalDefines.Length > 0 || reduceLocalAccess) { csOptions.DefinedPreProcessorSymbols = new StringList(); if (reduceLocalAccess) csOptions.DefinedPreProcessorSymbols.Add("REDUCE_LOCAL_ACCESS"); foreach (string ppSym in additionalDefines) csOptions.DefinedPreProcessorSymbols.Add(ppSym); } Stream templateStream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream( "Microsoft.Zing." + templateName + ".cs"); System.IO.StreamReader reader = new StreamReader(templateStream, true); string src = reader.ReadToEnd(); Document doc = new Document(templateName, 1, src, SymDocumentType.Text, typeof(DebuggerLanguage).GUID, SymLanguageVendor.Microsoft); System.Compiler.ErrorNodeList errorNodes = new ErrorNodeList(); Module module = (new CS.Compiler()).CreateModule(csOptions, errorNodes); CS.Parser parser = new CS.Parser(doc, errorNodes, module, csOptions); CompilationUnit cu = parser.ParseCompilationUnit(src, templateName + ".cs", csOptions, errorNodes, null); if (errorNodes.Count > 0) throw new ApplicationException("Internal error: template fails to compile"); return cu; }
public static CompilationUnit GetApplicationTemplate(Module targetModule, CompilerParameters options, bool hasGlobals, bool usesHeap) { CSOPTIONS csOptions = new CSOPTIONS(); csOptions.OutputAssembly = options.OutputAssembly; csOptions.AllowUnsafeCode = true; #if REDUCE_LOCAL_ACCESS csOptions.DefinedPreProcessorSymbols = new StringList(1); csOptions.DefinedPreProcessorSymbols.Add("REDUCE_LOCAL_ACCESS"); #endif Stream templateStream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream( "Microsoft.Zing.Template.cs"); System.IO.StreamReader reader = new StreamReader(templateStream, true); string src = reader.ReadToEnd(); Document doc = new Document("Template", 1, src, SymDocumentType.Text, typeof(DebuggerLanguage).GUID, SymLanguageVendor.Microsoft); System.Compiler.ErrorNodeList errorNodes = new ErrorNodeList(); CS.Parser parser = new CS.Parser(doc, errorNodes, targetModule, csOptions); CompilationUnit cu = parser.ParseCompilationUnit(src, "Template.cs", csOptions, errorNodes, null); if (errorNodes.Count > 0) throw new ApplicationException("Internal error: application template fails to compile"); return cu; }