logo

oasis-root

Compiled tree of Oasis Linux based on own branch at <https://hacktivis.me/git/oasis/> git clone https://anongit.hacktivis.me/git/oasis-root.git

xml.lua (2705B)


  1. -- Copyright 2006-2024 Mitchell. See LICENSE.
  2. -- XML LPeg lexer.
  3. local lexer = lexer
  4. local P, S = lpeg.P, lpeg.S
  5. local lex = lexer.new(...)
  6. -- Comments and CDATA.
  7. lex:add_rule('comment', lex:tag(lexer.COMMENT, lexer.range('<!--', '-->')))
  8. lex:add_rule('cdata', lex:tag('cdata', lexer.range('<![CDATA[', ']]>')))
  9. -- Doctype.
  10. local ws = lex:get_rule('whitespace')
  11. local identifier = (lexer.alpha + S('_-')) * (lexer.alnum + S('_-'))^0
  12. local doctype = lex:tag(lexer.TAG .. '.doctype', '<!DOCTYPE') * ws *
  13. lex:tag(lexer.TAG .. '.doctype', identifier) * (ws * identifier)^-1 * (1 - P('>'))^0 *
  14. lex:tag(lexer.TAG .. '.doctype', '>')
  15. lex:add_rule('doctype', doctype)
  16. -- Processing instructions.
  17. lex:add_rule('proc_insn', lex:tag(lexer.TAG .. '.pi', '<?' * identifier + '?>'))
  18. -- Tags.
  19. local namespace = lex:tag(lexer.OPERATOR, ':') * lex:tag(lexer.LABEL, identifier)
  20. lex:add_rule('element', lex:tag(lexer.TAG, '<' * P('/')^-1 * identifier) * namespace^-1)
  21. -- Closing tags.
  22. lex:add_rule('close_tag', lex:tag(lexer.TAG, P('/')^-1 * '>'))
  23. -- Equals.
  24. -- TODO: performance is terrible on large files.
  25. local in_tag = P(function(input, index)
  26. local before = input:sub(1, index - 1)
  27. local s, e = before:find('<[^>]-$'), before:find('>[^<]-$')
  28. if s and e then return s > e end
  29. if s then return true end
  30. return input:find('^[^<]->', index) ~= nil
  31. end)
  32. local equals = lex:tag(lexer.OPERATOR, '=') -- * in_tag
  33. -- lex:add_rule('equal', equals)
  34. -- Attributes.
  35. local attribute_eq = lex:tag(lexer.ATTRIBUTE, identifier) * namespace^-1 * ws^-1 * equals
  36. lex:add_rule('attribute', attribute_eq)
  37. -- Strings.
  38. local sq_str = lexer.range("'", false, false)
  39. local dq_str = lexer.range('"', false, false)
  40. lex:add_rule('string', lex:tag(lexer.STRING, lexer.after_set('=', sq_str + dq_str)))
  41. -- Numbers.
  42. local number = lex:tag(lexer.NUMBER, lexer.dec_num * P('%')^-1)
  43. lex:add_rule('number', lexer.after_set('=', number)) -- *in_tag)
  44. -- Entities.
  45. local predefined = lex:tag(lexer.CONSTANT_BUILTIN .. '.entity',
  46. '&' * lexer.word_match('lt gt amp apos quot') * ';')
  47. local general = lex:tag(lexer.CONSTANT .. '.entity', '&' * identifier * ';')
  48. lex:add_rule('entity', predefined + general)
  49. -- Fold Points.
  50. local function disambiguate_lt(text, pos, line, s) return not line:find('^</', s) and 1 or -1 end
  51. lex:add_fold_point(lexer.TAG, '<', disambiguate_lt)
  52. lex:add_fold_point(lexer.TAG, '/>', -1)
  53. lex:add_fold_point(lexer.COMMENT, '<!--', '-->')
  54. lex:add_fold_point('cdata', '<![CDATA[', ']]>')
  55. lexer.property['scintillua.comment'] = '<!--|-->'
  56. lexer.property['scintillua.angle.braces'] = '1'
  57. lexer.property['scintillua.word.chars'] =
  58. 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-'
  59. return lex