From 9141cba6a9db6ae3106e39d92508cb91c390049b Mon Sep 17 00:00:00 2001 From: Martin Haug Date: Mon, 8 Nov 2021 12:01:35 +0100 Subject: Deal with the effects of keywords --- src/parse/mod.rs | 43 ++++++++++++++++++++++++++++++++++++++----- src/parse/parser.rs | 6 ++++++ 2 files changed, 44 insertions(+), 5 deletions(-) (limited to 'src/parse') diff --git a/src/parse/mod.rs b/src/parse/mod.rs index 02777350..afeb34f1 100644 --- a/src/parse/mod.rs +++ b/src/parse/mod.rs @@ -29,7 +29,7 @@ pub fn parse(src: &str) -> Rc { pub fn parse_atomic(src: &str, _: bool) -> Option> { let mut p = Parser::new(src, TokenMode::Code); primary(&mut p, true).ok()?; - p.eject() + p.eject_partial() } /// Parse some markup. Returns `Some` if all of the input was consumed. @@ -49,10 +49,32 @@ pub fn parse_markup_elements(src: &str, mut at_start: bool) -> Option p.eject() } -/// Parse some code. Returns `Some` if all of the input was consumed. -pub fn parse_code(src: &str, _: bool) -> Option> { - let mut p = Parser::new(src, TokenMode::Code); - expr_list(&mut p); +/// Parse a template literal. Returns `Some` if all of the input was consumed. +pub fn parse_template(source: &str, _: bool) -> Option> { + let mut p = Parser::new(source, TokenMode::Code); + if !matches!(p.peek(), Some(NodeKind::LeftBracket)) { + return None; + } + + template(&mut p); + p.eject() +} + +/// Parse a code block. Returns `Some` if all of the input was consumed. +pub fn parse_block(source: &str, _: bool) -> Option> { + let mut p = Parser::new(source, TokenMode::Code); + if !matches!(p.peek(), Some(NodeKind::LeftBrace)) { + return None; + } + + block(&mut p); + p.eject() +} + +/// Parse a comment. Returns `Some` if all of the input was consumed. +pub fn parse_comment(source: &str, _: bool) -> Option> { + let mut p = Parser::new(source, TokenMode::Code); + comment(&mut p).ok()?; p.eject() } @@ -742,3 +764,14 @@ fn body(p: &mut Parser) -> ParseResult { } Ok(()) } + +/// Parse a comment. +fn comment(p: &mut Parser) -> ParseResult { + match p.peek() { + Some(NodeKind::LineComment | NodeKind::BlockComment) => { + p.eat(); + Ok(()) + } + _ => Err(()), + } +} diff --git a/src/parse/parser.rs b/src/parse/parser.rs index 451e18f1..31c918a8 100644 --- a/src/parse/parser.rs +++ b/src/parse/parser.rs @@ -95,6 +95,12 @@ impl<'s> Parser<'s> { output } + /// End the parsing process and return multiple children, even if there + /// remains stuff in the string. + pub fn eject_partial(self) -> Option> { + self.group_success().then(|| self.children) + } + /// Whether the end of the source string or group is reached. pub fn eof(&self) -> bool { self.eof -- cgit v1.2.3