Skip to main content

semantic/parser/
parser_types.rs

1// SPDX-License-Identifier: Apache-2.0
2//! Parsed AST data types.
3
4/// A function definition.
5#[derive(Clone, Debug, PartialEq)]
6pub struct FunctionDef {
7    pub name: String,
8    /// Enclosing impl / type / module / receiver, when the parser can
9    /// see one. Empty for a file-scope function.
10    pub container: String,
11    pub signature: String,
12    pub start_line: usize,
13    pub end_line: usize,
14    pub content: String,
15}
16
17impl FunctionDef {
18    /// `container::name`, or the bare name when the parser saw no container.
19    pub fn qualified_name(&self) -> String {
20        if self.container.is_empty() {
21            self.name.clone()
22        } else {
23            format!("{}::{}", self.container, self.name)
24        }
25    }
26
27    /// Identity that distinguishes `Foo::run` from `Bar::run` and
28    /// overloads that share a bare name. Used as the capture-time
29    /// `changed_symbols` key.
30    pub fn symbol_identity(&self) -> String {
31        let qualified = self.qualified_name();
32        if self.signature.is_empty() {
33            qualified
34        } else {
35            format!("{qualified}|{}", self.signature)
36        }
37    }
38}
39
40/// A call expression extracted from the parsed tree, not source text.
41#[derive(Clone, Debug, PartialEq, Eq)]
42pub struct CallSite {
43    pub name: String,
44    /// Path or receiver segments ahead of the callee (`Bar` in `Bar::run`,
45    /// `foo` in `foo.run()`). Empty for a bare `run()`.
46    pub qualifier: Vec<String>,
47}
48
49/// An import statement.
50#[derive(Clone, Debug, PartialEq)]
51pub struct Import {
52    pub raw: String,
53    pub kind: ImportKind,
54}
55
56/// Type of import.
57#[derive(Clone, Copy, Debug, PartialEq, Eq)]
58pub enum ImportKind {
59    Use,
60    ExternCrate,
61    Require,
62    Import,
63}