Skip to content

Commit

Permalink
Handle case-sensitivity in PARSE matching for quoted/text
Browse files Browse the repository at this point in the history
The /CASE option wasn't fully supported in UPARSE.  This gives basic
support so that matching words/paths via quoted will work.
  • Loading branch information
hostilefork committed Feb 22, 2024
1 parent 5af0a46 commit 2deb26e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/mezz/uparse.r
Original file line number Diff line number Diff line change
Expand Up @@ -1153,10 +1153,12 @@ default-combinators: make map! reduce [
return: "The rule series matched against (not input value)"
[text!]
value [text!]
<local> neq?
][
case [
any-array? input [
if input.1 <> value [
neq?: either state.case [:strict-not-equal?] [:not-equal?]
if neq? input.1 value [
return raise "Value at parse position does not match TEXT!"
]
remainder: next input
Expand Down Expand Up @@ -1485,7 +1487,7 @@ default-combinators: make map! reduce [
[nihil? element? splice?] ; !!! splice b.c. called by @(...)
@pending [<opt> block!]
value [quoted? quasi?]
<local> comb
<local> comb neq?
][
; It is legal to say:
;
Expand All @@ -1502,8 +1504,9 @@ default-combinators: make map! reduce [
]

if any-array? input [
neq?: either state.case [:strict-not-equal?] [:not-equal?]
if quoted? value [
if input.1 <> unquote value [
if neq? input.1 unquote value [
return raise [
"Value at parse position wasn't unquote of QUOTED! item"
]
Expand All @@ -1516,7 +1519,7 @@ default-combinators: make map! reduce [
fail "Only antiform matched against array content is splice"
]
for-each item unquasi value [
if input.1 <> item [
if neq? input.1 item [
return raise [
"Value at input position didn't match splice element"
]
Expand Down
15 changes: 15 additions & 0 deletions tests/parse/parse-quoted.test.reb
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,18 @@
]
)
]

; Case sensitivity (consider this also for THE or other quoted construct
[
(strict-equal? 'a (parse [a] ['a]))
(strict-equal? 'A (parse [a] ['A]))

(strict-equal? 'a (parse/case [a] ['a]))
~parse-mismatch~ !! (parse/case [a] ['A])

(strict-equal? 'p/a (parse [p/a] ['p/a]))
(strict-equal? 'p/A (parse [p/a] ['p/A]))

(strict-equal? 'p/a (parse/case [p/a] ['p/a]))
~parse-mismatch~ !! (parse/case [p/a] ['p/A])
]
6 changes: 6 additions & 0 deletions tests/parse/parse-text.test.reb
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@
("TeSt" == parse/case "TeSt" ["TeSt"])
]

; Casing in a block
[
("a" = parse/case ["a"] ["a"])
~parse-mismatch~ !! (parse/case ["a"] ["A"])
]

; String unicode
[
(#é == parse "abcdé" [#a #b #c #d #é])
Expand Down
5 changes: 5 additions & 0 deletions tests/parse/parse-try.test.reb
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,8 @@
(10 == parse #{0A} [try <any>])
(12 == parse #{0A0B0C} [<any> try #{0B} <any>])
]

[https://gitter.im/red/bugs?at=638e27b34cb5585f9666500d (
not ok? parse [1] [try (x: true)]
x = true
)]

0 comments on commit 2deb26e

Please sign in to comment.