package main import "core:strings" // Symbol extraction is per-language: the diff only shows text, so each // language needs its own idea of what a declaration looks like. Lang :: enum { Odin, Rust, C_Family, // .c .h .cc .cpp .cxx .hpp .hh .hxx .ipp .tpp .inl Python, // .py Ruby, // .rb Other, } // C_EXTENSIONS are the suffixes handed to ctags. `.h' is shared by C and C++, // which is why the family is named C_Family and not Cpp: universal-ctags picks // the dialect from the extension, and for a header that could be either it reads // the syntax. The list lives here so that `lang_of', the commit's file list and // symbol_name all agree on what a C-family file is. C_EXTENSIONS :: [?]string{ ".c", ".h", ".cc", ".cpp", ".cxx", ".hpp", ".hh", ".hxx", ".ipp", ".tpp", ".inl", } lang_of :: proc(file: string) -> Lang { switch { case strings.has_suffix(file, ".odin"): return .Odin case strings.has_suffix(file, ".rs"): return .Rust case strings.has_suffix(file, ".py"): return .Python case strings.has_suffix(file, ".rb"): return .Ruby } for ext in C_EXTENSIONS { if strings.has_suffix(file, ext) { return .C_Family } } return .Other } // parse_decl reports the declaration NAME and KIND introduced or removed by a // single line of a diff, if that line declares something. parse_decl :: proc(file, line: string) -> (name: string, kind: string, ok: bool) { switch lang_of(file) { case .Odin: return parse_odin_decl(line) case .Rust: return parse_rust_decl(line) case .Python: return parse_python_decl(line) case .Ruby: return parse_ruby_decl(line) case .C_Family: // Deliberately no lexical scan. In C and C++ a diff line cannot say // whether it declares anything: inside a body `foo(bar);' is a call and // inside a class body the same bytes declare a constructor, and both are // indented the same way. Declarations come from ctags over the whole // file instead -- see blob_tags in git.odin -- and what a hunk line can // still give us is the enclosing function, which is the hunk-header path. case .Other: } return "", "", false } // c_family_kind maps a universal-ctags kind name to the store's vocabulary, // rather than inventing a second one. The kinds left out are the ones that // churn without changing what a hunk was about: members and enumerators and // variables inside a body, namespaces (structure, not behaviour), and the // bookkeeping tags (header, parameter) that say nothing about a declaration. c_family_kind :: proc(tag_kind: string) -> (kind: string, ok: bool) { switch tag_kind { case "function", "prototype", "method": return "fn", true case "class", "struct", "union", "enum", "typedef": return "type", true case "macro": // A macro is a named constant as far as a reader is concerned. return "const", true case "variable", "externvar": return "var", true } return "", false } // -- Odin ------------------------------------------------------------------- // // NAME :: proc ... -> proc // NAME :: struct/enum/... -> type // NAME :: value -> const // NAME: Type -> var (top level only: struct fields are indented) // NAME := value -> var (top level only) parse_odin_decl :: proc(line: string) -> (name: string, kind: string, ok: bool) { indented := len(line) > 0 && (line[0] == ' ' || line[0] == '\t') s := strings.trim_left_space(line) if len(s) == 0 || s[0] == '@' || s[0] == '/' || s[0] == '}' || s[0] == '#' { return "", "", false } if sep := strings.index(s, "::"); sep >= 0 { lhs := strings.trim_space(s[:sep]) if !is_ident(lhs) { return "", "", false } rhs := strings.trim_space(s[sep + 2:]) switch { case strings.has_prefix(rhs, "proc"): return lhs, "proc", true case strings.has_prefix(rhs, "struct"), strings.has_prefix(rhs, "enum"), strings.has_prefix(rhs, "union"), strings.has_prefix(rhs, "bit_set"), strings.has_prefix(rhs, "distinct"), strings.has_prefix(rhs, "#type"), strings.has_prefix(rhs, "map["), strings.has_prefix(rhs, "matrix["), strings.has_prefix(rhs, "proc{"): return lhs, "type", true case: return lhs, "const", true } } // Globals: only when not indented, otherwise this is a struct field. if indented { return "", "", false } if sep := strings.index(s, ":="); sep > 0 { lhs := strings.trim_space(s[:sep]) if is_ident(lhs) { return lhs, "var", true } } if sep := strings.index_byte(s, ':'); sep > 0 { lhs := strings.trim_space(s[:sep]) if is_ident(lhs) { return lhs, "var", true } } return "", "", false } // -- Rust ------------------------------------------------------------------- // // fn NAME / pub fn NAME -> fn (methods included; they are indented) // struct/enum/trait/type/mod -> type // const NAME: / static NAME: -> const parse_rust_decl :: proc(line: string) -> (name: string, kind: string, ok: bool) { s := strings.trim_left_space(line) if len(s) == 0 || s[0] == '/' || s[0] == '#' || s[0] == '}' { return "", "", false } // Drop modifiers so that `pub(crate) async unsafe fn` still matches. for { switch { case strings.has_prefix(s, "pub(crate) "): s = strings.trim_left_space(s[len("pub(crate) "):]) case strings.has_prefix(s, "pub(super) "): s = strings.trim_left_space(s[len("pub(super) "):]) case strings.has_prefix(s, "pub "): s = strings.trim_left_space(s[len("pub "):]) case strings.has_prefix(s, "async "): s = strings.trim_left_space(s[len("async "):]) case strings.has_prefix(s, "unsafe "): s = strings.trim_left_space(s[len("unsafe "):]) case strings.has_prefix(s, "extern "): // `extern "C" fn foo' s = strings.trim_left_space(s[len("extern "):]) if len(s) > 0 && s[0] == '"' { if end := strings.index_byte(s[1:], '"'); end >= 0 { s = strings.trim_left_space(s[end + 2:]) } } case strings.has_prefix(s, "const fn "): // const fn is a function, not a constant if n, nok := ident_after(s, "const fn "); nok { return n, "fn", true } return "", "", false case strings.has_prefix(s, "default "), strings.has_prefix(s, "unsafe impl "): s = strings.trim_left_space(s[strings.index_byte(s, ' ') + 1:]) case: } break } entries := [?]struct { prefix: string, kind: string, }{ {"fn ", "fn"}, {"struct ", "type"}, {"enum ", "type"}, {"trait ", "type"}, {"type ", "type"}, {"mod ", "type"}, {"union ", "type"}, {"const ", "const"}, {"static ", "const"}, } for e in entries { if strings.has_prefix(s, e.prefix) { if n, nok := ident_after(s, e.prefix); nok { return n, e.kind, true } return "", "", false } } return "", "", false } // -- Python ----------------------------------------------------------------- // // def NAME(...): -> fn (and `async def') // class NAME: -> type // NAME = value -> const (module level only; indented is inside a def) // NAME: Type -> var (module level only) // // A line scanner is the right tool here, unlike C and C++: a Python // declaration is the first thing on its line, so there is no statement that // can look like one -- `foo(bar)` is a call and `def foo(bar):` is not, and the // keyword settles it. What it cannot see is a definition built at run time // (`f = lambda x: x`, a `def` inside an `exec`), which is why the gist of a // symbol is a name and the file it lives in, not a promise about the AST. parse_python_decl :: proc(line: string) -> (name: string, kind: string, ok: bool) { indented := len(line) > 0 && (line[0] == ' ' || line[0] == '\t') s := strings.trim_left_space(line) if len(s) == 0 || s[0] == '#' || s[0] == '@' || s[0] == '"' || s[0] == '\'' { return "", "", false } if strings.has_prefix(s, "async def ") { if n, nok := ident_after(s, "async def "); nok { return n, "fn", true } return "", "", false } if strings.has_prefix(s, "def ") { if n, nok := ident_after(s, "def "); nok { return n, "fn", true } return "", "", false } if strings.has_prefix(s, "class ") { if n, nok := ident_after(s, "class "); nok { return n, "type", true } return "", "", false } if indented { // Inside a def or a class body: an assignment there is a local, not a // declaration a reader asks "why" about. return "", "", false } if n, aok := assignment_name(s); aok { return n, "const", true } if sep := strings.index_byte(s, ':'); sep > 0 { if lhs := strings.trim_space(s[:sep]); is_ident(lhs) && !strings.contains(s, "=") { return lhs, "var", true } } return "", "", false } // -- Ruby ------------------------------------------------------------------- // // def NAME / def self.NAME / def NAME? -> fn // class NAME / module NAME -> type // NAME = value -> const (module level, capitalised) // // Ruby's declarations are line-anchored in the same way, with one trap the // callers of this store care about: `def` and `class` also appear inside // strings and heredocs, and no line scanner can tell. Anchoring on the first // token keeps that rare. parse_ruby_decl :: proc(line: string) -> (name: string, kind: string, ok: bool) { indented := len(line) > 0 && (line[0] == ' ' || line[0] == '\t') s := strings.trim_left_space(line) if len(s) == 0 || s[0] == '#' || s[0] == '=' { return "", "", false } if strings.has_prefix(s, "def ") { rest := s[len("def "):] if strings.has_prefix(rest, "self.") { rest = rest[len("self."):] } if n, nok := ident_after(rest, ""); nok { // `ready?', `save!', `name=' are one identifier to Ruby. return strings.trim_right(n, "?!=:"), "fn", true } return "", "", false } if strings.has_prefix(s, "class ") || strings.has_prefix(s, "module ") { prefix := strings.has_prefix(s, "class ") ? "class " : "module " if n, nok := ident_after(s, prefix); nok { return n, "type", true } return "", "", false } if indented { return "", "", false } // A Ruby constant is capitalised; `x = 1` at column 0 is a local in a // script and not worth a symbol. if n, aok := assignment_name(s); aok && n[0] >= 'A' && n[0] <= 'Z' { return n, "const", true } return "", "", false } // assignment_name reports NAME from `NAME = value' (not `NAME == value'). assignment_name :: proc(s: string) -> (name: string, ok: bool) { for i in 0 ..< len(s) { if s[i] != '=' { continue } if i + 1 < len(s) && s[i + 1] == '=' { return "", false } if i > 0 && (s[i - 1] == '!' || s[i - 1] == '<' || s[i - 1] == '>' || s[i - 1] == '=') { return "", false } lhs := strings.trim_space(s[:i]) if is_ident(lhs) { return lhs, true } return "", false } return "", false } // ident_after reads the identifier that follows PREFIX in S. ident_after :: proc(s, prefix: string) -> (name: string, ok: bool) { rest := strings.trim_left_space(s[len(prefix):]) end := 0 for end < len(rest) { c := rest[end] if (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_' { end += 1 } else { break } } if end == 0 { return "", false } return rest[:end], true } is_ident :: proc(s: string) -> bool { if len(s) == 0 { return false } for c, i in s { switch { case c == '_': case c >= 'a' && c <= 'z': case c >= 'A' && c <= 'Z': case c >= '0' && c <= '9' && i > 0: case: return false } } return true }