diag & logging

This commit is contained in:
core 2024-01-16 17:59:34 -05:00
parent d71bc58420
commit adba163511
Signed by: core
GPG Key ID: FDBF740DADDCEECF
14 changed files with 488 additions and 46 deletions

316
Cargo.lock generated
View File

@ -2,26 +2,118 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "bitflags"
version = "2.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed570934406eb16438a4e976b1b4500774099c13b8cb96eec99f620f05090ddf"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "colored"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cbf2150cce219b664a8a70df7a1f933836724b503f8a413af9365b4dcc4d90b8"
dependencies = [
"lazy_static",
"windows-sys 0.48.0",
]
[[package]]
name = "errno"
version = "0.3.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a258e46cdc063eb8519c00b9fc845fc47bcfca4130e2f08e88665ceda8474245"
dependencies = [
"libc",
"windows-sys 0.52.0",
]
[[package]]
name = "hermit-abi"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d77f7ec81a6d05a3abb01ab6eb7590f6083d08449fe5a1c8b1e620283546ccb7"
[[package]]
name = "is-terminal"
version = "0.4.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0bad00257d07be169d870ab665980b06cdb366d792ad690bf2e76876dc503455"
dependencies = [
"hermit-abi",
"rustix",
"windows-sys 0.52.0",
]
[[package]]
name = "kabel"
version = "0.1.0"
dependencies = [
"is-terminal",
"libkabel",
"tracing-subscriber",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.2.152"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "13e3bf6590cbc649f4d1a3eefc9d5d6eb746f5200ffb04e5e142700b8faa56e7"
[[package]]
name = "libkabel"
version = "0.1.0"
dependencies = [
"colored",
"tracing",
]
[[package]]
name = "linux-raw-sys"
version = "0.4.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4cd1a83af159aa67994778be9070f0ae1bd732942279cabb14f86f986a21456"
[[package]]
name = "log"
version = "0.4.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f"
[[package]]
name = "nu-ansi-term"
version = "0.46.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77a8165726e8236064dbb45459242600304b42a5ea24ee2948e18e023bf7ba84"
dependencies = [
"overload",
"winapi",
]
[[package]]
name = "once_cell"
version = "1.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"
[[package]]
name = "overload"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39"
[[package]]
name = "pin-project-lite"
version = "0.2.13"
@ -46,6 +138,34 @@ dependencies = [
"proc-macro2",
]
[[package]]
name = "rustix"
version = "0.38.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "322394588aaf33c24007e8bb3238ee3e4c5c09c084ab32bc73890b99ff326bca"
dependencies = [
"bitflags",
"errno",
"libc",
"linux-raw-sys",
"windows-sys 0.52.0",
]
[[package]]
name = "sharded-slab"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6"
dependencies = [
"lazy_static",
]
[[package]]
name = "smallvec"
version = "1.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2593d31f82ead8df961d8bd23a64c2ccf2eb5dd34b0a34bfb4dd54011c72009e"
[[package]]
name = "syn"
version = "2.0.48"
@ -57,6 +177,16 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "thread_local"
version = "1.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3fdd6f064ccff2d6567adcb3873ca630700f00b5ad3f060c25b5dcfd9a4ce152"
dependencies = [
"cfg-if",
"once_cell",
]
[[package]]
name = "tracing"
version = "0.1.40"
@ -86,6 +216,32 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54"
dependencies = [
"once_cell",
"valuable",
]
[[package]]
name = "tracing-log"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3"
dependencies = [
"log",
"once_cell",
"tracing-core",
]
[[package]]
name = "tracing-subscriber"
version = "0.3.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ad0f048c97dbd9faa9b7df56362b8ebcaa52adb06b498c050d2f4e32f90a7a8b"
dependencies = [
"nu-ansi-term",
"sharded-slab",
"smallvec",
"thread_local",
"tracing-core",
"tracing-log",
]
[[package]]
@ -93,3 +249,163 @@ name = "unicode-ident"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "valuable"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d"
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "windows-sys"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
dependencies = [
"windows-targets 0.48.5",
]
[[package]]
name = "windows-sys"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
dependencies = [
"windows-targets 0.52.0",
]
[[package]]
name = "windows-targets"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c"
dependencies = [
"windows_aarch64_gnullvm 0.48.5",
"windows_aarch64_msvc 0.48.5",
"windows_i686_gnu 0.48.5",
"windows_i686_msvc 0.48.5",
"windows_x86_64_gnu 0.48.5",
"windows_x86_64_gnullvm 0.48.5",
"windows_x86_64_msvc 0.48.5",
]
[[package]]
name = "windows-targets"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a18201040b24831fbb9e4eb208f8892e1f50a37feb53cc7ff887feb8f50e7cd"
dependencies = [
"windows_aarch64_gnullvm 0.52.0",
"windows_aarch64_msvc 0.52.0",
"windows_i686_gnu 0.52.0",
"windows_i686_msvc 0.52.0",
"windows_x86_64_gnu 0.52.0",
"windows_x86_64_gnullvm 0.52.0",
"windows_x86_64_msvc 0.52.0",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8"
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb7764e35d4db8a7921e09562a0304bf2f93e0a51bfccee0bd0bb0b666b015ea"
[[package]]
name = "windows_aarch64_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc"
[[package]]
name = "windows_aarch64_msvc"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbaa0368d4f1d2aaefc55b6fcfee13f41544ddf36801e793edbbfd7d7df075ef"
[[package]]
name = "windows_i686_gnu"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e"
[[package]]
name = "windows_i686_gnu"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a28637cb1fa3560a16915793afb20081aba2c92ee8af57b4d5f28e4b3e7df313"
[[package]]
name = "windows_i686_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406"
[[package]]
name = "windows_i686_msvc"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ffe5e8e31046ce6230cc7215707b816e339ff4d4d67c65dffa206fd0f7aa7b9a"
[[package]]
name = "windows_x86_64_gnu"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e"
[[package]]
name = "windows_x86_64_gnu"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d6fa32db2bc4a2f5abeacf2b69f7992cd09dca97498da74a151a3132c26befd"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a657e1e9d3f514745a572a6846d3c7aa7dbe1658c056ed9c3344c4109a6949e"
[[package]]
name = "windows_x86_64_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"
[[package]]
name = "windows_x86_64_msvc"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04"

View File

@ -8,4 +8,6 @@ description = "A dead-simple scripting language for use in games"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
libkabel = { version = "0.1", path = "../libkabel" }
libkabel = { version = "0.1", path = "../libkabel", features = ["pretty-emitter"] }
is-terminal = "0.4"
tracing-subscriber = "0.3"

View File

@ -2,7 +2,9 @@ use std::env;
use std::error::Error;
use std::fs::File;
use std::io::{self, Read};
use libkabel::lexutil::Token;
use libkabel::diagnostics::emitters::Emitter;
use libkabel::error::KError;
use libkabel::lexer::token::Token;
fn main() -> Result<(), Box<dyn Error>> {
let argv: Vec<String> = env::args().collect();
@ -26,17 +28,28 @@ fn main() -> Result<(), Box<dyn Error>> {
Ok(fd) => fd,
};
tracing_subscriber::fmt::init();
let mut text_source = String::new();
source_fd.read_to_string(&mut text_source)?;
// Lex!
let lexed: Vec<Token> = match libkabel::lexer::lexer(&text_source) {
Err(e) => {
eprintln!("Lexer fail with {}", e);
match e {
KError::InternalError(e) => {
eprintln!("ICE! Please report this to the kabel developers, along with what you were doing: {:?}", e);
}
KError::UserError(diags) => {
eprintln!("{}", libkabel::diagnostics::emitters::basic::BasicEmitter::emit(diags, text_source));
}
}
std::process::exit(1);
}
Ok(lexed) => lexed,
};
println!("{:#?}", lexed);
Ok(())
}

View File

@ -7,3 +7,8 @@ edition = "2021"
[dependencies]
tracing = "0.1"
colored = { version = "2", optional = true }
[features]
default = []
pretty-emitter = ["colored"]

View File

@ -0,0 +1,19 @@
use crate::diagnostics::Diagnostic;
use crate::diagnostics::emitters::Emitter;
use std::fmt::Write;
pub struct BasicEmitter;
impl Emitter for BasicEmitter {
type Output = String;
fn emit(diag: Vec<Diagnostic>, _source: String) -> Self::Output {
let mut output = String::new();
for msg in diag {
writeln!(output, "{:?}", msg).unwrap();
}
output
}
}

View File

@ -0,0 +1,10 @@
use crate::diagnostics::Diagnostic;
pub mod basic;
#[cfg(feature = "pretty-emitter")]
pub mod pretty;
pub trait Emitter {
type Output;
fn emit(diag: Vec<Diagnostic>, source: String) -> Self::Output;
}

View File

@ -0,0 +1,24 @@
use crate::diagnostics::span::Span;
pub mod span;
pub mod emitters;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Diagnostic {
pub diag_type: DiagnosticType,
pub spans: Vec<SpanWithLabel>
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DiagnosticType {
Error,
Warning,
Help
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SpanWithLabel {
pub span: Span,
pub span_type: DiagnosticType,
pub label: Option<String>,
}

View File

@ -0,0 +1,11 @@
#[derive(Debug, Clone, PartialEq, Eq, Copy)]
pub struct Span {
pub start: u64,
pub end: u64
}
impl Span {
pub fn new(start: u64, end: u64) -> Self {
Self { start, end }
}
}

10
libkabel/src/error.rs Normal file
View File

@ -0,0 +1,10 @@
use crate::diagnostics::Diagnostic;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum KError {
InternalError(InternalError),
UserError(Vec<Diagnostic>)
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum InternalError {}

View File

@ -1,3 +0,0 @@
pub enum Error {
}

View File

@ -1,7 +1,10 @@
use crate::lexutil;
use lexutil::{ArithOperator, Bracket, Literal, Statement, Token};
use std::error::Error;
use token::{ArithOperator, Bracket, Literal, Statement, Token};
use tracing::debug;
use crate::diagnostics::{Diagnostic, DiagnosticType, SpanWithLabel};
use crate::diagnostics::span::Span;
use crate::error::KError;
pub mod token;
#[derive(Debug)]
enum State {
@ -11,7 +14,7 @@ enum State {
BuildingToken,
}
pub fn lexer(text_source: &str) -> Result<Vec<Token>, Box<dyn Error>> {
pub fn lexer(text_source: &str) -> Result<Vec<Token>, KError> {
debug!("lexing!");
let mut current_token = String::new();
@ -20,7 +23,12 @@ pub fn lexer(text_source: &str) -> Result<Vec<Token>, Box<dyn Error>> {
let mut chars = text_source.chars().peekable();
let mut pos: u64 = 0;
let mut span_start: u64 = 0;
while let Some(c) = chars.next() {
pos += 1;
match state {
State::Commenting => {
// Stop commenting at end of line
@ -33,9 +41,12 @@ pub fn lexer(text_source: &str) -> Result<Vec<Token>, Box<dyn Error>> {
if let Some(c_peek) = chars.peek() {
if c != '\\' && *c_peek == '\"' {
chars.next();
pos += 1;
current_token.push(c);
let tok_cpy = current_token.clone();
lexed.push(Token::Literal(Literal::Str(tok_cpy)));
lexed.push(Token::Literal(Span::new(span_start, pos), Literal::Str(tok_cpy)));
state = State::BuildingToken;
current_token = String::new();
} else {
@ -43,25 +54,41 @@ pub fn lexer(text_source: &str) -> Result<Vec<Token>, Box<dyn Error>> {
}
} else {
continue; // we're at the end. we should bring a user error
// because this string was not properly delimited
// because this string was not properly delimited
}
}
State::Numbering => {
// If next char isn't numeric, is at end of this number literal
if let Some(c_peek) = chars.peek() {
current_token.push(c);
current_token.push(c);
if !c_peek.is_ascii_digit() {
lexed.push(Token::Literal(Literal::Num(
// if this unwrap fails, we've failed
// to confirm that this is a number literal
current_token.parse::<f64>().unwrap(),
)));
let num = match current_token.parse::<f64>() {
Ok(n) => n,
Err(_) => {
return Err(KError::UserError(vec![
Diagnostic {
diag_type: DiagnosticType::Error,
spans: vec![
SpanWithLabel {
span: Span::new(span_start, pos),
span_type: DiagnosticType::Error,
label: Some("this is not a valid numeric literal".to_string()),
}
],
}
]));
}
};
lexed.push(Token::Literal(
Span::new(span_start, pos),
Literal::Num(num)
));
state = State::BuildingToken;
current_token = String::new();
span_start = pos + 1;
}
} else {
continue; // we're at the end. not a problem because
// numbers self-terminate
}
}
State::BuildingToken => {
@ -70,15 +97,23 @@ pub fn lexer(text_source: &str) -> Result<Vec<Token>, Box<dyn Error>> {
current_token = String::new();
// We don't need to push c because it's the quote delimiter,
// which has already served its purpose as an indicator
span_start = pos;
continue;
} else if c.is_ascii_digit() {
state = State::Numbering;
current_token = c.to_string();
span_start = pos;
continue;
}
// Known meaningful tokens
if current_token.is_empty() {
span_start = pos;
}
current_token.push(c);
match current_token.as_str() {
"\n" => {
continue;
@ -88,59 +123,58 @@ pub fn lexer(text_source: &str) -> Result<Vec<Token>, Box<dyn Error>> {
current_token = String::new();
}
";" => {
lexed.push(Token::Statement(Statement::Terminator));
lexed.push(Token::Statement(Span::new(span_start, pos), Statement::Terminator));
current_token = String::new();
}
"if " => {
lexed.push(Token::Statement(Statement::Conditional));
lexed.push(Token::Statement(Span::new(span_start, pos), Statement::Conditional));
current_token = String::new();
}
"to " => {
lexed.push(Token::Statement(Statement::FunctionDef));
lexed.push(Token::Statement(Span::new(span_start, pos), Statement::FunctionDef));
current_token = String::new();
}
"for " => {
lexed.push(Token::Statement(Statement::ForLoop));
lexed.push(Token::Statement(Span::new(span_start, pos), Statement::ForLoop));
current_token = String::new();
}
"while " => {
lexed.push(Token::Statement(Statement::WhileLoop));
lexed.push(Token::Statement(Span::new(span_start, pos), Statement::WhileLoop));
current_token = String::new();
}
"(" => {
lexed.push(Token::Bracket(Bracket::Open));
lexed.push(Token::Bracket(Span::new(span_start, pos), Bracket::Open));
current_token = String::new();
}
")" => {
lexed.push(Token::Bracket(Bracket::Close));
lexed.push(Token::Bracket(Span::new(span_start, pos), Bracket::Close));
current_token = String::new();
}
"*" => {
lexed.push(Token::ArithOperator(ArithOperator::Multiply));
lexed.push(Token::ArithOperator(Span::new(span_start, pos), ArithOperator::Multiply));
current_token = String::new();
}
"/" => {
lexed.push(Token::ArithOperator(ArithOperator::Divide));
lexed.push(Token::ArithOperator(Span::new(span_start, pos), ArithOperator::Divide));
current_token = String::new();
}
"+" => {
lexed.push(Token::ArithOperator(ArithOperator::Add));
lexed.push(Token::ArithOperator(Span::new(span_start, pos), ArithOperator::Add));
current_token = String::new();
}
"-" => {
lexed.push(Token::ArithOperator(ArithOperator::Subtract));
lexed.push(Token::ArithOperator(Span::new(span_start, pos), ArithOperator::Subtract));
current_token = String::new();
}
"^" => {
lexed.push(Token::ArithOperator(ArithOperator::Exponentiate));
lexed.push(Token::ArithOperator(Span::new(span_start, pos), ArithOperator::Exponentiate));
current_token = String::new();
}
"%" => {
lexed.push(Token::ArithOperator(ArithOperator::Reduce));
lexed.push(Token::ArithOperator(Span::new(span_start, pos), ArithOperator::Reduce));
current_token = String::new();
}
&_ => {}
_ => {}
}
}
}

View File

@ -1,3 +1,4 @@
use crate::diagnostics::span::Span;
use crate::variables;
// parts of Token
@ -41,9 +42,9 @@ pub enum Bracket {
#[derive(Debug)]
pub enum Token {
Literal(Literal),
ArithOperator(ArithOperator),
Statement(Statement),
Bracket(Bracket),
Variable(variables::Variable),
Literal(Span, Literal),
ArithOperator(Span, ArithOperator),
Statement(Span, Statement),
Bracket(Span, Bracket),
Variable(Span, variables::Variable),
}

View File

@ -1,5 +1,5 @@
pub mod errors;
pub mod lexer;
pub mod lexutil;
pub mod modules;
pub mod variables;
pub mod diagnostics;
pub mod lexer;
pub mod error;