nimrod.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. """
  2. pygments.lexers.nimrod
  3. ~~~~~~~~~~~~~~~~~~~~~~
  4. Lexer for the Nim language (formerly known as Nimrod).
  5. :copyright: Copyright 2006-2024 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. import re
  9. from pygments.lexer import RegexLexer, include, default, bygroups
  10. from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
  11. Number, Punctuation, Error
  12. __all__ = ['NimrodLexer']
  13. class NimrodLexer(RegexLexer):
  14. """
  15. For Nim source code.
  16. """
  17. name = 'Nimrod'
  18. url = 'http://nim-lang.org/'
  19. aliases = ['nimrod', 'nim']
  20. filenames = ['*.nim', '*.nimrod']
  21. mimetypes = ['text/x-nim']
  22. version_added = '1.5'
  23. flags = re.MULTILINE | re.IGNORECASE
  24. def underscorize(words):
  25. newWords = []
  26. new = []
  27. for word in words:
  28. for ch in word:
  29. new.append(ch)
  30. new.append("_?")
  31. newWords.append(''.join(new))
  32. new = []
  33. return "|".join(newWords)
  34. keywords = [
  35. 'addr', 'and', 'as', 'asm', 'bind', 'block', 'break', 'case',
  36. 'cast', 'concept', 'const', 'continue', 'converter', 'defer', 'discard',
  37. 'distinct', 'div', 'do', 'elif', 'else', 'end', 'enum', 'except',
  38. 'export', 'finally', 'for', 'if', 'in', 'yield', 'interface',
  39. 'is', 'isnot', 'iterator', 'let', 'mixin', 'mod',
  40. 'not', 'notin', 'object', 'of', 'or', 'out', 'ptr', 'raise',
  41. 'ref', 'return', 'shl', 'shr', 'static', 'try',
  42. 'tuple', 'type', 'using', 'when', 'while', 'xor'
  43. ]
  44. keywordsPseudo = [
  45. 'nil', 'true', 'false'
  46. ]
  47. opWords = [
  48. 'and', 'or', 'not', 'xor', 'shl', 'shr', 'div', 'mod', 'in',
  49. 'notin', 'is', 'isnot'
  50. ]
  51. types = [
  52. 'int', 'int8', 'int16', 'int32', 'int64', 'float', 'float32', 'float64',
  53. 'bool', 'char', 'range', 'array', 'seq', 'set', 'string'
  54. ]
  55. tokens = {
  56. 'root': [
  57. # Comments
  58. (r'##\[', String.Doc, 'doccomment'),
  59. (r'##.*$', String.Doc),
  60. (r'#\[', Comment.Multiline, 'comment'),
  61. (r'#.*$', Comment),
  62. # Pragmas
  63. (r'\{\.', String.Other, 'pragma'),
  64. # Operators
  65. (r'[*=><+\-/@$~&%!?|\\\[\]]', Operator),
  66. (r'\.\.|\.|,|\[\.|\.\]|\{\.|\.\}|\(\.|\.\)|\{|\}|\(|\)|:|\^|`|;',
  67. Punctuation),
  68. # Case statement branch
  69. (r'(\n\s*)(of)(\s)', bygroups(Text.Whitespace, Keyword,
  70. Text.Whitespace), 'casebranch'),
  71. # Strings
  72. (r'(?:[\w]+)"', String, 'rdqs'),
  73. (r'"""', String.Double, 'tdqs'),
  74. ('"', String, 'dqs'),
  75. # Char
  76. ("'", String.Char, 'chars'),
  77. # Keywords
  78. (rf'({underscorize(opWords)})\b', Operator.Word),
  79. (r'(proc|func|method|macro|template)(\s)(?![(\[\]])',
  80. bygroups(Keyword, Text.Whitespace), 'funcname'),
  81. (rf'({underscorize(keywords)})\b', Keyword),
  82. (r'({})\b'.format(underscorize(['from', 'import', 'include', 'export'])),
  83. Keyword.Namespace),
  84. (r'(v_?a_?r)\b', Keyword.Declaration),
  85. (rf'({underscorize(types)})\b', Name.Builtin),
  86. (rf'({underscorize(keywordsPseudo)})\b', Keyword.Pseudo),
  87. # Identifiers
  88. (r'\b((?![_\d])\w)(((?!_)\w)|(_(?!_)\w))*', Name),
  89. # Numbers
  90. (r'[0-9][0-9_]*(?=([e.]|\'f(32|64)))',
  91. Number.Float, ('float-suffix', 'float-number')),
  92. (r'0x[a-f0-9][a-f0-9_]*', Number.Hex, 'int-suffix'),
  93. (r'0b[01][01_]*', Number.Bin, 'int-suffix'),
  94. (r'0o[0-7][0-7_]*', Number.Oct, 'int-suffix'),
  95. (r'[0-9][0-9_]*', Number.Integer, 'int-suffix'),
  96. # Whitespace
  97. (r'\s+', Text.Whitespace),
  98. (r'.+$', Error),
  99. ],
  100. 'chars': [
  101. (r'\\([\\abcefnrtvl"\']|x[a-f0-9]{2}|[0-9]{1,3})', String.Escape),
  102. (r"'", String.Char, '#pop'),
  103. (r".", String.Char)
  104. ],
  105. 'strings': [
  106. (r'(?<!\$)\$(\d+|#|\w+)+', String.Interpol),
  107. (r'[^\\\'"$\n]+', String),
  108. # quotes, dollars and backslashes must be parsed one at a time
  109. (r'[\'"\\]', String),
  110. # unhandled string formatting sign
  111. (r'\$', String)
  112. # newlines are an error (use "nl" state)
  113. ],
  114. 'doccomment': [
  115. (r'[^\]#]+', String.Doc),
  116. (r'##\[', String.Doc, '#push'),
  117. (r'\]##', String.Doc, '#pop'),
  118. (r'[\]#]', String.Doc),
  119. ],
  120. 'comment': [
  121. (r'[^\]#]+', Comment.Multiline),
  122. (r'#\[', Comment.Multiline, '#push'),
  123. (r'\]#', Comment.Multiline, '#pop'),
  124. (r'[\]#]', Comment.Multiline),
  125. ],
  126. 'dqs': [
  127. (r'\\([\\abcefnrtvl"\']|\n|x[a-f0-9]{2}|[0-9]{1,3})',
  128. String.Escape),
  129. (r'"', String, '#pop'),
  130. include('strings')
  131. ],
  132. 'rdqs': [
  133. (r'"(?!")', String, '#pop'),
  134. (r'""', String.Escape),
  135. include('strings')
  136. ],
  137. 'tdqs': [
  138. (r'"""', String.Double, '#pop'),
  139. include('strings'),
  140. (r'\n', String.Double)
  141. ],
  142. 'funcname': [
  143. (r'((?![\d_])\w)(((?!_)\w)|(_(?!_)\w))*', Name.Function, '#pop'),
  144. (r'`.+`', Name.Function, '#pop')
  145. ],
  146. 'nl': [
  147. (r'\n', String)
  148. ],
  149. 'float-number': [
  150. (r'\.(?!\.)[0-9_]*[f]*', Number.Float),
  151. (r'e[+-]?[0-9][0-9_]*', Number.Float),
  152. default('#pop')
  153. ],
  154. 'float-suffix': [
  155. (r'\'f(32|64)', Number.Float),
  156. default('#pop')
  157. ],
  158. 'int-suffix': [
  159. (r'\'i(32|64)', Number.Integer.Long),
  160. (r'\'i(8|16)', Number.Integer),
  161. default('#pop')
  162. ],
  163. 'casebranch': [
  164. (r',', Punctuation),
  165. (r'[\n ]+', Text.Whitespace),
  166. (r':', Operator, '#pop'),
  167. (r'\w+|[^:]', Name.Label),
  168. ],
  169. 'pragma': [
  170. (r'[:,]', Text),
  171. (r'[\n ]+', Text.Whitespace),
  172. (r'\.\}', String.Other, '#pop'),
  173. (r'\w+|\W+|[^.}]', String.Other),
  174. ],
  175. }