Skip to content

Commit

Permalink
Add names to built-in and user-defined syntax extensions
Browse files Browse the repository at this point in the history
We track these names in `walkable_grammar` via the
`reader->insert_pattern()` and `...->update_rule()` functions.
  • Loading branch information
Witiko committed Oct 3, 2022
1 parent ee31bc3 commit c8398a5
Showing 1 changed file with 39 additions and 7 deletions.
46 changes: 39 additions & 7 deletions markdown.dtx
Original file line number Diff line number Diff line change
Expand Up @@ -21611,7 +21611,9 @@ function M.reader.new(writer, options)
%
% \end{markdown}
% \begin{macrocode}
local current_extension_name = nil
self.insert_pattern = function(selector, pattern)
assert(current_extension_name ~= nil)
local _, _, lhs, pos, rhs = selector:find("^(%a+)%s+([%a%s]+%a+)%s+(%a+)$")
assert(lhs ~= nil,
[[Expected selector in form "LHS (before|after|instead of) RHS", not "]]
Expand All @@ -21635,10 +21637,11 @@ function M.reader.new(writer, options)
assert(index ~= nil,
[[Rule ]] .. lhs .. [[ -> ]] .. rhs
.. [[ does not exist in markdown grammar]])
local accountable_pattern = { pattern, current_extension_name }
if pos == "instead of" then
rule[index] = pattern
rule[index] = accountable_pattern
else
table.insert(rule, index, pattern)
table.insert(rule, index, accountable_pattern)
end
end
% \end{macrocode}
Expand Down Expand Up @@ -21713,7 +21716,8 @@ function M.reader.new(writer, options)
self.update_rule = function(rule_name, pattern)
assert(syntax[rule_name] ~= nil,
[[Rule ]] .. rule_name .. [[ -> ... does not exist in markdown grammar]])
walkable_syntax[rule_name] = { pattern }
local accountable_pattern = { pattern, current_extension_name }
walkable_syntax[rule_name] = { accountable_pattern }
end
% \end{macrocode}
% \par
Expand Down Expand Up @@ -21747,6 +21751,7 @@ function M.reader.new(writer, options)
% \end{markdown}
% \begin{macrocode}
for _, extension in ipairs(extensions) do
current_extension_name = extension.name
extension.extend_writer(writer)
extension.extend_reader(self)
end
Expand Down Expand Up @@ -21779,10 +21784,24 @@ function M.reader.new(writer, options)
syntax[lhs] = parsers.fail
for _, rhs in ipairs(rule) do
local pattern
% \end{macrocode}
% \begin{markdown}
%
% Although the interface of the \luamref{reader->insert_pattern} method does
% document this (see Section <#luauserextensions>), we allow the
% \luamref{reader->insert_pattern} and \luamref{reader->update_rule}
% methods to insert not just \acro{peg} patterns, but also rule names that
% reference the \acro{peg} grammar of Markdown.
%
% \end{markdown}
% \begin{macrocode}
if type(rhs) == "string" then
pattern = V(rhs)
else
pattern = rhs
pattern = rhs[1]
if type(pattern) == "string" then
pattern = V(pattern)
end
end
syntax[lhs] = syntax[lhs] + pattern
end
Expand All @@ -21791,9 +21810,9 @@ function M.reader.new(writer, options)
% \par
% \begin{markdown}
%
% Finalize the parser by enabling built-in syntax extensions and producing
% special parsers for difficult edge cases such as blocks nested in definition
% lists or inline content nested in link, note, and image labels.
% Finalize the parser by reacting to options and by producing special parsers
% for difficult edge cases such as blocks nested in definition lists or
% inline content nested in link, note, and image labels.
%
% \end{markdown}
% \begin{macrocode}
Expand Down Expand Up @@ -21984,6 +22003,7 @@ M.extensions.citations = function(citation_nbsps)
["#"] = "\\markdownRendererHash{}",
}
return {
name = "built-in citations syntax extension",
extend_writer = function(self)
local options = self.options

Expand Down Expand Up @@ -22208,6 +22228,7 @@ M.extensions.content_blocks = function(language_map)
end)()

return {
name = "built-in content_blocks syntax extension",
extend_writer = function(self)
% \end{macrocode}
% \par
Expand Down Expand Up @@ -22335,6 +22356,7 @@ end
% \begin{macrocode}
M.extensions.definition_lists = function(tight_lists)
return {
name = "built-in definition_lists syntax extension",
extend_writer = function(self)
% \end{macrocode}
% \par
Expand Down Expand Up @@ -22434,6 +22456,7 @@ end
% \begin{macrocode}
M.extensions.fenced_code = function(blank_before_code_fence)
return {
name = "built-in fenced_code syntax extension",
extend_writer = function(self)
local options = self.options

Expand Down Expand Up @@ -22551,6 +22574,7 @@ end
M.extensions.footnotes = function(footnotes, inline_footnotes)
assert(footnotes or inline_footnotes)
return {
name = "built-in footnotes syntax extension",
extend_writer = function(self)
% \end{macrocode}
% \par
Expand Down Expand Up @@ -22635,6 +22659,7 @@ end
% \begin{macrocode}
M.extensions.header_attributes = function()
return {
name = "built-in header_attributes syntax extension",
extend_writer = function()
end, extend_reader = function(self)
local parsers = self.parsers
Expand Down Expand Up @@ -22702,6 +22727,7 @@ end
% \begin{macrocode}
M.extensions.jekyll_data = function(expect_jekyll_data)
return {
name = "built-in jekyll_data syntax extension",
extend_writer = function(self)
% \end{macrocode}
% \par
Expand Down Expand Up @@ -22910,6 +22936,7 @@ M.extensions.pipe_tables = function(table_captions)
end

return {
name = "built-in pipe_tables syntax extension",
extend_writer = function(self)
% \end{macrocode}
% \par
Expand Down Expand Up @@ -23015,6 +23042,7 @@ end
% \begin{macrocode}
M.extensions.strike_through = function()
return {
name = "built-in strike_through syntax extension",
extend_writer = function(self)
% \end{macrocode}
% \par
Expand Down Expand Up @@ -23055,6 +23083,7 @@ end
% \begin{macrocode}
M.extensions.superscripts = function()
return {
name = "built-in superscripts syntax extension",
extend_writer = function(self)
% \end{macrocode}
% \par
Expand Down Expand Up @@ -23094,6 +23123,7 @@ end
% \begin{macrocode}
M.extensions.subscripts = function()
return {
name = "built-in subscripts syntax extension",
extend_writer = function(self)
% \end{macrocode}
% \par
Expand Down Expand Up @@ -23133,6 +23163,7 @@ end
% \begin{macrocode}
M.extensions.fancy_lists = function()
return {
name = "built-in fancy_lists syntax extension",
extend_writer = function(self)
local options = self.options

Expand Down Expand Up @@ -23506,6 +23537,7 @@ function M.new(options)
% \end{markdown}
% \begin{macrocode}
local extension = {
name = [[user-defined "]] .. pathname .. [[" syntax extension]],
extend_reader = user_extension.finalize_grammar,
extend_writer = function() end,
}
Expand Down

0 comments on commit c8398a5

Please sign in to comment.