From e62b50a51ae0d2b2e68a0a42d7ad8f3180c0b3e0 Mon Sep 17 00:00:00 2001 From: Schrottkatze Date: Wed, 24 Apr 2024 19:55:16 +0200 Subject: [PATCH] lang: make `Marker`s debuggable --- crates/lang/src/parser.rs | 10 +++++----- crates/lang/src/parser/grammar.rs | 2 +- crates/lang/src/parser/grammar/expression.rs | 2 +- .../parser/grammar/expression/collection/matrix.rs | 14 +++++++------- .../src/parser/grammar/expression/instruction.rs | 4 ++-- crates/lang/src/parser/grammar/expression/lit.rs | 2 +- testfiles/test.owo | 2 +- 7 files changed, 18 insertions(+), 18 deletions(-) diff --git a/crates/lang/src/parser.rs b/crates/lang/src/parser.rs index 996f15a..dfd80c5 100644 --- a/crates/lang/src/parser.rs +++ b/crates/lang/src/parser.rs @@ -47,10 +47,10 @@ impl<'src, 'toks> Parser<'src, 'toks> { self.input.kind(self.pos) } - pub(crate) fn start(&mut self) -> Marker { + pub(crate) fn start(&mut self, name: &str) -> Marker { let pos = self.events.len(); self.push_ev(Event::tombstone()); - Marker::new(pos) + Marker::new(pos, name) } pub(crate) fn at(&self, kind: SyntaxKind) -> bool { @@ -88,10 +88,10 @@ pub(crate) struct Marker { } impl Marker { - pub(crate) fn new(pos: usize) -> Self { + pub(crate) fn new(pos: usize, name: &str) -> Self { Self { pos, - bomb: DropBomb::new("Marker must be completed or abandoned"), + bomb: DropBomb::new(format!("Marker {name} must be completed or abandoned")), } } @@ -135,7 +135,7 @@ pub(crate) struct CompletedMarker { impl CompletedMarker { pub(crate) fn precede(self, p: &mut Parser<'_, '_>) -> Marker { - let new_pos = p.start(); + let new_pos = p.start("new_pos"); match &mut p.events[self.pos] { Event::Start { forward_parent, .. } => { diff --git a/crates/lang/src/parser/grammar.rs b/crates/lang/src/parser/grammar.rs index f301d4e..75ed999 100644 --- a/crates/lang/src/parser/grammar.rs +++ b/crates/lang/src/parser/grammar.rs @@ -5,7 +5,7 @@ use super::Parser; mod expression; pub fn source_file(p: &mut Parser) { - let root = p.start(); + let root = p.start("root"); expression::expression(p); p.eat_succeeding_ws(); diff --git a/crates/lang/src/parser/grammar/expression.rs b/crates/lang/src/parser/grammar/expression.rs index cb1f410..3f3a43e 100644 --- a/crates/lang/src/parser/grammar/expression.rs +++ b/crates/lang/src/parser/grammar/expression.rs @@ -47,7 +47,7 @@ mod collection { } pub fn expression(p: &mut Parser) -> Option { - let expr = p.start(); + let expr = p.start("expr"); if atom(p).or_else(|| instr(p)).is_none() { expr.abandon(p); diff --git a/crates/lang/src/parser/grammar/expression/collection/matrix.rs b/crates/lang/src/parser/grammar/expression/collection/matrix.rs index 58ba9e1..2c572f5 100644 --- a/crates/lang/src/parser/grammar/expression/collection/matrix.rs +++ b/crates/lang/src/parser/grammar/expression/collection/matrix.rs @@ -6,7 +6,7 @@ use crate::parser::{ }; pub fn matrix(p: &mut Parser) -> CompletedMarker { - let matrix = p.start(); + let matrix = p.start("matrix"); p.eat(MAT_KW); if !p.eat(PAT_DIMENSIONS) { @@ -20,7 +20,7 @@ pub fn matrix(p: &mut Parser) -> CompletedMarker { } fn matrix_body(p: &mut Parser) { - let mat_body = p.start(); + let mat_body = p.start("mat_body"); if !p.eat(L_BRACK) { mat_body.complete_err(p, SyntaxError::Expected(vec![MAT_BODY])); return (); @@ -28,10 +28,10 @@ fn matrix_body(p: &mut Parser) { let mut going = true; - let mut mat_row = p.start(); + let mut mat_row = p.start("mat_row"); let mut row_items = 0; while going { - let mat_item = p.start(); + let mat_item = p.start("mat_item"); if expression(p).is_some() { mat_item.complete(p, MAT_ITEM); row_items += 1; @@ -39,13 +39,13 @@ fn matrix_body(p: &mut Parser) { COMMA => p.do_bump(), SEMICOLON => { mat_row.complete(p, MAT_ROW); - mat_row = p.start(); + mat_row = p.start("mat_row"); p.do_bump(); row_items = 0; } R_BRACK => going = false, _ => { - let err = p.start(); + let err = p.start("err"); p.do_bump(); err.complete_err(p, SyntaxError::Expected(vec![COMMA, SEMICOLON, R_BRACK])); @@ -54,7 +54,7 @@ fn matrix_body(p: &mut Parser) { } else if p.at(R_BRACK) { going = false; } else { - let err = p.start(); + let err = p.start("err"); p.do_bump(); err.complete_err(p, SyntaxError::Expected(vec![EXPR, R_BRACK])); } diff --git a/crates/lang/src/parser/grammar/expression/instruction.rs b/crates/lang/src/parser/grammar/expression/instruction.rs index 5da0f43..5a12f4c 100644 --- a/crates/lang/src/parser/grammar/expression/instruction.rs +++ b/crates/lang/src/parser/grammar/expression/instruction.rs @@ -7,7 +7,7 @@ pub fn instr(p: &mut Parser) -> Option { return None; } - let instr = p.start(); + let instr = p.start("instr"); instr_name(p); instr_params(p); @@ -16,7 +16,7 @@ pub fn instr(p: &mut Parser) -> Option { } fn instr_name(p: &mut Parser) { - let instr_name = p.start(); + let instr_name = p.start("instr_name"); while p.at(IDENT) { p.do_bump(); diff --git a/crates/lang/src/parser/grammar/expression/lit.rs b/crates/lang/src/parser/grammar/expression/lit.rs index bb48360..afc1408 100644 --- a/crates/lang/src/parser/grammar/expression/lit.rs +++ b/crates/lang/src/parser/grammar/expression/lit.rs @@ -12,7 +12,7 @@ pub fn literal(p: &mut Parser) -> Option { return None; } - let lit = p.start(); + let lit = p.start("lit"); p.do_bump(); diff --git a/testfiles/test.owo b/testfiles/test.owo index 87a5bc8..4a3beda 100644 --- a/testfiles/test.owo +++ b/testfiles/test.owo @@ -1,4 +1,4 @@ mat 2x2 [ - 1, 2; + 1 2, 2; 3, 4 ]