peg-tree-utils.scm 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. (library (peg-tree-utils)
  2. ;; export all non-terminal symbols for testing and usage
  3. ;; elsewhere in the code
  4. (export get-value-from-peg-tree
  5. matches-up-to-position?
  6. exhausting-match?)
  7. (import
  8. (except (rnrs base) let-values map error)
  9. (only (guile)
  10. lambda* λ
  11. string=?)
  12. (srfi srfi-1)
  13. (ice-9 peg)))
  14. (define rest cdr)
  15. (define get-value-from-peg-tree
  16. (λ (peg-tree label label-equal?)
  17. (cond
  18. [(null? peg-tree) #f]
  19. [(pair? (first peg-tree))
  20. (or (get-value-from-peg-tree (first peg-tree) label label-equal?)
  21. (get-value-from-peg-tree (rest peg-tree) label label-equal?))]
  22. [(label-equal? (first peg-tree) label)
  23. ;;(cadadr peg-tree)
  24. peg-tree]
  25. [else
  26. (get-value-from-peg-tree (rest peg-tree) label label-equal?)])))
  27. (define matches-up-to-position?
  28. (λ (peg-pattern the-string pos)
  29. (let ([match-result (match-pattern peg-pattern the-string)])
  30. (and (peg-record? match-result)
  31. (>= (peg:end match-result) pos)))))
  32. (define exhausting-match?
  33. (λ (peg-pattern matched-string)
  34. (let ([pos (string-length matched-string)])
  35. (matches-up-to-position? peg-pattern matched-string pos))))