Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kotlin: add basic regex parser #2769

Merged
merged 5 commits into from
Dec 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Units/parser-kotlin.r/kotlin-comments.d/expected.tags
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
f input.kt /^\/* comment *\/ var f = 6 \/* another comment *\/$/;" v
15 changes: 15 additions & 0 deletions Units/parser-kotlin.r/kotlin-comments.d/input.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// var a = 1

/* var b = 2 */

/*
var c = 3
*/

/** var d = 4 */

/**
* var e = 5
*/

/* comment */ var f = 6 /* another comment */
8 changes: 8 additions & 0 deletions Units/parser-kotlin.r/kotlin-destructuring.d/expected.tags
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
a input.kt /^val (a, b) = functionReturningPair()$/;" C
b input.kt /^val (a, b) = functionReturningPair()$/;" C
c input.kt /^val (c: Int, d: String) = functionReturningPair()$/;" C
d input.kt /^val (c: Int, d: String) = functionReturningPair()$/;" C
e input.kt /^var (e, f) = functionReturningPair()$/;" v
f input.kt /^var (e, f) = functionReturningPair()$/;" v
g input.kt /^var (g: Int, h: Pair<String, Int>) = functionReturningPair()$/;" v
h input.kt /^var (g: Int, h: Pair<String, Int>) = functionReturningPair()$/;" v
4 changes: 4 additions & 0 deletions Units/parser-kotlin.r/kotlin-destructuring.d/input.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
val (a, b) = functionReturningPair()
val (c: Int, d: String) = functionReturningPair()
var (e, f) = functionReturningPair()
var (g: Int, h: Pair<String, Int>) = functionReturningPair()
3 changes: 3 additions & 0 deletions Units/parser-kotlin.r/kotlin-extensions.d/expected.tags
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
A input.kt /^class A$/;" c
test input.kt /^fun A.test() = true$/;" m
value input.kt /^val A.value$/;" C
6 changes: 6 additions & 0 deletions Units/parser-kotlin.r/kotlin-extensions.d/input.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class A

fun A.test() = true

val A.value
get() = "AAA"
5 changes: 5 additions & 0 deletions Units/parser-kotlin.r/kotlin-generics.d/expected.tags
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Bar input.kt /^class Bar<T, U>(val a: T, val b: U): Foo<T>$/;" c
Foo input.kt /^interface Foo<T> {$/;" i
a input.kt /^class Bar<T, U>(val a: T, val b: U): Foo<T>$/;" C
b input.kt /^class Bar<T, U>(val a: T, val b: U): Foo<T>$/;" C
genericFunction input.kt /^ fun<T: Any> genericFunction() = 42$/;" m
5 changes: 5 additions & 0 deletions Units/parser-kotlin.r/kotlin-generics.d/input.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
interface Foo<T> {
fun<T: Any> genericFunction() = 42
}

class Bar<T, U>(val a: T, val b: U): Foo<T>
3 changes: 3 additions & 0 deletions Units/parser-kotlin.r/kotlin-multilinestrings.d/expected.tags
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
used1 input.kt /^var used1 = """var unused1 = 1"""$/;" v
used2 input.kt /^var used2 = """$/;" v
used3 input.kt /^var used3 = """var unused3 = 3""" + """var unused4 = 4"""$/;" v
5 changes: 5 additions & 0 deletions Units/parser-kotlin.r/kotlin-multilinestrings.d/input.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var used1 = """var unused1 = 1"""
var used2 = """
var unused2 = 2
"""
var used3 = """var unused3 = 3""" + """var unused4 = 4"""
2 changes: 2 additions & 0 deletions Units/parser-kotlin.r/kotlin-strings.d/expected.tags
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
used1 input.kt /^var used1 = "var unused1 = 1"$/;" v
used2 input.kt /^var used2 = "\\"var\\" unused2 = 2"$/;" v
2 changes: 2 additions & 0 deletions Units/parser-kotlin.r/kotlin-strings.d/input.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
var used1 = "var unused1 = 1"
var used2 = "\"var\" unused2 = 2"
1 change: 1 addition & 0 deletions Units/parser-kotlin.r/kotlin-syntax.d/args.ctags
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--sort=no
11 changes: 11 additions & 0 deletions Units/parser-kotlin.r/kotlin-syntax.d/expected.tags
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
com.example.helloworld input.kt /^package com.example.helloworld$/;" p
AbstractWorker input.kt /^interface AbstractWorker {$/;" i
process input.kt /^ abstract fun process(): String$/;" m
Worker input.kt /^class Worker<T>(val name: T): AbstractWorker {$/;" c
name input.kt /^class Worker<T>(val name: T): AbstractWorker {$/;" C
process input.kt /^ override fun process(): String = name.toString()$/;" m
Global input.kt /^object Global {$/;" o
greeting input.kt /^ const val greeting = "Hello"$/;" C
StringWorker input.kt /^typealias StringWorker = Worker<String>$/;" T
main input.kt /^fun main() {$/;" m
result input.kt /^ var result: String = Global.greeting$/;" v
22 changes: 22 additions & 0 deletions Units/parser-kotlin.r/kotlin-syntax.d/input.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.example.helloworld

interface AbstractWorker {
abstract fun process(): String
}

class Worker<T>(val name: T): AbstractWorker {
override fun process(): String = name.toString()
}

object Global {
const val greeting = "Hello"
}

typealias StringWorker = Worker<String>

fun main() {
var result: String = Global.greeting
result = "$result " + StringWorker("World").process()
result += "!"
println(result)
}
1 change: 1 addition & 0 deletions docs/news.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1339,6 +1339,7 @@ The following parsers have been added:
* JSON
* Julia
* Kconfig *optlib*
* Kotlin *optlib*
* GNU linker script(LdScript)
* Man page *optlib*
* Markdown *optlib*
Expand Down
1 change: 1 addition & 0 deletions main/parsers_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
JsonParser, \
JuliaParser, \
KconfigParser, \
KotlinParser, \
LdScriptParser, \
LispParser, \
LiterateHaskellParser, \
Expand Down
1 change: 1 addition & 0 deletions makefiles/optlib2c_input.mak
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ OPTLIB2C_INPUT = \
optlib/gdbinit.ctags \
optlib/inko.ctags \
optlib/kconfig.ctags \
optlib/kotlin.ctags \
optlib/man.ctags \
optlib/markdown.ctags \
optlib/meson.ctags \
Expand Down
171 changes: 171 additions & 0 deletions optlib/kotlin.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
/*
* Generated by ./misc/optlib2c from optlib/kotlin.ctags, Don't edit this manually.
*/
#include "general.h"
#include "parse.h"
#include "routines.h"
#include "field.h"
#include "xtag.h"


static void initializeKotlinParser (const langType language)
{

addLanguageRegexTable (language, "toplevel");
addLanguageRegexTable (language, "comment");
addLanguageRegexTable (language, "string");
addLanguageRegexTable (language, "mlstring");
addLanguageRegexTable (language, "vals");
addLanguageRegexTable (language, "vars");

addLanguageTagMultiTableRegex (language, "toplevel",
"^/\\*",
"", "", "{tenter=comment}", NULL);
addLanguageTagMultiTableRegex (language, "toplevel",
"^//[^\n]*\n",
"", "", "", NULL);
addLanguageTagMultiTableRegex (language, "toplevel",
"^\"\"\"",
"", "", "{tenter=mlstring}", NULL);
addLanguageTagMultiTableRegex (language, "toplevel",
"^\"",
"", "", "{tenter=string}", NULL);
addLanguageTagMultiTableRegex (language, "toplevel",
"^'.{1,2}'",
"", "", "", NULL);
addLanguageTagMultiTableRegex (language, "toplevel",
"^package[[:space:]]+([^\n]+)",
"\\1", "p", "", NULL);
addLanguageTagMultiTableRegex (language, "toplevel",
"^val[[:space:]]+\\([[:space:]]*",
"", "", "{tenter=vals}", NULL);
addLanguageTagMultiTableRegex (language, "toplevel",
"^var[[:space:]]+\\([[:space:]]*",
"", "", "{tenter=vars}", NULL);
addLanguageTagMultiTableRegex (language, "toplevel",
"^class[[:space:]]+([[:alnum:]_]+)",
"\\1", "c", "", NULL);
addLanguageTagMultiTableRegex (language, "toplevel",
"^interface[[:space:]]+([[:alnum:]_]+)",
"\\1", "i", "", NULL);
addLanguageTagMultiTableRegex (language, "toplevel",
"^object[[:space:]]+([[:alnum:]_]+)",
"\\1", "o", "", NULL);
addLanguageTagMultiTableRegex (language, "toplevel",
"^typealias[[:space:]]+([[:alnum:]_]+)",
"\\1", "T", "", NULL);
addLanguageTagMultiTableRegex (language, "toplevel",
"^fun(<.*>)*[[:space:]]+([[:alnum:]_]+\\.)*([[:alnum:]_]+)",
"\\3", "m", "", NULL);
addLanguageTagMultiTableRegex (language, "toplevel",
"^val[[:space:]]+([[:alnum:]_]+\\.)*([[:alnum:]_]+)",
"\\2", "C", "", NULL);
addLanguageTagMultiTableRegex (language, "toplevel",
"^var[[:space:]]+([[:alnum:]_]+\\.)*([[:alnum:]_]+)",
"\\2", "v", "", NULL);
addLanguageTagMultiTableRegex (language, "toplevel",
"^[[:alnum:]]+",
"", "", "", NULL);
addLanguageTagMultiTableRegex (language, "toplevel",
"^.",
"", "", "", NULL);
addLanguageTagMultiTableRegex (language, "comment",
"^\\*/",
"", "", "{tleave}", NULL);
addLanguageTagMultiTableRegex (language, "comment",
"^[^*]+",
"", "", "", NULL);
addLanguageTagMultiTableRegex (language, "comment",
"^.",
"", "", "", NULL);
addLanguageTagMultiTableRegex (language, "string",
"^[^\\\\\"]+",
"", "", "", NULL);
addLanguageTagMultiTableRegex (language, "string",
"^\\\\.",
"", "", "", NULL);
addLanguageTagMultiTableRegex (language, "string",
"^\"",
"", "", "{tleave}", NULL);
addLanguageTagMultiTableRegex (language, "string",
"^.",
"", "", "", NULL);
addLanguageTagMultiTableRegex (language, "mlstring",
"^\"\"\"",
"", "", "{tleave}", NULL);
addLanguageTagMultiTableRegex (language, "mlstring",
"^[^\\\\\"]+",
"", "", "", NULL);
addLanguageTagMultiTableRegex (language, "mlstring",
"^.",
"", "", "", NULL);
addLanguageTagMultiTableRegex (language, "vals",
"^([[:alnum:]_]+)[[:space:]]*(:[[:space:]]*[[:alnum:]_]+)*[[:space:]]*,*[[:space:]]*",
"\\1", "C", "", NULL);
addLanguageTagMultiTableRegex (language, "vals",
"^\\)",
"", "", "{tleave}", NULL);
addLanguageTagMultiTableRegex (language, "vars",
"^([[:alnum:]_]+)[[:space:]]*(:[[:space:]]*[[:alnum:]_]+)*[[:space:]]*,*[[:space:]]*",
"\\1", "v", "", NULL);
addLanguageTagMultiTableRegex (language, "vars",
"^\\)",
"", "", "{tleave}", NULL);
}

extern parserDefinition* KotlinParser (void)
{
static const char *const extensions [] = {
"kt",
"kts",
NULL
};

static const char *const aliases [] = {
NULL
};

static const char *const patterns [] = {
NULL
};

static kindDefinition KotlinKindTable [] = {
{
true, 'p', "package", "packages",
},
{
true, 'c', "class", "classes",
},
{
true, 'o', "object", "objects",
},
{
true, 'i', "interface", "interfaces",
},
{
true, 'T', "typealias", "typealiases",
},
{
true, 'm', "method", "methods",
},
{
true, 'C', "constant", "constants",
},
{
true, 'v', "variable", "variables",
},
};

parserDefinition* const def = parserNew ("Kotlin");

def->enabled = true;
def->extensions = extensions;
def->patterns = patterns;
def->aliases = aliases;
def->method = METHOD_NOT_CRAFTED|METHOD_REGEX;
def->kindTable = KotlinKindTable;
def->kindCount = ARRAY_SIZE(KotlinKindTable);
def->initialize = initializeKotlinParser;

return def;
}
85 changes: 85 additions & 0 deletions optlib/kotlin.ctags
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#
# Copyright (c) 2020, Jan Dolinár
#
# Author: Jan Dolinár <dolik.rce@gmail.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
# USA.
#
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a URL for the language reference page of Kotlin?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added


# Reference documentation for Kotlin language can be found
# at https://kotlinlang.org/docs/reference/, including its
# grammar (https://kotlinlang.org/docs/reference/grammar.html).

# Known issues:
# - annotations are not handled correctly

# Regex for Kotlin
--langdef=Kotlin
--map-Kotlin=+.kt
--map-Kotlin=+.kts

--kinddef-Kotlin=p,package,packages
--kinddef-Kotlin=c,class,classes
--kinddef-Kotlin=o,object,objects
--kinddef-Kotlin=i,interface,interfaces
--kinddef-Kotlin=T,typealias,typealiases
--kinddef-Kotlin=m,method,methods
--kinddef-Kotlin=C,constant,constants
--kinddef-Kotlin=v,variable,variables

--_tabledef-Kotlin=toplevel
--_tabledef-Kotlin=comment
--_tabledef-Kotlin=string
--_tabledef-Kotlin=mlstring
--_tabledef-Kotlin=vals
--_tabledef-Kotlin=vars

--_mtable-regex-Kotlin=comment/\*\///{tleave}
--_mtable-regex-Kotlin=comment/[^*]+//
--_mtable-regex-Kotlin=comment/.//

--_mtable-regex-Kotlin=mlstring/"""//{tleave}
--_mtable-regex-Kotlin=mlstring/[^\\"]+//
--_mtable-regex-Kotlin=mlstring/.//

--_mtable-regex-Kotlin=string/[^\\"]+//
--_mtable-regex-Kotlin=string/\\.//
--_mtable-regex-Kotlin=string/"//{tleave}
--_mtable-regex-Kotlin=string/.//

--_mtable-regex-Kotlin=vals/([[:alnum:]_]+)[[:space:]]*(:[[:space:]]*[[:alnum:]_]+)*[[:space:]]*,*[[:space:]]*/\1/C/
--_mtable-regex-Kotlin=vals/\)//{tleave}

--_mtable-regex-Kotlin=vars/([[:alnum:]_]+)[[:space:]]*(:[[:space:]]*[[:alnum:]_]+)*[[:space:]]*,*[[:space:]]*/\1/v/
--_mtable-regex-Kotlin=vars/\)//{tleave}

--_mtable-regex-Kotlin=toplevel/\/\*//{tenter=comment}
--_mtable-regex-Kotlin=toplevel/\/\/[^\n]*\n//
--_mtable-regex-Kotlin=toplevel/"""//{tenter=mlstring}
--_mtable-regex-Kotlin=toplevel/"//{tenter=string}
--_mtable-regex-Kotlin=toplevel/'.{1,2}'//
--_mtable-regex-Kotlin=toplevel/package[[:space:]]+([^\n]+)/\1/p/
--_mtable-regex-Kotlin=toplevel/val[[:space:]]+\([[:space:]]*//{tenter=vals}
--_mtable-regex-Kotlin=toplevel/var[[:space:]]+\([[:space:]]*//{tenter=vars}
--_mtable-regex-Kotlin=toplevel/class[[:space:]]+([[:alnum:]_]+)/\1/c/
--_mtable-regex-Kotlin=toplevel/interface[[:space:]]+([[:alnum:]_]+)/\1/i/
--_mtable-regex-Kotlin=toplevel/object[[:space:]]+([[:alnum:]_]+)/\1/o/
--_mtable-regex-Kotlin=toplevel/typealias[[:space:]]+([[:alnum:]_]+)/\1/T/
--_mtable-regex-Kotlin=toplevel/fun(<.*>)*[[:space:]]+([[:alnum:]_]+\.)*([[:alnum:]_]+)/\3/m/
--_mtable-regex-Kotlin=toplevel/val[[:space:]]+([[:alnum:]_]+\.)*([[:alnum:]_]+)/\2/C/
--_mtable-regex-Kotlin=toplevel/var[[:space:]]+([[:alnum:]_]+\.)*([[:alnum:]_]+)/\2/v/
--_mtable-regex-Kotlin=toplevel/[[:alnum:]]+//
--_mtable-regex-Kotlin=toplevel/.//
1 change: 1 addition & 0 deletions win32/ctags_vs2013.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@
<ClCompile Include="..\optlib\gdbinit.c" />
<ClCompile Include="..\optlib\inko.c" />
<ClCompile Include="..\optlib\kconfig.c" />
<ClCompile Include="..\optlib\kotlin.c" />
<ClCompile Include="..\optlib\man.c" />
<ClCompile Include="..\optlib\markdown.c" />
<ClCompile Include="..\optlib\meson.c" />
Expand Down
Loading