Skip to content

Commit

Permalink
Merge pull request #780 from MakeNowJust/crystal-lang
Browse files Browse the repository at this point in the history
Crystal support
  • Loading branch information
Golmote committed Sep 27, 2015
2 parents c48f55d + 239b95f commit 5b473de
Show file tree
Hide file tree
Showing 8 changed files with 206 additions and 0 deletions.
5 changes: 5 additions & 0 deletions components.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ var components = {
"require": "javascript",
"owner": "R-osey"
},
"crystal": {
"title": "Crystal",
"require": "ruby",
"owner": "MakeNowJust"
},
"css-extras": {
"title": "CSS Extras",
"require": "css",
Expand Down
53 changes: 53 additions & 0 deletions components/prism-crystal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
(function(Prism) {
Prism.languages.crystal = Prism.languages.extend('ruby', {
keyword: [
/\b(?:abstract|alias|as|asm|begin|break|case|class|def|do|else|elsif|end|ensure|enum|extend|for|fun|if|ifdef|include|instance_sizeof|lib|macro|module|next|of|out|pointerof|private|protected|rescue|return|require|self|sizeof|struct|super|then|type|typeof|undef|union|unless|until|when|while|with|yield|__DIR__|__FILE__|__LINE__)\b/,
{
pattern: /(\.\s*)(?:is_a|responds_to)\?/,
lookbehind: true
}
],

number: /\b(?:0b[01_]*[01]|0o[0-7_]*[0-7]|0x[0-9a-fA-F_]*[0-9a-fA-F]|(?:[0-9](?:[0-9_]*[0-9])?)(?:\.[0-9_]*[0-9])?(?:[eE][+-]?[0-9_]*[0-9])?)(?:_(?:[uif](?:8|16|32|64))?)?\b/,
});

var rest = Prism.util.clone(Prism.languages.crystal);

Prism.languages.insertBefore('crystal', 'string', {
attribute: {
pattern: /@\[.+?\]/,
alias: 'attr-name',
inside: {
delimiter: {
pattern: /^@\[|\]$/,
alias: 'tag'
},
rest: rest
}
},

expansion: [
{
pattern: /\{\{.+?\}\}/,
inside: {
delimiter: {
pattern: /^\{\{|\}\}$/,
alias: 'tag'
},
rest: rest
}
},
{
pattern: /\{%.+?%\}/,
inside: {
delimiter: {
pattern: /^\{%|%\}$/,
alias: 'tag'
},
rest: rest
}
}
]
});

}(Prism));
1 change: 1 addition & 0 deletions components/prism-crystal.min.js

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

33 changes: 33 additions & 0 deletions examples/prism-crystal.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<h1>Crystal</h1>
<p>To use this language, use the class "language-crystal".</p>

<h2>Number literals with underscores and postfix</h2>
<pre><code>1_u32
123_456.789e-10_f64</code></pre>

<h2>Attributes</h2>
<pre><code>@[AlwaysInline]
def foo
1
end</code></pre>

<h2>Macro expansions</h2>
<pre><code>{% for key, value in {foo: 100, bar: 20} %}
def {{ key.id }}
{{ value }}
end
{% end %}</code></pre>

<h2>Known failures</h2>
<p>There are certain edge cases where Prism will fail.
There are always such cases in every regex-based syntax highlighter.
However, Prism dares to be open and honest about them.
If a failure is listed here, it doesn’t mean it will never be fixed. This is more of a “known bugs” list, just with a certain type of bug.
</p>

<h3>Attriubte-like substrings</h3>
<pre><code>" @[AlwaysInline] "</code></pre>

<h3>Macro expansions in strings</h3>
<pre><code>" {{ foo }} "
" {% bar %} "</code></pre>
21 changes: 21 additions & 0 deletions tests/languages/crystal/attribute_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
@[AlwaysInline]
@[CallConvention("X86_StdCall")]

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

[
["attribute", [
["delimiter", "@["],
["constant", "AlwaysInline"],
["delimiter", "]"]
]],
["attribute", [
["delimiter", "@["],
["function", "CallConvention"], ["punctuation", "("], ["string", [ "\"X86_StdCall\"" ]], ["punctuation", ")"],
["delimiter", "]"]
]]
]

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

Checks for attributes.
37 changes: 37 additions & 0 deletions tests/languages/crystal/expansion_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{{ 1_u32 }}
{% 2_u32 %}
{{ { 3_u32 } }}
{% % 4_u32 % %}

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

[
["expansion", [
["delimiter", "{{"],
["number", "1_u32"],
["delimiter", "}}"]
]],
["expansion", [
["delimiter", "{%"],
["number", "2_u32"],
["delimiter", "%}"]
]],
["expansion", [
["delimiter", "{{"],
["punctuation", "{"],
["number", "3_u32"],
["punctuation", "}"],
["delimiter", "}}"]
]],
["expansion", [
["delimiter", "{%"],
["operator", "%"],
["number", "4_u32"],
["operator", "%"],
["delimiter", "%}"]
]]
]

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

Checks for macro expansions.
33 changes: 33 additions & 0 deletions tests/languages/crystal/keyword_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
abstract alias as asm begin break case
class;
def do else elsif
end ensure enum extend for fun
if ifdef include instance_sizeof
.is_a?
lib macro module next of out pointerof
private protected rescue
.responds_to?
return require self sizeof struct super
then type typeof undef union unless until when
while with yield __DIR__ __FILE__ __LINE__

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

[
["keyword", "abstract"], ["keyword", "alias"], ["keyword", "as"], ["keyword", "asm"], ["keyword", "begin"], ["keyword", "break"], ["keyword", "case"],
["keyword", "class"], ["punctuation", ";"],
["keyword", "def"], ["keyword", "do"], ["keyword", "else"], ["keyword", "elsif"],
["keyword", "end"], ["keyword", "ensure"], ["keyword", "enum"], ["keyword", "extend"], ["keyword", "for"], ["keyword", "fun"],
["keyword", "if"], ["keyword", "ifdef"], ["keyword", "include"], ["keyword", "instance_sizeof"],
["punctuation", "."], ["keyword", "is_a?"],
["keyword", "lib"], ["keyword", "macro"], ["keyword", "module"], ["keyword", "next"], ["keyword", "of"], ["keyword", "out"], ["keyword", "pointerof"],
["keyword", "private"], ["keyword", "protected"], ["keyword", "rescue"],
["punctuation", "."], ["keyword", "responds_to?"],
["keyword", "return"], ["keyword", "require"], ["keyword", "self"], ["keyword", "sizeof"], ["keyword", "struct"], ["keyword", "super"],
["keyword", "then"], ["keyword", "type"], ["keyword", "typeof"], ["keyword", "undef"], ["keyword", "union"], ["keyword", "unless"], ["keyword", "until"], ["keyword", "when"],
["keyword", "while"], ["keyword", "with"], ["keyword", "yield"], ["keyword", "__DIR__"], ["keyword", "__FILE__"], ["keyword", "__LINE__"]
]

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

Checks for all keywords.
23 changes: 23 additions & 0 deletions tests/languages/crystal/number_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
1
1_1
1_1_1
0b10_01
0o1_2_3
0x123456789abcdef
012_345.678_9e+10_f64

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

[
["number", "1"],
["number", "1_1"],
["number", "1_1_1"],
["number", "0b10_01"],
["number", "0o1_2_3"],
["number", "0x123456789abcdef"],
["number", "012_345.678_9e+10_f64"]
]

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

Checks for number literals.

0 comments on commit 5b473de

Please sign in to comment.