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

Added Kotlin lexer #354

Closed
wants to merge 3 commits into from
Closed

Conversation

citizenmatt
Copy link
Contributor

Based on Pygments parser, and version converted by @hastebrot.
Fixes #295

@hastebrot
Copy link

Any idea how I get the function generics highlighted as Punctuation, Name, Punctuation?

fun foo(arg: String) {
    println(arg)
}

fun <T> bar(arg: List<T>) {
    println(arg)
}

fun <S, T> baz(arg: Pair<S, T>) {
    println(arg)
}

@hastebrot
Copy link

Here is a lazy incomplete solution for a function state/rule:

rule %r'(fun)(\s+)' do
  groups Keyword, Text
  push :function
end

# ...

state :function do
  rule %r'(<)(.+)(>)(\s+)' do
    groups Operator, Name, Operator, Text
  end
  rule /#{id}/, Name::Function, :pop!
end

This needs to be improved in order to correctly highlight fun<S, T>foo(), fun <S, T> foo() and fun <S,\nT> foo().

@hastebrot
Copy link

Changes:

state :root do
  rule %r'(fun)' do token Keyword; push :function end
  # ...
end

state :function do
  rule /</, Punctuation, :function_type_params
  rule /\s+/, Text
  rule /#{id}(\.)/ do groups Name::Class, Operator end
  rule /#{id}/, Name::Function, :pop!
end

state :function_type_params do
  rule />/, Punctuation, :pop!
  mixin :root
end

Samples:

val foo: String = "foo"
val bar: List<String> = listOf("bar")
val baz: Pair<Int, String> = 42 to "baz"

fun foo(arg: String) {}
fun <T> bar(arg: List<T>) {}
fun <S, T> baz(arg: Pair<S, T>) {}

fun<T>Quux.foo() {}
fun <T : Any> Quux.bar() {}
inline fun <reified T : Any> Quux.baz() {}

module Rouge
module Lexers
class Kotlin < RegexLexer
title "Java"

Choose a reason for hiding this comment

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

"Kotlin" not "Java".

@dimsuz
Copy link

dimsuz commented Apr 1, 2016

Hello! What needs to be done for this to be merged?
Kotlin is gaining popularity and it would be really nice to have syntax highlighting support for it.

@ElMassimo
Copy link

Yep, specially since Pygments has supported Kotlin for a while, and Rouge aims to be a drop-in replacement with Pygments.

Having language parity with Pygments is even more important since GitHub pages will only support Rouge starting May 1st.

@davidschreiber
Copy link

👍 Pushing for this PR as well. Would love too see Kotlin support in Rouge.

@hastebrot
Copy link

hastebrot commented Apr 5, 2016

Here is list of remaining issues (tested with http://try.kotlinlang.org/#/Examples/Longer%20examples/Life/Life.kt):

  • identifiers for class, interface, object, package, import, val, var, fun use the rule for Java identifiers.
  • multi-line strings are not supported.
  • 2..3 matches 2. as Number (CSS class m).
  • matches non-keyword data as keyword.
  • function generics are not supported (fun <T> foo() {}).

Update: This was also mentioned in the related issue: #295

@jneen
Copy link
Member

jneen commented Jun 3, 2016

Hi! As I'm sure you know, writing lexers takes a lot of time and work, and having language parity with Pygments is currently a rather unrealistic goal. But I do hope to merge the Kotlin lexer soon!

Since #476 includes this work, I'm closing this PR in favor of that one.

@jneen jneen closed this Jun 3, 2016
lucidbee pushed a commit to BonsaiAI/rouge that referenced this pull request Mar 16, 2017
upgraded to wily to remove error from version trusty
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants