153 lines
4.1 KiB
Plaintext
153 lines
4.1 KiB
Plaintext
%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);
|
|
}
|
|
;
|