From 4862eec212ddc261efa2765f21c759295cc667e0 Mon Sep 17 00:00:00 2001 From: "Mike D. Lowis" Date: Mon, 30 Jul 2012 16:55:01 -0400 Subject: [PATCH] Refactord collector functions and added comments for tests that need to be written --- source/lexer.scm | 6 +++--- source/parse-utils.scm | 19 +++++++++---------- source/parser.scm | 9 ++++----- tests/test_parse_utils.scm | 12 +++++++++++- 4 files changed, 27 insertions(+), 19 deletions(-) diff --git a/source/lexer.scm b/source/lexer.scm index 7c616db..3173b3d 100644 --- a/source/lexer.scm +++ b/source/lexer.scm @@ -83,7 +83,7 @@ (if (and (not (eof-object? (buf-lookahead! in 1))) (char-numeric? (buf-lookahead! in 1))) - (collect-char in dlang/integer? "") + (collect-char in dlang/integer?) (abort "Expected an integer"))) (define (dlang/integer? in) @@ -116,7 +116,7 @@ (define text (string-append (string (char-match in #\")) - (collect-char in dlang/string-char? "") + (collect-char in dlang/string-char?) (string (char-match in #\")))) (token 'string text)) @@ -133,7 +133,7 @@ (token-text (dlang/id in))))) (define (dlang/id in) - (define str(collect-char in dlang/id-char? "")) + (define str(collect-char in dlang/id-char?)) (if (> (string-length str) 0) (token 'id str) (abort "An Id was expected but none found."))) diff --git a/source/parse-utils.scm b/source/parse-utils.scm index f23ff99..23172ad 100644 --- a/source/parse-utils.scm +++ b/source/parse-utils.scm @@ -83,17 +83,16 @@ (buf-release! buf) (not (null? result))) -(define (collect-char in fn str) - (if (fn in) - (collect-char in fn (string-append str (string (buf-consume! in)))) - str)) +(define (collect-char in predfn) + (list->string (collect in predfn buf-consume!))) -(define (consume-all in fn) - (when (fn in) +(define (consume-all in predfn) + (when (predfn in) (buf-consume! in) - (consume-all in fn))) + (consume-all in predfn))) -(define (collect in fn rulefn lst) +(define (collect in fn rulefn) (if (fn in) - (collect in fn rulefn (append lst (list (rulefn in)))) - lst)) + (cons (rulefn in) (collect in fn rulefn)) + '())) + diff --git a/source/parser.scm b/source/parser.scm index 2825222..3c08fd8 100644 --- a/source/parser.scm +++ b/source/parser.scm @@ -27,7 +27,7 @@ ;------------------------------------------------------------------------------ (define (dlang/program in) - (collect in dlang/has-expression? dlang/expression '())) + (collect in dlang/has-expression? dlang/expression)) (define (dlang/has-expression? in) (not (eof-object? (buf-lookahead! in 1)))) @@ -144,7 +144,7 @@ (define tree (syntree 'arglist "" '())) (token-match in 'lpar) (syntree-children-set! tree - (collect in dlang/list-end? dlang/arg-list-item '())) + (collect in dlang/list-end? dlang/arg-list-item)) (token-match in 'rpar) tree) @@ -160,7 +160,7 @@ (define tree (syntree 'args "" '())) (token-match in 'lpar) (syntree-children-set! tree - (collect in dlang/list-end? dlang/id-list-item '())) + (collect in dlang/list-end? dlang/id-list-item)) (token-match in 'rpar) tree) @@ -177,6 +177,5 @@ (collect in (lambda (buf) (not (token-matches? buf term))) - dlang/expression - '() ))) + dlang/expression))) diff --git a/tests/test_parse_utils.scm b/tests/test_parse_utils.scm index 7b1229f..74b3736 100644 --- a/tests/test_parse_utils.scm +++ b/tests/test_parse_utils.scm @@ -148,6 +148,9 @@ ; keyword-match ;------------------------------------------------------------------------------ +;(def-test "keyword-match should consume and return if next token matches" +;(def-test "keyword-match should error if next token not an id" +;(def-test "keyword-match should error if next token does not match" ; token->syntree ;------------------------------------------------------------------------------ @@ -158,13 +161,20 @@ ; test-apply ;------------------------------------------------------------------------------ +;(def-test "test-apply should return true if the input matches the applied rule" +;(def-test "test-apply should return false if the applied rule fails" ; collect-char ;------------------------------------------------------------------------------ +;(def-test "should return empty string if predicate function returns false" +;(def-test "should return string containing chars from buffer when predicate returns true" ; consume-all ;------------------------------------------------------------------------------ +;(def-test "should consume nothing if predicate never returns true" +;(def-test "should an item at a time until predicate returns false" ; collect ;------------------------------------------------------------------------------ - +;(def-test "should return empty list if predicate never returns true" +;(def-test "should return list of items for which predicate returned false" -- 2.52.0