Skip to content

Commit e651e1b

Browse files
committed
Slice abstraction for Text.
1 parent bff2a53 commit e651e1b

File tree

17 files changed

+1249
-714
lines changed

17 files changed

+1249
-714
lines changed

stdlib/source/library/lux/compiler/meta/cli/compiler.lux

+4-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
["[0]" product]
1313
["[0]" text (.only)
1414
["%" \\injection]
15-
["<[1]>" \\projection (.only Projection)]]
15+
["<[1]>" \\projection (.only Projection)]
16+
["[0]" slice
17+
["?[1]" \\projection]]]
1618
[collection
1719
["[0]" list (.use "[1]#[0]" functor)]]]
1820
[math
@@ -60,6 +62,6 @@
6062
(let [parameter (is (Projection Text)
6163
(<| (<>.after (<text>.this ..start))
6264
(<>.before (<text>.this ..end))
63-
(<text>.slice (<text>.many! (<text>.none_of! ..end)))))]
65+
(?slice.slice (?slice.many (?slice.none_of ..end)))))]
6466
(<>.and (<>.and parameter parameter)
6567
(<>.some parameter))))

stdlib/source/library/lux/data/format/json.lux

+6-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
["[0]" product]
2525
["[0]" text (.only \n)
2626
["<[1]>" \\projection (.only Projection)]
27-
["[0]" escape]]
27+
["[0]" escape]
28+
["[0]" slice
29+
["?[1]" \\projection]]]
2830
[collection
2931
["[0]" list (.use "[1]#[0]" monoid mix functor)
3032
["?[1]" \\projection]]
@@ -409,9 +411,9 @@
409411
[(.text "\" text.double_quote) text.double_quote]
410412
["\\" "\"]]))
411413
(<>.after (<text>.this "\u")
412-
(|> <text>.hexadecimal!
413-
(<text>.exactly! 4)
414-
<text>.slice
414+
(|> ?slice.hexadecimal
415+
(?slice.exactly 4)
416+
?slice.slice
415417
(<>.of natural.hex)
416418
(<>#each text.of_character)))
417419
)))

stdlib/source/library/lux/data/format/xml.lux

+18-16
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
[data
1313
["[0]" product]
1414
["[0]" text (.only \n)
15-
["<[1]>" \\projection (.only Projection Slice)]]
15+
["<[1]>" \\projection (.only Projection)]
16+
["[0]" slice (.only Slice)
17+
["?[1]" \\projection]]]
1618
[collection
1719
["[0]" list (.use "[1]#[0]" functor)]
1820
["[0]" dictionary (.only Dictionary)]]]
@@ -63,14 +65,14 @@
6365
[hex? (<>.maybe (<text>.this "x"))]
6466
(<| (by ! each (|>> .natural text.of_character))
6567
(<>.of integer.base_10)
66-
<text>.slice
67-
<text>.many!
68+
?slice.slice
69+
?slice.many
6870
(when hex?
6971
{.#None}
70-
<text>.decimal!
72+
?slice.decimal
7173

7274
{.#Some _}
73-
<text>.hexadecimal!)))
75+
?slice.hexadecimal)))
7476
(<>.before (<text>.this ";"))
7577
(<>.after (<text>.this "&#"))))
7678

@@ -86,12 +88,12 @@
8688

8789
(the xml_identifier
8890
(Projection Text)
89-
(<text>.slice
90-
(all <text>.and!
91-
(<>.either (<text>.one_of! "_")
92-
<text>.alpha!)
93-
(<text>.some! (<>.either (<text>.one_of! "_.-")
94-
<text>.alpha_num!)))))
91+
(?slice.slice
92+
(all ?slice.and
93+
(<>.either (?slice.one_of "_")
94+
?slice.alpha)
95+
(?slice.some (<>.either (?slice.one_of "_.-")
96+
?slice.alpha_num)))))
9597

9698
(the namespaced_name^
9799
(Projection Name)
@@ -145,8 +147,8 @@
145147

146148
(the comment^
147149
(Projection Slice)
148-
(|> (<text>.not! (<text>.this "--"))
149-
<text>.some!
150+
(|> (?slice.not (<text>.this "--"))
151+
?slice.some
150152
(<text>.enclosed ["<!--" "-->"])
151153
..spaced^))
152154

@@ -160,15 +162,15 @@
160162
(the cdata^
161163
(Projection Slice)
162164
(let [end (<text>.this "]]>")]
163-
(|> (<text>.some! (<text>.not! end))
165+
(|> (?slice.some (?slice.not end))
164166
(<>.after end)
165167
(<>.after (<text>.this "<![CDATA["))
166168
..spaced^)))
167169

168170
(the text^
169171
(Projection XML)
170172
(|> (..spaced^ (<text>.many xml_character^))
171-
(<>.either (<text>.slice cdata^))
173+
(<>.either (?slice.slice cdata^))
172174
(<>#each (|>> {#Text}))))
173175

174176
(the null^
@@ -189,7 +191,7 @@
189191
(pure {#Node tag attrs (list)}))
190192
(do <>.monad
191193
[_ (<text>.this ">")
192-
_ (<>.some (<>.either <text>.space!
194+
_ (<>.some (<>.either ?slice.space
193195
..comment^))
194196
_ (..close_tag^ tag)]
195197
(pure {#Node tag attrs (list)})))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
... This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
2+
... If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
3+
4+
(.using
5+
[library
6+
[lux (.except text macro)
7+
[type
8+
["[0]" nominal]]
9+
[aspect
10+
["[0]" view (.only View)]]
11+
[math
12+
["[0]" random (.only Random) (.use "[1]#[0]" functor)]
13+
[number
14+
[/64
15+
["n" natural]]]]
16+
[function
17+
[predicate (.only Predicate)]]
18+
[error
19+
["[0]" try (.only Try)]
20+
["[0]" exception (.only Exception)]]
21+
[abstract
22+
[equivalence (.only Equivalence)]]]]
23+
["[0]" //])
24+
25+
(nominal.every .public Slice
26+
(Record
27+
[#space Text
28+
#origin Natural
29+
#size Natural])
30+
31+
(the .public (whole it)
32+
(-> Text
33+
Slice)
34+
(nominal.of [#space it
35+
#origin 0
36+
#size (//.size it)]))
37+
38+
(exception.the .public (cannot_slice [space origin size])
39+
(Exception [Text Natural Natural])
40+
(exception.report
41+
(list ["Origin" (by n.base_10 as origin)]
42+
["Size" (by n.base_10 as size)]
43+
["Space" (//.as_text space)])))
44+
45+
(the .public (partial origin size space)
46+
(-> Natural Natural Text
47+
(Try Slice))
48+
(if (n.<= (//.size space) (n.+ origin size))
49+
{try.#Success (nominal.of [#space space
50+
#origin origin
51+
#size size])}
52+
(exception.except ..cannot_slice [space origin size])))
53+
54+
(the (as_text it)
55+
(-> Slice
56+
Text)
57+
(let [it (nominal.as it)]
58+
(.text_clip# (its #origin it)
59+
(its #size it)
60+
(its #space it))))
61+
62+
(the .public text
63+
(View Slice
64+
Text)
65+
(view.new whole as_text))
66+
67+
(the .public size
68+
(-> Slice
69+
Natural)
70+
(|>> nominal.as
71+
(its #size)))
72+
73+
(the .public empty
74+
Slice
75+
(whole //.empty))
76+
77+
(the .public empty?
78+
(Predicate Slice)
79+
(|>> size
80+
(n.= 0)))
81+
82+
(these (the macro (.in_module# .prelude template#macro))
83+
(the with_template (.in_module# .prelude with_template))
84+
(with_template [,name ,slot]
85+
[(the ,name
86+
(macro (_ ,it)
87+
[(its ,slot (nominal.as ,it))]))]
88+
89+
[[space ..#space]
90+
[origin ..#origin]]))
91+
92+
(the .public (+ origin it)
93+
(-> Slice
94+
(Change Slice))
95+
(if (empty? origin)
96+
it
97+
98+
(empty? it)
99+
origin
100+
101+
(let [same_space!
102+
(same? (..space origin)
103+
(..space it))
104+
105+
contiguity!
106+
(n.= (n.+ (..origin origin)
107+
(..size origin))
108+
(..origin it))]
109+
(and same_space!
110+
contiguity!))
111+
(nominal.of [#space (..space origin)
112+
#origin (..origin origin)
113+
#size (n.+ (..size origin) (..size it))])
114+
115+
... else
116+
(whole (.text (as_text origin) (as_text it)))))
117+
118+
(the .public (random size)
119+
(-> Natural
120+
(Random Slice))
121+
(|> (random.upper_cased size)
122+
(random#each whole)))
123+
124+
(the .public (= reference it)
125+
(-> Slice
126+
(Predicate Slice))
127+
(//.= (as_text reference)
128+
(as_text it)))
129+
130+
(the .public equivalence
131+
(Equivalence Slice)
132+
(implementation
133+
(the = ..=)))
134+
)

stdlib/source/library/lux/meta/configuration.lux

+4-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
[data
2020
["[0]" text (.only)
2121
["%" \\injection]
22-
["<[1]>" \\projection (.only Projection)]]
22+
["<[1]>" \\projection (.only Projection)]
23+
["[0]" slice
24+
["?[1]" \\projection]]]
2325
[collection
2426
["[0]" list (.use "[1]#[0]" functor)
2527
["/" property]
@@ -74,7 +76,7 @@
7476
(let [of_text (is (Projection Text)
7577
(<| (<>.after (<text>.this ..start))
7678
(<>.before (<text>.this ..end))
77-
(<text>.slice (<text>.some! (<text>.none_of! ..end)))))]
79+
(?slice.slice (?slice.some (?slice.none_of ..end)))))]
7880
(<>.some (<>.and of_text of_text))))
7981

8082
(exception.the .public invalid)

0 commit comments

Comments
 (0)