views:

773

answers:

5

I would like to find a javascript parser that can handle and evaluate simple expressions. The parser should be able to evaluate the regular mathematical expressions, and support custom functions with parameters. It also has to support strings handling. String concatenation with || operator support is preferred, but it is okay if + will do the trick.

Examples of an expression that should be handled by the parser:

3 * (2 + 1) - 1

2 * func(2, 2)

func('hello world', 0, 5) || ' you'

Anyone has implemented such thing or can find something similar?

Thank you

+2  A: 

Assuming you mean a javascript parser in javascript, you probably want eval()

see: https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Global_Functions/Eval

Just note that eval, if used improperly, can represent a security risk.

Jonathan Fingland
No, eval is not a choice for me. As you said, it will introduce security risks, and in my case the risk is really high. I want a custom and limited parser to do what is described above. But Thanks
+2  A: 

haven't used it, but a quick google reveals http://jsfromhell.com/classes/math-parser

edit:

What you want to do may be out of reach of the first link, you could also have a look at Douglas Crockford's "parser for Simplified JavaScript"

It's just a parser, so you would have to do all the evaluation yourself. It would, however, make it somewhat easier and it doesn't use eval.

cobbal
Thank you. This one sounds promising and the closest match to my request, however, it lacks strings support. Also modifying it to support strings is difficult, since it does not relay on recursive approach rather than an optimized (but nice) way of parsing. Can anyone help modifying it to support strings?
+3  A: 

I have a modified version of an ActionScript parser (written in AS, not parses AS) that supports custom functions, but not strings. It would probably be easy to add string support though. I'll upload it somewhere so you can get it at http://silentmatt.com/parser2.js http://silentmatt.com/parser3.js.

Edit: I added basic support for strings pretty easily. It doesn't support escape sequences and toJSFunction doesn't work, but it only took a few minutes to get it working. Changing the concatenation operator to "||" should be pretty easy too.

Here's how you would evaluate your example expressions:

js> var parser = new Parser();
js> parser.parse("3 * (2 + 1) - 1").evaluate();
8
js> parser.parse("2 * func(2; 2)").evaluate({ func:Math.pow });
8
js> function substr(s, start, end) { return s.substring(start, end); }
js> parser.parse("func('hello world'; 0; 5) + ' you'").evaluate({ func:substr });
hello you

I don't remember why I used semicolons as argument separators; I think it has something to do with differentiating between functions and built-in "operator" functions.

Another edit:

I've been playing with this a little, and now there's a version with better string support at http://silentmatt.com/parser3.js (toJSFunction works, and you can use standard JavaScript escape sequences). It also uses commas to separate arguments for all functions and || as the string concatenation operator instead of +, which only does addition.

Matthew Crumley
Thank you man. I really appreciate your help. This one matches what I described and it is also easy to modify and extend. This is what I was looking for. I am really thankful for this.
Another comment... Matthew, did I forgot to say that you are great! I really appreciate the modification in the third version man. keep up the good work. All the best.
No problem. I'd been meaning to play with this for awhile (I haven't touched it for several months). A lot of the credit goes to Raphael Graf, who wrote the original version though.
Matthew Crumley
A: 

See this tutorial for how to build arbitrary parsers/compilers. (Basically it automates the construction of recursive descent parsers from grammars, meaning you can change your expression syntax easily). The whole tutorial is done in JavaScript, so it applies directly to you.

http://www.bayfronttechnologies.com/mc_tutorial.html

Ira Baxter
A: 

Narcissus implements a proper JS parser in JS: http://mxr.mozilla.org/mozilla/source/js/narcissus/jsparse.js. Written by Brendan Eich (the JS creator) too!

Nickolay