Skip to content

Commit 25e8956

Browse files
authored
Merge pull request #115 from hackberrydev/refactor-parsers
Refactor parsers
2 parents f055219 + eecf707 commit 25e8956

6 files changed

Lines changed: 67 additions & 52 deletions

File tree

src/contact/parser.janet

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@
1212
(? (* (constant :last-contact) :last-contact)))
1313
,contact/build-contact)
1414
:name (* "# " (? (some :d)) (replace (capture (some (if-not "\n" 1))) ,string/trim) "\n")
15-
:detail (+ :category :birthday :other-detail)
16-
:category (* "- Category: " (constant :category) (capture (+ "A" "a" "B" "b" "C" "c" "D" "d")) "\n")
17-
:birthday (* "- Birthday: " (constant :birthday) (capture (* :d :d "-" :d :d)) "\n")
18-
:other-detail (* "- " (some (if-not ":" 1)) ": " (some (if-not "\n" 1)) "\n")
15+
:detail
16+
{:main (+ :category :birthday :other-detail)
17+
:category (* "- Category: " (constant :category) (capture (+ "A" "a" "B" "b" "C" "c" "D" "d")) "\n")
18+
:birthday (* "- Birthday: " (constant :birthday) (capture (* :d :d "-" :d :d)) "\n")
19+
:other-detail (* "- " (some (if-not ":" 1)) ": " (some (if-not "\n" 1)) "\n")}
1920
:last-contact (* "## " (replace :date ,d/parse))
2021
:date (capture (* :d :d :d :d "-" :d :d "-" :d :d))})
2122

src/plan/parser.janet

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,42 @@
1515
,plan/build-plan)
1616
:title (* "# " :text-line "\n")
1717
:text-line (capture (some (if-not "\n" 1)))
18-
:inbox (* :inbox-title (? "\n") :tasks (? "\n"))
19-
:inbox-title (* "## Inbox" (? "\n"))
20-
:days (group (any :day))
21-
:day (replace (* :day-title (? "\n") :events :tasks (? "\n")) ,day/build-day)
22-
:day-title (* "## " :date ", " :week-day (? "\n"))
23-
:date (replace (capture (* :d :d :d :d "-" :d :d "-" :d :d)) ,d/parse)
24-
:week-day (+ "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday" "Sunday")
25-
:events (group (any :event))
26-
:event (replace (* :event-begin :text-line (? "\n")) ,event/build-event)
27-
:event-begin (* "- " (if-not "[" 0))
28-
:tasks (group (any :task))
29-
:task (replace (* (constant :done) :task-begin
30-
" "
31-
(constant :title) (capture (some (if-not (+ "\n" " (missed on") 1)))
32-
(? :task-missed-on-date)
33-
(? "\n")
34-
(constant :body) :task-body
35-
(? "\n"))
36-
,struct)
37-
:task-begin (* "- " :checkbox)
38-
:checkbox (+ :checkbox-done :checkbox-pending)
39-
:checkbox-done (* (+ "[x]" "[X]") (constant true))
40-
:checkbox-pending (* "[ ]" (constant false))
41-
:task-body (group (any :task-body-line))
42-
:task-body-line (* " " :text-line (? "\n"))
43-
:task-missed-on-date (* " (missed on " (constant :missed-on) :date ")")})
18+
:inbox
19+
{:main (* :inbox-title (? "\n") :tasks (? "\n"))
20+
:inbox-title (* "## Inbox" (? "\n"))}
21+
:days
22+
{:main (group (any :day))
23+
:day
24+
{:main (replace (* :day-title (? "\n") :events :tasks (? "\n")) ,day/build-day)
25+
:day-title (* "## " :date ", " :week-day (? "\n"))
26+
:week-day (+ "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday" "Sunday")
27+
:events
28+
{:main (group (any :event))
29+
:event
30+
{:main (replace (* :event-begin :text-line (? "\n")) ,event/build-event)
31+
:event-begin (* "- " (if-not "[" 0))}}}}
32+
:tasks
33+
{:main (group (any :task))
34+
:task
35+
{:main (replace (* (constant :done) :task-begin
36+
" "
37+
(constant :title) (capture (some (if-not (+ "\n" " (missed on") 1)))
38+
(? :task-missed-on-date)
39+
(? "\n")
40+
(constant :body) :task-body
41+
(? "\n"))
42+
,struct)
43+
:task-begin
44+
{:main (* "- " :checkbox)
45+
:checkbox
46+
{:main (+ :checkbox-done :checkbox-pending)
47+
:checkbox-done (* (+ "[x]" "[X]") (constant true))
48+
:checkbox-pending (* "[ ]" (constant false))}}
49+
:task-missed-on-date (* " (missed on " (constant :missed-on) :date ")")
50+
:task-body
51+
{:main (group (any :task-body-line))
52+
:task-body-line (* " " :text-line (? "\n"))}}}
53+
:date (replace (capture (* :d :d :d :d "-" :d :d "-" :d :d)) ,d/parse)})
4454

4555
(defn- lines-count [plan-string &opt options]
4656
(default options {:ignore-whitespace true})

src/schedule_parser.janet

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77
(def schedule-grammar
88
~{:main (* (drop :title) :tasks)
99
:title (* "# " (some (+ :w+ :s+)))
10-
:tasks (group (any :task))
11-
:task (replace (* "- " :task-title :task-schedule (? "\n")) ,task/build-scheduled-task)
12-
:task-title (replace (capture (some (if-not (+ "(" "\n") 1))) ,string/trim)
13-
:task-schedule (* "(" (replace (capture (some (+ :w+ :s+ "-"))) ,string/trim) ")")})
10+
:tasks
11+
{:main (group (any :task))
12+
:task
13+
{:main (replace (* "- " (line) :task-title :task-schedule (? "\n"))
14+
,task/build-scheduled-task)
15+
:task-title (replace (capture (some (if-not (+ "(" "\n") 1))) ,string/trim)
16+
:task-schedule (* "(" (replace (capture (some (+ :w+ :s+ "-"))) ,string/trim) ")")}}})
1417

1518
(defn- task-lines-count
1619
```
@@ -41,5 +44,7 @@
4144
{:tasks tasks}
4245
{:errors [(string "Schedule can not be parsed - last parsed task is \""
4346
((last tasks) :title)
44-
"\"")]}))))
47+
"\""
48+
" on line "
49+
((last tasks) :line))]}))))
4550
{:errors ["Schedule can not be parsed"]})))

src/task.janet

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77
(default body @[])
88
{:title title :body body :done done})
99

10-
(defn build-scheduled-task [title schedule &opt body]
11-
(default body @[])
12-
{:title title :body body :done false :schedule schedule})
10+
(defn build-scheduled-task [line title schedule]
11+
{:line line :title title :done false :schedule schedule})
1312

1413
(defn build-missed-task [title date &opt body]
1514
(default body @[])

test/commands/schedule_tasks_test.janet

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,24 @@
1010
## Test scheduled-for?
1111

1212
(deftest "every Monday"
13-
(def task (task/build-scheduled-task "Weekly meeting" "every Monday"))
13+
(def task (task/build-scheduled-task 42 "Weekly meeting" "every Monday"))
1414
(test (scheduled-for? task (d/date 2022 1 24)) true)
1515
(test (not (scheduled-for? task (d/date 2022 1 25))) true))
1616

1717
(deftest "every Tuesday"
18-
(def task (task/build-scheduled-task "Weekly meeting" "every Tuesday"))
18+
(def task (task/build-scheduled-task 42 "Weekly meeting" "every Tuesday"))
1919
(test (not (scheduled-for? task (d/date 2022 1 24))) true)
2020
(test (scheduled-for? task (d/date 2022 1 25)) true))
2121

2222
(deftest "every month"
23-
(def task (task/build-scheduled-task "Review logs" "every month"))
23+
(def task (task/build-scheduled-task 42 "Review logs" "every month"))
2424
(test (scheduled-for? task (d/date 2022 1 1)) true)
2525
(test (not (scheduled-for? task (d/date 2022 1 2))) true)
2626
(test (scheduled-for? task (d/date 2022 6 1)) true)
2727
(test (not (scheduled-for? task (d/date 2022 6 15))) true))
2828

2929
(deftest "every 3 months"
30-
(def task (task/build-scheduled-task "Review logs" "every 3 months"))
30+
(def task (task/build-scheduled-task 42 "Review logs" "every 3 months"))
3131
(test (scheduled-for? task (d/date 2022 1 1)) true)
3232
(test (not (scheduled-for? task (d/date 2022 2 1))) true)
3333
(test (not (scheduled-for? task (d/date 2022 3 1))) true)
@@ -40,7 +40,7 @@
4040
(test (scheduled-for? task (d/date 2022 10 1)) true))
4141

4242
(deftest "every weekday"
43-
(def task (task/build-scheduled-task "Review logs" "every weekday"))
43+
(def task (task/build-scheduled-task 42 "Review logs" "every weekday"))
4444
(test (scheduled-for? task (d/date 2022 1 24)) true) # Monday
4545
(test (scheduled-for? task (d/date 2022 1 25)) true) # Tuesday
4646
(test (scheduled-for? task (d/date 2022 1 26)) true) # Wednesday
@@ -50,7 +50,7 @@
5050
(test (not (scheduled-for? task (d/date 2022 1 30))) true)) # Sunday
5151

5252
(deftest "every year on some date"
53-
(def task (task/build-scheduled-task "Review logs" "every year on 01-27"))
53+
(def task (task/build-scheduled-task 42 "Review logs" "every year on 01-27"))
5454
(test (scheduled-for? task (d/date 2022 1 27)) true)
5555
(test (scheduled-for? task (d/date 2023 1 27)) true)
5656
(test (scheduled-for? task (d/date 2024 1 27)) true)
@@ -59,15 +59,15 @@
5959
(test (not (scheduled-for? task (d/date 2022 2 1))) true))
6060

6161
(deftest "on some date"
62-
(def task (task/build-scheduled-task "Review logs" "on 2022-01-27"))
62+
(def task (task/build-scheduled-task 42 "Review logs" "on 2022-01-27"))
6363
(test (scheduled-for? task (d/date 2022 1 27)) true)
6464
(test (not (scheduled-for? task (d/date 2022 1 26))) true)
6565
(test (not (scheduled-for? task (d/date 2022 1 28))) true)
6666
(test (not (scheduled-for? task (d/date 2022 2 1))) true)
6767
(test (not (scheduled-for? task (d/date 2023 1 27))) true))
6868

6969
(deftest "every last day"
70-
(def task (task/build-scheduled-task "Review logs" "every last day"))
70+
(def task (task/build-scheduled-task 42 "Review logs" "every last day"))
7171
(test (scheduled-for? task (d/date 2022 1 31)) true)
7272
(test (scheduled-for? task (d/date 2022 2 28)) true)
7373
(test (scheduled-for? task (d/date 2022 3 31)) true)
@@ -84,7 +84,7 @@
8484
(test (not (scheduled-for? task (d/date 2022 1 30))) true))
8585

8686
(deftest "every last Friday"
87-
(def task (task/build-scheduled-task "Review logs" "every last Friday"))
87+
(def task (task/build-scheduled-task 42 "Review logs" "every last Friday"))
8888
(test (scheduled-for? task (d/date 2022 1 28)) true)
8989
(test (scheduled-for? task (d/date 2022 2 25)) true)
9090
(test (scheduled-for? task (d/date 2022 3 25)) true)
@@ -95,7 +95,7 @@
9595
## —————————————————————————————————————————————————————————————————————————————————————————————————
9696
## Test missed?
9797

98-
(def scheduled-task (task/build-scheduled-task "Weekly meeting" "on 2022-08-01"))
98+
(def scheduled-task (task/build-scheduled-task 42 "Weekly meeting" "on 2022-08-01"))
9999

100100
(deftest "returns true when the task is missed"
101101
(def plan (plan/build-plan
@@ -127,8 +127,8 @@
127127
## Test schedule-tasks
128128

129129
(def scheduled-tasks
130-
@[(task/build-scheduled-task "Weekly meeting" "every Monday")
131-
(task/build-scheduled-task "Check logs" "every Wednesday")])
130+
@[(task/build-scheduled-task 42 "Weekly meeting" "every Monday")
131+
(task/build-scheduled-task 42 "Check logs" "every Wednesday")])
132132

133133
(deftest "schedules tasks scheduled on specific weekdays"
134134
(def plan (plan/build-plan
@@ -168,7 +168,7 @@
168168

169169
(deftest "schedules missed monthly tasks"
170170
(def scheduled-tasks
171-
@[(task/build-scheduled-task "Review logs" "every month")])
171+
@[(task/build-scheduled-task 42 "Review logs" "every month")])
172172
(def day-1 (day/build-day (d/date 2022 7 5)))
173173
(def day-2 (day/build-day (d/date 2022 6 15)))
174174
(def plan (plan/build-plan :days @[day-1 day-2]))

test/schedule_parser_test.janet

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
```)
5858
(def result (schedule_parser/parse schedule-string))
5959
(test (first (result :errors))
60-
"Schedule can not be parsed - last parsed task is \"Weekly Meeting\""))
60+
"Schedule can not be parsed - last parsed task is \"Weekly Meeting\" on line 3"))
6161

6262

6363
(deftest "returns an error when the schedule is empty"

0 commit comments

Comments
 (0)