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

wsf.lua (3131B)


  1. -- Copyright 2006-2024 Mitchell. See LICENSE.
  2. -- WSF LPeg lexer (based on XML).
  3. -- Contributed by Jeff Stone.
  4. local lexer = lexer
  5. local P, S = lpeg.P, lpeg.S
  6. local lex = lexer.new(...)
  7. -- Comments.
  8. lex:add_rule('comment', lex:tag(lexer.COMMENT, lexer.range('<!--', '-->')))
  9. -- Elements.
  10. local identifier = (lexer.alpha + S('_-')) * (lexer.alnum + S('_-'))^0
  11. local tag = lex:tag(lexer.TAG, '<' * P('/')^-1 * identifier)
  12. lex:add_rule('tag', tag)
  13. -- Closing tags.
  14. local tag_close = lex:tag(lexer.TAG, P('/')^-1 * '>')
  15. lex:add_rule('tag_close', tag_close)
  16. -- Equals.
  17. -- TODO: performance is terrible on large files.
  18. local in_tag = P(function(input, index)
  19. local before = input:sub(1, index - 1)
  20. local s, e = before:find('<[^>]-$'), before:find('>[^<]-$')
  21. if s and e then return s > e end
  22. if s then return true end
  23. return input:find('^[^<]->', index) ~= nil
  24. end)
  25. local equals = lex:tag(lexer.OPERATOR, '=') -- * in_tag
  26. -- lex:add_rule('equals', equals)
  27. -- Attributes.
  28. local ws = lex:get_rule('whitespace')
  29. local attribute_eq = lex:tag(lexer.ATTRIBUTE, identifier) * ws^-1 * equals
  30. lex:add_rule('attribute', attribute_eq)
  31. -- Strings.
  32. local sq_str = lexer.range("'", false, false)
  33. local dq_str = lexer.range('"', false, false)
  34. local string = lex:tag(lexer.STRING, lexer.after_set('=', sq_str + dq_str))
  35. lex:add_rule('string', string)
  36. -- Numbers.
  37. local number = lex:tag(lexer.NUMBER, lexer.dec_num * P('%')^-1)
  38. lex:add_rule('number', lexer.after_set('=', number)) -- * in_tag)
  39. -- Entities.
  40. local predefined = lex:tag(lexer.CONSTANT_BUILTIN .. '.entity',
  41. '&' * lexer.word_match('lt gt amp apos quot') * ';')
  42. local general = lex:tag(lexer.CONSTANT .. '.entity', '&' * identifier * ';')
  43. lex:add_rule('entity', predefined + general)
  44. -- Fold points.
  45. local function disambiguate_lt(text, pos, line, s) return not line:find('^</', s) and 1 or -1 end
  46. lex:add_fold_point(lexer.TAG, '<', disambiguate_lt)
  47. lex:add_fold_point(lexer.TAG, '/>', -1)
  48. lex:add_fold_point(lexer.COMMENT, '<!--', '-->')
  49. -- Finally, add JavaScript and VBScript as embedded languages
  50. -- Tags that start embedded languages.
  51. local embed_start_tag = tag * (ws * attribute_eq * ws^-1 * string)^0 * ws^-1 * tag_close
  52. local embed_end_tag = tag * tag_close
  53. -- Embedded JavaScript.
  54. local js = lexer.load('javascript')
  55. local js_start_rule = #(P('<script') * (P(function(input, index)
  56. if input:find('^%s+language%s*=%s*(["\'])[jJ][ava]*[sS]cript%1', index) then return true end
  57. end) + '>')) * embed_start_tag -- <script language="javascript">
  58. local js_end_rule = #P('</script>') * embed_end_tag -- </script>
  59. lex:embed(js, js_start_rule, js_end_rule)
  60. -- Embedded VBScript.
  61. local vbs = lexer.load('vb', 'vbscript')
  62. local vbs_start_rule = #(P('<script') * (P(function(input, index)
  63. if input:find('^%s+language%s*=%s*(["\'])[vV][bB][sS]cript%1', index) then return true end
  64. end) + '>')) * embed_start_tag -- <script language="vbscript">
  65. local vbs_end_rule = #P('</script>') * embed_end_tag -- </script>
  66. lex:embed(vbs, vbs_start_rule, vbs_end_rule)
  67. lexer.property['scintillua.comment'] = '<!--|-->'
  68. lexer.property['scintillua.angle.braces'] = '1'
  69. return lex