eric6/ThirdParty/Pygments/pygments/lexers/make.py

changeset 8258
82b608e352ec
parent 8257
28146736bbfc
child 8259
2bbec88047dd
equal deleted inserted replaced
8257:28146736bbfc 8258:82b608e352ec
1 # -*- coding: utf-8 -*-
2 """
3 pygments.lexers.make
4 ~~~~~~~~~~~~~~~~~~~~
5
6 Lexers for Makefiles and similar.
7
8 :copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
9 :license: BSD, see LICENSE for details.
10 """
11
12 import re
13
14 from pygments.lexer import Lexer, RegexLexer, include, bygroups, \
15 do_insertions, using
16 from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
17 Punctuation
18 from pygments.lexers.shell import BashLexer
19
20 __all__ = ['MakefileLexer', 'BaseMakefileLexer', 'CMakeLexer']
21
22
23 class MakefileLexer(Lexer):
24 """
25 Lexer for BSD and GNU make extensions (lenient enough to handle both in
26 the same file even).
27
28 *Rewritten in Pygments 0.10.*
29 """
30
31 name = 'Makefile'
32 aliases = ['make', 'makefile', 'mf', 'bsdmake']
33 filenames = ['*.mak', '*.mk', 'Makefile', 'makefile', 'Makefile.*', 'GNUmakefile']
34 mimetypes = ['text/x-makefile']
35
36 r_special = re.compile(
37 r'^(?:'
38 # BSD Make
39 r'\.\s*(include|undef|error|warning|if|else|elif|endif|for|endfor)|'
40 # GNU Make
41 r'\s*(ifeq|ifneq|ifdef|ifndef|else|endif|-?include|define|endef|:|vpath)|'
42 # GNU Automake
43 r'\s*(if|else|endif))(?=\s)')
44 r_comment = re.compile(r'^\s*@?#')
45
46 def get_tokens_unprocessed(self, text):
47 ins = []
48 lines = text.splitlines(True)
49 done = ''
50 lex = BaseMakefileLexer(**self.options)
51 backslashflag = False
52 for line in lines:
53 if self.r_special.match(line) or backslashflag:
54 ins.append((len(done), [(0, Comment.Preproc, line)]))
55 backslashflag = line.strip().endswith('\\')
56 elif self.r_comment.match(line):
57 ins.append((len(done), [(0, Comment, line)]))
58 else:
59 done += line
60 yield from do_insertions(ins, lex.get_tokens_unprocessed(done))
61
62 def analyse_text(text):
63 # Many makefiles have $(BIG_CAPS) style variables
64 if re.search(r'\$\([A-Z_]+\)', text):
65 return 0.1
66
67
68 class BaseMakefileLexer(RegexLexer):
69 """
70 Lexer for simple Makefiles (no preprocessing).
71
72 .. versionadded:: 0.10
73 """
74
75 name = 'Base Makefile'
76 aliases = ['basemake']
77 filenames = []
78 mimetypes = []
79
80 tokens = {
81 'root': [
82 # recipes (need to allow spaces because of expandtabs)
83 (r'^(?:[\t ]+.*\n|\n)+', using(BashLexer)),
84 # special variables
85 (r'\$[<@$+%?|*]', Keyword),
86 (r'\s+', Text),
87 (r'#.*?\n', Comment),
88 (r'(export)(\s+)(?=[\w${}\t -]+\n)',
89 bygroups(Keyword, Text), 'export'),
90 (r'export\s+', Keyword),
91 # assignment
92 (r'([\w${}().-]+)(\s*)([!?:+]?=)([ \t]*)((?:.*\\\n)+|.*\n)',
93 bygroups(Name.Variable, Text, Operator, Text, using(BashLexer))),
94 # strings
95 (r'"(\\\\|\\[^\\]|[^"\\])*"', String.Double),
96 (r"'(\\\\|\\[^\\]|[^'\\])*'", String.Single),
97 # targets
98 (r'([^\n:]+)(:+)([ \t]*)', bygroups(Name.Function, Operator, Text),
99 'block-header'),
100 # expansions
101 (r'\$\(', Keyword, 'expansion'),
102 ],
103 'expansion': [
104 (r'[^\w$().-]+', Text),
105 (r'[\w.-]+', Name.Variable),
106 (r'\$', Keyword),
107 (r'\(', Keyword, '#push'),
108 (r'\)', Keyword, '#pop'),
109 ],
110 'export': [
111 (r'[\w${}-]+', Name.Variable),
112 (r'\n', Text, '#pop'),
113 (r'\s+', Text),
114 ],
115 'block-header': [
116 (r'[,|]', Punctuation),
117 (r'#.*?\n', Comment, '#pop'),
118 (r'\\\n', Text), # line continuation
119 (r'\$\(', Keyword, 'expansion'),
120 (r'[a-zA-Z_]+', Name),
121 (r'\n', Text, '#pop'),
122 (r'.', Text),
123 ],
124 }
125
126
127 class CMakeLexer(RegexLexer):
128 """
129 Lexer for `CMake <http://cmake.org/Wiki/CMake>`_ files.
130
131 .. versionadded:: 1.2
132 """
133 name = 'CMake'
134 aliases = ['cmake']
135 filenames = ['*.cmake', 'CMakeLists.txt']
136 mimetypes = ['text/x-cmake']
137
138 tokens = {
139 'root': [
140 # (r'(ADD_CUSTOM_COMMAND|ADD_CUSTOM_TARGET|ADD_DEFINITIONS|'
141 # r'ADD_DEPENDENCIES|ADD_EXECUTABLE|ADD_LIBRARY|ADD_SUBDIRECTORY|'
142 # r'ADD_TEST|AUX_SOURCE_DIRECTORY|BUILD_COMMAND|BUILD_NAME|'
143 # r'CMAKE_MINIMUM_REQUIRED|CONFIGURE_FILE|CREATE_TEST_SOURCELIST|'
144 # r'ELSE|ELSEIF|ENABLE_LANGUAGE|ENABLE_TESTING|ENDFOREACH|'
145 # r'ENDFUNCTION|ENDIF|ENDMACRO|ENDWHILE|EXEC_PROGRAM|'
146 # r'EXECUTE_PROCESS|EXPORT_LIBRARY_DEPENDENCIES|FILE|FIND_FILE|'
147 # r'FIND_LIBRARY|FIND_PACKAGE|FIND_PATH|FIND_PROGRAM|FLTK_WRAP_UI|'
148 # r'FOREACH|FUNCTION|GET_CMAKE_PROPERTY|GET_DIRECTORY_PROPERTY|'
149 # r'GET_FILENAME_COMPONENT|GET_SOURCE_FILE_PROPERTY|'
150 # r'GET_TARGET_PROPERTY|GET_TEST_PROPERTY|IF|INCLUDE|'
151 # r'INCLUDE_DIRECTORIES|INCLUDE_EXTERNAL_MSPROJECT|'
152 # r'INCLUDE_REGULAR_EXPRESSION|INSTALL|INSTALL_FILES|'
153 # r'INSTALL_PROGRAMS|INSTALL_TARGETS|LINK_DIRECTORIES|'
154 # r'LINK_LIBRARIES|LIST|LOAD_CACHE|LOAD_COMMAND|MACRO|'
155 # r'MAKE_DIRECTORY|MARK_AS_ADVANCED|MATH|MESSAGE|OPTION|'
156 # r'OUTPUT_REQUIRED_FILES|PROJECT|QT_WRAP_CPP|QT_WRAP_UI|REMOVE|'
157 # r'REMOVE_DEFINITIONS|SEPARATE_ARGUMENTS|SET|'
158 # r'SET_DIRECTORY_PROPERTIES|SET_SOURCE_FILES_PROPERTIES|'
159 # r'SET_TARGET_PROPERTIES|SET_TESTS_PROPERTIES|SITE_NAME|'
160 # r'SOURCE_GROUP|STRING|SUBDIR_DEPENDS|SUBDIRS|'
161 # r'TARGET_LINK_LIBRARIES|TRY_COMPILE|TRY_RUN|UNSET|'
162 # r'USE_MANGLED_MESA|UTILITY_SOURCE|VARIABLE_REQUIRES|'
163 # r'VTK_MAKE_INSTANTIATOR|VTK_WRAP_JAVA|VTK_WRAP_PYTHON|'
164 # r'VTK_WRAP_TCL|WHILE|WRITE_FILE|'
165 # r'COUNTARGS)\b', Name.Builtin, 'args'),
166 (r'\b(\w+)([ \t]*)(\()', bygroups(Name.Builtin, Text,
167 Punctuation), 'args'),
168 include('keywords'),
169 include('ws')
170 ],
171 'args': [
172 (r'\(', Punctuation, '#push'),
173 (r'\)', Punctuation, '#pop'),
174 (r'(\$\{)(.+?)(\})', bygroups(Operator, Name.Variable, Operator)),
175 (r'(\$ENV\{)(.+?)(\})', bygroups(Operator, Name.Variable, Operator)),
176 (r'(\$<)(.+?)(>)', bygroups(Operator, Name.Variable, Operator)),
177 (r'(?s)".*?"', String.Double),
178 (r'\\\S+', String),
179 (r'[^)$"# \t\n]+', String),
180 (r'\n', Text), # explicitly legal
181 include('keywords'),
182 include('ws')
183 ],
184 'string': [
185
186 ],
187 'keywords': [
188 (r'\b(WIN32|UNIX|APPLE|CYGWIN|BORLAND|MINGW|MSVC|MSVC_IDE|MSVC60|'
189 r'MSVC70|MSVC71|MSVC80|MSVC90)\b', Keyword),
190 ],
191 'ws': [
192 (r'[ \t]+', Text),
193 (r'#.*\n', Comment),
194 ]
195 }
196
197 def analyse_text(text):
198 exp = (
199 r'^[ \t]*CMAKE_MINIMUM_REQUIRED[ \t]*'
200 r'\([ \t]*VERSION[ \t]*\d+(\.\d+)*[ \t]*'
201 r'([ \t]FATAL_ERROR)?[ \t]*\)[ \t]*'
202 r'(#[^\n]*)?$'
203 )
204 if re.search(exp, text, flags=re.MULTILINE | re.IGNORECASE):
205 return 0.8
206 return 0.0

eric ide

mercurial