diff options
| author | Daniel Gavin <danielgavin5@hotmail.com> | 2021-12-05 20:14:07 +0100 |
|---|---|---|
| committer | Daniel Gavin <danielgavin5@hotmail.com> | 2021-12-05 20:14:07 +0100 |
| commit | 8f73e4e4f64b46bee43fe659cad8ea4d505fb3f0 (patch) | |
| tree | 1a6822ebc2b522c7f4eec8442ac2deca6efb2c53 /src | |
| parent | 40f45aa7568b664d9bfc5b1d89e47e46104af2b5 (diff) | |
Add odinfmt builder
Diffstat (limited to 'src')
| -rw-r--r-- | src/odin/format/format.odin | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/src/odin/format/format.odin b/src/odin/format/format.odin new file mode 100644 index 0000000..2abfbec --- /dev/null +++ b/src/odin/format/format.odin @@ -0,0 +1,40 @@ +package odin_format + +import "shared:odin/printer" +import "core:odin/parser" +import "core:odin/ast" + +default_style := printer.default_style + +simplify :: proc(file: ^ast.File) { + +} + +format :: proc(filepath: string, source: string, config: printer.Config, parser_flags := parser.Flags{.Optional_Semicolons}, allocator := context.allocator) -> (string, bool) { + config := config + + pkg := ast.Package { + kind = .Normal, + } + + file := ast.File { + pkg = &pkg, + src = source, + fullpath = filepath, + } + + config.newline_limit = clamp(config.newline_limit, 0, 16) + config.spaces = clamp(config.spaces, 1, 16) + + p := parser.default_parser(parser_flags) + + ok := parser.parse_file(&p, &file) + + if !ok || file.syntax_error_count > 0 { + return {}, false + } + + prnt := printer.make_printer(config, allocator) + + return printer.print(&prnt, &file), true +} |