Loading

Paste #pynt5ihc6

  1. local function process_line(line, language)
  2.   -- Skip empty lines and comments
  3.   if string.find(line, "^%s*$") or string.find(line, "^%s*%-%-") then return end
  4.  
  5.   -- Process line with meta information
  6.   local _, _, languages = string.find(line, "^Language%s*%(([^%)]+)%)")
  7.   if languages then
  8.     local lang_names = {}
  9.     for name in string.gmatch(languages, "\"(%a+)\"") do
  10.       lang_names[#lang_names + 1] = name
  11.     end
  12.     if #lang_names > 0 then language.lang_names = lang_names end
  13.     return
  14.   end
  15.   local _, _, inherit = string.find(line, "^Inherit%s*%(\"(%a+)\"%s*%)")
  16.   if inherit then
  17.     language.inherit = inherit
  18.     return
  19.   end
  20.  
  21.   -- Process normal strings
  22.   local _, _, str_name, text = string.find(line, "^([^=]+)=%s*\"(.*)\"%s*$")
  23.   if str_name then
  24.     local _, last, name = string.find(str_name, "^%s*([a-z_]+)")
  25.     if not last then return nil end
  26.  
  27.     local names = {name}
  28.     local start = last + 1
  29.     while true do
  30.       _, last, name = string.find(str_name, "%.([a-z_]+)", start)
  31.       if not name then
  32.         _, last, name = string.find(str_name, "%[(%d+)%]", start)
  33.       end
  34.       if name then
  35.         names[#names + 1] = name
  36.         start = last + 1
  37.       else
  38.         break
  39.       end
  40.     end
  41.     if string.find(str_name, "%s*$", start) then
  42.       language.strings[#language.strings + 1] = {name = names, text = text}
  43.       return
  44.     end
  45.   end
  46. end
  47.  
  48. function loadLanguage(fname)
  49.   language = {strings = {}}
  50.   for line in io.lines(fname) do process_line(line, language) end
  51.   return language
  52. end

Comments