feat: Impl BooleanExpressions
This commit is contained in:
1 parent
ca95ab7a24
commit
094ea67e43
38 files changed
+3092
-503
No files matched your search
@@ -0,0 +1,21 @@
|
||||
namespace Bts.BooleanExpressions.Arguments;
|
||||
|
||||
class Argument : IArgument {
|
||||
readonly Func<IDictionary<string, object?>, object?> getValue;
|
||||
|
||||
public Argument(Func<IDictionary<string, object?>, object?> getValue) =>
|
||||
this.getValue = getValue;
|
||||
|
||||
public object? GetValue(IDictionary<string, object?> env) =>
|
||||
getValue(env);
|
||||
}
|
||||
|
||||
class Argument<T> : IArgument<T> {
|
||||
readonly Func<IDictionary<string, object?>, T> getValue;
|
||||
|
||||
public Argument(Func<IDictionary<string, object?>, T> getValue) =>
|
||||
this.getValue = getValue;
|
||||
|
||||
public T GetValue(IDictionary<string, object?> env) =>
|
||||
getValue(env);
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
namespace Bts.BooleanExpressions.Arguments;
|
||||
|
||||
using bts;
|
||||
|
||||
static class ArgumentComparer {
|
||||
public static int Compare(
|
||||
IArgument value1, IArgument value2, IDictionary<string,object?> env) {
|
||||
if (value1 is null) {
|
||||
throw new ArgumentNullException(nameof(value1));
|
||||
}
|
||||
if (value2 is null) {
|
||||
throw new ArgumentNullException(nameof(value2));
|
||||
}
|
||||
var cmp1 = value1.Cast<IComparable>();
|
||||
var cmp2 = value2.Cast<IComparable>();
|
||||
try {
|
||||
return cmp1.GetValue(env).CompareTo(cmp2.GetValue(env));
|
||||
} catch (Exception ex) {
|
||||
Log.Ui.error($"Comparison failed: {ex}");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
namespace Bts.BooleanExpressions.Arguments;
|
||||
|
||||
static class ArgumentExtensions {
|
||||
public static IArgument<T> Cast<T>(this IArgument argument) =>
|
||||
new Argument<T>(env => {
|
||||
var obj = argument.GetValue(env);
|
||||
return obj is T t ? t : throw new Exception(
|
||||
$"Expected argument.GetValue to return instance of type {typeof(T)} " +
|
||||
$"(actually {obj?.GetType()})");
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
namespace Bts.BooleanExpressions.Arguments;
|
||||
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
static class ArgumentMatcher {
|
||||
public static bool Match(
|
||||
IArgument arg1, IArgument arg2, IDictionary<string, object?> env) {
|
||||
var lobj = arg1.GetValue(env);
|
||||
var robj = arg2.GetValue(env);
|
||||
if (DBNull.Value.Equals(lobj) && robj is string) {
|
||||
return false;
|
||||
}
|
||||
if (lobj is string lstr && robj is string rstr) {
|
||||
return Regex.IsMatch(lstr, rstr);
|
||||
}
|
||||
throw new Exception(
|
||||
$"Type mismatch, cannot string match {lobj?.GetType()} with {robj?.GetType()}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
namespace Bts.BooleanExpressions.Arguments;
|
||||
|
||||
static class Constant {
|
||||
public static Constant<T> Create<T>(T value) =>
|
||||
new(value);
|
||||
}
|
||||
|
||||
class Constant<T> : IArgument<T> {
|
||||
readonly T value;
|
||||
|
||||
public Constant(T value) =>
|
||||
this.value = value;
|
||||
|
||||
public T GetValue(IDictionary<string, object?> env) =>
|
||||
value;
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
namespace Bts.BooleanExpressions.Arguments;
|
||||
|
||||
class Function : IArgument {
|
||||
public const string FunctionPrefix = "function:";
|
||||
|
||||
readonly string name;
|
||||
readonly IEnumerable<IArgument> args;
|
||||
|
||||
public Function(string name, IEnumerable<IArgument>? args = null) {
|
||||
this.name = name;
|
||||
this.args = args ?? Enumerable.Empty<IArgument>();
|
||||
}
|
||||
|
||||
public delegate object? Delegate(List<object?> args);
|
||||
|
||||
public object? GetValue(IDictionary<string, object?> env) {
|
||||
var argValues = args?.Select(a => a.GetValue(env))?.ToList() ?? new(0);
|
||||
|
||||
var k = FunctionPrefix + name;
|
||||
if (!env.TryGetValue(k, out var fnObj)) {
|
||||
throw new BooleanExpressionException($"Did not find function named '{name}'");
|
||||
}
|
||||
if (fnObj is not Delegate fn) {
|
||||
throw new BooleanExpressionException(
|
||||
$"Function {name} is not of type {typeof(Delegate)}");
|
||||
}
|
||||
return fn(argValues);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
namespace Bts.BooleanExpressions;
|
||||
|
||||
interface IArgument {
|
||||
object? GetValue(IDictionary<string, object?> env);
|
||||
}
|
||||
|
||||
interface IArgument<T> : IArgument {
|
||||
new T GetValue(IDictionary<string, object?> env);
|
||||
|
||||
object? IArgument.GetValue(IDictionary<string, object?> env) =>
|
||||
GetValue(env);
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
namespace Bts.BooleanExpressions.Arguments;
|
||||
|
||||
class Variable : IArgument {
|
||||
readonly string name;
|
||||
|
||||
public Variable(string name) =>
|
||||
this.name = name;
|
||||
|
||||
public object? GetValue(IDictionary<string, object?> env) =>
|
||||
env.TryGetValue(name, out var res) ?
|
||||
res : throw new BooleanExpressionException($"Did not find variable named '{name}'");
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
namespace Bts.BooleanExpressions;
|
||||
|
||||
class BinaryOperator : IBinaryOperator {
|
||||
readonly Func<IArgument?, IArgument?, IDictionary<string, object?>, bool> op;
|
||||
|
||||
public BinaryOperator(
|
||||
Func<IArgument?, IArgument?, IDictionary<string, object?>, bool> op) =>
|
||||
this.op = op;
|
||||
|
||||
public bool Evaluate(
|
||||
IArgument? arg1, IArgument? arg2, IDictionary<string, object?> bindings) =>
|
||||
op(arg1, arg2, bindings);
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
namespace Bts.BooleanExpressions;
|
||||
|
||||
using bts;
|
||||
|
||||
class BooleanExpression : IBooleanExpression {
|
||||
readonly Func<IDictionary<string, object?>, bool> evaluate;
|
||||
|
||||
public BooleanExpression(Func<IDictionary<string, object?>, bool> evaluate) =>
|
||||
this.evaluate = evaluate;
|
||||
|
||||
public bool Evaluate(IDictionary<string, object?> env) {
|
||||
try {
|
||||
return evaluate(env);
|
||||
} catch (Exception ex) {
|
||||
Log.assert(false, $"BooleanExpression.Evaluate failed: {ex}");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace Bts.BooleanExpressions;
|
||||
|
||||
public class BooleanExpressionException : Exception {
|
||||
public BooleanExpressionException(string format, params object[] args)
|
||||
: base(string.Format(format, args)) { }
|
||||
}
|
||||
@@ -0,0 +1,152 @@
|
||||
%namespace Bts.BooleanExpressions
|
||||
%partial
|
||||
%visibility internal
|
||||
%tokentype Token
|
||||
%parsertype BooleanExpressionParserImpl
|
||||
|
||||
%union {
|
||||
public int Integer;
|
||||
public double Real;
|
||||
public bool Boolean;
|
||||
public string String;
|
||||
public string Identifier;
|
||||
public DateTime DateTime;
|
||||
public ImmutableList<Definition> Definitions;
|
||||
public Definition Definition;
|
||||
public ImmutableList<string> Parameters;
|
||||
public ImmutableList<IArgument> Arguments;
|
||||
public IArgument Argument;
|
||||
public BooleanExpression Expression;
|
||||
public BoundBooleanExpression BoundExpression;
|
||||
}
|
||||
|
||||
/* Constants */
|
||||
%token BOOLEAN_CONSTANT, INTEGER_CONSTANT, STRING_CONSTANT, NULL_CONSTANT, DATE_TIME_CONSTANT, REAL_CONSTANT
|
||||
/* Logical Operators */
|
||||
%token OR, AND, XOR, NOT
|
||||
/* Syntax */
|
||||
%token PARENTHESIS_OPEN, PARENTHESIS_CLOSE, COMMA, IDENTIFIER, DEFINE, BEGIN_DEF, END_DEF
|
||||
/* Relational */
|
||||
%token EQUAL, LESS_EQUAL, GREATER_EQUAL, LESS, GREATER, DIFFERENT, DIFFERENT2, MATCHES
|
||||
|
||||
%left OR AND XOR
|
||||
%right NOT
|
||||
%nonassoc EQUAL
|
||||
|
||||
%type <BoundExpression> BoundExpression
|
||||
%type <Definitions> Definitions
|
||||
%type <Definition> Definition
|
||||
%type <Expression> Expression
|
||||
%type <Parameters> Parameters
|
||||
%type <Arguments> Arguments
|
||||
%type <Argument> Argument
|
||||
|
||||
%%
|
||||
|
||||
BoundExpression : Definitions Expression { $$ = new($2, $1); }
|
||||
| Expression;
|
||||
|
||||
Definitions :
|
||||
Definition { $$ = ImmutableList<Definition>.Empty.Add($1); }
|
||||
| Definitions Definition { $$ = $1.Add($2); };
|
||||
|
||||
Definition :
|
||||
DEFINE IDENTIFIER PARENTHESIS_OPEN Parameters PARENTHESIS_CLOSE BEGIN_DEF Expression END_DEF {
|
||||
$$ = new($2.Identifier, $4, $7);
|
||||
};
|
||||
|
||||
Expression :
|
||||
Expression OR Expression {
|
||||
$$ = new(env => $1.Evaluate(env) || $3.Evaluate(env));
|
||||
}
|
||||
| Expression AND Expression {
|
||||
$$ = new(env => $1.Evaluate(env) && $3.Evaluate(env));
|
||||
}
|
||||
| Expression XOR Expression {
|
||||
$$ = new(env => $1.Evaluate(env) != $3.Evaluate(env));
|
||||
}
|
||||
| NOT Expression {
|
||||
$$ = new(env => !$2.Evaluate(env));
|
||||
}
|
||||
| PARENTHESIS_OPEN Expression PARENTHESIS_CLOSE {
|
||||
$$ = $2;
|
||||
}
|
||||
| Argument GREATER Argument {
|
||||
$$ = new(env => Arguments.ArgumentComparer.Compare($1, $3, env) > 0);
|
||||
}
|
||||
| Argument GREATER_EQUAL Argument {
|
||||
$$ = new(env => Arguments.ArgumentComparer.Compare($1, $3, env) >= 0);
|
||||
}
|
||||
| Argument EQUAL Argument {
|
||||
$$ = new(env => Arguments.ArgumentComparer.Compare($1, $3, env) == 0);
|
||||
}
|
||||
| Argument LESS_EQUAL Argument {
|
||||
$$ = new(env => Arguments.ArgumentComparer.Compare($1, $3, env) <= 0);
|
||||
}
|
||||
| Argument LESS Argument {
|
||||
$$ = new(env => Arguments.ArgumentComparer.Compare($1, $3, env) < 0);
|
||||
}
|
||||
| Argument DIFFERENT Argument {
|
||||
$$ = new(env => Arguments.ArgumentComparer.Compare($1, $3, env) != 0);
|
||||
}
|
||||
| Argument DIFFERENT2 Argument {
|
||||
$$ = new(env => Arguments.ArgumentComparer.Compare($1, $3, env) != 0);
|
||||
}
|
||||
| Argument MATCHES Argument {
|
||||
$$ = new(env => Arguments.ArgumentMatcher.Match($1, $3, env));
|
||||
}
|
||||
| Argument {
|
||||
$$ = new(env => (bool)$1.GetValue(env));
|
||||
}
|
||||
;
|
||||
|
||||
Parameters :
|
||||
{
|
||||
$$ = ImmutableList<string>.Empty;
|
||||
}
|
||||
| IDENTIFIER {
|
||||
$$ = ImmutableList<string>.Empty.Add($1.Identifier);
|
||||
}
|
||||
| IDENTIFIER COMMA Parameters {
|
||||
$$ = $3.Insert(0, $1.Identifier);
|
||||
}
|
||||
;
|
||||
|
||||
Arguments :
|
||||
{
|
||||
$$ = ImmutableList<IArgument>.Empty;
|
||||
}
|
||||
| Argument {
|
||||
$$ = ImmutableList<IArgument>.Empty.Add($1);
|
||||
}
|
||||
| Arguments COMMA Argument {
|
||||
$$ = $1.Add($3);
|
||||
}
|
||||
;
|
||||
|
||||
Argument : IDENTIFIER
|
||||
{
|
||||
$$ = new Arguments.Variable($1.Identifier);
|
||||
}
|
||||
| BOOLEAN_CONSTANT {
|
||||
$$ = Arguments.Constant.Create($1.Boolean);
|
||||
}
|
||||
| NULL_CONSTANT {
|
||||
$$ = Arguments.Constant.Create(DBNull.Value);
|
||||
}
|
||||
| REAL_CONSTANT {
|
||||
$$ = Arguments.Constant.Create($1.Real);
|
||||
}
|
||||
| STRING_CONSTANT {
|
||||
$$ = Arguments.Constant.Create($1.String);
|
||||
}
|
||||
| INTEGER_CONSTANT {
|
||||
$$ = Arguments.Constant.Create($1.Integer);
|
||||
}
|
||||
| DATE_TIME_CONSTANT {
|
||||
$$ = Arguments.Constant.Create($1.DateTime);
|
||||
}
|
||||
| IDENTIFIER PARENTHESIS_OPEN Arguments PARENTHESIS_CLOSE {
|
||||
$$ = new Arguments.Function($1.Identifier, $3);
|
||||
}
|
||||
;
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace Bts.BooleanExpressions;
|
||||
|
||||
public class BooleanExpressionInterpreterException : System.Exception {
|
||||
public BooleanExpressionInterpreterException(string message)
|
||||
: base(message) { }
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
namespace Bts.BooleanExpressions;
|
||||
|
||||
using Bts.ExpressionCore;
|
||||
|
||||
public static class BooleanExpressionParser {
|
||||
public static IBooleanExpression Parse(Stream expression) =>
|
||||
new Parser().Parse(expression);
|
||||
|
||||
public static IBooleanExpression Parse(string expression) =>
|
||||
new Parser().Parse(expression);
|
||||
|
||||
class Parser : IParser<IBooleanExpression> {
|
||||
public IBooleanExpression Parse(Stream expression) =>
|
||||
new BooleanExpressionParserImpl().Parse(expression).BoundExpression;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
namespace Bts.BooleanExpressions;
|
||||
|
||||
using QUT.Gppg;
|
||||
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
partial class BooleanExpressionParserImpl {
|
||||
public BooleanExpressionParserImpl(AbstractScanner<ValueType, LexLocation>? scanner = null)
|
||||
: base(scanner) { }
|
||||
|
||||
public ValueType Parse(string s) {
|
||||
using var ms = new MemoryStream(Encoding.Default.GetBytes(s));
|
||||
return Parse(ms);
|
||||
}
|
||||
|
||||
public ValueType Parse(Stream stream) {
|
||||
Scanner = new BooleanExpressionScanner(stream);
|
||||
Parse();
|
||||
return CurrentSemanticValue;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
namespace Bts.BooleanExpressions;
|
||||
|
||||
using System;
|
||||
|
||||
partial class BooleanExpressionScanner {
|
||||
bts.Logger Logger => bts.Log.Gen;
|
||||
|
||||
public override void yyerror(string format, params object[] args) {
|
||||
base.yyerror(format, args);
|
||||
Console.WriteLine(format, args);
|
||||
Console.WriteLine();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,174 @@
|
||||
%namespace Bts.BooleanExpressions
|
||||
%scannertype BooleanExpressionScanner
|
||||
%visibility internal
|
||||
%tokentype Token
|
||||
|
||||
%option stack, minimize, parser, verbose, persistbuffer, noembedbuffers
|
||||
|
||||
/* Literal Constants */
|
||||
Letter [a-zA-Z]
|
||||
BooleanConstant 'true'|'false'
|
||||
CSStringConstant \"([^\"\\]*|\\.)*\"
|
||||
DateTimeConstant ([1-9]|'10'|'11'|'12')\/([1-9]|([1-2][0-9])|'30'|'31')\/[0-9][0-9][0-9]*
|
||||
IntegerConstant -?[0-9]+
|
||||
NullConstant 'null'
|
||||
RealConstant -?[0-9]+\.[0-9]+
|
||||
StringConstant \'([^\'\\]*|\\.)*\'
|
||||
|
||||
/* Relational Operators */
|
||||
Equal =
|
||||
LessEqual <=
|
||||
GreaterEqual >=
|
||||
Less <
|
||||
Greater >
|
||||
Different !=
|
||||
Different2 <>
|
||||
Matches ~=
|
||||
|
||||
/* Logical Operators */
|
||||
Or or
|
||||
And and
|
||||
Not not
|
||||
Xor xor
|
||||
|
||||
Define define
|
||||
BeginDef \{
|
||||
EndDef \}
|
||||
|
||||
Identifier {Letter}({Letter}*[0-9]*_*)*
|
||||
|
||||
/* Utility Tokens */
|
||||
OpenParenthesis \(
|
||||
CloseParenthesis \)
|
||||
Comma ,
|
||||
Negate '!'
|
||||
MapsTo ':'
|
||||
IsOfType '/'
|
||||
|
||||
%%
|
||||
|
||||
{StringConstant} {
|
||||
yylval.String = yytext.Substring(1, yytext.Length - 2).Replace(@"\'", "'");
|
||||
return (int)Token.STRING_CONSTANT;
|
||||
}
|
||||
|
||||
{BooleanConstant} {
|
||||
yylval.Boolean = yytext.ToLower() switch {
|
||||
"true" => true,
|
||||
"false" => false,
|
||||
_ => throw new Exception(
|
||||
string.Format("Expected yytext to be 'true' or 'false' (actually '{0}')", yytext))
|
||||
};
|
||||
return (int)Token.BOOLEAN_CONSTANT;
|
||||
}
|
||||
|
||||
{NullConstant} {
|
||||
yylval.Null = DBNull.Value;
|
||||
return (int)Token.NULL_CONSTANT;
|
||||
}
|
||||
|
||||
{DateTimeConstant} {
|
||||
yylval.DateTime = DateTime.Parse(yytext);
|
||||
return (int)Token.DATE_TIME_CONSTANT;
|
||||
}
|
||||
|
||||
{IntegerConstant} {
|
||||
yylval.Integer = int.Parse(yytext);
|
||||
return (int)Token.INTEGER_CONSTANT;
|
||||
}
|
||||
|
||||
{RealConstant} {
|
||||
yylval.Real = double.Parse(yytext);
|
||||
return (int)Token.REAL_CONSTANT;
|
||||
}
|
||||
|
||||
{OpenParenthesis} {
|
||||
return (int)Token.PARENTHESIS_OPEN;
|
||||
}
|
||||
|
||||
{CloseParenthesis} {
|
||||
return (int)Token.PARENTHESIS_CLOSE;
|
||||
}
|
||||
|
||||
{Comma} {
|
||||
return (int)Token.COMMA;
|
||||
}
|
||||
|
||||
{Negate} {
|
||||
return (int)Token.NEGATE;
|
||||
}
|
||||
|
||||
{MapsTo} {
|
||||
return (int)Token.MAPS_TO;
|
||||
}
|
||||
|
||||
{Equal} {
|
||||
return (int)Token.EQUAL;
|
||||
}
|
||||
|
||||
{LessEqual} {
|
||||
return (int)Token.LESS_EQUAL;
|
||||
}
|
||||
|
||||
{GreaterEqual} {
|
||||
return (int)Token.GREATER_EQUAL;
|
||||
}
|
||||
|
||||
{Less} {
|
||||
return (int)Token.LESS;
|
||||
}
|
||||
|
||||
{Greater} {
|
||||
return (int)Token.GREATER;
|
||||
}
|
||||
|
||||
{Different} {
|
||||
return (int)Token.DIFFERENT;
|
||||
}
|
||||
|
||||
{Different2} {
|
||||
return (int)Token.DIFFERENT2;
|
||||
}
|
||||
|
||||
{Matches} {
|
||||
return (int)Token.MATCHES;
|
||||
}
|
||||
|
||||
{Or} {
|
||||
return (int)Token.OR;
|
||||
}
|
||||
|
||||
{And} {
|
||||
return (int)Token.AND;
|
||||
}
|
||||
|
||||
{Not} {
|
||||
return (int)Token.NOT;
|
||||
}
|
||||
|
||||
{Xor} {
|
||||
return (int)Token.XOR;
|
||||
}
|
||||
|
||||
{IsOfType} {
|
||||
return (int)Token.IS_OF_TYPE;
|
||||
}
|
||||
|
||||
{Define} {
|
||||
return (int)Token.DEFINE;
|
||||
}
|
||||
|
||||
{BeginDef} {
|
||||
return (int)Token.BEGIN_DEF;
|
||||
}
|
||||
|
||||
{EndDef} {
|
||||
return (int)Token.END_DEF;
|
||||
}
|
||||
|
||||
{Identifier} {
|
||||
yylval.Identifier = yytext;
|
||||
return (int)Token.IDENTIFIER;
|
||||
}
|
||||
|
||||
%%
|
||||
@@ -2,25 +2,23 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="NUnit" Version="3.13.3" />
|
||||
<PackageReference Include="Google.Protobuf" Version="3.24.3" />
|
||||
<PackageReference Include="YaccLexTools" Version="1.2.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Reference Include="Tools">
|
||||
<HintPath>$(Externals)\bin\cstools\Tools.dll</HintPath>
|
||||
</Reference>
|
||||
<ProjectReference Include="..\ExpressionCore\ExpressionCore.csproj" />
|
||||
<ProjectReference Include="..\..\client\spark_core\spark_core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Include="$(DataStreamGrammarDir)\BooleanExpressionTokens.lexer.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Include="$(DataStreamGrammarDir)\BooleanExpressionSyntax.parser.cs" />
|
||||
<Scanner Include="BooleanExpressionTokens.lex" />
|
||||
<Parser Include="BooleanExpressionGrammar.y" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,32 @@
|
||||
namespace Bts.BooleanExpressions;
|
||||
|
||||
using Bts.BooleanExpressions.Arguments;
|
||||
|
||||
class BoundBooleanExpression : IBooleanExpression {
|
||||
readonly IBooleanExpression expression;
|
||||
readonly IDictionary<string, FunctionDefinition> definitions;
|
||||
readonly Dictionary<string, object?> identifiers;
|
||||
|
||||
public BoundBooleanExpression(
|
||||
IBooleanExpression expression, IEnumerable<Definition>? definitions = null) {
|
||||
this.expression = expression;
|
||||
var defs = definitions ?? Enumerable.Empty<Definition>();
|
||||
identifiers = defs.ToDictionary(
|
||||
kv => kv.id, kv => (object?)kv.exp);
|
||||
this.definitions = extractFunctions(defs).ToDictionary(kv => kv.Key, kv => kv.Value);
|
||||
}
|
||||
|
||||
public bool Evaluate(IDictionary<string, object?> env) {
|
||||
foreach (var kv in env) {
|
||||
identifiers[kv.Key] = kv.Value;
|
||||
}
|
||||
env = new EvaluationEnvironment(identifiers, definitions);
|
||||
return expression.Evaluate(env);
|
||||
}
|
||||
|
||||
static IEnumerable<KeyValuePair<string, FunctionDefinition>> extractFunctions(
|
||||
IEnumerable<Definition> definitions) =>
|
||||
definitions.Select(d =>
|
||||
new KeyValuePair<string, FunctionDefinition>(
|
||||
Function.FunctionPrefix + d.id, new(d.id, d.args.ToList(), d.exp)));
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
namespace Bts.BooleanExpressions;
|
||||
|
||||
using System.Collections.Immutable;
|
||||
|
||||
public class Definition {
|
||||
public Definition(string id, ImmutableList<string> args, IBooleanExpression exp) {
|
||||
this.id = id;
|
||||
this.args = args;
|
||||
this.exp = exp;
|
||||
}
|
||||
|
||||
public string id { get; }
|
||||
public ImmutableList<string> args { get; }
|
||||
public IBooleanExpression exp { get; }
|
||||
|
||||
public override string ToString() =>
|
||||
$"{id}({string.Join(", ", args)} | {exp?.GetType()})";
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
namespace Bts.BooleanExpressions;
|
||||
|
||||
using Bts.BooleanExpressions.Arguments;
|
||||
|
||||
using Spark.Core.Collections;
|
||||
|
||||
using System.Collections;
|
||||
|
||||
class EvaluationEnvironment : IDictionary<string, object?> {
|
||||
IDictionary<string, object?> userBindings;
|
||||
IDictionary<string, FunctionDefinition> definitions;
|
||||
|
||||
public EvaluationEnvironment(
|
||||
IDictionary<string, object?> userBindings,
|
||||
IDictionary<string, FunctionDefinition> definitions) {
|
||||
this.userBindings = userBindings;
|
||||
this.definitions = definitions;
|
||||
}
|
||||
|
||||
public int Count =>
|
||||
definitions.Count + userBindings.Count;
|
||||
|
||||
public bool IsReadOnly => true;
|
||||
|
||||
public ICollection<string> Keys =>
|
||||
AggregateCollection.Create(userBindings.Keys, definitions.Keys);
|
||||
|
||||
public ICollection<object?> Values =>
|
||||
AggregateCollection.Create(
|
||||
userBindings.Values, SelectorCollection.Create(definitions.Values, v => (object?)v));
|
||||
|
||||
public object? this[string key] {
|
||||
get {
|
||||
var keystr = key.ToString();
|
||||
if (keystr is null || !definitions.TryGetValue(keystr, out var fd)) {
|
||||
return userBindings.TryGetValue(key, out var ubnd) ? ubnd :
|
||||
throw new KeyNotFoundException(
|
||||
$"EvaluationEnvironment does not contain key '{key}'");
|
||||
}
|
||||
return new Function.Delegate(Evaluate);
|
||||
|
||||
object? Evaluate(List<object?> args) {
|
||||
if (args.Count != fd.args.Count)
|
||||
throw new BooleanExpressionInterpreterException(
|
||||
$"Parameter count mismatch for {fd.name}. Expected: {fd.args} Got: {args.Count}");
|
||||
else {
|
||||
var newBindings = fd.args.Zip(args).ToDictionary();
|
||||
return fd.expression.Evaluate(new EvaluationEnvironment(
|
||||
AggregateDictionary.Create(newBindings, userBindings), definitions));
|
||||
}
|
||||
}
|
||||
}
|
||||
set => throw new NotSupportedException();
|
||||
}
|
||||
|
||||
public bool ContainsKey(string key) {
|
||||
var keystr = key.ToString();
|
||||
return
|
||||
(keystr is not null && definitions.ContainsKey(keystr)) ||
|
||||
userBindings.ContainsKey(key);
|
||||
}
|
||||
|
||||
public IEnumerator<KeyValuePair<string, object?>> GetEnumerator() {
|
||||
foreach (var kv in userBindings) {
|
||||
yield return kv;
|
||||
}
|
||||
foreach (var kv in definitions) {
|
||||
if (!userBindings.ContainsKey(kv.Key)) {
|
||||
yield return new(kv.Key, kv.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public bool TryGetValue(string key, out object? value) {
|
||||
var keystr = key.ToString();
|
||||
if (keystr is null || !definitions.TryGetValue(keystr, out var fd)) {
|
||||
return userBindings.TryGetValue(key, out value);
|
||||
}
|
||||
value = new Function.Delegate(Evaluate);
|
||||
return true;
|
||||
|
||||
object? Evaluate(List<object?> args) {
|
||||
if (args.Count != fd.args.Count)
|
||||
throw new BooleanExpressionInterpreterException(
|
||||
$"Parameter count mismatch for {fd.name}. Expected: {fd.args} Got: {args.Count}");
|
||||
else {
|
||||
var newBindings = fd.args.Zip(args).ToDictionary();
|
||||
return fd.expression.Evaluate(new EvaluationEnvironment(
|
||||
AggregateDictionary.Create(newBindings, userBindings), definitions));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void CopyTo(KeyValuePair<string, object?>[] array, int arrayIndex) {
|
||||
foreach (var kv in userBindings) {
|
||||
array[arrayIndex++] = kv;
|
||||
}
|
||||
foreach (var kv in definitions) {
|
||||
array[arrayIndex++] = new(kv.Key, kv.Value);
|
||||
}
|
||||
}
|
||||
|
||||
void IDictionary<string, object?>.Add(string key, object? value) =>
|
||||
throw new NotSupportedException();
|
||||
|
||||
void ICollection<KeyValuePair<string, object?>>.Add(KeyValuePair<string, object?> item) =>
|
||||
throw new NotSupportedException();
|
||||
|
||||
void ICollection<KeyValuePair<string, object?>>.Clear() =>
|
||||
throw new NotSupportedException();
|
||||
|
||||
bool ICollection<KeyValuePair<string, object?>>.Contains(KeyValuePair<string, object?> item) =>
|
||||
TryGetValue(item.Key, out var v) && Equals(v, item.Value);
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() =>
|
||||
GetEnumerator();
|
||||
|
||||
bool IDictionary<string, object?>.Remove(string key) =>
|
||||
throw new NotSupportedException();
|
||||
|
||||
bool ICollection<KeyValuePair<string, object?>>.Remove(KeyValuePair<string, object?> item) =>
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
namespace Bts.BooleanExpressions;
|
||||
|
||||
class FunctionDefinition {
|
||||
public FunctionDefinition(
|
||||
string name, IList<string> args, IBooleanExpression expression) {
|
||||
this.name = name;
|
||||
this.args = args;
|
||||
this.expression = expression;
|
||||
}
|
||||
|
||||
public string name { get; }
|
||||
public IList<string> args { get; }
|
||||
public IBooleanExpression expression { get; }
|
||||
}
|
||||
@@ -0,0 +1,720 @@
|
||||
// ==============================================================
|
||||
// <auto-generated>
|
||||
// This code automatically produced from an embedded resource.
|
||||
// Do not edit this file, or it will become incompatible with
|
||||
// the specification from which it was generated.
|
||||
// </auto-generated>
|
||||
// ==============================================================
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Globalization;
|
||||
|
||||
namespace QUT.GplexBuffers
|
||||
{
|
||||
// Code copied from GPLEX embedded resource
|
||||
[Serializable]
|
||||
internal class BufferException : Exception
|
||||
{
|
||||
public BufferException() { }
|
||||
public BufferException(string message) : base(message) { }
|
||||
public BufferException(string message, Exception innerException)
|
||||
: base(message, innerException) { }
|
||||
protected BufferException(SerializationInfo info, StreamingContext context)
|
||||
: base(info, context) { }
|
||||
}
|
||||
|
||||
internal abstract class ScanBuff
|
||||
{
|
||||
private string fileNm;
|
||||
|
||||
public const int EndOfFile = -1;
|
||||
public const int UnicodeReplacementChar = 0xFFFD;
|
||||
|
||||
public bool IsFile { get { return (fileNm != null); } }
|
||||
public string FileName { get { return fileNm; } set { fileNm = value; } }
|
||||
|
||||
public abstract int Pos { get; set; }
|
||||
public abstract int Read();
|
||||
public virtual void Mark() { }
|
||||
|
||||
public abstract string GetString(int begin, int limit);
|
||||
|
||||
public static ScanBuff GetBuffer(string source)
|
||||
{
|
||||
return new StringBuffer(source);
|
||||
}
|
||||
|
||||
public static ScanBuff GetBuffer(IList<string> source)
|
||||
{
|
||||
return new LineBuffer(source);
|
||||
}
|
||||
|
||||
#if (!NOFILES)
|
||||
public static ScanBuff GetBuffer(Stream source)
|
||||
{
|
||||
return new BuildBuffer(source);
|
||||
}
|
||||
|
||||
#if (!BYTEMODE)
|
||||
public static ScanBuff GetBuffer(Stream source, int fallbackCodePage)
|
||||
{
|
||||
return new BuildBuffer(source, fallbackCodePage);
|
||||
}
|
||||
#endif // !BYTEMODE
|
||||
#endif // !NOFILES
|
||||
}
|
||||
|
||||
#region Buffer classes
|
||||
|
||||
// ==============================================================
|
||||
// ===== Definitions for various ScanBuff derived classes ====
|
||||
// ==============================================================
|
||||
// =============== String input ================
|
||||
// ==============================================================
|
||||
|
||||
/// <summary>
|
||||
/// This class reads characters from a single string as
|
||||
/// required, for example, by Visual Studio language services
|
||||
/// </summary>
|
||||
sealed class StringBuffer : ScanBuff
|
||||
{
|
||||
string str; // input buffer
|
||||
int bPos; // current position in buffer
|
||||
int sLen;
|
||||
|
||||
public StringBuffer(string source)
|
||||
{
|
||||
this.str = source;
|
||||
this.sLen = source.Length;
|
||||
this.FileName = null;
|
||||
}
|
||||
|
||||
public override int Read()
|
||||
{
|
||||
if (bPos < sLen) return str[bPos++];
|
||||
else if (bPos == sLen) { bPos++; return '\n'; } // one strike, see new line
|
||||
else { bPos++; return EndOfFile; } // two strikes and you're out!
|
||||
}
|
||||
|
||||
public override string GetString(int begin, int limit)
|
||||
{
|
||||
// "limit" can be greater than sLen with the BABEL
|
||||
// option set. Read returns a "virtual" EOL if
|
||||
// an attempt is made to read past the end of the
|
||||
// string buffer. Without the guard any attempt
|
||||
// to fetch yytext for a token that includes the
|
||||
// EOL will throw an index exception.
|
||||
if (limit > sLen) limit = sLen;
|
||||
if (limit <= begin) return "";
|
||||
else return str.Substring(begin, limit - begin);
|
||||
}
|
||||
|
||||
public override int Pos
|
||||
{
|
||||
get { return bPos; }
|
||||
set { bPos = value; }
|
||||
}
|
||||
|
||||
public override string ToString() { return "StringBuffer"; }
|
||||
}
|
||||
|
||||
// ==============================================================
|
||||
// The LineBuff class contributed by Nigel Horspool,
|
||||
// nigelh@cs.uvic.cs
|
||||
// ==============================================================
|
||||
|
||||
sealed class LineBuffer : ScanBuff
|
||||
{
|
||||
IList<string> line; // list of source lines from a file
|
||||
int numLines; // number of strings in line list
|
||||
string curLine; // current line in that list
|
||||
int cLine; // index of current line in the list
|
||||
int curLen; // length of current line
|
||||
int curLineStart; // position of line start in whole file
|
||||
int curLineEnd; // position of line end in whole file
|
||||
int maxPos; // max position ever visited in whole file
|
||||
int cPos; // ordinal number of code in source
|
||||
|
||||
// Constructed from a list of strings, one per source line.
|
||||
// The lines have had trailing '\n' characters removed.
|
||||
public LineBuffer(IList<string> lineList)
|
||||
{
|
||||
line = lineList;
|
||||
numLines = line.Count;
|
||||
cPos = curLineStart = 0;
|
||||
curLine = (numLines > 0 ? line[0] : "");
|
||||
maxPos = curLineEnd = curLen = curLine.Length;
|
||||
cLine = 1;
|
||||
FileName = null;
|
||||
}
|
||||
|
||||
public override int Read()
|
||||
{
|
||||
if (cPos < curLineEnd)
|
||||
return curLine[cPos++ - curLineStart];
|
||||
if (cPos++ == curLineEnd)
|
||||
return '\n';
|
||||
if (cLine >= numLines)
|
||||
return EndOfFile;
|
||||
curLine = line[cLine];
|
||||
curLen = curLine.Length;
|
||||
curLineStart = curLineEnd + 1;
|
||||
curLineEnd = curLineStart + curLen;
|
||||
if (curLineEnd > maxPos)
|
||||
maxPos = curLineEnd;
|
||||
cLine++;
|
||||
return curLen > 0 ? curLine[0] : '\n';
|
||||
}
|
||||
|
||||
// To speed up searches for the line containing a position
|
||||
private int cachedPosition;
|
||||
private int cachedIxdex;
|
||||
private int cachedLineStart;
|
||||
|
||||
// Given a position pos within the entire source, the results are
|
||||
// ix -- the index of the containing line
|
||||
// lstart -- the position of the first character on that line
|
||||
private void findIndex(int pos, out int ix, out int lstart)
|
||||
{
|
||||
if (pos >= cachedPosition)
|
||||
{
|
||||
ix = cachedIxdex; lstart = cachedLineStart;
|
||||
}
|
||||
else
|
||||
{
|
||||
ix = lstart = 0;
|
||||
}
|
||||
while (ix < numLines)
|
||||
{
|
||||
int len = line[ix].Length + 1;
|
||||
if (pos < lstart + len) break;
|
||||
lstart += len;
|
||||
ix++;
|
||||
}
|
||||
cachedPosition = pos;
|
||||
cachedIxdex = ix;
|
||||
cachedLineStart = lstart;
|
||||
}
|
||||
|
||||
public override string GetString(int begin, int limit)
|
||||
{
|
||||
if (begin >= maxPos || limit <= begin) return "";
|
||||
int endIx, begIx, endLineStart, begLineStart;
|
||||
findIndex(begin, out begIx, out begLineStart);
|
||||
int begCol = begin - begLineStart;
|
||||
findIndex(limit, out endIx, out endLineStart);
|
||||
int endCol = limit - endLineStart;
|
||||
string s = line[begIx];
|
||||
if (begIx == endIx)
|
||||
{
|
||||
// the usual case, substring all on one line
|
||||
return (endCol <= s.Length) ?
|
||||
s.Substring(begCol, endCol - begCol)
|
||||
: s.Substring(begCol) + "\n";
|
||||
}
|
||||
// the string spans multiple lines, yuk!
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (begCol < s.Length)
|
||||
sb.Append(s.Substring(begCol));
|
||||
for (; ; )
|
||||
{
|
||||
sb.Append("\n");
|
||||
s = line[++begIx];
|
||||
if (begIx >= endIx) break;
|
||||
sb.Append(s);
|
||||
}
|
||||
if (endCol <= s.Length)
|
||||
{
|
||||
sb.Append(s.Substring(0, endCol));
|
||||
}
|
||||
else
|
||||
{
|
||||
sb.Append(s);
|
||||
sb.Append("\n");
|
||||
}
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
public override int Pos
|
||||
{
|
||||
get { return cPos; }
|
||||
set
|
||||
{
|
||||
cPos = value;
|
||||
findIndex(cPos, out cLine, out curLineStart);
|
||||
// cLine should be the *next* line after curLine.
|
||||
curLine = (cLine < numLines ? line[cLine++] : "");
|
||||
curLineEnd = curLineStart + curLine.Length;
|
||||
}
|
||||
}
|
||||
|
||||
public override string ToString() { return "LineBuffer"; }
|
||||
}
|
||||
|
||||
#if (!NOFILES)
|
||||
// ==============================================================
|
||||
// ===== class BuildBuff : for unicode text files ========
|
||||
// ==============================================================
|
||||
|
||||
class BuildBuffer : ScanBuff
|
||||
{
|
||||
// Double buffer for char stream.
|
||||
class BufferElement
|
||||
{
|
||||
StringBuilder bldr = new StringBuilder();
|
||||
StringBuilder next = new StringBuilder();
|
||||
int minIx;
|
||||
int maxIx;
|
||||
int brkIx;
|
||||
bool appendToNext;
|
||||
|
||||
internal BufferElement() { }
|
||||
|
||||
internal int MaxIndex { get { return maxIx; } }
|
||||
// internal int MinIndex { get { return minIx; } }
|
||||
|
||||
internal char this[int index]
|
||||
{
|
||||
get
|
||||
{
|
||||
if (index < minIx || index >= maxIx)
|
||||
throw new BufferException("Index was outside data buffer");
|
||||
else if (index < brkIx)
|
||||
return bldr[index - minIx];
|
||||
else
|
||||
return next[index - brkIx];
|
||||
}
|
||||
}
|
||||
|
||||
internal void Append(char[] block, int count)
|
||||
{
|
||||
maxIx += count;
|
||||
if (appendToNext)
|
||||
this.next.Append(block, 0, count);
|
||||
else
|
||||
{
|
||||
this.bldr.Append(block, 0, count);
|
||||
brkIx = maxIx;
|
||||
appendToNext = true;
|
||||
}
|
||||
}
|
||||
|
||||
internal string GetString(int start, int limit)
|
||||
{
|
||||
if (limit <= start)
|
||||
return "";
|
||||
if (start >= minIx && limit <= maxIx)
|
||||
if (limit < brkIx) // String entirely in bldr builder
|
||||
return bldr.ToString(start - minIx, limit - start);
|
||||
else if (start >= brkIx) // String entirely in next builder
|
||||
return next.ToString(start - brkIx, limit - start);
|
||||
else // Must do a string-concatenation
|
||||
return
|
||||
bldr.ToString(start - minIx, brkIx - start) +
|
||||
next.ToString(0, limit - brkIx);
|
||||
else
|
||||
throw new BufferException("String was outside data buffer");
|
||||
}
|
||||
|
||||
internal void Mark(int limit)
|
||||
{
|
||||
if (limit > brkIx + 16) // Rotate blocks
|
||||
{
|
||||
StringBuilder temp = bldr;
|
||||
bldr = next;
|
||||
next = temp;
|
||||
next.Length = 0;
|
||||
minIx = brkIx;
|
||||
brkIx = maxIx;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
BufferElement data = new BufferElement();
|
||||
|
||||
int bPos; // Postion index in the StringBuilder
|
||||
BlockReader NextBlk; // Delegate that serves char-arrays;
|
||||
|
||||
private string EncodingName
|
||||
{
|
||||
get
|
||||
{
|
||||
StreamReader rdr = NextBlk.Target as StreamReader;
|
||||
return (rdr == null ? "raw-bytes" : rdr.CurrentEncoding.BodyName);
|
||||
}
|
||||
}
|
||||
|
||||
public BuildBuffer(Stream stream)
|
||||
{
|
||||
FileStream fStrm = (stream as FileStream);
|
||||
if (fStrm != null) FileName = fStrm.Name;
|
||||
NextBlk = BlockReaderFactory.Raw(stream);
|
||||
}
|
||||
|
||||
#if (!BYTEMODE)
|
||||
public BuildBuffer(Stream stream, int fallbackCodePage)
|
||||
{
|
||||
FileStream fStrm = (stream as FileStream);
|
||||
if (fStrm != null) FileName = fStrm.Name;
|
||||
NextBlk = BlockReaderFactory.Get(stream, fallbackCodePage);
|
||||
}
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Marks a conservative lower bound for the buffer,
|
||||
/// allowing space to be reclaimed. If an application
|
||||
/// needs to call GetString at arbitrary past locations
|
||||
/// in the input stream, Mark() is not called.
|
||||
/// </summary>
|
||||
public override void Mark() { data.Mark(bPos - 2); }
|
||||
|
||||
public override int Pos
|
||||
{
|
||||
get { return bPos; }
|
||||
set { bPos = value; }
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Read returns the ordinal number of the next char, or
|
||||
/// EOF (-1) for an end of stream. Note that the next
|
||||
/// code point may require *two* calls of Read().
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public override int Read()
|
||||
{
|
||||
//
|
||||
// Characters at positions
|
||||
// [data.offset, data.offset + data.bldr.Length)
|
||||
// are available in data.bldr.
|
||||
//
|
||||
if (bPos < data.MaxIndex)
|
||||
{
|
||||
// ch0 cannot be EOF
|
||||
return (int)data[bPos++];
|
||||
}
|
||||
else // Read from underlying stream
|
||||
{
|
||||
// Experimental code, blocks of page size
|
||||
char[] chrs = new char[4096];
|
||||
int count = NextBlk(chrs, 0, 4096);
|
||||
if (count == 0)
|
||||
return EndOfFile;
|
||||
else
|
||||
{
|
||||
data.Append(chrs, count);
|
||||
return (int)data[bPos++];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override string GetString(int begin, int limit)
|
||||
{
|
||||
return data.GetString(begin, limit);
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return "StringBuilder buffer, encoding: " + this.EncodingName;
|
||||
}
|
||||
}
|
||||
|
||||
// =============== End ScanBuff-derived classes ==================
|
||||
|
||||
internal delegate int BlockReader(char[] block, int index, int number);
|
||||
|
||||
// A delegate factory, serving up a delegate that
|
||||
// reads a block of characters from the underlying
|
||||
// encoded stream, via a StreamReader object.
|
||||
//
|
||||
internal static class BlockReaderFactory
|
||||
{
|
||||
public static BlockReader Raw(Stream stream)
|
||||
{
|
||||
return delegate(char[] block, int index, int number)
|
||||
{
|
||||
byte[] b = new byte[number];
|
||||
int count = stream.Read(b, 0, number);
|
||||
int i = 0;
|
||||
int j = index;
|
||||
for (; i < count; i++, j++)
|
||||
block[j] = (char)b[i];
|
||||
return count;
|
||||
};
|
||||
}
|
||||
|
||||
#if (!BYTEMODE)
|
||||
public static BlockReader Get(Stream stream, int fallbackCodePage)
|
||||
{
|
||||
Encoding encoding;
|
||||
int preamble = Preamble(stream);
|
||||
|
||||
if (preamble != 0) // There is a valid BOM here!
|
||||
encoding = Encoding.GetEncoding(preamble);
|
||||
else if (fallbackCodePage == -1) // Fallback is "raw" bytes
|
||||
return Raw(stream);
|
||||
else if (fallbackCodePage != -2) // Anything but "guess"
|
||||
encoding = Encoding.GetEncoding(fallbackCodePage);
|
||||
else // This is the "guess" option
|
||||
{
|
||||
int guess = new Guesser(stream).GuessCodePage();
|
||||
stream.Seek(0, SeekOrigin.Begin);
|
||||
if (guess == -1) // ==> this is a 7-bit file
|
||||
encoding = Encoding.ASCII;
|
||||
else if (guess == 65001)
|
||||
encoding = Encoding.UTF8;
|
||||
else // ==> use the machine default
|
||||
encoding = Encoding.Default;
|
||||
}
|
||||
StreamReader reader = new StreamReader(stream, encoding);
|
||||
return reader.Read;
|
||||
}
|
||||
|
||||
static int Preamble(Stream stream)
|
||||
{
|
||||
int b0 = stream.ReadByte();
|
||||
int b1 = stream.ReadByte();
|
||||
|
||||
if (b0 == 0xfe && b1 == 0xff)
|
||||
return 1201; // UTF16BE
|
||||
if (b0 == 0xff && b1 == 0xfe)
|
||||
return 1200; // UTF16LE
|
||||
|
||||
int b2 = stream.ReadByte();
|
||||
if (b0 == 0xef && b1 == 0xbb && b2 == 0xbf)
|
||||
return 65001; // UTF8
|
||||
//
|
||||
// There is no unicode preamble, so we
|
||||
// return denoter for the machine default.
|
||||
//
|
||||
stream.Seek(0, SeekOrigin.Begin);
|
||||
return 0;
|
||||
}
|
||||
#endif // !BYTEMODE
|
||||
}
|
||||
#endif // !NOFILES
|
||||
#endregion Buffer classes
|
||||
|
||||
// ==============================================================
|
||||
// ============ class CodePageHandling =============
|
||||
// ==============================================================
|
||||
#if (!NOFILES)
|
||||
public static class CodePageHandling
|
||||
{
|
||||
public static int GetCodePage(string option)
|
||||
{
|
||||
string command = option.ToUpperInvariant();
|
||||
if (command.StartsWith("CodePage:", StringComparison.OrdinalIgnoreCase))
|
||||
command = command.Substring(9);
|
||||
try
|
||||
{
|
||||
if (command.Equals("RAW"))
|
||||
return -1;
|
||||
else if (command.Equals("GUESS"))
|
||||
return -2;
|
||||
else if (command.Equals("DEFAULT"))
|
||||
return 0;
|
||||
else if (char.IsDigit(command[0]))
|
||||
return int.Parse(command, CultureInfo.InvariantCulture);
|
||||
else
|
||||
{
|
||||
Encoding enc = Encoding.GetEncoding(command);
|
||||
return enc.CodePage;
|
||||
}
|
||||
}
|
||||
catch (FormatException)
|
||||
{
|
||||
Console.Error.WriteLine(
|
||||
"Invalid format \"{0}\", using machine default", option);
|
||||
}
|
||||
catch (ArgumentException)
|
||||
{
|
||||
Console.Error.WriteLine(
|
||||
"Unknown code page \"{0}\", using machine default", option);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
#region guesser
|
||||
#if (!BYTEMODE)
|
||||
// ==============================================================
|
||||
// ============ Encoding Guesser =============
|
||||
// ==============================================================
|
||||
|
||||
/// <summary>
|
||||
/// This class provides a simple finite state automaton that
|
||||
/// scans the file looking for (1) valid UTF-8 byte patterns,
|
||||
/// (2) bytes >= 0x80 which are not part of a UTF-8 sequence.
|
||||
/// The method then guesses whether it is UTF-8 or maybe some
|
||||
/// local machine default encoding. This works well for the
|
||||
/// various Latin encodings.
|
||||
/// </summary>
|
||||
internal class Guesser
|
||||
{
|
||||
ScanBuff buffer;
|
||||
|
||||
public int GuessCodePage() { return Scan(); }
|
||||
|
||||
const int maxAccept = 10;
|
||||
const int initial = 0;
|
||||
const int eofNum = 0;
|
||||
const int goStart = -1;
|
||||
const int INITIAL = 0;
|
||||
const int EndToken = 0;
|
||||
|
||||
#region user code
|
||||
/*
|
||||
* Reads the bytes of a file to determine if it is
|
||||
* UTF-8 or a single-byte code page file.
|
||||
*/
|
||||
public long utfX;
|
||||
public long uppr;
|
||||
#endregion user code
|
||||
|
||||
int state;
|
||||
int currentStart = startState[0];
|
||||
int code;
|
||||
|
||||
#region ScannerTables
|
||||
static int[] startState = new int[] { 11, 0 };
|
||||
|
||||
#region CharacterMap
|
||||
static sbyte[] map = new sbyte[256] {
|
||||
/* '\0' */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
/* '\x10' */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
/* '\x20' */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
/* '0' */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
/* '@' */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
/* 'P' */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
/* '`' */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
/* 'p' */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
/* '\x80' */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
|
||||
/* '\x90' */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
|
||||
/* '\xA0' */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
|
||||
/* '\xB0' */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
|
||||
/* '\xC0' */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
||||
/* '\xD0' */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
||||
/* '\xE0' */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
|
||||
/* '\xF0' */ 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5 };
|
||||
#endregion
|
||||
|
||||
static sbyte[][] nextState = new sbyte[][] {
|
||||
new sbyte[] {0, 0, 0, 0, 0, 0},
|
||||
new sbyte[] {-1, -1, 10, -1, -1, -1},
|
||||
new sbyte[] {-1, -1, -1, -1, -1, -1},
|
||||
new sbyte[] {-1, -1, 8, -1, -1, -1},
|
||||
new sbyte[] {-1, -1, 5, -1, -1, -1},
|
||||
new sbyte[] {-1, -1, 6, -1, -1, -1},
|
||||
new sbyte[] {-1, -1, 7, -1, -1, -1},
|
||||
null,
|
||||
new sbyte[] {-1, -1, 9, -1, -1, -1},
|
||||
null,
|
||||
null,
|
||||
new sbyte[] {-1, 1, 2, 3, 4, 2}
|
||||
};
|
||||
|
||||
|
||||
[SuppressMessage("Microsoft.Performance", "CA1810:InitializeReferenceTypeStaticFieldsInline")]
|
||||
// Reason for suppression: cannot have self-reference in array initializer.
|
||||
static Guesser()
|
||||
{
|
||||
nextState[7] = nextState[2];
|
||||
nextState[9] = nextState[2];
|
||||
nextState[10] = nextState[2];
|
||||
}
|
||||
|
||||
int NextState()
|
||||
{
|
||||
if (code == ScanBuff.EndOfFile)
|
||||
return eofNum;
|
||||
else
|
||||
return nextState[state][map[code]];
|
||||
}
|
||||
#endregion
|
||||
|
||||
public Guesser(System.IO.Stream file) { SetSource(file); }
|
||||
|
||||
public void SetSource(System.IO.Stream source)
|
||||
{
|
||||
this.buffer = new BuildBuffer(source);
|
||||
code = buffer.Read();
|
||||
}
|
||||
|
||||
int Scan()
|
||||
{
|
||||
for (; ; )
|
||||
{
|
||||
int next;
|
||||
state = currentStart;
|
||||
while ((next = NextState()) == goStart)
|
||||
code = buffer.Read();
|
||||
|
||||
state = next;
|
||||
code = buffer.Read();
|
||||
|
||||
while ((next = NextState()) > eofNum)
|
||||
{
|
||||
state = next;
|
||||
code = buffer.Read();
|
||||
}
|
||||
if (state <= maxAccept)
|
||||
{
|
||||
#region ActionSwitch
|
||||
#pragma warning disable 162
|
||||
switch (state)
|
||||
{
|
||||
case eofNum:
|
||||
switch (currentStart)
|
||||
{
|
||||
case 11:
|
||||
if (utfX == 0 && uppr == 0) return -1; /* raw ascii */
|
||||
else if (uppr * 10 > utfX) return 0; /* default code page */
|
||||
else return 65001; /* UTF-8 encoding */
|
||||
break;
|
||||
}
|
||||
return EndToken;
|
||||
case 1: // Recognized '{Upper128}', Shortest string "\xC0"
|
||||
case 2: // Recognized '{Upper128}', Shortest string "\x80"
|
||||
case 3: // Recognized '{Upper128}', Shortest string "\xE0"
|
||||
case 4: // Recognized '{Upper128}', Shortest string "\xF0"
|
||||
uppr++;
|
||||
break;
|
||||
case 5: // Recognized '{Utf8pfx4}{Utf8cont}', Shortest string "\xF0\x80"
|
||||
uppr += 2;
|
||||
break;
|
||||
case 6: // Recognized '{Utf8pfx4}{Utf8cont}{2}', Shortest string "\xF0\x80\x80"
|
||||
uppr += 3;
|
||||
break;
|
||||
case 7: // Recognized '{Utf8pfx4}{Utf8cont}{3}', Shortest string "\xF0\x80\x80\x80"
|
||||
utfX += 3;
|
||||
break;
|
||||
case 8: // Recognized '{Utf8pfx3}{Utf8cont}', Shortest string "\xE0\x80"
|
||||
uppr += 2;
|
||||
break;
|
||||
case 9: // Recognized '{Utf8pfx3}{Utf8cont}{2}', Shortest string "\xE0\x80\x80"
|
||||
utfX += 2;
|
||||
break;
|
||||
case 10: // Recognized '{Utf8pfx2}{Utf8cont}', Shortest string "\xC0\x80"
|
||||
utfX++;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
#pragma warning restore 162
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
}
|
||||
} // end class Guesser
|
||||
|
||||
#endif // !BYTEMODE
|
||||
#endregion
|
||||
#endif // !NOFILES
|
||||
|
||||
// End of code copied from embedded resource
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
namespace Bts.BooleanExpressions;
|
||||
|
||||
interface IBinaryOperator {
|
||||
bool Evaluate(IArgument? left, IArgument? right, IDictionary<string, object?> env);
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
namespace Bts.BooleanExpressions;
|
||||
|
||||
public interface IBooleanExpression {
|
||||
bool Evaluate(IDictionary<string, object?> env);
|
||||
}
|
||||
@@ -0,0 +1,884 @@
|
||||
// ==============================================================
|
||||
// <auto-generated>
|
||||
// This code automatically produced from an embedded resource.
|
||||
// Do not edit this file, or it will become incompatible with
|
||||
// the specification from which it was generated.
|
||||
// </auto-generated>
|
||||
// ==============================================================
|
||||
|
||||
// Gardens Point Parser Generator
|
||||
// Copyright (c) Wayne Kelly, QUT 2005-2010
|
||||
// (see accompanying GPPGcopyright.rtf)
|
||||
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.Globalization;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace QUT.Gppg {
|
||||
/// <summary>
|
||||
/// Abstract class for GPPG shift-reduce parsers.
|
||||
/// Parsers generated by GPPG derive from this base
|
||||
/// class, overriding the abstract Initialize() and
|
||||
/// DoAction() methods.
|
||||
/// </summary>
|
||||
/// <typeparam name="TValue">Semantic value type</typeparam>
|
||||
/// <typeparam name="TSpan">Location type</typeparam>
|
||||
#if EXPORT_GPPG
|
||||
public abstract class ShiftReduceParser<TValue, TSpan>
|
||||
#else
|
||||
internal abstract class ShiftReduceParser<TValue, TSpan>
|
||||
#endif
|
||||
where TSpan : IMerge<TSpan>, new() {
|
||||
private AbstractScanner<TValue, TSpan> scanner;
|
||||
/// <summary>
|
||||
/// The abstract scanner for this parser.
|
||||
/// </summary>
|
||||
protected AbstractScanner<TValue, TSpan> Scanner {
|
||||
get { return scanner; }
|
||||
set { scanner = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Constructor for base class
|
||||
/// </summary>
|
||||
/// <param name="scanner">Scanner instance for this parser</param>
|
||||
protected ShiftReduceParser( AbstractScanner<TValue, TSpan> scanner ) {
|
||||
this.scanner = scanner;
|
||||
}
|
||||
|
||||
// ==============================================================
|
||||
// TECHNICAL EXPLANATION.
|
||||
// Why the next two fields are not exposed via properties.
|
||||
// ==============================================================
|
||||
// These fields are of the generic parameter types, and are
|
||||
// frequently instantiated as struct types in derived classes.
|
||||
// Semantic actions are defined in the derived classes and refer
|
||||
// to instance fields of these structs. In such cases the code
|
||||
// "get_CurrentSemanticValue().myField = blah;" will fail since
|
||||
// the getter pushes the value of the field, not the reference.
|
||||
// So, in the presence of properties, gppg would need to encode
|
||||
// such field accesses as ...
|
||||
// "tmp = get_CurrentSemanticValue(); // Fetch value
|
||||
// tmp.myField = blah; // update
|
||||
// set_CurrentSemanticValue(tmp); " // Write update back.
|
||||
// There is no issue if TValue is restricted to be a ref type.
|
||||
// The same explanation applies to scanner.yylval.
|
||||
// ==============================================================
|
||||
/// <summary>
|
||||
/// The current value of the "$$" symbolic variable in the parser
|
||||
/// </summary>
|
||||
[SuppressMessage( "Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields" )]
|
||||
protected TValue CurrentSemanticValue;
|
||||
|
||||
/// <summary>
|
||||
/// The current value of the "@$" symbolic variable in the parser
|
||||
/// </summary>
|
||||
[SuppressMessage( "Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields" )]
|
||||
protected TSpan CurrentLocationSpan;
|
||||
protected int NextToken;
|
||||
|
||||
private TSpan LastSpan;
|
||||
private State FsaState;
|
||||
private bool recovering;
|
||||
private int tokensSinceLastError;
|
||||
|
||||
private PushdownPrefixState<State> StateStack = new PushdownPrefixState<State>();
|
||||
private PushdownPrefixState<TValue> valueStack = new PushdownPrefixState<TValue>();
|
||||
private PushdownPrefixState<TSpan> locationStack = new PushdownPrefixState<TSpan>();
|
||||
|
||||
/// <summary>
|
||||
/// The stack of semantic value (YYSTYPE) values.
|
||||
/// </summary>
|
||||
protected PushdownPrefixState<TValue> ValueStack { get { return valueStack; } }
|
||||
|
||||
/// <summary>
|
||||
/// The stack of location value (YYLTYPE) varlues.
|
||||
/// </summary>
|
||||
protected PushdownPrefixState<TSpan> LocationStack { get { return locationStack; } }
|
||||
|
||||
private int errorToken;
|
||||
private int endOfFileToken;
|
||||
private string[] nonTerminals;
|
||||
private State[] states;
|
||||
private Rule[] rules;
|
||||
|
||||
/// <summary>
|
||||
/// Initialization method to allow derived classes
|
||||
/// to insert the rule list into this base class.
|
||||
/// </summary>
|
||||
/// <param name="rules">The array of Rule objects</param>
|
||||
protected void InitRules( Rule[] rules ) { this.rules = rules; }
|
||||
|
||||
/// <summary>
|
||||
/// Initialization method to allow derived classes to
|
||||
/// insert the states table into this base class.
|
||||
/// </summary>
|
||||
/// <param name="states">The pre-initialized states table</param>
|
||||
protected void InitStates( State[] states ) { this.states = states; }
|
||||
|
||||
/// <summary>
|
||||
/// OBSOLETE FOR VERSION 1.4.0
|
||||
/// </summary>
|
||||
/// <param name="size"></param>
|
||||
protected void InitStateTable( int size ) { states = new State[size]; }
|
||||
|
||||
/// <summary>
|
||||
/// Initialization method to allow derived classes
|
||||
/// to insert the special value for the error and EOF tokens.
|
||||
/// </summary>
|
||||
/// <param name="err">The error state ordinal</param>
|
||||
/// <param name="end">The EOF stat ordinal</param>
|
||||
protected void InitSpecialTokens( int err, int end ) {
|
||||
errorToken = err;
|
||||
endOfFileToken = end;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initialization method to allow derived classes to
|
||||
/// insert the non-terminal symbol names into this base class.
|
||||
/// </summary>
|
||||
/// <param name="names">Non-terminal symbol names</param>
|
||||
protected void InitNonTerminals( string[] names ) { nonTerminals = names; }
|
||||
|
||||
#region YYAbort, YYAccept etcetera.
|
||||
[Serializable]
|
||||
[SuppressMessage( "Microsoft.Design", "CA1064:ExceptionsShouldBePublic" )]
|
||||
// Reason for FxCop message suppression -
|
||||
// This exception cannot escape from the local context
|
||||
private class AcceptException : Exception {
|
||||
internal AcceptException() { }
|
||||
protected AcceptException( SerializationInfo i, StreamingContext c ) : base( i, c ) { }
|
||||
}
|
||||
[Serializable]
|
||||
[SuppressMessage( "Microsoft.Design", "CA1064:ExceptionsShouldBePublic" )]
|
||||
// Reason for FxCop message suppression -
|
||||
// This exception cannot escape from the local context
|
||||
private class AbortException : Exception {
|
||||
internal AbortException() { }
|
||||
protected AbortException( SerializationInfo i, StreamingContext c ) : base( i, c ) { }
|
||||
}
|
||||
[Serializable]
|
||||
[SuppressMessage( "Microsoft.Design", "CA1064:ExceptionsShouldBePublic" )]
|
||||
// Reason for FxCop message suppression -
|
||||
// This exception cannot escape from the local context
|
||||
private class ErrorException : Exception {
|
||||
internal ErrorException() { }
|
||||
protected ErrorException( SerializationInfo i, StreamingContext c ) : base( i, c ) { }
|
||||
}
|
||||
|
||||
// The following methods are only called from within
|
||||
// a semantic action. The thrown exceptions can never
|
||||
// propagate outside the ShiftReduceParser class in
|
||||
// which they are nested.
|
||||
|
||||
/// <summary>
|
||||
/// Force parser to terminate, returning "true"
|
||||
/// </summary>
|
||||
protected static void YYAccept() { throw new AcceptException(); }
|
||||
|
||||
/// <summary>
|
||||
/// Force parser to terminate, returning "false"
|
||||
/// </summary>
|
||||
protected static void YYAbort() { throw new AbortException(); }
|
||||
|
||||
/// <summary>
|
||||
/// Force parser to terminate, returning
|
||||
/// "false" if error recovery fails.
|
||||
/// </summary>
|
||||
protected static void YYError() { throw new ErrorException(); }
|
||||
|
||||
/// <summary>
|
||||
/// Check if parser in error recovery state.
|
||||
/// </summary>
|
||||
protected bool YYRecovering { get { return recovering; } }
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Abstract base method. ShiftReduceParser calls this
|
||||
/// to initialize the base class data structures. Concrete
|
||||
/// parser classes must override this method.
|
||||
/// </summary>
|
||||
protected abstract void Initialize();
|
||||
|
||||
/// <summary>
|
||||
/// Main entry point of the Shift-Reduce Parser.
|
||||
/// </summary>
|
||||
/// <returns>True if parse succeeds, else false for
|
||||
/// unrecoverable errors</returns>
|
||||
public bool Parse() {
|
||||
Initialize(); // allow derived classes to instantiate rules, states and nonTerminals
|
||||
|
||||
NextToken = 0;
|
||||
FsaState = states[0];
|
||||
|
||||
StateStack.Push( FsaState );
|
||||
valueStack.Push( CurrentSemanticValue );
|
||||
LocationStack.Push( CurrentLocationSpan );
|
||||
|
||||
while (true) {
|
||||
#if TRACE_ACTIONS
|
||||
Console.Error.WriteLine("Entering state {0} ", FsaState.number);
|
||||
DisplayStack();
|
||||
#endif
|
||||
int action = FsaState.defaultAction;
|
||||
|
||||
if (FsaState.ParserTable != null) {
|
||||
if (NextToken == 0) {
|
||||
// We save the last token span, so that the location span
|
||||
// of production right hand sides that begin or end with a
|
||||
// nullable production will be correct.
|
||||
LastSpan = scanner.yylloc;
|
||||
NextToken = scanner.yylex();
|
||||
#if TRACE_ACTIONS
|
||||
Console.Error.WriteLine( "Reading: Next token is {0}", TerminalToString( NextToken ) );
|
||||
#endif
|
||||
}
|
||||
#if TRACE_ACTIONS
|
||||
else
|
||||
Console.Error.WriteLine( "Next token is still {0}", TerminalToString( NextToken ) );
|
||||
#endif
|
||||
if (FsaState.ParserTable.ContainsKey( NextToken ))
|
||||
action = FsaState.ParserTable[NextToken];
|
||||
}
|
||||
|
||||
if (action > 0) // shift
|
||||
{
|
||||
Shift( action );
|
||||
}
|
||||
else if (action < 0) // reduce
|
||||
{
|
||||
try {
|
||||
Reduce( -action );
|
||||
if (action == -1) // accept
|
||||
return true;
|
||||
}
|
||||
catch (AbortException) {
|
||||
return false;
|
||||
}
|
||||
catch (AcceptException) {
|
||||
return true;
|
||||
}
|
||||
catch (ErrorException) {
|
||||
if (!ErrorRecovery())
|
||||
return false;
|
||||
else
|
||||
throw; // Rethrow x, preserving information.
|
||||
}
|
||||
}
|
||||
else if (action == 0 && !ErrorRecovery()) // error
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private void Shift( int stateIndex ) {
|
||||
#if TRACE_ACTIONS
|
||||
Console.Error.Write("Shifting token {0}, ", TerminalToString(NextToken));
|
||||
#endif
|
||||
FsaState = states[stateIndex];
|
||||
|
||||
valueStack.Push( scanner.yylval );
|
||||
StateStack.Push( FsaState );
|
||||
LocationStack.Push( scanner.yylloc );
|
||||
|
||||
if (recovering) {
|
||||
if (NextToken != errorToken)
|
||||
tokensSinceLastError++;
|
||||
|
||||
if (tokensSinceLastError > 5)
|
||||
recovering = false;
|
||||
}
|
||||
|
||||
if (NextToken != endOfFileToken)
|
||||
NextToken = 0;
|
||||
}
|
||||
|
||||
private void Reduce( int ruleNumber ) {
|
||||
#if TRACE_ACTIONS
|
||||
DisplayRule(ruleNumber);
|
||||
#endif
|
||||
Rule rule = rules[ruleNumber];
|
||||
int rhLen = rule.RightHandSide.Length;
|
||||
//
|
||||
// Default actions for unit productions.
|
||||
//
|
||||
if (rhLen == 1) {
|
||||
CurrentSemanticValue = valueStack.TopElement(); // Default action: $$ = $1;
|
||||
CurrentLocationSpan = LocationStack.TopElement(); // Default action "@$ = @1;
|
||||
}
|
||||
else if (rhLen == 0) {
|
||||
// Create a new blank value.
|
||||
// Explicit semantic action may mutate this value
|
||||
CurrentSemanticValue = default( TValue );
|
||||
// The location span for an empty production will start with the
|
||||
// beginning of the next lexeme, and end with the finish of the
|
||||
// previous lexeme. This gives the correct behaviour when this
|
||||
// nonsense value is used in later Merge operations.
|
||||
CurrentLocationSpan = (scanner.yylloc != null && LastSpan != null ?
|
||||
scanner.yylloc.Merge( LastSpan ) :
|
||||
default( TSpan ));
|
||||
}
|
||||
else {
|
||||
// Default action: $$ = $1;
|
||||
CurrentSemanticValue = valueStack[LocationStack.Depth - rhLen];
|
||||
// Default action "@$ = @1.Merge(@N)" for location info.
|
||||
TSpan at1 = LocationStack[LocationStack.Depth - rhLen];
|
||||
TSpan atN = LocationStack[LocationStack.Depth - 1];
|
||||
CurrentLocationSpan =
|
||||
((at1 != null && atN != null) ? at1.Merge( atN ) : default( TSpan ));
|
||||
}
|
||||
|
||||
DoAction( ruleNumber );
|
||||
|
||||
for (int i = 0; i < rule.RightHandSide.Length; i++) {
|
||||
StateStack.Pop();
|
||||
valueStack.Pop();
|
||||
LocationStack.Pop();
|
||||
}
|
||||
FsaState = StateStack.TopElement();
|
||||
|
||||
if (FsaState.Goto.ContainsKey( rule.LeftHandSide ))
|
||||
FsaState = states[FsaState.Goto[rule.LeftHandSide]];
|
||||
|
||||
StateStack.Push( FsaState );
|
||||
valueStack.Push( CurrentSemanticValue );
|
||||
LocationStack.Push( CurrentLocationSpan );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Execute the selected action from array.
|
||||
/// Must be overriden in derived classes.
|
||||
/// </summary>
|
||||
/// <param name="actionNumber">Index of the action to perform</param>
|
||||
protected abstract void DoAction( int actionNumber );
|
||||
|
||||
private bool ErrorRecovery() {
|
||||
bool discard;
|
||||
|
||||
if (!recovering) // if not recovering from previous error
|
||||
ReportError();
|
||||
|
||||
if (!FindErrorRecoveryState())
|
||||
return false;
|
||||
//
|
||||
// The interim fix for the "looping in error recovery"
|
||||
// artifact involved moving the setting of the recovering
|
||||
// bool until after invalid tokens have been discarded.
|
||||
//
|
||||
ShiftErrorToken();
|
||||
discard = DiscardInvalidTokens();
|
||||
recovering = true;
|
||||
tokensSinceLastError = 0;
|
||||
return discard;
|
||||
}
|
||||
|
||||
private void ReportError() {
|
||||
StringBuilder errorMsg = new StringBuilder();
|
||||
errorMsg.AppendFormat( "Syntax error, unexpected {0}", TerminalToString( NextToken ) );
|
||||
|
||||
if (FsaState.ParserTable.Count < 7) {
|
||||
bool first = true;
|
||||
foreach (int terminal in FsaState.ParserTable.Keys) {
|
||||
if (first)
|
||||
errorMsg.Append( ", expecting " );
|
||||
else
|
||||
errorMsg.Append( ", or " );
|
||||
|
||||
errorMsg.Append( TerminalToString( terminal ) );
|
||||
first = false;
|
||||
}
|
||||
}
|
||||
scanner.yyerror( errorMsg.ToString() );
|
||||
}
|
||||
|
||||
private void ShiftErrorToken() {
|
||||
int old_next = NextToken;
|
||||
NextToken = errorToken;
|
||||
|
||||
Shift( FsaState.ParserTable[NextToken] );
|
||||
|
||||
#if TRACE_ACTIONS
|
||||
Console.Error.WriteLine("Entering state {0} ", FsaState.number);
|
||||
#endif
|
||||
NextToken = old_next;
|
||||
}
|
||||
|
||||
private bool FindErrorRecoveryState() {
|
||||
while (true) // pop states until one found that accepts error token
|
||||
{
|
||||
if (FsaState.ParserTable != null &&
|
||||
FsaState.ParserTable.ContainsKey( errorToken ) &&
|
||||
FsaState.ParserTable[errorToken] > 0) // shift
|
||||
return true;
|
||||
|
||||
#if TRACE_ACTIONS
|
||||
Console.Error.WriteLine("Error: popping state {0}", StateStack.TopElement().number);
|
||||
#endif
|
||||
StateStack.Pop();
|
||||
valueStack.Pop();
|
||||
LocationStack.Pop();
|
||||
|
||||
#if TRACE_ACTIONS
|
||||
DisplayStack();
|
||||
#endif
|
||||
if (StateStack.IsEmpty()) {
|
||||
#if TRACE_ACTIONS
|
||||
Console.Error.WriteLine("Aborting: didn't find a state that accepts error token");
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
else
|
||||
FsaState = StateStack.TopElement();
|
||||
}
|
||||
}
|
||||
|
||||
private bool DiscardInvalidTokens() {
|
||||
|
||||
int action = FsaState.defaultAction;
|
||||
|
||||
if (FsaState.ParserTable != null) {
|
||||
// Discard tokens until find one that works ...
|
||||
while (true) {
|
||||
if (NextToken == 0) {
|
||||
#if TRACE_ACTIONS
|
||||
Console.Error.Write("Reading a token: ");
|
||||
#endif
|
||||
NextToken = scanner.yylex();
|
||||
}
|
||||
#if TRACE_ACTIONS
|
||||
Console.Error.WriteLine("Next token is {0}", TerminalToString(NextToken));
|
||||
#endif
|
||||
if (NextToken == endOfFileToken)
|
||||
return false;
|
||||
|
||||
if (FsaState.ParserTable.ContainsKey( NextToken ))
|
||||
action = FsaState.ParserTable[NextToken];
|
||||
|
||||
if (action != 0)
|
||||
return true;
|
||||
else {
|
||||
#if TRACE_ACTIONS
|
||||
Console.Error.WriteLine("Error: Discarding {0}", TerminalToString(NextToken));
|
||||
#endif
|
||||
NextToken = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (recovering && tokensSinceLastError == 0) {
|
||||
//
|
||||
// Boolean recovering is not set until after the first
|
||||
// error token has been shifted. Thus if we get back
|
||||
// here with recovering set and no tokens read we are
|
||||
// looping on the same error recovery action. This
|
||||
// happens if current_state.ParserTable is null because
|
||||
// the state has an LR(0) reduction, but not all
|
||||
// lookahead tokens are valid. This only occurs for
|
||||
// error productions that *end* on "error".
|
||||
//
|
||||
// This action discards tokens one at a time until
|
||||
// the looping stops. Another attack would be to always
|
||||
// use the LALR(1) table if a production ends on "error"
|
||||
//
|
||||
#if TRACE_ACTIONS
|
||||
Console.Error.WriteLine("Error: panic discard of {0}", TerminalToString(NextToken));
|
||||
#endif
|
||||
if (NextToken == endOfFileToken)
|
||||
return false;
|
||||
NextToken = 0;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Traditional YACC method. Discards the next input token.
|
||||
/// </summary>
|
||||
[SuppressMessage( "Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "yyclearin" )]
|
||||
[SuppressMessage( "Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "yyclearin" )]
|
||||
// Reason for FxCop message suppression -
|
||||
// This is a traditional name for YACC-like functionality
|
||||
protected void yyclearin() { NextToken = 0; }
|
||||
|
||||
/// <summary>
|
||||
/// Tradional YACC method. Clear the "recovering" flag.
|
||||
/// </summary>
|
||||
[SuppressMessage( "Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "yyerrok" )]
|
||||
[SuppressMessage( "Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "yyerrok" )]
|
||||
// Reason for FxCop message suppression -
|
||||
// This is a traditional name for YACC-like functionality
|
||||
protected void yyerrok() { recovering = false; }
|
||||
|
||||
/// <summary>
|
||||
/// OBSOLETE FOR VERSION 1.4.0
|
||||
/// Method used by derived types to insert new
|
||||
/// state instances in the "states" array.
|
||||
/// </summary>
|
||||
/// <param name="stateNumber">index of the state</param>
|
||||
/// <param name="state">data for the state</param>
|
||||
protected void AddState( int stateNumber, State state ) {
|
||||
states[stateNumber] = state;
|
||||
state.number = stateNumber;
|
||||
}
|
||||
|
||||
private void DisplayStack() {
|
||||
Console.Error.Write( "State stack is now:" );
|
||||
for (int i = 0; i < StateStack.Depth; i++)
|
||||
Console.Error.Write( " {0}", StateStack[i].number );
|
||||
Console.Error.WriteLine();
|
||||
}
|
||||
|
||||
private void DisplayRule( int ruleNumber ) {
|
||||
Console.Error.Write( "Reducing stack by rule {0}, ", ruleNumber );
|
||||
DisplayProduction( rules[ruleNumber] );
|
||||
}
|
||||
|
||||
private void DisplayProduction( Rule rule ) {
|
||||
if (rule.RightHandSide.Length == 0)
|
||||
Console.Error.Write( "/* empty */ " );
|
||||
else
|
||||
foreach (int symbol in rule.RightHandSide)
|
||||
Console.Error.Write( "{0} ", SymbolToString( symbol ) );
|
||||
|
||||
Console.Error.WriteLine( "-> {0}", SymbolToString( rule.LeftHandSide ) );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Abstract state class naming terminal symbols.
|
||||
/// This is overridden by derived classes with the
|
||||
/// name (or alias) to be used in error messages.
|
||||
/// </summary>
|
||||
/// <param name="terminal">The terminal ordinal</param>
|
||||
/// <returns></returns>
|
||||
protected abstract string TerminalToString( int terminal );
|
||||
|
||||
private string SymbolToString( int symbol ) {
|
||||
if (symbol < 0)
|
||||
return nonTerminals[-symbol - 1];
|
||||
else
|
||||
return TerminalToString( symbol );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Return text representation of argument character
|
||||
/// </summary>
|
||||
/// <param name="input">The character to convert</param>
|
||||
/// <returns>String representation of the character</returns>
|
||||
protected static string CharToString( char input ) {
|
||||
switch (input) {
|
||||
case '\a': return @"'\a'";
|
||||
case '\b': return @"'\b'";
|
||||
case '\f': return @"'\f'";
|
||||
case '\n': return @"'\n'";
|
||||
case '\r': return @"'\r'";
|
||||
case '\t': return @"'\t'";
|
||||
case '\v': return @"'\v'";
|
||||
case '\0': return @"'\0'";
|
||||
default: return string.Format( CultureInfo.InvariantCulture, "'{0}'", input );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Classes implementing this interface must supply a
|
||||
/// method that merges two location objects to return
|
||||
/// a new object of the same type.
|
||||
/// GPPG-generated parsers have the default location
|
||||
/// action equivalent to "@$ = @1.Merge(@N);" where N
|
||||
/// is the right-hand-side length of the production.
|
||||
/// </summary>
|
||||
/// <typeparam name="TSpan">The Location type</typeparam>
|
||||
#if EXPORT_GPPG
|
||||
public interface IMerge<TSpan>
|
||||
#else
|
||||
internal interface IMerge<TSpan>
|
||||
#endif
|
||||
{
|
||||
/// <summary>
|
||||
/// Interface method that creates a location object from
|
||||
/// the current and last object. Typically used to create
|
||||
/// a location object extending from the start of the @1
|
||||
/// object to the end of the @N object.
|
||||
/// </summary>
|
||||
/// <param name="last">The lexically last object to merge</param>
|
||||
/// <returns>The merged location object</returns>
|
||||
TSpan Merge( TSpan last );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This is the default class that carries location
|
||||
/// information from the scanner to the parser.
|
||||
/// If you don't declare "%YYLTYPE Foo" the parser
|
||||
/// will expect to deal with this type.
|
||||
/// </summary>
|
||||
#if EXPORT_GPPG
|
||||
public class LexLocation : IMerge<LexLocation>
|
||||
#else
|
||||
[SuppressMessage( "Microsoft.Performance", "CA1812:AvoidUninstantiatedInternalClasses" )]
|
||||
internal class LexLocation : IMerge<LexLocation>
|
||||
#endif
|
||||
{
|
||||
private int startLine; // start line
|
||||
private int startColumn; // start column
|
||||
private int endLine; // end line
|
||||
private int endColumn; // end column
|
||||
|
||||
/// <summary>
|
||||
/// The line at which the text span starts.
|
||||
/// </summary>
|
||||
public int StartLine { get { return startLine; } }
|
||||
|
||||
/// <summary>
|
||||
/// The column at which the text span starts.
|
||||
/// </summary>
|
||||
public int StartColumn { get { return startColumn; } }
|
||||
|
||||
/// <summary>
|
||||
/// The line on which the text span ends.
|
||||
/// </summary>
|
||||
public int EndLine { get { return endLine; } }
|
||||
|
||||
/// <summary>
|
||||
/// The column of the first character
|
||||
/// beyond the end of the text span.
|
||||
/// </summary>
|
||||
public int EndColumn { get { return endColumn; } }
|
||||
|
||||
/// <summary>
|
||||
/// Default no-arg constructor.
|
||||
/// </summary>
|
||||
public LexLocation() { }
|
||||
|
||||
/// <summary>
|
||||
/// Constructor for text-span with given start and end.
|
||||
/// </summary>
|
||||
/// <param name="sl">start line</param>
|
||||
/// <param name="sc">start column</param>
|
||||
/// <param name="el">end line </param>
|
||||
/// <param name="ec">end column</param>
|
||||
public LexLocation( int sl, int sc, int el, int ec ) { startLine = sl; startColumn = sc; endLine = el; endColumn = ec; }
|
||||
|
||||
/// <summary>
|
||||
/// Create a text location which spans from the
|
||||
/// start of "this" to the end of the argument "last"
|
||||
/// </summary>
|
||||
/// <param name="last">The last location in the result span</param>
|
||||
/// <returns>The merged span</returns>
|
||||
public LexLocation Merge( LexLocation last ) { return new LexLocation( this.startLine, this.startColumn, last.endLine, last.endColumn ); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Abstract scanner class that GPPG expects its scanners to
|
||||
/// extend.
|
||||
/// </summary>
|
||||
/// <typeparam name="TValue">Semantic value type YYSTYPE</typeparam>
|
||||
/// <typeparam name="TSpan">Source location type YYLTYPE</typeparam>
|
||||
#if EXPORT_GPPG
|
||||
public abstract class AbstractScanner<TValue, TSpan>
|
||||
#else
|
||||
internal abstract class AbstractScanner<TValue, TSpan>
|
||||
#endif
|
||||
where TSpan : IMerge<TSpan> {
|
||||
/// <summary>
|
||||
/// Lexical value optionally set by the scanner. The value
|
||||
/// is of the %YYSTYPE type declared in the parser spec.
|
||||
/// </summary>
|
||||
[SuppressMessage( "Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields" )]
|
||||
[SuppressMessage( "Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "yylval" )]
|
||||
[SuppressMessage( "Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "yylval" )]
|
||||
// Reason for FxCop message suppression -
|
||||
// This is a traditional name for YACC-like functionality
|
||||
// A field must be declared for this value of parametric type,
|
||||
// since it may be instantiated by a value struct. If it were
|
||||
// implemented as a property, machine generated code in derived
|
||||
// types would not be able to select on the returned value.
|
||||
#pragma warning disable 649
|
||||
public TValue yylval; // Lexical value: set by scanner
|
||||
#pragma warning restore 649
|
||||
|
||||
/// <summary>
|
||||
/// Current scanner location property. The value is of the
|
||||
/// type declared by %YYLTYPE in the parser specification.
|
||||
/// </summary>
|
||||
[SuppressMessage( "Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "yylloc" )]
|
||||
[SuppressMessage( "Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "yylloc" )]
|
||||
// Reason for FxCop message suppression -
|
||||
// This is a traditional name for YACC-like functionality
|
||||
public virtual TSpan yylloc {
|
||||
get { return default( TSpan ); } // Empty implementation allowing
|
||||
set { /* skip */ } // yylloc to be ignored entirely.
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Main call point for LEX-like scanners. Returns an int
|
||||
/// corresponding to the token recognized by the scanner.
|
||||
/// </summary>
|
||||
/// <returns>An int corresponding to the token</returns>
|
||||
[SuppressMessage( "Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "yylex" )]
|
||||
[SuppressMessage( "Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "yylex" )]
|
||||
// Reason for FxCop message suppression -
|
||||
// This is a traditional name for YACC-like functionality
|
||||
public abstract int yylex();
|
||||
|
||||
/// <summary>
|
||||
/// Traditional error reporting provided by LEX-like scanners
|
||||
/// to their YACC-like clients.
|
||||
/// </summary>
|
||||
/// <param name="format">Message format string</param>
|
||||
/// <param name="args">Optional array of args</param>
|
||||
[SuppressMessage( "Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "yyerror" )]
|
||||
[SuppressMessage( "Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "yyerror" )]
|
||||
// Reason for FxCop message suppression -
|
||||
// This is a traditional name for YACC-like functionality
|
||||
public virtual void yyerror( string format, params object[] args ) { }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Encapsulated state for the parser.
|
||||
/// Opaque to users, visible to the tool-generated code.
|
||||
/// </summary>
|
||||
#if EXPORT_GPPG
|
||||
public class State
|
||||
{
|
||||
/// <summary>
|
||||
/// The number of states in the automaton.
|
||||
/// </summary>
|
||||
public int number;
|
||||
#else
|
||||
internal class State {
|
||||
/// <summary>
|
||||
/// The index of this state in the states array.
|
||||
/// </summary>
|
||||
internal int number;
|
||||
#endif
|
||||
internal Dictionary<int, int> ParserTable; // Terminal -> ParseAction
|
||||
internal Dictionary<int, int> Goto; // NonTerminal -> State;
|
||||
internal int defaultAction; // = 0; // ParseAction
|
||||
|
||||
/// <summary>
|
||||
/// State transition data for this state. Pairs of elements of the
|
||||
/// goto array associate symbol ordinals with next state indices.
|
||||
/// The actions array is passed to another constructor.
|
||||
/// </summary>
|
||||
/// <param name="actions">The action list</param>c
|
||||
/// <param name="goToList">Next state data</param>
|
||||
public State( int[] actions, int[] goToList )
|
||||
: this( actions ) {
|
||||
Goto = new Dictionary<int, int>();
|
||||
for (int i = 0; i < goToList.Length; i += 2)
|
||||
Goto.Add( goToList[i], goToList[i + 1] );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Action data for this state. Pairs of elements of the
|
||||
/// action array associate action ordinals with each of
|
||||
/// those symbols that have actions in the current state.
|
||||
/// </summary>
|
||||
/// <param name="actions">The action array</param>
|
||||
public State( int[] actions ) {
|
||||
ParserTable = new Dictionary<int, int>();
|
||||
for (int i = 0; i < actions.Length; i += 2)
|
||||
ParserTable.Add( actions[i], actions[i + 1] );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set the default action for this state.
|
||||
/// </summary>
|
||||
/// <param name="defaultAction">Ordinal of the default action</param>
|
||||
public State( int defaultAction ) {
|
||||
this.defaultAction = defaultAction;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set the default action and the state transition table.
|
||||
/// </summary>
|
||||
/// <param name="defaultAction">The default action</param>
|
||||
/// <param name="goToList">Transitions from this state</param>
|
||||
public State( int defaultAction, int[] goToList )
|
||||
: this( defaultAction ) {
|
||||
Goto = new Dictionary<int, int>();
|
||||
for (int i = 0; i < goToList.Length; i += 2)
|
||||
Goto.Add( goToList[i], goToList[i + 1] );
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Rule representation at runtime.
|
||||
/// </summary>
|
||||
#if EXPORT_GPPG
|
||||
public class Rule
|
||||
#else
|
||||
internal class Rule
|
||||
#endif
|
||||
{
|
||||
internal int LeftHandSide; // symbol
|
||||
internal int[] RightHandSide; // symbols
|
||||
|
||||
/// <summary>
|
||||
/// Rule constructor. This holds the ordinal of
|
||||
/// the left hand side symbol, and the list of
|
||||
/// right hand side symbols, in lexical order.
|
||||
/// </summary>
|
||||
/// <param name="left">The LHS non-terminal</param>
|
||||
/// <param name="right">The RHS symbols, in lexical order</param>
|
||||
public Rule( int left, int[] right ) {
|
||||
this.LeftHandSide = left;
|
||||
this.RightHandSide = right;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stack utility for the shift-reduce parser.
|
||||
/// GPPG parsers have three instances:
|
||||
/// (1) The parser state stack, T = QUT.Gppg.State,
|
||||
/// (2) The semantic value stack, T = TValue,
|
||||
/// (3) The location stack, T = TSpan.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
#if EXPORT_GPPG
|
||||
public class PushdownPrefixState<T>
|
||||
#else
|
||||
internal class PushdownPrefixState<T>
|
||||
#endif
|
||||
{
|
||||
// Note that we cannot use the BCL Stack<T> class
|
||||
// here as derived types need to index into stacks.
|
||||
//
|
||||
private T[] array = new T[8];
|
||||
private int tos = 0;
|
||||
|
||||
/// <summary>
|
||||
/// Indexer for values of the stack below the top.
|
||||
/// </summary>
|
||||
/// <param name="index">index of the element, starting from the bottom</param>
|
||||
/// <returns>the selected element</returns>
|
||||
public T this[int index] { get { return array[index]; } }
|
||||
|
||||
/// <summary>
|
||||
/// The current depth of the stack.
|
||||
/// </summary>
|
||||
public int Depth { get { return tos; } }
|
||||
|
||||
internal void Push( T value ) {
|
||||
if (tos >= array.Length) {
|
||||
T[] newarray = new T[array.Length * 2];
|
||||
System.Array.Copy( array, newarray, tos );
|
||||
array = newarray;
|
||||
}
|
||||
array[tos++] = value;
|
||||
}
|
||||
|
||||
internal T Pop() {
|
||||
T rslt = array[--tos];
|
||||
array[tos] = default( T );
|
||||
return rslt;
|
||||
}
|
||||
|
||||
internal T TopElement() { return array[tos - 1]; }
|
||||
|
||||
internal bool IsEmpty() { return tos == 0; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
global using System;
|
||||
global using System.Linq;
|
||||
global using System.Collections.Generic;
|
||||
global using System.Collections.Immutable;
|
||||
@@ -1,289 +0,0 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace BCG
|
||||
{
|
||||
public delegate bool BooleanEval(object arg1,
|
||||
object arg2,
|
||||
IDictionary bindings);
|
||||
|
||||
public class BooleanExpressionException : Exception
|
||||
{
|
||||
public BooleanExpressionException(string format, params object[] args)
|
||||
: base(String.Format(format, args))
|
||||
{ }
|
||||
}
|
||||
|
||||
public abstract class BooleanExpressionValue
|
||||
{
|
||||
public abstract object getValue(IDictionary bindings);
|
||||
}
|
||||
|
||||
public class BooleanExpressionConstant : BooleanExpressionValue
|
||||
{
|
||||
public BooleanExpressionConstant(object o)
|
||||
{ myObj = o; }
|
||||
public override object getValue(IDictionary bindings)
|
||||
{ return myObj; }
|
||||
private object myObj;
|
||||
}
|
||||
|
||||
public class BooleanExpressionVariable : BooleanExpressionValue
|
||||
{
|
||||
public BooleanExpressionVariable(string id)
|
||||
{ myId = id; }
|
||||
|
||||
public string getId()
|
||||
{ return myId; }
|
||||
|
||||
public override object getValue(IDictionary bindings)
|
||||
{ return bindings[myId]; }
|
||||
private string myId;
|
||||
}
|
||||
|
||||
public delegate object BEFunction(List<object> args);
|
||||
|
||||
public class BooleanExpressionFunction : BooleanExpressionValue
|
||||
{
|
||||
public BooleanExpressionFunction(string fn, ArrayList args)
|
||||
{
|
||||
myFunction = fn;
|
||||
myArgs = args;
|
||||
}
|
||||
|
||||
public string getId()
|
||||
{ return myFunction; }
|
||||
|
||||
public override object getValue(IDictionary bindings)
|
||||
{
|
||||
List<object> argValues = new List<object>();
|
||||
if (myArgs != null)
|
||||
foreach (object o in myArgs)
|
||||
if (o is BooleanExpressionValue)
|
||||
argValues.Add(((BooleanExpressionValue)o).getValue(bindings));
|
||||
else
|
||||
throw new Exception("Unknown arg value type: " + o.ToString());
|
||||
|
||||
string k = BooleanExpressionEvaluator.functionPrefix + myFunction;
|
||||
if (bindings.Contains(k))
|
||||
return ((BEFunction)(bindings[k]))(argValues);
|
||||
else
|
||||
throw new BooleanExpressionException("function {0} is not defined", myFunction);
|
||||
}
|
||||
|
||||
private string myFunction;
|
||||
private ArrayList myArgs;
|
||||
}
|
||||
|
||||
public class BooleanExpressionEvaluator
|
||||
{
|
||||
public const string functionPrefix = "function:";
|
||||
|
||||
public BooleanExpressionEvaluator (object arg1,
|
||||
BooleanEval f)
|
||||
: this (arg1, null, f)
|
||||
{}
|
||||
|
||||
public BooleanExpressionEvaluator (object arg1,
|
||||
object arg2,
|
||||
BooleanEval f)
|
||||
{
|
||||
myArg1 = arg1;
|
||||
myArg2 = arg2;
|
||||
myF = f;
|
||||
|
||||
}
|
||||
|
||||
static BooleanExpressionEvaluator()
|
||||
{
|
||||
myNumberTypes = new Hashtable();
|
||||
|
||||
myNumberTypes[typeof(byte)] = true;
|
||||
myNumberTypes[typeof(sbyte)] = true;
|
||||
|
||||
myNumberTypes[typeof(short)] = true;
|
||||
myNumberTypes[typeof(ushort)] = true;
|
||||
|
||||
myNumberTypes[typeof(int)] = true;
|
||||
myNumberTypes[typeof(uint)] = true;
|
||||
|
||||
myNumberTypes[typeof(long)] = true;
|
||||
myNumberTypes[typeof(ulong)] = true;
|
||||
|
||||
myNumberTypes[typeof(double)] = true;
|
||||
myNumberTypes[typeof(float)] = true;
|
||||
}
|
||||
|
||||
public bool eval (IDictionary bindings)
|
||||
{
|
||||
return
|
||||
myF (myArg1, myArg2,bindings);
|
||||
}
|
||||
|
||||
|
||||
public static bool and (object arg1, object arg2, IDictionary bindings)
|
||||
{
|
||||
return
|
||||
(arg1 as BooleanExpressionEvaluator).eval (bindings)
|
||||
&&
|
||||
(arg2 as BooleanExpressionEvaluator).eval (bindings);
|
||||
|
||||
}
|
||||
|
||||
public static bool or (object arg1, object arg2, IDictionary bindings)
|
||||
{
|
||||
return
|
||||
(arg1 as BooleanExpressionEvaluator).eval (bindings)
|
||||
||
|
||||
(arg2 as BooleanExpressionEvaluator).eval (bindings);
|
||||
|
||||
}
|
||||
|
||||
public static bool xor (object arg1, object arg2, IDictionary bindings)
|
||||
{
|
||||
return
|
||||
(arg1 as BooleanExpressionEvaluator).eval (bindings)
|
||||
!=
|
||||
(arg2 as BooleanExpressionEvaluator).eval (bindings);
|
||||
|
||||
}
|
||||
|
||||
public static bool not (object arg1, object arg2, IDictionary bindings)
|
||||
{
|
||||
return
|
||||
!(arg1 as BooleanExpressionEvaluator).eval (bindings);
|
||||
}
|
||||
|
||||
public static bool booleanValue(object arg1, object arg2, IDictionary bindings)
|
||||
{
|
||||
return (bool)(arg1 as BooleanExpressionValue).getValue(bindings);
|
||||
}
|
||||
|
||||
public static bool myIsConversionLegal(Type t1, Type t2)
|
||||
{
|
||||
return
|
||||
myNumberTypes.Contains(t1) &&
|
||||
myNumberTypes.Contains(t2);
|
||||
}
|
||||
|
||||
public static bool smaller (object arg1, object arg2, IDictionary bindings)
|
||||
{
|
||||
object lobj = (arg1 as BooleanExpressionValue).getValue(bindings);
|
||||
object robj = (arg2 as BooleanExpressionValue).getValue(bindings);
|
||||
|
||||
try
|
||||
{
|
||||
if (lobj is DBNull && robj is DBNull)
|
||||
return false;
|
||||
|
||||
// anything is bigger than DBNull
|
||||
if (lobj is DBNull)
|
||||
return true;
|
||||
|
||||
if (robj is DBNull)
|
||||
return false;
|
||||
|
||||
if (lobj is string)
|
||||
lobj = ((string)lobj).Trim();
|
||||
|
||||
if (robj is string)
|
||||
robj = ((string)robj).Trim();
|
||||
|
||||
|
||||
|
||||
if (!lobj.GetType().Equals(robj.GetType()))
|
||||
{
|
||||
if (myIsConversionLegal(lobj.GetType(), robj.GetType()))
|
||||
lobj =
|
||||
Convert.ChangeType(lobj,robj.GetType());
|
||||
else if (myIsConversionLegal(robj.GetType(), lobj.GetType()))
|
||||
robj =
|
||||
Convert.ChangeType(robj,lobj.GetType());
|
||||
}
|
||||
|
||||
IComparable ic = lobj as IComparable;
|
||||
return ic.CompareTo(robj) < 0;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
//BlueEventLog.logEvent(String.Format("Comparison between {0} and {1} failed",
|
||||
// lobj == null? "null" : lobj, robj == null? null : robj),BlueEventLevel.Warning);
|
||||
//BlueEventLog.logEvent(e);
|
||||
throw (e);
|
||||
}
|
||||
}
|
||||
|
||||
public static bool equal (object arg1, object arg2, IDictionary bindings)
|
||||
{
|
||||
return
|
||||
!(smaller (arg1, arg2, bindings) )
|
||||
&&
|
||||
!(smaller (arg2, arg1, bindings));
|
||||
}
|
||||
|
||||
public static bool greater (object arg1, object arg2, IDictionary bindings)
|
||||
{
|
||||
return
|
||||
smaller (arg2,arg1, bindings);
|
||||
}
|
||||
|
||||
public static bool smallerEqual (object arg1, object arg2, IDictionary bindings)
|
||||
{
|
||||
return
|
||||
!greater(arg1, arg2, bindings);
|
||||
}
|
||||
|
||||
public static bool greaterEqual (object arg1, object arg2, IDictionary bindings)
|
||||
{
|
||||
return
|
||||
!smaller (arg1, arg2, bindings);
|
||||
}
|
||||
|
||||
public static bool different (object arg1, object arg2, IDictionary bindings)
|
||||
{
|
||||
return
|
||||
!equal (arg1, arg2, bindings);
|
||||
}
|
||||
|
||||
public static bool matches (object arg1, object arg2, IDictionary bindings)
|
||||
{
|
||||
try
|
||||
{
|
||||
object lobj = (arg1 as BooleanExpressionValue).getValue(bindings);
|
||||
object robj = (arg2 as BooleanExpressionValue).getValue(bindings);
|
||||
|
||||
if (DBNull.Value.Equals(lobj) && (robj is string))
|
||||
return false;
|
||||
else if (lobj is string && robj is string)
|
||||
return Regex.IsMatch((string) lobj, (string) robj);
|
||||
else
|
||||
throw new Exception(String.Format("Type mismatch, cannot string match {0} with {1}",
|
||||
lobj.GetType().Name,
|
||||
robj.GetType().Name));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
//BlueEventLog.logEvent(string.Format ("Comparison between {0} and {1} failed",
|
||||
// arg1 == null? "null" : arg1, arg2 == null? null : arg2));
|
||||
//BlueEventLog.logEvent(e);
|
||||
throw (e);
|
||||
}
|
||||
}
|
||||
|
||||
public object getOperand1()
|
||||
{return myArg1;}
|
||||
|
||||
public object getOperand2()
|
||||
{return myArg2;}
|
||||
|
||||
|
||||
private object myArg1;
|
||||
private object myArg2;
|
||||
private BooleanEval myF;
|
||||
|
||||
private static Hashtable myNumberTypes ;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,191 +0,0 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.IO;
|
||||
using NUnit.Framework;
|
||||
using System.Diagnostics;
|
||||
using BCG.Evaluator.BooleanExpression;
|
||||
|
||||
namespace BCG
|
||||
{
|
||||
[TestFixture]
|
||||
public class BooleanExpressionsTests
|
||||
{
|
||||
[Test]
|
||||
public void testBooleanExpressions()
|
||||
{
|
||||
BCG.Evaluator.BooleanExpression.syntax parser =
|
||||
new BCG.Evaluator.BooleanExpression.syntax();
|
||||
|
||||
BooleanProgram exp = (BooleanProgram)parser.Parse("a=12/12/1912 and 2 = null or b=2 and not c=1 and b > a");
|
||||
|
||||
Hashtable h = new Hashtable();
|
||||
h["a"] = new DateTime(1912, 12, 12, 0, 0, 1);
|
||||
h["b"] = 2;
|
||||
h["c"] = 2;
|
||||
h["d"] = new DateTime(1912, 12, 12, 0, 0, 2);
|
||||
|
||||
Assert.That(exp.eval.eval(h));
|
||||
|
||||
parser =
|
||||
new BCG.Evaluator.BooleanExpression.syntax();
|
||||
|
||||
exp = (BooleanProgram)parser.Parse("a=1 and (b=2 or b=3)");
|
||||
|
||||
h = new Hashtable();
|
||||
h["a"] = 1;
|
||||
h["b"] = 5;
|
||||
|
||||
Assert.That(!exp.eval.eval(h));
|
||||
|
||||
// comparisons
|
||||
|
||||
// >
|
||||
exp = (BooleanProgram)parser.Parse("a>b");
|
||||
h = new Hashtable();
|
||||
h["a"] = 1;
|
||||
h["b"] = 5;
|
||||
|
||||
Assert.That(!exp.eval.eval(h));
|
||||
|
||||
h["a"] = 5;
|
||||
h["b"] = 1;
|
||||
|
||||
Assert.That(exp.eval.eval(h));
|
||||
|
||||
h["a"] = 1;
|
||||
h["b"] = 1;
|
||||
|
||||
Assert.That(!exp.eval.eval(h));
|
||||
|
||||
// <
|
||||
exp = (BooleanProgram)parser.Parse("a<b");
|
||||
h = new Hashtable();
|
||||
h["a"] = 1;
|
||||
h["b"] = 5;
|
||||
|
||||
Assert.That(exp.eval.eval(h));
|
||||
|
||||
h["a"] = 5;
|
||||
h["b"] = 1;
|
||||
|
||||
Assert.That(!exp.eval.eval(h));
|
||||
|
||||
h["a"] = 1;
|
||||
h["b"] = 1;
|
||||
|
||||
Assert.That(!exp.eval.eval(h));
|
||||
|
||||
|
||||
// =
|
||||
exp = (BooleanProgram)parser.Parse("a=b");
|
||||
h = new Hashtable();
|
||||
h["a"] = 1;
|
||||
h["b"] = 5;
|
||||
|
||||
Assert.That(!exp.eval.eval(h));
|
||||
|
||||
h["a"] = 1;
|
||||
h["b"] = 1;
|
||||
|
||||
Assert.That(exp.eval.eval(h));
|
||||
|
||||
|
||||
// <=
|
||||
exp = (BooleanProgram)parser.Parse("a<=b");
|
||||
h = new Hashtable();
|
||||
h["a"] = 1;
|
||||
h["b"] = 5;
|
||||
|
||||
Assert.That(exp.eval.eval(h));
|
||||
|
||||
h["a"] = 5;
|
||||
h["b"] = 1;
|
||||
|
||||
Assert.That(!exp.eval.eval(h));
|
||||
|
||||
h["a"] = 1;
|
||||
h["b"] = 1;
|
||||
|
||||
Assert.That(exp.eval.eval(h));
|
||||
|
||||
// >=
|
||||
exp = (BooleanProgram)parser.Parse("a>=b");
|
||||
h = new Hashtable();
|
||||
h["a"] = 1;
|
||||
h["b"] = 5;
|
||||
|
||||
Assert.That(!exp.eval.eval(h));
|
||||
|
||||
h["a"] = 5;
|
||||
h["b"] = 1;
|
||||
|
||||
Assert.That(exp.eval.eval(h));
|
||||
|
||||
h["a"] = 1;
|
||||
h["b"] = 1;
|
||||
|
||||
Assert.That(exp.eval.eval(h));
|
||||
|
||||
// <>
|
||||
exp = (BooleanProgram)parser.Parse("a<>b");
|
||||
h = new Hashtable();
|
||||
h["a"] = 1;
|
||||
h["b"] = 5;
|
||||
|
||||
Assert.That(exp.eval.eval(h));
|
||||
|
||||
h["a"] = 1;
|
||||
h["b"] = 1;
|
||||
|
||||
Assert.That(!exp.eval.eval(h));
|
||||
|
||||
// ~= (matches)
|
||||
exp = (BooleanProgram)parser.Parse("a~=b");
|
||||
h = new Hashtable();
|
||||
h["a"] = "test";
|
||||
h["b"] = "test";
|
||||
|
||||
Assert.That(exp.eval.eval(h));
|
||||
|
||||
h = new Hashtable();
|
||||
h["a"] = "test";
|
||||
h["b"] = "te.*";
|
||||
|
||||
Assert.That(exp.eval.eval(h));
|
||||
|
||||
h = new Hashtable();
|
||||
h["a"] = "test";
|
||||
h["b"] = "ti.*";
|
||||
|
||||
Assert.That(!exp.eval.eval(h));
|
||||
|
||||
h["a"] = DBNull.Value;
|
||||
h["b"] = "1";
|
||||
|
||||
Assert.That(!exp.eval.eval(h));
|
||||
|
||||
|
||||
// mixing ints and doubles
|
||||
exp = (BooleanProgram)parser.Parse("a=b");
|
||||
h = new Hashtable();
|
||||
h["a"] = 1.0;
|
||||
h["b"] = 5;
|
||||
|
||||
Assert.That(!exp.eval.eval(h));
|
||||
|
||||
exp = (BooleanProgram)parser.Parse("a=b");
|
||||
h = new Hashtable();
|
||||
h["a"] = 1;
|
||||
h["b"] = 1.0;
|
||||
|
||||
exp = (BooleanProgram)parser.Parse("a=b");
|
||||
h = new Hashtable();
|
||||
h["a"] = 1.1;
|
||||
h["b"] = 1;
|
||||
|
||||
Assert.That(!exp.eval.eval(h));
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,18 +1,15 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
namespace BCG;
|
||||
|
||||
using BCG.Utilities;
|
||||
using Bts.BooleanExpressions;
|
||||
|
||||
namespace BCG
|
||||
{
|
||||
public class BooleanExpressionFilter : IStreamFilter
|
||||
{
|
||||
public BooleanExpressionFilter(string exp)
|
||||
{myInterpreter = new BooleanExpressionsInterpreter(exp);}
|
||||
using Spark.Core.Collections;
|
||||
|
||||
public bool filter(IBlueRow dr)
|
||||
{ return myInterpreter.eval(dr); }
|
||||
public class BooleanExpressionFilter : IStreamFilter {
|
||||
IBooleanExpression myInterpreter;
|
||||
|
||||
private BooleanExpressionsInterpreter myInterpreter;
|
||||
}
|
||||
public BooleanExpressionFilter(string exp) =>
|
||||
myInterpreter = BooleanExpressionParser.Parse(exp);
|
||||
|
||||
public bool filter(IBlueRow dr) =>
|
||||
myInterpreter.Evaluate(GenericDictionaryWrapper.Create<string, object?>(dr));
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
#nullable enable
|
||||
namespace Spark.Core.Collections {
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public static class AggregateCollection {
|
||||
public static ICollection<T> Create<T>(ICollection<T> src1, ICollection<T> src2) =>
|
||||
new AggregateCollection<T>(src1, src2);
|
||||
}
|
||||
|
||||
public class AggregateCollection<T> : ICollection, ICollection<T> {
|
||||
readonly ICollection<T> src1;
|
||||
readonly ICollection<T> src2;
|
||||
|
||||
public AggregateCollection(ICollection<T> src1, ICollection<T> src2) {
|
||||
this.src1 = src1;
|
||||
this.src2 = src2;
|
||||
}
|
||||
|
||||
public int Count => src1.Count + src2.Count;
|
||||
|
||||
public bool IsReadOnly => true;
|
||||
|
||||
public bool IsSynchronized => false;
|
||||
|
||||
object ICollection.SyncRoot => new();
|
||||
|
||||
void ICollection<T>.Add(T item) =>
|
||||
throw new NotSupportedException();
|
||||
|
||||
void ICollection<T>.Clear() =>
|
||||
throw new NotSupportedException();
|
||||
|
||||
public bool Contains(T item) =>
|
||||
src1.Contains(item) || src2.Contains(item);
|
||||
|
||||
public void CopyTo(T[] array, int arrayIndex) {
|
||||
foreach (var v in src1) {
|
||||
array[arrayIndex++] = v;
|
||||
}
|
||||
foreach (var v in src2) {
|
||||
array[arrayIndex++] = v;
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerator<T> GetEnumerator() {
|
||||
foreach (var v in src1) {
|
||||
yield return v;
|
||||
}
|
||||
foreach (var v in src2) {
|
||||
yield return v;
|
||||
}
|
||||
}
|
||||
|
||||
public bool Remove(T item) =>
|
||||
throw new NotSupportedException();
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() =>
|
||||
GetEnumerator();
|
||||
|
||||
public void CopyTo(Array array, int index) {
|
||||
foreach (var v in src1) {
|
||||
array.SetValue(v, index++);
|
||||
}
|
||||
foreach (var v in src2) {
|
||||
array.SetValue(v, index++);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
#nullable enable
|
||||
namespace Spark.Core.Collections {
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
public static class AggregateDictionary {
|
||||
public static AggregateDictionary<K, V> Create<K, V>(
|
||||
IDictionary<K, V> src1, IDictionary<K, V> src2) where K : notnull =>
|
||||
new(src1, src2);
|
||||
}
|
||||
|
||||
public class AggregateDictionary<TKey, TValue> : IDictionary<TKey, TValue> where TKey : notnull {
|
||||
readonly IDictionary<TKey, TValue> src1;
|
||||
readonly IDictionary<TKey, TValue> src2;
|
||||
|
||||
public AggregateDictionary(IDictionary<TKey, TValue> src1, IDictionary<TKey, TValue> src2) {
|
||||
this.src1 = src1;
|
||||
this.src2 = src2;
|
||||
}
|
||||
|
||||
public int Count => src1.Count + src2.Count;
|
||||
|
||||
public bool IsReadOnly => true;
|
||||
|
||||
public ICollection<TKey> Keys =>
|
||||
AggregateCollection.Create(src1.Keys, src2.Keys);
|
||||
|
||||
public ICollection<TValue> Values =>
|
||||
AggregateCollection.Create(src1.Values, src2.Values);
|
||||
|
||||
public TValue this[TKey key] {
|
||||
get {
|
||||
if (src1.TryGetValue(key, out var v1)) {
|
||||
return v1;
|
||||
}
|
||||
if (src2.TryGetValue(key, out var v2)) {
|
||||
return v2;
|
||||
}
|
||||
throw new KeyNotFoundException();
|
||||
}
|
||||
set => throw new NotSupportedException();
|
||||
}
|
||||
|
||||
public void Add(TKey key, TValue value) =>
|
||||
throw new NotSupportedException();
|
||||
|
||||
public void Add(KeyValuePair<TKey, TValue> item) =>
|
||||
throw new NotSupportedException();
|
||||
|
||||
public void Clear() =>
|
||||
throw new NotSupportedException();
|
||||
|
||||
public bool Contains(KeyValuePair<TKey, TValue> item) =>
|
||||
src1.Contains(item) || src2.Contains(item);
|
||||
|
||||
public bool ContainsKey(TKey key) =>
|
||||
(key is TKey k1 && src1.ContainsKey(k1)) || (key is TKey k2 && src2.ContainsKey(k2));
|
||||
|
||||
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex) =>
|
||||
AggregateCollection.Create(src1, src2).CopyTo(array, arrayIndex);
|
||||
|
||||
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() {
|
||||
foreach (var kv in src1) {
|
||||
yield return new(kv.Key, kv.Value);
|
||||
}
|
||||
foreach (var kv in src2) {
|
||||
yield return new(kv.Key, kv.Value);
|
||||
}
|
||||
}
|
||||
|
||||
public bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value) =>
|
||||
src1.TryGetValue(key, out value) ||
|
||||
src2.TryGetValue(key, out value);
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() =>
|
||||
GetEnumerator();
|
||||
|
||||
bool IDictionary<TKey, TValue>.Remove(TKey key) =>
|
||||
throw new NotSupportedException();
|
||||
|
||||
bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item) =>
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
#nullable enable
|
||||
namespace Spark.Core.Collections {
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public static class AggregateDictionaryEnumerator {
|
||||
public static AggregateDictionaryEnumerator<K1, V1, K2, V2> Create<K1, V1, K2, V2>(
|
||||
IDictionary<K1, V1> primary, IDictionary<K2, V2> secondary) where K1 : notnull where K2 : notnull =>
|
||||
new(primary, secondary);
|
||||
}
|
||||
|
||||
public class AggregateDictionaryEnumerator<K1, V1, K2, V2> : IDictionaryEnumerator where K1 : notnull where K2 : notnull {
|
||||
readonly IDictionary<K1, V1> src1;
|
||||
readonly IDictionary<K2, V2> src2;
|
||||
|
||||
IEnumerator<KeyValuePair<K1, V1>>? en1;
|
||||
IEnumerator<KeyValuePair<K2, V2>>? en2;
|
||||
bool done;
|
||||
|
||||
public AggregateDictionaryEnumerator(IDictionary<K1, V1> src1, IDictionary<K2, V2> src2) {
|
||||
this.src1 = src1;
|
||||
this.src2 = src2;
|
||||
}
|
||||
|
||||
(KeyValuePair<K1, V1>?, KeyValuePair<K2, V2>?) currentOneof {
|
||||
get {
|
||||
if (en1 is not null) {
|
||||
var kv = en1.Current;
|
||||
return (new(kv.Key, kv.Value), null);
|
||||
}
|
||||
if (en2 is not null) {
|
||||
var kv = en2.Current;
|
||||
return (null, new(kv.Key, kv.Value));
|
||||
}
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
}
|
||||
|
||||
TResult selectResult<TResult>(
|
||||
Func<KeyValuePair<K1, V1>, TResult> from1, Func<KeyValuePair<K2, V2>, TResult> from2) {
|
||||
var (kv1, kv2) = currentOneof;
|
||||
if (kv1.HasValue) {
|
||||
return from1(kv1.Value);
|
||||
}
|
||||
if (kv2.HasValue) {
|
||||
return from2(kv2.Value);
|
||||
}
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
|
||||
public DictionaryEntry Entry =>
|
||||
selectResult<DictionaryEntry>(
|
||||
kv => new(kv.Key, kv.Value),
|
||||
kv => new(kv.Key, kv.Value));
|
||||
|
||||
public object Key =>
|
||||
selectResult<object>(
|
||||
kv => kv.Key,
|
||||
kv => kv.Key);
|
||||
|
||||
public object? Value =>
|
||||
selectResult<object?>(
|
||||
kv => kv.Value,
|
||||
kv => kv.Value);
|
||||
|
||||
object IEnumerator.Current =>
|
||||
Entry;
|
||||
|
||||
public bool MoveNext() {
|
||||
if (done) {
|
||||
return false;
|
||||
}
|
||||
if (en1 is null && en2 is null) {
|
||||
// Hasn't started yet
|
||||
return moveFirst();
|
||||
}
|
||||
return moveSubsequent();
|
||||
}
|
||||
|
||||
public void Reset() {
|
||||
en1?.Reset();
|
||||
en2?.Reset();
|
||||
done = false;
|
||||
}
|
||||
|
||||
bool moveFirst() {
|
||||
en1 = src1.GetEnumerator();
|
||||
if (en1.MoveNext()) {
|
||||
return true;
|
||||
}
|
||||
// Ok, so en1 has no elements. Set it to null, and try MoveNext w/ en2.
|
||||
en1 = null;
|
||||
en2 = src2.GetEnumerator();
|
||||
if (en2.MoveNext()) {
|
||||
return true;
|
||||
}
|
||||
// Ok, en2 has no elements either. Set it to null, and return false.
|
||||
en2 = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool moveSubsequent() {
|
||||
if (en1 is not null) {
|
||||
if (en1.MoveNext()) {
|
||||
return true;
|
||||
}
|
||||
en1 = null;
|
||||
if (en2 is not null) {
|
||||
throw new InvalidOperationException("Invalid state.");
|
||||
}
|
||||
en2 = src2.GetEnumerator();
|
||||
} else if (en2 is null) {
|
||||
throw new InvalidOperationException("Invalid state.");
|
||||
}
|
||||
if (en2.MoveNext()) {
|
||||
return true;
|
||||
}
|
||||
done = true;
|
||||
en2 = null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
#nullable enable
|
||||
namespace Spark.Core.Collections {
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public static class AggregateEnumerator {
|
||||
public static AggregateEnumerator<T1, T2> Create<T1, T2>(
|
||||
IEnumerable<T1> src1, IEnumerable<T2> src2) =>
|
||||
new(src1, src2);
|
||||
}
|
||||
|
||||
public class AggregateEnumerator<T1, T2> : IEnumerator, IEnumerator<object?> {
|
||||
readonly IEnumerable<T1> src1;
|
||||
readonly IEnumerable<T2> src2;
|
||||
IEnumerator<T1>? en1;
|
||||
IEnumerator<T2>? en2;
|
||||
|
||||
bool done;
|
||||
|
||||
public AggregateEnumerator(IEnumerable<T1> src1, IEnumerable<T2> src2) {
|
||||
this.src1 = src1;
|
||||
this.src2 = src2;
|
||||
}
|
||||
|
||||
object? IEnumerator<object?>.Current => Current;
|
||||
|
||||
public object? Current => selectResult<object?>(v => v, v => v);
|
||||
|
||||
public bool MoveNext() {
|
||||
if (done) {
|
||||
return false;
|
||||
}
|
||||
if (en1 is null && en2 is null) {
|
||||
// Hasn't started yet
|
||||
return moveFirst();
|
||||
}
|
||||
return moveSubsequent();
|
||||
}
|
||||
|
||||
public void Reset() {
|
||||
en1?.Reset();
|
||||
en2?.Reset();
|
||||
done = false;
|
||||
}
|
||||
|
||||
((bool HasValue, T1 Value)? FirstVal, (bool HasValue, T2 Value)? SecondVal) currentOneof {
|
||||
get {
|
||||
if (en1 is not null) {
|
||||
var firstVal = en1.Current;
|
||||
return ((true, firstVal), null);
|
||||
}
|
||||
if (en2 is not null) {
|
||||
var secondVal = en2.Current;
|
||||
return (null, (true, secondVal));
|
||||
}
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
}
|
||||
|
||||
TResult selectResult<TResult>(
|
||||
Func<T1, TResult> from1, Func<T2, TResult> from2) {
|
||||
var (firstVal, secondVal) = currentOneof;
|
||||
if (firstVal.HasValue) {
|
||||
return from1(firstVal.Value.Value);
|
||||
}
|
||||
if (secondVal.HasValue) {
|
||||
return from2(secondVal.Value.Value);
|
||||
}
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
|
||||
bool moveFirst() {
|
||||
en1 = src1.GetEnumerator();
|
||||
if (en1.MoveNext()) {
|
||||
return true;
|
||||
}
|
||||
// Ok, so en1 has no elements. Set it to null, and try MoveNext w/ en2.
|
||||
en1 = null;
|
||||
en2 = src2.GetEnumerator();
|
||||
if (en2.MoveNext()) {
|
||||
return true;
|
||||
}
|
||||
// Ok, en2 has no elements either. Set it to null, and return false.
|
||||
en2 = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool moveSubsequent() {
|
||||
if (en1 is not null) {
|
||||
if (en1.MoveNext()) {
|
||||
return true;
|
||||
}
|
||||
en1 = null;
|
||||
if (en2 is not null) {
|
||||
throw new InvalidOperationException("Invalid state.");
|
||||
}
|
||||
en2 = src2.GetEnumerator();
|
||||
} else if (en2 is null) {
|
||||
throw new InvalidOperationException("Invalid state.");
|
||||
}
|
||||
if (en2.MoveNext()) {
|
||||
return true;
|
||||
}
|
||||
done = true;
|
||||
en2 = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
public void Dispose() {
|
||||
en1?.Dispose();
|
||||
en1 = null;
|
||||
en2?.Dispose();
|
||||
en2 = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
#nullable enable
|
||||
namespace Spark.Core.Collections;
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
public static class SelectorCollection {
|
||||
public static ICollection<TResult> Create<T, TResult>(
|
||||
ICollection<T> source, Func<T, TResult> convert, Func<TResult, T>? convertBack = null) =>
|
||||
new SelectorCollection<T, TResult>(source, convert, convertBack);
|
||||
}
|
||||
|
||||
public class SelectorCollection<T, TResult> : ICollection<TResult> {
|
||||
readonly ICollection<T> source;
|
||||
readonly Func<T, TResult> convert;
|
||||
readonly Func<TResult, T>? convertBack;
|
||||
|
||||
public SelectorCollection(
|
||||
ICollection<T> source, Func<T, TResult> convert, Func<TResult, T>? convertBack = null) {
|
||||
this.source = source;
|
||||
this.convert = convert;
|
||||
this.convertBack = convertBack;
|
||||
}
|
||||
|
||||
public int Count => source.Count;
|
||||
|
||||
public bool IsReadOnly => source.IsReadOnly;
|
||||
|
||||
public void Add(TResult item) {
|
||||
if (convertBack is null) {
|
||||
throw new InvalidOperationException(
|
||||
"Unable to add item because convertBack delegate is null");
|
||||
}
|
||||
source.Add(convertBack(item));
|
||||
}
|
||||
|
||||
public void Clear() =>
|
||||
source.Clear();
|
||||
|
||||
public bool Contains(TResult item) =>
|
||||
source.Select(convert).Contains(item);
|
||||
|
||||
public void CopyTo(TResult[] array, int arrayIndex) {
|
||||
foreach (var v in source) {
|
||||
array[arrayIndex++] = convert(v);
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerator<TResult> GetEnumerator() {
|
||||
foreach (var v in source) {
|
||||
yield return convert(v);
|
||||
}
|
||||
}
|
||||
|
||||
public bool Remove(TResult item) {
|
||||
if (convertBack is null) {
|
||||
throw new InvalidOperationException(
|
||||
"Unable to remove item because convertBack delegate is null");
|
||||
}
|
||||
return source.Remove(convertBack(item));
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() =>
|
||||
GetEnumerator();
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
#nullable enable
|
||||
namespace Spark.DataStream {
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public static class GenericCollectionWrapper {
|
||||
public static GenericCollectionWrapper<T> Create<T>(
|
||||
ICollection source, Func<object?, T>? cast = null) =>
|
||||
new(source, cast);
|
||||
}
|
||||
|
||||
public class GenericCollectionWrapper<T> : ICollection<T> {
|
||||
readonly ICollection source;
|
||||
readonly ICollection<T>? genericSource;
|
||||
readonly IList? listSource;
|
||||
readonly Func<object?, T> cast;
|
||||
|
||||
public GenericCollectionWrapper(ICollection source, Func<object?, T>? cast = null) {
|
||||
this.source = source;
|
||||
this.cast = cast ?? ConvertUtils.try_convert<T>;
|
||||
genericSource = source as ICollection<T>;
|
||||
listSource = source as IList;
|
||||
}
|
||||
|
||||
public int Count => source.Count;
|
||||
|
||||
public bool IsReadOnly => genericSource?.IsReadOnly ?? false;
|
||||
|
||||
public void Add(T item) {
|
||||
if (genericSource is not null) {
|
||||
genericSource.Add(item);
|
||||
return;
|
||||
}
|
||||
if (listSource is not null) {
|
||||
listSource?.Add(item);
|
||||
return;
|
||||
}
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
|
||||
public void Clear() {
|
||||
if (genericSource is not null) {
|
||||
genericSource?.Clear();
|
||||
return;
|
||||
}
|
||||
listSource?.Clear();
|
||||
}
|
||||
|
||||
public bool Contains(T item) {
|
||||
if (genericSource is not null) {
|
||||
return genericSource.Contains(item);
|
||||
}
|
||||
var cmp = EqualityComparer<T>.Default;
|
||||
foreach (var obj in source) {
|
||||
if (cmp.Equals(cast(obj), item)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void CopyTo(T[] array, int arrayIndex) {
|
||||
if (genericSource is not null) {
|
||||
genericSource.CopyTo(array, arrayIndex);
|
||||
return;
|
||||
}
|
||||
foreach (var obj in source) {
|
||||
array[arrayIndex++] = cast(obj);
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerator<T> GetEnumerator() {
|
||||
if (genericSource is not null) {
|
||||
return genericSource.GetEnumerator();
|
||||
}
|
||||
return castImpl();
|
||||
IEnumerator<T> castImpl() {
|
||||
foreach (var obj in source) {
|
||||
yield return cast(obj);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public bool Remove(T item) {
|
||||
if (genericSource is not null) {
|
||||
return genericSource.Remove(item);
|
||||
}
|
||||
if (listSource is not null) {
|
||||
int origCount = Count;
|
||||
listSource.Remove(cast(item));
|
||||
return Count == origCount - 1;
|
||||
}
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() =>
|
||||
source.GetEnumerator();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
#nullable enable
|
||||
namespace Spark.Core.Collections {
|
||||
using Spark.DataStream;
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
public static class GenericDictionaryWrapper {
|
||||
public static GenericDictionaryWrapper<TKey, TValue> Create<TKey, TValue>(
|
||||
IDictionary source,
|
||||
Func<object, TKey>? castKey = null,
|
||||
Func<object?, TValue>? castValue = null) where TKey : notnull =>
|
||||
new(source, castKey, castValue);
|
||||
}
|
||||
|
||||
public class GenericDictionaryWrapper<TKey, TValue> : IDictionary<TKey, TValue> where TKey : notnull {
|
||||
readonly IDictionary source;
|
||||
readonly Func<object, TKey> castKey;
|
||||
readonly Func<object?, TValue> castValue;
|
||||
|
||||
public GenericDictionaryWrapper(
|
||||
IDictionary source, Func<object, TKey>? castKey = null, Func<object?, TValue>? castValue = null) {
|
||||
this.source = source;
|
||||
this.castKey = castKey ?? ConvertUtils.try_convert<TKey>;
|
||||
this.castValue = castValue ?? ConvertUtils.try_convert<TValue>;
|
||||
}
|
||||
|
||||
public TValue this[TKey key] {
|
||||
get => castValue(source[key]);
|
||||
set => source[key] = castValue(value);
|
||||
}
|
||||
|
||||
public ICollection<TKey> Keys => GenericCollectionWrapper.Create<TKey>(source.Keys);
|
||||
|
||||
public ICollection<TValue> Values => GenericCollectionWrapper.Create<TValue>(source.Values);
|
||||
|
||||
public int Count => source.Count;
|
||||
|
||||
public bool IsReadOnly => source.IsReadOnly;
|
||||
|
||||
public void Add(TKey key, TValue value) =>
|
||||
source.Add(key, value);
|
||||
|
||||
public void Add(KeyValuePair<TKey, TValue> item) =>
|
||||
source.Add(item.Key, item.Value);
|
||||
|
||||
public void Clear() =>
|
||||
source.Clear();
|
||||
|
||||
public bool Contains(KeyValuePair<TKey, TValue> item) =>
|
||||
source.Contains(item.Key) && Equals(castValue(source[item.Key]), item.Value);
|
||||
|
||||
public bool ContainsKey(TKey key) =>
|
||||
source.Contains(key);
|
||||
|
||||
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex) {
|
||||
foreach (DictionaryEntry kv in source) {
|
||||
array[arrayIndex++] = new(castKey(kv.Key), castValue(kv.Value));
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() {
|
||||
foreach (DictionaryEntry kv in source) {
|
||||
yield return new(castKey(kv.Key), castValue(kv.Value));
|
||||
}
|
||||
}
|
||||
|
||||
public bool Remove(TKey key) {
|
||||
int origCount = source.Count;
|
||||
source.Remove(key);
|
||||
return source.Count == origCount - 1;
|
||||
}
|
||||
|
||||
public bool Remove(KeyValuePair<TKey, TValue> item) =>
|
||||
Contains(item) && Remove(item.Key);
|
||||
|
||||
public bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value) {
|
||||
if (!source.Contains(key)) {
|
||||
value = default;
|
||||
return false;
|
||||
}
|
||||
value = castValue(source[key]);
|
||||
return true;
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() =>
|
||||
GetEnumerator();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
#nullable enable
|
||||
namespace Spark.Core.Collections {
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public static class NonNullCollectionWrapper {
|
||||
public static NonNullCollectionWrapper<T> Create<T>(ICollection<T?> src) =>
|
||||
new(src);
|
||||
}
|
||||
|
||||
public class NonNullCollectionWrapper<T> : ICollection<T> {
|
||||
readonly ICollection<T?> src;
|
||||
|
||||
public NonNullCollectionWrapper(ICollection<T?> src) =>
|
||||
this.src = src;
|
||||
|
||||
public int Count => src.Count;
|
||||
|
||||
public bool IsReadOnly => src.IsReadOnly;
|
||||
|
||||
public void Add(T item) =>
|
||||
src.Add(item);
|
||||
|
||||
public void Clear() =>
|
||||
src.Clear();
|
||||
|
||||
public bool Contains(T item) =>
|
||||
src.Contains(item);
|
||||
|
||||
public void CopyTo(T[] array, int arrayIndex) =>
|
||||
src.CopyTo(array, arrayIndex);
|
||||
|
||||
public IEnumerator<T> GetEnumerator() {
|
||||
foreach (var v in src) {
|
||||
if (v is null) {
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
yield return v;
|
||||
}
|
||||
}
|
||||
|
||||
public bool Remove(T item) =>
|
||||
src.Remove(item);
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() =>
|
||||
GetEnumerator();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user