File locations in production rules
In a production function (e.g. the rhs of > or |>) is it possible to get the location in the file that's currently being parsed? It would be very useful to be able to extract ideally byte ranges, or at least a character-line position a la the error message for use in downstream error messages. I tried to figure out how to get this information out of the same logic that's used to generate the error messages but it doesn't seem to be present in the production rule logic?
Right, the line and column location information can only be added to a parse error message because the function that constructs that message has access to the whole input string. But the individual parsing functions that are created from the grammar rules only receive the suffix of the input that hasn't been parsed yet, not the whole input. So they can't always know what was parsed before they were called, and can't pass that information to the semantics function on the right side of > or |>.
However, when you pass an input string to a parsing function, it is converted to a SubString, which does keep track of its offset within the original string, even as more substrings are made from it during parsing (via input[start:finish]). So if in your semantics function (RHS of > or |>) you have access to a string derived from the input in this way (which should be all of them, unless you made some fresh strings yourself in other semantics functions for subexpressions), and it's from the position you're interested in extracting, you can get the value of its offset field to extract that position. It will be 0-based instead of 1-based, so if you wanted to use it to index into the original string you would have to add 1. So if a SubString called s were derived from the input, the range of input indices it corresponds to would be (s.offset + 1):(s.offset + lastindex(s)). I think. I haven't tested this idea.