Advertisement
einsteinK

API Dump Parser

Sep 7th, 2014
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.95 KB | None | 0 0
  1. -- SB-version of the API Dump Parser, as used on http://wiki.roblox.com
  2. -- Credits for the actual parser go to Anaminus
  3. -- Credits to einsteinK for editing the parser, and implementing it on the wiki
  4. -- Credits to einsteinK for quickly making this into a single script
  5.  
  6. -- Note: Unlike the wiki, which fetches the api dump from a wiki page, this script
  7. -- fetches the api dump from Anaminus' site: anaminus.github.io (Thanks for that)
  8.  
  9. -- how to match various names
  10. local mC = '[%w_<> ]*[%w_<>]' -- class name
  11. local mM = '[%w_ ]*[%w_]'     -- member name
  12. local mT = '[%w_]+'           -- value type
  13. local mE = '[%w_ ]*[%w_]'     -- enum item name
  14.  
  15. -- Parses an item's tags (stuff in square brackets)
  16. local function ParseTags(item,tags)
  17.     local tagSet = {}
  18.     for tag in tags:gmatch("%[(.-)%]") do
  19.         tagSet[tag] = true
  20.     end
  21.     item.tags = tagSet
  22. end
  23.  
  24. --  Parses comma-separated arguments
  25. local function ParseArguments(out,data)
  26.     if #data > 2 then
  27.         for arg in data:sub(2,-2):gmatch("[^,]+") do -- make this better
  28.             local type,name,default
  29.             = arg:match("^ ?("..mT..") (%w+)(.*)$")
  30.  
  31.             if #default > 0 then
  32.                 default = default:match("^ = (.*)$")
  33.             else
  34.                 default = nil
  35.             end
  36.             out[#out+1] = {
  37.                 Type = type;
  38.                 Name = name;
  39.                 Default = default;
  40.             }
  41.         end
  42.     end
  43. end
  44.  
  45. local ParseItem = {
  46.     Class = function(data)
  47.         local className,superClass,tags
  48.         = data:match("^("..mC..") : ("..mC..")(.*)$")
  49.  
  50.         if not className then
  51.             className,tags = data:match("^("..mC..")(.*)$")
  52.         end
  53.  
  54.         local item = {
  55.             Name = className;
  56.             Superclass = superClass;
  57.             Subclasses = {};
  58.             Properties = {};
  59.             Functions = {};
  60.             YieldFunctions = {};
  61.             Callbacks = {};
  62.             Events = {};
  63.         }
  64.         ParseTags(item,tags)
  65.         return item
  66.     end;
  67.     Property = function(data)
  68.         local valueType,className,memberName,tags
  69.         = data:match("^("..mT..") ("..mC..")%.("..mM..")(.*)$")
  70.  
  71.         local item = {
  72.             Class = className;
  73.             Name = memberName;
  74.             ValueType = valueType;
  75.         }
  76.         ParseTags(item,tags)
  77.         return item
  78.     end;
  79.     Function = function(data)
  80.         local returnType,className,memberName,argumentData,tags
  81.         = data:match("^("..mT..") ("..mC..")%:("..mM..")(%b())(.*)$")
  82.  
  83.         local item = {
  84.             Class = className;
  85.             Name = memberName;
  86.             ReturnType = returnType;
  87.             Arguments = {};
  88.         }
  89.         ParseArguments(item.Arguments,argumentData)
  90.         ParseTags(item,tags)
  91.         return item
  92.     end;
  93.     Event = function(data)
  94.         local className,memberName,argData,tags
  95.         = data:match("^("..mC..")%.("..mM..")(%b())(.*)$")
  96.  
  97.         local item = {
  98.             Class = className;
  99.             Name = memberName;
  100.             Arguments = {};
  101.         }
  102.         ParseArguments(item.Arguments,argData)
  103.         ParseTags(item,tags)
  104.         return item
  105.     end;
  106.     Callback = function(data)
  107.         local returnType,className,memberName,argData,tags
  108.         = data:match("^("..mT..") ("..mC..")%.("..mM..")(%b())(.*)$")
  109.  
  110.         local item = {
  111.             Class = className;
  112.             Name = memberName;
  113.             ReturnType = returnType;
  114.             Arguments = {};
  115.         }
  116.         ParseArguments(item.Arguments,argData)
  117.         ParseTags(item,tags)
  118.         return item
  119.     end;
  120.     YieldFunction = function(data)
  121.         local returnType,className,memberName,argumentData,tags
  122.         = data:match("^("..mT..") ("..mC..")%:("..mM..")(%b())(.*)$")
  123.  
  124.         local item = {
  125.             Class = className;
  126.             Name = memberName;
  127.             ReturnType = returnType;
  128.             Arguments = {};
  129.         }
  130.         ParseArguments(item.Arguments,argumentData)
  131.         ParseTags(item,tags)
  132.         return item
  133.     end;
  134.     Enum = function(data)
  135.         local enumName,tags
  136.         = data:match("^("..mC..")(.*)$")
  137.  
  138.         local item = {
  139.             Name = enumName;
  140.             Items = {};
  141.         }
  142.         ParseTags(item,tags)
  143.         return item
  144.     end;
  145.     EnumItem = function(data)
  146.         local enumName,itemName,itemValue,tags
  147.         = data:match("^("..mC..")%.("..mE..") : (%d+)(.*)$")
  148.  
  149.         local item = {
  150.             Enum = enumName;
  151.             Name = itemName;
  152.             Value = tonumber(itemValue) or itemValue;
  153.         }
  154.         ParseTags(item,tags)
  155.         return item
  156.     end;
  157. }
  158.  
  159. -- Parse API Dump, line by line
  160. function Parse(source)
  161.     local database = {}
  162.     local classes = {}
  163.     local properties = {}
  164.     local events = {}
  165.     local callbacks = {}
  166.     local functions = {}
  167.     local yfunctions = {}
  168.     local enums = {}
  169.     local enumitems = {}
  170.     local nLine = 0
  171.     for line in source:gmatch("[^\r\n]+") do
  172.         nLine = nLine + 1
  173.         local type,data = line:match("^\t*(%w+) (.+)$")
  174.         local parser = ParseItem[type]
  175.         if parser then
  176.             local item,err = parser(data)
  177.             if item then
  178.                 item.type = type
  179.                 database[#database+1] = item
  180.                 if type == "Class" then
  181.                     classes[item.Name] = item
  182.                 elseif type == "Function" then
  183.                     functions[item.Name] = item
  184.                 elseif type == "YieldFunction" then
  185.                     yfunctions[item.Name] = item
  186.                 elseif type == "Callback" then
  187.                     callbacks[item.Name] = item
  188.                 elseif type == "Event" then
  189.                     events[item.Name] = item
  190.                 elseif type == "Property" then
  191.                     properties[item.Name] = item
  192.                 elseif type == "Enum" then
  193.                     enums[item.Name] = item
  194.                 elseif type == "EnumItem" then
  195.                     enumitems[item.Name] = item
  196.                 end
  197.             else
  198.                 print("error parsing line "..nLine..": "..err)
  199.             end
  200.         else
  201.             print("unsupported item type `"..tostring(type).."` on line "..nLine)
  202.         end
  203.     end
  204.     for k,v in pairs(classes) do
  205.         local parent = classes[v.Superclass]
  206.         if parent then
  207.             parent.Subclasses[k] = v
  208.         end
  209.     end
  210.     for a,b in pairs({Functions=functions,YieldFunctions=yfunctions,Events=events,Properties=properties,Callbacks=callbacks}) do
  211.         for k,v in pairs(b) do
  212.             local parent = classes[v.Class]
  213.             if parent then
  214.                 parent[a][k] = v
  215.             end
  216.         end
  217.     end
  218.     for k,v in pairs(enumitems) do
  219.         local parent = enums[v.Enum]
  220.         if parent then
  221.             parent.Items[k] = v
  222.         end
  223.     end
  224.     return {
  225.         Classes=classes;
  226.         Enums=enums;
  227.         EnumItems=enumitems;
  228.         Functions=functions;
  229.         YieldFunctions=yfunctions;
  230.         Events=events;
  231.         Properties=properties;
  232.         Callbacks=callbacks;
  233.     }
  234. end
  235.  
  236. local u = "https://github.com/Anaminus/anaminus.github.io/raw/master/rbx/raw/api/latest.txt"
  237. local ad = Parse(game:GetService("HttpService"):GetAsync(u))
  238. shared.AD,_G.AD = ad,ad
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement