stata.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. """
  2. pygments.lexers.stata
  3. ~~~~~~~~~~~~~~~~~~~~~
  4. Lexer for Stata
  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, default, include, words
  10. from pygments.token import Comment, Keyword, Name, Number, \
  11. String, Text, Operator
  12. from pygments.lexers._stata_builtins import builtins_base, builtins_functions
  13. __all__ = ['StataLexer']
  14. class StataLexer(RegexLexer):
  15. """
  16. For `Stata <http://www.stata.com/>`_ do files.
  17. .. versionadded:: 2.2
  18. """
  19. # Syntax based on
  20. # - http://fmwww.bc.edu/RePEc/bocode/s/synlightlist.ado
  21. # - https://github.com/isagalaev/highlight.js/blob/master/src/languages/stata.js
  22. # - https://github.com/jpitblado/vim-stata/blob/master/syntax/stata.vim
  23. name = 'Stata'
  24. aliases = ['stata', 'do']
  25. filenames = ['*.do', '*.ado']
  26. mimetypes = ['text/x-stata', 'text/stata', 'application/x-stata']
  27. flags = re.MULTILINE | re.DOTALL
  28. tokens = {
  29. 'root': [
  30. include('comments'),
  31. include('strings'),
  32. include('macros'),
  33. include('numbers'),
  34. include('keywords'),
  35. include('operators'),
  36. include('format'),
  37. (r'.', Text),
  38. ],
  39. # Comments are a complicated beast in Stata because they can be
  40. # nested and there are a few corner cases with that. See:
  41. # - github.com/kylebarron/language-stata/issues/90
  42. # - statalist.org/forums/forum/general-stata-discussion/general/1448244
  43. 'comments': [
  44. (r'(^//|(?<=\s)//)(?!/)', Comment.Single, 'comments-double-slash'),
  45. (r'^\s*\*', Comment.Single, 'comments-star'),
  46. (r'/\*', Comment.Multiline, 'comments-block'),
  47. (r'(^///|(?<=\s)///)', Comment.Special, 'comments-triple-slash')
  48. ],
  49. 'comments-block': [
  50. (r'/\*', Comment.Multiline, '#push'),
  51. # this ends and restarts a comment block. but need to catch this so
  52. # that it doesn\'t start _another_ level of comment blocks
  53. (r'\*/\*', Comment.Multiline),
  54. (r'(\*/\s+\*(?!/)[^\n]*)|(\*/)', Comment.Multiline, '#pop'),
  55. # Match anything else as a character inside the comment
  56. (r'.', Comment.Multiline),
  57. ],
  58. 'comments-star': [
  59. (r'///.*?\n', Comment.Single,
  60. ('#pop', 'comments-triple-slash')),
  61. (r'(^//|(?<=\s)//)(?!/)', Comment.Single,
  62. ('#pop', 'comments-double-slash')),
  63. (r'/\*', Comment.Multiline, 'comments-block'),
  64. (r'.(?=\n)', Comment.Single, '#pop'),
  65. (r'.', Comment.Single),
  66. ],
  67. 'comments-triple-slash': [
  68. (r'\n', Comment.Special, '#pop'),
  69. # A // breaks out of a comment for the rest of the line
  70. (r'//.*?(?=\n)', Comment.Single, '#pop'),
  71. (r'.', Comment.Special),
  72. ],
  73. 'comments-double-slash': [
  74. (r'\n', Text, '#pop'),
  75. (r'.', Comment.Single),
  76. ],
  77. # `"compound string"' and regular "string"; note the former are
  78. # nested.
  79. 'strings': [
  80. (r'`"', String, 'string-compound'),
  81. (r'(?<!`)"', String, 'string-regular'),
  82. ],
  83. 'string-compound': [
  84. (r'`"', String, '#push'),
  85. (r'"\'', String, '#pop'),
  86. (r'\\\\|\\"|\\\$|\\`|\\\n', String.Escape),
  87. include('macros'),
  88. (r'.', String)
  89. ],
  90. 'string-regular': [
  91. (r'(")(?!\')|(?=\n)', String, '#pop'),
  92. (r'\\\\|\\"|\\\$|\\`|\\\n', String.Escape),
  93. include('macros'),
  94. (r'.', String)
  95. ],
  96. # A local is usually
  97. # `\w{0,31}'
  98. # `:extended macro'
  99. # `=expression'
  100. # `[rsen](results)'
  101. # `(++--)scalar(++--)'
  102. #
  103. # However, there are all sorts of weird rules wrt edge
  104. # cases. Instead of writing 27 exceptions, anything inside
  105. # `' is a local.
  106. #
  107. # A global is more restricted, so we do follow rules. Note only
  108. # locals explicitly enclosed ${} can be nested.
  109. 'macros': [
  110. (r'\$(\{|(?=[$`]))', Name.Variable.Global, 'macro-global-nested'),
  111. (r'\$', Name.Variable.Global, 'macro-global-name'),
  112. (r'`', Name.Variable, 'macro-local'),
  113. ],
  114. 'macro-local': [
  115. (r'`', Name.Variable, '#push'),
  116. (r"'", Name.Variable, '#pop'),
  117. (r'\$(\{|(?=[$`]))', Name.Variable.Global, 'macro-global-nested'),
  118. (r'\$', Name.Variable.Global, 'macro-global-name'),
  119. (r'.', Name.Variable), # fallback
  120. ],
  121. 'macro-global-nested': [
  122. (r'\$(\{|(?=[$`]))', Name.Variable.Global, '#push'),
  123. (r'\}', Name.Variable.Global, '#pop'),
  124. (r'\$', Name.Variable.Global, 'macro-global-name'),
  125. (r'`', Name.Variable, 'macro-local'),
  126. (r'\w', Name.Variable.Global), # fallback
  127. default('#pop'),
  128. ],
  129. 'macro-global-name': [
  130. (r'\$(\{|(?=[$`]))', Name.Variable.Global, 'macro-global-nested', '#pop'),
  131. (r'\$', Name.Variable.Global, 'macro-global-name', '#pop'),
  132. (r'`', Name.Variable, 'macro-local', '#pop'),
  133. (r'\w{1,32}', Name.Variable.Global, '#pop'),
  134. ],
  135. # Built in functions and statements
  136. 'keywords': [
  137. (words(builtins_functions, prefix = r'\b', suffix = r'(?=\()'),
  138. Name.Function),
  139. (words(builtins_base, prefix = r'(^\s*|\s)', suffix = r'\b'),
  140. Keyword),
  141. ],
  142. # http://www.stata.com/help.cgi?operators
  143. 'operators': [
  144. (r'-|==|<=|>=|<|>|&|!=', Operator),
  145. (r'\*|\+|\^|/|!|~|==|~=', Operator)
  146. ],
  147. # Stata numbers
  148. 'numbers': [
  149. # decimal number
  150. (r'\b[+-]?([0-9]+(\.[0-9]+)?|\.[0-9]+|\.)([eE][+-]?[0-9]+)?[i]?\b',
  151. Number),
  152. ],
  153. # Stata formats
  154. 'format': [
  155. (r'%-?\d{1,2}(\.\d{1,2})?[gfe]c?', Name.Other),
  156. (r'%(21x|16H|16L|8H|8L)', Name.Other),
  157. (r'%-?(tc|tC|td|tw|tm|tq|th|ty|tg)\S{0,32}', Name.Other),
  158. (r'%[-~]?\d{1,4}s', Name.Other),
  159. ]
  160. }