Compare commits

...

2 commits

Author SHA1 Message Date
9510d9254c
lang: fix matrix recovery 2024-04-24 19:55:25 +02:00
e62b50a51a
lang: make Markers debuggable 2024-04-24 19:55:16 +02:00
7 changed files with 20 additions and 18 deletions

View file

@ -47,10 +47,10 @@ impl<'src, 'toks> Parser<'src, 'toks> {
self.input.kind(self.pos) 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(); let pos = self.events.len();
self.push_ev(Event::tombstone()); self.push_ev(Event::tombstone());
Marker::new(pos) Marker::new(pos, name)
} }
pub(crate) fn at(&self, kind: SyntaxKind) -> bool { pub(crate) fn at(&self, kind: SyntaxKind) -> bool {
@ -88,10 +88,10 @@ pub(crate) struct Marker {
} }
impl Marker { impl Marker {
pub(crate) fn new(pos: usize) -> Self { pub(crate) fn new(pos: usize, name: &str) -> Self {
Self { Self {
pos, 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 { impl CompletedMarker {
pub(crate) fn precede(self, p: &mut Parser<'_, '_>) -> Marker { 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] { match &mut p.events[self.pos] {
Event::Start { forward_parent, .. } => { Event::Start { forward_parent, .. } => {

View file

@ -5,7 +5,7 @@ use super::Parser;
mod expression; mod expression;
pub fn source_file(p: &mut Parser) { pub fn source_file(p: &mut Parser) {
let root = p.start(); let root = p.start("root");
expression::expression(p); expression::expression(p);
p.eat_succeeding_ws(); p.eat_succeeding_ws();

View file

@ -47,7 +47,7 @@ mod collection {
} }
pub fn expression(p: &mut Parser) -> Option<CompletedMarker> { pub fn expression(p: &mut Parser) -> Option<CompletedMarker> {
let expr = p.start(); let expr = p.start("expr");
if atom(p).or_else(|| instr(p)).is_none() { if atom(p).or_else(|| instr(p)).is_none() {
expr.abandon(p); expr.abandon(p);

View file

@ -6,7 +6,7 @@ use crate::parser::{
}; };
pub fn matrix(p: &mut Parser) -> CompletedMarker { pub fn matrix(p: &mut Parser) -> CompletedMarker {
let matrix = p.start(); let matrix = p.start("matrix");
p.eat(MAT_KW); p.eat(MAT_KW);
if !p.eat(PAT_DIMENSIONS) { if !p.eat(PAT_DIMENSIONS) {
@ -20,7 +20,7 @@ pub fn matrix(p: &mut Parser) -> CompletedMarker {
} }
fn matrix_body(p: &mut Parser) { fn matrix_body(p: &mut Parser) {
let mat_body = p.start(); let mat_body = p.start("mat_body");
if !p.eat(L_BRACK) { if !p.eat(L_BRACK) {
mat_body.complete_err(p, SyntaxError::Expected(vec![MAT_BODY])); mat_body.complete_err(p, SyntaxError::Expected(vec![MAT_BODY]));
return (); return ();
@ -28,10 +28,10 @@ fn matrix_body(p: &mut Parser) {
let mut going = true; let mut going = true;
let mut mat_row = p.start(); let mut mat_row = p.start("mat_row");
let mut row_items = 0; let mut row_items = 0;
while going { while going {
let mat_item = p.start(); let mat_item = p.start("mat_item");
if expression(p).is_some() { if expression(p).is_some() {
mat_item.complete(p, MAT_ITEM); mat_item.complete(p, MAT_ITEM);
row_items += 1; row_items += 1;
@ -39,22 +39,24 @@ fn matrix_body(p: &mut Parser) {
COMMA => p.do_bump(), COMMA => p.do_bump(),
SEMICOLON => { SEMICOLON => {
mat_row.complete(p, MAT_ROW); mat_row.complete(p, MAT_ROW);
mat_row = p.start(); mat_row = p.start("mat_row");
p.do_bump(); p.do_bump();
row_items = 0; row_items = 0;
} }
R_BRACK => going = false, R_BRACK => going = false,
_ => { _ => {
let err = p.start(); let err = p.start("err");
p.do_bump(); p.do_bump();
err.complete_err(p, SyntaxError::Expected(vec![COMMA, SEMICOLON, R_BRACK])); err.complete_err(p, SyntaxError::Expected(vec![COMMA, SEMICOLON, R_BRACK]));
} }
}; };
} else if p.at(R_BRACK) { } else if p.at(R_BRACK) {
mat_item.abandon(p);
going = false; going = false;
} else { } else {
let err = p.start(); mat_item.abandon(p);
let err = p.start("err");
p.do_bump(); p.do_bump();
err.complete_err(p, SyntaxError::Expected(vec![EXPR, R_BRACK])); err.complete_err(p, SyntaxError::Expected(vec![EXPR, R_BRACK]));
} }

View file

@ -7,7 +7,7 @@ pub fn instr(p: &mut Parser) -> Option<CompletedMarker> {
return None; return None;
} }
let instr = p.start(); let instr = p.start("instr");
instr_name(p); instr_name(p);
instr_params(p); instr_params(p);
@ -16,7 +16,7 @@ pub fn instr(p: &mut Parser) -> Option<CompletedMarker> {
} }
fn instr_name(p: &mut Parser) { fn instr_name(p: &mut Parser) {
let instr_name = p.start(); let instr_name = p.start("instr_name");
while p.at(IDENT) { while p.at(IDENT) {
p.do_bump(); p.do_bump();

View file

@ -12,7 +12,7 @@ pub fn literal(p: &mut Parser) -> Option<CompletedMarker> {
return None; return None;
} }
let lit = p.start(); let lit = p.start("lit");
p.do_bump(); p.do_bump();

View file

@ -1,4 +1,4 @@
mat 2x2 [ mat 2x2 [
1, 2; 1 2, 2;
3, 4 3, 4
] ]