Skip to content
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

enh(llvm) Improve grammar and align assembly like grammars #2830

Merged
merged 11 commits into from Nov 13, 2020
95 changes: 61 additions & 34 deletions src/languages/llvm.js
Expand Up @@ -7,7 +7,54 @@ Category: assembler
*/

export default function(hljs) {
var identifier = '([-a-zA-Z$._][\\w\\-$.]*)';
const IDENT_RE = '([-a-zA-Z$._][\\w\\-$.]*)';
const TYPE = {
className: 'type',
begin: /\bi\d+(?=\s|\b)/
};
const OPERATOR = {
className: 'operator',
relevance: 0,
begin: /=/
};
const PUNCTUATION = {
className: 'punctuation',
relevance: 0,
begin: /,/
};
const NUMBER = {
className: 'number',
variants: [
{ begin: '0[xX][a-fA-F0-9]+' },
{ begin: '-?\\d+(?:[.]\\d+)?(?:[eE][-+]?\\d+(?:[.]\\d+)?)?' }
],
relevance: 0
};
const LABEL = {
className: 'symbol',
variants: [
{ begin: /^\s*[a-z]+:/ }, // labels
],
relevance: 0
};
const VARIABLE = {
className: 'variable',
variants: [
{ begin: '%' + IDENT_RE },
{ begin: '%\\d+' },
{ begin: '#\\d+' },
]
};
const FUNCTION = {
className: 'title',
variants: [
{ begin: '@' + IDENT_RE },
{ begin: '@\\d+' },
{ begin: '!' + IDENT_RE },
{ begin: '!\\d+' + IDENT_RE }
]
};

return {
name: 'LLVM IR',
keywords:
Expand Down Expand Up @@ -49,48 +96,28 @@ export default function(hljs) {
'extractvalue insertvalue atomicrmw cmpxchg fence ' +
'argmemonly double',
contains: [
{
className: 'keyword',
begin: 'i\\d+'
},
hljs.COMMENT(
';', '\\n', {relevance: 0}
TYPE,
hljs.COMMENT(/;\s*$/,
// this matches "empty comments"...
// ...because it's far more likely this is a statement terminator in
// another language than an actual comment
{ relevance: 0 }
),
joshgoebel marked this conversation as resolved.
Show resolved Hide resolved
// Double quote string
hljs.COMMENT(/;/, /$/),
hljs.QUOTE_STRING_MODE,
{
className: 'string',
variants: [
// Double-quoted string
{ begin: '"', end: '[^\\\\]"' },
],
relevance: 0
},
{
className: 'title',
variants: [
{ begin: '@' + identifier },
{ begin: '@\\d+' },
{ begin: '!' + identifier },
{ begin: '!\\d+' + identifier }
]
},
{
className: 'symbol',
variants: [
{ begin: '%' + identifier },
{ begin: '%\\d+' },
{ begin: '#\\d+' },
]
},
{
className: 'number',
variants: [
{ begin: '0[xX][a-fA-F0-9]+' },
{ begin: '-?\\d+(?:[.]\\d+)?(?:[eE][-+]?\\d+(?:[.]\\d+)?)?' }
],
relevance: 0
},
FUNCTION,
PUNCTUATION,
OPERATOR,
VARIABLE,
LABEL,
NUMBER
]
};
}
8 changes: 8 additions & 0 deletions test/markup/llvm/simple.expect.txt
@@ -0,0 +1,8 @@
<span class="hljs-comment">;; foooo</span>
<span class="hljs-keyword">define</span> <span class="hljs-type">i32</span> <span class="hljs-title">@mul_add</span>(<span class="hljs-type">i32</span> <span class="hljs-variable">%x</span><span class="hljs-punctuation">,</span> <span class="hljs-type">i32</span> <span class="hljs-variable">%y</span><span class="hljs-punctuation">,</span> <span class="hljs-type">i32</span> <span class="hljs-variable">%z</span>) {
<span class="hljs-symbol"> entry:</span>
<span class="hljs-variable">%tmp</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">mul</span> <span class="hljs-type">i32</span> <span class="hljs-variable">%x</span><span class="hljs-punctuation">,</span> <span class="hljs-variable">%y</span>
<span class="hljs-variable">%tmp2</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">add</span> <span class="hljs-type">i32</span> <span class="hljs-variable">%tmp</span><span class="hljs-punctuation">,</span> <span class="hljs-variable">%z</span>
<span class="hljs-variable">%tmp3</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">add</span> <span class="hljs-type">i32</span> <span class="hljs-variable">%tmp</span><span class="hljs-punctuation">,</span> <span class="hljs-number">0</span>
<span class="hljs-keyword">ret</span> <span class="hljs-type">i32</span> <span class="hljs-variable">%tmp3</span>
}
8 changes: 8 additions & 0 deletions test/markup/llvm/simple.txt
@@ -0,0 +1,8 @@
;; foooo
define i32 @mul_add(i32 %x, i32 %y, i32 %z) {
entry:
%tmp = mul i32 %x, %y
%tmp2 = add i32 %tmp, %z
%tmp3 = add i32 %tmp, 0
ret i32 %tmp3
}