1

Background

I am currently writing a function which needs to accept a command as a string. The string needs to be split into it's finer parts so that the function may then use those parts. Here is an example string:

People.filter(Hometown.eq("Pittsburgh"))

Which, in this simplified example, I would need to resolve to something like:

var objectToTraverse = "People";
var actionToTake = "filter";
var comparer = "Hometown";
var targetValue = "Pittsburgh";

For the example above, the split operation is fairly easy:

var _command = "People.filter(Hometown.eq("Pittsburgh"))";
var command = _command.split(".");

var objectToTraverse = command[0];
var actionToTake = command[1].split("(")[0];
var comparer = command[1].split("(")[1];
var targetValue = command[2].split("(")[1].split(")")[0];

But what if you have a more complex command such as this: People.filter(Hometown.eq("Pittsburgh") && Gender.eq("male") || Age.eq(>= 35)).orderBy(Age,Gender).limitResultSet(10)

The pattern may be different each time.

Question

Using JavaScript, how do I efficiently parse a string-command based on various mixed delimiters and conditions such as this:

People.filter(Hometown.eq("Pittsburgh") && Gender.eq("male") || Age.eq(>= 35)).orderBy(Age,Gender).limitResultSet(10)

Please Note

I am aware of all of the frameworks available already for querying data in JavaScript and, while I appreciate all tips and advice, I would still like an answer to this question regardless of what else is available out there.

4

2 回答 2

1

The only way to sensibly parse this is with (wait for it...) a parser!

See http://javascript.crockford.com/tdop/tdop.html for Douglas Crockford's take on how to write parsers in Javascript.

于 2012-04-17T18:07:22.553 回答
1

Regex, particularly JavaScript's flavor of regex which lacks recursion and balanced groups, is not up to the task of parsing code like this. Your sample code, with its well-formed nested parentheses, is a classic example of a context-free grammar, which far wiser men than I have declared undoable with regex.

What you need is an actual parser. There are full JavaScript parsers available that are, themselves, implemented in JavaScript (JSLint has one in it). Or, if you only want to allow certain types of expressions, you can generate a parser to suit any grammar you choose.

One thing to be aware of: This is definitely doable with a parser (albeit not with regex alone). But it's unlikely to be easy or simple. If this is for a real-world project, unless you have a driving reason to write your own markup/programming language, I suggest you consider using an existing solution. If you're describing expressions rather than imperative code (e.g. LINQ vs. classic C#), XML or JSON could be good ways to do it.

于 2012-04-17T18:21:42.003 回答