1

I am looking for a way to parse a string (key-value pair) that has null value. I am using Irony nuget in c#.

Example string 1: key = "value"

Example string 2: key = null

I am able to parse Example string 1 using the below way:

var stringLiteral = new StringLiteral("String", "\"");
var filterRule = new BnfExpression();
filterRule |= ((new NonTerminal("Key") { Rule = "Key" }) + (new NonTerminal("ComparisonOp")) + stringLiteral);

I am not quite sure with Example string 2. Is there actually a way to parse nullable values in Irony?

Deepak
  • 2,660
  • 2
  • 8
  • 23
  • 1
    you could check for string.isnullorempty then change it to empty? – Leandro Bardelli Apr 10 '23 at 19:50
  • 1
    Yes but this is just an example string. The actual string comes from user fields and I can’t traverse and check for null value and replace it with empty string as the string can be way more complex. I am just looking for a way of doing this similar to StringLiteral – Deepak Apr 10 '23 at 20:08

1 Answers1

1

I guess I found an answer to this. Posting here so someone with the same requirement can get it.

var nullLiteral = new RegexBasedTerminal("Nullable", "(NULL)");
var filterRule = new BnfExpression();
filterRule |= ((new NonTerminal("Key") { Rule = "Key" }) + (new NonTerminal("ComparisonOp")) + nullLiteral );

This regular expression gets the null match and then you can parse without errors.

Deepak
  • 2,660
  • 2
  • 8
  • 23