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
;; whitespace.
(define (parser-preprocess-line line)
(irregex-replace (irregex "[ \\t]*$" 'u)
(irregex-replace (irregex "^[ \\t]*" 'u)
(irregex-replace (irregex "#.*$" 'u) line "")
"")
""))
(let* ((llen (string-length line))
(ppos (let ploop ((pidx 0))
(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
;; removed, returns either #f if nothing was parsed, symbol if only