Faster stage 0 parser.

This commit is contained in:
Dominik Pantůček 2023-05-16 21:52:07 +02:00
parent 2ed6dacb40
commit debeb2fd59

View file

@ -41,11 +41,28 @@
;; Pass 0: Removes any comments and removes any leading and trailing ;; Pass 0: Removes any comments and removes any leading and trailing
;; whitespace. ;; whitespace.
(define (parser-preprocess-line line) (define (parser-preprocess-line line)
(irregex-replace (irregex "[ \\t]*$" 'u) (let* ((llen (string-length line))
(irregex-replace (irregex "^[ \\t]*" 'u) (ppos (let ploop ((pidx 0))
(irregex-replace (irregex "#.*$" 'u) line "") (if (or (= pidx llen)
"") (not (let ((ch (string-ref line pidx)))
"")) (or (eq? ch #\space)
(eq? ch #\tab)))))
pidx
(ploop (add1 pidx)))))
(hpos (let hloop ((hidx ppos))
(if (or (= hidx llen)
(eq? (string-ref line hidx) #\#))
hidx
(hloop (add1 hidx)))))
(spos (let sloop ((sidx (sub1 hpos)))
(if (< sidx ppos)
ppos
(let ((ch (string-ref line sidx)))
(if (or (eq? ch #\space)
(eq? ch #\tab))
(sloop (sub1 sidx))
(add1 sidx)))))))
(substring line ppos spos)))
;; Pass 1: Expects line with comments and surrounding whitespace ;; Pass 1: Expects line with comments and surrounding whitespace
;; removed, returns either #f if nothing was parsed, symbol if only ;; removed, returns either #f if nothing was parsed, symbol if only