Skip to content

Commit

Permalink
CMake: new parser
Browse files Browse the repository at this point in the history
  • Loading branch information
hadrielk committed Aug 11, 2018
1 parent 7a6202b commit 729059f
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/news.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ The following parsers have been added:
* Automake
* AutoIt
* Clojure
* CMake *optlib*
* CSS
* Ctags option library *optlib*
* CUDA
Expand Down
5 changes: 3 additions & 2 deletions makefiles/translator_input.mak
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# -*- makefile -*-
TRANSLATOR_INPUT = \
optlib/RSpec.ctags \
optlib/RSpec.ctags \
optlib/cmake.ctags \
optlib/ctags-optlib.ctags \
optlib/elm.ctags \
optlib/elm.ctags \
optlib/gdbinit.ctags \
optlib/man.ctags \
optlib/markdown.ctags \
Expand Down
158 changes: 158 additions & 0 deletions optlib/cmake.ctags
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
#
# cmake.ctags --- multitable regex parser for CMake's files
#
# Copyright (c) 2018, 128 Technology, Inc.
#
# Author: Hadriel Kaplan (hadrielk@yahoo.com)
#
# This source code is released for free distribution under the terms of the
# GNU General Public License version 2 or (at your option) any later version.
#
#
# Overview:
#
# This universal-ctags optlib option file defines the parser for tagging
# CMake files. It supports tagging the following:
#
# - cmake function and macro names
# - build target, executable, and library target names
# - cmake variables and options
# - cmake project names
#
# Caveats:
#
# Names that are ${} references to variables are not tagged.
#
# For example, given the following:
#
# set(PROJECT_NAME_STR ${PROJECT_NAME})
# add_executable( ${PROJECT_NAME_STR} ... )
# add_custom_target( ${PROJECT_NAME_STR}_tests ... )
# add_library( sharedlib ... )
#
# the variable 'PROJECT_NAME_STR' and target 'sharedlib' will both be tagged,
# but the other targets will not be.
#
#
# References:
#
# - https://cmake.org/cmake/help/latest/manual/cmake-language.7.html
#

--langdef=CMake
--map-CMake=+.cmake
--map-CMake=+(CMakeLists.txt)

#
# Kinds
#
--kinddef-CMake=f,function,functions
--kinddef-CMake=m,macro,macros
--kinddef-CMake=t,target,targets
--kinddef-CMake=v,variable,variable definitions
--kinddef-CMake=D,option,options specified with -D
--kinddef-CMake=p,project,projects

#
# Tables
#
--_tabledef-CMake=main
--_tabledef-CMake=variable
--_tabledef-CMake=function
--_tabledef-CMake=macro
--_tabledef-CMake=target
--_tabledef-CMake=option
--_tabledef-CMake=project

#
# comment
#
--_tabledef-CMake=commentBegin
--_tabledef-CMake=commentMultiline

--_mtable-regex-CMake=commentBegin/\[\[//{tjump=commentMultiline}
--_mtable-regex-CMake=commentBegin/[^\n]*[ \t\n]*//{tleave}

--_mtable-regex-CMake=commentMultiline/\]\][ \t\n]*//{tleave}
--_mtable-regex-CMake=commentMultiline/.[^]]*//

--_tabledef-CMake=skipComment
--_mtable-regex-CMake=skipComment/#//{tenter=commentBegin}

#
# Utilities
#
--_tabledef-CMake=skipWhiteSpace
--_tabledef-CMake=skipToName
--_tabledef-CMake=nextToken

--_mtable-regex-CMake=skipWhiteSpace/[ \t\n]+//

--_mtable-extend-CMake=skipToName+skipWhiteSpace
--_mtable-extend-CMake=skipToName+skipComment

--_mtable-regex-CMake=nextToken/[^ \t\n]+[ \t\n]*//

#
# main
#
# This first regex entry may seem odd - it's purely for improving performance, by
# matching tokens with leading characters that could not possibly match a later regex,
# and just skipping the whole token (and trailing whitespace). This one regex line
# improved performance by an order of magnitude.
--_mtable-regex-CMake=main/[^sSfFmMaAoOpP# \t\n][^ #\t\n]*[ \t\n]+//
--_mtable-extend-CMake=main+skipComment
--_mtable-regex-CMake=main/set[ \t]*\(//{icase}{tenter=variable}
--_mtable-regex-CMake=main/function[ \t]*\(//{icase}{tenter=function}
--_mtable-regex-CMake=main/macro[ \t]*\(//{icase}{tenter=macro}
--_mtable-regex-CMake=main/add_(custom_target|executable|library)[ \t]*\(//{icase}{tenter=target}
--_mtable-regex-CMake=main/option[ \t]*\(//{icase}{tenter=option}
--_mtable-regex-CMake=main/project[ \t]*\(//{icase}{tenter=project}
--_mtable-extend-CMake=main+nextToken
--_mtable-extend-CMake=main+skipWhiteSpace


#
# Each of the following basically work the same way, and only differ in the
# exact pattern allowed to be their name, and the Kind they add. Note that they
# capture a required trailing '[# \t\n\)]', to verify the full name token matched
# the name's pattern, but then we advanceTo=2start for the next round, so that
# we don't go past a potential '#' comment token but instead match it again in
# the main table as a comment (or whitespace if it was whitespace).
#

#
# variable
#
--_mtable-regex-CMake=variable/([A-Za-z0-9_.-]+)([# \t\n\)])/\1/v/{tleave}{advanceTo=2start}
--_mtable-extend-CMake=variable+skipToName

#
# function
#
--_mtable-regex-CMake=function/([A-Za-z_][A-Za-z0-9_]*)([# \t\n\)])/\1/f/{tleave}{advanceTo=2start}
--_mtable-extend-CMake=function+skipToName

#
# macro
#
--_mtable-regex-CMake=macro/([A-Za-z_][A-Za-z0-9_]*)([# \t\n\)])/\1/m/{tleave}{advanceTo=2start}
--_mtable-extend-CMake=macro+skipToName

#
# target
#
--_mtable-regex-CMake=target/([A-Za-z0-9_.-]+)([# \t\n\)])/\1/t/{tleave}{advanceTo=2start}
--_mtable-extend-CMake=target+skipToName

#
# option
#
--_mtable-regex-CMake=option/([A-Za-z0-9_.-]+)([# \t\n\)])/\1/D/{tleave}{advanceTo=2start}
--_mtable-extend-CMake=option+skipToName

#
# project
#
--_mtable-regex-CMake=project/([A-Za-z0-9_.-]+)([# \t\n\)])/\1/p/{tleave}{advanceTo=2start}
--_mtable-extend-CMake=project+skipToName

0 comments on commit 729059f

Please sign in to comment.