-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.elm
More file actions
160 lines (140 loc) · 3.96 KB
/
Main.elm
File metadata and controls
160 lines (140 loc) · 3.96 KB
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
module Main exposing (main)
import Browser
import Html exposing (Html)
import Html.Attributes exposing (value, style)
import Html.Events exposing (onInput)
import Parser exposing (Parser, Problem(..), (|.), (|=), symbol, spaces)
type alias Cron =
{ minutes : Maybe (Some Minutes)
, hours : Maybe (Some Hours)
, days : Maybe (Some Days)
, months : Maybe (Some Months)
, daysOfWeek : Maybe (Some DaysOfWeek)
}
type Some tag = Some Val (List Val)
type Val
= Range (Int, Int)
| RangeWithSteps (Int, Int) Int
| Steps Int
| Single Int
type Minutes = Minutes
type Hours = Hours
type Days = Days
type Months = Months
type DaysOfWeek = DaysOfWeek
cron : Parser Cron
cron =
Parser.succeed Cron
|= ifAny (some (0, 59))
|. spaces
|= ifAny (some (0, 23))
|. spaces
|= ifAny (some (1, 31))
|. spaces
|= ifAny (some (1, 12))
|. spaces
|= ifAny (some (0, 7))
|. Parser.end
ifAny : Parser a -> Parser (Maybe a)
ifAny p =
Parser.oneOf
[ Parser.succeed Just |= p
, Parser.succeed Nothing |. symbol "*"
]
some : (Int, Int) -> Parser (Some a)
some bounds =
Parser.succeed Some
|= val bounds
|= Parser.loop [] (many <| val bounds)
many : Parser a -> List a -> Parser (Parser.Step (List a) (List a))
many p vs =
Parser.oneOf
[ Parser.succeed (\v -> Parser.Loop (v :: vs))
|. symbol ","
|= p
, Parser.succeed ()
|> Parser.map (\() -> Parser.Done (List.reverse vs))
]
val : (Int, Int) -> Parser Val
val (f, t) =
Parser.oneOf
[ steps
, intBetween f t
|> Parser.andThen
(\x -> Parser.oneOf
[ range (x, t)
-- ^ future left bound should be greater
-- than current right bound "x"
, Parser.succeed <| Single x
]
)
]
steps : Parser Val
steps =
Parser.succeed Steps
|. symbol "*/"
|= Parser.int
range : (Int, Int) -> Parser Val
range (right, to) =
Parser.succeed
(\left mbStep ->
case mbStep of
Just s -> RangeWithSteps (right, left) s
Nothing -> Range (right, left)
)
|. symbol "-"
|= intBetween (right + 1) to
-- try to get -^ a non-empty range
|= Parser.oneOf
[ Parser.succeed Just
|. symbol "/"
|= Parser.int
, Parser.succeed Nothing
]
intBetween : Int -> Int -> Parser Int
intBetween from to =
Parser.int
|> Parser.andThen
(\x ->
if x >= from && x <= to
then Parser.succeed x
else Parser.problem "Bad range!"
)
-- GUI
example : String
example = "0,3-10/2,20-45/5,*/4 */3,*/5 * * *"
type alias Model
= { input : String
, result : Result (List Parser.DeadEnd) Cron
}
main : Program () Model String
main =
Browser.sandbox
{ init = fromInput example
, view = view
, update = always << fromInput
}
fromInput : String -> Model
fromInput s = { input = s, result = Parser.run cron s }
view : Model -> Html String
view m =
Html.div []
[ Html.input [ style "width" "99%", onInput identity, value m.input ] []
, case m.result of
Err ps -> Html.ul [] <| List.map (viewDeadEnd m.input) ps
Ok x ->
Html.pre []
[ Html.code []
[ Html.text <| Debug.toString x ]]
]
viewDeadEnd : String -> Parser.DeadEnd -> Html a
viewDeadEnd input {col, problem} =
Html.li []
[ Html.text <| Debug.toString problem
, Html.pre []
[ Html.text <| String.concat
[ String.append input "\n"
, String.append (String.repeat (col - 1) " ") "^"
]
]
]