I am biting the bullet, mostly as a learning exercise, by attempting to rewrite my tokeniser using DCG instead of standard forms. I must be mad as it took enough pain to get it this far and it’s all unit tested and works!
Anyway, I want to use phrase_from_file but having re-read all the posts about lazy_list_location not being fit for constant use, and having used push-backs a few times, I want to know how you push the initial state onto the stream…
the rules would be something like this I guess, the state being a triple of (Offset,Line,Column) initially (0,1,0)…question is how do I get it in there!!!
rule(Args) -->
skip_ws,
do_something(Args).
do_something(Args), [P] -->
[P0],
...code...
transformed(P0, P).
skip_ws, [P] --> [P0, Chr],
update_pos_by_chr(Chr, P),
memberchk(Chr, ` \r\n\t`).
Am I on the right lines ? What I am trying to convey is my understanding that if a DCG rule is a leaf rule i.e. calls no other rules then it MUST pop off, modify and push back the state. If it calls other rules they must do the same and the responsibility is mine for ensuring the stack is kept in a consistent state. Actually I suddenly feel like I am doing FORTH coding!
This is both depressingly hard work and terrifyingly interesting all at the same time. I will finish it one day but I have to know I did as good a job as I could.
With your help!
Thanks
Sean