clean.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. """
  2. pygments.lexers.clean
  3. ~~~~~~~~~~~~~~~~~~~~~
  4. Lexer for the Clean language.
  5. :copyright: Copyright 2006-2021 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 (http://clean.cs.ru.nl/Clean).
  16. .. versionadded: 2.2
  17. """
  18. name = 'Clean'
  19. aliases = ['clean']
  20. filenames = ['*.icl', '*.dcl']
  21. keywords = (
  22. 'case', 'ccall', 'class', 'code', 'code inline', 'derive', 'export',
  23. 'foreign', 'generic', 'if', 'in', 'infix', 'infixl', 'infixr',
  24. 'instance', 'let', 'of', 'otherwise', 'special', 'stdcall', 'where',
  25. 'with')
  26. modulewords = ('implementation', 'definition', 'system')
  27. lowerId = r'[a-z`][\w`]*'
  28. upperId = r'[A-Z`][\w`]*'
  29. funnyId = r'[~@#$%\^?!+\-*<>\\/|&=:]+'
  30. scoreUpperId = r'_' + upperId
  31. scoreLowerId = r'_' + lowerId
  32. moduleId = r'[a-zA-Z_][a-zA-Z0-9_.`]+'
  33. classId = '|'.join([lowerId, upperId, funnyId])
  34. tokens = {
  35. 'root': [
  36. include('comments'),
  37. include('keywords'),
  38. include('module'),
  39. include('import'),
  40. include('whitespace'),
  41. include('literals'),
  42. include('operators'),
  43. include('delimiters'),
  44. include('names'),
  45. ],
  46. 'whitespace': [
  47. (r'\s+', Whitespace),
  48. ],
  49. 'comments': [
  50. (r'//.*\n', Comment.Single),
  51. (r'/\*', Comment.Multi, 'comments.in'),
  52. (r'/\*\*', Comment.Special, 'comments.in'),
  53. ],
  54. 'comments.in': [
  55. (r'\*\/', Comment.Multi, '#pop'),
  56. (r'/\*', Comment.Multi, '#push'),
  57. (r'[^*/]+', Comment.Multi),
  58. (r'\*(?!/)', Comment.Multi),
  59. (r'/', Comment.Multi),
  60. ],
  61. 'keywords': [
  62. (words(keywords, prefix=r'\b', suffix=r'\b'), Keyword),
  63. ],
  64. 'module': [
  65. (words(modulewords, prefix=r'\b', suffix=r'\b'), Keyword.Namespace),
  66. (r'\bmodule\b', Keyword.Namespace, 'module.name'),
  67. ],
  68. 'module.name': [
  69. include('whitespace'),
  70. (moduleId, Name.Class, '#pop'),
  71. ],
  72. 'import': [
  73. (r'\b(import)\b(\s*)', bygroups(Keyword, Whitespace), 'import.module'),
  74. (r'\b(from)\b(\s*)\b(' + moduleId + r')\b(\s*)\b(import)\b',
  75. bygroups(Keyword, Whitespace, Name.Class, Whitespace, Keyword),
  76. 'import.what'),
  77. ],
  78. 'import.module': [
  79. (r'\b(qualified)\b(\s*)', bygroups(Keyword, Whitespace)),
  80. (r'(\s*)\b(as)\b', bygroups(Whitespace, Keyword), ('#pop', 'import.module.as')),
  81. (moduleId, Name.Class),
  82. (r'(\s*)(,)(\s*)', bygroups(Whitespace, Punctuation, Whitespace)),
  83. (r'\s+', Whitespace),
  84. default('#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. }