[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Parsing beyond sexps
From: |
Mattias Engdegård |
Subject: |
Re: Parsing beyond sexps |
Date: |
Sun, 19 Jan 2020 10:46:18 +0100 |
19 jan. 2020 kl. 09.11 skrev Damien Cassou <address@hidden>:
> This is not enough though as I want comments and line/column positions
> in the yielded values. Is there anything I could use from Emacs core
> that would make my life easier?
Nothing in Emacs core that I know of, but you could have a look at how the
'relint' package does it: keep track of the position before each top-level
form, and then maintain a path of list indices taken as it descends into each
form. Example:
(defun select-weapon (opponent)
"A Matter of Life and Death."
(if (fencing-master-p opponent)
'duelling-pistols
'rapier))
The path to duelling-pistols would be (1 2 4), since it is built up backwards
as a stack during descent: first take element 4 of the top-level form to the
'if'-expression, then element 2 of that form to the (quote duelling-pistols),
and finally element 1 to get past the quote. Maintaining the path during
traversal turns out to be fairly cheap.
The tuple <top-level-position, path> can then be transformed into <line,
column> (or just a buffer position) when required for diagnostics; see
'relint--pos-line-col-from-toplevel-pos-path'. It's somewhat slow but not
time-critical.