Well, here is my problem: I have an application that uses a custom Javascript implementation,
but no support for Regular Expressions.
However, I’d like to be able to parse templates nevertheless; preferably using C++.
A template might look like this (ASP-style template):
<% var foo = someFunction("with a string");
var bar = anotherFunction(["with", "an", "array"]); %>
<b>This is html, and this is a variable: <%= bar %></b>
<% if(foo) { %>
<b> foo is 'true'</b>
<% } else { %>
<b> foo is 'false'. terrible. </b>
<% } %>
So the general structure is pretty simple (and I’d assume, relatively parseable).
My question is, Is it possible to parse such a template with a while() loop, going through each character, instead of using regular expressions?
And since my attempts to do that failed horribly, how could it be done?
Thank you!
Have you thought about using a Finite State Machine? Here are some links for your to look at.
In short: FSM consists of finite number of states and transitions between these states. So the process of your parsing might be expressed as follows (pseudocode):
Of course, this is hugely simplified. A real parser would look different. But I hope you got an idea.