ParserResults ParseTemplate(ITextSource textBuffer) { var host = new RazorEngineHost(new CSharpRazorCodeLanguage()); var engine = new RazorTemplateEngine(host); return(engine.ParseTemplate(textBuffer.CreateReader())); }
public IEnumerable <ISearchResult> FindAllValidating(ITextSource document, int offset, int length) { using (var stream = document.CreateReader()) { var doc = (IDocument)document; var xmlDoc = new XPathDocument(stream);; var navigator = xmlDoc.CreateNavigator(); XPathExpression expr = null; XPathNodeIterator iterator; try { expr = navigator.Compile(_xPath); iterator = navigator.Select(expr); } catch (System.Xml.XPath.XPathException) { yield break; } while (iterator.MoveNext()) { var current = iterator.Current; var segment = XmlSegment(doc, ((IXmlLineInfo)current).LineNumber, ((IXmlLineInfo)current).LinePosition); if (segment != null && segment.Offset >= offset && segment.EndOffset <= (offset + length)) { yield return(new XPathSearchResult() { StartOffset = segment.Offset, Length = segment.Length }); } } } }
public IEnumerable<ISearchResult> FindAllValidating(ITextSource document, int offset, int length) { using (var stream = document.CreateReader()) { var doc = (IDocument)document; var xmlDoc = new XPathDocument(stream); ; var navigator = xmlDoc.CreateNavigator(); XPathExpression expr = null; XPathNodeIterator iterator; try { expr = navigator.Compile(_xPath); iterator = navigator.Select(expr); } catch (System.Xml.XPath.XPathException) { yield break; } while (iterator.MoveNext()) { var current = iterator.Current; var segment = XmlSegment(doc, ((IXmlLineInfo)current).LineNumber, ((IXmlLineInfo)current).LinePosition); if (segment != null && segment.Offset >= offset && segment.EndOffset <= (offset + length)) { yield return new XPathSearchResult() { StartOffset = segment.Offset, Length = segment.Length }; } } } }
private async Task WriteTextSource(Stream stream, ITextSource data) { using (var writer = new StreamWriter(stream)) using (var reader = data.CreateReader()) { await reader.CopyToAsync(writer); } }
public IList<FoldingRegion> GetFolds(ITextSource textSource) { try { GetFolds(textSource.CreateReader()); return folds; } catch (XmlException) { } return null; }
public IList <FoldingRegion> GetFolds(ITextSource textSource) { try { GetFolds(textSource.CreateReader()); return(folds); } catch (XmlException) { } return(null); }
public void Parse(IProject project, string fileName, string source) { this.FileName = fileName; this.Content = new StringTextSource(source); this.Document = new StringBuilderDocument(this.Content); this.Project = project; CSharpParser p = project.CreateParser(); this.SyntaxTree = p.Parse(Content.CreateReader(), fileName); this.ParsedFile = this.SyntaxTree.ToTypeSystem(); }
/// <summary> /// Creates a new TextReader to read from this text source. /// </summary> public static TextReader CreateReader(this ITextSource source, ISegment segment) { if (source == null) { throw new ArgumentNullException("source"); } if (segment == null) { throw new ArgumentNullException("segment"); } return(source.CreateReader(segment.Offset, segment.Length)); }
private void Parse(IProject project, string fileName, string source) { Console.WriteLine("Loading " + fileName); this.FileName = fileName; this.Content = new StringTextSource(source); this.Document = new StringBuilderDocument(this.Content); this.Project = project; CSharpParser p = project.CreateParser(); this.SyntaxTree = p.Parse(Content.CreateReader(), fileName); this.ParsedFile = this.SyntaxTree.ToTypeSystem(); if(this.Project.ProjectContent != null) this.Project.ProjectContent.AddOrUpdateFiles(this.ParsedFile); }
/// <summary> /// Process the <paramref name="source"/> /// </summary> /// <param name="source">The source to process/parse for SQL statement batches</param> /// <param name="stripComments"><c>true</c> when the comments should be stripped</param> public void Process(ITextSource source, bool stripComments = false) { var output = new StringWriter() { NewLine = _newLine, }; var context = new SearchContext(_rangeSearchers, _specialTokenSearchers, stripComments); context.BatchSql += (sender, evt) => { output.Write(evt.SqlContent); if (evt.IsEndOfLine) { output.WriteLine(); } }; context.SpecialToken += (sender, evt) => { output.Flush(); var sqlText = output.ToString(); OnSqlText(new SqlTextEventArgs(sqlText)); OnSpecialToken(evt); output = new StringWriter(); }; var reader = source.CreateReader(); if (reader == null) { return; } var status = new SearchStatus(context, reader); do { status = status.Process(); }while (status != null); var remaining = output.ToString(); if (!string.IsNullOrWhiteSpace(remaining)) { OnSqlText(new SqlTextEventArgs(remaining)); } }
private void Parse(IProject project, string fileName, string source) { Console.WriteLine("Loading " + fileName); this.FileName = fileName; this.Content = new StringTextSource(source); this.Document = new StringBuilderDocument(this.Content); this.Project = project; CSharpParser p = project.CreateParser(); this.SyntaxTree = p.Parse(Content.CreateReader(), fileName); this.ParsedFile = this.SyntaxTree.ToTypeSystem(); if (this.Project.ProjectContent != null) { this.Project.ProjectContent.AddOrUpdateFiles(this.ParsedFile); } }
private void Parse(IProject project, string fileName, string source) { Console.WriteLine("Loading " + fileName); this.FileName = fileName; this.Content = new StringTextSource(source); this.Document = new ReadOnlyDocument(this.Content); this.Project = project; CSharpParser p = project.CreateParser(); this.SyntaxTree = p.Parse(Content.CreateReader(), fileName); if (p.HasErrors) { Console.WriteLine("Error parsing " + fileName + ":"); foreach (var error in p.Errors) { Console.WriteLine(" " + error.Region + " " + error.Message); } } this.ParsedFile = this.SyntaxTree.ToTypeSystem(); }
public CSharpFile(CSharpProject project, string fileName) { this.Project = project; this.FileName = fileName; this.Content = new StringTextSource(File.ReadAllText(FileName)); this.LinesOfCode = 1 + this.Content.Text.Count(c => c == '\n'); CSharpParser p = project.CreateParser(); this.CompilationUnit = p.Parse(Content.CreateReader(), fileName); if (p.HasErrors) { Console.WriteLine("Error parsing " + fileName + ":"); foreach (var error in p.ErrorPrinter.Errors) { Console.WriteLine(" " + error.Region + " " + error.Message); } } this.ParsedFile = this.CompilationUnit.ToTypeSystem(); }
public IEnumerable <ISearchResult> FindAllForwardOnly(ITextSource document, int offset, int length) { var xc = new XPathCollection(); try { xc.Add(_xPath); } catch (Exception) { yield break; } using (var reader = new XmlTextReader(document.CreateReader())) using (var xpathReader = new XPathReader(reader, xc)) { var lineInfo = xpathReader as IXmlLineInfo; var doc = (IDocument)document; ISegment segment; while (Read(xpathReader)) { if (xpathReader.Match(0) && xpathReader.NodeType != XmlNodeType.EndElement) { segment = null; try { segment = XmlSegment(doc, lineInfo.LineNumber, lineInfo.LinePosition); } catch (Exception) { } if (segment != null && segment.Offset >= offset && segment.EndOffset <= (offset + length)) { yield return(new XPathSearchResult() { StartOffset = segment.Offset, Length = segment.Length }); } } } } }
private void Parse(IProject project, string fileName, string source) { Console.WriteLine("Loading " + fileName); this.FileName = fileName; this.Content = new StringTextSource(source); this.Document = new StringBuilderDocument(this.Content); this.Project = project; CSharpParser p = project.CreateParser(); this.SyntaxTree = p.Parse(Content.CreateReader(), fileName); if (p.HasErrors) { Console.WriteLine("Error parsing " + fileName + ":"); foreach (var error in p.Errors) { Console.WriteLine(" " + error.Region + " " + error.Message); } } this.ParsedFile = this.SyntaxTree.ToTypeSystem(); if (this.Project.ProjectContent != null) { this.Project.ProjectContent.AddOrUpdateFiles(this.ParsedFile); } }
/// <summary> /// Creates a new TextReader to read from this text buffer. /// </summary> public System.IO.TextReader CreateReader() { return(textSource.CreateReader()); }
public IEnumerable<ISearchResult> FindAllForwardOnly(ITextSource document, int offset, int length) { var xc = new XPathCollection(); try { xc.Add(_xPath); } catch (Exception) { yield break; } using (var reader = new XmlTextReader(document.CreateReader())) using (var xpathReader = new XPathReader(reader, xc)) { var lineInfo = xpathReader as IXmlLineInfo; var doc = (IDocument)document; ISegment segment; while (Read(xpathReader)) { if (xpathReader.Match(0) && xpathReader.NodeType != XmlNodeType.EndElement) { segment = null; try { segment = XmlSegment(doc, lineInfo.LineNumber, lineInfo.LinePosition); } catch (Exception) { } if (segment != null && segment.Offset >= offset && segment.EndOffset <= (offset + length)) { yield return new XPathSearchResult() { StartOffset = segment.Offset, Length = segment.Length }; } } } } }
ParserResults ParseTemplate(ITextSource textBuffer) { var host = new RazorEngineHost(new CSharpRazorCodeLanguage()); var engine = new RazorTemplateEngine(host); return engine.ParseTemplate(textBuffer.CreateReader()); }
/// <summary> /// Creates a new TextReader to read from this text buffer. /// </summary> public TextReader CreateReader() { return(textSource.CreateReader()); }
public override IEnumerable <IEditorScript> GetScripts(ITextSource text, int offset, bool readOnly) { var item = GetCurrentItem(text, offset); if (item != null) { var generator = new ScriptMenuGenerator() { Conn = _conn, ConnData = _connData, Items = new[] { item } }; var extras = new List <IEditorScript>(); if (!readOnly) { extras.Add(new EditorScriptExecute() { Name = "Transform: Criteria to Where Clause", Execute = () => { var doc = text as IDocument; if (doc != null) { try { var segment = GetCurrentQuerySegment(text, offset); var queryItem = _conn.AmlContext.FromXml(text.CreateReader(segment.Offset, segment.Length)).AssertItem(); queryItem.QueryType().Set("ignore"); queryItem.Where().Remove(); var settings = new SqlSettings(_conn) { RenderOption = SqlRenderOption.WhereClause }; var elem = XElement.Load(text.CreateReader(segment.Offset, segment.Length)); var whereClause = elem.Attribute("where")?.Value ?? ""; if (whereClause != "") { whereClause += " and "; } whereClause += queryItem.ToQueryItem().ToArasSql(settings); elem.SetAttributeValue("where", whereClause); foreach (var child in elem.Elements().ToArray()) { child.Remove(); } doc.Replace(segment.Offset, segment.Length, elem.ToString()); } catch (Exception ex) { // Do nothing } } return(Task.FromResult(true)); } }); } return(generator.GetScripts().Concat(extras)); } return(Enumerable.Empty <IEditorScript>()); }
public static MSBuildRootDocument Parse( string filename, ITextSource textSource, MSBuildRootDocument previous, MSBuildSchemaProvider schemaProvider, IRuntimeInformation runtimeInfo, CancellationToken token) { var xmlParser = new XmlParser(new XmlRootState(), true); try { xmlParser.Parse(textSource.CreateReader()); } catch (Exception ex) { LoggingService.LogError("Unhandled error parsing xml document", ex); } var xdocument = xmlParser.Nodes.GetRoot(); if (xdocument != null && xdocument.RootElement != null) { if (!xdocument.RootElement.IsEnded) { xdocument.RootElement.End(xmlParser.Location); } } //FIXME: unfortunately the XML parser's regions only have line+col locations, not offsets //so we need to create an ITextDocument to extract tag bodies //we should fix this by changing the parser to use offsets for the tag locations ITextDocument textDoc = textSource as ITextDocument ?? TextEditorFactory.CreateNewDocument(textSource, filename, MSBuildTextEditorExtension.MSBuildMimeType); var propVals = new PropertyValueCollector(true); string projectPath = filename; var doc = new MSBuildRootDocument(filename) { XDocument = xdocument, Text = textDoc, RuntimeInformation = runtimeInfo }; doc.Errors.AddRange(xmlParser.Errors); try { doc.Schema = previous?.Schema ?? schemaProvider.GetSchema(filename, null); } catch (Exception ex) { LoggingService.LogError("Error loading schema", ex); } var importedFiles = new HashSet <string> (StringComparer.OrdinalIgnoreCase) { filename }; var taskBuilder = new TaskMetadataBuilder(doc); var extension = Path.GetExtension(filename); string MakeRelativeMSBuildPathAbsolute(string path) { var dir = Path.GetDirectoryName(doc.Filename); path = path.Replace('\\', Path.DirectorySeparatorChar); return(Path.GetFullPath(Path.Combine(dir, path))); } Import TryImportFile(string possibleFile) { try { var fi = new FileInfo(possibleFile); if (fi.Exists) { var imp = doc.GetCachedOrParse(importedFiles, previous, possibleFile, null, fi.LastWriteTimeUtc, projectPath, propVals, taskBuilder, schemaProvider, token); doc.Imports.Add(possibleFile, imp); return(imp); } } catch (Exception ex) { LoggingService.LogError($"Error importing '{possibleFile}'", ex); } return(null); } Import TryImportSibling(string ifHasThisExtension, string thenTryThisExtension) { if (string.Equals(ifHasThisExtension, extension, StringComparison.OrdinalIgnoreCase)) { var siblingFilename = Path.ChangeExtension(filename, thenTryThisExtension); return(TryImportFile(siblingFilename)); } return(null); } void TryImportIntellisenseImports(MSBuildSchema schema) { foreach (var intellisenseImport in schema.IntelliSenseImports) { TryImportFile(MakeRelativeMSBuildPathAbsolute(intellisenseImport)); } } try { //if this is a targets file, try to import the props _at the top_ var propsImport = TryImportSibling(".targets", ".props"); // this currently only happens in the root file // it's a quick hack to allow files to get some basic intellisense by // importing the files _that they themselves expect to be imported from_. // we also try to load them from the sibling props, as a paired targets/props // will likely share a schema file. var schema = doc.Schema ?? propsImport?.Document?.Schema; if (schema != null) { TryImportIntellisenseImports(doc.Schema); } doc.Build( xdocument, textDoc, runtimeInfo, propVals, taskBuilder, (imp, sdk) => doc.ResolveImport(importedFiles, previous, projectPath, filename, imp, sdk, propVals, taskBuilder, schemaProvider, token) ); //if this is a props file, try to import the targets _at the bottom_ var targetsImport = TryImportSibling(".props", ".targets"); //and if we didn't load intellisense import already, try to load them from the sibling targets if (schema == null && targetsImport?.Document?.Schema != null) { TryImportIntellisenseImports(targetsImport.Document.Schema); } } catch (Exception ex) { LoggingService.LogError($"Error building document '{projectPath}'", ex); } try { var binpath = doc.RuntimeInformation.GetBinPath(); foreach (var t in Directory.GetFiles(binpath, "*.tasks")) { doc.LoadTasks(importedFiles, previous, t, propVals, taskBuilder, schemaProvider, token); } foreach (var t in Directory.GetFiles(binpath, "*.overridetasks")) { doc.LoadTasks(importedFiles, previous, t, propVals, taskBuilder, schemaProvider, token); } } catch (Exception ex) { LoggingService.LogError("Error resolving tasks", ex); } try { if (previous != null) { // try to recover some values that may have been collected from the imports, as they // will not have been re-evaluated var fx = previous.Frameworks.FirstOrDefault(); if (fx != null) { propVals.Collect("TargetFramework", fx.GetShortFolderName()); propVals.Collect("TargetFrameworkVersion", FrameworkInfoProvider.FormatDisplayVersion(fx.Version)); propVals.Collect("TargetFrameworkIdentifier", fx.Framework); } } doc.Frameworks = propVals.GetFrameworks(); } catch (Exception ex) { LoggingService.LogError("Error determining project framework", ex); doc.Frameworks = new List <NuGetFramework> (); } try { //this has to run in a second pass so that it runs after all the schemas are loaded var validator = new MSBuildDocumentValidator(); validator.Run(doc.XDocument, filename, textDoc, doc); } catch (Exception ex) { LoggingService.LogError("Error in validation", ex); } return(doc); }
//[DebuggerStepThrough()] public static XmlState ProcessFragment(ITextSource fragment, Func<XmlReader, int, XmlState, bool> processor) { var lineOffsets = new List<int>() {0}; var state = XmlState.Other; var line = 1; var lastTag = new KeyValuePair<int, int>(0, 0); var attrValueQuote = '"'; for (var i = 0; i < fragment.TextLength; i++) { switch (fragment.GetCharAt(i)) { case '\r': if (i + 1 < fragment.TextLength && fragment.GetCharAt(i + 1) == '\n') i++; line++; lineOffsets.Add(i + 1); if (state == XmlState.Tag) state = XmlState.Attribute; break; case '\n': line++; lineOffsets.Add(i + 1); if (state == XmlState.Tag) state = XmlState.Attribute; break; default: switch (state) { case XmlState.Attribute: if (fragment.GetCharAt(i) == '=') { i++; if (i < fragment.TextLength) attrValueQuote = fragment.GetCharAt(i); state = XmlState.AttributeValue; } else if (fragment.GetCharAt(i) == '>') { state = XmlState.Other; } break; case XmlState.AttributeValue: if (fragment.GetCharAt(i) == '"' || fragment.GetCharAt(i) == '\'') { state = XmlState.Tag; } break; case XmlState.CData: if (i + 2 < fragment.TextLength && fragment.GetCharAt(i) == ']' && fragment.GetCharAt(i + 1) == ']' && fragment.GetCharAt(i + 2) == '>') { i += 2; state = XmlState.Other; } break; case XmlState.Comment: if (i + 2 < fragment.TextLength && fragment.GetCharAt(i) == '-' && fragment.GetCharAt(i + 1) == '-' && fragment.GetCharAt(i + 2) == '>') { i += 2; state = XmlState.Other; } break; case XmlState.Tag: if (char.IsWhiteSpace(fragment.GetCharAt(i))) { state = XmlState.Attribute; } else if (fragment.GetCharAt(i) == '>') { state = XmlState.Other; } break; case XmlState.Other: if (fragment.GetCharAt(i) == '<') { if (i + 3 < fragment.TextLength && fragment.GetCharAt(i + 1) == '!' && fragment.GetCharAt(i + 2) == '-' && fragment.GetCharAt(i + 3) == '-') { i += 3; state = XmlState.Comment; } else if (i + 8 < fragment.TextLength && fragment.GetCharAt(i + 1) == '!' && fragment.GetCharAt(i + 2) == '[' && fragment.GetCharAt(i + 3) == 'C' && fragment.GetCharAt(i + 4) == 'D' && fragment.GetCharAt(i + 5) == 'A' && fragment.GetCharAt(i + 6) == 'T' && fragment.GetCharAt(i + 7) == 'A' && fragment.GetCharAt(i + 8) == '[') { i += 8; state = XmlState.CData; } else { state = XmlState.Tag; lastTag = new KeyValuePair<int, int>(line, i - lineOffsets.Last() + 2); } } break; } break; } } const string __noName = "___NO_NAME___"; const string __eof = "{`EOF`}"; var suffix = string.Empty; switch (state) { case XmlState.Attribute: if (char.IsWhiteSpace(fragment.GetCharAt(fragment.TextLength - 1))) { state = XmlState.AttributeStart; suffix += ">"; } else { suffix += "=\"\">"; } break; case XmlState.AttributeValue: if (fragment.GetCharAt(fragment.TextLength - 1) == '=') { suffix += "''>"; } else { suffix += attrValueQuote.ToString() + ">"; } break; case XmlState.CData: suffix += "]]>"; break; case XmlState.Comment: suffix += "-->"; break; case XmlState.Tag: if (fragment.GetCharAt(fragment.TextLength - 1) == '<') suffix += __noName; suffix += ">"; break; } suffix += "<!--" + __eof + "-->"; var settings = new XmlReaderSettings() { ConformanceLevel = ConformanceLevel.Fragment }; var textReader = new AugmentedReader(fragment.CreateReader(), suffix); var reader = XmlReader.Create(textReader, settings); var lineInfo = reader as IXmlLineInfo; try { bool keepGoing = true; while (keepGoing && reader.Read() && !(reader.NodeType == XmlNodeType.Comment && reader.Value == __eof)) { if (reader.LocalName != __noName) { keepGoing = processor.Invoke(reader, lineOffsets[lineInfo.LineNumber - 1] + lineInfo.LinePosition - (reader.NodeType == XmlNodeType.Element ? 2 : 1), state); if (reader.NodeType == XmlNodeType.Element && lineInfo.LineNumber == lastTag.Key && lineInfo.LinePosition == lastTag.Value) { for (var i = 0; i < reader.AttributeCount; i++) { reader.MoveToAttribute(i); keepGoing = processor.Invoke(reader, lineOffsets[lineInfo.LineNumber - 1] + lineInfo.LinePosition - 1, state); } } } } } catch (XmlException) { // Do Nothing } return state; }
public CSharpFile(CSharpProject project, string fileName) { this.Project = project; this.FileName = fileName; this.Content = new StringTextSource(File.ReadAllText(FileName)); this.LinesOfCode = 1 + this.Content.Text.Count(c => c == '\n'); CSharpParser p = new CSharpParser(project.CompilerSettings); this.CompilationUnit = p.Parse(Content.CreateReader(), fileName); if (p.HasErrors) { Console.WriteLine("Error parsing " + fileName + ":"); foreach (var error in p.ErrorsAndWarnings) { Console.WriteLine(" " + error.Region + " " + error.Message); } } this.ParsedFile = this.CompilationUnit.ToTypeSystem(); }
public CompilationUnit Parse (ITextSource textSource, string fileName, int lineModifier = 0) { return Parse (textSource.CreateReader (), fileName, lineModifier); }
//[DebuggerStepThrough()] public static XmlState ProcessFragment(ITextSource fragment, Func <XmlReader, int, XmlState, bool> processor) { var lineOffsets = new List <int>() { 0 }; var state = XmlState.Other; var line = 1; var lastTag = new KeyValuePair <int, int>(0, 0); var attrValueQuote = '"'; for (var i = 0; i < fragment.TextLength; i++) { switch (fragment.GetCharAt(i)) { case '\r': if (i + 1 < fragment.TextLength && fragment.GetCharAt(i + 1) == '\n') { i++; } line++; lineOffsets.Add(i + 1); if (state == XmlState.Tag) { state = XmlState.Attribute; } break; case '\n': line++; lineOffsets.Add(i + 1); if (state == XmlState.Tag) { state = XmlState.Attribute; } break; default: switch (state) { case XmlState.Attribute: if (fragment.GetCharAt(i) == '=') { i++; if (i < fragment.TextLength) { attrValueQuote = fragment.GetCharAt(i); } state = XmlState.AttributeValue; } else if (fragment.GetCharAt(i) == '>') { state = XmlState.Other; } break; case XmlState.AttributeValue: if (fragment.GetCharAt(i) == '"' || fragment.GetCharAt(i) == '\'') { state = XmlState.Tag; } break; case XmlState.CData: if (i + 2 < fragment.TextLength && fragment.GetCharAt(i) == ']' && fragment.GetCharAt(i + 1) == ']' && fragment.GetCharAt(i + 2) == '>') { i += 2; state = XmlState.Other; } break; case XmlState.Comment: if (i + 2 < fragment.TextLength && fragment.GetCharAt(i) == '-' && fragment.GetCharAt(i + 1) == '-' && fragment.GetCharAt(i + 2) == '>') { i += 2; state = XmlState.Other; } break; case XmlState.Tag: if (char.IsWhiteSpace(fragment.GetCharAt(i))) { state = XmlState.Attribute; } else if (fragment.GetCharAt(i) == '>') { state = XmlState.Other; } break; case XmlState.Other: if (fragment.GetCharAt(i) == '<') { if (i + 3 < fragment.TextLength && fragment.GetCharAt(i + 1) == '!' && fragment.GetCharAt(i + 2) == '-' && fragment.GetCharAt(i + 3) == '-') { i += 3; state = XmlState.Comment; } else if (i + 8 < fragment.TextLength && fragment.GetCharAt(i + 1) == '!' && fragment.GetCharAt(i + 2) == '[' && fragment.GetCharAt(i + 3) == 'C' && fragment.GetCharAt(i + 4) == 'D' && fragment.GetCharAt(i + 5) == 'A' && fragment.GetCharAt(i + 6) == 'T' && fragment.GetCharAt(i + 7) == 'A' && fragment.GetCharAt(i + 8) == '[') { i += 8; state = XmlState.CData; } else { state = XmlState.Tag; lastTag = new KeyValuePair <int, int>(line, i - lineOffsets.Last() + 2); } } break; } break; } } const string __noName = "___NO_NAME___"; const string __eof = "{`EOF`}"; var suffix = string.Empty; switch (state) { case XmlState.Attribute: if (char.IsWhiteSpace(fragment.GetCharAt(fragment.TextLength - 1))) { state = XmlState.AttributeStart; suffix += ">"; } else { suffix += "=\"\">"; } break; case XmlState.AttributeValue: if (fragment.GetCharAt(fragment.TextLength - 1) == '=') { suffix += "''>"; } else { suffix += attrValueQuote.ToString() + ">"; } break; case XmlState.CData: suffix += "]]>"; break; case XmlState.Comment: suffix += "-->"; break; case XmlState.Tag: if (fragment.GetCharAt(fragment.TextLength - 1) == '<') { suffix += __noName; } suffix += ">"; break; } suffix += "<!--" + __eof + "-->"; var settings = new XmlReaderSettings() { ConformanceLevel = ConformanceLevel.Fragment }; var textReader = new AugmentedReader(fragment.CreateReader(), suffix); var reader = XmlReader.Create(textReader, settings); var lineInfo = reader as IXmlLineInfo; try { bool keepGoing = true; while (keepGoing && reader.Read() && !(reader.NodeType == XmlNodeType.Comment && reader.Value == __eof)) { if (reader.LocalName != __noName) { keepGoing = processor.Invoke(reader, lineOffsets[lineInfo.LineNumber - 1] + lineInfo.LinePosition - (reader.NodeType == XmlNodeType.Element ? 2 : 1), state); if (reader.NodeType == XmlNodeType.Element && lineInfo.LineNumber == lastTag.Key && lineInfo.LinePosition == lastTag.Value) { for (var i = 0; i < reader.AttributeCount; i++) { reader.MoveToAttribute(i); keepGoing = processor.Invoke(reader, lineOffsets[lineInfo.LineNumber - 1] + lineInfo.LinePosition - 1, state); } } } } } catch (XmlException) { // Do Nothing } return(state); }
public static MSBuildRootDocument Parse( ITextSource textSource, string filePath, MSBuildRootDocument previous, MSBuildSchemaProvider schemaProvider, IRuntimeInformation runtimeInfo, ITaskMetadataBuilder taskBuilder, CancellationToken token) { var xmlParser = new XmlTreeParser(new XmlRootState()); var(xdocument, _) = xmlParser.Parse(textSource.CreateReader()); var propVals = new PropertyValueCollector(true); var doc = new MSBuildRootDocument(filePath) { XDocument = xdocument, Text = textSource, RuntimeInformation = runtimeInfo }; var importedFiles = new HashSet <string> (StringComparer.OrdinalIgnoreCase); if (filePath != null) { try { doc.Schema = previous?.Schema ?? schemaProvider.GetSchema(filePath, null); } catch (Exception ex) { LoggingService.LogError("Error loading schema", ex); } importedFiles.Add(filePath); } var parseContext = new MSBuildParserContext( runtimeInfo, doc, previous, importedFiles, filePath, propVals, taskBuilder, schemaProvider, token); if (filePath != null) { doc.FileEvaluationContext = new MSBuildFileEvaluationContext(parseContext.RuntimeEvaluationContext, filePath, filePath); } else { doc.FileEvaluationContext = parseContext.RuntimeEvaluationContext; } string MakeRelativeMSBuildPathAbsolute(string path) { var dir = Path.GetDirectoryName(doc.Filename); path = path.Replace('\\', Path.DirectorySeparatorChar); return(Path.GetFullPath(Path.Combine(dir, path))); } Import TryImportFile(string label, string possibleFile) { try { var fi = new FileInfo(possibleFile); if (fi.Exists) { var imp = parseContext.GetCachedOrParse(label, possibleFile, null, fi.LastWriteTimeUtc); doc.AddImport(imp); return(imp); } } catch (Exception ex) when(parseContext.IsNotCancellation(ex)) { LoggingService.LogError($"Error importing '{possibleFile}'", ex); } return(null); } Import TryImportSibling(string ifHasThisExtension, string thenTryThisExtension) { if (filePath == null) { return(null); } var extension = Path.GetExtension(filePath); if (string.Equals(ifHasThisExtension, extension, StringComparison.OrdinalIgnoreCase)) { var siblingFilename = Path.ChangeExtension(filePath, thenTryThisExtension); return(TryImportFile("(implicit)", siblingFilename)); } return(null); } void TryImportIntellisenseImports(MSBuildSchema schema) { foreach (var intellisenseImport in schema.IntelliSenseImports) { TryImportFile("(from schema)", MakeRelativeMSBuildPathAbsolute(intellisenseImport)); } } try { //if this is a targets file, try to import the props _at the top_ var propsImport = TryImportSibling(".targets", ".props"); // this currently only happens in the root file // it's a quick hack to allow files to get some basic intellisense by // importing the files _that they themselves expect to be imported from_. // we also try to load them from the sibling props, as a paired targets/props // will likely share a schema file. var schema = doc.Schema ?? propsImport?.Document?.Schema; if (schema != null) { TryImportIntellisenseImports(doc.Schema); } doc.Build(xdocument, textSource, parseContext); //if this is a props file, try to import the targets _at the bottom_ var targetsImport = TryImportSibling(".props", ".targets"); //and if we didn't load intellisense import already, try to load them from the sibling targets if (schema == null && targetsImport?.Document?.Schema != null) { TryImportIntellisenseImports(targetsImport.Document.Schema); } } catch (Exception ex) when(parseContext.IsNotCancellation(ex)) { LoggingService.LogError($"Error building document '{filePath ?? "[unnamed]"}'", ex); } try { var binpath = parseContext.RuntimeInformation.BinPath; foreach (var t in Directory.GetFiles(binpath, "*.tasks")) { doc.LoadTasks(parseContext, "(core tasks)", t); } foreach (var t in Directory.GetFiles(binpath, "*.overridetasks")) { doc.LoadTasks(parseContext, "(core overridetasks)", t); } } catch (Exception ex) when(parseContext.IsNotCancellation(ex)) { LoggingService.LogError("Error resolving tasks", ex); } try { if (previous != null) { // try to recover some values that may have been collected from the imports, as they // will not have been re-evaluated var fx = previous.Frameworks.FirstOrDefault(); if (fx != null) { propVals.Collect("TargetFramework", fx.GetShortFolderName()); propVals.Collect("TargetFrameworkVersion", FrameworkInfoProvider.FormatDisplayVersion(fx.Version)); propVals.Collect("TargetFrameworkIdentifier", fx.Framework); } } doc.Frameworks = propVals.GetFrameworks(); } catch (Exception ex) { LoggingService.LogError("Error determining project framework", ex); doc.Frameworks = new List <NuGetFramework> (); } try { //this has to run in a second pass so that it runs after all the schemas are loaded var validator = new MSBuildDocumentValidator(); validator.Run(doc.XDocument, textSource, doc); } catch (Exception ex) when(parseContext.IsNotCancellation(ex)) { LoggingService.LogError("Error in validation", ex); } return(doc); }
public static MSBuildRootDocument Parse( string filename, ITextSource textSource, MSBuildRootDocument previous, MSBuildSchemaProvider schemaProvider, IRuntimeInformation runtimeInfo, CancellationToken token) { var xmlParser = new XmlParser(new XmlRootState(), true); try { xmlParser.Parse(textSource.CreateReader()); } catch (Exception ex) { LoggingService.LogError("Unhandled error parsing xml document", ex); } var xdocument = xmlParser.Nodes.GetRoot(); if (xdocument != null && xdocument.RootElement != null) { if (!xdocument.RootElement.IsEnded) { xdocument.RootElement.End(xmlParser.Location); } } //FIXME: unfortunately the XML parser's regions only have line+col locations, not offsets //so we need to create an ITextDocument to extract tag bodies //we should fix this by changing the parser to use offsets for the tag locations ITextDocument textDoc = textSource as ITextDocument ?? TextEditorFactory.CreateNewDocument(textSource, filename, MSBuildTextEditorExtension.MSBuildMimeType); var propVals = new PropertyValueCollector(true); string projectPath = filename; var doc = new MSBuildRootDocument(filename); doc.XDocument = xdocument; doc.Text = textDoc; doc.RuntimeInformation = runtimeInfo; doc.Errors.AddRange(xmlParser.Errors); var importedFiles = new HashSet <string> (StringComparer.OrdinalIgnoreCase); importedFiles.Add(filename); var taskBuilder = new TaskMetadataBuilder(doc); try { doc.Build( xdocument, textDoc, runtimeInfo, propVals, taskBuilder, (imp, sdk) => doc.ResolveImport(importedFiles, previous, projectPath, filename, imp, sdk, propVals, taskBuilder, schemaProvider, token) ); } catch (Exception ex) { LoggingService.LogError("Error building document", ex); } try { var binpath = doc.RuntimeInformation.GetBinPath(); foreach (var t in Directory.GetFiles(binpath, "*.tasks")) { doc.LoadTasks(importedFiles, previous, t, propVals, taskBuilder, schemaProvider, token); } foreach (var t in Directory.GetFiles(binpath, "*.overridetasks")) { doc.LoadTasks(importedFiles, previous, t, propVals, taskBuilder, schemaProvider, token); } } catch (Exception ex) { LoggingService.LogError("Error resolving tasks", ex); } try { if (previous != null) { // try to recover some values that may have been collected from the imports, as they // will not have been re-evaluated var fx = previous.Frameworks.FirstOrDefault(); if (fx != null) { propVals.Collect("TargetFramework", fx.GetShortFolderName()); propVals.Collect("TargetFrameworkVersion", FrameworkInfoProvider.FormatDisplayVersion(fx.Version)); propVals.Collect("TargetFrameworkIdentifier", fx.Framework); } } doc.Frameworks = propVals.GetFrameworks(); } catch (Exception ex) { LoggingService.LogError("Error determining project framework", ex); doc.Frameworks = new List <NuGetFramework> (); } try { doc.Schema = previous?.Schema ?? schemaProvider.GetSchema(filename, null); } catch (Exception ex) { LoggingService.LogError("Error loading schema", ex); } try { //this has to run in a second pass so that it runs after all the schemas are loaded var validator = new MSBuildDocumentValidator(); validator.Run(doc.XDocument, filename, textDoc, doc); } catch (Exception ex) { LoggingService.LogError("Error in validation", ex); } return(doc); }