Lignum

Meta Parser

May 12th, 2014
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.96 KB | None | 0 0
  1. local argv = { ... }
  2. local argc = #argv
  3.  
  4. if argc < 1 then
  5.   printError "Usage: metaparser <file>"
  6.   return
  7. end
  8.  
  9. local file = fs.open(argv[1], "r")
  10. local line = ""
  11.  
  12. local hasMatched = false
  13.  
  14. local function isEmpty(str)
  15.   return str == nil or str == ""
  16. end
  17.  
  18. while true do
  19.   line = file.readLine()
  20.  
  21.   -- When the end of the file is reached
  22.   -- stop reading.
  23.   if line == nil then
  24.     break
  25.   end
  26.    
  27.   local metadata, key, param, value = line:match("(%-%-%s-@(%w+)%s-%[?(%w-)]?:%s(.+))")
  28.  
  29.   -- Only print when something was found.
  30.   if metadata ~= nil then
  31.     hasMatched = true
  32.    
  33.     print("Key: " .. tostring(key))
  34.     print("Param: " .. tostring(param))
  35.     print("Value: " .. tostring(value))
  36.     print()
  37.   elseif (metadata == nil and hasMatched) and not isEmpty(line) then
  38.     -- If the line can't be matched
  39.     -- and there already have been parsed
  40.     -- lines, end the scanning process.
  41.     break
  42.   end
  43. end
  44.  
  45. file.close()
Advertisement
Add Comment
Please, Sign In to add comment