agentshield/parser/
mod.rs1pub mod json_schema;
2pub mod python;
3pub mod shell;
4pub mod typescript;
5
6use std::collections::HashSet;
7use std::path::Path;
8
9use crate::error::Result;
10use crate::ir::execution_surface::*;
11use crate::ir::{ArgumentSource, Language, SourceLocation};
12
13#[derive(Debug, Clone, Default)]
15pub struct ParsedFile {
16 pub commands: Vec<CommandInvocation>,
17 pub file_operations: Vec<FileOperation>,
18 pub network_operations: Vec<NetworkOperation>,
19 pub env_accesses: Vec<EnvAccess>,
20 pub dynamic_exec: Vec<DynamicExec>,
21 pub function_params: Vec<FunctionParam>,
23 pub function_defs: Vec<FunctionDef>,
25 pub call_sites: Vec<CallSite>,
27 pub sanitized_vars: HashSet<String>,
29}
30
31#[derive(Debug, Clone)]
33pub struct FunctionParam {
34 pub function_name: String,
35 pub param_name: String,
36 pub location: SourceLocation,
37}
38
39#[derive(Debug, Clone)]
41pub struct FunctionDef {
42 pub name: String,
43 pub params: Vec<String>,
44 pub is_exported: bool,
45 pub location: SourceLocation,
46}
47
48#[derive(Debug, Clone)]
50pub struct CallSite {
51 pub callee: String,
53 pub arguments: Vec<ArgumentSource>,
55 pub caller: Option<String>,
57 pub location: SourceLocation,
58}
59
60pub trait LanguageParser: Send + Sync {
63 fn language(&self) -> Language;
64 fn parse_file(&self, path: &Path, content: &str) -> Result<ParsedFile>;
65}
66
67pub fn parser_for_language(lang: Language) -> Option<Box<dyn LanguageParser>> {
69 match lang {
70 Language::Python => Some(Box::new(python::PythonParser)),
71 Language::TypeScript | Language::JavaScript => Some(Box::new(typescript::TypeScriptParser)),
72 Language::Shell => Some(Box::new(shell::ShellParser)),
73 _ => None,
74 }
75}