jslt.py 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. """
  2. pygments.lexers.jslt
  3. ~~~~~~~~~~~~~~~~~~~~
  4. Lexers for the JSLT language
  5. :copyright: Copyright 2006-2024 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. from pygments.lexer import RegexLexer, combined, words
  9. from pygments.token import Comment, Keyword, Name, Number, Operator, \
  10. Punctuation, String, Whitespace
  11. __all__ = ['JSLTLexer']
  12. _WORD_END = r'(?=[^0-9A-Z_a-z-])'
  13. class JSLTLexer(RegexLexer):
  14. """
  15. For JSLT source.
  16. """
  17. name = 'JSLT'
  18. url = 'https://github.com/schibsted/jslt'
  19. filenames = ['*.jslt']
  20. aliases = ['jslt']
  21. mimetypes = ['text/x-jslt']
  22. version_added = '2.10'
  23. tokens = {
  24. 'root': [
  25. (r'[\t\n\f\r ]+', Whitespace),
  26. (r'//.*(\n|\Z)', Comment.Single),
  27. (r'-?(0|[1-9][0-9]*)', Number.Integer),
  28. (r'-?(0|[1-9][0-9]*)(.[0-9]+a)?([Ee][+-]?[0-9]+)', Number.Float),
  29. (r'"([^"\\]|\\.)*"', String.Double),
  30. (r'[(),:\[\]{}]', Punctuation),
  31. (r'(!=|[<=>]=?)', Operator),
  32. (r'[*+/|-]', Operator),
  33. (r'\.', Operator),
  34. (words(('import',), suffix=_WORD_END), Keyword.Namespace, combined('import-path', 'whitespace')),
  35. (words(('as',), suffix=_WORD_END), Keyword.Namespace, combined('import-alias', 'whitespace')),
  36. (words(('let',), suffix=_WORD_END), Keyword.Declaration, combined('constant', 'whitespace')),
  37. (words(('def',), suffix=_WORD_END), Keyword.Declaration, combined('function', 'whitespace')),
  38. (words(('false', 'null', 'true'), suffix=_WORD_END), Keyword.Constant),
  39. (words(('else', 'for', 'if'), suffix=_WORD_END), Keyword),
  40. (words(('and', 'or'), suffix=_WORD_END), Operator.Word),
  41. (words((
  42. 'all', 'any', 'array', 'boolean', 'capture', 'ceiling',
  43. 'contains', 'ends-with', 'error', 'flatten', 'floor',
  44. 'format-time', 'from-json', 'get-key', 'hash-int', 'index-of',
  45. 'is-array', 'is-boolean', 'is-decimal', 'is-integer',
  46. 'is-number', 'is-object', 'is-string', 'join', 'lowercase',
  47. 'max', 'min', 'mod', 'not', 'now', 'number', 'parse-time',
  48. 'parse-url', 'random', 'replace', 'round', 'sha256-hex', 'size',
  49. 'split', 'starts-with', 'string', 'sum', 'test', 'to-json',
  50. 'trim', 'uppercase', 'zip', 'zip-with-index', 'fallback'), suffix=_WORD_END),
  51. Name.Builtin),
  52. (r'[A-Z_a-z][0-9A-Z_a-z-]*:[A-Z_a-z][0-9A-Z_a-z-]*', Name.Function),
  53. (r'[A-Z_a-z][0-9A-Z_a-z-]*', Name),
  54. (r'\$[A-Z_a-z][0-9A-Z_a-z-]*', Name.Variable),
  55. ],
  56. 'constant': [
  57. (r'[A-Z_a-z][0-9A-Z_a-z-]*', Name.Variable, 'root'),
  58. ],
  59. 'function': [
  60. (r'[A-Z_a-z][0-9A-Z_a-z-]*', Name.Function, combined('function-parameter-list', 'whitespace')),
  61. ],
  62. 'function-parameter-list': [
  63. (r'\(', Punctuation, combined('function-parameters', 'whitespace')),
  64. ],
  65. 'function-parameters': [
  66. (r',', Punctuation),
  67. (r'\)', Punctuation, 'root'),
  68. (r'[A-Z_a-z][0-9A-Z_a-z-]*', Name.Variable),
  69. ],
  70. 'import-path': [
  71. (r'"([^"]|\\.)*"', String.Symbol, 'root'),
  72. ],
  73. 'import-alias': [
  74. (r'[A-Z_a-z][0-9A-Z_a-z-]*', Name.Namespace, 'root'),
  75. ],
  76. 'string': [
  77. (r'"', String.Double, '#pop'),
  78. (r'\\.', String.Escape),
  79. ],
  80. 'whitespace': [
  81. (r'[\t\n\f\r ]+', Whitespace),
  82. (r'//.*(\n|\Z)', Comment.Single),
  83. ]
  84. }