Skip to content

Commit

Permalink
Fix the parser not failing when an alg container invalid input in the…
Browse files Browse the repository at this point in the history
… middle

It would just accept the start of the algorithm up to that point as if nothing
had happened. A major source of confusion.
  • Loading branch information
mikavilpas committed Jan 19, 2018
1 parent 349ad85 commit 319ee7c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
29 changes: 15 additions & 14 deletions src/squanmate/alg/parser.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,16 @@

(defparser whitespace []
(optional
(p/choice (p/many (p/token whitespace?))
;; This character is used in some algorithms to show that
;; the algorithm flips the middle layer. It's currently
;; ignored by the parser.
(p/many (p/char "*")))))
(p/many (p/token whitespace?))))

(defparser safe-ignore-characters []
;; Characters that are considered an optional part of an algorithm, and thus
;; are completely safe to ignore (they add no vital information to it)
(optional
;; This character is used in some algorithms to show that
;; the algorithm flips the middle layer. It's currently
;; ignored by the parser.
(p/many (p/char "*"))))

(defparser slice []
(p/>> (whitespace)
Expand Down Expand Up @@ -110,20 +115,16 @@
_ (whitespace)]
(p/always rotations)))

(defparser empty-alg []
(let->> [_ (whitespace)
_ (p/eof)]
(p/always [])))

(defparser step []
(p/>> (whitespace)
(p/either (p/attempt (slice))
(p/either (slice)
(rotations))))

(defparser algorithm []
(p/let->> [alg-steps (p/choice (p/attempt (p/many1 (step)))
(empty-alg))
_ (whitespace)]
(p/let->> [alg-steps (p/many (step))
_ (whitespace)
_ (safe-ignore-characters)
_ (p/eof)]
(p/always (flatten alg-steps))))

(defn parse
Expand Down
5 changes: 4 additions & 1 deletion test/squanmate/alg/parser_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,7 @@
(a/parse "M2 U’ M2 U M2"))))

(deftest parser-fails-test []
(is (either/left? (a/parse "not an algorithm"))))
(is (either/left? (a/parse "not an algorithm")))
(is (either/left? (a/parse ",")))
(is (either/left? (a/parse "1,0/-1,0,/-3,0/"))
"parsing a case with an extra comma in the middle should be an error"))

0 comments on commit 319ee7c

Please sign in to comment.