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:
parent
2ff494d108
commit
fe351b5ca4
484 changed files with 7099 additions and 18084 deletions
16
parser/expression/primary/block_identifier.lua
Normal file
16
parser/expression/primary/block_identifier.lua
Normal 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
|
||||
}
|
||||
205
parser/expression/primary/function_definition.lua
Normal file
205
parser/expression/primary/function_definition.lua
Normal 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
|
||||
}
|
||||
40
parser/expression/primary/identifier.lua
Normal file
40
parser/expression/primary/identifier.lua
Normal 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
|
||||
}
|
||||
42
parser/expression/primary/init.lua
Normal file
42
parser/expression/primary/init.lua
Normal 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
|
||||
}
|
||||
19
parser/expression/primary/number.lua
Normal file
19
parser/expression/primary/number.lua
Normal 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
|
||||
}
|
||||
31
parser/expression/primary/parenthesis.lua
Normal file
31
parser/expression/primary/parenthesis.lua
Normal 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
|
||||
}
|
||||
8
parser/expression/primary/prefix/else.lua
Normal file
8
parser/expression/primary/prefix/else.lua
Normal 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["~_"]
|
||||
}
|
||||
35
parser/expression/primary/prefix/function.lua
Normal file
35
parser/expression/primary/prefix/function.lua
Normal 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
|
||||
}
|
||||
9
parser/expression/primary/prefix/mutable.lua
Normal file
9
parser/expression/primary/prefix/mutable.lua
Normal 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["*_"]
|
||||
}
|
||||
9
parser/expression/primary/prefix/negation.lua
Normal file
9
parser/expression/primary/prefix/negation.lua
Normal 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["-_"]
|
||||
}
|
||||
9
parser/expression/primary/prefix/not.lua
Normal file
9
parser/expression/primary/prefix/not.lua
Normal 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["!_"]
|
||||
}
|
||||
34
parser/expression/primary/prefix/prefix.lua
Normal file
34
parser/expression/primary/prefix/prefix.lua
Normal 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
|
||||
}
|
||||
11
parser/expression/primary/prefix/prefix_quote_right.lua
Normal file
11
parser/expression/primary/prefix/prefix_quote_right.lua
Normal 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
|
||||
}
|
||||
15
parser/expression/primary/prefix/return.lua
Normal file
15
parser/expression/primary/prefix/return.lua
Normal 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
|
||||
}
|
||||
13
parser/expression/primary/prefix/semicolon.lua
Normal file
13
parser/expression/primary/prefix/semicolon.lua
Normal 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
|
||||
}
|
||||
33
parser/expression/primary/primary.lua
Normal file
33
parser/expression/primary/primary.lua
Normal 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
|
||||
}
|
||||
78
parser/expression/primary/string.lua
Normal file
78
parser/expression/primary/string.lua
Normal 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
|
||||
}
|
||||
17
parser/expression/primary/struct.lua
Normal file
17
parser/expression/primary/struct.lua
Normal 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
|
||||
}
|
||||
40
parser/expression/primary/symbol.lua
Normal file
40
parser/expression/primary/symbol.lua
Normal 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
|
||||
}
|
||||
24
parser/expression/primary/text.lua
Normal file
24
parser/expression/primary/text.lua
Normal 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
|
||||
}
|
||||
41
parser/expression/primary/tuple.lua
Normal file
41
parser/expression/primary/tuple.lua
Normal 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,
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue