1
0
mirror of https://git.savannah.gnu.org/git/emacs.git synced 2024-11-24 07:20:37 +00:00

(with-peg-rules): Fix references to rulesets (bug#74018)

PEG rules get "compiled" to functions with name `peg-rule <RULE>`.
`define-peg-ruleset` instead defines it PEG rules with name
`peg-rule <RULESET> <RULE>`, so that they can be made visible
by `with-peg-rules` simply by adding local aliases from
`peg-rule <RULE>` to `peg-rule <RULESET> <RULE>`.

Apparently when I added `define-peg-ruleset` I somehow failed to
install some of the corresponding code in `with-peg-rules`, so
the aliases were not installed, making it "impossible" to use
rulesets.
[ I still have no idea how this happened and/or where
  the missing code went, so I "recreated" it.  ]

* lisp/progmodes/peg.el (with-peg-rules): Install the aliases
for the rulesets.
(peg--translate-rule-body): Try and preserve
location info when emitting a warning.

* test/lisp/progmodes/peg-tests.el (peg-test-myrules): New ruleset.
(peg-test-ruleset): New test.
This commit is contained in:
Stefan Monnier 2024-10-28 22:14:10 -04:00
parent 70f084db2f
commit cc6a11f483
2 changed files with 27 additions and 2 deletions

View File

@ -412,6 +412,7 @@ sequencing `and' operator of PEG grammars."
(full-rname (format "%s %s" name rname)))
(push `(define-peg-rule ,full-rname . ,(cdr rule)) defs)
(push `(,(peg--rule-id rname) #',(peg--rule-id full-rname)) aliases)))
(require 'cl-lib)
`(cl-flet ,aliases
,@defs
(eval-and-compile (put ',name 'peg--rules ',aliases)))))
@ -432,7 +433,8 @@ rulesets defined previously with `define-peg-ruleset'."
(progn (push rule rulesets) nil)
(cons (car rule) (peg-normalize `(and . ,(cdr rule))))))
rules)))
(ctx (assq :peg-rules macroexpand-all-environment)))
(ctx (assq :peg-rules macroexpand-all-environment))
(body
(macroexpand-all
`(cl-labels
,(mapcar (lambda (rule)
@ -444,6 +446,15 @@ rulesets defined previously with `define-peg-ruleset'."
,@body)
`((:peg-rules ,@(append rules (cdr ctx)))
,@macroexpand-all-environment))))
(if (null rulesets)
body
`(cl-flet ,(mapcan (lambda (ruleset)
(let ((aliases (get ruleset 'peg--rules)))
(unless aliases
(message "Unknown PEG ruleset: %S" ruleset))
(copy-sequence aliases)))
rulesets)
,body))))
;;;;; Old entry points
@ -645,7 +656,7 @@ rulesets defined previously with `define-peg-ruleset'."
(code (peg-translate-exp exp)))
(cond
((null msg) code)
(t (macroexp-warn-and-return msg code)))))
(t (macroexp-warn-and-return msg code 'peg nil exp)))))
;; This is the main translation function.
(defun peg-translate-exp (exp)

View File

@ -180,6 +180,20 @@ resp. succeeded instead of signaling an error."
(should (eobp)))
)
(define-peg-ruleset peg-test-myrules
(sign () (or "+" "-" ""))
(digit () [0-9])
(nat () digit (* digit))
(int () sign digit (* digit))
(float () int "." nat))
(ert-deftest peg-test-ruleset ()
(with-peg-rules
(peg-test-myrules
(complex float "+i" float))
(should (peg-parse-string nat "123" t))
(should (not (peg-parse-string nat "home" t)))))
;;; Examples:
;; peg-ex-recognize-int recognizes integers. An integer begins with a