-
-
Notifications
You must be signed in to change notification settings - Fork 81
/
syntax.v
53 lines (48 loc) · 1.44 KB
/
syntax.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by a GPL license
// that can be found in the LICENSE file.
module main
import os
import json
const builtin_v_syntax_file_content = $embed_file('syntax/v.syntax').to_string()
struct Syntax {
name string
extensions []string
fmt_cmd string
keywords []string
literals []string
}
fn (mut ved Ved) load_syntaxes() {
println('loading syntax files...')
vsyntax := json.decode(Syntax, builtin_v_syntax_file_content) or {
panic('the builtin syntax file "${builtin_v_syntax_file_content}" can not be decoded ${err}')
}
ved.syntaxes << vsyntax
files := os.walk_ext(syntax_dir, '.syntax')
for file in files {
fcontent := os.read_file(file) or {
eprintln(' error: cannot load syntax file ${file}: ${err.msg()}')
'{}'
}
syntax := json.decode(Syntax, fcontent) or {
eprintln(' error: cannot load syntax file ${file}: ${err.msg()}')
Syntax{}
}
if file.ends_with('v.syntax') {
// allow overriding the builtin embedded syntax at runtime:
ved.syntaxes[0] = syntax
continue
}
ved.syntaxes << syntax
}
println('${files.len} syntax files loaded + the compile time builtin syntax for .v')
}
fn (mut ved Ved) set_current_syntax_idx(ext string) {
for i, syntax in ved.syntaxes {
if ext in syntax.extensions {
println('selected syntax ${syntax.name} for extension ${ext}')
ved.current_syntax_idx = i
break
}
}
}