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

Add support for V langauge #2687

Merged
merged 7 commits into from Jan 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion components.js

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions components.json
Expand Up @@ -1199,6 +1199,11 @@
"alias": ["uscript", "uc"],
"owner": "RunDevelopment"
},
"v": {
"title": "V",
"require": "clike",
"owner": "taggon"
},
"vala": {
"title": "Vala",
"require": "clike",
Expand Down
79 changes: 79 additions & 0 deletions components/prism-v.js
@@ -0,0 +1,79 @@
(function(Prism) {
var interpolationExpr = {
pattern: /[\s\S]+/,
inside: null
};

Prism.languages.v = Prism.languages.extend('clike', {
'string': [
{
pattern: /`(?:\\\`|\\?[^\`]{1,2})`/, // using {1,2} instead of `u` flag for compatibility
alias: 'rune'
},
{
pattern: /r?(["'])(?:\\(?:\r\n|[\s\S])|(?!\1)[^\\\r\n])*\1/,
alias: 'quoted-string',
greedy: true,
inside: {
'interpolation': {
pattern: /((?:^|[^\\])(?:\\{2})*)\$(?:\{[^{}]*\}|\w+(?:\.\w+(?:\([^\(\)]*\))?|\[[^\[\]]+\])*)/,
lookbehind: true,
inside: {
'interpolation-variable': {
pattern: /^\$\w[\s\S]*$/,
alias: 'variable'
},
'interpolation-punctuation': {
pattern: /^\${|}$/,
alias: 'punctuation'
},
'interpolation-expression': interpolationExpr
}
}
}
}
],
'class-name': {
pattern: /(\b(?:enum|interface|struct|type)\s+)(?:C\.)?[\w]+/,
lookbehind: true
},
'keyword': /(?:\b(?:as|asm|assert|atomic|break|chan|const|continue|defer|else|embed|enum|fn|for|__global|go(?:to)?|if|import|in|interface|is|lock|match|module|mut|none|or|pub|return|rlock|select|shared|sizeof|static|struct|type(?:of)?|union|unsafe)|\$(?:if|else|for)|#(?:include|flag))\b/,
'number': /\b(?:0x[a-f\d]+(?:_[a-f\d]+)*|0b[01]+(?:_[01]+)*|0o[0-7]+(?:_[0-7]+)*|\d+(?:_\d+)*(?:\.\d+(?:_\d+)*)?)\b/i,
'operator': /~|\?|[*\/%^!=]=?|\+[=+]?|-[=-]?|\|[=|]?|&(?:=|&|\^=?)?|>(?:>=?|=)?|<(?:<=?|=|-)?|:=|\.\.\.?/,
'builtin': /\b(?:any(?:_int|_float)?|bool|byte(?:ptr)?|charptr|f(?:32|64)|i(?:8|16|nt|64|128)|rune|size_t|string|u(?:16|32|64|128)|voidptr)\b/
});

interpolationExpr.inside = Prism.languages.v;

Prism.languages.insertBefore('v', 'operator', {
'attribute': {
pattern: /^\s*\[(?:deprecated|unsafe_fn|typedef|live|inline|flag|ref_only|windows_stdcall|direct_array_access)\]/m,
alias: 'annotation',
inside: {
'punctuation': /[\[\]]/,
'keyword': /\w+/
}
},
'generic': {
pattern: /\<\w+\>(?=\s*[\)\{])/,
inside: {
'punctuation': /[<>]/,
'class-name': /\w+/
}
}
});

Prism.languages.insertBefore('v', 'function', {
'generic-function': {
// e.g. foo<T>( ...
pattern: /\w+\s*<\w+>(?=\()/,
inside: {
'function': /^\w+/,
'generic': {
pattern: /<\w+>/,
inside: Prism.languages.v.generic.inside
}
}
}
});
})(Prism);
1 change: 1 addition & 0 deletions components/prism-v.min.js

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

91 changes: 91 additions & 0 deletions examples/prism-v.html
@@ -0,0 +1,91 @@
<h2>Comments</h2>
<pre><code>// This is a comment
/* This is a comment
on multiple lines */</code></pre>

<h2>Numbers</h2>
<pre><code>123
0x7B
0b01111011
0o173
170141183460469231731687303715884105727
1_000_000
0b0_11
3_122.55
0xF_F
0o17_3
72.40
072.40
2.71828
</code></pre>

<h2>Runes and strings</h2>
<pre><code>'\t'
'\000'
'\x07'
'\u12e4'
'\U00101234'
`abc`
`multi-line
string`
"Hello, world!"
"multi-line
string"</code></pre>

<h2>String interpolation</h2>
<pre><code>'Hello, $name!'
"age = $user.age"
'can register = ${user.age > 13}'
'x = ${x:4.2f}'
'[${x:10}]'
'[${int(x):-10}]'
</code></pre>

<h2>Struct</h2>
<pre><code>struct Foo {
a int // private immutable (default)
mut:
b int // private mutable
c int // (you can list multiple fields with the same access modifier)
pub:
d int // public immutable (readonly)
pub mut:
e int // public, but mutable only in parent module
__global:
f int // public and mutable both inside and outside parent module
} // (not recommended to use, that's why the 'global' keyword
// starts with __)
</code></pre>

<h2>Functions</h2>
<pre><code>func(a, b int, z float64) bool { return a*b &lt; int(z) }</code></pre>

<h2>Full example</h2>
<pre><code>
module mymodule

import external_module

fn sqr(n int) int {
return n * n
}

fn run(value int, op fn (int) int) int {
return op(value)
}

fn main() {
println(run(5, sqr)) // "25"
// Anonymous functions can be declared inside other functions:
double_fn := fn (n int) int {
return n + n
}
println(run(5, double_fn)) // "10"
// Functions can be passed around without assigning them to variables:
res := run(5, fn (n int) int {
return n + n
})

external_module.say_hi()
}
</code></pre>
1 change: 1 addition & 0 deletions plugins/autoloader/prism-autoloader.js
Expand Up @@ -141,6 +141,7 @@
"textile": "markup",
"twig": "markup",
"typescript": "javascript",
"v": "clike",
"vala": "clike",
"vbnet": "basic",
"velocity": "markup",
Expand Down
2 changes: 1 addition & 1 deletion plugins/autoloader/prism-autoloader.min.js

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

13 changes: 13 additions & 0 deletions tests/languages/v/boolean_feature.test
@@ -0,0 +1,13 @@
true
false

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

[
["boolean", "true"],
["boolean", "false"]
]

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

Check for boolean
41 changes: 41 additions & 0 deletions tests/languages/v/class-name_feature.test
@@ -0,0 +1,41 @@
struct Abc { }
type Alphabet = Abc | Xyz
enum Token { }
interface Speaker { }
struct Repo<T> { }

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

[
["keyword", "struct"],
["class-name", "Abc"],
["punctuation", "{"],
["punctuation", "}"],

["keyword", "type"],
["class-name", "Alphabet"],
["operator", "="],
" Abc ",
["operator", "|"],
" Xyz\r\n",

["keyword", "enum"],
["class-name", "Token"],
["punctuation", "{"],
["punctuation", "}"],

["keyword", "interface"],
["class-name", "Speaker"],
["punctuation", "{"],
["punctuation", "}"],

["keyword", "struct"],
["class-name", "Repo"],
["generic", [
["punctuation", "<"],
["class-name", "T"],
["punctuation", ">"]
]],
["punctuation", "{"],
["punctuation", "}"]
]