8000 Kotlin by mikrethor · Pull Request #258 · rubychan/coderay · GitHub
[go: up one dir, main page]

Skip to content

Kotlin #258

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

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Kotlin: implement multiline strings, fix nested braces
  • Loading branch information
Sergey Mashkov committed Aug 6, 2017
commit 10ae5207bcc064abc3c7bd9200c23233f3d27247
41 changes: 37 additions & 4 deletions lib/coderay/scanners/kotlin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def scan_tokens encoder, options
last_token_dot = false
class_name_follows = false
delimiters = []
states = []

until eos?

Expand Down Expand Up @@ -90,14 +91,22 @@ def scan_tokens encoder, options
elsif (match = scan(/{/))
class_name_follows = false
encoder.text_token match, :operator
states << :initial
elsif (match = scan(/}/))
encoder.text_token match, :operator

unless delimiters.empty?
string_delimiter = delimiters.pop
encoder.end_group state
state = :string
unless states.empty?
state = states.pop

if state == :string || state == :multiline_string
string_delimiter = delimiters.pop
encoder.end_group :initial
end
end
elsif (match = scan(/"""/))
state = :multiline_string
encoder.begin_group :string
encoder.text_token match, :delimiter
elsif (match = scan(/["']/))
state = :string
encoder.begin_group state
Expand Down Expand Up @@ -129,6 +138,7 @@ def scan_tokens encoder, options
encoder.begin_group state

delimiters << string_d F656 elimiter
states << :string
string_delimiter = nil
elsif (match = scan(/ \$ #{IDENT} /ox))
encoder.text_token match, :ident
Expand All @@ -152,6 +162,29 @@ def scan_tokens encoder, options
else
raise_inspect "else case \" reached; %p not handled." % peek(1), encoder
end
when :multiline_string
if (match = scan(/\${/))
encoder.text_token match, :operator

state = :initial
encoder.begin_group state

delimiters << nil
states << :multiline_string
elsif (match = scan(/ \$ #{IDENT} /ox))
encoder.text_token match, :ident
elsif (match = scan(/ [^$\\"]+ /x))
encoder.text_token match, :content
elsif (match = scan(/"""/x))
encoder.text_token match, :delimiter
encoder.end_group :string
state = :initial
string_delimiter = nil
elsif (match = scan(/"/))
encoder.text_token match, :content
else
raise_inspect "else case \" reached; %p not handled." % peek(1), encoder
end
else
raise_inspect 'Unknown state', encoder
end
Expand Down
7 changes: 7 additions & 0 deletions test/executable/source.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ class Test {
val template2 = "abc${1 + 'b'}def"
val template3 = "abc $var def"

val multiline = """ first line $var ${1 + 1}
second line
and quotes: ' " '' "" ok
"""

val innerBraaces = " before ${ if (true) { 1 } else { 2 } }"

var v: Int = 0

fun function(): ReturnType {
Expand Down
0