stata.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # -*- coding: utf-8 -*-
  2. """
  3. pygments.lexers.stata
  4. ~~~~~~~~~~~~~~~~~~~~~
  5. Lexer for Stata
  6. :copyright: Copyright 2006-2017 by the Pygments team, see AUTHORS.
  7. :license: BSD, see LICENSE for details.
  8. """
  9. from pygments.lexer import RegexLexer, 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. # - http://github.com/isagalaev/highlight.js/blob/master/src/languages/stata.js
  22. # - http://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. tokens = {
  28. 'root': [
  29. include('comments'),
  30. include('vars-strings'),
  31. include('numbers'),
  32. include('keywords'),
  33. (r'.', Text),
  34. ],
  35. # Global and local macros; regular and special strings
  36. 'vars-strings': [
  37. (r'\$[\w{]', Name.Variable.Global, 'var_validglobal'),
  38. (r'`\w{0,31}\'', Name.Variable),
  39. (r'"', String, 'string_dquote'),
  40. (r'`"', String, 'string_mquote'),
  41. ],
  42. # For either string type, highlight macros as macros
  43. 'string_dquote': [
  44. (r'"', String, '#pop'),
  45. (r'\\\\|\\"|\\\n', String.Escape),
  46. (r'\$', Name.Variable.Global, 'var_validglobal'),
  47. (r'`', Name.Variable, 'var_validlocal'),
  48. (r'[^$`"\\]+', String),
  49. (r'[$"\\]', String),
  50. ],
  51. 'string_mquote': [
  52. (r'"\'', String, '#pop'),
  53. (r'\\\\|\\"|\\\n', String.Escape),
  54. (r'\$', Name.Variable.Global, 'var_validglobal'),
  55. (r'`', Name.Variable, 'var_validlocal'),
  56. (r'[^$`"\\]+', String),
  57. (r'[$"\\]', String),
  58. ],
  59. 'var_validglobal': [
  60. (r'\{\w{0,32}\}', Name.Variable.Global, '#pop'),
  61. (r'\w{1,32}', Name.Variable.Global, '#pop'),
  62. ],
  63. 'var_validlocal': [
  64. (r'\w{0,31}\'', Name.Variable, '#pop'),
  65. ],
  66. # * only OK at line start, // OK anywhere
  67. 'comments': [
  68. (r'^\s*\*.*$', Comment),
  69. (r'//.*', Comment.Single),
  70. (r'/\*.*?\*/', Comment.Multiline),
  71. (r'/[*](.|\n)*?[*]/', Comment.Multiline),
  72. ],
  73. # Built in functions and statements
  74. 'keywords': [
  75. (words(builtins_functions, prefix = r'\b', suffix = r'\('),
  76. Name.Function),
  77. (words(builtins_base, prefix = r'(^\s*|\s)', suffix = r'\b'),
  78. Keyword),
  79. ],
  80. # http://www.stata.com/help.cgi?operators
  81. 'operators': [
  82. (r'-|==|<=|>=|<|>|&|!=', Operator),
  83. (r'\*|\+|\^|/|!|~|==|~=', Operator)
  84. ],
  85. # Stata numbers
  86. 'numbers': [
  87. # decimal number
  88. (r'\b[+-]?([0-9]+(\.[0-9]+)?|\.[0-9]+|\.)([eE][+-]?[0-9]+)?[i]?\b',
  89. Number),
  90. ],
  91. # Stata formats
  92. 'format': [
  93. (r'%-?\d{1,2}(\.\d{1,2})?[gfe]c?', Name.Variable),
  94. (r'%(21x|16H|16L|8H|8L)', Name.Variable),
  95. (r'%-?(tc|tC|td|tw|tm|tq|th|ty|tg).{0,32}', Name.Variable),
  96. (r'%[-~]?\d{1,4}s', Name.Variable),
  97. ]
  98. }