-
Notifications
You must be signed in to change notification settings - Fork 624
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
77fc848
kotlin: regex parser
dolik-rce 2c892b1
Kotlin,refactor: group the code block by table
masatake 9632a36
Kotlin: use [[:space:]] instead of \s
masatake e1ab236
kotlin: improve units
dolik-rce aa1f1cd
kotlin: parse package declarations
dolik-rce File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
f input.kt /^\/* comment *\/ var f = 6 \/* another comment *\/$/;" v |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
3
Units/parser-kotlin.r/kotlin-multilinestrings.d/expected.tags
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
var used1 = "var unused1 = 1" | ||
var used2 = "\"var\" unused2 = 2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
--sort=no |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
# | ||
|
||
# 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/.// |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added