Skip to content

Commit

Permalink
Ruby: Add heredoc literals
Browse files Browse the repository at this point in the history
Adds support for basic 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

There are several more styles of heredocs to add as well.
  • Loading branch information
sj26 committed May 3, 2021
1 parent 4914178 commit 0d083fd
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
15 changes: 15 additions & 0 deletions components/prism-ruby.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,21 @@
inside: {
'interpolation': interpolation
}
},
{
pattern: /<<([a-z_]\w*)[\r\n](?:.*[\r\n])*?\1/i,
alias: 'heredoc-string',
greedy: true,
inside: {
'delimiter': {
pattern: /^<<[a-z_]\w*|[a-z_]\w*$/i,
alias: 'symbol',
inside: {
'punctuation': /^<<([-~]?)/
}
},
'interpolation': interpolation
}
}
];

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
20 changes: 19 additions & 1 deletion tests/languages/ruby/string_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ bar"
%x[foo #{ 42 }]
%x<foo #{ 42 }>

<<STRING
foo #{42} bar
STRING

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

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

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

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

0 comments on commit 0d083fd

Please sign in to comment.