cm_hl.js 976 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // custom mode
  2. 'use strict'
  3. import CodeMirror from 'codemirror'
  4. export default function () {
  5. CodeMirror.defineMode('hosts', function () {
  6. function tokenBase (stream) {
  7. if (stream.eatSpace()) return null
  8. let sol = stream.sol()
  9. let ch = stream.next()
  10. let s = stream.string
  11. if (ch === '#') {
  12. stream.skipToEnd()
  13. return 'comment'
  14. }
  15. if (!s.match(/^\s*([\d.]+|[\da-f:.%lo]+)\s+\w/i)) {
  16. return 'error'
  17. }
  18. if (sol && ch.match(/[\w.:%]/)) {
  19. stream.eatWhile(/[\w.:%]/)
  20. return 'ip'
  21. }
  22. return null
  23. }
  24. function tokenize (stream, state) {
  25. return (state.tokens[0] || tokenBase)(stream, state)
  26. }
  27. return {
  28. startState: function () {
  29. return {tokens: []}
  30. },
  31. token: function (stream, state) {
  32. return tokenize(stream, state)
  33. },
  34. lineComment: '#'
  35. }
  36. })
  37. //CodeMirror.defineMIME('text/x-hosts', 'hosts');
  38. }