syntax_parse.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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_ARGUMENTS = set([
  41. 'cchar',
  42. 'conceal',
  43. 'contained',
  44. 'containedin',
  45. 'nextgroup',
  46. 'skipempty',
  47. 'skipnl',
  48. 'skipwhite',
  49. 'transparent',
  50. 'concealends',
  51. 'contains',
  52. 'display',
  53. 'extend',
  54. 'fold',
  55. 'oneline',
  56. 'keepend',
  57. 'excludenl',
  58. ])
  59. # We want to parse lines starting with these args
  60. ALLOWED_SYNTAX_ARGUMENTS = set([
  61. 'contained',
  62. ])
  63. # These are the parent groups from which we want to extract keywords
  64. ROOT_GROUPS = set([
  65. 'Statement',
  66. 'Boolean',
  67. 'Include',
  68. 'Type',
  69. 'Identifier',
  70. ])
  71. class SyntaxGroup( object ):
  72. def __init__( self, name, lines = None ):
  73. self.name = name
  74. self.lines = lines if lines else []
  75. self.children = []
  76. def SyntaxKeywordsForCurrentBuffer():
  77. vim.command( 'redir => b:ycm_syntax' )
  78. vim.command( 'silent! syntax list' )
  79. vim.command( 'redir END' )
  80. syntax_output = vimsupport.GetVariableValue( 'b:ycm_syntax' )
  81. return _KeywordsFromSyntaxListOutput( syntax_output )
  82. def _KeywordsFromSyntaxListOutput( syntax_output ):
  83. group_name_to_group = _SyntaxGroupsFromOutput( syntax_output )
  84. _ConnectGroupChildren( group_name_to_group )
  85. groups_with_keywords = []
  86. for root_group in ROOT_GROUPS:
  87. groups_with_keywords.extend(
  88. _GetAllDescendentats( group_name_to_group[ root_group ] ) )
  89. keywords = []
  90. for group in groups_with_keywords:
  91. keywords.extend( _ExtractKeywordsFromGroup( group ) )
  92. return set( keywords )
  93. def _SyntaxGroupsFromOutput( syntax_output ):
  94. group_name_to_group = _CreateInitialGroupMap()
  95. lines = syntax_output.split( '\n' )
  96. looking_for_group = True
  97. current_group = None
  98. for line in lines:
  99. if not line:
  100. continue
  101. match = SYNTAX_GROUP_REGEX.search( line )
  102. if match:
  103. if looking_for_group:
  104. looking_for_group = False
  105. else:
  106. group_name_to_group[ current_group.name ] = current_group
  107. current_group = SyntaxGroup( match.group( 'group_name' ),
  108. [ match.group( 'content').strip() ] )
  109. else:
  110. if looking_for_group:
  111. continue
  112. if line[ 0 ] == ' ' or line[ 0 ] == '\t':
  113. current_group.lines.append( line.strip() )
  114. if current_group:
  115. group_name_to_group[ current_group.name ] = current_group
  116. return group_name_to_group
  117. def _CreateInitialGroupMap():
  118. def AddToGroupMap( name, parent ):
  119. new_group = SyntaxGroup( name )
  120. group_name_to_group[ name ] = new_group
  121. parent.children.append( new_group )
  122. statement_group = SyntaxGroup( 'Statement' )
  123. type_group = SyntaxGroup( 'Type' )
  124. identifier_group = SyntaxGroup( 'Identifier' )
  125. # See `:h group-name` for details on how the initial group hierarchy is built
  126. group_name_to_group = {
  127. 'Statement': statement_group,
  128. 'Type': type_group,
  129. 'Boolean': SyntaxGroup( 'Boolean' ),
  130. 'Include': SyntaxGroup( 'Include' ),
  131. 'Identifier': identifier_group,
  132. }
  133. AddToGroupMap( 'Conditional', statement_group )
  134. AddToGroupMap( 'Repeat' , statement_group )
  135. AddToGroupMap( 'Label' , statement_group )
  136. AddToGroupMap( 'Operator' , statement_group )
  137. AddToGroupMap( 'Keyword' , statement_group )
  138. AddToGroupMap( 'Exception' , statement_group )
  139. AddToGroupMap( 'StorageClass', type_group )
  140. AddToGroupMap( 'Structure' , type_group )
  141. AddToGroupMap( 'Typedef' , type_group )
  142. AddToGroupMap( 'Function', identifier_group )
  143. return group_name_to_group
  144. def _ConnectGroupChildren( group_name_to_group ):
  145. def GetParentNames( group ):
  146. links_to = 'links to '
  147. parent_names = []
  148. for line in group.lines:
  149. if line.startswith( links_to ):
  150. parent_names.append( line[ len( links_to ): ] )
  151. return parent_names
  152. for group in itervalues( group_name_to_group ):
  153. parent_names = GetParentNames( group )
  154. for parent_name in parent_names:
  155. try:
  156. parent_group = group_name_to_group[ parent_name ]
  157. except KeyError:
  158. continue
  159. parent_group.children.append( group )
  160. def _GetAllDescendentats( root_group ):
  161. descendants = []
  162. for child in root_group.children:
  163. descendants.append( child )
  164. descendants.extend( _GetAllDescendentats( child ) )
  165. return descendants
  166. def _ExtractKeywordsFromGroup( group ):
  167. keywords = []
  168. for line in group.lines:
  169. if line.startswith( 'links to ' ):
  170. continue
  171. words = line.split()
  172. if not words or ( words[ 0 ] in SYNTAX_ARGUMENTS and
  173. words[ 0 ] not in ALLOWED_SYNTAX_ARGUMENTS ):
  174. continue
  175. for word in words:
  176. if ( word not in SYNTAX_ARGUMENTS and
  177. not SYNTAX_ARGUMENT_REGEX.match( word ) and
  178. KEYWORD_REGEX.match( word ) ):
  179. if word.endswith( ',' ):
  180. word = word[ :-1 ]
  181. keywords.append( word )
  182. return keywords