How to parse a mustache like syntax? #65
-
Sorry for asking what could be a trivial question but how'd one start parsing a mustache like syntax? Something like: <h2>{{ foo }} </h2>
{{ bar }}
<p> text here </p> Where the mustache tags I sort of got it working but my solution feels like a total hack (I'm embarrassed to even show it). I'm sure there must be a better way to parse something like this. I'm using |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Not sure how exactly you want to parse your example, but the following parser separates the text and the mustache parts nicely: final startMustache = string('{{');
final endMustache = string('}}');
final mustache =
startMustache & any().starLazy(endMustache).flatten() & endMustache;
final content = startMustache.neg().plus().flatten();
final document = (mustache | content).star().end();
final input = '''<h2>{{ foo }} </h2>
{{ bar }}
<p> text here </p>''';
final output = document.parse(input);
print(output); Not sure what you use |
Beta Was this translation helpful? Give feedback.
-
Thank you! I was close but not close enough. As far as |
Beta Was this translation helpful? Give feedback.
Not sure how exactly you want to parse your example, but the following parser separates the text and the mustache parts nicely:
Not sure what you use
ref()
for? This is normally only useful when you have a recursive grammar. In this example I don't see recursion, but maybe I misunderstood what you are tr…