Idorobots / spartan

A small Lisp dialect that serves me as a test bed for programming language features.
MIT License
13 stars 3 forks source link

Captures in PEG parser. #181

Open Idorobots opened 8 months ago

Idorobots commented 8 months ago

Most of the rules just select a part of the parse result, give it a name and push it downward in a more convenient format:

(ValidCommand
  (Spacing (/ "compile" "exec" "repl"))
  (lambda (input result)
    (match (list 'command (cadr (match-match result)))
           (match-start result)
           (match-end result))))

This could have been done by the rule itself, by imposing a more useful format for the match result:

 (ValidCommand
  (Spacing (@ 'command
              (/ "compile" "exec" "repl"))))

With the default full-match under the rule name. Then the parser would merge all these partial results automatically to produce a value with all the captures included.