elm.py 2.9 KB

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