OpenSDE Framework (without history before r20070)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
2.3 KiB

  1. -- --- SDE-COPYRIGHT-NOTE-BEGIN ---
  2. -- This copyright note is auto-generated by ./scripts/Create-CopyPatch.
  3. --
  4. -- Filename: misc/lua/sde/desc.lua
  5. -- Copyright (C) 2006 The OpenSDE Project
  6. -- Copyright (C) 2005 - 2006 The T2 SDE Project
  7. -- Copyright (C) 2005 - 2006 Juergen "George" Sawinski
  8. --
  9. -- More information can be found in the files COPYING and README.
  10. --
  11. -- This program is free software; you can redistribute it and/or modify
  12. -- it under the terms of the GNU General Public License as published by
  13. -- the Free Software Foundation; version 2 of the License. A copy of the
  14. -- GNU General Public License can be found in the file COPYING.
  15. -- --- SDE-COPYRIGHT-NOTE-END ---
  16. -- TODO:
  17. -- - parser should also accept string as input
  18. -- - implement the desc.validator
  19. -- DESCRIPTION:
  20. -- t = desc.parse(line-iterator)
  21. -- Parse ".desc" file format by passing a line iterator and
  22. -- return a table, where lower-case of last entry in
  23. -- etc/desc_format
  24. -- is the key. Example:
  25. -- Parsing "[I] Title" returns `{ title = "Title" }'
  26. -- package object structure
  27. desc = desc or {}
  28. desc.__format__ = {}
  29. -- FIXME setmetatable(desc, { __call = function })
  30. -- parse .desc text ; expects line iterator function as argument
  31. function desc.parse(iter)
  32. local retval = {}
  33. -- FIXME: Perhaps we'll gain some performance by not reading
  34. -- line by line
  35. for line in io.open(iter):lines() do
  36. local tag,cnt
  37. _,_,tag,cnt = string.find(line, "([[][^]]*[]])[ ]+(.*)")
  38. if tag then
  39. local fmt = desc.__format__[tag]
  40. if fmt then
  41. retval[fmt.name] = retval[fmt.name] or {}
  42. table.insert(retval[fmt.name], cnt)
  43. end
  44. end
  45. end
  46. return retval
  47. end
  48. -- similar to desc.parse, but validate the description
  49. function desc.validate(iter)
  50. local retval = desc.parse(iter)
  51. -- TODO implement validating
  52. return retval
  53. end
  54. -- init: parse etc/desc_format
  55. for line in io.open("etc/desc_format"):lines() do
  56. local required=false
  57. local tag
  58. if string.match(line, "^[[]") ~= nil then
  59. -- check if tag is required
  60. if string.match(line, "([*])") ~= nil then required=true; end
  61. -- use last tag definition as name
  62. for t in string.gfind(line,"[[]([^]]*)[]]") do tag = t; end
  63. tag = string.lower(tag)
  64. -- sort into __format__
  65. for t in string.gfind(line,"([[][^]]*[]])") do
  66. desc.__format__[t] = { name = tag; required = required }
  67. end
  68. end
  69. end