Moved Parser.cs to MixFix.cs to keep the grammar generation separate.

This commit is contained in:
Suzanne Soy 2020-08-21 01:32:18 +00:00
parent fce4e7f53c
commit 62f7209b14
4 changed files with 7 additions and 89 deletions

View File

@ -14,6 +14,8 @@ public static class LexerGenerator {
Case("Space"), Case("Space"),
Case("Int"), Case("Int"),
Case("Decimal"), Case("Decimal"),
Case("Eq"),
Case("Space"),
Case("String"), Case("String"),
Case("StringOpen"), Case("StringOpen"),
Case("StringClose")), Case("StringClose")),

View File

@ -8,53 +8,7 @@ using S = Lexer.S;
using Lexeme = Lexer.Lexeme; using Lexeme = Lexer.Lexeme;
using static Global; using static Global;
using PrecedenceDAG = ImmutableDefaultDictionary<string, Parser.DAGNode>;
public static partial class Parser { public static partial class Parser {
public static DAGNode EmptyDAGNode = new DAGNode(
infixLeftAssociative: ImmutableList<Operator>.Empty,
prefix: ImmutableList<Operator>.Empty,
closed: ImmutableList<Operator>.Empty,
terminal: ImmutableList<Operator>.Empty,
infixRightAssociative: ImmutableList<Operator>.Empty,
infixNonAssociative: ImmutableList<Operator>.Empty,
postfix: ImmutableList<Operator>.Empty,
successorNodes: ImmutableList<string>.Empty
);
public static PrecedenceDAG DefaultPrecedenceDAG
= new PrecedenceDAG(EmptyDAGNode);
public static Whole With<Whole>(this ILens<DAGNode, Whole> 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<Lexeme> e) {
}
public static Ast.Expr Parse(string source) { public static Ast.Expr Parse(string source) {
return Lexer.Lex(source) return Lexer.Lex(source)
.SelectMany(lexeme => .SelectMany(lexeme =>
@ -62,6 +16,7 @@ public static partial class Parser {
Int: () => Ast.Expr.Int(Int32.Parse(lexeme.lexeme)).Singleton(), Int: () => Ast.Expr.Int(Int32.Parse(lexeme.lexeme)).Singleton(),
String: () => Ast.Expr.String(lexeme.lexeme).Singleton(), String: () => Ast.Expr.String(lexeme.lexeme).Singleton(),
Space: () => Enumerable.Empty<Ast.Expr>(), // ignore Space: () => Enumerable.Empty<Ast.Expr>(), // ignore
Eq: () => Enumerable.Empty<Ast.Expr>(),
End: () => Enumerable.Empty<Ast.Expr>(), End: () => Enumerable.Empty<Ast.Expr>(),
Decimal: () => Enumerable.Empty<Ast.Expr>(), Decimal: () => Enumerable.Empty<Ast.Expr>(),
StringOpen: () => Enumerable.Empty<Ast.Expr>(), StringOpen: () => Enumerable.Empty<Ast.Expr>(),

View File

@ -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<Parser.Grammar>", "Or"),
Case("ImmutableList<Parser.Grammar>", "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<S>", "parts"),
Field("ImmutableList<string>", "holes")),
Record("DAGNode",
Field("ImmutableList<Operator>", "infixLeftAssociative"),
Field("ImmutableList<Operator>", "infixRightAssociative"),
Field("ImmutableList<Operator>", "infixNonAssociative"),
Field("ImmutableList<Operator>", "prefix"),
Field("ImmutableList<Operator>", "postfix"),
Field("ImmutableList<Operator>", "closed"),
Field("ImmutableList<Operator>", "terminal"),
Field("ImmutableList<string>", "successorNodes"))));
}
}

View File

@ -145,7 +145,10 @@ public static class VariantGenerator {
} }
private static void VariantClass(this Action<string> w, string qualifier, string name, Variant variant) { private static void VariantClass(this Action<string> 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.PrivateConstructor(qualifier, name, variant);
w($""); w($"");
w.Visitor(qualifier, name, variant); w.Visitor(qualifier, name, variant);