From 0ae26c344aa7477a18c2019cfa0062a9a745d70d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 9 Feb 2018 22:44:50 +0300 Subject: Drop ErrorBuilder nonsense --- src/lib.rs | 2 +- src/parser/event.rs | 6 +++-- src/parser/grammar/attributes.rs | 8 +++---- src/parser/grammar/expressions.rs | 2 +- src/parser/grammar/items/mod.rs | 8 +++---- src/parser/grammar/items/structs.rs | 4 ++-- src/parser/grammar/items/use_item.rs | 2 +- src/parser/grammar/mod.rs | 6 ++--- src/parser/grammar/paths.rs | 4 +++- src/parser/parser.rs | 33 ++++++++++++-------------- src/tree/file_builder.rs | 45 +++++++++++------------------------- src/tree/mod.rs | 2 +- 12 files changed, 52 insertions(+), 70 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 0ea8f6a63..f4a313b7a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,7 +25,7 @@ mod parser; pub mod syntax_kinds; pub use text::{TextRange, TextUnit}; pub use tree::{File, Node, SyntaxKind, Token}; -pub(crate) use tree::{FileBuilder, Sink}; +pub(crate) use tree::{FileBuilder, Sink, ErrorMsg}; pub use lexer::{next_token, tokenize}; pub use parser::parse; diff --git a/src/parser/event.rs b/src/parser/event.rs index e97350c89..546dbcf62 100644 --- a/src/parser/event.rs +++ b/src/parser/event.rs @@ -1,4 +1,4 @@ -use {File, FileBuilder, Sink, SyntaxKind, TextUnit, Token}; +use {File, FileBuilder, ErrorMsg, Sink, SyntaxKind, TextUnit, Token}; use syntax_kinds::TOMBSTONE; use super::is_insignificant; @@ -140,7 +140,9 @@ pub(super) fn to_file(text: String, tokens: &[Token], events: Vec) -> Fil } builder.leaf(kind, len); } - &Event::Error { ref message } => builder.error().message(message.clone()).emit(), + &Event::Error { ref message } => builder.error(ErrorMsg { + message: message.clone() + }) } } builder.finish() diff --git a/src/parser/grammar/attributes.rs b/src/parser/grammar/attributes.rs index 8bf04afce..92dfb99ef 100644 --- a/src/parser/grammar/attributes.rs +++ b/src/parser/grammar/attributes.rs @@ -37,7 +37,7 @@ fn meta_item(p: &mut Parser) { EQ => { p.bump(); if !expressions::literal(p) { - p.error().message("expected literal").emit(); + p.error("expected literal"); } } L_PAREN => meta_item_arg_list(p), @@ -45,7 +45,7 @@ fn meta_item(p: &mut Parser) { } meta_item.complete(p, META_ITEM); } else { - p.error().message("expected attribute value").emit() + p.error("expected attribute value"); } } @@ -60,12 +60,12 @@ fn meta_item_arg_list(p: &mut Parser) { let message = "expected attribute"; if items::ITEM_FIRST.contains(c) { - p.error().message(message).emit(); + p.error(message); return; } let err = p.start(); - p.error().message(message).emit(); + p.error(message); p.bump(); err.complete(p, ERROR); continue; diff --git a/src/parser/grammar/expressions.rs b/src/parser/grammar/expressions.rs index 8caaf3553..3704cb16f 100644 --- a/src/parser/grammar/expressions.rs +++ b/src/parser/grammar/expressions.rs @@ -15,6 +15,6 @@ pub(super) fn literal(p: &mut Parser) -> bool { pub(super) fn expr(p: &mut Parser) { if !literal(p) { - p.error().message("expected expression").emit(); + p.error("expected expression"); } } diff --git a/src/parser/grammar/items/mod.rs b/src/parser/grammar/items/mod.rs index 37f2ab132..b73628ec0 100644 --- a/src/parser/grammar/items/mod.rs +++ b/src/parser/grammar/items/mod.rs @@ -51,7 +51,7 @@ fn item(p: &mut Parser) { // extern struct Foo; _ => { item.abandon(p); - p.error().message("expected `fn` or `{`").emit(); + p.error("expected `fn` or `{`"); return; } } @@ -121,7 +121,7 @@ fn item(p: &mut Parser) { abi(p); if !p.at(FN_KW) { item.abandon(p); - p.error().message("expected function").emit(); + p.error("expected function"); return; } fn_item(p); @@ -144,7 +144,7 @@ fn item(p: &mut Parser) { if t == L_CURLY { error_block(p, message); } else { - p.error().message(message).emit(); + p.error(message); } return; } @@ -234,7 +234,7 @@ fn fn_item(p: &mut Parser) { if p.at(L_PAREN) { fn_value_parameters(p); } else { - p.error().message("expected function arguments").emit(); + p.error("expected function arguments"); } if p.at(L_CURLY) { diff --git a/src/parser/grammar/items/structs.rs b/src/parser/grammar/items/structs.rs index 69d95c698..640b940e4 100644 --- a/src/parser/grammar/items/structs.rs +++ b/src/parser/grammar/items/structs.rs @@ -19,7 +19,7 @@ pub(super) fn struct_item(p: &mut Parser) { L_CURLY => named_fields(p), _ => { //TODO: special case `(` error message - p.error().message("expected `;` or `{`").emit(); + p.error("expected `;` or `{`"); return; } } @@ -34,7 +34,7 @@ pub(super) fn struct_item(p: &mut Parser) { p.expect(SEMI); } _ => { - p.error().message("expected `;`, `{`, or `(`").emit(); + p.error("expected `;`, `{`, or `(`"); return; } } diff --git a/src/parser/grammar/items/use_item.rs b/src/parser/grammar/items/use_item.rs index 38e7b3f8a..a3f7f0da8 100644 --- a/src/parser/grammar/items/use_item.rs +++ b/src/parser/grammar/items/use_item.rs @@ -37,7 +37,7 @@ fn use_tree(p: &mut Parser) { L_CURLY => nested_trees(p), _ => { // is this unreachable? - p.error().message("expected `{` or `*`").emit(); + p.error("expected `{` or `*`"); } } } diff --git a/src/parser/grammar/mod.rs b/src/parser/grammar/mod.rs index afce308d0..b949583ff 100644 --- a/src/parser/grammar/mod.rs +++ b/src/parser/grammar/mod.rs @@ -53,7 +53,7 @@ fn alias(p: &mut Parser) -> bool { fn error_block(p: &mut Parser, message: &str) { assert!(p.at(L_CURLY)); let err = p.start(); - p.error().message(message).emit(); + p.error(message); p.bump(); let mut level: u32 = 1; while level > 0 && !p.at(EOF) { @@ -74,7 +74,7 @@ impl<'p> Parser<'p> { fn err_and_bump(&mut self, message: &str) { let err = self.start(); - self.error().message(message).emit(); + self.error(message); self.bump(); err.complete(self, ERROR); } @@ -84,7 +84,7 @@ impl<'p> Parser<'p> { self.bump(); true } else { - self.error().message(format!("expected {:?}", kind)).emit(); + self.error(format!("expected {:?}", kind)); false } } diff --git a/src/parser/grammar/paths.rs b/src/parser/grammar/paths.rs index 6efac2610..1aa30f28b 100644 --- a/src/parser/grammar/paths.rs +++ b/src/parser/grammar/paths.rs @@ -43,7 +43,9 @@ fn path_segment(p: &mut Parser, first: bool) { } match p.current() { IDENT | SELF_KW | SUPER_KW => p.bump(), - _ => p.error().message("expected identifier").emit(), + _ => { + p.error("expected identifier"); + }, }; segment.complete(p, PATH_SEGMENT); } diff --git a/src/parser/parser.rs b/src/parser/parser.rs index 752d532d0..069701483 100644 --- a/src/parser/parser.rs +++ b/src/parser/parser.rs @@ -27,9 +27,9 @@ impl Marker { if idx == p.events.len() - 1 { match p.events.pop() { Some(Event::Start { - kind: TOMBSTONE, - forward_parent: None, - }) => (), + kind: TOMBSTONE, + forward_parent: None, + }) => (), _ => unreachable!(), } } @@ -129,8 +129,8 @@ impl<'t> Parser<'t> { m } - pub(crate) fn error<'p>(&'p mut self) -> ErrorBuilder<'p, 't> { - ErrorBuilder::new(self) + pub(crate) fn error<'p, T: Into>(&'p mut self, msg: T) -> ErrorBuilder<'p, 't> { + ErrorBuilder::new(self, msg.into()) } pub(crate) fn bump(&mut self) { @@ -175,25 +175,22 @@ impl<'t> Parser<'t> { } pub(crate) struct ErrorBuilder<'p, 't: 'p> { - message: Option, + message: String, parser: &'p mut Parser<'t>, } +impl<'p, 't: 'p> Drop for ErrorBuilder<'p, 't> { + fn drop(&mut self) { + let message = ::std::mem::replace(&mut self.message, String::new()); + self.parser.event(Event::Error { message }); + } +} + impl<'t, 'p> ErrorBuilder<'p, 't> { - fn new(parser: &'p mut Parser<'t>) -> Self { + fn new(parser: &'p mut Parser<'t>, message: String) -> Self { ErrorBuilder { - message: None, + message, parser, } } - - pub fn message>(mut self, m: M) -> Self { - self.message = Some(m.into()); - self - } - - pub fn emit(self) { - let message = self.message.expect("Error message not set"); - self.parser.event(Event::Error { message }); - } } diff --git a/src/tree/file_builder.rs b/src/tree/file_builder.rs index 47038496d..81702b83e 100644 --- a/src/tree/file_builder.rs +++ b/src/tree/file_builder.rs @@ -14,7 +14,7 @@ pub(crate) trait Sink { fn leaf(&mut self, kind: SyntaxKind, len: TextUnit); fn start_internal(&mut self, kind: SyntaxKind); fn finish_internal(&mut self); - fn error(&mut self) -> ErrorBuilder; + fn error(&mut self, err: ErrorMsg); } #[derive(Debug)] @@ -22,7 +22,8 @@ pub(crate) struct FileBuilder { text: String, nodes: Vec, errors: Vec, - in_progress: Vec<(NodeIdx, Option)>, // (parent, last_child) + in_progress: Vec<(NodeIdx, Option)>, + // (parent, last_child) pos: TextUnit, } @@ -65,8 +66,13 @@ impl Sink for FileBuilder { } } - fn error(&mut self) -> ErrorBuilder { - ErrorBuilder::new(self) + fn error(&mut self, err: ErrorMsg) { + let &(node, after_child) = self.in_progress.last().unwrap(); + self.errors.push(SyntaxErrorData { + node, + message: err.message, + after_child, + }) } } @@ -149,32 +155,7 @@ fn grow(left: &mut TextRange, right: TextRange) { *left = TextRange::from_to(left.start(), right.end()) } -#[derive(Debug)] -pub struct ErrorBuilder<'f> { - message: Option, - builder: &'f mut FileBuilder, -} - -impl<'f> ErrorBuilder<'f> { - fn new(builder: &'f mut FileBuilder) -> Self { - ErrorBuilder { - message: None, - builder, - } - } - - pub fn message>(mut self, m: M) -> Self { - self.message = Some(m.into()); - self - } - - pub fn emit(self) { - let message = self.message.expect("Error message not set"); - let &(node, after_child) = self.builder.in_progress.last().unwrap(); - self.builder.errors.push(SyntaxErrorData { - node, - message, - after_child, - }) - } +#[derive(Default)] +pub(crate) struct ErrorMsg { + pub(crate) message: String } diff --git a/src/tree/mod.rs b/src/tree/mod.rs index 9ed0504c7..b7ed59793 100644 --- a/src/tree/mod.rs +++ b/src/tree/mod.rs @@ -4,7 +4,7 @@ use std::fmt; use std::cmp; mod file_builder; -pub(crate) use self::file_builder::{FileBuilder, Sink}; +pub(crate) use self::file_builder::{FileBuilder, Sink, ErrorMsg}; pub use syntax_kinds::SyntaxKind; -- cgit v1.2.3 From ac932df22a993f0b287e60aa42055405ef94a6ae Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 9 Feb 2018 22:55:50 +0300 Subject: reformat --- src/lib.rs | 2 +- src/parser/event.rs | 6 +++--- src/parser/grammar/paths.rs | 2 +- src/parser/parser.rs | 11 ++++------- src/tree/file_builder.rs | 2 +- src/tree/mod.rs | 2 +- 6 files changed, 11 insertions(+), 14 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index f4a313b7a..153458644 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,7 +25,7 @@ mod parser; pub mod syntax_kinds; pub use text::{TextRange, TextUnit}; pub use tree::{File, Node, SyntaxKind, Token}; -pub(crate) use tree::{FileBuilder, Sink, ErrorMsg}; +pub(crate) use tree::{ErrorMsg, FileBuilder, Sink}; pub use lexer::{next_token, tokenize}; pub use parser::parse; diff --git a/src/parser/event.rs b/src/parser/event.rs index 546dbcf62..90348398e 100644 --- a/src/parser/event.rs +++ b/src/parser/event.rs @@ -1,4 +1,4 @@ -use {File, FileBuilder, ErrorMsg, Sink, SyntaxKind, TextUnit, Token}; +use {ErrorMsg, File, FileBuilder, Sink, SyntaxKind, TextUnit, Token}; use syntax_kinds::TOMBSTONE; use super::is_insignificant; @@ -141,8 +141,8 @@ pub(super) fn to_file(text: String, tokens: &[Token], events: Vec) -> Fil builder.leaf(kind, len); } &Event::Error { ref message } => builder.error(ErrorMsg { - message: message.clone() - }) + message: message.clone(), + }), } } builder.finish() diff --git a/src/parser/grammar/paths.rs b/src/parser/grammar/paths.rs index 1aa30f28b..a7fc90774 100644 --- a/src/parser/grammar/paths.rs +++ b/src/parser/grammar/paths.rs @@ -45,7 +45,7 @@ fn path_segment(p: &mut Parser, first: bool) { IDENT | SELF_KW | SUPER_KW => p.bump(), _ => { p.error("expected identifier"); - }, + } }; segment.complete(p, PATH_SEGMENT); } diff --git a/src/parser/parser.rs b/src/parser/parser.rs index 069701483..7c8e47cb6 100644 --- a/src/parser/parser.rs +++ b/src/parser/parser.rs @@ -27,9 +27,9 @@ impl Marker { if idx == p.events.len() - 1 { match p.events.pop() { Some(Event::Start { - kind: TOMBSTONE, - forward_parent: None, - }) => (), + kind: TOMBSTONE, + forward_parent: None, + }) => (), _ => unreachable!(), } } @@ -188,9 +188,6 @@ impl<'p, 't: 'p> Drop for ErrorBuilder<'p, 't> { impl<'t, 'p> ErrorBuilder<'p, 't> { fn new(parser: &'p mut Parser<'t>, message: String) -> Self { - ErrorBuilder { - message, - parser, - } + ErrorBuilder { message, parser } } } diff --git a/src/tree/file_builder.rs b/src/tree/file_builder.rs index 81702b83e..3c7e2d7cf 100644 --- a/src/tree/file_builder.rs +++ b/src/tree/file_builder.rs @@ -157,5 +157,5 @@ fn grow(left: &mut TextRange, right: TextRange) { #[derive(Default)] pub(crate) struct ErrorMsg { - pub(crate) message: String + pub(crate) message: String, } diff --git a/src/tree/mod.rs b/src/tree/mod.rs index b7ed59793..ebf26777b 100644 --- a/src/tree/mod.rs +++ b/src/tree/mod.rs @@ -4,7 +4,7 @@ use std::fmt; use std::cmp; mod file_builder; -pub(crate) use self::file_builder::{FileBuilder, Sink, ErrorMsg}; +pub(crate) use self::file_builder::{ErrorMsg, FileBuilder, Sink}; pub use syntax_kinds::SyntaxKind; -- cgit v1.2.3