clean.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. # -*- coding: utf-8 -*-
  2. """
  3. pygments.lexers.clean
  4. ~~~~~~~~~~~~~~~~~~~~~
  5. Lexer for the Clean 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 ExtendedRegexLexer, words, include, bygroups
  10. from pygments.token import Comment, Error, Keyword, Literal, Name, Number, \
  11. Operator, Punctuation, String, Whitespace
  12. __all__ = ['CleanLexer']
  13. class CleanLexer(ExtendedRegexLexer):
  14. """
  15. Lexer for the general purpose, state-of-the-art, pure and lazy functional
  16. programming language Clean (http://clean.cs.ru.nl/Clean).
  17. .. versionadded: 2.2
  18. """
  19. name = 'Clean'
  20. aliases = ['clean']
  21. filenames = ['*.icl', '*.dcl']
  22. keywords = (
  23. 'case', 'ccall', 'class', 'code', 'code inline', 'derive', 'export',
  24. 'foreign', 'generic', 'if', 'in', 'infix', 'infixl', 'infixr',
  25. 'instance', 'let', 'of', 'otherwise', 'special', 'stdcall', 'where',
  26. 'with')
  27. modulewords = ('implementation', 'definition', 'system')
  28. lowerId = r'[a-z`][\w\d`]*'
  29. upperId = r'[A-Z`][\w\d`]*'
  30. funnyId = r'[~@#\$%\^?!+\-*<>\\/|&=:]+'
  31. scoreUpperId = r'_' + upperId
  32. scoreLowerId = r'_' + lowerId
  33. moduleId = r'[a-zA-Z_][a-zA-Z0-9_.`]+'
  34. classId = '|'.join([lowerId, upperId, funnyId])
  35. tokens = {
  36. 'root': [
  37. include('comments'),
  38. include('keywords'),
  39. include('module'),
  40. include('import'),
  41. include('whitespace'),
  42. include('literals'),
  43. include('operators'),
  44. include('delimiters'),
  45. include('names'),
  46. ],
  47. 'whitespace': [
  48. (r'\s+', Whitespace),
  49. ],
  50. 'comments': [
  51. (r'//.*\n', Comment.Single),
  52. (r'/\*', Comment.Multi, 'comments.in'),
  53. (r'/\*\*', Comment.Special, 'comments.in'),
  54. ],
  55. 'comments.in': [
  56. (r'\*\/', Comment.Multi, '#pop'),
  57. (r'/\*', Comment.Multi, '#push'),
  58. (r'[^*/]+', Comment.Multi),
  59. (r'\*(?!/)', Comment.Multi),
  60. (r'/', Comment.Multi),
  61. ],
  62. 'keywords': [
  63. (words(keywords, prefix=r'\b', suffix=r'\b'), Keyword),
  64. ],
  65. 'module': [
  66. (words(modulewords, prefix=r'\b', suffix=r'\b'), Keyword.Namespace),
  67. (r'\bmodule\b', Keyword.Namespace, 'module.name'),
  68. ],
  69. 'module.name': [
  70. include('whitespace'),
  71. (moduleId, Name.Class, '#pop'),
  72. ],
  73. 'import': [
  74. (r'\b(import)\b(\s*)', bygroups(Keyword, Whitespace), 'import.module'),
  75. (r'\b(from)\b(\s*)\b(' + moduleId + r')\b(\s*)\b(import)\b',
  76. bygroups(Keyword, Whitespace, Name.Class, Whitespace, Keyword),
  77. 'import.what'),
  78. ],
  79. 'import.module': [
  80. (r'\b(qualified)\b(\s*)', bygroups(Keyword, Whitespace)),
  81. (r'(\s*)\b(as)\b', bygroups(Whitespace, Keyword), ('#pop', 'import.module.as')),
  82. (moduleId, Name.Class),
  83. (r'(\s*)(,)(\s*)', bygroups(Whitespace, Punctuation, Whitespace)),
  84. (r'\s*', Whitespace, '#pop'),
  85. ],
  86. 'import.module.as': [
  87. include('whitespace'),
  88. (lowerId, Name.Class, '#pop'),
  89. (upperId, Name.Class, '#pop'),
  90. ],
  91. 'import.what': [
  92. (r'\b(class)\b(\s+)(' + classId + r')',
  93. bygroups(Keyword, Whitespace, Name.Class), 'import.what.class'),
  94. (r'\b(instance)(\s+)(' + classId + r')(\s+)',
  95. bygroups(Keyword, Whitespace, Name.Class, Whitespace), 'import.what.instance'),
  96. (r'(::)(\s*)\b(' + upperId + r')\b',
  97. bygroups(Punctuation, Whitespace, Name.Class), 'import.what.type'),
  98. (r'\b(generic)\b(\s+)\b(' + lowerId + '|' + upperId + r')\b',
  99. bygroups(Keyword, Whitespace, Name)),
  100. include('names'),
  101. (r'(,)(\s+)', bygroups(Punctuation, Whitespace)),
  102. (r'$', Whitespace, '#pop'),
  103. include('whitespace'),
  104. ],
  105. 'import.what.class': [
  106. (r',', Punctuation, '#pop'),
  107. (r'\(', Punctuation, 'import.what.class.members'),
  108. (r'$', Whitespace, '#pop:2'),
  109. include('whitespace'),
  110. ],
  111. 'import.what.class.members': [
  112. (r',', Punctuation),
  113. (r'\.\.', Punctuation),
  114. (r'\)', Punctuation, '#pop'),
  115. include('names'),
  116. ],
  117. 'import.what.instance': [
  118. (r'[,)]', Punctuation, '#pop'),
  119. (r'\(', Punctuation, 'import.what.instance'),
  120. (r'$', Whitespace, '#pop:2'),
  121. include('whitespace'),
  122. include('names'),
  123. ],
  124. 'import.what.type': [
  125. (r',', Punctuation, '#pop'),
  126. (r'[({]', Punctuation, 'import.what.type.consesandfields'),
  127. (r'$', Whitespace, '#pop:2'),
  128. include('whitespace'),
  129. ],
  130. 'import.what.type.consesandfields': [
  131. (r',', Punctuation),
  132. (r'\.\.', Punctuation),
  133. (r'[)}]', Punctuation, '#pop'),
  134. include('names'),
  135. ],
  136. 'literals': [
  137. (r'\'([^\'\\]|\\(x[\da-fA-F]+|\d+|.))\'', Literal.Char),
  138. (r'[+~-]?0[0-7]+\b', Number.Oct),
  139. (r'[+~-]?\d+\.\d+(E[+-]?\d+)?', Number.Float),
  140. (r'[+~-]?\d+\b', Number.Integer),
  141. (r'[+~-]?0x[\da-fA-F]+\b', Number.Hex),
  142. (r'True|False', Literal),
  143. (r'"', String.Double, 'literals.stringd'),
  144. ],
  145. 'literals.stringd': [
  146. (r'[^\\"\n]+', String.Double),
  147. (r'"', String.Double, '#pop'),
  148. (r'\\.', String.Double),
  149. (r'[$\n]', Error, '#pop'),
  150. ],
  151. 'operators': [
  152. (r'[-~@#\$%\^?!+*<>\\/|&=:\.]+', Operator),
  153. (r'\b_+\b', Operator),
  154. ],
  155. 'delimiters': [
  156. (r'[,;(){}\[\]]', Punctuation),
  157. (r'(\')([\w`.]+)(\')',
  158. bygroups(Punctuation, Name.Class, Punctuation)),
  159. ],
  160. 'names': [
  161. (lowerId, Name),
  162. (scoreLowerId, Name),
  163. (funnyId, Name.Function),
  164. (upperId, Name.Class),
  165. (scoreUpperId, Name.Class),
  166. ]
  167. }