Comments (5)
That being said ... the following works fine (using between)
""
("")
Parsing logic
private static readonly Parser<char, char> Quote = Char('"');
private static readonly Parser<char, string> String =
Token(c => c != '"')
.ManyString()
.Between(Quote);
// Quoted string
// (results in Literal)
private static readonly Parser<char, IExpr> LiteralString
= Tok(String)
.Select<IExpr>(value => new Literal(value))
.Labelled("string literal");
from pidgin.
I suspect you have an infinite loop in your recursive expr
call. Could you supply your full code please?
from pidgin.
Thanks again for getting back to me so quickly.
Happy to supply the code (see below), but the loop is in the Parser, with the code I supplied. I have a number of functions that work great, the only failure is in the TEST() permutation.
Examples of Functions that work
Result = eval.ParseString("INT(123)", 123);
Result = eval.ParseString("INT("123")", 123);
Result = eval.ParseString("STR(123)", "123");
Result = eval.ParseString("IF(1==1,"123","456")", "123");
Result = eval.ParseString("IF(1⩵1,"123","456")", "123");
Result = eval.ParseString("IF(1!=1,"123","456")", "456");
Result = eval.ParseString("IF(1≠1,"123","456")", "456");
Result = eval.ParseString("SUBSTRING("ABCDEF",2,3)", "CDE");
Result = eval.ParseString("SUBSTRING("ABCDEF",2)", "CDEF");
Result = eval.ParseString("SUM(123,"456",100)", 679);
TEST() never makes it out of the parser.
Any thoughts would be greatly appreciated
Parser code
public static class ExprParser
{
private static Parser<char, T> Tok(Parser<char, T> token)
=> Try(token).Before(SkipWhitespaces);
private static Parser<char, string> Tok(string token)
=> Tok(String(token));
private static Parser<char, T> Parenthesised<T>(Parser<char, T> parser)
=> parser.Between(Tok("("), Tok(")"));
private static Parser<char, Func<IExpr, IExpr, IExpr>> Binary(Parser<char, BinaryOperatorType> op)
=> op.Select<Func<IExpr, IExpr, IExpr>>(type => (l, r) => new BinaryOp(type, l, r));
private static Parser<char, Func<IExpr, IExpr>> Unary(Parser<char, UnaryOperatorType> op)
=> op.Select<Func<IExpr, IExpr>>(type => o => new UnaryOp(type, o));
private static readonly Parser<char, Func<IExpr, IExpr, IExpr>> Plus
= Binary(Tok("+").ThenReturn(BinaryOperatorType.Plus));
private static readonly Parser<char, Func<IExpr, IExpr, IExpr>> Minus
= Binary(Tok("-").ThenReturn(BinaryOperatorType.Minus));
private static readonly Parser<char, Func<IExpr, IExpr, IExpr>> Multiply
= Binary(Tok("*").ThenReturn(BinaryOperatorType.Multiply));
private static readonly Parser<char, Func<IExpr, IExpr, IExpr>> Divide
= Binary(Tok("/").ThenReturn(BinaryOperatorType.Divide));
private static readonly Parser<char, Func<IExpr, IExpr, IExpr>> EqualTo
= Binary(Tok("=").Then(String("=")).ThenReturn(BinaryOperatorType.EqualTo)); // "=="
private static readonly Parser<char, Func<IExpr, IExpr, IExpr>> EqualToA
// //= Binary(Tok("=").ThenReturn(BinaryOperatorType.EqualTo)); // 'FULLWIDTH EQUALS SIGN' (U+FF1D)
= Binary(Tok("⩵").ThenReturn(BinaryOperatorType.EqualTo)); // Two Consecutive Equals Signs (U+2A75)
private static readonly Parser<char, Func<IExpr, IExpr, IExpr>> NotEqualTo
= Binary(Tok("!").Then(String("=")).ThenReturn(BinaryOperatorType.NotEqualTo)); // "!="
private static readonly Parser<char, Func<IExpr, IExpr, IExpr>> NotEqualToA
= Binary(Tok("≠").ThenReturn(BinaryOperatorType.NotEqualTo)); // 'NOT EQUAL TO' (U+2260)
private static readonly Parser<char, Func<IExpr, IExpr, IExpr>> AssignTo
= Binary(Tok(":").Then(String("=")).ThenReturn(BinaryOperatorType.AssignTo)); // ":="
private static readonly Parser<char, Func<IExpr, IExpr, IExpr>> AssignToA
= Binary(Tok("≔").ThenReturn(BinaryOperatorType.AssignTo)); // 'COLON EQUALS' (U+2254)
private static readonly Parser<char, Func<IExpr, IExpr>> Neg
= Unary(Tok("-").ThenReturn(UnaryOperatorType.Neg));
private static readonly Parser<char, Func<IExpr, IExpr>> UPlus
= Unary(Tok("+").ThenReturn(UnaryOperatorType.UPlus));
private static readonly Parser<char, Func<IExpr, IExpr>> Complement
= Unary(Tok("~").ThenReturn(UnaryOperatorType.Complement));
private static readonly Parser<char, IExpr> Identifier
= Tok(Letter.Then(LetterOrDigit.ManyString(), (h, t) => h + t))
.Select<IExpr>(name => new Identifier(name))
.Labelled("identifier");
private static readonly Parser<char, IExpr> Literal
= Tok(Num)
.Select<IExpr>(value => new Literal(value))
.Labelled("integer literal");
// Floating point number
// (not working)
// private static readonly Parser<char, char> Period = Char('.');
// private static readonly Parser<char, string> LiteralFloat
// = Try(FloatString, LiteralFloat)
// .Select<IExpr>(value => new Literal(value))
// .Labelled("float literal");
// private static readonly Parser<char, string> FloatString =
// Digit.ManyString().Then(Period).Then(Digit).ManyString();
private static readonly Parser<char, char> Quote = Char('"');
private static readonly Parser<char, string> String =
Token(c => c != '"')
.ManyString()
.Between(Quote);
// Quoted string
// (results in Literal)
private static readonly Parser<char, IExpr> LiteralString
= Tok(String)
.Select<IExpr>(value => new Literal(value))
.Labelled("string literal");
private static Parser<char, IExpr> BuildExpressionParser()
{
Parser<char, IExpr> expr = null;
var term = OneOf(
Identifier,
Literal,
LiteralString,
//LiteralFloat,
Parenthesised(Rec(() => expr)).Labelled("parenthesised expression")
);
var call = Parenthesised(Rec(() => expr).Separated(Tok(",")))
.Select<Func<IExpr, IExpr>>(args => method => new Call(method, args.ToImmutableArray()))
.Labelled("function call");
// IEnumerable<OperatorTableRow<char, IExpr>>OperTable= new[]
// {
// Operator.PostfixChainable(call),
// Operator.Prefix(Neg).And(Operator.Prefix(Complement)).And(Operator.Prefix(UPlus)),
// Operator.InfixL(Multiply).And(Operator.InfixL(Divide)),
// Operator.InfixL(Plus).And(Operator.InfixL(Minus)),
// Operator.InfixL(EqualTo).And(Operator.InfixL(NotEqualTo))
// };
expr = ExpressionParser.Build(
term,
new[]
{
Operator.PostfixChainable(call),
Operator.Prefix(Neg).And(Operator.Prefix(Complement)).And(Operator.Prefix(UPlus)),
//Operator.InfixL(Multiply).And(Operator.InfixL(Divide)).And(Operator.InfixL(Plus)).And(Operator.InfixL(Minus)),
Operator.InfixL(Multiply).And(Operator.InfixL(Divide)),
Operator.InfixL(Plus).And(Operator.InfixL(Minus)),
Operator.InfixL(EqualTo).And(Operator.InfixL(EqualToA))
.And(Operator.InfixL(NotEqualTo)).And(Operator.InfixL(NotEqualToA))
.And(Operator.InfixL(AssignTo)).And(Operator.InfixL(AssignToA))
//Operator.InfixL(EqualToA).And(Operator.InfixL(NotEqualToA)).And(Operator.InfixL(AssignToA))
//Operator.InfixL(EqualTo).And(Operator.InfixL(NotEqualTo)),
//Operator.InfixL(AssignTo)
}
).Labelled("expression");
return expr;
}
private static readonly Parser<char, IExpr> Expr = BuildExpressionParser();
public static IExpr ParseOrThrow(string input)
{
try
{
return Expr.ParseOrThrow(input);
}
catch(Exception ex)
{
AppLog.LogException(ex, $"Expression parser {input.Truncate(50)}");
return null;
}
}
//=> Expr.ParseOrThrow(input);
}
from pidgin.
I can't reproduce the issue using the code you supplied. ParseOrThrow("TEST()")
correctly returns new Call(new Identifier("TEST"), ImmutableArray<IExpr>.Empty)
.
from pidgin.
My apologizes, I had not integrated with the latest version. Corrected and verified.
from pidgin.
Related Issues (20)
- Question: can this URN parser be improved? HOT 2
- Question: ways around lack of left recursion? HOT 12
- Expression handling examples/documentation HOT 1
- Parsing context HOT 4
- Is there a good way to turn Digit.Repeat(n) into a string? HOT 2
- How do I keep both sides of a match? HOT 4
- Question: How to parse a list of strings? HOT 1
- Tried to rewind past the start of the input. Please report this as a bug in Pidgin! HOT 1
- Parsing pseudo freeform text HOT 4
- Matching an exact string HOT 1
- Add support for .net framework in the new versions HOT 1
- Docs website is down HOT 9
- Support trimming and AOT HOT 5
- AOT generic expansion warning HOT 15
- Can you write an example for a Luau parser? HOT 2
- Consider writing tutorials instead of examples HOT 1
- Confusing API: unexpected EOF error HOT 1
- Can't locate documentation: parsing non-char streams. HOT 4
- Maybe Bug: Many/UnsignedInt Operator ignores Whitespace HOT 6
- Need help with `Try` or `Int(10)` HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pidgin.