Skip to content

Commit

Permalink
Make <end> UPARSE combinator invisible
Browse files Browse the repository at this point in the history
Most cases using `<end>` or `to <end>` or `thru <end>` have no interest in
the value of the tail of the series.

If the tail is desired, that can be done with `<end> <here>`

Making `<end>` invisible means you can avoid writing `elide <end>` in the
much more common cases.
  • Loading branch information
hostilefork committed Feb 21, 2024
1 parent 3aaf5fe commit e0056b2
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 63 deletions.
6 changes: 3 additions & 3 deletions src/mezz/uparse.r
Original file line number Diff line number Diff line change
Expand Up @@ -760,12 +760,12 @@ default-combinators: make map! reduce [

<end> combinator [
{Only match if the input is at the end}
return: "End position of the parse input"
[any-series?]
return: "Invisible"
[nihil?]
][
if tail? input [
remainder: input
return input
return nihil
]
return raise "PARSE position not at <end>"
]
Expand Down
2 changes: 1 addition & 1 deletion tests/parse/parse-bitset.test.reb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
count-up n 512 [
if n = 1 [continue]

if not try parse (append copy "" codepoint-to-char n - 1) [
if not ok? parse (append copy "" codepoint-to-char n - 1) [
c: any-char <end>
][
fail "Parse didn't work"
Expand Down
2 changes: 1 addition & 1 deletion tests/parse/parse-blank.test.reb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

(raised? parse [x] ['x blank])
('_ = parse [x _] ['x _])
([] == parse [x] [try blank 'x <end>])
('x == parse [x] [try blank 'x <end>])

(raised? parse [] [blank blank blank])

Expand Down
12 changes: 7 additions & 5 deletions tests/parse/parse-collect.test.reb
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,8 @@

; Nested collect
[
(did all [
[] == parse [1 2 3 4] [
(all [
[1 4] == parse [1 2 3 4] [
a: collect [
keep integer!
b: collect [keep spread across repeat 2 integer!]
Expand Down Expand Up @@ -351,8 +351,8 @@ https://github.com/metaeducation/ren-c/issues/935
x = ["aaa" "b"]
])

(did all [
"" == parse "aaabbb" [x: collect [keep across to "b"] to <end>]
(all [
["aaa"] == parse "aaabbb" [x: collect [keep across to "b"] to <end>]
x = ["aaa"]
])

Expand Down Expand Up @@ -428,8 +428,10 @@ https://github.com/metaeducation/ren-c/issues/935
; red>> test
; == ""
;
; But in Ren-C, <end> is invisible. So KEEP fails with no argument.
;
[https://github.com/red/red/issues/2561
([""] = parse "" [collect [keep to <end>]])
~???~ !! (parse "" [collect [keep to <end>]])
([""] = parse "" [collect [keep across to <end>]])
]

Expand Down
2 changes: 1 addition & 1 deletion tests/parse/parse-get-group.test.reb
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
| #"]" :(f x)
]
]
return "" == parse s [try some r <end>]
return ok? parse s [try some r <end>]
]

f "420,]]"
Expand Down
6 changes: 3 additions & 3 deletions tests/parse/parse-into.test.reb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
)

[(
"" == parse "baaabccc" [
"a" == parse "baaabccc" [
subparse [between "b" "b"] [some "a" <end>] to <end>
]
)(
Expand All @@ -78,11 +78,11 @@
)(
raised? parse "baaabccc" [subparse [between "b" "b"] ["a"], to <end>]
)(
"" == parse "baaabccc" [
"c" == parse "baaabccc" [
subparse [between "b" "b"] ["a" to <end>], "c", to <end>
]
)(
"" == parse "aaabccc" [subparse [across to "b"] [some "a"], to <end>]
"a" == parse "aaabccc" [subparse [across to "b"] [some "a"], to <end>]
)]


Expand Down
6 changes: 3 additions & 3 deletions tests/parse/parse-not.test.reb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
'a == parse [a] [not wb 'a]
)
(raised? parse [a a] [not ['a 'a] to <end>])
([] == parse [a a] [not [some 'b] to <end>])
(~not~ == parse [a a] [not [some 'b] to <end>])
]

[
Expand All @@ -49,7 +49,7 @@
#a == parse "a" [not wb #a]
)
(raised? parse "aa" [not [#a #a] to <end>])
("" == parse "aa" [not [some #b] to <end>])
(~not~ == parse "aa" [not [some #b] to <end>])
]

[
Expand All @@ -63,5 +63,5 @@
#{0A} == parse #{0A} [not wb #{0A}]
)
(raised? parse #{0A0A} [not [#{0A} #{0A}] to <end>])
(#{} == parse #{0A0A} [not [some #{0B}] to <end>])
(~not~ == parse #{0A0A} [not [some #{0B}] to <end>])
]
4 changes: 2 additions & 2 deletions tests/parse/parse-some.test.reb
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@
]

[https://github.com/red/red/issues/3108
([] == parse [1] [some further [to <end>]])
([] == parse [1] [some further [to [<end>]]])
(void? parse [1] [some further [to <end>]])
(void? parse [1] [some further [to [<end>]]])
]

(#c == parse "baaac" [<any> some [#a] #c])
Expand Down
23 changes: 13 additions & 10 deletions tests/parse/parse-tag-end.test.reb
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,24 @@
; In UPARSE, the <end> tag should be used. This frees up the END
; word for variables (like start and end for ranges being copied, for
; example, or begin and end, etc.)
;
; It vanishes, because this is the overwhelmingly most useful behavior.
; e.g. recognizing a single element in a block can be done with [<any> <end>].


; BLOCK! end tests from %parse-test.red
[
(
block: [a]
(tail block) = parse block ['a <end>]
'a = parse block ['a <end>]
)
(raised? parse [a b] ['a <end>])
([] == parse [a] [<any> <end>])
('a == parse [a] [<any> <end>])
(raised? parse [a b] [<any> <end>])
([] == parse [] [<end>])
(void? parse [] [<end>])
(
be6: ~
did all [
all [
1 == parse [] [<end> (be6: 1)]
be6 = 1
]
Expand All @@ -28,12 +31,12 @@
[
(
text: "a"
(tail text) == parse text [#a <end>]
#a == parse text [#a <end>]
)
(raised? parse "ab" [#a <end>])
("" == parse "a" [<any> <end>])
(#a == parse "a" [<any> <end>])
(raised? parse "ab" [<any> <end>])
("" == parse "" [<end>])
(void? parse "" [<end>])
(
be6: ~
did all [
Expand All @@ -47,12 +50,12 @@
[
(
binary: #{0A}
(tail binary) == parse #{0A} [#{0A} <end>]
#{0A} == parse #{0A} [#{0A} <end>]
)
(raised? parse #{0A0B} [#{0A} <end>])
(#{} == parse #{0A} [<any> <end>])
(10 == parse #{0A} [<any> <end>])
(raised? parse #{0A0B} [<any> <end>])
(#{} == parse #{} [<end>])
(void? parse #{} [<end>])
(
be6: ~
did all [
Expand Down
10 changes: 6 additions & 4 deletions tests/parse/parse-text.test.reb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
("a" == parse "a" ["a"])
("ab" == parse "ab" ["ab"])
("abc" == parse "abc" ["abc"])
("" == parse "abc" ["abc" <end>])
("abc" == parse "abc" ["abc" <end>])

; Ren-C does not mandate that rules make progress, so matching empty strings
; works, as it does in Red.
Expand Down Expand Up @@ -92,7 +92,7 @@
(
test: to-binary {The C😺T Test}
did all [
#{} == parse test [to {c😺t} x: across to space to <end>]
#{43F09F98BA54} == parse test [to {c😺t} x: across to space to <end>]
x = #{43F09F98BA54}
"C😺T" = to-text x
]
Expand Down Expand Up @@ -153,14 +153,16 @@
(
res: ~
did all [
"" == parse str [thru "ipsum" <any> res: across to #" " to <end>]
"dolor" == parse str [
thru "ipsum" <any> res: across to #" " to <end>
]
res = "dolor"
]
)
(
res: ~
did all [
"" == parse str [thru #p res: <here> to <end>]
"sum dolor sit amet." == parse str [thru #p res: <here> to <end>]
9 = index? res
]
)
Expand Down
28 changes: 14 additions & 14 deletions tests/parse/parse-thru.test.reb
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@

; Edge case of matching END with THRU
;
("" == parse "" [thru ["a" | <end>]])
([] == parse [] [thru ["a" | <end>]])
(void? parse "" [thru ["a" | <end>]])
(void? parse [] [thru ["a" | <end>]])

[
([] == parse [] [thru <end>])
(void? parse [] [thru <end>])
('a == parse [a] [thru 'a])
([] == parse [a] [thru 'a <end>])
('a == parse [a] [thru 'a <end>])
(raised? parse [a] [thru 'a <any>])
('b == parse [a b] [thru 'b])
('a == parse [a] [thru ['a]])
([] == parse [a] [thru ['a] <end>])
('a == parse [a] [thru ['a] <end>])
(raised? parse [a] [thru ['a] <any>])
('b == parse [a b] [thru ['b]])
]
Expand Down Expand Up @@ -102,14 +102,14 @@
)

[
("" == parse "" [thru <end>])
(void? parse "" [thru <end>])
(#a == parse "a" [thru #a])
("" == parse "a" [thru #a <end>])
(#a == parse "a" [thru #a <end>])
(raised? parse "a" [thru #a <any>])
(#b == parse "ab" [thru #a <any>])
(#a == parse "aaba" [<any> thru #a repeat 2 <any>])
(#a == parse "a" [thru [#a]])
("" == parse "a" [thru [#a] <end>])
(#a == parse "a" [thru [#a] <end>])
(raised? parse "a" [thru [#a] <any>])
(#b == parse "ab" [thru [#a] <any>])
(#a == parse "aaba" [<any> thru [#a] repeat 2 <any>])
Expand All @@ -133,14 +133,14 @@
wa: [#{0A}]
true
)
(#{} == parse #{} [thru <end>])
(void? parse #{} [thru <end>])
(#{0A} == parse #{0A} [thru #{0A}])
(#{} == parse #{0A} [thru #{0A} <end>])
(#{0A} == parse #{0A} [thru #{0A} <end>])
(raised? parse #{0A} [thru #{0A} <any>])
(11 == parse #{0A0B} [thru #{0A} <any>])
(10 == parse #{0A0A0B0A} [<any> thru #{0A} repeat 2 <any>])
(#{0A} == parse #{0A} [thru [#{0A}]])
(#{} == parse #{0A} [thru [#{0A}] <end>])
(#{0A} == parse #{0A} [thru [#{0A}] <end>])
(raised? parse #{0A} [thru [#{0A}] <any>])
(11 == parse #{0A0B} [thru [#{0A}] <any>])
(10 == parse #{0A0A0B0A} [<any> thru [#{0A}] repeat 2 <any>])
Expand All @@ -157,19 +157,19 @@
(
res: ~
did all [
#{} == parse bin [thru #{CAFE} <any> res: across to # to <end>]
#{BABE} == parse bin [thru #{CAFE} <any> res: across to # to <end>]
res = #{BABE}
]
)
(
res: ~
did all [
#{} == parse bin [thru #{BABE} res: <here> to <end>]
#{00DEADBEEF00} == parse bin [thru #{BABE} res: <here> to <end>]
9 = index? res
]
)
]

[https://github.com/red/red/issues/3427
(%"" == parse/part %234 ["23" thru [<end>]] 3)
("23" == parse/part %234 ["23" thru [<end>]] 3)
]
Loading

0 comments on commit e0056b2

Please sign in to comment.