-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Changes from 5 commits
061650b
b23ef0c
5ea3b50
af1513f
9a11ee9
3d8947c
c646846
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,54 @@ | ||||||
Prism.languages.v = Prism.languages.extend('clike', { | ||||||
'string': [ | ||||||
{ | ||||||
pattern: /`(?:\\[\s\S]|[^\\`])*`/, | ||||||
alias: 'backtick-quoted-string', | ||||||
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*<[^<>]*>(?=\()/, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
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
|
||||||
} | ||||||
} | ||||||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 < 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> |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
true | ||
false | ||
|
||
---------------------------------------------------- | ||
|
||
[ | ||
["boolean", "true"], | ||
["boolean", "false"] | ||
] | ||
|
||
---------------------------------------------------- | ||
|
||
Check for boolean |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
|
||
taggon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
---------------------------------------------------- | ||
|
||
[ | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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", "}"] | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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", "}"] | ||
] |
There was a problem hiding this comment.
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?