clean.py 6.3 KB

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