|
| 1 | +using System.Linq.Expressions; |
| 2 | +using Hyperbee.Collections; |
| 3 | +using Hyperbee.Expressions; |
| 4 | +using Hyperbee.XS; |
| 5 | +using Hyperbee.XS.Core; |
| 6 | +using Hyperbee.XS.Core.Parsers; |
| 7 | +using Hyperbee.XS.Core.Writer; |
| 8 | +using Parlot.Fluent; |
| 9 | +using static Parlot.Fluent.Parsers; |
| 10 | + |
| 11 | +namespace Hyperbee.Xs.Extensions; |
| 12 | + |
| 13 | +public class EnumerableParseExtension : IParseExtension, IExpressionWriter, IXsWriter |
| 14 | +{ |
| 15 | + public ExtensionType Type => ExtensionType.Expression; |
| 16 | + public string Key => "enumerable"; |
| 17 | + |
| 18 | + public Parser<Expression> CreateParser( ExtensionBinder binder ) |
| 19 | + { |
| 20 | + var (_, statement) = binder; |
| 21 | + |
| 22 | + return XsParsers.Bounded( |
| 23 | + static ctx => |
| 24 | + { |
| 25 | + ctx.EnterScope( FrameType.Block ); |
| 26 | + }, |
| 27 | + Between( |
| 28 | + // This is basically a block, but we need the parts |
| 29 | + Terms.Char( '{' ), |
| 30 | + ZeroOrMany( statement ), |
| 31 | + Terms.Char( '}' ) |
| 32 | + ).Named( "enumerable block" ) |
| 33 | + .Then<Expression>( static ( ctx, parts ) => |
| 34 | + ExpressionExtensions.BlockEnumerable( |
| 35 | + [.. ctx.Scope().Variables.EnumerateValues( LinkedNode.Current )], |
| 36 | + [.. parts] |
| 37 | + ) |
| 38 | + ), |
| 39 | + static ctx => |
| 40 | + { |
| 41 | + ctx.ExitScope(); |
| 42 | + } |
| 43 | + ).Named( "enumerable" ); |
| 44 | + } |
| 45 | + |
| 46 | + public bool CanWrite( Expression node ) |
| 47 | + { |
| 48 | + return node is EnumerableBlockExpression; |
| 49 | + } |
| 50 | + |
| 51 | + public void WriteExpression( Expression node, ExpressionWriterContext context ) |
| 52 | + { |
| 53 | + if ( node is not EnumerableBlockExpression enumerableBlockExpression ) |
| 54 | + return; |
| 55 | + |
| 56 | + using var writer = context.EnterExpression( "Hyperbee.Expressions.ExpressionExtensions.BlockEnumerable", true, false ); |
| 57 | + |
| 58 | + var variableCount = enumerableBlockExpression.Variables.Count; |
| 59 | + |
| 60 | + if ( variableCount > 0 ) |
| 61 | + { |
| 62 | + writer.WriteParamExpressions( enumerableBlockExpression.Variables, true ); |
| 63 | + } |
| 64 | + |
| 65 | + var expressionCount = enumerableBlockExpression.Expressions.Count; |
| 66 | + |
| 67 | + if ( expressionCount != 0 ) |
| 68 | + { |
| 69 | + if ( variableCount > 0 ) |
| 70 | + { |
| 71 | + writer.Write( ",\n" ); |
| 72 | + } |
| 73 | + |
| 74 | + for ( var i = 0; i < expressionCount; i++ ) |
| 75 | + { |
| 76 | + writer.WriteExpression( enumerableBlockExpression.Expressions[i] ); |
| 77 | + if ( i < expressionCount - 1 ) |
| 78 | + { |
| 79 | + writer.Write( "," ); |
| 80 | + } |
| 81 | + writer.Write( "\n" ); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + public void WriteExpression( Expression node, XsWriterContext context ) |
| 87 | + { |
| 88 | + if ( node is not EnumerableBlockExpression enumerableBlockExpression ) |
| 89 | + return; |
| 90 | + |
| 91 | + using var writer = context.GetWriter(); |
| 92 | + |
| 93 | + var expressionCount = enumerableBlockExpression.Expressions.Count; |
| 94 | + |
| 95 | + writer.Write( "enumerable {\n" ); |
| 96 | + writer.Indent(); |
| 97 | + |
| 98 | + for ( var i = 0; i < expressionCount; i++ ) |
| 99 | + { |
| 100 | + writer.WriteExpression( enumerableBlockExpression.Expressions[i] ); |
| 101 | + writer.WriteTerminated(); |
| 102 | + } |
| 103 | + |
| 104 | + writer.Outdent(); |
| 105 | + writer.Write( "}\n" ); |
| 106 | + context.SkipTerminated = true; |
| 107 | + } |
| 108 | +} |
0 commit comments