purescript.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. """
  2. pygments.lexers.purescript
  3. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. Lexer for the PureScript language.
  5. :copyright: Copyright 2006-present by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. from pygments.lexer import RegexLexer, bygroups, default
  9. from pygments.token import Comment, Operator, Keyword, Name, String, \
  10. Number, Punctuation, Whitespace
  11. from pygments import unistring as uni
  12. __all__ = ['PureScriptLexer']
  13. class PureScriptLexer(RegexLexer):
  14. """
  15. A lexer for the PureScript language.
  16. """
  17. name = 'PureScript'
  18. url = 'https://www.purescript.org/'
  19. aliases = ['purescript', 'purs']
  20. filenames = ['*.purs']
  21. mimetypes = ['text/x-purescript']
  22. version_added = '2.21'
  23. reserved = ('ado', 'case', 'class', 'data', 'derive', 'do', 'else',
  24. 'false', 'forall', 'foreign', 'if', 'in', 'infix[lr]?',
  25. 'instance', 'let', 'newtype', 'of',
  26. 'then', 'true', 'type', 'where', '_')
  27. tokens = {
  28. 'root': [
  29. # Whitespace
  30. (r'\s+', Whitespace),
  31. # Single-line comments
  32. (r'--.*?$', Comment.Single),
  33. # Multi-line comments
  34. (r'\{-', Comment.Multiline, 'comment'),
  35. # Lexemes:
  36. # Identifiers
  37. (r"\bimport(?!')\b", Keyword.Reserved, 'import'),
  38. (r"\bmodule(?!')\b", Keyword.Reserved, 'module'),
  39. (r'\b({})(?!\')\b'.format('|'.join(reserved)), Keyword.Reserved),
  40. # Typed holes
  41. (r'\?\w[\w\']*', Name.Variable),
  42. # Character literal (before names to avoid confusion)
  43. (r"'[^\\]'", String.Char),
  44. # Identifiers
  45. (r'^[_' + uni.Ll + r'][\w\']*', Name.Function),
  46. (r"[_" + uni.Ll + r"][\w']*", Name),
  47. (r'[' + uni.Lu + r'][\w\']*', Keyword.Type),
  48. # Backtick operator
  49. (r'`', Punctuation),
  50. # Operators
  51. (r'\\(?![' + uni.Sm + uni.So + uni.Sc +
  52. r':!#%&*.\\/?@^-]+)', Name.Function), # lambda
  53. # Unicode operators
  54. (r'[∀→←⇒∷]', Operator.Word),
  55. (r'(<-|::|->|=>|=)(?![' + uni.Sm + uni.So + uni.Sc +
  56. r':!#%&*.\\/?@^-]+)', Operator.Word),
  57. (r':[' + uni.Sm + uni.So + uni.Sc +
  58. r':!#%&*.\\/?@^-]*', Keyword.Type), # Constructor operators
  59. (r'[' + uni.Sm + uni.So + uni.Sc +
  60. r':!#%&*.\\/?@^-]+', Operator), # Other operators
  61. # Numbers
  62. (r'\d(_*\d)*_*e[+-]?\d(_*\d)*', Number.Float),
  63. (r'\d(_*\d)*\.\d(_*\d)*(_*e[+-]?\d(_*\d)*)?', Number.Float),
  64. (r'0x[\da-fA-F]+', Number.Hex),
  65. (r'\d(_*\d)*', Number.Integer),
  66. # Character/String Literals
  67. (r"'", String.Char, 'character'),
  68. (r'"""', String, 'rawstring'),
  69. (r'"', String, 'string'),
  70. # Special
  71. (r'[][(),;{}]', Punctuation),
  72. ],
  73. 'import': [
  74. (r'\s+', Whitespace),
  75. # import X as Y
  76. (r'([' + uni.Lu + r'][\w.]*)(\s+)(as)(\s+)([' + uni.Lu + r'][\w.]*)',
  77. bygroups(Name.Namespace, Whitespace, Keyword, Whitespace, Name),
  78. '#pop'),
  79. # import X hiding (functions)
  80. (r'([' + uni.Lu + r'][\w.]*)(\s+)(hiding)(\s+)(\()',
  81. bygroups(Name.Namespace, Whitespace, Keyword, Whitespace,
  82. Punctuation), 'funclist'),
  83. # import X (functions)
  84. (r'([' + uni.Lu + r'][\w.]*)(\s+)(\()',
  85. bygroups(Name.Namespace, Whitespace, Punctuation), 'funclist'),
  86. # 'as' after funclist: import X (foo) as Y
  87. (r'(as)(\s+)([' + uni.Lu + r'][\w.]*)',
  88. bygroups(Keyword, Whitespace, Name), '#pop'),
  89. # import X (bare module name, must start with uppercase)
  90. (r'[' + uni.Lu + r'][\w.]*', Name.Namespace, '#pop'),
  91. # pop when import is done
  92. default('#pop'),
  93. ],
  94. 'module': [
  95. (r'\s+', Whitespace),
  96. (r'([' + uni.Lu + r'][\w.]*)(\s+)(\()',
  97. bygroups(Name.Namespace, Whitespace, Punctuation), 'funclist'),
  98. (r'[' + uni.Lu + r'][\w.]*', Name.Namespace, '#pop'),
  99. default('#pop'),
  100. ],
  101. 'funclist': [
  102. (r'\s+', Whitespace),
  103. (r'(module)(\s+)([' + uni.Lu + r'][\w.]*)',
  104. bygroups(Keyword.Reserved, Whitespace, Name.Namespace)),
  105. (r"\b(class|type)(?!')\b", Keyword.Reserved),
  106. (r'[' + uni.Lu + r']\w*', Keyword.Type),
  107. (r'(_[\w\']+|[' + uni.Ll + r'][\w\']*)', Name.Function),
  108. (r'--.*?$', Comment.Single),
  109. (r'\{-', Comment.Multiline, 'comment'),
  110. (r',', Punctuation),
  111. (r'\.\.', Punctuation),
  112. (r'[' + uni.Sm + uni.So + uni.Sc +
  113. r':!#%&*.\\/?@^-]+', Operator),
  114. (r'\(', Punctuation, 'funclist'),
  115. (r'\)', Punctuation, '#pop'),
  116. ],
  117. 'comment': [
  118. (r'[^-{}]+', Comment.Multiline),
  119. (r'\{-', Comment.Multiline, '#push'),
  120. (r'-\}', Comment.Multiline, '#pop'),
  121. (r'[-{}]', Comment.Multiline),
  122. ],
  123. 'character': [
  124. (r"[^\\']'", String.Char, '#pop'),
  125. (r"\\", String.Escape, 'escape'),
  126. ("'", String.Char, '#pop'),
  127. ],
  128. 'string': [
  129. (r'[^\\"]+', String),
  130. (r"\\", String.Escape, 'escape'),
  131. ('"', String, '#pop'),
  132. ],
  133. 'rawstring': [
  134. (r'"""', String, '#pop'),
  135. (r'[^"]+', String),
  136. (r'"', String),
  137. ],
  138. 'escape': [
  139. (r'[nrt"\'\\]', String.Escape, '#pop'),
  140. (r'x[\da-fA-F]{1,6}', String.Escape, '#pop'),
  141. # String gaps: backslash-whitespace-backslash
  142. (r'(\s+)(\\)', bygroups(Whitespace, String.Escape), '#pop'),
  143. # Invalid escape: pop back so the next char is handled normally
  144. default('#pop'),
  145. ],
  146. }