nimrod.py 5.0 KB

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