nimrod.py 5.1 KB

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