I want to parser a simple matlab-like for-loop, using ANTLR.
The loop is like :
for i=1:8
y(i) = a(i) + i;
end
I want to parse the loop and parse 8 times the y(i) = a(i) + i
statement, in order to do some actions on each statement.
My rule is as follows (actions are described in C#) :
forloop
@init
{
string c = "";
int mark = input.Mark();
}
@after
{
if (constants[c] < $i2.value) {
SetConst(c, constants[c] + 1);
input.Rewind(mark);
}
}
: 'for' IDENT '=' i1=constant ':' i2=constant NEWLINE
{
c = $IDENT.text;
if (!IsConst(c)) {
AddConst(c, $i1.value);
}
}
statements?
'end'
;
Actually, when ANTLR parses the statements
rule, it triggers some actions. So, here, I tell ANTLR that i is a constant which value is 1
for starters, and then I want to reiterate the statements
parsing, while incrementing my i
constant.
To reiterate, I use input.Mark() and input.Rewind(), but it does not work as I expect, and some error is raised by ANTLR, telling me that some "NEWLINE" tokens are not present at the 'for' keyword.
How can I handle loop-parsing if I want to trigger some actions until the loop is over ?