syntax_parse.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. # Not installing aliases from python-future; it's unreliable and slow.
  22. from builtins import * # noqa
  23. from future.utils import itervalues
  24. import re
  25. import vim
  26. from ycm import vimsupport
  27. SYNTAX_GROUP_REGEX = re.compile(
  28. r"""^
  29. (?P<group_name>\w+)
  30. \s+
  31. xxx
  32. \s+
  33. (?P<content>.+?)
  34. $""",
  35. re.VERBOSE )
  36. KEYWORD_REGEX = re.compile( r'^(\w+),?$' )
  37. SYNTAX_ARGUMENT_REGEX = re.compile(
  38. r"^\w+=.*$" )
  39. SYNTAX_REGION_ARGUMENT_REGEX = re.compile(
  40. r"^(?:matchgroup|start)=.*$")
  41. # See ":h syn-nextgroup".
  42. SYNTAX_NEXTGROUP_ARGUMENTS = set([
  43. 'skipwhite',
  44. 'skipnl',
  45. 'skipempty'
  46. ])
  47. # These are the parent groups from which we want to extract keywords.
  48. ROOT_GROUPS = set([
  49. 'Boolean',
  50. 'Identifier',
  51. 'Statement',
  52. 'PreProc',
  53. 'Type'
  54. ])
  55. class SyntaxGroup( object ):
  56. def __init__( self, name, lines = None ):
  57. self.name = name
  58. self.lines = lines if lines else []
  59. self.children = []
  60. def SyntaxKeywordsForCurrentBuffer():
  61. vim.command( 'redir => b:ycm_syntax' )
  62. vim.command( 'silent! syntax list' )
  63. vim.command( 'redir END' )
  64. syntax_output = vimsupport.VimExpressionToPythonType( 'b:ycm_syntax' )
  65. return _KeywordsFromSyntaxListOutput( syntax_output )
  66. def _KeywordsFromSyntaxListOutput( syntax_output ):
  67. group_name_to_group = _SyntaxGroupsFromOutput( syntax_output )
  68. _ConnectGroupChildren( group_name_to_group )
  69. groups_with_keywords = []
  70. for root_group in ROOT_GROUPS:
  71. groups_with_keywords.extend(
  72. _GetAllDescendentats( group_name_to_group[ root_group ] ) )
  73. keywords = []
  74. for group in groups_with_keywords:
  75. keywords.extend( _ExtractKeywordsFromGroup( group ) )
  76. return set( keywords )
  77. def _SyntaxGroupsFromOutput( syntax_output ):
  78. group_name_to_group = _CreateInitialGroupMap()
  79. lines = syntax_output.split( '\n' )
  80. looking_for_group = True
  81. current_group = None
  82. for line in lines:
  83. if not line:
  84. continue
  85. match = SYNTAX_GROUP_REGEX.search( line )
  86. if match:
  87. if looking_for_group:
  88. looking_for_group = False
  89. else:
  90. group_name_to_group[ current_group.name ] = current_group
  91. current_group = SyntaxGroup( match.group( 'group_name' ),
  92. [ match.group( 'content').strip() ] )
  93. else:
  94. if looking_for_group:
  95. continue
  96. if line[ 0 ] == ' ' or line[ 0 ] == '\t':
  97. current_group.lines.append( line.strip() )
  98. if current_group:
  99. group_name_to_group[ current_group.name ] = current_group
  100. return group_name_to_group
  101. def _CreateInitialGroupMap():
  102. def AddToGroupMap( name, parent ):
  103. new_group = SyntaxGroup( name )
  104. group_name_to_group[ name ] = new_group
  105. parent.children.append( new_group )
  106. identifier_group = SyntaxGroup( 'Identifier' )
  107. statement_group = SyntaxGroup( 'Statement' )
  108. type_group = SyntaxGroup( 'Type' )
  109. preproc_group = SyntaxGroup( 'PreProc' )
  110. # See ":h group-name" for details on how the initial group hierarchy is built.
  111. group_name_to_group = {
  112. 'Boolean': SyntaxGroup( 'Boolean' ),
  113. 'Identifier': identifier_group,
  114. 'Statement': statement_group,
  115. 'PreProc': preproc_group,
  116. 'Type': type_group
  117. }
  118. AddToGroupMap( 'Function', identifier_group )
  119. AddToGroupMap( 'Conditional', statement_group )
  120. AddToGroupMap( 'Repeat' , statement_group )
  121. AddToGroupMap( 'Label' , statement_group )
  122. AddToGroupMap( 'Operator' , statement_group )
  123. AddToGroupMap( 'Keyword' , statement_group )
  124. AddToGroupMap( 'Exception' , statement_group )
  125. AddToGroupMap( 'StorageClass', type_group )
  126. AddToGroupMap( 'Structure' , type_group )
  127. AddToGroupMap( 'Typedef' , type_group )
  128. AddToGroupMap( 'Include' , preproc_group )
  129. AddToGroupMap( 'Define' , preproc_group )
  130. AddToGroupMap( 'Macro' , preproc_group )
  131. AddToGroupMap( 'PreCondit', preproc_group )
  132. return group_name_to_group
  133. def _ConnectGroupChildren( group_name_to_group ):
  134. def GetParentNames( group ):
  135. links_to = 'links to '
  136. parent_names = []
  137. for line in group.lines:
  138. if line.startswith( links_to ):
  139. parent_names.append( line[ len( links_to ): ] )
  140. return parent_names
  141. for group in itervalues( group_name_to_group ):
  142. parent_names = GetParentNames( group )
  143. for parent_name in parent_names:
  144. try:
  145. parent_group = group_name_to_group[ parent_name ]
  146. except KeyError:
  147. continue
  148. parent_group.children.append( group )
  149. def _GetAllDescendentats( root_group ):
  150. descendants = []
  151. for child in root_group.children:
  152. descendants.append( child )
  153. descendants.extend( _GetAllDescendentats( child ) )
  154. return descendants
  155. def _ExtractKeywordsFromLine( line ):
  156. if line.startswith( 'links to ' ):
  157. return []
  158. # Ignore "syntax match" lines (see ":h syn-match").
  159. if line.startswith( 'match ' ):
  160. return []
  161. words = line.split()
  162. if not words:
  163. return []
  164. # Ignore "syntax region" lines (see ":h syn-region"). They always start
  165. # with matchgroup= or start= in the syntax list.
  166. if SYNTAX_REGION_ARGUMENT_REGEX.match( words[ 0 ] ):
  167. return []
  168. # Ignore "nextgroup=" argument in first position and the arguments
  169. # "skipwhite", "skipnl", and "skipempty" that immediately come after.
  170. nextgroup_at_start = False
  171. if words[ 0 ].startswith( 'nextgroup=' ):
  172. nextgroup_at_start = True
  173. words = words[ 1: ]
  174. # Ignore "contained" argument in first position.
  175. if words[ 0 ] == 'contained':
  176. words = words[ 1: ]
  177. keywords = []
  178. for word in words:
  179. if nextgroup_at_start and word in SYNTAX_NEXTGROUP_ARGUMENTS:
  180. continue
  181. nextgroup_at_start = False
  182. keyword_matched = KEYWORD_REGEX.match( word )
  183. if keyword_matched:
  184. keywords.append( keyword_matched.group( 1 ) )
  185. return keywords
  186. def _ExtractKeywordsFromGroup( group ):
  187. keywords = []
  188. for line in group.lines:
  189. keywords.extend( _ExtractKeywordsFromLine( line ) )
  190. return keywords