0
$\begingroup$

I'm writing a web app that requires a component that let's the user input a piece of text thats automatically formatted based on something the client calls "macros". These macros have a very specific syntax. Examples:

  • "{{ greetings_alternatives}}, my name is {{ names_alternatives|cap }}" -In this example, {{ greetings_alternatives}} will be substituted (randomly) by "Hello", "Hi" or "Greetings" and {{ names_alternatives|cap }} will be subsituted by "John Doe" or "Jane Doe" and then capitalized (because of |cap). We can also have multitple modififers, example: {{ greetings_alternatives|caps|reverse }} this will pick a random name, capitalize it and then reverse the character order of the name.

I've been thinking about using a lexer to split the initial into tokens and then feed these tokens to a parser to generate a parse tree. So basically it will be a "one-pass compiler", but i'm not sure if this is the best way to go. Any suggestions?

Note: There are way more symbols and rules than what i exemplified, hence the thought of using stuff i learned from compiler theory for this.

$\endgroup$
2
  • $\begingroup$ That looks like a lexer could be useful, but since it will have to be a state machine (the "islands" inside the moustaches follow different rules than the surrounding sea of text) you.might find that the pure model from your compiler class doesn't apply. Whether you need a generated parser to build a parse tree depends on the nature of your language, which you have not specified at all. For what you present, it would be overkill, but (all) we know is that it is more complex. Any specific answer would likely be too simplistic or too elaborate, so I'm not going to even try. $\endgroup$
    – rici
    Jan 30, 2022 at 14:30
  • $\begingroup$ Also, I don't think this is a good question for this site. If you have a specific issue to solve in a specific language, ask on SO. $\endgroup$
    – rici
    Jan 30, 2022 at 14:32

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.