v2 / vlib / v / tests / options / option_match_cases_test.v
63 lines · 54 sloc · 1.26 KB · 104bc212813dee6afe2ac57a1cef25c921d869f8
Raw
1module main
2
3type SecondTokenizerValue = []rune | Keyword
4
5enum AdvancedTokenType {
6 identifier
7 keyword
8 newline
9}
10
11struct SecondTokenizerToken {
12 type AdvancedTokenType
13 value ?SecondTokenizerValue
14}
15
16enum Keyword {
17 module
18 import
19}
20
21pub struct FileAST {
22pub mut:
23 module []rune
24}
25
26pub fn build_ast(tokens []SecondTokenizerToken) !FileAST {
27 mut file_ast := FileAST{}
28 mut i := -1
29
30 for i < tokens.len - 1 {
31 i++
32 token := tokens[i]
33 match true {
34 file_ast.module.len == 0 && token.type == .keyword
35 && token.value == ?SecondTokenizerValue(Keyword.module) {
36 file_ast.module = [`a`]
37 }
38 file_ast.module.len == 0 {
39 return error('Expected `module` keyword at the start of the file, but got `${token.type}`')
40 }
41 token.type == .keyword && token.value == ?SecondTokenizerValue(Keyword.module) {
42 return error('Multiple `module` declarations are not allowed, but got another one')
43 }
44 token.type == .keyword && token.value == ?SecondTokenizerValue(Keyword.import) {
45 dump('import')
46 }
47 token.type == .newline {
48 // ignore newlines
49 continue
50 }
51 else {
52 // TODO: turn this into an error after implementing all other AST nodes
53 continue
54 }
55 }
56 }
57
58 return file_ast
59}
60
61fn test_main() {
62 build_ast([]SecondTokenizerToken{}) or { panic(err) }
63}
64