- Notifications
You must be signed in to change notification settings - Fork113
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
mikrethor wants to merge12 commits intorubychan:masterChoose a base branch frommikrethor:kotlin
base:master
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Open
Kotlin#258
Changes fromall commits
Commits
Show all changes
12 commits Select commitHold shift + click to select a range
8defb26 Kotlin: initial scanner implementation
63c5556 Kotlin: prepare smoke test
10ae520 Kotlin: implement multiline strings, fix nested braces
b1fbdab Kotlin: smoke test and expected test data
356644e Kotlin: fix regexp escapes
57ede8c Merge branch 'master' of https://github.com/mikrethor/coderay into ko…
a88b0a9 Formating for to comply with rubocop
04aeafa Indent
26092c3 include?
4ea6ae2 redundant else
b82b9e4 redundant else
1a0c3e4 Missing space after #
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
2 changes: 2 additions & 0 deletions.gitignore
This file contains hidden or 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
2 changes: 2 additions & 0 deletionslib/coderay/helpers/file_type.rb
This file contains hidden or 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
192 changes: 192 additions & 0 deletionslib/coderay/scanners/kotlin.rb
This file contains hidden or 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,192 @@ | ||
| 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 = [] | ||
| states = [] | ||
| 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 | ||
| # noinspection RubyEmptyElseBlockInspection | ||
| case match | ||
| when 'import' | ||
| :include | ||
| when 'package' | ||
| :namespace | ||
| when 'class', 'interface' | ||
| class_name_follows = true | ||
| 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 | ||
| states << :initial | ||
| elsif (match = scan(/\}/)) | ||
| encoder.text_token match, :operator | ||
| unless states.empty? | ||
| state = states.pop | ||
| if [:multiline_string, :string].include? state | ||
| 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 | ||
| 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 | ||
| states << :string | ||
| 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 == "'" && !(%W[\\\\ \\'].include? 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 | ||
| 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 | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
38 changes: 38 additions & 0 deletionstest/executable/source.kt
This file contains hidden or 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,38 @@ | ||
| class Test { | ||
| val text1: String = "abc \' def \u0000 \n \" hehe " | ||
| val text2: List<Char> = listOf('a', '\n', '\'', '"', '\"') | ||
| val numbers = listOf(0, 12, 1.0f, 1.0, 1L, 0x1f, -1, -12, -1.0f, -1.0, -1L) | ||
| val template = "abc${1 + "b"}def" | ||
| val template2 = "abc${1 + 'b'}def" | ||
| val template3 = "abc $var def" | ||
| val multiline = """ first line $var ${1 + 1} | ||
| second line | ||
| and quotes: ' " '' "" ok | ||
| """ | ||
| val innerBraces = " before ${ if (true) { 1 } else { 2 } }" | ||
| var v: Int = 0 | ||
| fun function(): ReturnType { | ||
| } | ||
| fun <T : Any> parametrizedFunction(): T = TODO() | ||
| fun references() { | ||
| super.references() | ||
| this.references() | ||
| } | ||
| @Annotation | ||
| class Annotated | ||
| inner class Inner<in T, out E> | ||
| object O | ||
| companion object { | ||
| } | ||
| } |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
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.