Skip to content

How to parse a mustache like syntax? #65

Answered by renggli
ashokgelal asked this question in Q&A
Discussion options

You must be logged in to vote

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 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…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by renggli
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #65 on December 08, 2020 19:57.