webmcp

diff framework/env/param/_get_parser.lua @ 0:9fdfb27f8e67

Version 1.0.0
author jbe/bsw
date Sun Oct 25 12:00:00 2009 +0100 (2009-10-25)
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/framework/env/param/_get_parser.lua	Sun Oct 25 12:00:00 2009 +0100
     1.3 @@ -0,0 +1,72 @@
     1.4 +function param._get_parser(format_info, param_type)
     1.5 +  if format_info == nil or format_info == "" then
     1.6 +    return function(str)
     1.7 +      return param_type:load(str)
     1.8 +    end
     1.9 +  else
    1.10 +    local format_options = {}
    1.11 +    local format_type, rest = string.match(
    1.12 +      format_info,
    1.13 +      "^([A-Za-z][A-Za-z0-9_]*)(.*)$"
    1.14 +    )
    1.15 +    if format_type then
    1.16 +      rest = string.gsub(rest, "\\\\", "\\e")
    1.17 +      rest = string.gsub(rest, "\\'", "\\q")
    1.18 +      if
    1.19 +        string.find(rest, "\\$") or
    1.20 +        string.find(rest, "\\[^eq]")
    1.21 +      then
    1.22 +        format_type = nil
    1.23 +      else
    1.24 +        while rest ~= "" do
    1.25 +          local key, value, new_rest
    1.26 +          key, value, new_rest = string.match(
    1.27 +            rest,
    1.28 +            "^-([A-Za-z][A-Za-z0-9_]*)-'([^']*)'(.*)$"
    1.29 +          )
    1.30 +          if value then
    1.31 +            value = string.gsub(value, "\\q", "'")
    1.32 +            value = string.gsub(value, "\\e", "\\")
    1.33 +            format_options[key] = value
    1.34 +          else
    1.35 +            key, value, new_rest = string.match(
    1.36 +              rest,
    1.37 +              "^-([A-Za-z][A-Za-z0-9_]*)-([^-]*)(.*)$"
    1.38 +            )
    1.39 +            if value then
    1.40 +              if string.find(value, "^[0-9.Ee+-]+$") then
    1.41 +                local num = tonumber(value)
    1.42 +                if not num then
    1.43 +                  format_type = nil
    1.44 +                  break
    1.45 +                end
    1.46 +                format_options[key] = num
    1.47 +              elseif value == "t" then
    1.48 +                format_options[key] = true
    1.49 +              elseif value == "f" then
    1.50 +                format_options[key] = false
    1.51 +              else
    1.52 +                format_type = nil
    1.53 +                break
    1.54 +              end
    1.55 +            else
    1.56 +              format_type = nil
    1.57 +              break
    1.58 +            end
    1.59 +          end
    1.60 +          rest = new_rest
    1.61 +        end
    1.62 +      end
    1.63 +    end
    1.64 +    if not format_type then
    1.65 +      error("Illegal format string in GET/POST parameters found.")
    1.66 +    end
    1.67 +    local parse_func = parse[format_type]
    1.68 +    if not parse_func then
    1.69 +      error("Unknown format identifier in GET/POST parameters encountered.")
    1.70 +    end
    1.71 +    return function(str)
    1.72 +      return parse_func(str, param_type, format_options)
    1.73 +    end
    1.74 +  end
    1.75 +end

Impressum / About Us