From 07b70621212bf0abcf5758fbf971dc8facbfff39 Mon Sep 17 00:00:00 2001 From: Brian Rogers Date: Sun, 15 Apr 2018 12:13:55 -0700 Subject: [PATCH] ExpressionSample - reimplement stmt parser in terms of expr --- .../GWParse.Test/Statements/Dim.cs | 2 +- .../GWParse/Expressions/Expr.cs | 10 +++- .../GWParse/Statements/AssignmentStatement.cs | 34 +------------ .../GWParse/Statements/DimensionStatement.cs | 34 +------------ .../GWParse/Statements/Stmt.cs | 50 +++---------------- 5 files changed, 20 insertions(+), 110 deletions(-) diff --git a/projects/ExpressionSample/GWParse.Test/Statements/Dim.cs b/projects/ExpressionSample/GWParse.Test/Statements/Dim.cs index 28cf852..d9743e6 100644 --- a/projects/ExpressionSample/GWParse.Test/Statements/Dim.cs +++ b/projects/ExpressionSample/GWParse.Test/Statements/Dim.cs @@ -11,7 +11,7 @@ public sealed class Dim [InlineData("DIM R(1)", "Dim(NumA(R, NumL(1)))")] [InlineData("DIM R(1,2)", "Dim(NumA(R, NumL(1), NumL(2)))")] [Theory] - public void Valid(string input, string output) + public void OneItem(string input, string output) { Test.Good(input, output); } diff --git a/projects/ExpressionSample/GWParse/Expressions/Expr.cs b/projects/ExpressionSample/GWParse/Expressions/Expr.cs index 2a6d3b1..445e7a7 100644 --- a/projects/ExpressionSample/GWParse/Expressions/Expr.cs +++ b/projects/ExpressionSample/GWParse/Expressions/Expr.cs @@ -11,7 +11,11 @@ namespace GWParse.Expressions internal static class Expr { - private static readonly Parser Any = Parse.Ref(() => Root); + public static readonly Parser Any = Parse.Ref(() => Root); + + public static readonly Parser AnyArray = Var.ArrayAny; + + public static readonly Parser AnyVar = Var.Any; private static readonly Parser Paren = from lp in Ch.LeftParen @@ -144,6 +148,10 @@ private static class Var public static readonly Parser NumAny = NumArray.Or(NumScalar); public static readonly Parser StrAny = StrArray.Or(StrScalar); + + public static readonly Parser ArrayAny = StrArray.Or(NumArray); + + public static readonly Parser Any = StrAny.Or(NumAny); } private static class Str diff --git a/projects/ExpressionSample/GWParse/Statements/AssignmentStatement.cs b/projects/ExpressionSample/GWParse/Statements/AssignmentStatement.cs index afad697..8e82f8a 100644 --- a/projects/ExpressionSample/GWParse/Statements/AssignmentStatement.cs +++ b/projects/ExpressionSample/GWParse/Statements/AssignmentStatement.cs @@ -4,51 +4,19 @@ namespace GWParse.Statements { - using System; using GWParse.Expressions; internal sealed class AssignmentStatement : BasicStatement { - private static readonly IExpressionVisitor Visitor = new LValueVisitor(); - private readonly BasicExpression left; private readonly BasicExpression right; public AssignmentStatement(BasicExpression left, BasicExpression right) { - this.left = EnsureLValue(left); + this.left = left; this.right = right; } public override string ToString() => "Assign(" + this.left + ", " + this.right + ")"; - - private static BasicExpression EnsureLValue(BasicExpression left) - { - left.Accept(Visitor); - return left; - } - - private sealed class LValueVisitor : IExpressionVisitor - { - public void Array(BasicType type, string name, BasicExpression[] subs) - { - // Allowed - } - - public void Literal(BasicType type, object o) - { - throw new NotSupportedException("Not an L-value."); - } - - public void Operator(string name, BasicExpression[] operands) - { - throw new NotSupportedException("Not an L-value."); - } - - public void Variable(BasicType type, string name) - { - // Allowed - } - } } } \ No newline at end of file diff --git a/projects/ExpressionSample/GWParse/Statements/DimensionStatement.cs b/projects/ExpressionSample/GWParse/Statements/DimensionStatement.cs index a65e380..ff473bb 100644 --- a/projects/ExpressionSample/GWParse/Statements/DimensionStatement.cs +++ b/projects/ExpressionSample/GWParse/Statements/DimensionStatement.cs @@ -4,49 +4,17 @@ namespace GWParse.Statements { - using System; using GWParse.Expressions; internal sealed class DimensionStatement : BasicStatement { - private static readonly IExpressionVisitor Visitor = new ArrayVisitor(); - private readonly BasicExpression a; public DimensionStatement(BasicExpression a) { - this.a = EnsureArray(a); + this.a = a; } public override string ToString() => "Dim(" + this.a + ")"; - - private static BasicExpression EnsureArray(BasicExpression a) - { - a.Accept(Visitor); - return a; - } - - private sealed class ArrayVisitor : IExpressionVisitor - { - public void Array(BasicType type, string name, BasicExpression[] subs) - { - // Allowed - } - - public void Literal(BasicType type, object o) - { - throw new NotSupportedException("Not an array."); - } - - public void Operator(string name, BasicExpression[] operands) - { - throw new NotSupportedException("Not an array."); - } - - public void Variable(BasicType type, string name) - { - throw new NotSupportedException("Not an array."); - } - } } } \ No newline at end of file diff --git a/projects/ExpressionSample/GWParse/Statements/Stmt.cs b/projects/ExpressionSample/GWParse/Statements/Stmt.cs index b9c5b1c..8dcb30c 100644 --- a/projects/ExpressionSample/GWParse/Statements/Stmt.cs +++ b/projects/ExpressionSample/GWParse/Statements/Stmt.cs @@ -5,6 +5,8 @@ namespace GWParse.Statements { using System; + using System.Collections.Generic; + using System.Linq; using GWParse.Expressions; using Sprache; @@ -27,14 +29,14 @@ internal static class Stmt private static readonly Parser Dim = from k in Parse.IgnoreCase("DIM").Token() - from s in Parse.AnyChar.AtLeastOnce().Text() - select DimA(s); + from a in Expr.AnyArray + select new DimensionStatement(a); private static readonly Parser Assign = - from left in Parse.CharExcept('=').AtLeastOnce().Text() - from o in Parse.Char('=') - from right in Parse.AnyChar.AtLeastOnce().Text() - select Asgn(left, right); + from left in Expr.AnyVar + from o in Parse.Char('=').Token() + from right in Expr.Any + select new AssignmentStatement(left, right); private static readonly Parser Any = Rem.Or(Cls).Or(Dim).Or(Assign); @@ -50,41 +52,5 @@ public static BasicStatement FromString(string input) throw new FormatException("Bad statement '" + input + "'.", e); } } - - private static BasicStatement Asgn(string left, string right) - { - try - { - return new AssignmentStatement(Expr(left), Expr(right)); - } - catch (NotSupportedException e) - { - throw new ParseException(e.Message); - } - } - - private static BasicStatement DimA(string a) - { - try - { - return new DimensionStatement(Expr(a)); - } - catch (NotSupportedException e) - { - throw new ParseException(e.Message); - } - } - - private static BasicExpression Expr(string s) - { - try - { - return BasicExpression.FromString(s); - } - catch (FormatException e) - { - throw new ParseException(e.Message); - } - } } } \ No newline at end of file