lgx_runtime/core/
parser.rs1use crate::models::{ASTNode, ParsedCode};
2use anyhow::Result;
3use tracing::debug;
4
5pub struct Parser {
6 }
8
9impl Parser {
10 pub fn new() -> Self {
11 Self {}
12 }
13
14 pub fn parse(&self, code: &str) -> Result<ParsedCode> {
15 debug!("Parsing LGX code: {} characters", code.len());
16
17 let mut nodes = Vec::new();
18 let lines: Vec<&str> = code.lines().collect();
19
20 for (line_num, line) in lines.iter().enumerate() {
21 let trimmed = line.trim();
22 if !trimmed.is_empty() && !trimmed.starts_with('#') {
23 if let Some(node) = self.parse_line(trimmed, line_num + 1)? {
24 nodes.push(node);
25 }
26 }
27 }
28
29 Ok(ParsedCode {
30 nodes,
31 source: code.to_string(),
32 line_count: lines.len(),
33 })
34 }
35
36 fn parse_line(&self, line: &str, line_num: usize) -> Result<Option<ASTNode>> {
37 if line.starts_with("CREATE") {
38 return Ok(Some(ASTNode::Create {
39 entity: self.extract_entity(line),
40 fields: self.extract_fields(line),
41 line: line_num,
42 }));
43 }
44
45 if line.starts_with("READ") {
46 return Ok(Some(ASTNode::Read {
47 entity: self.extract_entity(line),
48 conditions: self.extract_conditions(line),
49 line: line_num,
50 }));
51 }
52
53 if line.starts_with("UPDATE") {
54 return Ok(Some(ASTNode::Update {
55 entity: self.extract_entity(line),
56 fields: self.extract_fields(line),
57 conditions: self.extract_conditions(line),
58 line: line_num,
59 }));
60 }
61
62 if line.starts_with("DELETE") {
63 return Ok(Some(ASTNode::Delete {
64 entity: self.extract_entity(line),
65 conditions: self.extract_conditions(line),
66 line: line_num,
67 }));
68 }
69
70 if line.starts_with("FUNCTION") {
71 return Ok(Some(ASTNode::Function {
72 name: self.extract_function_name(line),
73 parameters: self.extract_parameters(line),
74 body: self.extract_function_body(line),
75 line: line_num,
76 }));
77 }
78
79 Ok(None)
80 }
81
82 fn extract_entity(&self, line: &str) -> String {
83 let parts: Vec<&str> = line.split_whitespace().collect();
84 if parts.len() > 1 {
85 parts[1].to_string()
86 } else {
87 String::new()
88 }
89 }
90
91 fn extract_fields(&self, _line: &str) -> Vec<String> {
92 Vec::new()
93 }
94
95 fn extract_conditions(&self, _line: &str) -> Vec<String> {
96 Vec::new()
97 }
98
99 fn extract_function_name(&self, line: &str) -> String {
100 let parts: Vec<&str> = line.split_whitespace().collect();
101 if parts.len() > 1 {
102 parts[1].to_string()
103 } else {
104 String::new()
105 }
106 }
107
108 fn extract_parameters(&self, _line: &str) -> Vec<String> {
109 Vec::new()
110 }
111
112 fn extract_function_body(&self, _line: &str) -> String {
113 String::new()
114 }
115}