Skip to content

Kotlin scanner #213

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 5 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
Next Next commit
Kotlin: initial scanner implementation
  • Loading branch information
Sergey Mashkov committed Aug 6, 2017
commit 8defb26516e72915ba38f74286b2ddce2cf99aa5
2 changes: 2 additions & 0 deletions lib/coderay/helpers/file_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ def type_from_shebang filename
'java' => :java,
'js' => :java_script,
'json' => :json,
'kt' => :kotlin,
'kts' => :kotlin,
'lua' => :lua,
'mab' => :ruby,
'pas' => :delphi,
Expand Down
163 changes: 163 additions & 0 deletions lib/coderay/scanners/kotlin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
module CodeRay
module Scanners

load :java

class Kotlin < Java

register_for :kotlin
file_extension 'kt'

KOTLIN_KEYWORDS = %w[
package import
as? as is
val var
class interface object fun init get set
in out
if when else for while do return break continue
]

KOTLIN_MODIFIERS = %w[
annotation enum data sealed companion
abstract open final
public protected private internal
inline suspend
inner
]

TYPES = %w[
Boolean Byte Char class Double Float Int Long Short Unit Nothing Any
]

STRING_CONTENT_PATTERN = {
"'" => /[^\\'$]+/,
'"' => /[^\\"$]+/,
} # :nodoc:s

IDENT_KIND = Java::IDENT_KIND.dup.
add(TYPES, :type).
add(KOTLIN_KEYWORDS, :keyword).
add(KOTLIN_MODIFIERS, :keyword) # :nodoc:


def setup
@state = :initial
end

def scan_tokens encoder, options
string_delimiter = nil
state = options[:state] || @state
last_token_dot = false
class_name_follows = false
delimiters = []

until eos?

case state

when :initial
if (match = scan(/ \s+ | \\\n /x))
encoder.text_token match, :space
next
elsif (match = scan(%r! // [^\n\\]* (?: \\. [^\n\\]* )* | /\* (?: .*? \*/ | .* ) !mx))
encoder.text_token match, :comment
next
elsif (match = scan(/ TODO \( /ox))
encoder.text_token "TODO", :comment
encoder.text_token "(", :operator
elsif (match = scan(/ #{IDENT} /ox))
kind = IDENT_KIND[match]
if last_token_dot
kind = :ident
elsif class_name_follows
kind = :class
class_name_follows = false
else
case match
when 'import'
package_name_expected = :include
when 'package'
package_name_expected = :namespace
when 'class', 'interface'
class_name_follows = true
else
# nothing
end
end
encoder.text_token match, kind
elsif (match = scan(/ \.(?!\d) | [,?:()\[\]] | -- | \+\+ | && | \|\| | \*\*=? | [-+*\/%^~&|<>=!]=? /x))
encoder.text_token match, :operator
elsif (match = scan(/{/))
class_name_follows = false
encoder.text_token match, :operator
elsif (match = scan(/}/))
encoder.text_token match, :operator

unless delimiters.empty?
string_delimiter = delimiters.pop
encoder.end_group state
state = :string
end
elsif (match = scan(/["']/))
state = :string
encoder.begin_group state
string_delimiter = match
encoder.text_token match, :delimiter
elsif check(/[\d.]/)
if (match = scan(/0[xX][0-9A-Fa-f]+/))
encoder.text_token match, :hex
elsif (match = scan(/(?>0[0-7]+)(?![89.eEfF])/))
encoder.text_token match, :octal
elsif (match = scan(/\d+[fFdD]|\d*\.\d+(?:[eE][+-]?\d+)?[fFdD]?|\d+[eE][+-]?\d+[fFdD]?/))
encoder.text_token match, :float
elsif (match = scan(/\d+[lL]?/))
encoder.text_token match, :integer
end

elsif (match = scan(/ @ #{IDENT} /ox))
encoder.text_token match, :annotation

else
encoder.text_token getch, :error
end

when :string
if (match = scan(/\${/))
encoder.text_token match, :operator

state = :initial
encoder.begin_group state

delimiters << string_delimiter
string_delimiter = nil
elsif (match = scan(/ \$ #{IDENT} /ox))
encoder.text_token match, :ident
elsif (match = scan(STRING_CONTENT_PATTERN[string_delimiter]))
encoder.text_token match, :content
elsif (match = scan(/ \\ (?: #{ESCAPE} | #{UNICODE_ESCAPE} ) /mox))
if string_delimiter == "'" && !(match == "\\\\" || match == "\\'")
encoder.text_token match, :content
else
encoder.text_token match, :char
end
elsif (match = scan(/["']/))
encoder.text_token match, :delimiter
encoder.end_group state
state = :initial
string_delimiter = nil
elsif (match = scan(/ \\ | $ /x))
encoder.end_group state
state = :initial
encoder.text_token match, :error unless match.empty?
else
raise_inspect "else case \" reached; %p not handled." % peek(1), encoder
end
else
raise_inspect 'Unknown state', encoder
end

end
end
end
end
end