Jakey4543

variable thing

Feb 23rd, 2022
1,267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.24 KB | None | 0 0
  1. local Utils = script.Parent
  2. local IsInString = require(Utils.IsInString).is
  3.  
  4. local function ISO(l,si)
  5.     if type(si) == "table" then
  6.         if si[l] then
  7.             return true
  8.         else
  9.             return false
  10.         end
  11.     else
  12.         if l == si then
  13.             return true
  14.         else
  15.             return false
  16.         end
  17.     end
  18. end
  19.  
  20. local function isV3(location,str,settings) -- incomprehensible code cause i wrote it like 1.5 years ago when i made the first version
  21.     local start = nil
  22.    
  23.     if string.sub(str,location,location) == string.sub(settings["Vector3Identifier"],1,1) then
  24.         start = location
  25.         for i = location,#str,1 do
  26.             local letter = string.sub(str,i,i)
  27.             if letter == settings["Vector3Open"] then
  28.  
  29.                 if string.sub(str,start,i) == settings["Vector3Identifier"]..settings["Vector3Open"] then
  30.                     return true,i
  31.                 end
  32.             end
  33.         end
  34.     end
  35.     return false,nil
  36. end
  37.  
  38. local module = {}
  39.  
  40.     function module.scrape(str,settings)
  41.         local variables = {}
  42.        
  43.         local identifier = settings["VarIdentifier"]
  44.         local stringIdentifier = settings["StringIdentifier"]
  45.         local v3Identifier = settings["Vector3Identifier"]
  46.         local v3Open = settings["Vector3Open"]
  47.         local v3Close = settings["Vector3Close"]
  48.         local v3Sep = settings["Vector3Seperator"]
  49.        
  50.         local foundvariable = false
  51.         local hasVarName = false
  52.         local hasValStart = false
  53.         local variableStart = nil
  54.         local variableEnd = nil
  55.         local valueStart = nil
  56.         local valueEnd = nil
  57.        
  58.         local hasVarType = false
  59.        
  60.         local isString = false
  61.         local isNumberVar = false
  62.         local isVector3 = false
  63.        
  64.         local x,y,z = nil,nil,nil
  65.        
  66.         local xStart,yStart,zStart = nil,nil,nil
  67.         local xEnd,yEnd,zEnd = nil,nil,nil
  68.         local v3hasOpened = false
  69.        
  70.         for textIndex = 1,#str do
  71.             local letter = string.sub(str,textIndex,textIndex)
  72.            
  73.             if foundvariable == false then
  74.                 if letter == identifier then
  75.                     if IsInString(str,textIndex,settings) == false then
  76.                         variableStart = textIndex+1
  77.                         foundvariable = true
  78.                     end
  79.                 end
  80.                 continue
  81.             end
  82.            
  83.             if letter == " " and hasVarName == false then
  84.                 if str:sub(textIndex+1,textIndex+1) ~= "=" then
  85.                     warn("Expected letter got ' '")
  86.                     return {"ERROR","Expected letter got ' '"}
  87.                 else
  88.                     hasVarName = true
  89.                     variableEnd = textIndex-1
  90.                     continue
  91.                 end
  92.             end
  93.            
  94.             if tonumber(letter) ~= nil and hasVarName == false then
  95.                 warn("Expected letter got number")
  96.                 return {"ERROR","Expected letter got number"}
  97.             end
  98.            
  99.             if letter == "&" then
  100.                 warn("Cannot use variable identifier in variable name")
  101.                 return {"ERROR","Cannot use variable identifier in variable name"}
  102.             end
  103.            
  104.            
  105.             if hasVarName == false then
  106.                 continue
  107.             end
  108.            
  109.             do -- String variable checker
  110.                 if ISO(letter,stringIdentifier) == true and isString == false and hasVarType == false then
  111.                     isString = true
  112.                     hasVarType = true
  113.                     valueStart = textIndex
  114.                     continue
  115.                 end
  116.                
  117.                 if ISO(letter,stringIdentifier) == true and isString == true then
  118.                     valueEnd  = textIndex
  119.                    
  120.                     table.insert(variables,{
  121.                         ["Name"] = str:sub(variableStart,variableEnd),
  122.                         ["Value"] = str:sub(valueStart,valueEnd),
  123.                         ["Type"] = "string"
  124.                     })
  125.                    
  126.                     foundvariable = false
  127.                     hasVarName = false
  128.                     isString = false
  129.                     hasVarType = false
  130.                    
  131.                     continue
  132.                    
  133.                 end
  134.             end
  135.            
  136.             do -- Number variable checker
  137.                 local allowed = {
  138.                     ["1"] = true,
  139.                     ["2"] = true,
  140.                     ["3"] = true,
  141.                     ["4"] = true,
  142.                     ["5"] = true,
  143.                     ["6"] = true,
  144.                     ["7"] = true,
  145.                     ["8"] = true,
  146.                     ["9"] = true,
  147.                     ["0"] = true,
  148.                     ["-"] = true,
  149.                     ["."] = true,
  150.                 }
  151.                
  152.                 if allowed[letter] ~= nil and isNumberVar == false and hasVarType == false then
  153.                     isNumberVar = true
  154.                     hasVarType = true
  155.                     valueStart = textIndex
  156.                     continue
  157.                 end
  158.                
  159.                 if isNumberVar == true then
  160.                     if allowed[letter] == nil and letter ~= " " then
  161.                         warn("Var type number got letter")
  162.                         return {"ERROR","Var type number got letter"}
  163.                     end
  164.                    
  165.                     if textIndex == #str then -- variable end
  166.                         valueEnd = textIndex
  167.                        
  168.                         if tonumber(str:sub(valueStart,valueEnd)) == nil then
  169.                             warn("Attempt to save variable of type number but variable value is not a number")
  170.                             return {"ERROR","Attempt to save variable of type number but variable value is not a number"}
  171.                         end
  172.                        
  173.                         table.insert(variables,{
  174.                             ["Name"] = str:sub(variableStart,variableEnd),
  175.                             ["Value"] = tonumber(str:sub(valueStart,valueEnd)),
  176.                             ["Type"] = "number"
  177.                         })
  178.  
  179.                         foundvariable = false
  180.                         hasVarName = false
  181.                         isNumberVar = false
  182.                         hasVarType = false
  183.                        
  184.                         continue
  185.                     end
  186.                    
  187.                     if letter == " " then
  188.                         valueEnd = textIndex
  189.  
  190.                         if tonumber(str:sub(valueStart,valueEnd)) == nil then
  191.                             warn("Attempt to save variable of type number but variable value is not a number")
  192.                             return {"ERROR","Attempt to save variable of type number but variable value is not a number"}
  193.                         end
  194.  
  195.                         table.insert(variables,{
  196.                             ["Name"] = str:sub(variableStart,variableEnd),
  197.                             ["Value"] = tonumber(str:sub(valueStart,valueEnd)),
  198.                             ["Type"] = "number"
  199.                         })
  200.  
  201.                         foundvariable = false
  202.                         hasVarName = false
  203.                         isNumberVar = false
  204.                         hasVarType = false
  205.                        
  206.                         continue
  207.                     end
  208.                 end
  209.             end
  210.            
  211.             do -- Vector3 variable checker
  212.                
  213.                 local is,e = isV3(textIndex,str,settings)
  214.                 if is == true and hasVarType == false then
  215.                     valueStart = e
  216.                     isVector3 = true
  217.                     hasVarType = true
  218.                 end
  219.                
  220.                 if isVector3 == true then
  221.                     if letter == v3Open then
  222.                         xStart = textIndex+1
  223.                         v3hasOpened = true
  224.                         continue
  225.                     --else
  226.                     --  continue
  227.                     end
  228.                    
  229.                     if v3hasOpened == false then
  230.                         continue
  231.                     end
  232.                    
  233.                     if xStart ~= nil and xEnd == nil then
  234.                         if letter == v3Sep then
  235.                             xEnd = textIndex-1
  236.                             yStart = textIndex+1
  237.                             continue
  238.                         else
  239.                             continue
  240.                         end
  241.                     end
  242.                    
  243.                     if yStart ~= nil and yEnd == nil then
  244.                         if letter == v3Sep then
  245.                             yEnd = textIndex-1
  246.                             zStart = textIndex+1
  247.                             continue
  248.                         else
  249.                             continue
  250.                         end
  251.                     end
  252.                    
  253.                     if zStart ~= nil and zEnd == nil then
  254.                         if letter == v3Close then
  255.                             zEnd = textIndex-1
  256.                         else
  257.                             continue
  258.                         end
  259.                     end
  260.                    
  261.                     x = tonumber(str:sub(xStart,xEnd))
  262.                     y = tonumber(str:sub(yStart,yEnd))
  263.                     z = tonumber(str:sub(zStart,zEnd))
  264.                    
  265.                     if x == nil then warn("X of vector 3 is not a number") return {"ERROR","X of vector 3 is not a number"} end
  266.                     if y == nil then warn("Y of vector 3 is not a number") return {"ERROR","Y of vector 3 is not a number"} end
  267.                     if z == nil then warn("Z of vector 3 is not a number") return {"ERROR","Z of vector 3 is not a number"} end
  268.                    
  269.                     table.insert(variables,{
  270.                         ["Name"] = str:sub(variableStart,variableEnd),
  271.                         ["Value"] = Vector3.new(x,y,z),
  272.                         ["Type"] = "vector3"
  273.                     })
  274.                    
  275.                     hasVarType = false
  276.                     xStart = nil
  277.                     xEnd = nil
  278.                     yStart = nil
  279.                     yEnd = nil
  280.                     zStart = nil
  281.                     zEnd = nil
  282.                    
  283.                     x = nil
  284.                     y = nil
  285.                     z = nil
  286.                    
  287.                     continue
  288.                 end
  289.             end
  290.         end
  291.        
  292.         return variables
  293.     end
  294.  
  295. return module
  296.  
Advertisement
Add Comment
Please, Sign In to add comment