1
0
Fork 0
mirror of https://github.com/Reuh/anselme.git synced 2025-10-27 16:49:31 +00:00

Comments: switch from (( to /* delimiters

Double parentheses turned out to conflict too often with normal code. I guess sometimes I shouldn't try to make my own syntax up.
This commit is contained in:
Étienne Fildadut 2023-12-31 19:54:54 +01:00
parent 9e73269cda
commit 861877503a
3 changed files with 20 additions and 20 deletions

View file

@ -3,20 +3,20 @@ local primary = require("anselme.parser.expression.primary.primary")
local comment local comment
comment = primary { comment = primary {
match = function(self, str) match = function(self, str)
return str:match("^%(%(") return str:match("^%/%*")
end, end,
parse = function(self, source, str, limit_pattern) parse = function(self, source, str, limit_pattern)
local rem = source:consume(str:match("^(%(%()(.*)$")) local rem = source:consume(str:match("^(%/%*)(.*)$"))
local content_list = {} local content_list = {}
while not rem:match("^%)%)") do while not rem:match("^%*%/") do
local content local content
content, rem = rem:match("^([^%(%)]*)(.-)$") content, rem = rem:match("^([^%/%*]*)(.-)$")
-- cut the text prematurely at limit_pattern if relevant -- cut the text prematurely at limit_pattern if relevant
if limit_pattern and content:match(limit_pattern) then if limit_pattern and content:match(limit_pattern) then
local pos = content:match("()"..limit_pattern) -- limit_pattern can contain $, so can't directly extract with captures local pos = content:match("()"..limit_pattern) -- limit_pattern can contain $, so can't directly extract with captures
content, rem = source:count(content:sub(1, pos-1)), ("))%s%s"):format(content:sub(pos), rem) content, rem = source:count(content:sub(1, pos-1)), ("*/%s%s"):format(content:sub(pos), rem)
source:increment(-2) source:increment(-2)
else else
source:count(content) source:count(content)
@ -25,30 +25,30 @@ comment = primary {
table.insert(content_list, content) table.insert(content_list, content)
-- nested comment -- nested comment
if rem:match("^%(%(") then if rem:match("^%/%*") then
local subcomment local subcomment
subcomment, rem = comment:parse(source, rem, limit_pattern) subcomment, rem = comment:parse(source, rem, limit_pattern)
table.insert(content_list, "((") table.insert(content_list, "/*")
table.insert(content_list, subcomment) table.insert(content_list, subcomment)
table.insert(content_list, "))") table.insert(content_list, "*/")
-- no end token after the comment -- no end token after the comment
elseif not rem:match("^%)%)") then elseif not rem:match("^%*%/") then
-- single ) or (, keep on commentin' -- single * or /, keep on commentin'
if rem:match("^[%)%(]") then if rem:match("^[%*%/]") then
local s local s
s, rem = source:count(rem:match("^([%)%(])(.-)$")) s, rem = source:count(rem:match("^([%*%/])(.-)$"))
table.insert(content_list, s) table.insert(content_list, s)
-- anything other than end-of-line -- anything other than end-of-line
elseif rem:match("[^%s]") then elseif rem:match("[^%s]") then
error(("unexpected %q at end of comment"):format(rem), 0) error(("unexpected %q at end of comment"):format(rem), 0)
-- consumed everything until end-of-line, close your eyes and imagine the text has been closed -- consumed everything until end-of-line, close your eyes and imagine the text has been closed
else else
rem = rem .. "))" rem = rem .. "*/"
end end
end end
end end
rem = source:consume(rem:match("^(%)%))(.*)$")) rem = source:consume(rem:match("^(%*%/)(.*)$"))
return table.concat(content_list, ""), rem return table.concat(content_list, ""), rem
end end

View file

@ -51,7 +51,7 @@ return [[
s.current checkpoint = () s.current checkpoint = ()
@s! @s!
((Additionnal helpers)) /*Additionnal helpers*/
:@$ cycle(l::tuple) :@$ cycle(l::tuple)
:i = 2 :i = 2
i <= l!len ~? i <= l!len ~?

View file

@ -1,9 +1,9 @@
((hey couic + 5)) /*hey couic + 5*/
((nested ((comments)) d)) /*nested /*comments*/ d*/
2 ((end of line)) 2 /*end of line*/
((start of line)) 3 /*start of line*/ 3
5 + ((middle)) 3 5 + /*middle*/ 3