예제 #1
0
        static void SerializeMessageOrTerm(IndentingWriter writer,
                                           Ast.MessageTermBase messageOrTerm)
        {
            if (messageOrTerm.Comment != null)
            {
                SerializeComment(writer, messageOrTerm.Comment);
                writer.Write("\n");
            }

            SerializeIdentifier(writer, messageOrTerm.Id);
            writer.Write(" =");

            if (messageOrTerm.Value != null)
            {
                SerializeValue(writer, messageOrTerm.Value);
            }

            if (messageOrTerm.Attributes != null)
            {
                foreach (var attribute in messageOrTerm.Attributes)
                {
                    SerializeAttribute(writer, attribute);
                }
            }
            writer.Write('\n');
        }
예제 #2
0
 static void SerializeStringLiteral(IndentingWriter writer,
                                    Ast.StringLiteral expr)
 {
     writer.Write('"');
     writer.Write(expr.Value);
     writer.Write('"');
 }
예제 #3
0
        static void SerializeComment(IndentingWriter writer,
                                     Ast.BaseComment comment, string prefix)
        {
            var lines = comment.Content.Split('\n');

            for (int i = 0; i < lines.Length; ++i)
            {
                if (i > 0)
                {
                    writer.Write('\n');
                }

                var line = lines[i];
                if (String.IsNullOrEmpty(line))
                {
                    writer.Write(prefix);
                }
                else
                {
                    writer.Write(prefix);
                    writer.Write(' ');
                    writer.Write(line);
                }
            }
        }
예제 #4
0
 static void SerializeVariantExpression(IndentingWriter writer,
                                        Ast.VariantExpression expr)
 {
     SerializeExpression(writer, expr.Reference);
     writer.Write('[');
     SerializeVariantKey(writer, expr.Key);
     writer.Write(']');
 }
예제 #5
0
 static void SerializeAttribute(IndentingWriter writer, Ast.Attribute attribute)
 {
     writer.Indent();
     writer.Write("\n.");
     SerializeIdentifier(writer, attribute.Id);
     writer.Write(" =");
     SerializeValue(writer, attribute.Value);
     writer.Dedent();
 }
예제 #6
0
 static void SerializeVariantList(IndentingWriter writer, Ast.VariantList variantList)
 {
     writer.Indent();
     writer.Write("\n{");
     foreach (var variant in variantList.Variants)
     {
         SerializeVariant(writer, variant);
     }
     writer.Write("\n}");
     writer.Dedent();
 }
예제 #7
0
        static void SerializeSelectExpression(IndentingWriter writer,
                                              Ast.SelectExpression expr)
        {
            SerializeExpression(writer, expr.Selector);
            writer.Write(" ->");

            if (expr.Variants != null)
            {
                foreach (var variant in expr.Variants)
                {
                    SerializeVariant(writer, variant);
                }
            }
            writer.Write('\n');
        }
예제 #8
0
 static void SerializeJunk(IndentingWriter writer, Ast.Junk junk)
 {
     if (!String.IsNullOrEmpty(junk.Content))
     {
         writer.Write(junk.Content);
     }
 }
예제 #9
0
 static void SerializeNamedArgument(IndentingWriter writer,
                                    Ast.NamedArgument arg)
 {
     SerializeIdentifier(writer, arg.Name);
     writer.Write(": ");
     SerializeArgumentValue(writer, arg.Value);
 }
예제 #10
0
 static void SerializeAttributeExpression(IndentingWriter writer,
                                          Ast.AttributeExpression expr)
 {
     SerializeExpression(writer, expr.Ref);
     writer.Write('.');
     SerializeIdentifier(writer, expr.Name);
 }
예제 #11
0
 static void SerializeVariantName(IndentingWriter writer,
                                  Ast.VariantName name)
 {
     if (!String.IsNullOrEmpty(name.Name))
     {
         writer.Write(name.Name);
     }
 }
예제 #12
0
 static void SerializeIdentifier(IndentingWriter writer,
                                 Ast.Identifier id)
 {
     if (!String.IsNullOrEmpty(id.Name))
     {
         writer.Write(id.Name);
     }
 }
예제 #13
0
 static void SerializeTextElement(IndentingWriter writer,
                                  Ast.TextElement text)
 {
     if (!String.IsNullOrEmpty(text.Value))
     {
         writer.Write(text.Value);
     }
 }
예제 #14
0
        static void SerializeCallExpression(IndentingWriter writer,
                                            Ast.CallExpression expr)
        {
            SerializeFunction(writer, expr.Callee);
            writer.Write('(');
            bool first = true;

            if (expr.Positional != null)
            {
                SerializeArgumentList(writer, ref first, expr.Positional,
                                      (arg) => SerializeExpression(writer, arg));
            }
            if (expr.Named != null)
            {
                SerializeArgumentList(writer, ref first, expr.Named,
                                      (arg) => SerializeNamedArgument(writer, arg));
            }
            writer.Write(')');
        }
예제 #15
0
        static void SerializePattern(IndentingWriter writer, Ast.Pattern pattern)
        {
            writer.Indent();
            if (pattern.Elements.Any(IncludesNewLine) ||
                pattern.Elements.Any(IsSelectExpression))
            {
                writer.Write('\n');
            }
            else
            {
                writer.Write(' ');
            }

            foreach (var element in pattern.Elements)
            {
                SerializeElement(writer, element);
            }

            writer.Dedent();
        }
예제 #16
0
 static void SerializeVariant(IndentingWriter writer,
                              Ast.Variant variant)
 {
     writer.Write('\n');
     // squiffy indentation: 3 spaces for default, otherwise 4
     if (variant.IsDefault)
     {
         writer.Write("   *");
     }
     else
     {
         writer.Write("    ");
     }
     writer.Write('[');
     SerializeVariantKey(writer, variant.Key);
     writer.Write(']');
     writer.Indent();
     SerializeValue(writer, variant.Value);
     writer.Dedent();
 }
예제 #17
0
 static void SerializeEntry(IndentingWriter writer, Ast.Entry entry, State state)
 {
     if (entry is Ast.MessageTermBase messageOrTerm)
     {
         SerializeMessageOrTerm(writer, messageOrTerm);
     }
     else if (entry is Ast.BaseComment)
     {
         if (IsFlagSet(state, State.HasEntries))
         {
             writer.Write('\n');
         }
         if (entry is Ast.Comment comment)
         {
             SerializeComment(writer, comment);
         }
         else if (entry is Ast.GroupComment groupComment)
         {
             SerializeGroupComment(writer, groupComment);
         }
         else if (entry is Ast.ResourceComment resourceComment)
         {
             SerializeResourceComment(writer, resourceComment);
         }
         else
         {
             throw new InvalidOperationException(
                       $"Unknown comment type {entry.GetType()}");
         }
         writer.Write("\n\n");
     }
     else if (entry is Ast.Junk junk)
     {
         SerializeJunk(writer, junk);
     }
     else
     {
         throw new InvalidOperationException($"Unknown entry type {entry.GetType()}");
     }
 }
예제 #18
0
        static void SerializePlaceable(IndentingWriter writer,
                                       Ast.Placeable placeable)
        {
            var expr = placeable.Expression;

            if (expr is Ast.Placeable placeableExpr)
            {
                writer.Write('{');
                SerializePlaceable(writer, placeableExpr);
                writer.Write('}');
            }
            else if (expr is Ast.SelectExpression selectExpr)
            {
                // Special-case select expression to control the whitespace around the
                // opening and the closing brace.
                writer.Write("{ ");
                // TODO: writer.Write(');
                // TODO: if (selectExpr.Selector != null)
                // TODO: {
                // TODO:     writer.Write(' ');
                // TODO: }
                SerializeSelectExpression(writer, selectExpr);
                writer.Write('}');
            }
            else
            {
                writer.Write("{ ");
                SerializeExpression(writer, expr);
                writer.Write(" }");
            }
        }
예제 #19
0
 static void SerializeArgumentList <T>(IndentingWriter writer,
                                       ref bool first, IReadOnlyList <T> args, Action <T> serializeArg)
 {
     foreach (var arg in args)
     {
         if (!first)
         {
             writer.Write(", ");
         }
         first = false;
         serializeArg(arg);
     }
 }
예제 #20
0
 static void SerializeFunction(IndentingWriter writer,
                               Ast.Function fun /* it wasn't */)
 {
     writer.Write(fun.Name);
 }
예제 #21
0
 static void SerializeVariableReference(IndentingWriter writer,
                                        Ast.VariableReference varReference)
 {
     writer.Write('$');
     SerializeIdentifier(writer, varReference.Id);
 }
예제 #22
0
 static void SerializeNumberLiteral(IndentingWriter writer,
                                    Ast.NumberLiteral expr)
 {
     writer.Write(expr.Value);
 }