Skip to content

Commit

Permalink
kotlin: regex parser
Browse files Browse the repository at this point in the history
  • Loading branch information
dolik-rce committed Dec 22, 2020
1 parent 165f7e3 commit eeb6227
Show file tree
Hide file tree
Showing 21 changed files with 352 additions and 0 deletions.
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: String) = functionReturningPair()$/;" v
h input.kt /^var (g: Int, h: String) = 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: String) = 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"""
1 change: 1 addition & 0 deletions Units/parser-kotlin.r/kotlin-quotes.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-quotes.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 */
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
10 changes: 10 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,10 @@
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
20 changes: 20 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,20 @@
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 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
165 changes: 165 additions & 0 deletions optlib/kotlin.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/*
* 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",
"^val\\s+\\(\\s*",
"", "", "{tenter=vals}", NULL);
addLanguageTagMultiTableRegex (language, "toplevel",
"^var\\s+\\(\\s*",
"", "", "{tenter=vars}", NULL);
addLanguageTagMultiTableRegex (language, "toplevel",
"^class\\s+([[:alnum:]_]+)",
"\\1", "c", "", NULL);
addLanguageTagMultiTableRegex (language, "toplevel",
"^interface\\s+([[:alnum:]_]+)",
"\\1", "i", "", NULL);
addLanguageTagMultiTableRegex (language, "toplevel",
"^object\\s+([[:alnum:]_]+)",
"\\1", "o", "", NULL);
addLanguageTagMultiTableRegex (language, "toplevel",
"^typealias\\s+([[:alnum:]_]+)",
"\\1", "T", "", NULL);
addLanguageTagMultiTableRegex (language, "toplevel",
"^fun(<.*>)*\\s+([[:alnum:]_]+\\.)*([[:alnum:]_]+)",
"\\3", "m", "", NULL);
addLanguageTagMultiTableRegex (language, "toplevel",
"^val\\s+([[:alnum:]_]+\\.)*([[:alnum:]_]+)",
"\\2", "C", "", NULL);
addLanguageTagMultiTableRegex (language, "toplevel",
"^var\\s+([[: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:]_]+)\\s*(:\\s*[[:alnum:]_]+)*\\s*,*\\s*",
"\\1", "C", "", NULL);
addLanguageTagMultiTableRegex (language, "vals",
"^\\)",
"", "", "{tleave}", NULL);
addLanguageTagMultiTableRegex (language, "vars",
"^([[:alnum:]_]+)\\s*(:\\s*[[:alnum:]_]+)*\\s*,*\\s*",
"\\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, '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;
}
91 changes: 91 additions & 0 deletions optlib/kotlin.ctags
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#
# 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.
#

# 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=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

# ignore anything in comment
--_mtable-regex-Kotlin=toplevel/\/\*//{tenter=comment}
--_mtable-regex-Kotlin=toplevel/\/\/[^\n]*\n//
--_mtable-regex-Kotlin=comment/\*\///{tleave}
--_mtable-regex-Kotlin=comment/[^*]+//
--_mtable-regex-Kotlin=comment/.//

# ignore anything in multiline strings
--_mtable-regex-Kotlin=toplevel/"""//{tenter=mlstring}
--_mtable-regex-Kotlin=mlstring/"""//{tleave}
--_mtable-regex-Kotlin=mlstring/[^\\"]+//
--_mtable-regex-Kotlin=mlstring/.//

# ignore anything in strings
--_mtable-regex-Kotlin=toplevel/"//{tenter=string}
--_mtable-regex-Kotlin=string/[^\\"]+//
--_mtable-regex-Kotlin=string/\\.//
--_mtable-regex-Kotlin=string/"//{tleave}
--_mtable-regex-Kotlin=string/.//

# ignore character literals
--_mtable-regex-Kotlin=toplevel/'.{1,2}'//

# handle destructuring declarations
--_mtable-regex-Kotlin=toplevel/val\s+\(\s*//{tenter=vals}
--_mtable-regex-Kotlin=vals/([[:alnum:]_]+)\s*(:\s*[[:alnum:]_]+)*\s*,*\s*/\1/C/
--_mtable-regex-Kotlin=vals/\)//{tleave}
--_mtable-regex-Kotlin=toplevel/var\s+\(\s*//{tenter=vars}
--_mtable-regex-Kotlin=vars/([[:alnum:]_]+)\s*(:\s*[[:alnum:]_]+)*\s*,*\s*/\1/v/
--_mtable-regex-Kotlin=vars/\)//{tleave}

# parse the language constructs
--_mtable-regex-Kotlin=toplevel/class\s+([[:alnum:]_]+)/\1/c/
--_mtable-regex-Kotlin=toplevel/interface\s+([[:alnum:]_]+)/\1/i/
--_mtable-regex-Kotlin=toplevel/object\s+([[:alnum:]_]+)/\1/o/
--_mtable-regex-Kotlin=toplevel/typealias\s+([[:alnum:]_]+)/\1/T/
--_mtable-regex-Kotlin=toplevel/fun(<.*>)*\s+([[:alnum:]_]+\.)*([[:alnum:]_]+)/\3/m/
--_mtable-regex-Kotlin=toplevel/val\s+([[:alnum:]_]+\.)*([[:alnum:]_]+)/\2/C/
--_mtable-regex-Kotlin=toplevel/var\s+([[:alnum:]_]+\.)*([[:alnum:]_]+)/\2/v/

# ignore anything else
--_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
3 changes: 3 additions & 0 deletions win32/ctags_vs2013.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@
<ClCompile Include="..\optlib\kconfig.c">
<Filter>Source Files\optlib</Filter>
</ClCompile>
<ClCompile Include="..\optlib\kotlin.c">
<Filter>Source Files\optlib</Filter>
</ClCompile>
<ClCompile Include="..\optlib\man.c">
<Filter>Source Files\optlib</Filter>
</ClCompile>
Expand Down

0 comments on commit eeb6227

Please sign in to comment.