1
0
mirror of https://github.com/janet-lang/janet synced 2024-06-25 06:33:16 +00:00

Fix bugs in parsing logic

This commit is contained in:
Michael Camilleri 2020-11-27 18:28:58 +09:00
parent ba2e0489e6
commit 02224d5aa9
No known key found for this signature in database
GPG Key ID: 7EB218A48DF8B572

View File

@ -1669,7 +1669,7 @@
(def line @"") (def line @"")
(var line-width 0) (var line-width 0)
(def levels @[0]) (def levels @[0])
(var level 0) (var leading 0)
(var c nil) (var c nil)
(def base-indent (def base-indent
@ -1704,41 +1704,38 @@
(set c (get str (++ pos*)))) (set c (get str (++ pos*))))
(set pos pos*)) (set pos pos*))
(defn update-level [] (defn skip-line-indent []
(var pos* pos) (var pos* pos)
(set c (get str pos*)) (set c (get str pos*))
(while (and (not= nil c) (while (and (not= nil c)
(not= 10 c)
(= 32 c)) (= 32 c))
(set c (get str (++ pos*)))) (set c (get str (++ pos*))))
(set level (- pos* pos))) (set leading (- pos* pos))
(set pos pos*))
(defn reset-level [] (defn update-levels []
(while (< level (array/peek levels)) (while (< leading (array/peek levels))
(array/pop levels)) (array/pop levels)))
(+= pos level))
(defn start-nl? [] (defn start-nl? []
(= 10 (get str pos))) (= 10 (get str pos)))
(defn start-fcb? [] (defn start-fcb? []
(and (or (= 0 level) (and (= 96 (get str (+ pos)))
(= (array/peek levels) level)) (= 96 (get str (+ pos 1)))
(= 96 (get str (+ level pos))) (= 96 (get str (+ pos 2)))))
(= 96 (get str (+ level pos 1)))
(= 96 (get str (+ level pos 2)))))
(defn end-fcb? [] (defn end-fcb? []
(and (or (= 0 level) (and (= 96 (get str (+ pos)))
(= (array/peek levels) level)) (= 96 (get str (+ pos 1)))
(= 96 (get str (+ level pos))) (= 96 (get str (+ pos 2)))
(= 96 (get str (+ level pos 1))) (= 10 (get str (+ pos 3)))))
(= 96 (get str (+ level pos 2)))
(= 10 (get str (+ level pos 3)))))
(defn start-icb? [] (defn start-icb? []
(and (not= level (array/peek levels)) (and (not= leading (array/peek levels))
(or (= 4 level) (or (= 4 leading)
(= 4 (- level (array/peek levels)))))) (= 4 (- leading (array/peek levels))))))
(defn start-ul? [] (defn start-ul? []
(var pos* pos) (var pos* pos)
@ -1778,11 +1775,8 @@
(defn push-bullet [] (defn push-bullet []
(var pos* pos) (var pos* pos)
(buffer/push-string line (buffer/new-filled leading 32))
(set c (get str pos*)) (set c (get str pos*))
# Add leading space
(while (and (not= nil c) (= 32 c))
(buffer/push-byte line c)
(set c (get str (++ pos*))))
# Add bullet # Add bullet
(while (and (not= nil c) (not= 32 c)) (while (and (not= nil c) (not= 32 c))
(buffer/push-byte line c) (buffer/push-byte line c)
@ -1792,10 +1786,11 @@
(buffer/push-byte line c) (buffer/push-byte line c)
(set c (get str (++ pos*)))) (set c (get str (++ pos*))))
# Record indentation if necessary # Record indentation if necessary
(def item-level (+ level (- pos* pos))) (def item-indent (+ leading (- pos* pos)))
(when (not= item-level (array/peek levels)) (when (not= item-indent (array/peek levels))
(array/push levels item-level)) (array/push levels item-indent))
(set line-width item-level) # Update line width
(+= line-width item-indent)
# Update position # Update position
(set pos pos*)) (set pos pos*))
@ -1803,7 +1798,9 @@
(def word @"") (def word @"")
(var word-len 0) (var word-len 0)
# Build a word # Build a word
(while (and (not= nil c) (not= 10 c) (not= 32 c)) (while (and (not= nil c)
(not= 10 c)
(not= 32 c))
(buffer/push-byte word c) (buffer/push-byte word c)
(++ word-len) (++ word-len)
(set c (get str (++ pos)))) (set c (get str (++ pos))))
@ -1830,26 +1827,28 @@
(++ pos))) (++ pos)))
(defn push-list [] (defn push-list []
(reset-level) (update-levels)
# Set up the indentation
(def list-indent (+ indent (array/peek levels)))
# Indent first line # Indent first line
(buffer/push-string line (buffer/new-filled list-indent 32)) (buffer/push-string line (buffer/new-filled indent 32))
(set line-width list-indent) (set line-width indent)
# Add bullet # Add bullet
(push-bullet) (push-bullet)
# Add words # Add words
(set c (get str pos)) (set c (get str pos))
(while (and (not= nil c) (while (and (not= nil c)
(not= 10 c) (not= 10 c))
(not (or (start-ul?)
(start-ol?))))
# Skip spaces # Skip spaces
(while (= 32 c) (while (= 32 c)
(set c (get str (++ pos)))) (set c (get str (++ pos))))
# Add word # Add word
(push-word (+ list-indent (array/peek levels))) (push-word (+ indent (array/peek levels)))
(set c (get str (++ pos)))) (def old-c c)
(set c (get str (++ pos)))
# Check if next line is a new item
(when (and (= 10 old-c)
(or (start-ul?)
(start-ol?)))
(set c (get str (-- pos)))))
# Add final line # Add final line
(buffer/push-string res line) (buffer/push-string res line)
(buffer/clear line) (buffer/clear line)
@ -1858,6 +1857,7 @@
(push-nl)) (push-nl))
(defn push-fcb [] (defn push-fcb []
(update-levels)
(push-line) (push-line)
(skip-base-indent) (skip-base-indent)
(while (not (end-fcb?)) (while (not (end-fcb?))
@ -1866,6 +1866,7 @@
(push-line)) (push-line))
(defn push-icb [] (defn push-icb []
(buffer/push-string res (buffer/new-filled leading 32))
(push-line) (push-line)
(skip-base-indent) (skip-base-indent)
(while (not (start-nl?)) (while (not (start-nl?))
@ -1874,7 +1875,7 @@
(push-nl)) (push-nl))
(defn push-p [] (defn push-p []
(reset-level) (update-levels)
# Set up the indentation # Set up the indentation
(def para-indent (+ indent (array/peek levels))) (def para-indent (+ indent (array/peek levels)))
# Indent first line # Indent first line
@ -1900,7 +1901,7 @@
(while (< pos len) (while (< pos len)
(skip-base-indent) (skip-base-indent)
(update-level) (skip-line-indent)
(cond (cond
(start-nl?) (start-nl?)
(push-nl) (push-nl)