finally fix script, block, tuple, list, dict, etc. off by one errors for last term

This commit is contained in:
Scott Richmond 2024-07-19 16:00:17 -04:00
parent e9fee4c0e1
commit 4a069278b8

View File

@ -305,7 +305,9 @@
(advance parser) # consume the :lparen
(def ast @{:type :args :data @[] :token origin :partial false})
(while (not (check parser :rparen))
(accept-many parser ;separates)
(accept-many parser :newline :comma)
(when (= :break ((current parser) :type))
(break (advance parser)))
(when (check parser :eof)
(def err {:type :error :token origin :msg "unclosed paren"})
(array/push (parser :errors) err)
@ -367,7 +369,9 @@
(advance parser) # consume the :lparen
(def ast {:type :tuple :data @[] :token origin})
(while (not (check parser :rparen))
(accept-many parser ;separates)
(accept-many parser :newline :comma)
(when (= :break ((current parser) :type))
(break (advance parser)))
(when (check parser :eof)
(def err {:type :error :token origin :msg "unclosed paren"})
(array/push (parser :errors) err)
@ -383,7 +387,9 @@
(advance parser)
(def ast {:type :list :data @[] :token origin})
(while (not (check parser :rbracket))
(accept-many parser ;separates)
(accept-many parser :newline :comma)
(when (= :break ((current parser) :type))
(break (advance parser)))
(when (check parser :eof)
(def err {:type :error :token origin :msg "unclosed bracket"})
(array/push (parser :errors) err)
@ -406,7 +412,9 @@
(advance parser)
(def ast {:type :set :data @[] :token origin})
(while (not (check parser :rbrace))
(accept-many parser ;separates)
(accept-many parser :newline :comma)
(when (= :break ((current parser) :type))
(break (advance parser)))
(when (check parser :eof)
(def err {:type :error :token origin :msg "unclosed brace"})
(array/push (parser :errors) err)
@ -429,7 +437,9 @@
(advance parser)
(def ast {:type :dict :data @[] :token origin})
(while (not (check parser :rbrace))
(accept-many parser ;separates)
(accept-many parser :newline :comma)
(when (= :break ((current parser) :type))
(break (advance parser)))
(when (check parser :eof)
(def err {:type :error :token origin :msg "unclosed brace"})
(array/push (parser :errors) err)
@ -479,7 +489,9 @@
(advance parser) # consume the :lparen
(def ast {:type :tuple :data @[] :token origin})
(while (not (check parser :rparen))
(accept-many parser ;separates)
(accept-many parser :newline :comma)
(when (= :break ((current parser) :type))
(break (advance parser)))
(when (check parser :eof)
(def err {:type :error :token origin :msg "unclosed paren"})
(array/push (parser :errors) err)
@ -501,7 +513,9 @@
(advance parser)
(def ast {:type :list :data @[] :token origin})
(while (not (check parser :rbracket))
(accept-many parser ;separates)
(accept-many parser :newline :comma)
(when (= :break ((current parser) :type))
(break (advance parser)))
(when (check parser :eof)
(def err {:type :error :token origin :msg "unclosed bracket"})
(array/push (parser :errors) err)
@ -523,7 +537,9 @@
(advance parser)
(def ast {:type :dict :data @[] :token origin})
(while (not (check parser :rbrace))
(accept-many parser ;separates)
(accept-many parser :newline :comma)
(when (= :break ((current parser) :type))
(break (advance parser)))
(when (check parser :eof)
(def err {:type :error :token origin :msg "unclosed brace"})
(array/push (parser :errors) err)
@ -827,7 +843,9 @@
(expect parser :lbrace) (advance parser)
(def data @[])
(while (not (check parser :rbrace))
(accept-many parser ;terminators)
(accept-many parser :newline :semicolon)
(when (= :break ((current parser) :type))
(break (advance parser)))
(if (check parser :eof)
(error {:type :error :token origin :data data :msg "unclosed brace"}))
(array/push data (capture expr parser))
@ -1131,7 +1149,9 @@
(def lines @[])
(while (not (check parser :eof))
(print "starting script loop with " (pp-tok origin))
(accept-many parser ;terminators)
(accept-many parser :newline :semicolon)
(when (= :break ((current parser) :type))
(break (advance parser)))
(array/push lines (capture toplevel parser))
(capture terminator parser))
{:type :script :data lines :token origin})
@ -1145,13 +1165,11 @@
(do
# (comment
(def source `
(,,,,,1, 2, foo)
(foo bar)
`)
(def scanned (s/scan source))
# (print "\n***NEW PARSE***\n")
(def a-parser (new-parser scanned))
(def parsed (script a-parser))
(pp (map (fn [err] (err :msg)) ((parse scanned) :errors)))
(print (pp-ast ((parse scanned) :ast)))
(def parsed (parse scanned))
(pp (map (fn [err] (err :msg)) (parsed :errors)))
(print (pp-ast (parsed :ast)))
)