LexicalHelper.lua 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. local LexicalHelper = class("LexicalHelper")
  2. local slen = string.len
  3. local ssub = string.sub
  4. local sgsub = string.gsub
  5. local sfind = string.find
  6. local strim = string.trim
  7. local rtrim = string.rtrim
  8. local tinsert = table.insert
  9. local sgmatch = string.gmatch
  10. local _XmlUiNames =
  11. {
  12. ["dialog"] = true,
  13. ["Image"] = true,
  14. ["Text"] = true,
  15. ["Input"] = true,
  16. ["Button"] = true,
  17. ["Layout"] = true,
  18. ["ScrollView"] = true,
  19. ["ListView"] = true,
  20. ["RichText"] = true,
  21. ["TextAtlas"] = true,
  22. ["CircleBar"] = true,
  23. ["LoadingBar"] = true,
  24. ["CheckBox"] = true
  25. }
  26. function LexicalHelper:ctor()
  27. end
  28. function LexicalHelper:tbTransform( tb )
  29. local newtb = {}
  30. for i,v in ipairs(tb) do
  31. local _,_,key = sfind(v, "(.+)=")
  32. local _,_,val = sfind(v, "=(.+)")
  33. newtb[key] = strim(val)
  34. end
  35. return newtb
  36. end
  37. function LexicalHelper:parseCell(celldata)
  38. celldata = celldata.." "
  39. local tb = {}
  40. for v in sgmatch(celldata, "(.-) (.-)") do
  41. tinsert(tb, v)
  42. end
  43. local js = {}
  44. local t = 1
  45. for i,v in ipairs(tb) do
  46. local _b, _e = sfind(v, "(%a)=")
  47. if _b and _e then
  48. js[t] = v
  49. t = t + 1
  50. else
  51. if js[t-1] then
  52. js[t-1] = js[t-1].." "..v
  53. end
  54. end
  55. end
  56. return self:tbTransform(js)
  57. end
  58. function LexicalHelper:GetValidData( type, data )
  59. if not _XmlUiNames[tostring(type)] then
  60. SL:Print("Xml Parse ERROR , type: ", tostring(type))
  61. return false
  62. end
  63. local attrData = sgsub(data, "<(.-) ", "")
  64. attrData = sgsub(attrData, "(%s+)=", "=")
  65. return self:push_element(type, self:parseCell(attrData))
  66. end
  67. function LexicalHelper:ParseDialog(data)
  68. data = sgsub(data, "(/%s-)>", ">")
  69. data = sgsub(data, "\"", "")
  70. data = sgsub(data, "<(%s+)", "<")
  71. local node = {}
  72. local tWnd = {}
  73. local startWnd = false
  74. for v in sgmatch(data, "(.-)>") do
  75. if v then
  76. v = strim(v)
  77. local _, _, jType = sfind(v, "<(.-) ")
  78. if jType == "dialog" then
  79. local _data = self:GetValidData(jType, v)
  80. if _data then
  81. tinsert(node, 1, _data)
  82. end
  83. else
  84. local _, _, isdialogWnd = sfind(v, "</dialog(.-)")
  85. if isdialogWnd then
  86. break
  87. end
  88. if jType == "ScrollView" or jType == "ListView" then
  89. startWnd = true
  90. local _data = self:GetValidData(jType, v)
  91. if _data then
  92. tinsert(tWnd, 1, _data)
  93. end
  94. else
  95. local _, _, isEndWnd = sfind(v, "</ScrollView(.-)")
  96. if not isEndWnd then
  97. _, _, isEndWnd = sfind(v, "</ListView(.-)")
  98. end
  99. if isEndWnd then
  100. tinsert(node, tWnd)
  101. startWnd = false
  102. tWnd = {}
  103. else
  104. local _data = self:GetValidData(jType, v)
  105. if _data then
  106. if startWnd == true then
  107. tinsert(tWnd, _data)
  108. else
  109. tinsert(node, _data)
  110. end
  111. end
  112. end
  113. end
  114. end
  115. end
  116. end
  117. return node
  118. end
  119. -- 解析注释
  120. function LexicalHelper:ParseNote(source)
  121. -- 解析 --[[ xxx ]] 格式
  122. source = sgsub(source, "%-%-%[%[(.+)%]%]", "")
  123. -- 解析 -- 格式
  124. local newSource = ""
  125. source = source .."\n"
  126. for content in sgmatch(source, "(.-)[\n]") do
  127. local t1 = sfind(content, "\"(.-)%-%-") or sfind(content, "\'(.-)%-%-")
  128. local t2 = sfind(content, "%-%-")
  129. if t2 and not t1 then
  130. content = sgsub(content, "%-%-(.+)", "\n")
  131. end
  132. newSource = newSource .."\n".. content
  133. end
  134. if slen(newSource) < 1 then
  135. newSource = source
  136. end
  137. return newSource
  138. end
  139. function LexicalHelper:Parse(source, root)
  140. local function _IsNilResult(_t)
  141. return (_t and _t[1] and _t[2] and slen(_t[3] or "") > 0)
  142. end
  143. local xmls = {}
  144. local script = ""
  145. if source then
  146. source = self:ParseNote(source)
  147. if root then
  148. -- 不带script和dialog的子控件
  149. source = strim(source)
  150. return {xmls = self:ParseDialog(source)}
  151. end
  152. local scriptStr = {
  153. sfind(source, "<script>(.*)</script>")
  154. }
  155. local xmlStr = {
  156. sfind(source, "<dialog(.+)</dialog>")
  157. }
  158. if _IsNilResult(xmlStr) then
  159. local data = xmlStr[3]
  160. data = rtrim(data)
  161. data = "<dialog "..data.."</dialog>"
  162. xmls = self:ParseDialog(data)
  163. if _IsNilResult(scriptStr) then
  164. script = scriptStr[3]
  165. end
  166. else
  167. if _IsNilResult(scriptStr) then
  168. script = scriptStr[3]
  169. else
  170. script = source
  171. end
  172. end
  173. end
  174. return {script = script, xmls = xmls}
  175. end
  176. function LexicalHelper:push_element(type, kvmap)
  177. return {
  178. type = type, attr = kvmap
  179. }
  180. end
  181. return LexicalHelper