syntax_parse.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. # Copyright (C) 2013 Google Inc.
  2. #
  3. # This file is part of YouCompleteMe.
  4. #
  5. # YouCompleteMe is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # YouCompleteMe is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with YouCompleteMe. If not, see <http://www.gnu.org/licenses/>.
  17. from __future__ import unicode_literals
  18. from __future__ import print_function
  19. from __future__ import division
  20. from __future__ import absolute_import
  21. from future import standard_library
  22. standard_library.install_aliases()
  23. from builtins import * # noqa
  24. from future.utils import itervalues
  25. import re
  26. import vim
  27. from ycm import vimsupport
  28. SYNTAX_GROUP_REGEX = re.compile(
  29. r"""^
  30. (?P<group_name>\w+)
  31. \s+
  32. xxx
  33. \s+
  34. (?P<content>.+?)
  35. $""",
  36. re.VERBOSE )
  37. KEYWORD_REGEX = re.compile( r'^(\w+),?$' )
  38. SYNTAX_ARGUMENT_REGEX = re.compile(
  39. r"^\w+=.*$" )
  40. SYNTAX_REGION_ARGUMENT_REGEX = re.compile(
  41. r"^(?:matchgroup|start)=.*$")
  42. # See ":h syn-nextgroup".
  43. SYNTAX_NEXTGROUP_ARGUMENTS = set([
  44. 'skipwhite',
  45. 'skipnl',
  46. 'skipempty'
  47. ])
  48. # These are the parent groups from which we want to extract keywords.
  49. ROOT_GROUPS = set([
  50. 'Boolean',
  51. 'Identifier',
  52. 'Statement',
  53. 'PreProc',
  54. 'Type'
  55. ])
  56. class SyntaxGroup( object ):
  57. def __init__( self, name, lines = None ):
  58. self.name = name
  59. self.lines = lines if lines else []
  60. self.children = []
  61. def SyntaxKeywordsForCurrentBuffer():
  62. vim.command( 'redir => b:ycm_syntax' )
  63. vim.command( 'silent! syntax list' )
  64. vim.command( 'redir END' )
  65. syntax_output = vimsupport.VimExpressionToPythonType( 'b:ycm_syntax' )
  66. return _KeywordsFromSyntaxListOutput( syntax_output )
  67. def _KeywordsFromSyntaxListOutput( syntax_output ):
  68. group_name_to_group = _SyntaxGroupsFromOutput( syntax_output )
  69. _ConnectGroupChildren( group_name_to_group )
  70. groups_with_keywords = []
  71. for root_group in ROOT_GROUPS:
  72. groups_with_keywords.extend(
  73. _GetAllDescendentats( group_name_to_group[ root_group ] ) )
  74. keywords = []
  75. for group in groups_with_keywords:
  76. keywords.extend( _ExtractKeywordsFromGroup( group ) )
  77. return set( keywords )
  78. def _SyntaxGroupsFromOutput( syntax_output ):
  79. group_name_to_group = _CreateInitialGroupMap()
  80. lines = syntax_output.split( '\n' )
  81. looking_for_group = True
  82. current_group = None
  83. for line in lines:
  84. if not line:
  85. continue
  86. match = SYNTAX_GROUP_REGEX.search( line )
  87. if match:
  88. if looking_for_group:
  89. looking_for_group = False
  90. else:
  91. group_name_to_group[ current_group.name ] = current_group
  92. current_group = SyntaxGroup( match.group( 'group_name' ),
  93. [ match.group( 'content').strip() ] )
  94. else:
  95. if looking_for_group:
  96. continue
  97. if line[ 0 ] == ' ' or line[ 0 ] == '\t':
  98. current_group.lines.append( line.strip() )
  99. if current_group:
  100. group_name_to_group[ current_group.name ] = current_group
  101. return group_name_to_group
  102. def _CreateInitialGroupMap():
  103. def AddToGroupMap( name, parent ):
  104. new_group = SyntaxGroup( name )
  105. group_name_to_group[ name ] = new_group
  106. parent.children.append( new_group )
  107. identifier_group = SyntaxGroup( 'Identifier' )
  108. statement_group = SyntaxGroup( 'Statement' )
  109. type_group = SyntaxGroup( 'Type' )
  110. preproc_group = SyntaxGroup( 'PreProc' )
  111. # See ":h group-name" for details on how the initial group hierarchy is built.
  112. group_name_to_group = {
  113. 'Boolean': SyntaxGroup( 'Boolean' ),
  114. 'Identifier': identifier_group,
  115. 'Statement': statement_group,
  116. 'PreProc': preproc_group,
  117. 'Type': type_group
  118. }
  119. AddToGroupMap( 'Function', identifier_group )
  120. AddToGroupMap( 'Conditional', statement_group )
  121. AddToGroupMap( 'Repeat' , statement_group )
  122. AddToGroupMap( 'Label' , statement_group )
  123. AddToGroupMap( 'Operator' , statement_group )
  124. AddToGroupMap( 'Keyword' , statement_group )
  125. AddToGroupMap( 'Exception' , statement_group )
  126. AddToGroupMap( 'StorageClass', type_group )
  127. AddToGroupMap( 'Structure' , type_group )
  128. AddToGroupMap( 'Typedef' , type_group )
  129. AddToGroupMap( 'Include' , preproc_group )
  130. AddToGroupMap( 'Define' , preproc_group )
  131. AddToGroupMap( 'Macro' , preproc_group )
  132. AddToGroupMap( 'PreCondit', preproc_group )
  133. return group_name_to_group
  134. def _ConnectGroupChildren( group_name_to_group ):
  135. def GetParentNames( group ):
  136. links_to = 'links to '
  137. parent_names = []
  138. for line in group.lines:
  139. if line.startswith( links_to ):
  140. parent_names.append( line[ len( links_to ): ] )
  141. return parent_names
  142. for group in itervalues( group_name_to_group ):
  143. parent_names = GetParentNames( group )
  144. for parent_name in parent_names:
  145. try:
  146. parent_group = group_name_to_group[ parent_name ]
  147. except KeyError:
  148. continue
  149. parent_group.children.append( group )
  150. def _GetAllDescendentats( root_group ):
  151. descendants = []
  152. for child in root_group.children:
  153. descendants.append( child )
  154. descendants.extend( _GetAllDescendentats( child ) )
  155. return descendants
  156. def _ExtractKeywordsFromLine( line ):
  157. if line.startswith( 'links to ' ):
  158. return []
  159. # Ignore "syntax match" lines (see ":h syn-match").
  160. if line.startswith( 'match ' ):
  161. return []
  162. words = line.split()
  163. if not words:
  164. return []
  165. # Ignore "syntax region" lines (see ":h syn-region"). They always start
  166. # with matchgroup= or start= in the syntax list.
  167. if SYNTAX_REGION_ARGUMENT_REGEX.match( words[ 0 ] ):
  168. return []
  169. # Ignore "nextgroup=" argument in first position and the arguments
  170. # "skipwhite", "skipnl", and "skipempty" that immediately come after.
  171. nextgroup_at_start = False
  172. if words[ 0 ].startswith( 'nextgroup=' ):
  173. nextgroup_at_start = True
  174. words = words[ 1: ]
  175. # Ignore "contained" argument in first position.
  176. if words[ 0 ] == 'contained':
  177. words = words[ 1: ]
  178. keywords = []
  179. for word in words:
  180. if nextgroup_at_start and word in SYNTAX_NEXTGROUP_ARGUMENTS:
  181. continue
  182. nextgroup_at_start = False
  183. keyword_matched = KEYWORD_REGEX.match( word )
  184. if keyword_matched:
  185. keywords.append( keyword_matched.group( 1 ) )
  186. return keywords
  187. def _ExtractKeywordsFromGroup( group ):
  188. keywords = []
  189. for line in group.lines:
  190. keywords.extend( _ExtractKeywordsFromLine( line ) )
  191. return keywords