Skip to content

Commit

Permalink
Slice abstraction for Text.
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardoejp committed Sep 9, 2024
1 parent bff2a53 commit e651e1b
Show file tree
Hide file tree
Showing 17 changed files with 1,249 additions and 714 deletions.
6 changes: 4 additions & 2 deletions stdlib/source/library/lux/compiler/meta/cli/compiler.lux
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
["[0]" product]
["[0]" text (.only)
["%" \\injection]
["<[1]>" \\projection (.only Projection)]]
["<[1]>" \\projection (.only Projection)]
["[0]" slice
["?[1]" \\projection]]]
[collection
["[0]" list (.use "[1]#[0]" functor)]]]
[math
Expand Down Expand Up @@ -60,6 +62,6 @@
(let [parameter (is (Projection Text)
(<| (<>.after (<text>.this ..start))
(<>.before (<text>.this ..end))
(<text>.slice (<text>.many! (<text>.none_of! ..end)))))]
(?slice.slice (?slice.many (?slice.none_of ..end)))))]
(<>.and (<>.and parameter parameter)
(<>.some parameter))))
10 changes: 6 additions & 4 deletions stdlib/source/library/lux/data/format/json.lux
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
["[0]" product]
["[0]" text (.only \n)
["<[1]>" \\projection (.only Projection)]
["[0]" escape]]
["[0]" escape]
["[0]" slice
["?[1]" \\projection]]]
[collection
["[0]" list (.use "[1]#[0]" monoid mix functor)
["?[1]" \\projection]]
Expand Down Expand Up @@ -409,9 +411,9 @@
[(.text "\" text.double_quote) text.double_quote]
["\\" "\"]]))
(<>.after (<text>.this "\u")
(|> <text>.hexadecimal!
(<text>.exactly! 4)
<text>.slice
(|> ?slice.hexadecimal
(?slice.exactly 4)
?slice.slice
(<>.of natural.hex)
(<>#each text.of_character)))
)))
Expand Down
34 changes: 18 additions & 16 deletions stdlib/source/library/lux/data/format/xml.lux
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
[data
["[0]" product]
["[0]" text (.only \n)
["<[1]>" \\projection (.only Projection Slice)]]
["<[1]>" \\projection (.only Projection)]
["[0]" slice (.only Slice)
["?[1]" \\projection]]]
[collection
["[0]" list (.use "[1]#[0]" functor)]
["[0]" dictionary (.only Dictionary)]]]
Expand Down Expand Up @@ -63,14 +65,14 @@
[hex? (<>.maybe (<text>.this "x"))]
(<| (by ! each (|>> .natural text.of_character))
(<>.of integer.base_10)
<text>.slice
<text>.many!
?slice.slice
?slice.many
(when hex?
{.#None}
<text>.decimal!
?slice.decimal

{.#Some _}
<text>.hexadecimal!)))
?slice.hexadecimal)))
(<>.before (<text>.this ";"))
(<>.after (<text>.this "&#"))))

Expand All @@ -86,12 +88,12 @@

(the xml_identifier
(Projection Text)
(<text>.slice
(all <text>.and!
(<>.either (<text>.one_of! "_")
<text>.alpha!)
(<text>.some! (<>.either (<text>.one_of! "_.-")
<text>.alpha_num!)))))
(?slice.slice
(all ?slice.and
(<>.either (?slice.one_of "_")
?slice.alpha)
(?slice.some (<>.either (?slice.one_of "_.-")
?slice.alpha_num)))))

(the namespaced_name^
(Projection Name)
Expand Down Expand Up @@ -145,8 +147,8 @@

(the comment^
(Projection Slice)
(|> (<text>.not! (<text>.this "--"))
<text>.some!
(|> (?slice.not (<text>.this "--"))
?slice.some
(<text>.enclosed ["<!--" "-->"])
..spaced^))

Expand All @@ -160,15 +162,15 @@
(the cdata^
(Projection Slice)
(let [end (<text>.this "]]>")]
(|> (<text>.some! (<text>.not! end))
(|> (?slice.some (?slice.not end))
(<>.after end)
(<>.after (<text>.this "<![CDATA["))
..spaced^)))

(the text^
(Projection XML)
(|> (..spaced^ (<text>.many xml_character^))
(<>.either (<text>.slice cdata^))
(<>.either (?slice.slice cdata^))
(<>#each (|>> {#Text}))))

(the null^
Expand All @@ -189,7 +191,7 @@
(pure {#Node tag attrs (list)}))
(do <>.monad
[_ (<text>.this ">")
_ (<>.some (<>.either <text>.space!
_ (<>.some (<>.either ?slice.space
..comment^))
_ (..close_tag^ tag)]
(pure {#Node tag attrs (list)})))
Expand Down
134 changes: 134 additions & 0 deletions stdlib/source/library/lux/data/text/slice.lux
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
... This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
... If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.

(.using
[library
[lux (.except text macro)
[type
["[0]" nominal]]
[aspect
["[0]" view (.only View)]]
[math
["[0]" random (.only Random) (.use "[1]#[0]" functor)]
[number
[/64
["n" natural]]]]
[function
[predicate (.only Predicate)]]
[error
["[0]" try (.only Try)]
["[0]" exception (.only Exception)]]
[abstract
[equivalence (.only Equivalence)]]]]
["[0]" //])

(nominal.every .public Slice
(Record
[#space Text
#origin Natural
#size Natural])

(the .public (whole it)
(-> Text
Slice)
(nominal.of [#space it
#origin 0
#size (//.size it)]))

(exception.the .public (cannot_slice [space origin size])
(Exception [Text Natural Natural])
(exception.report
(list ["Origin" (by n.base_10 as origin)]
["Size" (by n.base_10 as size)]
["Space" (//.as_text space)])))

(the .public (partial origin size space)
(-> Natural Natural Text
(Try Slice))
(if (n.<= (//.size space) (n.+ origin size))
{try.#Success (nominal.of [#space space
#origin origin
#size size])}
(exception.except ..cannot_slice [space origin size])))

(the (as_text it)
(-> Slice
Text)
(let [it (nominal.as it)]
(.text_clip# (its #origin it)
(its #size it)
(its #space it))))

(the .public text
(View Slice
Text)
(view.new whole as_text))

(the .public size
(-> Slice
Natural)
(|>> nominal.as
(its #size)))

(the .public empty
Slice
(whole //.empty))

(the .public empty?
(Predicate Slice)
(|>> size
(n.= 0)))

(these (the macro (.in_module# .prelude template#macro))
(the with_template (.in_module# .prelude with_template))
(with_template [,name ,slot]
[(the ,name
(macro (_ ,it)
[(its ,slot (nominal.as ,it))]))]

[[space ..#space]
[origin ..#origin]]))

(the .public (+ origin it)
(-> Slice
(Change Slice))
(if (empty? origin)
it

(empty? it)
origin

(let [same_space!
(same? (..space origin)
(..space it))

contiguity!
(n.= (n.+ (..origin origin)
(..size origin))
(..origin it))]
(and same_space!
contiguity!))
(nominal.of [#space (..space origin)
#origin (..origin origin)
#size (n.+ (..size origin) (..size it))])

... else
(whole (.text (as_text origin) (as_text it)))))

(the .public (random size)
(-> Natural
(Random Slice))
(|> (random.upper_cased size)
(random#each whole)))

(the .public (= reference it)
(-> Slice
(Predicate Slice))
(//.= (as_text reference)
(as_text it)))

(the .public equivalence
(Equivalence Slice)
(implementation
(the = ..=)))
)
6 changes: 4 additions & 2 deletions stdlib/source/library/lux/meta/configuration.lux
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
[data
["[0]" text (.only)
["%" \\injection]
["<[1]>" \\projection (.only Projection)]]
["<[1]>" \\projection (.only Projection)]
["[0]" slice
["?[1]" \\projection]]]
[collection
["[0]" list (.use "[1]#[0]" functor)
["/" property]
Expand Down Expand Up @@ -74,7 +76,7 @@
(let [of_text (is (Projection Text)
(<| (<>.after (<text>.this ..start))
(<>.before (<text>.this ..end))
(<text>.slice (<text>.some! (<text>.none_of! ..end)))))]
(?slice.slice (?slice.some (?slice.none_of ..end)))))]
(<>.some (<>.and of_text of_text))))

(exception.the .public invalid)
Expand Down
Loading

0 comments on commit e651e1b

Please sign in to comment.