Skip to content

Commit

Permalink
Ruby: Add heredoc literals
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 5, 2021
1 parent 4397635 commit 8ac1c84
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 2 deletions.
29 changes: 29 additions & 0 deletions components/prism-ruby.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,35 @@
inside: {
'interpolation': interpolation
}
},
{
pattern: /<<[-~]?([a-z_]\w*)[\r\n](?:.*[\r\n])*?\s*\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])*?\s*\1/i,
alias: 'heredoc-string',
greedy: true,
inside: {
'delimiter': {
pattern: /^<<[-~]?'[a-z_]\w*'|[a-z_]\w*$/i,
alias: 'symbol',
inside: {
'punctuation': /^<<[-~]?'|'$/,
}
}
}
}
];

Expand Down
5 changes: 4 additions & 1 deletion examples/prism-ruby.html
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 8ac1c84

Please sign in to comment.