Skip to content

Commit 729059f

Browse files
committed
CMake: new parser
1 parent 7a6202b commit 729059f

File tree

3 files changed

+162
-2
lines changed

3 files changed

+162
-2
lines changed

docs/news.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ The following parsers have been added:
4242
* Automake
4343
* AutoIt
4444
* Clojure
45+
* CMake *optlib*
4546
* CSS
4647
* Ctags option library *optlib*
4748
* CUDA

makefiles/translator_input.mak

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# -*- makefile -*-
22
TRANSLATOR_INPUT = \
3-
optlib/RSpec.ctags \
3+
optlib/RSpec.ctags \
4+
optlib/cmake.ctags \
45
optlib/ctags-optlib.ctags \
5-
optlib/elm.ctags \
6+
optlib/elm.ctags \
67
optlib/gdbinit.ctags \
78
optlib/man.ctags \
89
optlib/markdown.ctags \

optlib/cmake.ctags

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
#
2+
# cmake.ctags --- multitable regex parser for CMake's files
3+
#
4+
# Copyright (c) 2018, 128 Technology, Inc.
5+
#
6+
# Author: Hadriel Kaplan ([email protected])
7+
#
8+
# This source code is released for free distribution under the terms of the
9+
# GNU General Public License version 2 or (at your option) any later version.
10+
#
11+
#
12+
# Overview:
13+
#
14+
# This universal-ctags optlib option file defines the parser for tagging
15+
# CMake files. It supports tagging the following:
16+
#
17+
# - cmake function and macro names
18+
# - build target, executable, and library target names
19+
# - cmake variables and options
20+
# - cmake project names
21+
#
22+
# Caveats:
23+
#
24+
# Names that are ${} references to variables are not tagged.
25+
#
26+
# For example, given the following:
27+
#
28+
# set(PROJECT_NAME_STR ${PROJECT_NAME})
29+
# add_executable( ${PROJECT_NAME_STR} ... )
30+
# add_custom_target( ${PROJECT_NAME_STR}_tests ... )
31+
# add_library( sharedlib ... )
32+
#
33+
# the variable 'PROJECT_NAME_STR' and target 'sharedlib' will both be tagged,
34+
# but the other targets will not be.
35+
#
36+
#
37+
# References:
38+
#
39+
# - https://cmake.org/cmake/help/latest/manual/cmake-language.7.html
40+
#
41+
42+
--langdef=CMake
43+
--map-CMake=+.cmake
44+
--map-CMake=+(CMakeLists.txt)
45+
46+
#
47+
# Kinds
48+
#
49+
--kinddef-CMake=f,function,functions
50+
--kinddef-CMake=m,macro,macros
51+
--kinddef-CMake=t,target,targets
52+
--kinddef-CMake=v,variable,variable definitions
53+
--kinddef-CMake=D,option,options specified with -D
54+
--kinddef-CMake=p,project,projects
55+
56+
#
57+
# Tables
58+
#
59+
--_tabledef-CMake=main
60+
--_tabledef-CMake=variable
61+
--_tabledef-CMake=function
62+
--_tabledef-CMake=macro
63+
--_tabledef-CMake=target
64+
--_tabledef-CMake=option
65+
--_tabledef-CMake=project
66+
67+
#
68+
# comment
69+
#
70+
--_tabledef-CMake=commentBegin
71+
--_tabledef-CMake=commentMultiline
72+
73+
--_mtable-regex-CMake=commentBegin/\[\[//{tjump=commentMultiline}
74+
--_mtable-regex-CMake=commentBegin/[^\n]*[ \t\n]*//{tleave}
75+
76+
--_mtable-regex-CMake=commentMultiline/\]\][ \t\n]*//{tleave}
77+
--_mtable-regex-CMake=commentMultiline/.[^]]*//
78+
79+
--_tabledef-CMake=skipComment
80+
--_mtable-regex-CMake=skipComment/#//{tenter=commentBegin}
81+
82+
#
83+
# Utilities
84+
#
85+
--_tabledef-CMake=skipWhiteSpace
86+
--_tabledef-CMake=skipToName
87+
--_tabledef-CMake=nextToken
88+
89+
--_mtable-regex-CMake=skipWhiteSpace/[ \t\n]+//
90+
91+
--_mtable-extend-CMake=skipToName+skipWhiteSpace
92+
--_mtable-extend-CMake=skipToName+skipComment
93+
94+
--_mtable-regex-CMake=nextToken/[^ \t\n]+[ \t\n]*//
95+
96+
#
97+
# main
98+
#
99+
# This first regex entry may seem odd - it's purely for improving performance, by
100+
# matching tokens with leading characters that could not possibly match a later regex,
101+
# and just skipping the whole token (and trailing whitespace). This one regex line
102+
# improved performance by an order of magnitude.
103+
--_mtable-regex-CMake=main/[^sSfFmMaAoOpP# \t\n][^ #\t\n]*[ \t\n]+//
104+
--_mtable-extend-CMake=main+skipComment
105+
--_mtable-regex-CMake=main/set[ \t]*\(//{icase}{tenter=variable}
106+
--_mtable-regex-CMake=main/function[ \t]*\(//{icase}{tenter=function}
107+
--_mtable-regex-CMake=main/macro[ \t]*\(//{icase}{tenter=macro}
108+
--_mtable-regex-CMake=main/add_(custom_target|executable|library)[ \t]*\(//{icase}{tenter=target}
109+
--_mtable-regex-CMake=main/option[ \t]*\(//{icase}{tenter=option}
110+
--_mtable-regex-CMake=main/project[ \t]*\(//{icase}{tenter=project}
111+
--_mtable-extend-CMake=main+nextToken
112+
--_mtable-extend-CMake=main+skipWhiteSpace
113+
114+
115+
#
116+
# Each of the following basically work the same way, and only differ in the
117+
# exact pattern allowed to be their name, and the Kind they add. Note that they
118+
# capture a required trailing '[# \t\n\)]', to verify the full name token matched
119+
# the name's pattern, but then we advanceTo=2start for the next round, so that
120+
# we don't go past a potential '#' comment token but instead match it again in
121+
# the main table as a comment (or whitespace if it was whitespace).
122+
#
123+
124+
#
125+
# variable
126+
#
127+
--_mtable-regex-CMake=variable/([A-Za-z0-9_.-]+)([# \t\n\)])/\1/v/{tleave}{advanceTo=2start}
128+
--_mtable-extend-CMake=variable+skipToName
129+
130+
#
131+
# function
132+
#
133+
--_mtable-regex-CMake=function/([A-Za-z_][A-Za-z0-9_]*)([# \t\n\)])/\1/f/{tleave}{advanceTo=2start}
134+
--_mtable-extend-CMake=function+skipToName
135+
136+
#
137+
# macro
138+
#
139+
--_mtable-regex-CMake=macro/([A-Za-z_][A-Za-z0-9_]*)([# \t\n\)])/\1/m/{tleave}{advanceTo=2start}
140+
--_mtable-extend-CMake=macro+skipToName
141+
142+
#
143+
# target
144+
#
145+
--_mtable-regex-CMake=target/([A-Za-z0-9_.-]+)([# \t\n\)])/\1/t/{tleave}{advanceTo=2start}
146+
--_mtable-extend-CMake=target+skipToName
147+
148+
#
149+
# option
150+
#
151+
--_mtable-regex-CMake=option/([A-Za-z0-9_.-]+)([# \t\n\)])/\1/D/{tleave}{advanceTo=2start}
152+
--_mtable-extend-CMake=option+skipToName
153+
154+
#
155+
# project
156+
#
157+
--_mtable-regex-CMake=project/([A-Za-z0-9_.-]+)([# \t\n\)])/\1/p/{tleave}{advanceTo=2start}
158+
--_mtable-extend-CMake=project+skipToName

0 commit comments

Comments
 (0)