Skip to content

Enhance/fix string interpolation model #20

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

Merged
merged 1 commit into from
Mar 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
55 changes: 35 additions & 20 deletions corpus/literals.txt
Original file line number Diff line number Diff line change
@@ -1,43 +1,58 @@
==========================
Multi-line strings
Simple strings
==========================

val string = """
val oneLineString = "I'm just on one line"

val multiLineString = """
a
b
c
$thisIsntInterpolated
${thisEither}
"""

---

(compilation_unit
(val_definition (identifier) (string))
(val_definition (identifier) (string)))

==========================
Interpolated strings
==========================

val string1 = "a $b c ${d} e"
val string1 = s"a $b ${c}"

val string2 = "$a"
val string2 = f"hi $name%s"
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

One future thing we can enhance even further here is identifying the formatters as identifiers, but for now there are a ton of other things that are probably more important to tackle than fine graining this.


val string3 = """
a
$b
c
${d}
"""
val string3 = raw"Not a new line \n${ha}"

val string4 = s"""
works even in multiline strings, ${name}
"""
---

(compilation_unit
(val_definition (identifier) (string
(interpolation (identifier))
(interpolation (block (identifier)))))
(val_definition (identifier) (string (interpolation (identifier))))
(val_definition (identifier) (string
(interpolation (identifier))
(interpolation (block (identifier))))))
(val_definition
(identifier)
(interpolated_string_expression
(identifier) (interpolated_string
(interpolation (identifier))
(interpolation (block (identifier))))))
(val_definition
(identifier)
(interpolated_string_expression
(identifier) (interpolated_string
(interpolation (identifier)))))
(val_definition
(identifier)
(interpolated_string_expression
(identifier) (interpolated_string
(interpolation (block (identifier))))))
(val_definition
(identifier)
(interpolated_string_expression
(identifier) (interpolated_string
(interpolation (block (identifier)))))))

==========================
Floating point numbers
Expand All @@ -48,4 +63,4 @@ val string = 3.14
---

(compilation_unit
(val_definition (identifier) (number)))
(val_definition (identifier) (number)))
6 changes: 3 additions & 3 deletions corpus/patterns.txt
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,13 @@ def showNotification(notification: Notification): String = {
(match_expression (identifier) (case_block
(case_clause
(case_class_pattern (type_identifier) (identifier) (identifier) (wildcard))
(string_transform_expression (identifier) (string (interpolation (identifier)) (interpolation (identifier)))))
(interpolated_string_expression (identifier) (interpolated_string (interpolation (identifier)) (interpolation (identifier)))))
(case_clause
(case_class_pattern (type_identifier) (identifier) (identifier))
(string_transform_expression (identifier) (string (interpolation (identifier)) (interpolation (identifier)))))
(interpolated_string_expression (identifier) (interpolated_string (interpolation (identifier)) (interpolation (identifier)))))
(case_clause
(case_class_pattern (type_identifier) (identifier) (identifier))
(string_transform_expression (identifier) (string (interpolation (identifier)) (interpolation (identifier))))))))))
(interpolated_string_expression (identifier) (interpolated_string (interpolation (identifier)) (interpolation (identifier))))))))))

============================
Infix patterns
Expand Down
50 changes: 28 additions & 22 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@ module.exports = grammar({
externals: $ => [
$._automatic_semicolon,
$._simple_string,
$._string_start,
$._string_middle,
$._string_end,
$._multiline_string_start,
$._multiline_string_middle,
$._multiline_string_end,
$._simple_multiline_string,
$._interpolated_string_middle,
$._interpolated_string_end,
$._interpolated_multiline_string_middle,
$._interpolated_multiline_string_end,
'else',
'catch',
'finally'
Expand Down Expand Up @@ -509,7 +508,7 @@ module.exports = grammar({
$.generic_function,
$.assignment_expression,
$.parenthesized_expression,
$.string_transform_expression,
$.interpolated_string_expression,
$.field_expression,
$.instance_expression,
// TODO: postfix and ascription
Expand Down Expand Up @@ -641,34 +640,41 @@ module.exports = grammar({

number: $ => /[\d\.]+/,

string_transform_expression: $ => seq(
interpolated_string_expression: $ => seq(
$.identifier,
$.string
$.interpolated_string
),

string: $ => choice(
$._simple_string,
_interpolated_string_start: $ => '"',

_interpolated_multiline_string_start: $ => '"""',

interpolation: $ => seq('$', choice($.identifier, $.block)),

interpolated_string: $ => choice(
seq(
$._string_start,
$.interpolation,
$._interpolated_string_start,
repeat(seq(
$._string_middle,
$.interpolation,
$._interpolated_string_middle,
$.interpolation
)),
$._string_end
$._interpolated_string_end
),
seq(
$._multiline_string_start,
$.interpolation,
$._interpolated_multiline_string_start,
repeat(seq(
$._multiline_string_middle,
$.interpolation,
$._interpolated_multiline_string_middle,
$.interpolation
)),
$._multiline_string_end
$._interpolated_multiline_string_end
)
),

interpolation: $ => seq('$', choice($.identifier, $.block)),
string :$ =>
choice(
$._simple_string,
$._simple_multiline_string
),

_semicolon: $ => choice(
';',
Expand Down
91 changes: 48 additions & 43 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -2648,7 +2648,7 @@
},
{
"type": "SYMBOL",
"name": "string_transform_expression"
"name": "interpolated_string_expression"
},
{
"type": "SYMBOL",
Expand Down Expand Up @@ -3338,7 +3338,7 @@
"type": "PATTERN",
"value": "[\\d\\.]+"
},
"string_transform_expression": {
"interpolated_string_expression": {
"type": "SEQ",
"members": [
{
Expand All @@ -3347,27 +3347,49 @@
},
{
"type": "SYMBOL",
"name": "string"
"name": "interpolated_string"
}
]
},
"string": {
"type": "CHOICE",
"_interpolated_string_start": {
"type": "STRING",
"value": "\""
},
"_interpolated_multiline_string_start": {
"type": "STRING",
"value": "\"\"\""
},
"interpolation": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_simple_string"
"type": "STRING",
"value": "$"
},
{
"type": "SEQ",
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_string_start"
"name": "identifier"
},
{
"type": "SYMBOL",
"name": "interpolation"
"name": "block"
}
]
}
]
},
"interpolated_string": {
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_interpolated_string_start"
},
{
"type": "REPEAT",
Expand All @@ -3376,7 +3398,7 @@
"members": [
{
"type": "SYMBOL",
"name": "_string_middle"
"name": "_interpolated_string_middle"
},
{
"type": "SYMBOL",
Expand All @@ -3387,7 +3409,7 @@
},
{
"type": "SYMBOL",
"name": "_string_end"
"name": "_interpolated_string_end"
}
]
},
Expand All @@ -3396,11 +3418,7 @@
"members": [
{
"type": "SYMBOL",
"name": "_multiline_string_start"
},
{
"type": "SYMBOL",
"name": "interpolation"
"name": "_interpolated_multiline_string_start"
},
{
"type": "REPEAT",
Expand All @@ -3409,7 +3427,7 @@
"members": [
{
"type": "SYMBOL",
"name": "_multiline_string_middle"
"name": "_interpolated_multiline_string_middle"
},
{
"type": "SYMBOL",
Expand All @@ -3420,31 +3438,22 @@
},
{
"type": "SYMBOL",
"name": "_multiline_string_end"
"name": "_interpolated_multiline_string_end"
}
]
}
]
},
"interpolation": {
"type": "SEQ",
"string": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "$"
"type": "SYMBOL",
"name": "_simple_string"
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "SYMBOL",
"name": "block"
}
]
"type": "SYMBOL",
"name": "_simple_multiline_string"
}
]
},
Expand Down Expand Up @@ -3528,27 +3537,23 @@
},
{
"type": "SYMBOL",
"name": "_string_start"
},
{
"type": "SYMBOL",
"name": "_string_middle"
"name": "_simple_multiline_string"
},
{
"type": "SYMBOL",
"name": "_string_end"
"name": "_interpolated_string_middle"
},
{
"type": "SYMBOL",
"name": "_multiline_string_start"
"name": "_interpolated_string_end"
},
{
"type": "SYMBOL",
"name": "_multiline_string_middle"
"name": "_interpolated_multiline_string_middle"
},
{
"type": "SYMBOL",
"name": "_multiline_string_end"
"name": "_interpolated_multiline_string_end"
},
{
"type": "STRING",
Expand Down
Loading