elm.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. """
  2. pygments.lexers.elm
  3. ~~~~~~~~~~~~~~~~~~~
  4. Lexer for the Elm programming language.
  5. :copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. from pygments.lexer import RegexLexer, words, include
  9. from pygments.token import Comment, Keyword, Name, Number, Punctuation, String, Text
  10. __all__ = ['ElmLexer']
  11. class ElmLexer(RegexLexer):
  12. """
  13. For `Elm <http://elm-lang.org/>`_ source code.
  14. .. versionadded:: 2.1
  15. """
  16. name = 'Elm'
  17. aliases = ['elm']
  18. filenames = ['*.elm']
  19. mimetypes = ['text/x-elm']
  20. validName = r'[a-z_][a-zA-Z0-9_\']*'
  21. specialName = r'^main '
  22. builtinOps = (
  23. '~', '||', '|>', '|', '`', '^', '\\', '\'', '>>', '>=', '>', '==',
  24. '=', '<~', '<|', '<=', '<<', '<-', '<', '::', ':', '/=', '//', '/',
  25. '..', '.', '->', '-', '++', '+', '*', '&&', '%',
  26. )
  27. reservedWords = words((
  28. 'alias', 'as', 'case', 'else', 'if', 'import', 'in',
  29. 'let', 'module', 'of', 'port', 'then', 'type', 'where',
  30. ), suffix=r'\b')
  31. tokens = {
  32. 'root': [
  33. # Comments
  34. (r'\{-', Comment.Multiline, 'comment'),
  35. (r'--.*', Comment.Single),
  36. # Whitespace
  37. (r'\s+', Text),
  38. # Strings
  39. (r'"', String, 'doublequote'),
  40. # Modules
  41. (r'^\s*module\s*', Keyword.Namespace, 'imports'),
  42. # Imports
  43. (r'^\s*import\s*', Keyword.Namespace, 'imports'),
  44. # Shaders
  45. (r'\[glsl\|.*', Name.Entity, 'shader'),
  46. # Keywords
  47. (reservedWords, Keyword.Reserved),
  48. # Types
  49. (r'[A-Z][a-zA-Z0-9_]*', Keyword.Type),
  50. # Main
  51. (specialName, Keyword.Reserved),
  52. # Prefix Operators
  53. (words((builtinOps), prefix=r'\(', suffix=r'\)'), Name.Function),
  54. # Infix Operators
  55. (words(builtinOps), Name.Function),
  56. # Numbers
  57. include('numbers'),
  58. # Variable Names
  59. (validName, Name.Variable),
  60. # Parens
  61. (r'[,()\[\]{}]', Punctuation),
  62. ],
  63. 'comment': [
  64. (r'-(?!\})', Comment.Multiline),
  65. (r'\{-', Comment.Multiline, 'comment'),
  66. (r'[^-}]', Comment.Multiline),
  67. (r'-\}', Comment.Multiline, '#pop'),
  68. ],
  69. 'doublequote': [
  70. (r'\\u[0-9a-fA-F]{4}', String.Escape),
  71. (r'\\[nrfvb\\"]', String.Escape),
  72. (r'[^"]', String),
  73. (r'"', String, '#pop'),
  74. ],
  75. 'imports': [
  76. (r'\w+(\.\w+)*', Name.Class, '#pop'),
  77. ],
  78. 'numbers': [
  79. (r'_?\d+\.(?=\d+)', Number.Float),
  80. (r'_?\d+', Number.Integer),
  81. ],
  82. 'shader': [
  83. (r'\|(?!\])', Name.Entity),
  84. (r'\|\]', Name.Entity, '#pop'),
  85. (r'.*\n', Name.Entity),
  86. ],
  87. }