clean.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. """
  2. pygments.lexers.clean
  3. ~~~~~~~~~~~~~~~~~~~~~
  4. Lexer for the Clean language.
  5. :copyright: Copyright 2006-2023 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. 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`]*'
  29. upperId = r'[A-Z`][\w`]*'
  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.Multiline, 'comments.in'),
  53. (r'/\*\*', Comment.Special, 'comments.in'),
  54. ],
  55. 'comments.in': [
  56. (r'\*\/', Comment.Multiline, '#pop'),
  57. (r'/\*', Comment.Multiline, '#push'),
  58. (r'[^*/]+', Comment.Multiline),
  59. (r'\*(?!/)', Comment.Multiline),
  60. (r'/', Comment.Multiline),
  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),
  85. default('#pop'),
  86. ],
  87. 'import.module.as': [
  88. include('whitespace'),
  89. (lowerId, Name.Class, '#pop'),
  90. (upperId, Name.Class, '#pop'),
  91. ],
  92. 'import.what': [
  93. (r'\b(class)\b(\s+)(' + classId + r')',
  94. bygroups(Keyword, Whitespace, Name.Class), 'import.what.class'),
  95. (r'\b(instance)(\s+)(' + classId + r')(\s+)',
  96. bygroups(Keyword, Whitespace, Name.Class, Whitespace), 'import.what.instance'),
  97. (r'(::)(\s*)\b(' + upperId + r')\b',
  98. bygroups(Punctuation, Whitespace, Name.Class), 'import.what.type'),
  99. (r'\b(generic)\b(\s+)\b(' + lowerId + '|' + upperId + r')\b',
  100. bygroups(Keyword, Whitespace, Name)),
  101. include('names'),
  102. (r'(,)(\s+)', bygroups(Punctuation, Whitespace)),
  103. (r'$', Whitespace, '#pop'),
  104. include('whitespace'),
  105. ],
  106. 'import.what.class': [
  107. (r',', Punctuation, '#pop'),
  108. (r'\(', Punctuation, 'import.what.class.members'),
  109. (r'$', Whitespace, '#pop:2'),
  110. include('whitespace'),
  111. ],
  112. 'import.what.class.members': [
  113. (r',', Punctuation),
  114. (r'\.\.', Punctuation),
  115. (r'\)', Punctuation, '#pop'),
  116. include('names'),
  117. ],
  118. 'import.what.instance': [
  119. (r'[,)]', Punctuation, '#pop'),
  120. (r'\(', Punctuation, 'import.what.instance'),
  121. (r'$', Whitespace, '#pop:2'),
  122. include('whitespace'),
  123. include('names'),
  124. ],
  125. 'import.what.type': [
  126. (r',', Punctuation, '#pop'),
  127. (r'[({]', Punctuation, 'import.what.type.consesandfields'),
  128. (r'$', Whitespace, '#pop:2'),
  129. include('whitespace'),
  130. ],
  131. 'import.what.type.consesandfields': [
  132. (r',', Punctuation),
  133. (r'\.\.', Punctuation),
  134. (r'[)}]', Punctuation, '#pop'),
  135. include('names'),
  136. ],
  137. 'literals': [
  138. (r'\'([^\'\\]|\\(x[\da-fA-F]+|\d+|.))\'', Literal.Char),
  139. (r'[+~-]?0[0-7]+\b', Number.Oct),
  140. (r'[+~-]?\d+\.\d+(E[+-]?\d+)?', Number.Float),
  141. (r'[+~-]?\d+\b', Number.Integer),
  142. (r'[+~-]?0x[\da-fA-F]+\b', Number.Hex),
  143. (r'True|False', Literal),
  144. (r'"', String.Double, 'literals.stringd'),
  145. ],
  146. 'literals.stringd': [
  147. (r'[^\\"\n]+', String.Double),
  148. (r'"', String.Double, '#pop'),
  149. (r'\\.', String.Double),
  150. (r'[$\n]', Error, '#pop'),
  151. ],
  152. 'operators': [
  153. (r'[-~@#$%\^?!+*<>\\/|&=:.]+', Operator),
  154. (r'\b_+\b', Operator),
  155. ],
  156. 'delimiters': [
  157. (r'[,;(){}\[\]]', Punctuation),
  158. (r'(\')([\w`.]+)(\')',
  159. bygroups(Punctuation, Name.Class, Punctuation)),
  160. ],
  161. 'names': [
  162. (lowerId, Name),
  163. (scoreLowerId, Name),
  164. (funnyId, Name.Function),
  165. (upperId, Name.Class),
  166. (scoreUpperId, Name.Class),
  167. ]
  168. }