feat: contents lexer working?

This commit is contained in:
Anish Lakhwara 2023-12-04 22:30:34 -08:00
parent fcc3fdf969
commit 682f37e49c

View file

@ -6,8 +6,8 @@
(let [(start end contents) (string.find str pattern current)]
(values
{:type typ
:contents contents}
end))))
:text contents}
(+ 1 end)))))
(local contents-grammar
{:match-first "[%*%_%~%[%>%`]"
@ -44,44 +44,50 @@
(let [grammar (or ?grammar contents-grammar)]
(var current 1)
(var lexed [])
(while (> (length contents) current)
(let [found (string.find contents (. grammar :match-first) current)]
(if (not (= nil found))
(let [char (string.sub contents found found)]
(table.insert lexed {:type "raw-text"
:contents (string.sub contents current (- found 1))})
(match char
; This would be better if we had a reverse lookup table
"_" (let [(block new-end) ((. grammar :Italics) contents current)]
(table.insert lexed block)
(set current new-end)) ; Italics
"*" (let [(block new-end) ((. grammar :Bold) contents current)]
(table.insert lexed block)
(set current new-end)) ; Bold
"~" (let [(block new-end) ((. grammar :Strikethrough) contents current)]
(table.insert lexed block)
(set current new-end)) ; Strikethrough
"`" (let [(block new-end) ((. grammar :Monospace) contents current)]
(table.insert lexed block)
(set current new-end)); Monospace
"[" (let [(block new-end) ((. grammar :Link) contents current)]
(table.insert lexed block)
(set current new-end)))) ; Links
(do
(table.insert lexed {:type "raw-text"
:contents contents})
(set current (length contents))))) ; No decoration at all
lexed)))
(while (and (not (= nil current)) (> (length contents) current))
(let [found (string.find contents (. grammar :match-first) current)]
(if (not (= nil found))
(let [char (string.sub contents found found)]
(if (> found 1)
(table.insert lexed {:type "raw-text"
:contents (string.sub contents current (- found 1))}))
(match char
; This would be better if we had a reverse lookup table
"_" (let [(block new-end) ((. grammar :Italics) contents current)]
(table.insert lexed block)
(set current new-end)) ; Italics
"*" (let [(block new-end) ((. grammar :Bold) contents current)]
(table.insert lexed block)
(set current new-end)) ; Bold
"~" (let [(block new-end) ((. grammar :Strikethrough) contents current)]
(table.insert lexed block)
(set current new-end)) ; Strikethrough
"`" (let [(block new-end) ((. grammar :Monospace) contents current)]
(table.insert lexed block)
(set current new-end)); Monospace
"[" (let [(block new-end) ((. grammar :Link) contents current)]
(table.insert lexed block)
(set current new-end)))) ; Links
(do
(table.insert lexed {:type "raw-text"
:text (string.sub contents current (length contents))})
(set current (length contents)))))) ; No decoration at all
lexed))
; ">" (let [(block new-end) ((. grammar :Spoiler) contents current)]
; (table.insert lexed block)
; (set current new-end))))
; (string.match "*This* other thing" "%*(.*)%*")
; (general-contents-fn "%*(.-)%*" "Bold" "*This* other thing" 1)
; ((. contents-grammar :Bold) "*This* other thing" 1)
; (contents-lexer "*This* is a paragraph.")
(fn list-rule [str ?regex]
(let [regex (or ?regex "(%s*)%-%s*(.-)\n")]
(var list-elems [])
(each [indents line (string.gmatch str regex)]
(table.insert list-elems {:indent-level (length indents)
:contents line}))
:contents (contents-lexer line)}))
{:type "List"
:elements list-elems}))
@ -103,12 +109,12 @@
(header-size contents) (string.match str regex)]
{:type "Header"
:size (- 4 (length header-size))
: contents}))}
:contents (contents-lexer contents)}))}
:Image {:rule (fn image-rule [str ?regex]
(let [regex (or ?regex "%!%[(.-)%]%((.-)%)\n")
(alt link) (string.match str regex)]
{:type "Image"
:contents alt
:contents (contents-lexer alt)
: link}))}
:List {:rule list-rule}
:Footnote {:rule (fn footnote-rule [str ?regex]
@ -129,10 +135,10 @@
(each [m (string.gmatch str regex)]
(set total (.. total m "\n"))))
{:type "Quote"
:contents total})}
:contents (contents-lexer total)})}
:Paragraph {:rule (fn paragraph-rule [str]
{:type "Paragraph"
:contents str})}})
:contents (contents-lexer str)})}})
; TODO
; All :contents entries must go through the contents parser