How to track position? #19
-
Hi, thanks for the great library. How would you go about keeping track of the current position? Let's say I want to design a language that has some syntax checking that needs to happen after everything has been parsed, I still want to be able to report syntax errors at the right position from the source. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
Hello, thanks for the question and thanks for using Farkle! I am glad to see such appreciation for it. That's a good question. Farkle actually has the ability to allow user code to track the position of tokens. Let's take a look at an example from the quick start guide: let numberStringRegex =
Regex.regexString @"\d+(\.\d+)?(e[+-]?\d+)?"
|> terminal "Number" (T(fun _ x -> float (x.ToString()))) See the first ignored parameter in the last line's lambda? It holds an object of type let numberStringRegex =
Regex.regexString @"\d+(\.\d+)?(e[+-]?\d+)?"
|> terminal "Number" (T(fun ctx x ->
printfn "Number at %O" ctx.StartPosition
float (x.ToString()))) Let me know if everything went well. 🙂 |
Beta Was this translation helpful? Give feedback.
-
@teo-tsirpanis is there an equivalent way to get the start/end positions for a non-terminal? |
Beta Was this translation helpful? Give feedback.
Hello, thanks for the question and thanks for using Farkle! I am glad to see such appreciation for it.
That's a good question. Farkle actually has the ability to allow user code to track the position of tokens. Let's take a look at an example from the quick start guide:
See the first ignored parameter in the last line's lambda? It holds an object of type
ITransformerContext
, which has properties that expose the token's starting and ending position. You can write with that something like the following: