summaryrefslogtreecommitdiff
path: root/rst_parser.lua
diff options
context:
space:
mode:
Diffstat (limited to 'rst_parser.lua')
-rw-r--r--rst_parser.lua136
1 files changed, 123 insertions, 13 deletions
diff --git a/rst_parser.lua b/rst_parser.lua
index cc4304d..54c3719 100644
--- a/rst_parser.lua
+++ b/rst_parser.lua
@@ -55,7 +55,8 @@ tracklists.bullets.max = 0
tracklists.lastbullet = ""
tracklists.roman_cache = {} -- storing roman numerals that were already converted
tracklists.currentindent = "" -- used in definition lists and elsewhere
-tracklists.currentwidth = 0-- table layout
+tracklists.currentwidth = 0 -- table layout
+tracklists.currentlayout = {} -- table layout
local enclosed_mapping = {
["'"] = "'",
@@ -212,14 +213,122 @@ local parser = P{
-- Table block
--------------------------------------------------------------------------------
- table_block = V"grid_table"
- --+ V"simple_table"
+ table_block = V"simple_table"
+ + V"grid_table"
/ rst.table
,
+--------------------------------------------------------------------------------
+-- Simple tables
+--------------------------------------------------------------------------------
+
+ simple_table = Ct(V"st_first_row"
+ * V"st_other_rows")
+ --* V"blank_line"^1
+ * V"end_block"^1
+ / function (tab)
+ return rst.simple_table(helpers.table.simple(tab))
+ end
+ ,
+
+ st_first_row = V"st_setindent"
+ * C(V"st_setlayout")
+ * V"space"^0
+ * V"eol"
+ ,
+
+ st_setindent = Cmt(V"space"^0, function(s, i, indent)
+ warn("sta-i", "true", #indent, "set", i)
+ tracklists.currentindent = indent
+ return true
+ end)
+ ,
+
+ st_matchindent = Cmt(V"space"^0, function(s, i, indent)
+ warn("sta-m", tracklists.currentindent == indent, #indent, #tracklists.currentindent, i)
+ return tracklists.currentindent == indent
+ end)
+ ,
+
+ st_setlayout = Cmt((V"equals"^1) * (V"spaces" * V"equals"^1)^1, function(s, i, layout)
+ local tc = tracklists.currentlayout
+ warn("sta-l", #layout, "set", "", i)
+ tc.raw = layout
+ tc.bounds = help.get_st_boundaries(layout)
+ return true
+ end)
+ ,
+
+ st_other_rows = (V"st_content"^1 * V"st_separator")^1,
+
+ st_content = C(V"st_matchlayout"),
+
+ st_matchlayout = -#V"st_separator" * Cmt((1 - V"eol")^1, function (s, i, content)
+ -- Don't check for matching indent but if the rest is
+ -- fine then the line should be sane. This allows
+ -- cells starting with spaces.
+ content = content:sub(#tracklists.currentindent)
+ local tcb = tracklists.currentlayout.bounds
+ local n = 1
+ local spaces_only = P" "^1
+ while n < #tcb.slices do
+ local from = tcb.slices[n] .stop
+ local to = tcb.slices[n+1].start
+ --print(n, from, to, content)
+ local between = spaces_only:match(content, from)
+ if not between then -- Cell spanning more than one row.
+ -- pass
+ warn("sta-c", "span", from, to, i)
+ elseif not (between >= to) then
+ warn("sta-c", "false", from, to, i)
+ return false
+ end
+ n = n + 1
+ end
+ warn("sta-c", "true", #tcb.slices, "", i)
+ return true
+ end)
+ * V"eol"
+ ,
+
+ st_separator = V"st_matchindent"
+ * C(V"st_normal_sep" + V"st_colspan_sep")
+ * V"eol"
+ ,
+
+ st_normal_sep = Cmt((V"equals"^1) * (V"spaces" * V"equals"^1)^1, function(s, i, layout)
+ warn("sta-s", tracklists.currentlayout.raw == layout, #layout, #tracklists.currentlayout.raw, i)
+ return tracklists.currentlayout.raw == layout
+ end)
+ ,
+
+ st_colspan_sep = Cmt(V"dash"^1 * (V"spaces" * V"dash"^1)^0, function(s, i, layout)
+ local tcb = tracklists.currentlayout.bounds
+ local this = help.get_st_boundaries (layout)
+ local start_valid = false
+ for start, _ in next, this.starts do
+ if tcb.starts[start] then
+ start_valid = true
+ local stop_valid = false
+ for stop, _ in next, this.stops do
+ if tcb.stops[stop] then -- bingo
+ stop_valid = true
+ end
+ end
+ if not stop_valid then
+ warn("sta-x", stop_valid, #layout, #tracklists.currentlayout.raw, i)
+ return false
+ end
+ end
+ end
+ warn("sta-x", start_valid, #layout, #tracklists.currentlayout.raw, i)
+ return start_valid
+ end)
+ ,
+
--------------------------------------------------------------------------------
--- Grid table
+-- Grid tables
--------------------------------------------------------------------------------
grid_table = Ct(V"gt_first_row"
@@ -235,7 +344,6 @@ local parser = P{
* V"eol"
,
- --gt_setindent = Cg(V"space"^0, "tableindent"),
gt_setindent = Cmt(V"space"^0, function(s, i, indent)
warn("tab-i", true, #indent, "set", i)
tracklists.currentindent = indent
@@ -256,7 +364,6 @@ local parser = P{
* V"gt_body"
,
- --gt_matchindent = Cmt(V"space"^0 * Cb"tableindent", function (s, i, this, matchme)
gt_matchindent = Cmt(V"space"^0, function (s, i, this)
local matchme = tracklists.currentindent
warn("tab-m", "indent", #this == #matchme, #this, #matchme, i)
@@ -652,7 +759,7 @@ local parser = P{
end),
--bullet_stop =V"blank_line" * Cs(Cc("")) / rst.stopitemize,
- bullet_stop =V"endpar" * Cs(Cc("")) / rst.stopitemize,
+ bullet_stop =V"end_block" * Cs(Cc("")) / rst.stopitemize,
bullet_init = V"eol"^0 * V"bullet_first" * V"bullet_itemrest",
@@ -758,7 +865,7 @@ local parser = P{
transition = V"eol"^0
* V"transition_line"
- * V"endpar"
+ * V"end_block"
/rst.transition,
--------------------------------------------------------------------------------
@@ -808,7 +915,7 @@ local parser = P{
* C(1 - V"whitespace" - V"eol")^1)^0)
* V"eol" * #(1 - V"whitespace" - "eol")) / rst.joinindented
+ C((1 - V"eol")^1) * V"eol" * #(V"double_dot" + V"eol")
- + (1 - V"endpar")^0 * Cc("make me constant!"),
+ + (1 - V"end_block")^0 * Cc("make me constant!"),
target = Ct((V"target_name" * (V"space"^0 * V"eol" * V"target_name")^0)
* V"space"^0
@@ -824,7 +931,7 @@ local parser = P{
/ rst.target,
target_block = (V"anonymous_target" + V"target")^1
- * V"endpar",
+ * V"end_block",
--------------------------------------------------------------------------------
-- Paragraphs * Inline Markup
@@ -833,7 +940,8 @@ local parser = P{
paragraph = V"par_setindent"
* Ct(C((1 - V"eol")^1) * V"eol"
* (V"par_matchindent" * C((1 - V"eol")^1) * V"eol")^0)
- * V"blank_line"^1
+ --* V"blank_line"^1
+ * V"end_block"^1
/ rst.paragraph,
par_setindent = Cmt(V"space"^0, function (s, i, indent)
@@ -926,7 +1034,7 @@ local parser = P{
-- Terminal Symbols and Low-Level Elements
--------------------------------------------------------------------------------
- word = (1 - V"punctuation" - V"endpar" - V"spacing" - V"eol")^1, -- TODO : no punctuation (later)
+ word = (1 - V"punctuation" - V"end_block" - V"spacing" - V"eol")^1, -- TODO : no punctuation (later)
asterisk = P"*",
double_asterisk = V"asterisk" * V"asterisk",
@@ -1017,7 +1125,9 @@ local parser = P{
eol = P"\n",
eof = V"eol"^0 * -P(1),
- endpar = V"eol" * (V"blank_line"^1 + V"eof"),
+ end_block = V"blank_line"^1
+ + (V"whitespace"^0 * V"eol"
+ * (V"whitespace"^0 * V"eol")^0 * V"eof"),
-- diverse markup character sets
delimiters = P"‐" + P"‑" + P"‒" + P"–" + V"emdash" + V"space", -- inline markup