diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2021-06-16 06:14:29 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2021-06-16 06:14:29 -0700 |
commit | 60301ab68ec6a6b7ecb72f83d411c651e029a587 (patch) | |
tree | 9682a11bc65ebb42ef012a13c10b6f61cfff08df | |
parent | 40c1dee7647ddb7d4768a2eadf9915ab29e62f59 (diff) | |
download | txr-60301ab68ec6a6b7ecb72f83d411c651e029a587.tar.gz txr-60301ab68ec6a6b7ecb72f83d411c651e029a587.tar.bz2 txr-60301ab68ec6a6b7ecb72f83d411c651e029a587.zip |
txr-case: upkeep.
* share/txr/stdlib/txr-case.tl (txr-case-impl): If the input
is a stream, then convert it to a lazy list of lines, so that
running multiple functions against it produces sane,
backtracking behavior, like a @(cases) construct.
* tests/011/txr-case.expected: Updated.
* tests/011/txr-case.txr: Now actually contains a test case
for txr-case.
* txr.1: Address an issue reported by Paul A. Patience: the
input to match-fun, txr-if and txr-when may be a stream. That
has always been the case in the implementation. Also document
that when the input is a single string, it is treated as a
list. Document the new requirement in txr-case that a stream
is converted into lazy list of lines.
-rw-r--r-- | share/txr/stdlib/txr-case.tl | 2 | ||||
-rw-r--r-- | tests/011/txr-case.expected | 1 | ||||
-rw-r--r-- | tests/011/txr-case.txr | 12 | ||||
-rw-r--r-- | txr.1 | 38 |
4 files changed, 48 insertions, 5 deletions
diff --git a/share/txr/stdlib/txr-case.tl b/share/txr/stdlib/txr-case.tl index 4f51ea48..244c58b7 100644 --- a/share/txr/stdlib/txr-case.tl +++ b/share/txr/stdlib/txr-case.tl @@ -65,4 +65,6 @@ (defmacro txr-case (input-expr . clauses) (let ((input (gensym "input-"))) ^(let ((,input ,input-expr)) + (if (streamp ,input) + (set ,input (get-lines ,input))) (txr-case-impl ,input ,*clauses)))) diff --git a/tests/011/txr-case.expected b/tests/011/txr-case.expected index 4af473e5..e7582780 100644 --- a/tests/011/txr-case.expected +++ b/tests/011/txr-case.expected @@ -2,3 +2,4 @@ no match for 09-10-20 match: year 2009, month 10, day 20 no match for July-15-2014 no match for foo +match: year 2021, month 06, day 16, foo:bar diff --git a/tests/011/txr-case.txr b/tests/011/txr-case.txr index 1aa80478..aa234ed8 100644 --- a/tests/011/txr-case.txr +++ b/tests/011/txr-case.txr @@ -7,3 +7,15 @@ (txr-if date (y m d) date (put-line `match: year @y, month @m, day @d`) (put-line `no match for @date`)))) +@(define notmatch ()) +blah +@(end) +@(define stuff (year month day a b)) +@(date year month day) +@a @b +@(end) +@(do (txr-case (make-strlist-input-stream '("2021-06-16" + "foo bar")) + + (notmatch () (put-line "notexpected")) + (stuff (y m d a b) (put-line `match: year @y, month @m, day @d, @a:@b`)))) @@ -71237,11 +71237,17 @@ which will be interpreted as pattern variables, and may be bound or unbound. If they are not symbols, then they are treated as expressions (of the pattern language, not \*(TL) and evaluated accordingly. -The +The optional .meta input -argument is a list of strings, which may be lazy. It represents the -lines of the text stream to be processed. If omitted, it defaults to -.codn nil . +argument is an object of one of several types. It may be a stream, +character string or list of strings. If it is a string, then +it is converted to a list containing that string. +A list of strings represents zero or more lines of text to be +processed. If the +.meta input +argument is omitted, then it defaults to +.codn nil , +interpreted as an empty list of lines. The .meta files @@ -71353,7 +71359,11 @@ macro invokes the \*(TX pattern matching function .meta name on some input given by the .meta input -parameter, which is a list of strings, or a single string. +parameter, whose semantics are the same as the +.meta input +argument of the +.code match-fun +funtion. If .meta name @@ -71515,6 +71525,24 @@ otherwise the forms are evaluated in order and the value of the last one specifies the result of .codn txr-case . +The value of the input +.meta input-form +is expected to be one of the same kinds of objects as given by the +requirements for the +.meta input +argument of the +.code match-fun +functions. + +If +.meta input-form +evaluates to a stream object according to the +.code streamp +function, then the stream is converted to a lazy list of lines, +as if by invoking the +.code get-lines +function on that stream; that list then serves as input to the clauses. + .coNP Function @ txr-parse .synb .mets (txr-parse >> [ source >> [ error-stream |