0

I'm trying to define a simplistic language using Irony. Some language usecases are

Dear {Name},

It is free text with therein references to fields enclosed in curly braces. a double curly brace escapes a field declaration. I came up with the following spec:

var orText = new FreeTextLiteral("Text", FreeTextOptions.AllowEof | FreeTextOptions.AllowEmpty);
var orFieldName = new FreeTextLiteral("FieldName");

//Nonterminals
var orField = new NonTerminal("Field");
var orValue = new NonTerminal("Value");

//Rules
orField.Rule = "{" + orFieldName + "}";
orValue.Rule = orText | orField;

Root = orValue;

However, the Irony GrammarExplorer only parses a Value which has a Text element. A field is not recognized. What am I missing here?

Ruudjah
  • 807
  • 7
  • 27

1 Answers1

0

I don't know much about Irony, but I think you need to make the following changes:

  1. Set terminator for your FreeTextLiteral. Otherwise, the parser won't know when to end parsing text or field name.
  2. Set Root to a non-terminal that lets you write a sequence of values, not just a single one.
  3. Get rid of AllowEmpty. After you make change #2, this will just mean Irony will think your input is full of empty Texts.

With these changes, the grammar looks like this:

var orText = new FreeTextLiteral("Text", FreeTextOptions.AllowEof, "{");
var orFieldName = new FreeTextLiteral("FieldName", "}");

//Nonterminals
var orField = new NonTerminal("Field");
var orValue = new NonTerminal("Value");

var orFile = new NonTerminal("File");

//Rules
orField.Rule = "{" + orFieldName + "}";
orValue.Rule = orText | orField;

orFile.Rule = MakeStarRule(orFile, orValue);

Root = orFile;

Which parses your input into something like:

        File
         |
 /-------+-------\
 |       |       |
Text   Value    Text
 |       |       |
Dear FieldName   ,
         |
        Name
svick
  • 236,525
  • 50
  • 385
  • 514