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 5 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
54 changes: 54 additions & 0 deletions components/prism-v.js
@@ -0,0 +1,54 @@
Prism.languages.v = Prism.languages.extend('clike', {
'string': [
{
pattern: /`(?:\\[\s\S]|[^\\`])*`/,
alias: 'backtick-quoted-string',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not alias is with its V-specific name: rune?

greedy: true
},
{
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+(?:\.\w+(?:\([^\(\)]*\))?|\[[^\[\]]+\])*$/,
taggon marked this conversation as resolved.
Show resolved Hide resolved
alias: 'variable'
},
'interpolation-punctuation': {
pattern: /^\${|}$/,
alias: 'punctuation'
},
rest: Prism.languages.v
taggon marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
}
],
'class-name': {
pattern: /\b(enum|interface|struct|type)\s+[\w.\\]+/,
taggon marked this conversation as resolved.
Show resolved Hide resolved
lookbehind: true
},
'keyword': /\b(?:as|asm|assert|atomic|break|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)\b/,
taggon marked this conversation as resolved.
Show resolved Hide resolved
'number': /\b(?:0x[a-f\d]+(?:_[a-f\d]+)*|0b[01]+(?:_[01]+)*|0o[0-7]+(?:_[0-7]+)*|\d+(?:_\d+)*(?:\.\d+(?:_\d+)*)?)\b/i,
'operator': /~|[*\/%^!=]=?|\+[=+]?|-[=-]?|\|[=|]?|&(?:=|&|\^=?)?|>(?:>=?|=)?|<(?:<=?|=|-)?|:=|\.\.\./,
taggon marked this conversation as resolved.
Show resolved Hide resolved
'builtin': /\b(?:any(?:_int|_float)?|bool|byte(?:ptr)?|charptr|i(?:8|16|nt|64|128)|rune|size_t|string|u(?:16|32|64|128))\b/
taggon marked this conversation as resolved.
Show resolved Hide resolved
});

Prism.languages.insertBefore('v', 'function', {
'generic-function': {
// e.g. foo<T>( ...
pattern: /\w+\s*<[^<>]*>(?=\()/,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. There can probably be spaces between > and (.
  2. \b for better performance.
Suggested change
pattern: /\w+\s*<[^<>]*>(?=\()/,
pattern: /\b\w+\s*<[^<>]*>(?=\s*\()/,

greedy: true,
inside: {
'function': /^\w+/,
'generic': {
pattern: /<[\s\S]+/, // everything after the first <
alias: 'class-name'
}
taggon marked this conversation as resolved.
Show resolved Hide resolved
}
}
});
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/v/boolean_feature.test
@@ -0,0 +1,13 @@
true
false

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

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

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

Check for boolean
6 changes: 6 additions & 0 deletions tests/v/builtin_feature.test
@@ -0,0 +1,6 @@

taggon marked this conversation as resolved.
Show resolved Hide resolved

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

[
]
39 changes: 39 additions & 0 deletions tests/v/class-name_feature.test
@@ -0,0 +1,39 @@
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"],
["operator", "<"],
"T",
["operator", ">"],
["punctuation", "{"],
["punctuation", "}"]
]
110 changes: 110 additions & 0 deletions tests/v/function_feature.test
@@ -0,0 +1,110 @@
fn init() { }
fn add(x int, y int) int { }
fn sum(a ...int) int { }
fn (mut t MyTime) century() int { }
fn (d Dog) speak() string { }
fn (r Repo) find_user_by_id(id int) ?User { }
fn new_repo<T>(db DB) Repo<T> { }
fn (r Repo<T>) find_by_id(id int) ?T { }

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

[
["keyword", "fn"],
["function", "init"],
["punctuation", "("],
["punctuation", ")"],
["punctuation", "{"],
["punctuation", "}"],

["keyword", "fn"],
["function", "add"],
["punctuation", "("],
"x ",
["builtin", "int"],
["punctuation", ","],
" y ",
["builtin", "int"],
["punctuation", ")"],
["builtin", "int"],
["punctuation", "{"],
["punctuation", "}"],

["keyword", "fn"],
["function", "sum"],
["punctuation", "("],
"a ",
["operator", "..."],
["builtin", "int"],
["punctuation", ")"],
["builtin", "int"],
["punctuation", "{"],
["punctuation", "}"],

["keyword", "fn"],
["punctuation", "("],
["keyword", "mut"],
" t MyTime",
["punctuation", ")"],
["function", "century"],
["punctuation", "("],
["punctuation", ")"],
["builtin", "int"],
["punctuation", "{"],
["punctuation", "}"],

["keyword", "fn"],
["punctuation", "("],
"d Dog",
["punctuation", ")"],
["function", "speak"],
["punctuation", "("],
["punctuation", ")"],
["builtin", "string"],
["punctuation", "{"],
["punctuation", "}"],

["keyword", "fn"],
["punctuation", "("],
"r Repo",
["punctuation", ")"],
["function", "find_user_by_id"],
["punctuation", "("],
"id ",
["builtin", "int"],
["punctuation", ")"],
" ?User ",
["punctuation", "{"],
["punctuation", "}"],

["keyword", "fn"],
["generic-function", [
["function", "new_repo"],
["generic", "<T>"]
]],
["punctuation", "("],
"db DB",
["punctuation", ")"],
" Repo",
["operator", "<"],
"T",
["operator", ">"],
["punctuation", "{"],
["punctuation", "}"],

["keyword", "fn"],
["punctuation", "("],
"r Repo",
["operator", "<"],
"T",
["operator", ">"],
["punctuation", ")"],
["function", "find_by_id"],
["punctuation", "("],
"id ",
["builtin", "int"],
["punctuation", ")"],
" ?T ",
["punctuation", "{"],
["punctuation", "}"]
]