aboutsummaryrefslogtreecommitdiff
path: root/source/lux/data/text.lux
blob: 533308dd02b55091881a15f11a2f510d4e80e232 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
##  Copyright (c) Eduardo Julian. All rights reserved.
##  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 http://mozilla.org/MPL/2.0/.

(;import lux
         (lux (meta macro
                    ast)
              (control (monoid #as m)
                       (eq #as E)
                       (ord #as O)
                       (show #as S)
                       (monad #as M #refer #all))
              (data (number (int #open ("i" Int/Number Int/Ord)))
                    maybe
                    (list #refer (#only foldL list list&)))))

## [Functions]
(def #export (size x)
  (-> Text Int)
  (_jvm_i2l (_jvm_invokevirtual "java.lang.String" "length" []
                                x [])))

(def #export (@ idx x)
  (-> Int Text (Maybe Char))
  (if (and (i< idx (size x))
           (i>= idx 0))
    (#;Some (_jvm_invokevirtual "java.lang.String" "charAt" ["int"]
                                x [(_jvm_l2i idx)]))
    #;None))

(def #export (contains? x y)
  (-> Text Text Bool)
  (_jvm_invokevirtual "java.lang.String" "contains" ["java.lang.CharSequence"]
                      x [y]))

(do-template [<name> <method>]
  [(def #export (<name> x)
     (-> Text Text)
     (_jvm_invokevirtual "java.lang.String" <method> []
                         x []))]
  [lower-case "toLowerCase"]
  [upper-case "toUpperCase"]
  [trim       "trim"]
  )

(def #export (sub' from to x)
  (-> Int Int Text (Maybe Text))
  (if (and (i< from to)
           (i>= from 0)
           (i<= to (size x)))
    (#;Some (_jvm_invokevirtual "java.lang.String" "substring" ["int" "int"]
                                x [(_jvm_l2i from) (_jvm_l2i to)]))
    #;None))

(def #export (sub from x)
  (-> Int Text (Maybe Text))
  (sub' from (size x) x))

(def #export (split at x)
  (-> Int Text (Maybe (, Text Text)))
  (if (and (i< at (size x))
           (i>= at 0))
    (let [pre (_jvm_invokevirtual "java.lang.String" "substring" ["int" "int"]
                                  x [(_jvm_l2i 0) (_jvm_l2i at)])
          post (_jvm_invokevirtual "java.lang.String" "substring" ["int"]
                                   x [(_jvm_l2i at)])]
      (#;Some [pre post]))
    #;None))

(def #export (replace pattern value template)
  (-> Text Text Text Text)
  (_jvm_invokevirtual "java.lang.String" "replace" ["java.lang.CharSequence" "java.lang.CharSequence"]
                      template [pattern value]))

(do-template [<common> <general> <method>]
  [(def #export (<general> pattern from x)
     (-> Text Int Text (Maybe Int))
     (if (and (i< from (size x)) (i>= from 0))
       (case (_jvm_i2l (_jvm_invokevirtual "java.lang.String" <method> ["java.lang.String" "int"]
                                           x [pattern (_jvm_l2i from)]))
         -1  #;None
         idx (#;Some idx))
       #;None))
   
   (def #export (<common> pattern x)
     (-> Text Text (Maybe Int))
     (case (_jvm_i2l (_jvm_invokevirtual "java.lang.String" <method> ["java.lang.String"]
                                         x [pattern]))
       -1  #;None
       idx (#;Some idx)))]

  [index-of      index-of'      "indexOf"]
  [last-index-of last-index-of' "lastIndexOf"]
  )

(def #export (starts-with? prefix x)
  (-> Text Text Bool)
  (case (index-of prefix x)
    (#;Some 0)
    true

    _
    false))

(def #export (ends-with? postfix x)
  (-> Text Text Bool)
  (case (last-index-of postfix x)
    (#;Some n)
    (i= (i+ n (size postfix))
        (size x))

    _
    false))

## [Structures]
(defstruct #export Text/Eq (E;Eq Text)
  (def (= x y)
    (_jvm_invokevirtual "java.lang.Object" "equals" ["java.lang.Object"]
                        x [y])))

(defstruct #export Text/Ord (O;Ord Text)
  (def _eq Text/Eq)

  (do-template [<name> <op>]
    [(def (<name> x y)
       (<op> (_jvm_i2l (_jvm_invokevirtual "java.lang.String" "compareTo" ["java.lang.String"]
                                           x [y]))
             0))]

    [<  i<]
    [<= i<=]
    [>  i>]
    [>= i>=]))

(defstruct #export Text/Show (S;Show Text)
  (def show id))

(defstruct #export Text/Monoid (m;Monoid Text)
  (def unit "")
  (def (++ x y)
    (_jvm_invokevirtual "java.lang.String" "concat" ["java.lang.String"]
                        x [y])))

## [Syntax]
(def (extract-var template)
  (-> Text (Maybe (, Text Text Text)))
  (do Maybe/Monad
    [pre-idx (index-of "#{" template)
     [pre in] (split pre-idx template)
     [_ in] (split 2 in)
     post-idx (index-of "}" in)
     [var post] (split post-idx in)
     [_ post] (split 1 post)]
    (wrap [pre var post])))

(def (unravel-template template)
  (-> Text (List AST))
  (case (extract-var template)
    (#;Some [pre var post])
    (list& (text$ pre) (symbol$ ["" var])
           (unravel-template post))

    #;None
    (list (text$ template))))

(defmacro #export (<> tokens state)
  (case tokens
    (\ (list [_ (#;TextS template)]))
    (let [++ (symbol$ ["" ""])]
      (#;Right state (list (` (;let [(~ ++) (;:: Text/Monoid m;++)]
                                    (;$ (~ ++) (~@ (unravel-template template))))))))

    _
    (#;Left "Wrong syntax for <>")))