From 2bba3d3de5a8cefe233f274fa2d8a2ba69799760 Mon Sep 17 00:00:00 2001 From: Ekaitz Zarraga Date: Mon, 12 Aug 2024 21:50:50 +0200 Subject: Make multiline steps not finish with spaces --- cook/parse.scm | 23 +++++++++++++++++------ tests/parse.scm | 11 ++++++++--- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/cook/parse.scm b/cook/parse.scm index 57c5178..2071fdd 100644 --- a/cook/parse.scm +++ b/cook/parse.scm @@ -106,6 +106,9 @@ https://github.com/cooklang/spec/blob/main/EBNF.md (define metadata-chars (char-set-difference text-chars (char-set #\:))) (define-grammar cook + (one-nl (,(parse-map + (parse-sre `(: eol (+ ,newline-chars) bol)) + (lambda _ '(#\space))))) (nl ((: ,newline-chars))) (whitespace ((=> x (+ ,whitespace-chars)) (list->string x))) @@ -162,11 +165,19 @@ https://github.com/cooklang/spec/blob/main/EBNF.md (step-line ((: (? ,whitespace) (=> s (+ ,text-item)) - (=> n (? ,nl))) - (append (concatenate s) (if n '(#\space) '())))) - - (step ((: (=> s ,step-line) (=> ns (* ,step-line))) - (make-step (merge-step-strings (concatenate (append (list s) ns)))))) + (? ,nl)) + (append! (concatenate! s)))) + + (step ((=> s (: ,step-line + ,(parse-map + (parse-repeat + (parse-map ;; Add space where newline was + step-line (lambda x + (append '(#\space) + (concatenate x))))) + (lambda x (concatenate! + (concatenate! x)))))) + (make-step (merge-step-strings (concatenate! s))))) (metadata ((: bol ">>" (* ,whitespace) (=> k ,meta-key) @@ -181,7 +192,7 @@ https://github.com/cooklang/spec/blob/main/EBNF.md (parse-seq (parse-repeat nl) (parse-optional parse-end)))))) - (concatenate els)))) + (concatenate! els)))) (define (parse-cook str) (let ((lis (parse-fully recipe str))) diff --git a/tests/parse.scm b/tests/parse.scm index 8a6c87b..e004a1b 100644 --- a/tests/parse.scm +++ b/tests/parse.scm @@ -17,24 +17,29 @@ (test-group "Official" + (test "testBasicDirection" - '(recipe (metadata ()) ((step ("Add a bit of chilli ")))) + '(recipe (metadata ()) ((step ("Add a bit of chilli")))) (cook->list (parse-cook "Add a bit of chilli\n"))) + (test "testComments" '(recipe (metadata ()) ((comment " testing comments"))) (cook->list (parse-cook "-- testing comments\n"))) + (test "testCommentsAfterIngredients" '(recipe (metadata ()) ((step ((ingredient "thyme" (amount 2 "sprigs")) - " " (comment " testing comments") "and some text ")))) + " " (comment " testing comments") "and some text")))) (cook->list (parse-cook "@thyme{2%sprigs} -- testing comments\nand some text\n"))) + (test "testCommentsWithIngredients" '(recipe (metadata ()) ((comment " testing comments") - (step ((ingredient "thyme" (amount 2 "sprigs")) " ")))) + (step ((ingredient "thyme" (amount 2 "sprigs")))))) (cook->list (parse-cook "-- testing comments\n@thyme{2%sprigs}\n"))) + ;(test "testDirectionsWithDegrees" ; '() ; (cook->list (parse-cook "Heat oven up to 200°C\n"))) -- cgit v1.2.3