| 1 | module main |
| 2 | |
| 3 | type SecondTokenizerValue = []rune | Keyword |
| 4 | |
| 5 | enum AdvancedTokenType { |
| 6 | identifier |
| 7 | keyword |
| 8 | newline |
| 9 | } |
| 10 | |
| 11 | struct SecondTokenizerToken { |
| 12 | type AdvancedTokenType |
| 13 | value ?SecondTokenizerValue |
| 14 | } |
| 15 | |
| 16 | enum Keyword { |
| 17 | module |
| 18 | import |
| 19 | } |
| 20 | |
| 21 | pub struct FileAST { |
| 22 | pub mut: |
| 23 | module []rune |
| 24 | } |
| 25 | |
| 26 | pub 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 | |
| 61 | fn test_main() { |
| 62 | build_ast([]SecondTokenizerToken{}) or { panic(err) } |
| 63 | } |
| 64 | |