|
1 # -*- coding: utf-8 -*- |
|
2 """ |
|
3 pygments.lexers.stata |
|
4 ~~~~~~~~~~~~~~~~~~~~~ |
|
5 |
|
6 Lexer for Stata |
|
7 |
|
8 :copyright: Copyright 2006-2017 by the Pygments team, see AUTHORS. |
|
9 :license: BSD, see LICENSE for details. |
|
10 """ |
|
11 |
|
12 from pygments.lexer import RegexLexer, include, words |
|
13 from pygments.token import Comment, Keyword, Name, Number, \ |
|
14 String, Text, Operator |
|
15 |
|
16 from pygments.lexers._stata_builtins import builtins_base, builtins_functions |
|
17 |
|
18 __all__ = ['StataLexer'] |
|
19 |
|
20 |
|
21 class StataLexer(RegexLexer): |
|
22 """ |
|
23 For `Stata <http://www.stata.com/>`_ do files. |
|
24 |
|
25 .. versionadded:: 2.2 |
|
26 """ |
|
27 # Syntax based on |
|
28 # - http://fmwww.bc.edu/RePEc/bocode/s/synlightlist.ado |
|
29 # - http://github.com/isagalaev/highlight.js/blob/master/src/languages/stata.js |
|
30 # - http://github.com/jpitblado/vim-stata/blob/master/syntax/stata.vim |
|
31 |
|
32 name = 'Stata' |
|
33 aliases = ['stata', 'do'] |
|
34 filenames = ['*.do', '*.ado'] |
|
35 mimetypes = ['text/x-stata', 'text/stata', 'application/x-stata'] |
|
36 |
|
37 tokens = { |
|
38 'root': [ |
|
39 include('comments'), |
|
40 include('vars-strings'), |
|
41 include('numbers'), |
|
42 include('keywords'), |
|
43 (r'.', Text), |
|
44 ], |
|
45 # Global and local macros; regular and special strings |
|
46 'vars-strings': [ |
|
47 (r'\$[\w{]', Name.Variable.Global, 'var_validglobal'), |
|
48 (r'`\w{0,31}\'', Name.Variable), |
|
49 (r'"', String, 'string_dquote'), |
|
50 (r'`"', String, 'string_mquote'), |
|
51 ], |
|
52 # For either string type, highlight macros as macros |
|
53 'string_dquote': [ |
|
54 (r'"', String, '#pop'), |
|
55 (r'\\\\|\\"|\\\n', String.Escape), |
|
56 (r'\$', Name.Variable.Global, 'var_validglobal'), |
|
57 (r'`', Name.Variable, 'var_validlocal'), |
|
58 (r'[^$`"\\]+', String), |
|
59 (r'[$"\\]', String), |
|
60 ], |
|
61 'string_mquote': [ |
|
62 (r'"\'', String, '#pop'), |
|
63 (r'\\\\|\\"|\\\n', String.Escape), |
|
64 (r'\$', Name.Variable.Global, 'var_validglobal'), |
|
65 (r'`', Name.Variable, 'var_validlocal'), |
|
66 (r'[^$`"\\]+', String), |
|
67 (r'[$"\\]', String), |
|
68 ], |
|
69 'var_validglobal': [ |
|
70 (r'\{\w{0,32}\}', Name.Variable.Global, '#pop'), |
|
71 (r'\w{1,32}', Name.Variable.Global, '#pop'), |
|
72 ], |
|
73 'var_validlocal': [ |
|
74 (r'\w{0,31}\'', Name.Variable, '#pop'), |
|
75 ], |
|
76 # * only OK at line start, // OK anywhere |
|
77 'comments': [ |
|
78 (r'^\s*\*.*$', Comment), |
|
79 (r'//.*', Comment.Single), |
|
80 (r'/\*.*?\*/', Comment.Multiline), |
|
81 (r'/[*](.|\n)*?[*]/', Comment.Multiline), |
|
82 ], |
|
83 # Built in functions and statements |
|
84 'keywords': [ |
|
85 (words(builtins_functions, prefix = r'\b', suffix = r'\('), |
|
86 Name.Function), |
|
87 (words(builtins_base, prefix = r'(^\s*|\s)', suffix = r'\b'), |
|
88 Keyword), |
|
89 ], |
|
90 # http://www.stata.com/help.cgi?operators |
|
91 'operators': [ |
|
92 (r'-|==|<=|>=|<|>|&|!=', Operator), |
|
93 (r'\*|\+|\^|/|!|~|==|~=', Operator) |
|
94 ], |
|
95 # Stata numbers |
|
96 'numbers': [ |
|
97 # decimal number |
|
98 (r'\b[+-]?([0-9]+(\.[0-9]+)?|\.[0-9]+|\.)([eE][+-]?[0-9]+)?[i]?\b', |
|
99 Number), |
|
100 ], |
|
101 # Stata formats |
|
102 'format': [ |
|
103 (r'%-?\d{1,2}(\.\d{1,2})?[gfe]c?', Name.Variable), |
|
104 (r'%(21x|16H|16L|8H|8L)', Name.Variable), |
|
105 (r'%-?(tc|tC|td|tw|tm|tq|th|ty|tg).{0,32}', Name.Variable), |
|
106 (r'%[-~]?\d{1,4}s', Name.Variable), |
|
107 ] |
|
108 } |