CSharp · Console
using System;
using Sprache;
using System.Linq;
using System.Collections.Generic;
namespace dotnet_parser
{
class Program
{
/*
ex := op '(' pr ')' //expression
pr := id ',' lt | ex (',' ex)* //parameter (* means zero or more)
op := string //operator
id := string //id
lt := quoted-string | number //literal
*/
static void Main(string[] args)
{
var ex = Parser.getEx().Parse(@"
OR(
AND(
EQUAL(title, ""Foo""),
AND(
GREATER_THAN(views, 50),
EQUAL(timestamp, 123456789)
)
),
EQUAL(id, ""123"")
)");
var sd =
new StoreData
{
id = "321",
title = "Foo",
views = 49,
timestamp = 123456789
};
var res = Calc.Run(ex, sd);
Console.WriteLine($"result {res}");
}
public class StoreData
{
public string id { get; set; }
public string title { get; set; }
public string content { get; set; }
public int views { get; set; }
public int timestamp { set; get; }
}
public class Calc {
public static bool Run<T>(Parser.Ex ex, T t) {
switch(ex.Op) {
case "NOT":
{
if(ex.Pr is Parser.PrEx exPr && exPr.Ex.Length == 1) {
return Run(exPr.Ex[0], t);
}
else {
throw new Exception($"invalid parameters to {ex.Op}");
}
}
case "AND":
case "OR":
{
if(ex.Pr is Parser.PrEx exPr && exPr.Ex.Length == 2) {
var l = Run(exPr.Ex[0], t);
var r = Run(exPr.Ex[1], t);
switch(ex.Op) {
case "AND": return l && r;
case "OR" : return l || r;
default:
throw new Exception();
}
}
else {
throw new Exception($"invalid parameters to {ex.Op}");
}
}
case "EQUAL":
case "GREATER_THAN":
case "LESS_THAN":
{
if(ex.Pr is Parser.PrIdLt exIdLt) {
var tt = typeof(T);
var p = tt.GetProperty(exIdLt.Id);
if(p == null) throw new Exception($"no property {exIdLt.Id} on {tt.Name}");
if(p.PropertyType == typeof(string) && exIdLt.Lt is Parser.LtQuotedString ltQuotedString){
var pval = p.GetValue(t) as string;
switch(ex.Op){
case "EQUAL":
return pval == ltQuotedString.Val;
default:
throw new Exception($"{ex.Op} invalid operator for string");
}
}
else if(p.PropertyType == typeof(int) && exIdLt.Lt is Parser.LtNumber ltNumber){
var pval = (int)p.GetValue(t);
int lval;
if(!int.TryParse(ltNumber.Val, out lval)) throw new Exception($"{ex.Op} {exIdLt.Id} {ltNumber.Val} is not a number");
switch(ex.Op){
case "EQUAL" : return pval == lval;
case "GREATER_THAN": return pval > lval;
case "LESS_THAN" : return pval < lval;
default:
throw new Exception($"{ex.Op} invalid operator for string");
}
}
else {
throw new Exception($"{ex.Op} {exIdLt.Id} invalid type");
}
}
else {
throw new Exception($"invalid parameters to {ex.Op}");
}
}
default:
throw new Exception($"{ex.Op} unknown operator");
}
}
}
public class Parser {
public class Ex {
public string Op { get; set; }
public Pr Pr { get; set; }
}
public interface Pr { }
public class PrIdLt : Pr {
public string Id { get; set; }
public Lt Lt { get; set; }
}
public class PrEx : Pr {
public Ex[] Ex { get; set; }
}
public interface Lt { }
public class LtQuotedString : Lt { public string Val { get; set; } }
public class LtNumber : Lt { public string Val { get; set; } }
public static Parser<Ex> getEx(){
return
from o in getOp()
from p in getPr().Contained(Parse.Char('('), Parse.Char(')')).Token()
select new Ex { Op = o, Pr = p };
}
public static Parser<Pr> getPr(){
var a =
from i in getId()
from c in Parse.Char(',').Token()
from l in getLt()
select new PrIdLt { Id = i, Lt = l };
var b =
from ex in Parse.Ref(() => getEx())
from em in Parse.Char(',').Token().Then(_ => Parse.Ref(() => getEx())).Many()
select new PrEx { Ex = em.Prepend(ex).ToArray() };
return a.Or<Pr>(b);
}
public static Parser<string> getOp() => Parse.Letter.Or(Parse.Char('_')).AtLeastOnce().Text().Token();
public static Parser<string> getId() => Parse.Identifier(Parse.Letter, Parse.LetterOrDigit).Token();
public static Parser<Lt> getLt(){
var quoted_string =
from open in Parse.Char('"')
from value in Parse.CharExcept('"').Many().Text()
from close in Parse.Char('"')
select value;
return
quoted_string.Select(a => new LtQuotedString { Val = a })
.Or<Lt>(Parse.Number.Select(a => new LtNumber(){ Val = a })).Token();
}
}
}
}