b3b00/csly

Simple expression parser silently fails

leryss opened this issue · 4 comments

Hi I got the following code for a very simple parser that supports addition. The problem is that the parser returns a result with "IsError=True" but no error messages or any other kind of information and i cant figure out why this is happening.

public enum ExpressionToken
    {
        // Base types
        [Lexeme(@"[0-9]+")]
        INT = 1,

        [Lexeme(@"TRUE|FALSE")]
        BOOL,

        [Lexeme(@"'[^']*'")]
        STRING,

        // Variables and such
        [Lexeme(@"[\w]+")]
        ID,

        [Lexeme(@"\$\$[\w]+")]
        PARAMETER,

        [Lexeme(@"\+")]
        PLUS,

        [Lexeme("[ \t\n]+", isSkippable: true)]
        WS
    }
    public class ExpressionParser
    {
        [Operation((int) ExpressionToken.PLUS, Affix.InFix, Associativity.Left, 10)]
        public Expression Comparison(Expression e1, Token<ExpressionToken> op, Expression e2)
        {
            return new BinaryOpExpression()
            {
                op = BinaryOperator.Plus,
                left_expr = e1,
                right_expr = e2
            };
        }

        [Operand]
        [Production("expr: term")]
        public Expression Operand(Expression f)
        {
            return f;
        }

        [Production("term: INT")]
        public Expression PrimaryInt(Token<ExpressionToken> tok)
        {
            return new IntExpression()
            {
                value = tok.IntValue
            };
        }

        [Production("term: STRING")]
        public Expression PrimaryStr(Token<ExpressionToken> tok)
        {
            return new StringExpression()
            {
                value = tok.Value.Substring(1, tok.Value.Length - 2)
            };
        }

        [Production("term: BOOL")]
        public Expression PrimaryBool(Token<ExpressionToken> tok)
        {
            return new BoolExpression()
            {
                value = bool.Parse(tok.Value)
            };
        }

        [Production("term: ID")]
        public Expression PrimaryId(Token<ExpressionToken> tok)
        {
            return new IdExpression()
            {
                name = tok.Value
            };
        }

        [Production("term: PARAMETER")]
        public Expression PrimaryParam(Token<ExpressionToken> tok)
        {
            return new ParamExpression
            {
                name = tok.Value.Substring(2)
            };
        }
    }
var parse_inst = new ExpressionParser();
var builder = new ParserBuilder<ExpressionToken, Expression>();
var parser = builder.BuildParser(parse_inst, ParserType.EBNF_LL_RECURSIVE_DESCENT, "expr");

var r = parser.Result.Parse("ba + bb");
b3b00 commented

hello,
first your parser can not succeed at parsing "a + b".
Let me explain :
your starting rule is expr as stated by

var parser = builder.BuildParser(parse_inst, ParserType.EBNF_LL_RECURSIVE_DESCENT, "expr");

and expr is term which in turn is either an int a bool a string or an id. So at best you will match the first ID "aa" but you could never parse an addition.

When using the expression parser generator the root rule for expression parsing is _expressions (as stated here : https://github.com/b3b00/csly/wiki/expression-parsing#using-the-expression-parser)
So in your case you can call the parser this way :

var r = parser.Result.Parse("ba + bb", nameof(ExpressionParser)+"_expressions");

Nevertheless this does not explain why you dont have an error message and I will investigate it.

hope this helps

b3b00 commented

I've just found the bug and fixed it. Now you should have a message like

syntax error :unexpected  "PLUS [+] @line 1, column 4 on channel 0" (PLUS).

I've not yet published a new nuget though

b3b00 commented

@leryss have you found my answer ok ? please answer...

b3b00 commented

@leryss I've just public v2.9.0.5 that fixes your issue returning an error message. Please! tell me if you're ok with this fix ! It's important for CSLy to keep improving.