diff options
author | Ekaitz Zarraga <ekaitz@elenq.tech> | 2025-05-03 23:34:30 +0200 |
---|---|---|
committer | Ekaitz Zarraga <ekaitz@elenq.tech> | 2025-05-03 23:34:30 +0200 |
commit | 0513b006525c3e78f749121a5d1e7fbebd1a20bc (patch) | |
tree | c675ef454eb37ff221f64827ed18c28f696bc237 /cook/parse.scm | |
parent | b2a013f9d4beab2016f2828ddfd3fd79c4fd653f (diff) |
parse: use char-sets for proper trimming
Diffstat (limited to 'cook/parse.scm')
-rw-r--r-- | cook/parse.scm | 26 |
1 files changed, 17 insertions, 9 deletions
diff --git a/cook/parse.scm b/cook/parse.scm index 498b055..4f9eea8 100644 --- a/cook/parse.scm +++ b/cook/parse.scm @@ -112,6 +112,14 @@ https://github.com/cooklang/spec/blob/main/EBNF.md (define quantity-chars (char-set-difference text-chars (char-set #\} #\%))) (define metadata-chars (char-set-difference text-chars (char-set #\:))) +(define (string-trimmer-from-sets . sets) + (lambda (str) + (string-trim str + (lambda (x) + (char-set-contains? (apply char-set-union sets) x))))) + +(define string-trim-whitespace (string-trimmer-from-sets whitespace-chars)) + (define-grammar cook (one-nl (,(parse-map (parse-sre `(: eol (+ ,newline-chars) bol)) @@ -129,7 +137,7 @@ https://github.com/cooklang/spec/blob/main/EBNF.md (make-comment (list->string c)))) (note-line ((: ">" (=> c (+ ,any-text-chars)) (or eos ,nl)) - (string-trim (list->string c)))) + (string-trim-whitespace (list->string c)))) (note ((=> n (+ ,note-line)) (make-note (string-join n " ")))) @@ -137,11 +145,11 @@ https://github.com/cooklang/spec/blob/main/EBNF.md w)) (unit ((=> u ,(parse-map-substring (parse-repeat+ (parse-char unit-chars)) - string-trim)) + string-trim-whitespace)) u)) (quantity ((=> q ,(parse-map-substring (parse-repeat+ (parse-char quantity-chars)) - string-trim)) + string-trim-whitespace)) (or (string->number q) q))) (meta-key ((=> k (+ ,metadata-chars)) (list->string k))) @@ -198,15 +206,15 @@ https://github.com/cooklang/spec/blob/main/EBNF.md (make-step (merge-step-strings (concatenate! s))))) (section ((: "=" (=> c (+ ,any-text-chars)) (or eos ,nl)) - (make-section (string-trim (list->string c) - (lambda (x) - (or (char=? x #\space) - (char=? x #\=))))))) + (make-section + ((string-trimmer-from-sets whitespace-chars + (string->char-set "=")) + (list->string c))))) (metadata ((: bol ">>" - (=> k ,(parse-map meta-key string-trim)) + (=> k ,(parse-map meta-key string-trim-whitespace)) (* ,whitespace) ":" (* ,whitespace) - (=> v ,(parse-map any-text-item string-trim)) + (=> v ,(parse-map any-text-item string-trim-whitespace)) (* ,whitespace) eol) (make-metadata-line k v))) |