Skip to content

Commit

Permalink
Add support for indexing builtin libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
boatbomber committed Oct 6, 2021
1 parent a8b96c1 commit 7ed23e1
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 6 deletions.
34 changes: 28 additions & 6 deletions src/lexer/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ local COMMENT_INCOMP = "%-%-.*" --Incompleted Singleline-Comment
local lang = require(script.language)
local lua_keyword = lang.keyword
local lua_builtin = lang.builtin
local lua_libraries = lang.libraries

local lua_matches = {
-- Indentifiers
Expand Down Expand Up @@ -94,8 +95,8 @@ function lexer.scan(s: string)

local index = 1
local sz = #s
local p1, p2 = "", ""

-- stylua: ignore
return function()
if index <= sz then
for _, m in ipairs(lua_matches) do
Expand All @@ -109,21 +110,42 @@ function lexer.scan(s: string)
--end

local t = m[2]
local t2 = t

-- Process t into t2
if t == "var" then
-- Since we merge spaces into the tok, we need to remove them
-- in order to check the actual word it contains
local cleanTok = string.gsub(tok, Cleaner, "")

if lua_keyword[cleanTok] then
return "keyword", tok
t2 = "keyword"
elseif lua_builtin[cleanTok] then
return "builtin", tok
t2 = "builtin"
else
return "iden", tok
t2 = "iden"
end

if string.find(p1, "%.[%s%c]*$") then
-- The previous was a . so we need to special case indexing things
local parent = string.gsub(p2, Cleaner, "")
local lib = lua_libraries[parent]
if lib and lib[cleanTok] then
-- Indexing a builtin lib with existing item, treat as a builtin
t2 = "builtin"
else
-- Indexing a non builtin, can't be treated as a keyword/builtin
t2 = "iden"
end
-- print("indexing",parent,"with",cleanTok,"as",t2)
end
else
return t, tok
end

-- Record last 2 tokens for the indexing context check
p2 = p1
p1 = tok

return t2, tok
end
end
end
Expand Down
128 changes: 128 additions & 0 deletions src/lexer/language.lua
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,132 @@ return {
["Vector3"] = true,
["Vector3int16"] = true,
},

libraries = {
math = {
abs = true,
acos = true,
asin = true,
atan = true,
atan2 = true,
ceil = true,
clamp = true,
cos = true,
cosh = true,
deg = true,
exp = true,
floor = true,
fmod = true,
frexp = true,
ldexp = true,
log = true,
log10 = true,
max = true,
min = true,
modf = true,
noise = true,
pow = true,
rad = true,
random = true,
sinh = true,
sqrt = true,
tan = true,
tanh = true,
sign = true,
sin = true,
randomseed = true,

huge = true,
pi = true,
},

string = {
byte = true,
char = true,
find = true,
format = true,
gmatch = true,
gsub = true,
len = true,
lower = true,
match = true,
rep = true,
reverse = true,
split = true,
sub = true,
upper = true,
},

table = {
concat = true,
foreach = true,
foreachi = true,
getn = true,
insert = true,
remove = true,
sort = true,
find = true,
pack = true,
unpack = true,
move = true,
create = true,
},

debug = {
profilebegin = true,
profileend = true,
traceback = true,
},

task = {
wait = true,
spawn = true,
delay = true,
defer = true,
},

os = {
time = true,
date = true,
difftime = true,
clock = true,
},

coroutine = {
create = true,
isyieldable = true,
resume = true,
running = true,
status = true,
wrap = true,
yield = true,
},

bit32 = {
arshift = true,
band = true,
bnot = true,
bor = true,
btest = true,
bxor = true,
extract = true,
lrotate = true,
lshift = true,
replace = true,
rrotate = true,
rshift = true,
},

utf8 = {
char = true,
codepoint = true,
codes = true,
graphemes = true,
len = true,
nfcnormalize = true,
nfdnormalize = true,
offset = true,
charpattern = true,
},
},
}

0 comments on commit 7ed23e1

Please sign in to comment.