syntax_parse.py 6.2 KB

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