Skip to main content

sql_fun_sqlast/sem/
comment.rs

1use itertools::Itertools;
2
3use crate::{
4    sem::AstAndContextPair,
5    syn::{ListOpt, ObjectType, Opt},
6};
7
8use super::{AnalysisError, ParseContext, SemAst};
9
10/// analyzed [`crate::syn::CommentStmt`]
11#[derive(Debug, Clone)]
12pub struct Comment {
13    target: String,
14    object_type: ObjectType,
15    comment: String,
16}
17
18impl Comment {
19    /// comment target
20    #[must_use]
21    pub fn target(&self) -> &str {
22        &self.target
23    }
24
25    /// get comment target object type
26    #[must_use]
27    pub fn object_type(&self) -> &ObjectType {
28        &self.object_type
29    }
30
31    /// get a comment text
32    #[must_use]
33    pub fn comment(&self) -> &str {
34        &self.comment
35    }
36}
37
38/// analyze [`crate::syn::CommentStmt`]
39pub fn analyze_comment<TParseContext>(
40    mut context: TParseContext,
41    syn: crate::syn::CommentStmt,
42) -> Result<AstAndContextPair<TParseContext>, AnalysisError>
43where
44    TParseContext: ParseContext,
45{
46    let items = if let Some(list) = syn.get_object().as_list().as_inner() {
47        let Some(items) = list.get_items().map(|v| v.as_string()) else {
48            AnalysisError::raise_unexpected_none("comment_stmt.object.items")?
49        };
50        items.iter().map(crate::syn::String::get_sval).join(".")
51    } else {
52        let Some(sval) = syn.get_object().as_string().get_sval() else {
53            AnalysisError::raise_unexpected_none("comment_stmt.object")?
54        };
55        sval
56    };
57
58    let Some(object_type) = syn.get_objtype().as_inner() else {
59        AnalysisError::raise_unexpected_none("comment_stmt.objtype")?
60    };
61
62    let comment = syn.get_comment();
63    let comment = Comment {
64        target: items,
65        object_type,
66        comment,
67    };
68    context = context.apply_comment(&comment)?;
69    Ok(AstAndContextPair::new(SemAst::Comment(comment), context))
70}