Initial
This commit is contained in:
1
bfox-parser/.gitignore
vendored
Normal file
1
bfox-parser/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/target
|
||||
10
bfox-parser/Cargo.toml
Normal file
10
bfox-parser/Cargo.toml
Normal 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
56
bfox-parser/src/lib.rs
Normal 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
|
||||
}
|
||||
|
||||
}
|
||||
27
bfox-parser/src/types/mod.rs
Normal file
27
bfox-parser/src/types/mod.rs
Normal 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(())
|
||||
}
|
||||
}
|
||||
20
bfox-parser/src/types/token.rs
Normal file
20
bfox-parser/src/types/token.rs
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user