From 898601d81d6ae64c4024380eb38889d5fed41cd6 Mon Sep 17 00:00:00 2001 From: lexara-prime-ai Date: Thu, 11 Jul 2024 20:38:49 +0000 Subject: [PATCH] Created module, added module. --- rust/definition.ebnf | 7 ++ rust/src/iaas/lexer.rs | 121 +++++++++++++++++++++++++++++++ rust/src/iaas/mod.rs | 3 +- rust/src/iaas/samples/scratch.tf | 9 +++ 4 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 rust/definition.ebnf create mode 100644 rust/src/iaas/lexer.rs create mode 100644 rust/src/iaas/samples/scratch.tf diff --git a/rust/definition.ebnf b/rust/definition.ebnf new file mode 100644 index 0000000..825dae8 --- /dev/null +++ b/rust/definition.ebnf @@ -0,0 +1,7 @@ +configuration = { block } ; +block = block_type identifier "{" { key_value_pair } "}" ; +block_type = "provider" | "resource" ; +identifier = letter { letter | digit | "_" } ; +key_value_pair= identifier "=" ( string | number ) ; +string = '"' { character } '"' ; +number = digit { digit } ; \ No newline at end of file diff --git a/rust/src/iaas/lexer.rs b/rust/src/iaas/lexer.rs new file mode 100644 index 0000000..1837d9c --- /dev/null +++ b/rust/src/iaas/lexer.rs @@ -0,0 +1,121 @@ +#![allow(unused)] + +pub enum Token { + Provider, + Resource, + Identifier(String), + StringLiteral(String), + NumberLiteral(i64), + Equals, + LeftBrace, + RightBrace, + EOF, +} + +#[derive(Debug)] +pub struct Lexer<'l> { + pub input: &'l str, + pub position: usize, + pub current_char: Option, +} + +impl<'l> Lexer<'l> { + pub fn new(input: &'l str) -> Self { + let mut lexer = Self { + input, + position: 0, + current_char: None, + }; + + lexer + } + + pub fn advance(&mut self) { + if self.position < self.input.len() { + self.current_char = Some(self.input[self.position..].chars().next().unwrap()); + self.position += 1; + } else { + self.current_char = None; + } + } + + pub fn skip_whitespace(&mut self) { + while let Some(c) = self.current_char { + if c.is_whitespace() { + self.advance(); + } else { + break; + } + } + } + + pub fn peek_keyword(&self, keyword: &str) -> bool { + self.input[self.position - 1..].starts_with(keyword) + } + + pub fn consume_keyword(&mut self, keyword: &str) { + for _ in 0..keyword.len() { + self.advance(); + } + } + + pub fn read_identifier(&mut self) -> String { + let mut identifier = String::new(); + while let Some(c) = self.current_char { + if c.is_alphanumeric() || c == '_' { + identifier.push(c); + self.advance(); + } else { + break; + } + } + identifier + } + + pub fn read_string(&mut self) -> String { + self.advance(); + let mut string = String::new(); + while let Some(c) = self.current_char { + if c == '"' { + break; + } else { + string.push(c); + self.advance(); + } + } + self.advance(); + string + } + + pub fn read_number(&mut self) -> i64 { + let mut number = String::new(); + while let Some(c) = self.current_char { + if c.is_digit(10) { + number.push(c); + self.advance(); + } else { + break; + } + } + number.parse().unwrap() + } + + pub fn next_token(&mut self) -> Token { + self.skip_whitespace(); + + let token = match self.current_char { + Some('p') if self.peek_keyword("provider") => { + self.consume_keyword("provider"); + Token::Provider + } + + Some('r') if self.peek_keyword("resource") => { + self.consume_keyword("resource"); + Token::Resource + } + + Some(c) if c.is_alphabetic() => Token::Identifier(self.read_identifier()), + Some('"') => Token::StringLiteral(self.read_s) + } + } +} diff --git a/rust/src/iaas/mod.rs b/rust/src/iaas/mod.rs index a91a0e3..e86ed04 100644 --- a/rust/src/iaas/mod.rs +++ b/rust/src/iaas/mod.rs @@ -1 +1,2 @@ -pub mod tf_parser; \ No newline at end of file +pub mod tf_parser; +pub mod lexer; \ No newline at end of file diff --git a/rust/src/iaas/samples/scratch.tf b/rust/src/iaas/samples/scratch.tf new file mode 100644 index 0000000..485b329 --- /dev/null +++ b/rust/src/iaas/samples/scratch.tf @@ -0,0 +1,9 @@ +provider "render" { + api_key = "rnd_xxxxXXXXxxxxXXXXxxxXX" +} + +resource "render_service" "example" { + name = "example-service" + environment = "production" + replicas = 3 +}