make.py 7.2 KB

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