SQL grammars and escaping brackets [, ]

May 5, 2015 at 12:39 AM
I'm rather new at this, so if the question has been asked already, I apologize- if possible, please direct me to a relevant post/answer.

I am working with numerous SQL grammars, and SQL Server, for example, allows brackets to be used/escaped within Identifiers as so:

[Field[Name] // SQL Server just ignores the 2nd open brace
[Field]]Name] // SQL Server doubles the close brace to escape it

Also, I need to work with another SQL grammer that escapes the brackets with backslash:

[Field[Name]
[Field]Name]

It's unclear to me how to get Irony to parse/accept this in Identifier Terminals. Should I be using something like RegexBasedTerminal instead?

Thank you.
Coordinator
May 5, 2015 at 8:06 AM
Look at SQL grammar sample,, it uses TerminalFactory.CreateSqlExtIdentifier helper method to create Id that allows double-quotes or brackets as enclosing elements.
Modify it like this:
public static IdentifierTerminal CreateSqlExtIdentifier(Grammar grammar, string name) {
  var id = new IdentifierTerminal(name);
  StringLiteral term = new StringLiteral(name + "_qouted");
  term.AddStartEnd("[", "]", StringOptions.AllowsDoubledQuote | StringOptions.AllowsAllEscapes);
  term.AddStartEnd("\"", StringOptions.AllowsDoubledQuote | StringOptions.AllowsAllEscapes);
  term.EscapeChar = '\\';
  term.SetOutputTerminal(grammar, id); //term will be added to NonGrammarTerminals automatically 
  return id;
}
with this, it appears SQL grammar allows your variations.
Marked as answer by SCheetah777 on 5/5/2015 at 10:12 AM
May 5, 2015 at 6:12 PM
That works great- thank you for your help and for the parser.