|
View:
New views
3 Messages
—
Rating Filter:
Alert me
|
|
|
Best way to capture input line image?I have an application that I use SableCC as the parsing engine. In this particular application, the users can enter "commands" such as:
DoSomething Arg1=Name Arg2=OutputName Arg3=<a+b+3/(2+x)> where the arguments are generally names of objects that are operated on, but there can be a case where the argument is a math expression, enclosed in < > . The interpreter that I wrote parses the commands and if it sees an expression it evaluates the expression and passes on the result of that evaluation. So, in this case, the "DoSomething" routine would be handed a list of 3 arguments, [Name,OutputName, and the result of the expression]. My problem is that the expression may be perfectly valid, and evaluate to some number, say 0, that in the context of the "DoSomething" routine is not a valid input. Right now all I can do is to toss an error and tell the user that "arg 3 is not valid, ..." or something. I would like to be able to give more specific feedback, say for example the line number, columns, etc and perhaps even the actual image of the offending expression. But I don't want to have every individual routine responsible for doing the expression evaluations (i.e. I don't want to just pass the expression literally without evaluating it) My question is what is the best way capturing that information? The interpreter that I wrote works off of the AST, which is close but not really the actual input, but it seems like even that information is lost after I have evaluated the expressions, because at that point I have just the result that gets handed off to the other routine. Essentially, what I would like are hints on how to pass along what would be considered "debugging" information, or at least enough information so that I can offer the users more specific information than just a comment that the result is wrong. Anyone have any suggestions? Thanks! Roger -- Be Yourself @ mail.com! Choose From 200+ Email Addresses Get a Free Account at www.mail.com _______________________________________________ SableCC-Discussion mailing list SableCC-Discussion@... http://lists.sablecc.org/listinfo/sablecc-discussion |
|
|
Re: Best way to capture input line image?On Fri 2009-07-10 at 11:17h, Roger Pomeroy wrote on sablecc-discussion:
> I have an application that I use SableCC as the parsing engine. In > this particular application, the users can enter "commands" such as: > > DoSomething Arg1=Name Arg2=OutputName Arg3=<a+b+3/(2+x)> : > My problem is that the expression may be perfectly valid, and > evaluate to some number, say 0, that in the context of the > "DoSomething" routine is not a valid input. Right now all I can do > is to toss an error and tell the user that "arg 3 is not valid, ..." > or something. I would like to be able to give more specific > feedback, say for example the line number, columns, etc and perhaps > even the actual image of the offending expression. But I don't want > to have every individual routine responsible for doing the > expression evaluations (i.e. I don't want to just pass the > expression literally without evaluating it) I imagine you have a Command object corresponding to the AST node of the command, generated by the parser, having an execute() method, and also having access to the AST child nodes corresponding to the argument expressions. The execute() method would first evaluate each argument expression, and then pass the resulting values as arguments to the actual DoSomething() routine. If one of the arguments is invalid, the DoSomething() routine throws an exception of a specific type that provides the index of the invalid argument (e.g. an InvalidArgumentException exception class that has a getArgumentIndex() method.) The execute() method would catch these exceptions, and would use the argument index provided by the exception to look up the corresponding argument expression AST node, and generate an error message that includes line/column information and the expression string from the argument node together with the exception message. This way the DoSomething() routine doesn't have to know about argument expressions, and the execute() method in turn doesn't have to know how to validate the argument values. -- Niklas Matthies _______________________________________________ SableCC-Discussion mailing list SableCC-Discussion@... http://lists.sablecc.org/listinfo/sablecc-discussion |
|
|
RE: Best way to capture input line image?Nodes that derive from Token include the line and column number. If you
include them in your AST productions you can spit them back at the user if you run into a problem. Example: Tokens digit = 1-9; plus = '+'; minus = '-'; Concrete term {-> a_term } { add } digit plus digit {-> New a_term.add( digit, digit, plus ) } | { subtr } digit minus digit { -> New a_term.subtr( digit, digit, plus ) }; Abstract a_term { add } digit digit plus | { subtr } digit digit minus; Now you have the plus token in your AST node, and inside it is its line and column number. I'm not aware of how to get the original line, but with a row/col usually you can figure out what you need to figure out about what went wrong. -----Original Message----- From: sablecc-discussion-bounces+chris.vankirk=fdcjapan.com@... [mailto:sablecc-discussion-bounces+chris.vankirk=fdcjapan.com@.... org] On Behalf Of Niklas Matthies Sent: Saturday, July 11, 2009 5:09 AM To: Discussion mailing list for the SableCC project Subject: Re: Best way to capture input line image? On Fri 2009-07-10 at 11:17h, Roger Pomeroy wrote on sablecc-discussion: > I have an application that I use SableCC as the parsing engine. In > this particular application, the users can enter "commands" such as: > > DoSomething Arg1=Name Arg2=OutputName Arg3=<a+b+3/(2+x)> : > My problem is that the expression may be perfectly valid, and > evaluate to some number, say 0, that in the context of the > "DoSomething" routine is not a valid input. Right now all I can do > is to toss an error and tell the user that "arg 3 is not valid, ..." > or something. I would like to be able to give more specific > feedback, say for example the line number, columns, etc and perhaps > even the actual image of the offending expression. But I don't want > to have every individual routine responsible for doing the > expression evaluations (i.e. I don't want to just pass the > expression literally without evaluating it) I imagine you have a Command object corresponding to the AST node of the command, generated by the parser, having an execute() method, and also having access to the AST child nodes corresponding to the argument expressions. The execute() method would first evaluate each argument expression, and then pass the resulting values as arguments to the actual DoSomething() routine. If one of the arguments is invalid, the DoSomething() routine throws an exception of a specific type that provides the index of the invalid argument (e.g. an InvalidArgumentException exception class that has a getArgumentIndex() method.) The execute() method would catch these exceptions, and would use the argument index provided by the exception to look up the corresponding argument expression AST node, and generate an error message that includes line/column information and the expression string from the argument node together with the exception message. This way the DoSomething() routine doesn't have to know about argument expressions, and the execute() method in turn doesn't have to know how to validate the argument values. -- Niklas Matthies _______________________________________________ SableCC-Discussion mailing list SableCC-Discussion@... http://lists.sablecc.org/listinfo/sablecc-discussion _______________________________________________ SableCC-Discussion mailing list SableCC-Discussion@... http://lists.sablecc.org/listinfo/sablecc-discussion |
| Free embeddable forum powered by Nabble | Forum Help |