Skip to content

Commit

Permalink
Merge pull request #63 from hackberrydev/parse-errors
Browse files Browse the repository at this point in the history
Show last parsed line when plan can't be parsed
  • Loading branch information
strika authored Mar 11, 2022
2 parents 1f31431 + cfadc8b commit b138619
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 9 deletions.
13 changes: 7 additions & 6 deletions src/alas.janet
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,17 @@
(if error
(print error)
(let [plan-string (load-file-result :text)
parse-result (plan_parser/parse plan-string)]
(if parse-result
(let [serialize-empty-inbox (truthy? (string/find "## Inbox" plan-string))
plan (first parse-result)
parse-result (plan_parser/parse plan-string)
parse-error (parse-result :error)
plan (parse-result :plan)]
(if parse-error
(print parse-error)
(let [serialize-empty-inbox (plan_parser/serialize-empty-inbox? plan-string)
new-plan (run-commands plan file-path arguments)
new-plan-string (plan_serializer/serialize
new-plan
{:serialize-empty-inbox serialize-empty-inbox})]
(file_repository/save new-plan-string file-path))
(print "Plan could not be parsed.")))))
(file_repository/save new-plan-string file-path))))))

(defn- run-with-arguments [arguments]
(def file-path (arguments :default))
Expand Down
24 changes: 23 additions & 1 deletion src/plan_parser.janet
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
(import ./day)
(import ./event)
(import ./plan)
(import ./plan_serializer)

(def plan-grammar
~{:main (replace (* (constant :title) :title (? (* (constant :inbox) :inbox)) (constant :days) :days) ,plan/build-plan)
Expand All @@ -30,12 +31,33 @@
:checkbox-pending (* "[ ]" (constant false))
:task-body (replace (capture (some (if-not (+ :day-title :task-begin) 1))) ,string/trim)})

(defn- lines-count [plan-string &opt options]
(default options {:ignore-whitespace true})
(def filter-function (if (options :ignore-whitespace)
(fn [line] (not= (string/trim line) ""))
(fn [_line] true)))
(length (filter filter-function (string/split "\n" plan-string))))

## —————————————————————————————————————————————————————————————————————————————
## Public Interface

(defn serialize-empty-inbox? [plan-string]
(truthy? (string/find "## Inbox" plan-string)))

(defn parse
```
Parses plan string and returns plan as entities.
```
[plan-string]
(peg/match plan-grammar plan-string))
(def serialize-empty-inbox (serialize-empty-inbox? plan-string))
(def parse-result (peg/match plan-grammar plan-string))
(if parse-result
(let [plan (first parse-result)
parsed-plan-string (plan_serializer/serialize
plan
{:serialize-empty-inbox serialize-empty-inbox})]
(if (= (lines-count parsed-plan-string) (lines-count plan-string))
{:plan plan}
{:error (string "Plan can not be parsed: last parsed line is line "
(lines-count parsed-plan-string {:ignore-whitespace false}))}))
{:error "Plan can not be parsed"}))
58 changes: 56 additions & 2 deletions test/plan_parser_test.janet
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
- [x] #work - Review open pull requests
- [x] #work - Fix the flaky test
```)
(def plan (first (parse plan-string)))
(def plan ((parse plan-string) :plan))
(def inbox (plan :inbox))
(def day-1 ((plan :days) 0))
(def day-2 ((plan :days) 1))
Expand Down Expand Up @@ -57,12 +57,66 @@
- [x] #work - Review open pull requests
- [x] #work - Fix the flaky test
```)
(def plan (first (parse plan-string)))
(def plan ((parse plan-string) :plan))
(def day-1 ((plan :days) 0))
(def day-2 ((plan :days) 1))
(is (= "Main TODO" (plan :title)))
(is (empty? (plan :inbox)))
(is (= (d/date 2020 8 1) (day-1 :date)))
(is (= (d/date 2020 7 31) (day-2 :date))))

(deftest parse-plan-with-empty-inbox
(def plan-string
```
# Main TODO
## Inbox
## 2020-08-01, Saturday
- [ ] Develop photos
- [x] Pay bills
## 2020-07-31, Friday
- Talked to Mike & Molly
- [x] #work - Review open pull requests
- [x] #work - Fix the flaky test
```)
(def plan ((parse plan-string) :plan))
(def day-1 ((plan :days) 0))
(def day-2 ((plan :days) 1))
(is (= "Main TODO" (plan :title)))
(is (empty? (plan :inbox)))
(is (= (d/date 2020 8 1) (day-1 :date)))
(is (= (d/date 2020 7 31) (day-2 :date))))

(deftest parse-when-plan-can-not-be-parsed
(def plan-string
```
## Main TODO
- [ ] Pay bills
- [O] Talk to Mike
```)
(def parse-result (parse plan-string))
(is (parse-result :error))
(is (= "Plan can not be parsed" (parse-result :error))))

(deftest parse-when-plan-can-partially-be-parsed
(def plan-string
```
# Main TODO
## 2020-01-31, Friday
## 2020-01-30, Thursday
## Tomorrow
```)
(def parse-result (parse plan-string))
(is (parse-result :error))
(is (= "Plan can not be parsed: last parsed line is line 6"
(parse-result :error))))

(run-tests!)

0 comments on commit b138619

Please sign in to comment.