mirror of
https://github.com/janet-lang/janet
synced 2024-12-28 01:10:26 +00:00
Fix bugs in parsing logic
This commit is contained in:
parent
ba2e0489e6
commit
02224d5aa9
@ -1669,7 +1669,7 @@
|
||||
(def line @"")
|
||||
(var line-width 0)
|
||||
(def levels @[0])
|
||||
(var level 0)
|
||||
(var leading 0)
|
||||
(var c nil)
|
||||
|
||||
(def base-indent
|
||||
@ -1704,41 +1704,38 @@
|
||||
(set c (get str (++ pos*))))
|
||||
(set pos pos*))
|
||||
|
||||
(defn update-level []
|
||||
(defn skip-line-indent []
|
||||
(var pos* pos)
|
||||
(set c (get str pos*))
|
||||
(while (and (not= nil c)
|
||||
(not= 10 c)
|
||||
(= 32 c))
|
||||
(set c (get str (++ pos*))))
|
||||
(set level (- pos* pos)))
|
||||
(set leading (- pos* pos))
|
||||
(set pos pos*))
|
||||
|
||||
(defn reset-level []
|
||||
(while (< level (array/peek levels))
|
||||
(array/pop levels))
|
||||
(+= pos level))
|
||||
(defn update-levels []
|
||||
(while (< leading (array/peek levels))
|
||||
(array/pop levels)))
|
||||
|
||||
(defn start-nl? []
|
||||
(= 10 (get str pos)))
|
||||
|
||||
(defn start-fcb? []
|
||||
(and (or (= 0 level)
|
||||
(= (array/peek levels) level))
|
||||
(= 96 (get str (+ level pos)))
|
||||
(= 96 (get str (+ level pos 1)))
|
||||
(= 96 (get str (+ level pos 2)))))
|
||||
(and (= 96 (get str (+ pos)))
|
||||
(= 96 (get str (+ pos 1)))
|
||||
(= 96 (get str (+ pos 2)))))
|
||||
|
||||
(defn end-fcb? []
|
||||
(and (or (= 0 level)
|
||||
(= (array/peek levels) level))
|
||||
(= 96 (get str (+ level pos)))
|
||||
(= 96 (get str (+ level pos 1)))
|
||||
(= 96 (get str (+ level pos 2)))
|
||||
(= 10 (get str (+ level pos 3)))))
|
||||
(and (= 96 (get str (+ pos)))
|
||||
(= 96 (get str (+ pos 1)))
|
||||
(= 96 (get str (+ pos 2)))
|
||||
(= 10 (get str (+ pos 3)))))
|
||||
|
||||
(defn start-icb? []
|
||||
(and (not= level (array/peek levels))
|
||||
(or (= 4 level)
|
||||
(= 4 (- level (array/peek levels))))))
|
||||
(and (not= leading (array/peek levels))
|
||||
(or (= 4 leading)
|
||||
(= 4 (- leading (array/peek levels))))))
|
||||
|
||||
(defn start-ul? []
|
||||
(var pos* pos)
|
||||
@ -1778,11 +1775,8 @@
|
||||
|
||||
(defn push-bullet []
|
||||
(var pos* pos)
|
||||
(buffer/push-string line (buffer/new-filled leading 32))
|
||||
(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
|
||||
(while (and (not= nil c) (not= 32 c))
|
||||
(buffer/push-byte line c)
|
||||
@ -1792,10 +1786,11 @@
|
||||
(buffer/push-byte line c)
|
||||
(set c (get str (++ pos*))))
|
||||
# Record indentation if necessary
|
||||
(def item-level (+ level (- pos* pos)))
|
||||
(when (not= item-level (array/peek levels))
|
||||
(array/push levels item-level))
|
||||
(set line-width item-level)
|
||||
(def item-indent (+ leading (- pos* pos)))
|
||||
(when (not= item-indent (array/peek levels))
|
||||
(array/push levels item-indent))
|
||||
# Update line width
|
||||
(+= line-width item-indent)
|
||||
# Update position
|
||||
(set pos pos*))
|
||||
|
||||
@ -1803,7 +1798,9 @@
|
||||
(def word @"")
|
||||
(var word-len 0)
|
||||
# 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)
|
||||
(++ word-len)
|
||||
(set c (get str (++ pos))))
|
||||
@ -1830,26 +1827,28 @@
|
||||
(++ pos)))
|
||||
|
||||
(defn push-list []
|
||||
(reset-level)
|
||||
# Set up the indentation
|
||||
(def list-indent (+ indent (array/peek levels)))
|
||||
(update-levels)
|
||||
# Indent first line
|
||||
(buffer/push-string line (buffer/new-filled list-indent 32))
|
||||
(set line-width list-indent)
|
||||
(buffer/push-string line (buffer/new-filled indent 32))
|
||||
(set line-width indent)
|
||||
# Add bullet
|
||||
(push-bullet)
|
||||
# Add words
|
||||
(set c (get str pos))
|
||||
(while (and (not= nil c)
|
||||
(not= 10 c)
|
||||
(not (or (start-ul?)
|
||||
(start-ol?))))
|
||||
(not= 10 c))
|
||||
# Skip spaces
|
||||
(while (= 32 c)
|
||||
(set c (get str (++ pos))))
|
||||
# Add word
|
||||
(push-word (+ list-indent (array/peek levels)))
|
||||
(set c (get str (++ pos))))
|
||||
(push-word (+ indent (array/peek levels)))
|
||||
(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
|
||||
(buffer/push-string res line)
|
||||
(buffer/clear line)
|
||||
@ -1858,6 +1857,7 @@
|
||||
(push-nl))
|
||||
|
||||
(defn push-fcb []
|
||||
(update-levels)
|
||||
(push-line)
|
||||
(skip-base-indent)
|
||||
(while (not (end-fcb?))
|
||||
@ -1866,6 +1866,7 @@
|
||||
(push-line))
|
||||
|
||||
(defn push-icb []
|
||||
(buffer/push-string res (buffer/new-filled leading 32))
|
||||
(push-line)
|
||||
(skip-base-indent)
|
||||
(while (not (start-nl?))
|
||||
@ -1874,7 +1875,7 @@
|
||||
(push-nl))
|
||||
|
||||
(defn push-p []
|
||||
(reset-level)
|
||||
(update-levels)
|
||||
# Set up the indentation
|
||||
(def para-indent (+ indent (array/peek levels)))
|
||||
# Indent first line
|
||||
@ -1900,7 +1901,7 @@
|
||||
|
||||
(while (< pos len)
|
||||
(skip-base-indent)
|
||||
(update-level)
|
||||
(skip-line-indent)
|
||||
(cond
|
||||
(start-nl?)
|
||||
(push-nl)
|
||||
|
Loading…
Reference in New Issue
Block a user