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

Anselme v2.0.0-alpha rewrite

Woke up and felt like changing a couple things. It's actually been worked on for a while, little at a time...

The goal was to make the language and implementation much simpler. Well I don't know if it really ended up being simpler but it sure is more robust.

Main changes:
* proper first class functions and closures supports! proper scoping rules! no more namespace shenanigans!
* everything is an expression, no more statements! make the implementation both simpler and more complex, but it's much more consistent now! the syntax has massively changed as a result though.
* much more organized and easy to modify codebase: one file for each AST node, no more random fields or behavior set by some random node exceptionally, everything should now follow the same API defined in ast.abstract.Node

Every foundational feature should be implemented right now. The vast majority of things that were possible in v2 are possible now; some things aren't, but that's usually because v2 is a bit more sane.
The main missing things before a proper release are tests and documentation. There's a few other things that might be implemented later, see the ideas.md file.
This commit is contained in:
Étienne Fildadut 2023-12-17 17:15:16 +01:00
parent 2ff494d108
commit fe351b5ca4
484 changed files with 7099 additions and 18084 deletions

View file

@ -0,0 +1,57 @@
local primary = require("parser.expression.primary.primary")
local comment
comment = primary {
match = function(self, str)
return str:match("^%(%(")
end,
parse = function(self, source, str, limit_pattern)
local rem = source:consume(str:match("^(%(%()(.*)$"))
local content_list = {}
while not rem:match("^%)%)") do
local content
content, rem = rem:match("^([^%(%)]*)(.-)$")
-- cut the text prematurely at limit_pattern if relevant
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
content, rem = source:count(content:sub(1, pos-1)), ("))%s%s"):format(content:sub(pos), rem)
source:increment(-2)
else
source:count(content)
end
table.insert(content_list, content)
-- nested comment
if rem:match("^%(%(") then
local subcomment
subcomment, rem = comment:parse(source, rem, limit_pattern)
table.insert(content_list, "((")
for _, c in ipairs(subcomment) do table.insert(content_list, c) end
table.insert(content_list, "))")
-- no end token after the comment
elseif not rem:match("^%)%)") then
-- single ) or (, keep on commentin'
if rem:match("^[%)%(]") then
local s
s, rem = source:count(rem:match("^([%)%(])(.-)$"))
table.insert(content_list, s)
-- anything other than end-of-line
elseif rem:match("[^%s]") then
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
else
rem = rem .. "))"
end
end
end
rem = source:consume(rem:match("^(%)%))(.*)$"))
return table.concat(content_list, ""), rem
end
}
return comment

View file

@ -0,0 +1,40 @@
local primary = require("parser.expression.primary.primary")
local identifier = require("parser.expression.primary.identifier")
local expression_to_ast = require("parser.expression.to_ast")
local ast = require("ast")
local FunctionParameter = ast.FunctionParameter
local operator_priority = require("common").operator_priority
local assignment_priority = operator_priority["_=_"]
local type_check_priority = operator_priority["_::_"]
return primary {
match = function(self, str)
return identifier:match(str)
end,
parse = function(self, source, str, limit_pattern, no_default_value)
local source_param = source:clone()
-- name
local ident, rem = identifier:parse(source, str)
-- type check
local type_check
if rem:match("^%s*::") then
local scheck = source:consume(rem:match("^(%s*::%s*)(.*)$"))
type_check, rem = expression_to_ast(source, scheck, limit_pattern, type_check_priority)
end
-- default value
local default
if not no_default_value then
if rem:match("^%s*=") then
local sdefault = source:consume(rem:match("^(%s*=%s*)(.*)$"))
default, rem = expression_to_ast(source, sdefault, limit_pattern, assignment_priority)
end
end
return FunctionParameter:new(ident, default, type_check):set_source(source_param), rem
end
}

View file

@ -0,0 +1,7 @@
local function_parameter = require("parser.expression.contextual.function_parameter")
return function_parameter {
parse = function(self, source, str, limit_pattern)
return function_parameter:parse(source, str, limit_pattern, true)
end
}

View file

@ -0,0 +1,49 @@
local primary = require("parser.expression.primary.primary")
local function_parameter = require("parser.expression.contextual.function_parameter")
local function_parameter_no_default = require("parser.expression.contextual.function_parameter_no_default")
local ast = require("ast")
local ParameterTuple = ast.ParameterTuple
return primary {
match = function(self, str)
return str:match("^%(")
end,
parse = function(self, source, str, limit_pattern)
local source_start = source:clone()
local parameters = ParameterTuple:new()
local rem = source:consume(str:match("^(%()(.*)$"))
-- i would LOVE to reuse the regular list parsing code for this, but unfortunately the list parsing code
-- itself depends on this and expect this to be available quite early, and it's ANNOYING
while not rem:match("^%s*%)") do
-- parameter
local func_param
func_param, rem = function_parameter:expect(source, rem, limit_pattern)
-- next! comma separator
if not rem:match("^%s*%)") then
if not rem:match("^%s*,") then
error(("unexpected %q at end of argument list"):format(rem), 0)
end
rem = source:consume(rem:match("^(%s*,)(.*)$"))
end
-- add
parameters:insert(func_param)
end
rem = rem:match("^%s*%)(.*)$")
-- assigment param
if rem:match("^%s*=") then
rem = source:consume(rem:match("^(%s*=%s*)(.*)$"))
local func_param
func_param, rem = function_parameter_no_default:expect(source, rem, limit_pattern)
parameters:insert_assignment(func_param)
end
return parameters:set_source(source_start), rem
end
}

View file

@ -0,0 +1,16 @@
local primary = require("parser.expression.primary.primary")
local ast = require("ast")
local Identifier, Call, ArgumentTuple = ast.Identifier, ast.Call, ast.ArgumentTuple
return primary {
match = function(self, str)
return str:match("^_")
end,
parse = function(self, source, str)
local source_start = source:clone()
local rem = source:consume(str:match("^(_)(.-)$"))
return Call:new(Identifier:new("_"), ArgumentTuple:new()):set_source(source_start), rem
end
}

View file

@ -0,0 +1,205 @@
local primary = require("parser.expression.primary.primary")
local function_parameter_no_default = require("parser.expression.contextual.function_parameter_no_default")
local parameter_tuple = require("parser.expression.contextual.parameter_tuple")
local identifier = require("parser.expression.primary.identifier")
local expression_to_ast = require("parser.expression.to_ast")
local escape = require("common").escape
local ast = require("ast")
local Symbol, Definition, Function, ParameterTuple = ast.Symbol, ast.Definition, ast.Function, ast.ParameterTuple
local regular_operators = require("common").regular_operators
local prefixes = regular_operators.prefixes
local suffixes = regular_operators.suffixes
local infixes = regular_operators.infixes
local operator_priority = require("common").operator_priority
-- same as function_parameter_no_default, but allow wrapping in (evenetual) parentheses
-- in order to solve some priotity issues (_._ has higher priority than _::_, leading to not being possible to overload it with type filtering without parentheses)
local function_parameter_maybe_parenthesis = function_parameter_no_default {
match = function(self, str)
if str:match("^%(") then
return function_parameter_no_default:match(str:match("^%((.*)$"))
else
return function_parameter_no_default:match(str)
end
end,
parse = function(self, source, str, limit_pattern)
if str:match("^%(") then
str = source:consume(str:match("^(%()(.*)$"))
local exp, rem = function_parameter_no_default:parse(source, str, limit_pattern)
if not rem:match("^%s*%)") then error(("unexpected %q at end of parenthesis"):format(rem), 0) end
rem = source:consume(rem:match("^(%s*%))(.-)$"))
return exp, rem
else
return function_parameter_no_default:parse(source, str, limit_pattern)
end
end
}
-- signature type 1: unary prefix
-- :$-parameter exp
-- returns symbol, parameter_tuple, rem if success
-- return nil otherwise
local function search_prefix_signature(modifiers, source, str, limit_pattern)
for _, pfx in ipairs(prefixes) do
local prefix = pfx[1]
local prefix_pattern = "%s*"..escape(prefix).."%s*"
if str:match("^"..prefix_pattern) then
-- operator name
local rem = source:consume(str:match("^("..prefix_pattern..")(.*)$"))
local symbol = Symbol:new(prefix.."_", modifiers):set_source(source:clone():increment(-1))
-- parameters
local parameter
parameter, rem = function_parameter_maybe_parenthesis:expect(source, rem, limit_pattern)
local parameters = ParameterTuple:new()
parameters:insert(parameter)
return symbol, parameters, rem
end
end
end
-- signature type 2: binary infix
-- should be checked before suffix signature
-- :$parameterA + parameterB exp
-- returns symbol, parameter_tuple, rem if success
-- return nil otherwise
local function search_infix_signature(modifiers, source, str, limit_pattern)
if function_parameter_maybe_parenthesis:match(str) then
local src = source:clone() -- operate on clone source since search success is not yet guaranteed
local parameter_a, rem = function_parameter_maybe_parenthesis:parse(src, str, limit_pattern)
local parameters = ParameterTuple:new()
parameters:insert(parameter_a)
for _, ifx in ipairs(infixes) do
local infix = ifx[1]
local infix_pattern = "%s*"..escape(infix).."%s*"
if rem:match("^"..infix_pattern) then
-- operator name
rem = src:consume(rem:match("^("..infix_pattern..")(.*)$"))
local symbol = Symbol:new("_"..infix.."_", modifiers):set_source(src:clone():increment(-1))
-- parameters
if function_parameter_maybe_parenthesis:match(rem) then
local parameter_b
parameter_b, rem = function_parameter_maybe_parenthesis:parse(src, rem, limit_pattern)
parameters:insert(parameter_b)
source:set(src)
return symbol, parameters, rem
else
return
end
end
end
end
end
-- signature type 3: unary suffix
-- :$parameter! exp
-- returns symbol, parameter_tuple, rem if success
-- return nil otherwise
local function search_suffix_signature(modifiers, source, str, limit_pattern)
if function_parameter_maybe_parenthesis:match(str) then
local src = source:clone() -- operate on clone source since search success is not yet guaranteed
local parameter_a, rem = function_parameter_maybe_parenthesis:parse(src, str, limit_pattern)
local parameters = ParameterTuple:new()
parameters:insert(parameter_a)
for _, sfx in ipairs(suffixes) do
local suffix = sfx[1]
local suffix_pattern = "%s*"..escape(suffix).."%s*"
if rem:match("^"..suffix_pattern) then
-- operator name
rem = src:count(rem:match("^("..suffix_pattern..")(.*)$"))
local symbol = Symbol:new("_"..suffix, modifiers):set_source(src:clone():increment(-1))
source:set(src)
return symbol, parameters, rem
end
end
end
end
-- signature type 4: regular function
-- :$identifier(parameter_tuple, ...) exp
-- returns symbol, parameter_tuple, rem if success
-- return nil otherwise
local function search_function_signature(modifiers, source, str, limit_pattern)
if identifier:match(str) then
local name_source = source:clone()
local name, rem = identifier:parse(source, str, limit_pattern)
-- name
local symbol = name:to_symbol(modifiers):set_source(name_source)
-- parse eventual parameters
local parameters
if parameter_tuple:match(rem) then
parameters, rem = parameter_tuple:parse(source, rem)
else
parameters = ParameterTuple:new()
end
return symbol, parameters, rem
end
end
return primary {
match = function(self, str)
return str:match("^%::?[&@]?%$")
end,
parse = function(self, source, str, limit_pattern)
local source_start = source:clone()
local mod_const, mod_exported, rem = source:consume(str:match("^(%:(:?)([&@]?)%$)(.-)$"))
-- get modifiers
local constant, exported, persistent
if mod_const == ":" then constant = true end
if mod_exported == "@" then exported = true
elseif mod_exported == "&" then persistent = true end
local modifiers = { constant = constant, exported = exported, persistent = persistent }
-- search for a valid signature
local symbol, parameters
local s, p, r = search_prefix_signature(modifiers, source, rem, limit_pattern)
if s then symbol, parameters, rem = s, p, r
else
s, p, r = search_infix_signature(modifiers, source, rem, limit_pattern)
if s then symbol, parameters, rem = s, p, r
else
s, p, r = search_suffix_signature(modifiers, source, rem, limit_pattern)
if s then symbol, parameters, rem = s, p, r
else
s, p, r = search_function_signature(modifiers, source, rem, limit_pattern)
if s then symbol, parameters, rem = s, p, r end
end
end
end
-- done
if symbol then
-- parse expression
local right
s, right, rem = pcall(expression_to_ast, source, rem, limit_pattern, operator_priority["$_"])
if not s then error(("invalid expression after unop %q: %s"):format(self.operator, right), 0) end
-- return function
local fn = Function:new(parameters, right):set_source(source_start)
return Definition:new(symbol, fn):set_source(source_start), rem
end
end
}

View file

@ -0,0 +1,40 @@
local primary = require("parser.expression.primary.primary")
local Identifier = require("ast.Identifier")
local disallowed_set = (".~`^+-=<>/[]*{}|\\_!?,;:()\"@&$#%"):gsub("[^%w]", "%%%1")
local identifier_pattern = "%s*[^0-9%s'"..disallowed_set.."][^"..disallowed_set.."]*"
local common = require("common")
local trim, escape = common.trim, common.escape
-- for operator identifiers
local regular_operators = require("common").regular_operators
local operators = {}
for _, prefix in ipairs(regular_operators.prefixes) do table.insert(operators, prefix[1].."_") end
for _, infix in ipairs(regular_operators.infixes) do table.insert(operators, "_"..infix[1].."_") end
for _, suffix in ipairs(regular_operators.suffixes) do table.insert(operators, "_"..suffix[1]) end
-- all valid identifier patterns
local identifier_patterns = { identifier_pattern }
for _, operator in ipairs(operators) do table.insert(identifier_patterns, "%s*"..escape(operator)) end
return primary {
match = function(self, str)
for _, pat in ipairs(identifier_patterns) do
if str:match("^"..pat) then return true end
end
return false
end,
parse = function(self, source, str)
for _, pat in ipairs(identifier_patterns) do
if str:match("^"..pat) then
local start_source = source:clone()
local name, rem = source:count(str:match("^("..pat..")(.-)$"))
name = trim(name)
return Identifier:new(name):set_source(start_source), rem
end
end
end
}

View file

@ -0,0 +1,42 @@
--- try to parse a primary expression
local function r(name)
return require("parser.expression.primary."..name), nil
end
local primaries = {
r("number"),
r("string"),
r("text"),
r("parenthesis"),
r("function_definition"),
r("symbol"),
r("identifier"),
r("block_identifier"),
r("tuple"),
r("struct"),
-- prefixes
-- 1
r("prefix.semicolon"),
r("prefix.function"),
-- 2
r("prefix.return"),
-- 3.5
r("prefix.else"),
-- 11
r("prefix.negation"),
r("prefix.not"),
r("prefix.mutable"),
}
return {
-- returns exp, rem if expression found
-- returns nil if no expression found
search = function(self, source, str, limit_pattern)
for _, primary in ipairs(primaries) do
local exp, rem = primary:search(source, str, limit_pattern)
if exp then return exp, rem end
end
end
}

View file

@ -0,0 +1,19 @@
local primary = require("parser.expression.primary.primary")
local Number = require("ast.Number")
return primary {
match = function(self, str)
return str:match("^%d*%.%d+") or str:match("^%d+")
end,
parse = function(self, source, str)
local start_source = source:clone()
local d, r = str:match("^(%d*%.%d+)(.*)$")
if not d then
d, r = source:count(str:match("^(%d+)(.*)$"))
else
source:count(d)
end
return Number:new(tonumber(d)):set_source(start_source), r
end
}

View file

@ -0,0 +1,31 @@
-- either parentheses or nil ()
local primary = require("parser.expression.primary.primary")
local ast = require("ast")
local Nil = ast.Nil
local expression_to_ast = require("parser.expression.to_ast")
return primary {
match = function(self, str)
return str:match("^%(")
end,
parse = function(self, source, str)
local start_source = source:clone()
local rem = source:consume(str:match("^(%()(.*)$"))
local exp
if rem:match("^%s*%)") then
exp = Nil:new()
else
local s
s, exp, rem = pcall(expression_to_ast, source, rem, "%)")
if not s then error("invalid expression inside parentheses: "..exp, 0) end
if not rem:match("^%s*%)") then error(("unexpected %q at end of parenthesis"):format(rem), 0) end
end
rem = source:consume(rem:match("^(%s*%))(.*)$"))
return exp:set_source(start_source), rem
end
}

View file

@ -0,0 +1,8 @@
local prefix_quote_right = require("parser.expression.primary.prefix.prefix_quote_right")
local operator_priority = require("common").operator_priority
return prefix_quote_right {
operator = "~",
identifier = "~_",
priority = operator_priority["~_"]
}

View file

@ -0,0 +1,35 @@
local prefix = require("parser.expression.primary.prefix.prefix")
local parameter_tuple = require("parser.expression.contextual.parameter_tuple")
local escape = require("common").escape
local expression_to_ast = require("parser.expression.to_ast")
local ast = require("ast")
local Function, ParameterTuple = ast.Function, ast.ParameterTuple
local operator_priority = require("common").operator_priority
return prefix {
operator = "$",
priority = operator_priority["$_"],
parse = function(self, source, str, limit_pattern)
local source_start = source:clone()
local escaped = escape(self.operator)
local rem = source:consume(str:match("^("..escaped..")(.*)$"))
-- parse eventual parameters
local parameters
if parameter_tuple:match(rem) then
parameters, rem = parameter_tuple:parse(source, rem)
else
parameters = ParameterTuple:new()
end
-- parse expression
local s, right
s, right, rem = pcall(expression_to_ast, source, rem, limit_pattern, self.priority)
if not s then error(("invalid expression after unop %q: %s"):format(self.operator, right), 0) end
return Function:new(parameters, right):set_source(source_start), rem
end
}

View file

@ -0,0 +1,9 @@
local prefix = require("parser.expression.primary.prefix.prefix")
local operator_priority = require("common").operator_priority
return prefix {
operator = "*",
identifier = "*_",
priority = operator_priority["*_"]
}

View file

@ -0,0 +1,9 @@
local prefix = require("parser.expression.primary.prefix.prefix")
local operator_priority = require("common").operator_priority
return prefix {
operator = "-",
identifier = "-_",
priority = operator_priority["-_"]
}

View file

@ -0,0 +1,9 @@
local prefix = require("parser.expression.primary.prefix.prefix")
local operator_priority = require("common").operator_priority
return prefix {
operator = "!",
identifier = "!_",
priority = operator_priority["!_"]
}

View file

@ -0,0 +1,34 @@
-- unary prefix operators, for example: the - in -5
local primary = require("parser.expression.primary.primary")
local escape = require("common").escape
local expression_to_ast = require("parser.expression.to_ast")
local ast = require("ast")
local Call, Identifier, ArgumentTuple = ast.Call, ast.Identifier, ast.ArgumentTuple
return primary {
operator = nil,
identifier = nil,
priority = nil,
match = function(self, str)
local escaped = escape(self.operator)
return str:match("^"..escaped)
end,
parse = function(self, source, str, limit_pattern)
local source_start = source:clone()
local escaped = escape(self.operator)
local sright = source:consume(str:match("^("..escaped..")(.*)$"))
local s, right, rem = pcall(expression_to_ast, source, sright, limit_pattern, self.priority)
if not s then error(("invalid expression after prefix operator %q: %s"):format(self.operator, right), 0) end
return self:build_ast(right):set_source(source_start), rem
end,
build_ast = function(self, right)
return Call:new(Identifier:new(self.identifier), ArgumentTuple:new(right))
end
}

View file

@ -0,0 +1,11 @@
local prefix = require("parser.expression.primary.prefix.prefix")
local ast = require("ast")
local Call, Identifier, ArgumentTuple, Quote = ast.Call, ast.Identifier, ast.ArgumentTuple, ast.Quote
return prefix {
build_ast = function(self, right)
right = Quote:new(right)
return Call:new(Identifier:new(self.identifier), ArgumentTuple:new(right))
end
}

View file

@ -0,0 +1,15 @@
local prefix = require("parser.expression.primary.prefix.prefix")
local ast = require("ast")
local Return = ast.Return
local operator_priority = require("common").operator_priority
return prefix {
operator = "@",
priority = operator_priority["@_"],
build_ast = function(self, right)
return Return:new(right)
end
}

View file

@ -0,0 +1,13 @@
local prefix = require("parser.expression.primary.prefix.prefix")
local operator_priority = require("common").operator_priority
return prefix {
operator = ";",
identifier = ";_",
priority = operator_priority[";_"],
build_ast = function(self, right)
return right
end
}

View file

@ -0,0 +1,33 @@
local class = require("class")
return class {
new = false, -- static class
-- returns exp, rem if expression found
-- returns nil if no expression found
search = function(self, source, str, limit_pattern)
if not self:match(str) then
return nil
end
return self:parse(source, str, limit_pattern)
end,
-- return bool
-- (not needed if you redefined :search)
match = function(self, str)
return false
end,
-- return AST, rem
-- (not needed if you redefined :search)
parse = function(self, source, str, limit_pattern)
error("unimplemented")
end,
-- class helpers --
-- return AST, rem
expect = function(self, source, str, limit_pattern)
local exp, rem = self:search(source, str, limit_pattern)
if not exp then error(("expected %s but got %s"):format(self.type, str)) end
return exp, rem
end
}

View file

@ -0,0 +1,78 @@
-- note: this is reused in primary.text, hence all the configurable fields
local primary = require("parser.expression.primary.primary")
local StringInterpolation = require("ast.StringInterpolation")
local ast = require("ast")
local String = ast.String
local expression_to_ast = require("parser.expression.to_ast")
local escape = require("common").escape
local escape_code = {
["n"] = "\n",
["t"] = "\t",
-- everything else is identity by default
}
return primary {
type = "string", -- interpolation type - used for errors
start_pattern = "\"", -- pattern that start the string interpolation
stop_char = "\"", -- character that stops the string interpolation - must be a single character!
allow_implicit_stop = false, -- set to true to allow the string to be closed implicitely when reaching the end of the expression or limit_pattern
interpolation = StringInterpolation,
match = function(self, str)
return str:match("^"..self.start_pattern)
end,
parse = function(self, source, str, limit_pattern)
local interpolation = self.interpolation:new()
local stop_pattern = escape(self.stop_char)
local start_source = source:clone()
local rem = source:consume(str:match("^("..self.start_pattern..")(.-)$"))
while not rem:match("^"..stop_pattern) do
local text_source = source:clone()
local text
text, rem = rem:match("^([^%{%\\"..stop_pattern.."]*)(.-)$") -- get all text until something potentially happens
-- cut the text prematurely at limit_pattern if relevant
if self.allow_implicit_stop and limit_pattern and text:match(limit_pattern) then
local pos = text:match("()"..limit_pattern) -- limit_pattern can contain $, so can't directly extract with captures
text, rem = source:count(text:sub(1, pos-1)), ("%s%s%s"):format(self.stop_char, text:sub(pos), rem)
source:increment(-1)
else
source:count(text)
end
interpolation:insert(String:new(text):set_source(text_source))
if rem:match("^%{") then
local ok, exp
ok, exp, rem = pcall(expression_to_ast, source, source:consume(rem:match("^(%{)(.*)$")), "%}")
if not ok then error("invalid expression inside interpolation: "..exp, 0) end
if not rem:match("^%s*%}") then error(("unexpected %q at end of interpolation"):format(rem), 0) end
rem = source:consume(rem:match("^(%s*%})(.*)$"))
interpolation:insert(exp)
elseif rem:match("^\\") then
text, rem = source:consume(rem:match("^(\\(.))(.*)$"))
interpolation:insert(String:new(escape_code[text] or text))
elseif not rem:match("^"..stop_pattern) then
if not self.allow_implicit_stop or rem:match("[^%s]") then
error(("unexpected %q at end of "..self.type):format(rem), 0)
-- consumed everything until end-of-line, implicit stop allowed, close your eyes and imagine the text has been closed
else
rem = rem .. self.stop_char
end
end
end
rem = source:consume(rem:match("^("..stop_pattern..")(.*)$"))
return interpolation:set_source(start_source), rem
end
}

View file

@ -0,0 +1,17 @@
local primary = require("parser.expression.primary.primary")
local tuple = require("parser.expression.primary.tuple")
local ast = require("ast")
local Struct = ast.Struct
return primary {
match = function(self, str)
return str:match("^%{")
end,
parse = function(self, source, str)
local l, rem = tuple:parse_tuple(source, str, "{", '}')
return Struct:from_tuple(l), rem
end
}

View file

@ -0,0 +1,40 @@
local primary = require("parser.expression.primary.primary")
local type_check = require("parser.expression.secondary.infix.type_check")
local identifier = require("parser.expression.primary.identifier")
local ast = require("ast")
local Nil = ast.Nil
return primary {
match = function(self, str)
if str:match("^%::?[&@]?") then
return identifier:match(str:match("^%::?[&@]?(.-)$"))
end
return false
end,
parse = function(self, source, str)
local mod_const, mod_export, rem = source:consume(str:match("^(%:(:?)([&@]?))(.-)$"))
local constant, persistent, type_check_exp, exported
-- get modifier
if mod_const == ":" then constant = true end
if mod_export == "&" then persistent = true
elseif mod_export == "@" then exported = true end
-- name
local ident
ident, rem = identifier:parse(source, rem)
-- type check
local nil_val = Nil:new()
if type_check:match(rem, 0, nil_val) then
local exp
exp, rem = type_check:parse(source, rem, nil, 0, nil_val)
type_check_exp = exp.arguments.list[2]
end
return ident:to_symbol{ constant = constant, persistent = persistent, exported = exported, type_check = type_check_exp }:set_source(source), rem
end
}

View file

@ -0,0 +1,24 @@
local string = require("parser.expression.primary.string")
local ast = require("ast")
local TextInterpolation = ast.TextInterpolation
return string {
type = "text",
start_pattern = "|%s?",
stop_char = "|",
allow_implicit_stop = true,
interpolation = TextInterpolation,
parse = function(self, source, str, limit_pattern)
local interpolation, rem = string.parse(self, source, str, limit_pattern)
-- restore | when chaining with a choice operator
if rem:match("^>") then
rem = "|" .. rem
source:increment(-1)
end
return interpolation, rem
end
}

View file

@ -0,0 +1,41 @@
local primary = require("parser.expression.primary.primary")
local ast = require("ast")
local Tuple = ast.Tuple
local expression_to_ast = require("parser.expression.to_ast")
local escape = require("common").escape
return primary {
match = function(self, str)
return str:match("^%[")
end,
parse = function(self, source, str)
return self:parse_tuple(source, str, "[", "]")
end,
parse_tuple = function(self, source, str, start_char, end_char)
local start_source = source:clone()
local rem = source:consume(str:match("^("..escape(start_char)..")(.*)$"))
local end_match = escape(end_char)
local l
if not rem:match("^%s*"..end_match) then
local s
s, l, rem = pcall(expression_to_ast, source, rem, end_match)
if not s then error("invalid expression in list: "..l, 0) end
end
if not Tuple:is(l) or l.explicit then l = Tuple:new(l) end -- single or no element
if not rem:match("^%s*"..end_match) then
error(("unexpected %q at end of list"):format(rem), 0)
end
rem = source:consume(rem:match("^(%s*"..end_match..")(.*)$"))
l.explicit = true
return l:set_source(start_source), rem
end,
}

View file

@ -0,0 +1,9 @@
local infix = require("parser.expression.secondary.infix.infix")
local operator_priority = require("common").operator_priority
return infix {
operator = "+",
identifier = "_+_",
priority = operator_priority["_+_"]
}

View file

@ -0,0 +1,9 @@
local infix_quote_right = require("parser.expression.secondary.infix.infix_quote_right")
local operator_priority = require("common").operator_priority
return infix_quote_right {
operator = "&",
identifier = "_&_",
priority = operator_priority["_&_"]
}

View file

@ -0,0 +1,23 @@
local infix = require("parser.expression.secondary.infix.infix")
local escape = require("common").escape
local operator_priority = require("common").operator_priority
local ast = require("ast")
local Identifier, Assignment = ast.Identifier, ast.Assignment
return infix {
operator = "=",
identifier = "_=_",
priority = operator_priority["_=_"],
-- return bool
match = function(self, str, current_priority, primary)
local escaped = escape(self.operator)
return self.priority > current_priority and str:match("^"..escaped) and Identifier:is(primary)
end,
build_ast = function(self, left, right)
return Assignment:new(left, right)
end
}

View file

@ -0,0 +1,24 @@
local infix = require("parser.expression.secondary.infix.infix")
local escape = require("common").escape
local operator_priority = require("common").operator_priority
local ast = require("ast")
local Call = ast.Call
return infix {
operator = "=",
identifier = "_=_",
priority = operator_priority["_=_"],
-- return bool
match = function(self, str, current_priority, primary)
local escaped = escape(self.operator)
return self.priority > current_priority and str:match("^"..escaped) and Call:is(primary)
end,
build_ast = function(self, left, right)
left.arguments:set_assignment(right)
return Call:new(left.func, left.arguments) -- recreate Call since we modified left.arguments
end,
}

View file

@ -0,0 +1,35 @@
local ast = require("ast")
local Call, Identifier, ArgumentTuple = ast.Call, ast.Identifier, ast.ArgumentTuple
local assignment = require("parser.expression.secondary.infix.assignment")
local assignment_call = require("parser.expression.secondary.infix.assignment_call")
local infixes = require("common").regular_operators.infixes
local generated = {}
for _, infix in ipairs(infixes) do
local operator = infix[1].."="
local identifier = "_=_"
local infix_identifier = "_"..infix[1].."_"
table.insert(generated, assignment {
operator = operator,
identifier = identifier,
build_ast = function(self, left, right)
right = Call:new(Identifier:new(infix_identifier), ArgumentTuple:new(left, right))
return assignment.build_ast(self, left, right)
end
})
table.insert(generated, assignment_call {
operator = operator,
identifier = identifier,
build_ast = function(self, left, right)
right = Call:new(Identifier:new(infix_identifier), ArgumentTuple:new(left, right))
return assignment_call.build_ast(self, left, right)
end
})
end
return generated

View file

@ -0,0 +1,28 @@
local infix = require("parser.expression.secondary.infix.infix")
local escape = require("common").escape
local identifier = require("parser.expression.primary.identifier")
local operator_priority = require("common").operator_priority
local ast = require("ast")
local Call, ArgumentTuple = ast.Call, ast.ArgumentTuple
return infix {
operator = "!",
identifier = "_!_",
priority = operator_priority["_!_"],
match = function(self, str, current_priority, primary)
local escaped = escape(self.operator)
return self.priority > current_priority and str:match("^"..escaped) and identifier:match(str:match("^"..escaped.."%s*(.-)$"))
end,
build_ast = function(self, left, right)
if Call:is(right) then
right.arguments:insert_positional(1, left)
return right
else
return Call:new(right, ArgumentTuple:new(left))
end
end
}

View file

@ -0,0 +1,17 @@
local infix = require("parser.expression.secondary.infix.infix")
local operator_priority = require("common").operator_priority
local ast = require("ast")
local Call, Identifier, ArgumentTuple, ResumeParentFunction, ParameterTuple, Function = ast.Call, ast.Identifier, ast.ArgumentTuple, ast.ResumeParentFunction, ast.ParameterTuple, ast.Function
return infix {
operator = "|>",
identifier = "_|>_",
priority = operator_priority["_|>_"],
build_ast = function(self, left, right)
right = Function:new(ParameterTuple:new(), ResumeParentFunction:new(right))
return Call:new(Identifier:new(self.identifier), ArgumentTuple:new(left, right))
end
}

View file

@ -0,0 +1,22 @@
local infix = require("parser.expression.secondary.infix.infix")
local escape = require("common").escape
local operator_priority = require("common").operator_priority
local ast = require("ast")
local Definition, Symbol = ast.Definition, ast.Symbol
return infix {
operator = "=",
identifier = "_=_",
priority = operator_priority["_=_"],
match = function(self, str, current_priority, primary)
local escaped = escape(self.operator)
return self.priority > current_priority and str:match("^"..escaped) and Symbol:is(primary)
end,
build_ast = function(self, left, right)
return Definition:new(left, right)
end
}

View file

@ -0,0 +1,9 @@
local infix = require("parser.expression.secondary.infix.infix")
local operator_priority = require("common").operator_priority
return infix {
operator = "!=",
identifier = "_!=_",
priority = operator_priority["_!=_"]
}

View file

@ -0,0 +1,9 @@
local infix = require("parser.expression.secondary.infix.infix")
local operator_priority = require("common").operator_priority
return infix {
operator = "/",
identifier = "_/_",
priority = operator_priority["_/_"]
}

View file

@ -0,0 +1,9 @@
local infix = require("parser.expression.secondary.infix.infix")
local operator_priority = require("common").operator_priority
return infix {
operator = "==",
identifier = "_==_",
priority = operator_priority["_==_"]
}

View file

@ -0,0 +1,9 @@
local infix = require("parser.expression.secondary.infix.infix")
local operator_priority = require("common").operator_priority
return infix {
operator = "^",
identifier = "_^_",
priority = operator_priority["_^_"]
}

View file

@ -0,0 +1,9 @@
local infix = require("parser.expression.secondary.infix.infix")
local operator_priority = require("common").operator_priority
return infix {
operator = ">",
identifier = "_>_",
priority = operator_priority["_>_"]
}

View file

@ -0,0 +1,9 @@
local infix = require("parser.expression.secondary.infix.infix")
local operator_priority = require("common").operator_priority
return infix {
operator = ">=",
identifier = "_>=_",
priority = operator_priority["_>=_"]
}

View file

@ -0,0 +1,9 @@
local infix_quote_right = require("parser.expression.secondary.infix.infix_quote_right")
local operator_priority = require("common").operator_priority
return infix_quote_right {
operator = "~",
identifier = "_~_",
priority = operator_priority["_~_"]
}

View file

@ -0,0 +1,23 @@
local infix = require("parser.expression.secondary.infix.infix")
local identifier = require("parser.expression.primary.identifier")
local operator_priority = require("common").operator_priority
local ast = require("ast")
local Call, Identifier, ArgumentTuple = ast.Call, ast.Identifier, ast.ArgumentTuple
return infix {
operator = "*",
identifier = "_*_",
priority = operator_priority["_*_"]+.5, -- just above / so 1/2x gives 1/(2x)
match = function(self, str, current_priority, primary)
return self.priority > current_priority and identifier:match(str)
end,
parse = function(self, source, str, limit_pattern, current_priority, primary)
local start_source = source:clone()
local right, rem = identifier:parse(source, str, limit_pattern)
return Call:new(Identifier:new(self.identifier), ArgumentTuple:new(primary, right)):set_source(start_source), rem
end
}

View file

@ -0,0 +1,19 @@
local infix = require("parser.expression.secondary.infix.infix")
local operator_priority = require("common").operator_priority
local ast = require("ast")
local Call, Identifier, ArgumentTuple = ast.Call, ast.Identifier, ast.ArgumentTuple
return infix {
operator = ".",
identifier = "_._",
priority = operator_priority["_._"],
build_ast = function(self, left, right)
if Identifier:is(right) then
right = right:to_string()
end
return Call:new(Identifier:new(self.identifier), ArgumentTuple:new(left, right))
end
}

View file

@ -0,0 +1,34 @@
local secondary = require("parser.expression.secondary.secondary")
local escape = require("common").escape
local expression_to_ast = require("parser.expression.to_ast")
local ast = require("ast")
local Call, Identifier, ArgumentTuple = ast.Call, ast.Identifier, ast.ArgumentTuple
return secondary {
operator = nil,
identifier = nil,
priority = nil,
-- return bool
match = function(self, str, current_priority, primary)
local escaped = escape(self.operator)
return self.priority > current_priority and str:match("^"..escaped)
end,
-- return AST, rem
parse = function(self, source, str, limit_pattern, current_priority, primary)
local start_source = source:clone()
local escaped = escape(self.operator)
local sright = source:consume(str:match("^("..escaped..")(.*)$"))
local s, right, rem = pcall(expression_to_ast, source, sright, limit_pattern, self.priority)
if not s then error(("invalid expression after binary operator %q: %s"):format(self.operator, right), 0) end
return self:build_ast(primary, right):set_source(start_source), rem
end,
build_ast = function(self, left, right)
return Call:new(Identifier:new(self.identifier), ArgumentTuple:new(left, right))
end
}

View file

@ -0,0 +1,32 @@
-- same as infix, but skip if no valid expression after the operator instead of erroring
-- useful for operators that are both valid as infix and as suffix
local infix = require("parser.expression.secondary.infix.infix")
local escape = require("common").escape
local expression_to_ast = require("parser.expression.to_ast")
return infix {
-- returns exp, rem if expression found
-- returns nil if no expression found
search = function(self, source, str, limit_pattern, current_priority, operating_on_primary)
if not self:match(str, current_priority, operating_on_primary) then
return nil
end
return self:maybe_parse(source, str, limit_pattern, current_priority, operating_on_primary)
end,
parse = function() error("no guaranteed parse for this operator") end,
-- return AST, rem
-- return nil
maybe_parse = function(self, source, str, limit_pattern, current_priority, primary)
local start_source = source:clone()
local escaped = escape(self.operator)
local sright = source:consume(str:match("^("..escaped..")(.*)$"))
local s, right, rem = pcall(expression_to_ast, source, sright, limit_pattern, self.priority)
if not s then return nil end
return self:build_ast(primary, right):set_source(start_source), rem
end,
}

View file

@ -0,0 +1,12 @@
local infix = require("parser.expression.secondary.infix.infix")
local ast = require("ast")
local Call, Identifier, ArgumentTuple, Quote = ast.Call, ast.Identifier, ast.ArgumentTuple, ast.Quote
return infix {
build_ast = function(self, left, right)
left = Quote:new(left)
right = Quote:new(right)
return Call:new(Identifier:new(self.identifier), ArgumentTuple:new(left, right))
end
}

View file

@ -0,0 +1,11 @@
local infix = require("parser.expression.secondary.infix.infix")
local ast = require("ast")
local Call, Identifier, ArgumentTuple, Quote = ast.Call, ast.Identifier, ast.ArgumentTuple, ast.Quote
return infix {
build_ast = function(self, left, right)
right = Quote:new(right)
return Call:new(Identifier:new(self.identifier), ArgumentTuple:new(left, right))
end
}

View file

@ -0,0 +1,9 @@
local infix = require("parser.expression.secondary.infix.infix")
local operator_priority = require("common").operator_priority
return infix {
operator = "//",
identifier = "_//_",
priority = operator_priority["_//_"]
}

View file

@ -0,0 +1,9 @@
local infix = require("parser.expression.secondary.infix.infix")
local operator_priority = require("common").operator_priority
return infix {
operator = "<",
identifier = "_<_",
priority = operator_priority["_<_"]
}

View file

@ -0,0 +1,9 @@
local infix = require("parser.expression.secondary.infix.infix")
local operator_priority = require("common").operator_priority
return infix {
operator = "<=",
identifier = "_<=_",
priority = operator_priority["_<=_"]
}

View file

@ -0,0 +1,9 @@
local infix = require("parser.expression.secondary.infix.infix")
local operator_priority = require("common").operator_priority
return infix {
operator = "%",
identifier = "_%_",
priority = operator_priority["_%_"]
}

View file

@ -0,0 +1,9 @@
local infix = require("parser.expression.secondary.infix.infix")
local operator_priority = require("common").operator_priority
return infix {
operator = "*",
identifier = "_*_",
priority = operator_priority["_*_"]
}

View file

@ -0,0 +1,9 @@
local infix_quote_right = require("parser.expression.secondary.infix.infix_quote_right")
local operator_priority = require("common").operator_priority
return infix_quote_right {
operator = "|",
identifier = "_|_",
priority = operator_priority["_|_"]
}

View file

@ -0,0 +1,9 @@
local infix = require("parser.expression.secondary.infix.infix")
local operator_priority = require("common").operator_priority
return infix {
operator = ":",
identifier = "_:_",
priority = operator_priority["_:_"]
}

View file

@ -0,0 +1,9 @@
local infix_or_suffix = require("parser.expression.secondary.infix.infix_or_suffix")
local operator_priority = require("common").operator_priority
return infix_or_suffix {
operator = ";",
identifier = "_;_",
priority = operator_priority["_;_"]
}

View file

@ -0,0 +1,9 @@
local infix = require("parser.expression.secondary.infix.infix")
local operator_priority = require("common").operator_priority
return infix {
operator = "-",
identifier = "_-_",
priority = operator_priority["_-_"]
}

View file

@ -0,0 +1,9 @@
local infix_quote_right = require("parser.expression.secondary.infix.infix_quote_right")
local operator_priority = require("common").operator_priority
return infix_quote_right {
operator = "#",
identifier = "_#_",
priority = operator_priority["_#_"]
}

View file

@ -0,0 +1,36 @@
local infix = require("parser.expression.secondary.infix.infix")
local escape = require("common").escape
local expression_to_ast = require("parser.expression.to_ast")
local operator_priority = require("common").operator_priority
local ast = require("ast")
local Tuple = ast.Tuple
return infix {
operator = ",",
identifier = "_,_",
priority = operator_priority["_,_"],
-- reminder: this :parse method is also called from primary.list as an helper to build list bracket litterals
parse = function(self, source, str, limit_pattern, current_priority, primary)
local start_source = source:clone()
local l = Tuple:new()
l:insert(primary)
local escaped = escape(self.operator)
local rem = str
while rem:match("^%s*"..escaped) do
rem = source:consume(rem:match("^(%s*"..escaped..")(.*)$"))
local s, right
s, right, rem = pcall(expression_to_ast, source, rem, limit_pattern, self.priority)
if not s then error(("invalid expression after binop %q: %s"):format(self.operator, right), 0) end
l:insert(right)
end
l.explicit = false
return l:set_source(start_source), rem
end
}

View file

@ -0,0 +1,9 @@
local infix = require("parser.expression.secondary.infix.infix")
local operator_priority = require("common").operator_priority
return infix {
operator = "::",
identifier = "_::_",
priority = operator_priority["_::_"]
}

View file

@ -0,0 +1,9 @@
local infix_quote_both = require("parser.expression.secondary.infix.infix_quote_both")
local operator_priority = require("common").operator_priority
return infix_quote_both {
operator = "~?",
identifier = "_~?_",
priority = operator_priority["_~?_"]
}

View file

@ -0,0 +1,78 @@
--- try to parse a secondary expression
local function r(name)
return require("parser.expression.secondary."..name), nil
end
local secondaries = {
-- binary infix operators
-- 1
r("infix.semicolon"),
-- 2
r("infix.tuple"),
r("infix.tag"),
-- 4
r("infix.while"),
r("infix.if"),
-- 6
r("infix.choice"),
r("infix.and"),
r("infix.or"),
-- 7
r("infix.equal"),
r("infix.different"),
r("infix.greater_equal"),
r("infix.lower_equal"),
r("infix.greater"),
r("infix.lower"),
-- 8
r("infix.addition"),
r("infix.substraction"),
-- 9
r("infix.multiplication"),
r("infix.integer_division"),
r("infix.division"),
r("infix.modulo"),
-- 9.5
r("infix.implicit_multiplication"),
-- 10
r("infix.exponent"),
-- 11
r("infix.type_check"),
-- 12
r("infix.call"),
-- 14
r("infix.index"),
-- 3
r("infix.assignment"), -- deported after equal
r("infix.assignment_call"),
r("infix.definition"),
-- 5
r("infix.pair"), -- deported after type_check
-- unary suffix operators
-- 1
r("suffix.semicolon"),
-- 12
r("suffix.exclamation_call"),
-- 13
r("suffix.call"),
}
-- add generated assignement+infix operator combos, before the rest
local assignment_operators = r("infix.assignment_with_infix")
for i, op in ipairs(assignment_operators) do
table.insert(secondaries, i, op)
end
return {
-- returns exp, rem if expression found
-- returns nil if no expression found
-- returns nil, err if error
search = function(self, source, str, limit_pattern, current_priority, primary)
for _, secondary in ipairs(secondaries) do
local exp, rem = secondary:search(source, str, limit_pattern, current_priority, primary)
if exp then return exp, rem end
end
end
}

View file

@ -0,0 +1,34 @@
local class = require("class")
return class {
new = false, -- static class
-- returns exp, rem if expression found
-- returns nil if no expression found
search = function(self, source, str, limit_pattern, current_priority, operating_on_primary)
if not self:match(str, current_priority, operating_on_primary) then
return nil
end
return self:parse(source, str, limit_pattern, current_priority, operating_on_primary)
end,
-- return bool
-- (not needed if you redefined :search)
match = function(self, str, current_priority, operating_on_primary)
return false
end,
-- return AST, rem
-- (not needed if you redefined :search)
-- assumes that :match was checked before, and can not return nil (may error though)
parse = function(self, source, str, limit_pattern, current_priority, operating_on_primary)
error("unimplemented")
end,
-- class helpers --
-- return AST, rem
expect = function(self, source, str, limit_pattern, current_priority, operating_on_primary)
local exp, rem = self:search(source, str, limit_pattern, current_priority, operating_on_primary)
if not exp then error(("expected %s but got %s"):format(self.type, str)) end
return exp, rem
end
}

View file

@ -0,0 +1,40 @@
-- index/call
local secondary = require("parser.expression.secondary.secondary")
local parenthesis = require("parser.expression.primary.parenthesis")
local operator_priority = require("common").operator_priority
local ast = require("ast")
local Call, ArgumentTuple, Tuple, Assignment, Nil = ast.Call, ast.ArgumentTuple, ast.Tuple, ast.Assignment, ast.Nil
return secondary {
priority = operator_priority["_()"],
match = function(self, str, current_priority, primary)
return self.priority > current_priority and parenthesis:match(str)
end,
parse = function(self, source, str, limit_pattern, current_priority, primary)
local start_source = source:clone()
local args = ArgumentTuple:new()
local exp, rem = parenthesis:parse(source, str, limit_pattern)
if Nil:is(exp) then
exp = Tuple:new()
elseif not Tuple:is(exp) or exp.explicit then -- single argument
exp = Tuple:new(exp)
end
for i, v in ipairs(exp.list) do
if Assignment:is(v) then
args:set_named(v.identifier, v.expression)
else
args:set_positional(i, v)
end
end
return Call:new(primary, args):set_source(start_source), rem
end
}

View file

@ -0,0 +1,15 @@
local suffix = require("parser.expression.secondary.suffix.suffix")
local operator_priority = require("common").operator_priority
local ast = require("ast")
local Call, ArgumentTuple = ast.Call, ast.ArgumentTuple
return suffix {
operator = "!",
priority = operator_priority["_!"],
build_ast = function(self, left)
return Call:new(left, ArgumentTuple:new())
end
}

View file

@ -0,0 +1,9 @@
local suffix = require("parser.expression.secondary.suffix.suffix")
local operator_priority = require("common").operator_priority
return suffix {
operator = ";",
identifier = "_;",
priority = operator_priority["_;"]
}

View file

@ -0,0 +1,31 @@
-- unary suffix operators, for example the ! in func!
local secondary = require("parser.expression.secondary.secondary")
local escape = require("common").escape
local ast = require("ast")
local Call, Identifier, ArgumentTuple = ast.Call, ast.Identifier, ast.ArgumentTuple
return secondary {
operator = nil,
identifier = nil,
priority = nil,
match = function(self, str, current_priority, primary)
local escaped = escape(self.operator)
return self.priority > current_priority and str:match("^"..escaped)
end,
parse = function(self, source, str, limit_pattern, current_priority, primary)
local start_source = source:clone()
local escaped = escape(self.operator)
local rem = source:consume(str:match("^("..escaped..")(.*)$"))
return self:build_ast(primary):set_source(start_source), rem
end,
build_ast = function(self, left)
return Call:new(Identifier:new(self.identifier), ArgumentTuple:new(left))
end
}

View file

@ -0,0 +1,50 @@
--- transform an expression string into raw AST
local primary, secondary
local comment = require("parser.expression.comment")
-- parse an expression, starting from a secondary element operating on operating_on_primary
-- returns expr, remaining
local function from_secondary(source, s, limit_pattern, current_priority, operating_on_primary)
s = source:consume(s:match("^(%s*)(.*)$"))
current_priority = current_priority or 0
-- if there is a comment, restart the parsing after the comment ends
local c, c_rem = comment:search(source, s, limit_pattern)
if c then return from_secondary(source, c_rem, limit_pattern, current_priority, operating_on_primary) end
-- secondary elements
local exp, rem = secondary:search(source, s, limit_pattern, current_priority, operating_on_primary)
if exp then return from_secondary(source, rem, limit_pattern, current_priority, exp) end
-- nothing to apply on primary
return operating_on_primary, s
end
--- parse an expression
-- current_priority: only elements of strictly higher priority will be parser
-- limit_pattern: set to a string pattern that will trigger the end of elements that would otherwise consume everything until end-of-line (pattern is not consumed)
-- fallback_exp: if no primary expression can be found, will return this instead. Used to avoid raising an error where an empty or comment-only expression is allowed.
-- return expr, remaining
local function expression_to_ast(source, s, limit_pattern, current_priority, fallback_exp)
s = source:consume(s:match("^(%s*)(.*)$"))
current_priority = current_priority or 0
-- if there is a comment, restart the parsing after the comment ends
local c, c_rem = comment:search(source, s, limit_pattern)
if c then return expression_to_ast(source, c_rem, limit_pattern, current_priority, fallback_exp) end
-- primary elements
local exp, rem = primary:search(source, s, limit_pattern)
if exp then return from_secondary(source, rem, limit_pattern, current_priority, exp) end
-- no valid primary expression
if fallback_exp then return fallback_exp, s end
error(("no valid expression before %q"):format(s), 0)
end
package.loaded[...] = expression_to_ast
primary = require("parser.expression.primary")
secondary = require("parser.expression.secondary")
-- return expr, remaining
return function(source, s, limit_pattern, current_priority, operating_on_primary, fallback_exp)
if operating_on_primary then return from_secondary(source, s, limit_pattern, current_priority, operating_on_primary)
else return expression_to_ast(source, s, limit_pattern, current_priority, fallback_exp) end
end