static public ParserNode Parse(string data, ParserType parserType, bool strict) { switch (parserType) { case ParserType.Balanced: return(BalancedVisitor.Parse(data, strict)); case ParserType.Columns: return(ColumnsVisitor.Parse(data, strict)); case ParserType.CSharp: return(CSharpVisitor.Parse(data, strict)); case ParserType.CSV: return(CSVVisitor.Parse(data, strict)); case ParserType.ExactColumns: return(ExactColumnsVisitor.Parse(data, strict)); case ParserType.HTML: return(HTMLVisitor.Parse(data, strict)); case ParserType.JSON: return(JSONVisitor.Parse(data, strict)); case ParserType.SQL: return(SQLVisitor.Parse(data, strict)); case ParserType.TSV: return(TSVVisitor.Parse(data, strict)); case ParserType.XML: return(XMLVisitor.Parse(data, strict)); default: throw new ArgumentException("Unable to parse this type"); } }
static public string Reformat(ParserNode node, string input, ParserType parserType) { switch (parserType) { case ParserType.HTML: return(HTMLVisitor.Format(node, input)); case ParserType.JSON: return(JSONVisitor.Format(node, input)); case ParserType.XML: return(XMLVisitor.Format(node, input)); default: throw new Exception("Unable to reformat this type"); } }
internal SyntaxTrivia ConvertTrivia(SyntaxTrivia t) { var switchExpr = t.RawKind; /* TODO ERROR: Skipped RegionDirectiveTrivia */ switch (switchExpr) { case (int)CS.SyntaxKind.WhitespaceTrivia: { return(VBFactory.WhitespaceTrivia(t.ToString())); } case (int)CS.SyntaxKind.EndOfLineTrivia: { return(global::VisualBasicSyntaxFactory.VBEOLTrivia); } case (int)CS.SyntaxKind.SingleLineCommentTrivia: { if (t.ToFullString().EndsWith("*/")) { return(VBFactory.CommentTrivia($"'{ReplaceLeadingSlashes(t.ToFullString().Substring(2, t.ToFullString().Length - 4))}")); } return(VBFactory.CommentTrivia($"'{ReplaceLeadingSlashes(t.ToFullString().Substring(2))}")); } case (int)CS.SyntaxKind.MultiLineCommentTrivia: { if (t.ToFullString().EndsWith("*/")) { return(VBFactory.CommentTrivia($"'{ReplaceLeadingSlashes(t.ToFullString().Substring(2, t.ToFullString().Length - 4)).Replace(Constants.vbLf, "").Replace(Constants.vbCr, "")}")); } return(VBFactory.CommentTrivia($"'{ReplaceLeadingSlashes(t.ToFullString().Substring(2)).Replace(Constants.vbLf, "").Replace(Constants.vbCr, "")}")); } case (int)CS.SyntaxKind.DocumentationCommentExteriorTrivia: { return(VBFactory.SyntaxTrivia(VB.SyntaxKind.CommentTrivia, "'''")); } case (int)CS.SyntaxKind.DisabledTextTrivia: { if (TriviaDepth > 0) { return(VBFactory.DisabledTextTrivia(t.ToString().WithoutNewLines(' '))); } return(VBFactory.DisabledTextTrivia(t.ToString().ConsistentNewlines())); } case (int)CS.SyntaxKind.PreprocessingMessageTrivia: { return(VBFactory.CommentTrivia($" ' {t}")); } case (int)CS.SyntaxKind.None: { return(default(SyntaxTrivia)); } } /* TODO ERROR: Skipped EndRegionDirectiveTrivia */ /* TODO ERROR: Skipped RegionDirectiveTrivia */ CSS.StructuredTriviaSyntax StructuredTrivia = (CSS.StructuredTriviaSyntax)t.GetStructure(); Debug.Assert(StructuredTrivia != null, $"Found new type of non structured trivia {t.RawKind}"); var switchExpr1 = t.RawKind; switch (switchExpr1) { case (int)CS.SyntaxKind.DefineDirectiveTrivia: { CSS.DefineDirectiveTriviaSyntax DefineDirective = (CSS.DefineDirectiveTriviaSyntax)StructuredTrivia; var Name = VBFactory.Identifier(DefineDirective.Name.ValueText); VBS.ExpressionSyntax value = VBFactory.TrueLiteralExpression(global::VisualBasicSyntaxFactory.TrueKeyword); return(VBFactory.Trivia(WithAppendedTriviaFromEndOfDirectiveToken(WithConvertedTriviaFrom(VBFactory.ConstDirectiveTrivia(Name, value), DefineDirective), DefineDirective.EndOfDirectiveToken) )); } case (int)CS.SyntaxKind.UndefDirectiveTrivia: { CSS.UndefDirectiveTriviaSyntax UndefineDirective = (CSS.UndefDirectiveTriviaSyntax)StructuredTrivia; var Name = VBFactory.Identifier(UndefineDirective.Name.ValueText); VBS.ExpressionSyntax value = global::VisualBasicSyntaxFactory.NothingExpression; return(VBFactory.Trivia(WithAppendedTriviaFromEndOfDirectiveToken(WithConvertedTriviaFrom(VBFactory.ConstDirectiveTrivia(Name, value), UndefineDirective), UndefineDirective.EndOfDirectiveToken) )); } case (int)CS.SyntaxKind.EndIfDirectiveTrivia: { if (TriviaDepth > 0) { TriviaDepth -= 1; return(VBFactory.CommentTrivia($"' TODO VB does not allow directives here, original statement {t.ToFullString().WithoutNewLines(' ')}")); } CSS.EndIfDirectiveTriviaSyntax EndIfDirective = (CSS.EndIfDirectiveTriviaSyntax)StructuredTrivia; return(VBFactory.Trivia(WithAppendedTriviaFromEndOfDirectiveToken(VBFactory.EndIfDirectiveTrivia().WithConvertedTrailingTriviaFrom(EndIfDirective.EndIfKeyword), EndIfDirective.EndOfDirectiveToken) )); } case (int)CS.SyntaxKind.ErrorDirectiveTrivia: { CSS.ErrorDirectiveTriviaSyntax ErrorDirective = (CSS.ErrorDirectiveTriviaSyntax)StructuredTrivia; return(VBFactory.CommentTrivia($"' TODO: Check VB does not support Error Directive Trivia, Original Directive {ErrorDirective.ToFullString()}")); } case (int)CS.SyntaxKind.IfDirectiveTrivia: { if (t.Token.Parent?.AncestorsAndSelf().OfType <CSS.InitializerExpressionSyntax>().Any() == true) { TriviaDepth += 1; } CSS.IfDirectiveTriviaSyntax IfDirective = (CSS.IfDirectiveTriviaSyntax)StructuredTrivia; string Expression1 = StringReplaceCondition(IfDirective.Condition.ToString()); return(VBFactory.Trivia(WithAppendedTriviaFromEndOfDirectiveToken(VBFactory.IfDirectiveTrivia(global::VisualBasicSyntaxFactory.IfKeyword, VBFactory.ParseExpression(Expression1)).With(IfDirective.GetLeadingTrivia().ConvertTrivia(), IfDirective.Condition.GetTrailingTrivia().ConvertTrivia()), IfDirective.EndOfDirectiveToken))); } case (int)CS.SyntaxKind.ElifDirectiveTrivia: { if (t.Token.Parent.AncestorsAndSelf().OfType <CSS.InitializerExpressionSyntax>().Any()) { TriviaDepth += 1; } CSS.ElifDirectiveTriviaSyntax ELIfDirective = (CSS.ElifDirectiveTriviaSyntax)StructuredTrivia; string Expression1 = StringReplaceCondition(ELIfDirective.Condition.ToString()); SyntaxToken IfOrElseIfKeyword; if (t.IsKind(CS.SyntaxKind.ElifDirectiveTrivia)) { IfOrElseIfKeyword = global::VisualBasicSyntaxFactory.ElseIfKeyword; } else { IfOrElseIfKeyword = global::VisualBasicSyntaxFactory.IfKeyword; } return(VBFactory.Trivia(WithAppendedTriviaFromEndOfDirectiveToken(VBFactory.ElseIfDirectiveTrivia(IfOrElseIfKeyword, VBFactory.ParseExpression(Expression1)) .With(ELIfDirective.GetLeadingTrivia().ConvertTrivia(), ELIfDirective.Condition.GetTrailingTrivia().ConvertTrivia()) , ELIfDirective.EndOfDirectiveToken))); } case (int)CS.SyntaxKind.LineDirectiveTrivia: { return(VBFactory.CommentTrivia($"' TODO: Check VB does not support Line Directive Trivia, Original Directive {t}")); } case (int)CS.SyntaxKind.ElseDirectiveTrivia: { return(VBFactory.Trivia(WithTrailingEOL(VBFactory.ElseDirectiveTrivia().NormalizeWhitespace().WithConvertedTrailingTriviaFrom(((CSS.ElseDirectiveTriviaSyntax)StructuredTrivia).ElseKeyword)))); } case (int)CS.SyntaxKind.EndRegionDirectiveTrivia: { CSS.EndRegionDirectiveTriviaSyntax EndRegionDirective = (CSS.EndRegionDirectiveTriviaSyntax)StructuredTrivia; return(VBFactory.Trivia(WithAppendedTriviaFromEndOfDirectiveToken(VBFactory.EndRegionDirectiveTrivia(global::VisualBasicSyntaxFactory.HashToken, global::VisualBasicSyntaxFactory.EndKeyword, global::VisualBasicSyntaxFactory.RegionKeyword), EndRegionDirective.EndOfDirectiveToken))); } case (int)CS.SyntaxKind.PragmaWarningDirectiveTrivia: { // Dim PragmaWarningDirectiveTrivia As CSS.PragmaWarningDirectiveTriviaSyntax = DirectCast(StructuredTrivia, CSS.PragmaWarningDirectiveTriviaSyntax) // Dim ErrorList As New List(Of VBS.IdentifierNameSyntax) // Dim TrailingTriviaStringBuilder As New StringBuilder // For Each i As CSS.ExpressionSyntax In PragmaWarningDirectiveTrivia.ErrorCodes // Dim ErrorCode As String = i.ToString // If ErrorCode.IsInteger Then // ErrorCode = $"CS_{ErrorCode}" // End If // ErrorList.Add(VBFactory.IdentifierName(ErrorCode)) // For Each Trivial As SyntaxTrivia In i.GetTrailingTrivia // TrailingTriviaStringBuilder.Append(Trivial.ToString) // Next // Next // Dim WarningDirectiveTrivia As VBS.DirectiveTriviaSyntax // If PragmaWarningDirectiveTrivia.DisableOrRestoreKeyword.IsKind(CS.SyntaxKind.DisableKeyword) Then // WarningDirectiveTrivia = VBFactory.DisableWarningDirectiveTrivia(ErrorList.ToArray) // Else // WarningDirectiveTrivia = VBFactory.EnableWarningDirectiveTrivia(ErrorList.ToArray) // End If // Return VBFactory.CommentTrivia($" ' TODO {WarningDirectiveTrivia.NormalizeWhitespace}{TrailingTriviaStringBuilder.ToString}") return(default(SyntaxTrivia)); } case (int)CS.SyntaxKind.RegionDirectiveTrivia: { CSS.RegionDirectiveTriviaSyntax RegionDirective = (CSS.RegionDirectiveTriviaSyntax)StructuredTrivia; var EndOfDirectiveToken = RegionDirective.EndOfDirectiveToken; string NameString = $"\"{EndOfDirectiveToken.LeadingTrivia.ToString().Replace("\"", "")}\""; var RegionDirectiveTriviaNode = VBFactory.RegionDirectiveTrivia(global::VisualBasicSyntaxFactory.HashToken, global::VisualBasicSyntaxFactory.RegionKeyword, VBFactory.StringLiteralToken(NameString, NameString) ).WithConvertedTrailingTriviaFrom(EndOfDirectiveToken); return(VBFactory.Trivia(WithTrailingEOL(RegionDirectiveTriviaNode))); } case (int)CS.SyntaxKind.SingleLineDocumentationCommentTrivia: { CSS.DocumentationCommentTriviaSyntax SingleLineDocumentationComment = (CSS.DocumentationCommentTriviaSyntax)StructuredTrivia; var walker = new XMLVisitor(); walker.Visit(SingleLineDocumentationComment); var xmlNodes = new List <VBS.XmlNodeSyntax>(); for (int i = 0, loopTo = SingleLineDocumentationComment.Content.Count - 1; i <= loopTo; i++) { var node = SingleLineDocumentationComment.Content[i]; if (!node.IsKind(CS.SyntaxKind.XmlText) && node.GetLeadingTrivia().Count > 0 && node.GetLeadingTrivia().First().IsKind(CS.SyntaxKind.DocumentationCommentExteriorTrivia)) { if (i < SingleLineDocumentationComment.Content.Count - 1) { var NextNode = SingleLineDocumentationComment.Content[i + 1]; if (!NextNode.IsKind(CS.SyntaxKind.XmlText) || NextNode.GetLeadingTrivia().Count == 0 || !NextNode.GetLeadingTrivia().First().IsKind(CS.SyntaxKind.DocumentationCommentExteriorTrivia)) { xmlNodes.Add(VBFactory.XmlText(" ").WithLeadingTrivia(VBFactory.DocumentationCommentExteriorTrivia("'''"))); } } node = node.WithoutLeadingTrivia(); } VBS.XmlNodeSyntax Item = (VBS.XmlNodeSyntax)node.Accept(walker); xmlNodes.Add(Item); } var DocumentationCommentTrivia = VBFactory.DocumentationCommentTrivia(VBFactory.List(xmlNodes.ToArray())); if (!DocumentationCommentTrivia.HasLeadingTrivia || !DocumentationCommentTrivia.GetLeadingTrivia()[0].IsKind(VB.SyntaxKind.DocumentationCommentExteriorTrivia)) { DocumentationCommentTrivia = DocumentationCommentTrivia.WithLeadingTrivia(VBFactory.DocumentationCommentExteriorTrivia("''' ")); } var _DocumentationComment = VBFactory.Trivia(DocumentationCommentTrivia.WithTrailingTrivia(VBFactory.EndOfLine(""))); return(_DocumentationComment); } case (int)CS.SyntaxKind.PragmaChecksumDirectiveTrivia: { CSS.PragmaChecksumDirectiveTriviaSyntax PragmaChecksumDirective = (CSS.PragmaChecksumDirectiveTriviaSyntax)StructuredTrivia; var Guid1 = VBFactory.ParseToken(PragmaChecksumDirective.Guid.Text.ToUpperInvariant()); var Bytes = VBFactory.ParseToken(PragmaChecksumDirective.Bytes.Text); var ExternalSource = VBFactory.ParseToken(PragmaChecksumDirective.File.Text); return(VBFactory.Trivia(WithAppendedTriviaFromEndOfDirectiveToken( VBFactory.ExternalChecksumDirectiveTrivia(global::VisualBasicSyntaxFactory.HashToken, global::VisualBasicSyntaxFactory.ExternalChecksumKeyword, global::VisualBasicSyntaxFactory.OpenParenToken, ExternalSource, global::VisualBasicSyntaxFactory.CommaToken, Guid1, global::VisualBasicSyntaxFactory.CommaToken, Bytes, global::VisualBasicSyntaxFactory.CloseParenToken), PragmaChecksumDirective.EndOfDirectiveToken))); } case (int)CS.SyntaxKind.SkippedTokensTrivia: { var Builder = new StringBuilder(); foreach (SyntaxToken tok in ((CSS.SkippedTokensTriviaSyntax)StructuredTrivia).Tokens) { Builder.Append(tok.ToString()); } return(VBFactory.CommentTrivia($"' TODO: Error SkippedTokensTrivia '{Builder}'")); } case (int)CS.SyntaxKind.BadDirectiveTrivia: { return(VBFactory.CommentTrivia($"' TODO: Skipped BadDirectiveTrivia")); } case (int)CS.SyntaxKind.ConflictMarkerTrivia: { break; } case (int)CS.SyntaxKind.LoadDirectiveTrivia: { break; } default: { Debug.WriteLine(((VB.SyntaxKind)Conversions.ToUShort(t.RawKind)).ToString()); break; } } throw new NotImplementedException($"t.Kind({(VB.SyntaxKind)Conversions.ToUShort(t.RawKind)}) Is unknown"); }