private SkippedTokensTriviaSyntax FileAsTrivia(SourceText text) { var builder = new SyntaxListBuilder(1); builder.Add(SyntaxFactory.BadToken(null, text.ToString(), null)); var fileAsTrivia = _syntaxFactory.SkippedTokensTrivia(builder.ToList <SyntaxToken>()); ForceEndOfFile(); // force the scanner to report that it is at the end of the input. return(fileAsTrivia); }
private SkippedTokensTriviaSyntax ParserErrorsAsTrivia(List <ParseErrorData> parseErrors, IDictionary <string, SourceText> includes) { // create one syntax token per error // and one syntax token for the main file // these tokens will get as many errors as needed. // We are only includin 1 syntax error per file (1003) and one parse error (9002) var textNode = SyntaxFactory.BadToken(null, _text.ToString(), null); var builder = new SyntaxListBuilder(parseErrors.Count + 1); if (!parseErrors.IsEmpty()) { bool hasSyntaxError = false; bool hasParserError = false; foreach (var e in parseErrors) { bool add = true; if (e.Code == ErrorCode.ERR_SyntaxError) { add = !hasSyntaxError; hasSyntaxError = true; } else if (e.Code == ErrorCode.ERR_ParserError) { add = !hasParserError; hasParserError = true; } if (!add) { continue; } if (e.Node != null) { var node = e.Node; var key = node.SourceFileName; int pos = node.Position; int len = node.FullWidth; if (node.SourceSymbol != null) { var sym = node.SourceSymbol as XSharpToken; key = sym.SourceName; pos = sym.Position; len = sym.FullWidth; } if (len <= 0 || pos < 0) { if (e.Node.Parent != null) { var xNode = e.Node as IXParseTree; pos = xNode.Position; len = xNode.FullWidth; } if (pos < 0) { pos = 0; } if (len <= 0) { len = 1; } } SourceText inc = null; if (key != null && includes.ContainsKey(key)) { inc = includes[key]; if (pos - 1 + len > inc.Length) { len = inc.Length - pos + 1; } } var diag = new SyntaxDiagnosticInfo(pos, len, e.Code, e.Args); if (inc != null) { var incNode = SyntaxFactory.BadToken(null, inc.ToString(), null); incNode = incNode.WithAdditionalDiagnostics(diag); incNode.XNode = e.Node; builder.Add(incNode); } else { textNode = textNode.WithAdditionalDiagnostics(diag); } } else { textNode = textNode.WithAdditionalDiagnostics(new SyntaxDiagnosticInfo(e.Code, e.Args)); } } } else { if (_options.ParseLevel == ParseLevel.Complete) { textNode = textNode.WithAdditionalDiagnostics(new SyntaxDiagnosticInfo(ErrorCode.ERR_ParserError, "Unknown error")); } } builder.Add(textNode); return(_syntaxFactory.SkippedTokensTrivia(builder.ToList <SyntaxToken>())); }