This commit is contained in:
2024-05-26 23:38:06 +03:00
commit 9aa15dfbab
16 changed files with 536 additions and 0 deletions

1
bfox-parser/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/target

10
bfox-parser/Cargo.toml Normal file
View File

@@ -0,0 +1,10 @@
[package]
name = "bfox-parser"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
anyhow = "1.0.86"
log = "0.4.21"

56
bfox-parser/src/lib.rs Normal file
View File

@@ -0,0 +1,56 @@
use types::{token::Token, Loc};
mod types;
pub struct Parser {
tokens: Vec<Token>,
chars: Vec<char>,
loc: Loc,
}
impl Parser {
pub fn new() -> Self {
Self {
tokens: Vec::new(),
chars: Vec::new(),
loc: Loc::default(),
}
}
pub fn parse(&mut self, code: String, file_name: String) -> anyhow::Result<()> {
self.loc.file = file_name;
self.loc.col = 0;
self.loc.line = 0;
self.chars = code.chars().collect();
// Parse here
Ok(())
}
/// Pops one token
fn next(&mut self) -> Option<char> {
if let Some(c) = self.chars.pop() {
match c {
'\n' => {
self.loc.line += 1;
self.loc.col = 0;
}
_ => {
self.loc.col += 1;
}
}
return Some(c);
}
None
}
pub fn tokens(&self) -> &[Token] {
&self.tokens
}
}

View File

@@ -0,0 +1,27 @@
pub mod token;
use std::fmt::Display;
#[derive(Debug, Default, Clone)]
pub struct Loc {
pub file: String,
pub line: usize,
pub col: usize
}
impl Loc {
pub fn new<S: ToString>(file: S, line: usize, col: usize) -> Self {
Self{
file: file.to_string(),
line,
col,
}
}
}
impl Display for Loc {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}:{}:{}", self.file, self.line, self.col)?;
Ok(())
}
}

View File

@@ -0,0 +1,20 @@
use super::Loc;
#[derive(Debug, Clone)]
pub struct Token {
pub typ: TokenType,
pub loc: Loc,
}
#[derive(Debug, Clone, Copy)]
pub enum TokenType {
Add,
Sub,
Left,
Right,
LoopR,
LoopL,
Print,
Input
}