syntax_parse.py 6.5 KB

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