From 62f7209b143076499049e69db3e44ca3c2026412 Mon Sep 17 00:00:00 2001 From: Suzanne Soy Date: Fri, 21 Aug 2020 01:32:18 +0000 Subject: [PATCH] Moved Parser.cs to MixFix.cs to keep the grammar generation separate. --- LexerGenerator.cs | 2 ++ Parser.cs | 47 +----------------------------------------- ParserGenerator.cs | 42 ------------------------------------- T4/VariantGenerator.cs | 5 ++++- 4 files changed, 7 insertions(+), 89 deletions(-) delete mode 100644 ParserGenerator.cs diff --git a/LexerGenerator.cs b/LexerGenerator.cs index 3aeba58..6cbead5 100644 --- a/LexerGenerator.cs +++ b/LexerGenerator.cs @@ -14,6 +14,8 @@ public static class LexerGenerator { Case("Space"), Case("Int"), Case("Decimal"), + Case("Eq"), + Case("Space"), Case("String"), Case("StringOpen"), Case("StringClose")), diff --git a/Parser.cs b/Parser.cs index 65b6d6c..a80a7a0 100644 --- a/Parser.cs +++ b/Parser.cs @@ -8,53 +8,7 @@ using S = Lexer.S; using Lexeme = Lexer.Lexeme; using static Global; -using PrecedenceDAG = ImmutableDefaultDictionary; - public static partial class Parser { - public static DAGNode EmptyDAGNode = new DAGNode( - infixLeftAssociative: ImmutableList.Empty, - prefix: ImmutableList.Empty, - closed: ImmutableList.Empty, - terminal: ImmutableList.Empty, - infixRightAssociative: ImmutableList.Empty, - infixNonAssociative: ImmutableList.Empty, - postfix: ImmutableList.Empty, - successorNodes: ImmutableList.Empty - ); - - public static PrecedenceDAG DefaultPrecedenceDAG - = new PrecedenceDAG(EmptyDAGNode); - - public static Whole With(this ILens node, Operator @operator) { - return @operator.fixity.Match( - Closed: - () => node.Closed().Cons(@operator), - InfixLeftAssociative: - () => node.InfixLeftAssociative().Cons(@operator), - InfixRightAssociative: - () => node.InfixRightAssociative().Cons(@operator), - InfixNonAssociative: - () => node.InfixNonAssociative().Cons(@operator), - Prefix: - () => node.Prefix().Cons(@operator), - Postfix: - () => node.Postfix().Cons(@operator), - Terminal: - () => node.Terminal().Cons(@operator) - ); - } - - public static PrecedenceDAG With(PrecedenceDAG precedenceDAG, Operator @operator) - => precedenceDAG.lens()[@operator.precedenceGroup].With(@operator); - - public static void DagToGrammar(DAGNode precedenceDAG) { - - } - - public static void RecursiveDescent(IEnumerable e) { - - } - public static Ast.Expr Parse(string source) { return Lexer.Lex(source) .SelectMany(lexeme => @@ -62,6 +16,7 @@ public static partial class Parser { Int: () => Ast.Expr.Int(Int32.Parse(lexeme.lexeme)).Singleton(), String: () => Ast.Expr.String(lexeme.lexeme).Singleton(), Space: () => Enumerable.Empty(), // ignore + Eq: () => Enumerable.Empty(), End: () => Enumerable.Empty(), Decimal: () => Enumerable.Empty(), StringOpen: () => Enumerable.Empty(), diff --git a/ParserGenerator.cs b/ParserGenerator.cs deleted file mode 100644 index bacba9d..0000000 --- a/ParserGenerator.cs +++ /dev/null @@ -1,42 +0,0 @@ -using static Generator; - -public static class ParserGenerator { - public static void Main() { - Generate( - "ParserGenerated.cs", - "using System.Collections.Immutable;\n" - + "using S = Lexer.S;", - "public static partial class Parser {", - "}", - "Parser.", - Types( - Variant("Grammar", - Case("ImmutableList", "Or"), - Case("ImmutableList", "Sequence")), - - Variant("Fixity", - Case("Closed"), - Case("InfixLeftAssociative"), - Case("InfixRightAssociative"), - Case("InfixNonAssociative"), - Case("Prefix"), - Case("Postfix"), - Case("Terminal")), - - Record("Operator", - Field("string", "precedenceGroup"), - Field("Fixity", "fixity"), - Field("ImmutableList", "parts"), - Field("ImmutableList", "holes")), - - Record("DAGNode", - Field("ImmutableList", "infixLeftAssociative"), - Field("ImmutableList", "infixRightAssociative"), - Field("ImmutableList", "infixNonAssociative"), - Field("ImmutableList", "prefix"), - Field("ImmutableList", "postfix"), - Field("ImmutableList", "closed"), - Field("ImmutableList", "terminal"), - Field("ImmutableList", "successorNodes")))); - } -} \ No newline at end of file diff --git a/T4/VariantGenerator.cs b/T4/VariantGenerator.cs index 8111ebb..bd86981 100644 --- a/T4/VariantGenerator.cs +++ b/T4/VariantGenerator.cs @@ -145,7 +145,10 @@ public static class VariantGenerator { } private static void VariantClass(this Action w, string qualifier, string name, Variant variant) { - w($" public abstract class {name} : IEquatable<{name}> {{"); + // Mark as partial to allow defining implicit conversions + // and other operators. It would be cleaner to directly + // specify these and keep the class impossible to extend. + w($" public abstract partial class {name} : IEquatable<{name}> {{"); w.PrivateConstructor(qualifier, name, variant); w($""); w.Visitor(qualifier, name, variant);