#| utils.scm -- parsing utilities Copyright © 2026 Hernán Ibarra Mejia. This file is part of `raw`. `raw` is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3 of the License. `raw` is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with `raw` If not, see . |# (import (chicken format)) (define (indent-char? char) (or (char=? char #\space) (char=? char #\tab))) (define (char->string char) (make-string 1 char)) (define (program-error errmsg) (error (string-append "raw: " errmsg))) (define (error-eof) (program-error "unexpected end of file")) (define (error-unexpected-char char) (program-error (sprintf "unexpected char: ~A" char))) (define (error-invalid-expression obj) (program-error (sprintf "invalid expression: ~A" obj))) (define (error-string-expected obj) (program-error (sprintf "expected string, got: ~A" obj))) (define (error-fatal) (program-error "fatal error")) (define (consume-indent port) (let loop ((result '())) (if (indent-char? (peek-char port)) (loop (cons (read-char port) result)) (reverse result)))) (define (consume-char expected-char port) (let ((char (read-char port))) (cond ((eof-object? char) (error-eof)) ((not (char=? char expected-char)) (error-unexpected-char char))))) (define (consume-char-list char-list port) (for-each (cut consume-char <> port) char-list))