You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
1.5 KiB
45 lines
1.5 KiB
2 years ago
|
using EC.Helper.RabbitFunc.Compiler;
|
||
|
using EC.Helper.RabbitFunc.Expressions;
|
||
|
|
||
|
namespace EC.Helper.RabbitFunc;
|
||
|
|
||
|
public class RabbitFuncUtil
|
||
|
{
|
||
|
public static LambdaExpression CompileFromSource(string source)
|
||
|
{
|
||
|
if (source == null) throw new ArgumentNullException(nameof(source));
|
||
|
|
||
|
LambdaExpression? lambda = null;
|
||
|
using var reader = new StringReader(source);
|
||
|
var tokenizer = new Tokenizer(reader);
|
||
|
var parser = new Parser(tokenizer);
|
||
|
while (!tokenizer.EndOfStream)
|
||
|
{
|
||
|
var expression = parser.Parse();
|
||
|
if (expression == null) break;
|
||
|
lambda = expression;
|
||
|
}
|
||
|
|
||
|
return lambda ?? throw new CompilerException(tokenizer.Position, "The formula was not found");
|
||
|
}
|
||
|
|
||
|
public static LambdaExpression CompileFromFile(string path)
|
||
|
{
|
||
|
if (path == null)
|
||
|
throw new ArgumentNullException(nameof(path));
|
||
|
|
||
|
LambdaExpression? lambda = null;
|
||
|
using var stream = new FileStream(path, FileMode.Open, FileAccess.Read);
|
||
|
using var reader = new StreamReader(stream, System.Text.Encoding.UTF8);
|
||
|
var tokenizer = new Tokenizer(reader);
|
||
|
var parser = new Parser(tokenizer);
|
||
|
while (!tokenizer.EndOfStream)
|
||
|
{
|
||
|
var expression = parser.Parse();
|
||
|
if (expression == null) break;
|
||
|
lambda = expression;
|
||
|
}
|
||
|
|
||
|
return lambda ?? throw new CompilerException(tokenizer.Position, "The formula was not found");
|
||
|
}
|
||
|
}
|