Don't understand my error

59 views
Skip to first unread message

KKitsune

unread,
Apr 30, 2011, 11:10:31 AM4/30/11
to sprache
Hi all! And thanks for this parser, the BEST i've seen for C#! But I'm
getting an error that I can't seem to understand.
Here is my parser code :
static Expression CompileFunction(string Code, Type RetT,
List<ParameterExpression> ParentVars, IEnumerable<ParameterExpression>
Params)
{
List<ParameterExpression> vars = new
List<ParameterExpression>(ParentVars);

var Identifier =
Parse.Letter.Or(Parse.Char('_')).AtLeastOnce()
.Concat(Parse.LetterOrDigit.Or(Parse.Char('_')).Many())
.Text().Token().Named("Identifier");

var Int = (from i in Parse.Number.Token() select
Expression.Constant(int.Parse(i, nfi))).Named("Int");

var Float =
(from integral in Parse.Number
from dot in Parse.Char('.')
from fraction in Parse.Number
from f in Parse.Char('f')
select Expression.Constant(float.Parse(integral + dot + fraction,
nfi)))
.Token().Named("Float");

var Double =
(from integral in Parse.Number
from dot in Parse.Char('.')
from fraction in Parse.Number
select Expression.Constant(double.Parse(integral + dot + fraction,
nfi)))
.Token().Named("Double");

var Number = Float.Or(Double).Or(Int);

var String =
(from open in Parse.Char('"')
from text in Parse.CharExcept('"').Many().Text()
from close in Parse.Char('"')
select Expression.Constant(text))
.Token().Named("String");

var Bool =
Parse.String("true").Return(Expression.Constant(true))
.Or(Parse.String("false").Return(Expression.Constant(false))).Token().Named("Bool");

var ParamDecl =
(from name in Identifier
from colon in Parse.Char(':').Token()
from type in Identifier
select Expression.Parameter(GetType(type), name))
.Token().Named("ParamDecl");

var Function =
(from kw in Parse.String("function").Token()
from lparen in Parse.Char('(').Token()
from pars in
(from f in ParamDecl
from r in Parse.Char(',').Then(_ => ParamDecl).Many()
select Prepand(f, r))
from rparen in Parse.Char(')').Token()
from colon in Parse.Char(':').Token()
from retT in Identifier
from begin in Parse.Char('{').Token()
from code in Parse.CharExcept('}').Many().Text()
from end in Parse.Char('}').Token()
select CompileFunction(code, GetType(retT), vars, pars))
.Named("Function");

var Constant = String.XOr(Number).XOr(Bool);

var Add = Operator("+", ExpressionType.AddChecked).XOr(Operator("+=",
ExpressionType.AddAssignChecked));
var Subtract = Operator("-",
ExpressionType.SubtractChecked).XOr(Operator("-=",
ExpressionType.SubtractAssignChecked));
var Multiply = Operator("*",
ExpressionType.MultiplyChecked).XOr(Operator("*=",
ExpressionType.MultiplyAssignChecked));
var Divide = Operator("/", ExpressionType.Divide).XOr(Operator("/=",
ExpressionType.DivideAssign));
var Assign = Operator("=", ExpressionType.Assign);

Parser<Expression> Term = null, Expr = null,
Factor =
((from lpar in Parse.Char('(').Token()
from expr in Parse.Ref(() => Expr)
from rpar in Parse.Char(')').Token()
select expr).Named("Expression"))
.XOr(Constant).XOr(Identifier.Select(name => GetVar(ref vars,
name))).Token();

Term = Parse.ChainOperator(Divide.Or(Multiply).Or(Assign), Factor,
MakeBinary);
Expr = Parse.ChainOperator(Add.Or(Subtract), Term, MakeBinary);

var VarInit =
(from kw in Parse.String("var").Token()
from name in Identifier
from equal in Parse.Char('=').Token()
from val in Function.XOr(Expr)
select Expression.Assign(CreateVar(ref vars, name, val.Type), val))
.Named("VarInit");

var Body =
(from op in VarInit.XOr(Expr)
from eol in Parse.Char(';').Token()
select op)
.Many().Named("Body");

BlockExpression block = Expression.Block(RetT, vars,
Body.Token().End().Parse(Code));
if (Params == null)
return Expression.Lambda<Action>(block);
else
{
List<Type> ParamsType = new List<Type>();
foreach (var p in Params) ParamsType.Add(p.Type);
Type fnc;
if (RetT == typeof(void))
fnc = Expression.GetActionType(ParamsType.ToArray());
else
{
ParamsType.Add(RetT);
fnc = Expression.GetFuncType(ParamsType.ToArray());
}

return Expression.Lambda(fnc, block, Params);
}
}

And I'm trying to parse this :
var Test = function()
{
var i = 3;
var PI = 3.14159;
i *= PI;
};

And I'm getting this strange error that's making me freak out :
Unexpected 'v'; Expected End of Input (Line 1, Column 1).

Can someone help me out?
Thanks in advance!

Nicholas Blumhardt

unread,
May 4, 2011, 6:57:47 AM5/4/11
to sprache...@googlegroups.com
Hi there!

At first glance I can't see anything that might be the cause - the best advice I can offer is to break the parser into small pieces and test those individually. Sorry it isn't much to go on.

Regards,
Nick

Daniel Grondin

unread,
May 6, 2011, 8:20:46 AM5/6/11
to sprache
Got it ... I just came with the answer (pretty simple) : the parameter
list in the function declaration expected to have at least one param
and I was giving it none...crash... so I modified it.
[...]
var Function =
(from kw in Parse.String("function").Token()
from lparen in Parse.Char('(').Token()
from pars in
(from f in ParamDecl
from r in Parse.Char(',').Then(_ => ParamDecl).Many()
select Prepand(f, r)).XOr(Parse.Return(new
List<ParameterExpression>() as IEnumerable<ParameterExpression>))
from rparen in Parse.Char(')').Token()
from colon in Parse.Char(':').Token()
from retT in Identifier
from begin in Parse.Char('{').Token()
from code in Parse.CharExcept('}').Many().Text()
from end in Parse.Char('}').Token()
select CompileFunction(code, GetType(retT), vars, pars))
.Named("Function");
[...]
And now it's working! Awesome parsing library, thanks a million!

On May 4, 5:57 am, Nicholas Blumhardt <nicholas.blumha...@gmail.com>
wrote:
> Hi there!
>
> At first glance I can't see anything that might be the cause - the best
> advice I can offer is to break the parser into small pieces and test those
> individually. Sorry it isn't much to go on.
>
> Regards,
> Nick
>
Reply all
Reply to author
Forward
0 new messages