G
gw7rib
I have a program that needs to do a small amount of relatively simple
parsing. The routines I've written work fine, but the code using them
is a bit long-winded.
I therefore had the idea of creating a class to do parsing. It could
be used as follows:
int a, n, x, y;
Parser par;
par << string;
if (par >> "From" >> ' ' >> x >> ' ' >> "to" >> ' ' >> y) a = 1;
else if (par >> "Number" >> ' ' >> n) a = 2;
else a = 3;
Then if string is "From 3 to 5" this will set a=1, x=3, y=5. If the
string is "Number 2" this will set a=2 and n=2. If string is
"Other" then a=3. For convenience, I'll assume that an input of "From
4 other" is allowed to alter the value of x while returning a=3.
I think I could write a class that would do this. It would need to
keep track of whether the current parsing was succeeding and, if so,
how far through the string it had got. It would need overloaded >>
operators, obviously, some of them taking references. And it would
need a conversion operator, which I think would need to be to void *,
which would not only return whether the current parse had succeeded
but would also reset the flag and counter ready for another attempt.
So my questions are, is this a sensible thing to try to do, and are
there any potential snags that I haven't spotted?
Thanks.
Paul.
parsing. The routines I've written work fine, but the code using them
is a bit long-winded.
I therefore had the idea of creating a class to do parsing. It could
be used as follows:
int a, n, x, y;
Parser par;
par << string;
if (par >> "From" >> ' ' >> x >> ' ' >> "to" >> ' ' >> y) a = 1;
else if (par >> "Number" >> ' ' >> n) a = 2;
else a = 3;
Then if string is "From 3 to 5" this will set a=1, x=3, y=5. If the
string is "Number 2" this will set a=2 and n=2. If string is
"Other" then a=3. For convenience, I'll assume that an input of "From
4 other" is allowed to alter the value of x while returning a=3.
I think I could write a class that would do this. It would need to
keep track of whether the current parsing was succeeding and, if so,
how far through the string it had got. It would need overloaded >>
operators, obviously, some of them taking references. And it would
need a conversion operator, which I think would need to be to void *,
which would not only return whether the current parse had succeeded
but would also reset the flag and counter ready for another attempt.
So my questions are, is this a sensible thing to try to do, and are
there any potential snags that I haven't spotted?
Thanks.
Paul.