Duck util-parser.

This commit is contained in:
Dominik Pantůček 2023-07-05 21:56:11 +02:00
parent b027d19f49
commit 1a2e9ee726
5 changed files with 73 additions and 41 deletions

View file

@ -25,8 +25,12 @@
(declare (unit util-parser))
(module
(import duck)
(module*
util-parser
#:doc ("This module contains common functions for both configuration and
member file parsers. All functions are UTF-8 aware.")
(
parser-preprocess-line
parser-parse-line
@ -39,7 +43,15 @@
;; Pass 0: Removes any comments and removes any leading and trailing
;; whitespace.
(define (parser-preprocess-line line)
(define/doc (parser-preprocess-line line)
("* ```line``` - a string with contents of one source line
If the input ```line``` contains the ```#``` character, the rest of
the line (including this character) is removed.
Any leading and trailing space is removed.
Returns a string representing the preprocessed line.")
(let* ((llen (string-length line))
(ppos (let ploop ((pidx 0))
(if (or (= pidx llen)
@ -67,7 +79,20 @@
;; removed, returns either #f if nothing was parsed, symbol if only
;; one token was there and pair of symbol and string if both key and
;; the value were present.
(define (parser-parse-line line)
(define/doc (parser-parse-line line)
("* ```line``` - preprocessed line (string)
If the ```line``` is empty, returns ```#f```.
If the line contains only one token consisting of non-whitespace
characters before the first whitespace character (there is no
whitespace), returns a symbol created by interning the whole
```line```.
When the ```line``` contains whitespace character(s), it returns a
pair consisting of symbol created by interning the string of
non-whitespace characters before the first whitespace character and
the string with the rest of the line.")
(let* ((llen (string-length line))
(spos (let sloop ((sidx 0))
(if (or (= sidx llen)