Skip to content

Commit

Permalink
Remove escape sequences from raw strings (Fixes #67) (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
nishtahir authored May 19, 2023
1 parent f47a0c4 commit bad0234
Show file tree
Hide file tree
Showing 13 changed files with 186 additions and 56 deletions.
30 changes: 29 additions & 1 deletion dist/Kotlin.JSON-tmLanguage
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@
"name": "string.quoted.triple.kotlin",
"patterns": [
{
"include": "#string-content"
"include": "#raw-string-content"
}
]
},
Expand Down Expand Up @@ -841,6 +841,34 @@
}
],
"repository": {
"raw-string-content": {
"patterns": [
{
"begin": "\\$(\\{)",
"beginCaptures": {
"1": {
"name": "punctuation.section.block.begin.kotlin"
}
},
"end": "\\}",
"endCaptures": {
"0": {
"name": "punctuation.section.block.end.kotlin"
}
},
"name": "entity.string.template.element.kotlin",
"patterns": [
{
"include": "#code"
}
]
},
{
"match": "\\$[a-zA-Z_]\\w*",
"name": "entity.string.template.element.kotlin"
}
]
},
"string-content": {
"patterns": [
{
Expand Down
3 changes: 2 additions & 1 deletion dist/Kotlin.YAML-tmLanguage
Original file line number Diff line number Diff line change
Expand Up @@ -324,10 +324,11 @@ repository:
- {match: '\b([0-9][0-9_]*([fFLuU]|[uU]L)?)\b', name: constant.numeric.integer.kotlin}
string:
patterns:
- {begin: '"""', beginCaptures: {'0': {name: punctuation.definition.string.begin.kotlin}}, end: '"""(?!")', endCaptures: {'0': {name: punctuation.definition.string.end.kotlin}}, name: string.quoted.triple.kotlin, patterns: [{include: '#string-content'}]}
- {begin: '"""', beginCaptures: {'0': {name: punctuation.definition.string.begin.kotlin}}, end: '"""(?!")', endCaptures: {'0': {name: punctuation.definition.string.end.kotlin}}, name: string.quoted.triple.kotlin, patterns: [{include: '#raw-string-content'}]}
- {begin: '(?!'')"', beginCaptures: {'0': {name: punctuation.definition.string.begin.kotlin}}, end: '"', endCaptures: {'0': {name: punctuation.definition.string.end.kotlin}}, name: string.quoted.double.kotlin, patterns: [{include: '#string-content'}]}
- {begin: '''', beginCaptures: {'0': {name: punctuation.definition.string.begin.kotlin}}, end: '''', endCaptures: {'0': {name: punctuation.definition.string.end.kotlin}}, name: string.quoted.single.kotlin, patterns: [{include: '#string-content'}]}
repository:
raw-string-content: {patterns: [{begin: '\$(\{)', beginCaptures: {'1': {name: punctuation.section.block.begin.kotlin}}, end: '\}', endCaptures: {'0': {name: punctuation.section.block.end.kotlin}}, name: entity.string.template.element.kotlin, patterns: [{include: '#code'}]}, {match: '\$[a-zA-Z_]\w*', name: entity.string.template.element.kotlin}]}
string-content: {patterns: [{match: '\\[0\\tnr"'']', name: constant.character.escape.kotlin}, {match: '\\(x[\da-fA-F]{2}|u[\da-fA-F]{4}|.)', name: constant.character.escape.unicode.kotlin}, {begin: '\$(\{)', beginCaptures: {'1': {name: punctuation.section.block.begin.kotlin}}, end: '\}', endCaptures: {'0': {name: punctuation.section.block.end.kotlin}}, name: entity.string.template.element.kotlin, patterns: [{include: '#code'}]}, {match: '\$[a-zA-Z_]\w*', name: entity.string.template.element.kotlin}]}
'null':
patterns:
Expand Down
45 changes: 44 additions & 1 deletion dist/Kotlin.tmLanguage
Original file line number Diff line number Diff line change
Expand Up @@ -1200,7 +1200,7 @@
<array>
<dict>
<key>include</key>
<string>#string-content</string>
<string>#raw-string-content</string>
</dict>
</array>
</dict>
Expand Down Expand Up @@ -1269,6 +1269,49 @@
</array>
<key>repository</key>
<dict>
<key>raw-string-content</key>
<dict>
<key>patterns</key>
<array>
<dict>
<key>begin</key>
<string>\$(\{)</string>
<key>beginCaptures</key>
<dict>
<key>1</key>
<dict>
<key>name</key>
<string>punctuation.section.block.begin.kotlin</string>
</dict>
</dict>
<key>end</key>
<string>\}</string>
<key>endCaptures</key>
<dict>
<key>0</key>
<dict>
<key>name</key>
<string>punctuation.section.block.end.kotlin</string>
</dict>
</dict>
<key>name</key>
<string>entity.string.template.element.kotlin</string>
<key>patterns</key>
<array>
<dict>
<key>include</key>
<string>#code</string>
</dict>
</array>
</dict>
<dict>
<key>match</key>
<string>\$[a-zA-Z_]\w*</string>
<key>name</key>
<string>entity.string.template.element.kotlin</string>
</dict>
</array>
</dict>
<key>string-content</key>
<dict>
<key>patterns</key>
Expand Down
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@
"url": "https://github.com/nishtahir/language-kotlin/issues"
},
"scripts": {
"build": "node scripts/build.js",
"build:snapshot": "npx vscode-tmgrammar-snap -u -g dist/Kotlin.tmLanguage -s source.kotlin \"snapshots/**/*.kt\"",
"build:grammar": "node scripts/build.js",
"build": "npm run build:grammar && npm run build:snapshot",
"format": "node scripts/format.js",
"coverage": "node scripts/coverage.js && npx codecov",
"sample": "npx vscode-tmgrammar-snap --help",
"snapshot": "npx vscode-tmgrammar-snap -g dist/Kotlin.tmLanguage -s source.kotlin \"snapshots/**/*.kt\"",
"grammar-test": "npx vscode-tmgrammar-test -g dist/Kotlin.tmLanguage \"test/**/*.test.kt\"",
"test": "npm run grammar-test && npm run snapshot"
"test:snapshot": "npx vscode-tmgrammar-snap -g dist/Kotlin.tmLanguage -s source.kotlin \"snapshots/**/*.kt\"",
"test:grammar": "npx vscode-tmgrammar-test -g dist/Kotlin.tmLanguage \"test/**/*.test.kt\"",
"test": "npm run test:grammar && npm run test:snapshot"
},
"devDependencies": {
"codecov": "^3.8.3",
Expand Down
8 changes: 8 additions & 0 deletions scripts/build.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/**
* This script is used to generate the final syntax files from the source files.
* It merges all source files into a single file and generates the final syntax files.
* - Kotlin.JSON-tmLanguage
* - Kotlin.YAML-tmLanguage
* - Kotlin.tmLanguage
*/

const fs = require("fs");
const path = require("path");
const plist = require("plist");
Expand Down
11 changes: 11 additions & 0 deletions scripts/coverage.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
/**
* This script generates a naive coverage report for the Kotlin.tmlanguage file.
* It is used to ensure that all scopes are tested.
*
* The script works by instrumenting the compiled Kotlin.tmlanguage file with
* coverage tracking scopes. It then runs all tests against the instrumented file
* and generates a coverage report.
*
* It does not consider the inner structure of the scopes, only that they are hit.
*/

const path = require("path");
const plist = require("plist");
const {
Expand Down
5 changes: 5 additions & 0 deletions scripts/format.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* This script formats all YAML files in the src/ directory.
* It is used to ensure a consistent code style.
*/

const fs = require("fs");
const path = require("path");
const yaml = require("yamljs");
Expand Down
4 changes: 2 additions & 2 deletions src/classes.YAML-tmLanguage
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ repository:
-
include: '#keywords'
-
begin: \b((?:(?:data|value)\s+)?class|(?:(?:fun|value)\s+)?interface)\b\s+(\w+)
begin: '\b((?:(?:data|value)\s+)?class|(?:(?:fun|value)\s+)?interface)\b\s+(\w+)'
beginCaptures:
'1':
name: storage.modifier.kotlin
Expand Down Expand Up @@ -76,5 +76,5 @@ repository:
name: punctuation.seperator.kotlin
-
include: '#types'
-
-
include: '#literals'
75 changes: 37 additions & 38 deletions src/generic.YAML-tmLanguage
Original file line number Diff line number Diff line change
@@ -1,38 +1,37 @@
repository:
generic:
patterns:
-
begin: '(?=\<(?:[A-Z_]|\*|in|out))'
end: '(?<=\>)(?!\>)'
patterns:
-
match: '<'
name: punctuation.bracket.angle.begin.kotlin
-
match: '>'
name: punctuation.bracket.angle.end.kotlin
-
match: '\*'
name: entity.name.type.generic.wildcard.kotlin
-
include: '#generic-parameter-list'
-
match: ','
name: punctuation.seperator.kotlin

repository:
generic-parameter-list:
patterns:
-
include: '#annotations'
-
match: '\b(in|out)\b'
name: storage.modifier.generic.variance.kotlin
-
include: '#built-in-types'
-
include: '#class-ident'
-
include: '#generic'
-
include: '#operators'
repository:
generic:
patterns:
-
begin: '(?=\<(?:[A-Z_]|\*|in|out))'
end: '(?<=\>)(?!\>)'
patterns:
-
match: '<'
name: punctuation.bracket.angle.begin.kotlin
-
match: '>'
name: punctuation.bracket.angle.end.kotlin
-
match: '\*'
name: entity.name.type.generic.wildcard.kotlin
-
include: '#generic-parameter-list'
-
match: ','
name: punctuation.seperator.kotlin
repository:
generic-parameter-list:
patterns:
-
include: '#annotations'
-
match: \b(in|out)\b
name: storage.modifier.generic.variance.kotlin
-
include: '#built-in-types'
-
include: '#class-ident'
-
include: '#generic'
-
include: '#operators'
2 changes: 0 additions & 2 deletions src/ident.YAML-tmLanguage
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# remove match to original master branch

repository:
class-ident:
patterns:
Expand Down
9 changes: 4 additions & 5 deletions src/keywords.YAML-tmLanguage
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ repository:
match: \b(\_)\b
name: punctuation.definition.variable.kotlin
-
match: \b(tailrec|operator|infix|typealias|reified|copy(?=\s+fun|\s+var))\b
match: '\b(tailrec|operator|infix|typealias|reified|copy(?=\s+fun|\s+var))\b'
name: storage.type.kotlin
-
match: \b(out|in|yield|typealias|override)\b
name: storage.modifier.kotlin
-
match: \b(?<![+-/%*=(,]\s)(inline|inner|external|public|private|protected|internal|abstract|final|sealed|enum|open|annotation|expect|actual|const|lateinit)(?=\s(?!(?:\s*)(?:[+-/%*=:).,]|$)))\b
match: '\b(?<![+-/%*=(,]\s)(inline|inner|external|public|private|protected|internal|abstract|final|sealed|enum|open|annotation|expect|actual|const|lateinit)(?=\s(?!(?:\s*)(?:[+-/%*=:).,]|$)))\b'
name: storage.modifier.kotlin
comment: 'Soft modifiers'
-
match: \b(vararg(?=\s+\w+:))\b
match: '\b(vararg(?=\s+\w+:))\b'
name: storage.modifier.kotlin
comment: 'Soft modifier'
-
match: \b(suspend(?!\s*[\(]?\s*\{))\b
match: '\b(suspend(?!\s*[\(]?\s*\{))\b'
name: storage.modifier.kotlin
comment: 'Soft modifier'
-
Expand All @@ -46,7 +46,6 @@ repository:
-
match: \b(companion|object)\b
name: storage.type.kotlin

operators:
patterns:
-
Expand Down
22 changes: 21 additions & 1 deletion src/literals.YAML-tmLanguage
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ repository:
name: string.quoted.triple.kotlin
patterns:
-
include: '#string-content'
include: '#raw-string-content'
-
begin: '(?!'')"'
beginCaptures:
Expand Down Expand Up @@ -71,6 +71,26 @@ repository:
-
include: '#string-content'
repository:
# https://kotlinlang.org/docs/strings.html#raw-strings
# Raw strings do not contain escape sequences.
raw-string-content:
patterns:
-
begin: '\$(\{)'
beginCaptures:
'1':
name: punctuation.section.block.begin.kotlin
end: '\}'
endCaptures:
'0':
name: punctuation.section.block.end.kotlin
name: entity.string.template.element.kotlin
patterns:
-
include: '#code'
-
match: '\$[a-zA-Z_]\w*'
name: entity.string.template.element.kotlin
string-content:
patterns:
-
Expand Down
17 changes: 17 additions & 0 deletions test/regression/67.test.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// SYNTAX TEST "source.kotlin" "Triple-quoted strings do not ignore backslash before closing sequence"

>val cmd = """bash -c "sudo -u test bash --noprofile --norc -euo pipefail "\"""
#^^^ source.kotlin storage.type.kotlin
# ^^^^^ source.kotlin
# ^ source.kotlin keyword.operator.assignment.kotlin
# ^ source.kotlin
# ^^^ source.kotlin string.quoted.triple.kotlin punctuation.definition.string.begin.kotlin
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ source.kotlin string.quoted.triple.kotlin
# ^^^ source.kotlin string.quoted.triple.kotlin punctuation.definition.string.end.kotlin
>val num = 1
#^^^ source.kotlin storage.type.kotlin
# ^^^^^ source.kotlin
# ^ source.kotlin keyword.operator.assignment.kotlin
# ^ source.kotlin
# ^ source.kotlin constant.numeric.integer.kotlin
>

0 comments on commit bad0234

Please sign in to comment.