mirror of
https://github.com/Reuh/anselme.git
synced 2025-10-27 16:49:31 +00:00
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.
40 lines
1.4 KiB
Lua
40 lines
1.4 KiB
Lua
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
|
|
}
|