Skip to content

Commit

Permalink
Ruby: Added heredoc literals (#2885)
Browse files Browse the repository at this point in the history
Adds support for heredoc-style string literals in the Ruby syntax.

https://ruby-doc.org/core-2.7.0/doc/syntax/literals_rdoc.html#label-Here+Documents+-28heredocs-29

This isn't perfect, but it's a good start.
  • Loading branch information
sj26 committed May 14, 2021
1 parent 74edb4e commit 20b77bf
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 3 deletions.
29 changes: 29 additions & 0 deletions components/prism-ruby.js
Expand Up @@ -102,6 +102,35 @@
inside: {
'interpolation': interpolation
}
},
{
pattern: /<<[-~]?([a-z_]\w*)[\r\n](?:.*[\r\n])*?[\t ]*\1/i,
alias: 'heredoc-string',
greedy: true,
inside: {
'delimiter': {
pattern: /^<<[-~]?[a-z_]\w*|[a-z_]\w*$/i,
alias: 'symbol',
inside: {
'punctuation': /^<<[-~]?/
}
},
'interpolation': interpolation
}
},
{
pattern: /<<[-~]?'([a-z_]\w*)'[\r\n](?:.*[\r\n])*?[\t ]*\1/i,
alias: 'heredoc-string',
greedy: true,
inside: {
'delimiter': {
pattern: /^<<[-~]?'[a-z_]\w*'|[a-z_]\w*$/i,
alias: 'symbol',
inside: {
'punctuation': /^<<[-~]?'|'$/,
}
}
}
}
];

Expand Down
2 changes: 1 addition & 1 deletion components/prism-ruby.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion examples/prism-ruby.html
Expand Up @@ -7,7 +7,10 @@ <h2>Comments</h2>

<h2>Strings</h2>
<pre><code>"foo \"bar\" baz"
'foo \'bar\' baz'</code></pre>
'foo \'bar\' baz'
&lt;&lt;STRING
here's some #{string} contents
STRING</code></pre>

<h2>Regular expressions</h2>
<pre><code>/foo?[ ]*bar/</code></pre>
Expand Down
90 changes: 89 additions & 1 deletion tests/languages/ruby/string_feature.test
Expand Up @@ -36,6 +36,25 @@ bar"
%x[foo #{ 42 }]
%x<foo #{ 42 }>

<<STRING
foo #{42} bar
STRING
<<-STRING
foo #{42} bar
STRING
<<~STRING
foo #{42} bar
STRING
<<'STRING'
foo #{42} bar
STRING
<<-'STRING'
foo #{42} bar
STRING
<<~'STRING'
foo #{42} bar
STRING

----------------------------------------------------

[
Expand Down Expand Up @@ -279,9 +298,78 @@ bar"
["delimiter", "}"]
]],
">"
]],
["string", [
["delimiter", [
["punctuation", "<<"],
"STRING"
]],
"\r\n foo ",
["interpolation", [
["delimiter", "#{"],
["number", "42"],
["delimiter", "}"]
]],
" bar\r\n",
["delimiter", ["STRING"]]
]],
["string", [
["delimiter", [
["punctuation", "<<-"],
"STRING"
]],
"\r\n foo ",
["interpolation", [
["delimiter", "#{"],
["number", "42"],
["delimiter", "}"]
]],
" bar\r\n ",
["delimiter", ["STRING"]]
]],
["string", [
["delimiter", [
["punctuation", "<<~"],
"STRING"
]],
"\r\n foo ",
["interpolation", [
["delimiter", "#{"],
["number", "42"],
["delimiter", "}"]
]],
" bar\r\n ",
["delimiter", ["STRING"]]
]],
["string", [
["delimiter", [
["punctuation", "<<'"],
"STRING",
["punctuation", "'"]
]],
"\r\n foo #{42} bar\r\n",
["delimiter", ["STRING"]]
]],
["string", [
["delimiter", [
["punctuation", "<<-'"],
"STRING",
["punctuation", "'"]
]],
"\r\n foo #{42} bar\r\n ",
["delimiter", ["STRING"]]
]],
["string", [
["delimiter", [
["punctuation", "<<~'"],
"STRING",
["punctuation", "'"]
]],
"\r\n foo #{42} bar\r\n ",
["delimiter", ["STRING"]]
]]
]

----------------------------------------------------

Checks for strings and string interpolation.
Checks for strings and string interpolation.

0 comments on commit 20b77bf

Please sign in to comment.