This project has moved and is read-only. For the latest updates, please go here.

Parse optionally-quoted urls or strings

Apr 5, 2013 at 9:15 AM

I'm prototyping a simple DSL to parse Ms-dos like commands. Two specific requirements I want to handle are parsing URL's cases such as:

openWeb ""
doThis folder
doThis "this folder"

and also mixed situations, such as:

someCommand commandParam url
someCommand "this command" ""
someCommand a ""

Obviously, I'd like the AST not to have the strings, if present (although I can remove them while parsing it).

Any ideas? note of the examples I've seen support this kind of situation.

Thanks in advance,
Apr 5, 2013 at 9:54 AM

I'm not sure that you really need any AST. For one-liners without complicated parsing rules it's usually better to use manual string parsing. Consider this option.
Apr 5, 2013 at 10:09 AM
To be fully honest, I'm not really looking forward to manually write parsing code. Been there before :)

Do you know how I can support the usage of strings parameters I mentioned above? do I have to create regexes for each of the params, which support optional quotes? Is there anything other built in?
Apr 5, 2013 at 10:34 AM
I think you should use something like this:
parameterList.Rule = MakeStarRule(parameterList, parameter);
parameter.Rule = StringLiteral | Identifier;
Where Identifier is IdentifierTerminal. Or you can create Url NonTerminal; maybe there is already something like this comes with Irony, I didn't check.

Within Init() function you can convert your parameters into any required form.
Apr 5, 2013 at 5:40 PM
as for optionally quoted strings - look at sample SQL grammar. Identifier (table or column name) may be optionally quoted in double quotes or square brackets. The trick is to use OutputTerminal property - so all three terminals output the same identifier terminal.