internal static CommandBase FromCode(SyntaxElement rootElement) { var tableNameDeclaration = rootElement.GetUniqueDescendant <NameDeclaration>( "Table Name", n => n.NameInParent == "TableName"); var mappingNameExpression = rootElement.GetUniqueDescendant <LiteralExpression>( "Mapping Name", n => n.NameInParent == "MappingName"); var mappingKindToken = rootElement.GetUniqueDescendant <SyntaxToken>( "Mapping Kind", n => n.NameInParent == "MappingKind"); var mappingFormatExpression = rootElement.GetUniqueDescendant <LiteralExpression>( "Mapping Format", n => n.NameInParent == "MappingFormat"); var mappingFormatFirstPart = QuotedText.FromLiteral(mappingFormatExpression); var mappingFormatExtraParts = rootElement .GetDescendants <CompoundStringLiteralExpression>() .SelectMany(c => c.Tokens) .Select(t => QuotedText.FromToken(t)); var mappingFormatParts = mappingFormatExtraParts .Prepend(mappingFormatFirstPart) .Select(q => q.Text); var mappingFormat = string.Concat(mappingFormatParts); var command = new CreateMappingCommand( EntityName.FromCode(tableNameDeclaration), mappingKindToken.Text, QuotedText.FromLiteral(mappingNameExpression), QuotedText.FromText(mappingFormat) !); return(command); }
private static (QuotedText?folder, QuotedText?docString) ExtractWithProperties( SyntaxElement rootElement) { var keywords = rootElement .GetDescendants <SyntaxElement>(n => n.Kind == SyntaxKind.FolderKeyword || n.Kind == SyntaxKind.DocStringKeyword); if (!keywords.Any()) { return(null, null); } else { var propertiesParent = keywords.First().Parent; var tokenSequence = propertiesParent .GetDescendants <SyntaxToken>(n => n.Kind == SyntaxKind.StringLiteralToken) .Select(n => QuotedText.FromToken(n)); var zip = keywords .Select(n => n.Kind) .Zip(tokenSequence, (k, t) => (k, t)) .ToImmutableArray(); QuotedText?folder = null; QuotedText?docString = null; foreach (var p in zip) { if (p.k == SyntaxKind.FolderKeyword) { folder = p.t; } else { docString = p.t; } } return(folder, docString); } }