From 5709cf031ba00ce8eb330a9a72ed0f572d591bcd Mon Sep 17 00:00:00 2001 From: Tad Hardesty Date: Sun, 8 Dec 2019 00:29:39 -0800 Subject: [PATCH] Add parse_expression convenience function --- src/dreammaker/constants.rs | 3 +-- src/dreammaker/parser.rs | 17 +++++++++++++++-- src/dreammaker/preprocessor.rs | 9 ++++----- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/dreammaker/constants.rs b/src/dreammaker/constants.rs index 6241abcb..b3cfb37f 100644 --- a/src/dreammaker/constants.rs +++ b/src/dreammaker/constants.rs @@ -391,11 +391,10 @@ impl fmt::Display for ConstFn { pub fn evaluate_str(location: Location, input: &[u8]) -> Result { use super::lexer::{Lexer, from_utf8_or_latin1_borrowed}; - use super::parser::Parser; let mut bytes = input.iter().map(|&x| Ok(x)); let ctx = Context::default(); - let expr = Parser::new(&ctx, Lexer::new(&ctx, Default::default(), &mut bytes)).require_expression()?; + let expr = crate::parser::parse_expression(&ctx, location, Lexer::new(&ctx, location.file, &mut bytes))?; if bytes.next().is_some() { return Err(DMError::new(location, format!("leftover: {:?} {}", from_utf8_or_latin1_borrowed(&input), bytes.len()))); } diff --git a/src/dreammaker/parser.rs b/src/dreammaker/parser.rs index c03b9e13..34c91aea 100644 --- a/src/dreammaker/parser.rs +++ b/src/dreammaker/parser.rs @@ -26,6 +26,19 @@ where Parser::new(context, iter.into_iter()).parse_object_tree() } +/// Parse a token stream into an expression. +/// +/// Fatal errors will be directly returned and miscellaneous diagnostics will +/// be registered with the provided `Context`. +pub fn parse_expression(context: &Context, location: Location, iter: I) -> Result +where + I: IntoIterator, +{ + let mut parser = Parser::new(context, iter.into_iter()); + parser.set_fallback_location(location); + parser.require_expression() +} + type Ident = String; // ---------------------------------------------------------------------------- @@ -399,7 +412,7 @@ where self.procs = true; } - pub fn set_fallback_location(&mut self, fallback: Location) { + fn set_fallback_location(&mut self, fallback: Location) { assert!(self.location == Default::default()); self.location = fallback; } @@ -1571,7 +1584,7 @@ where } /// Parse an expression at the current position. - pub fn require_expression(&mut self) -> Result { + fn require_expression(&mut self) -> Result { Ok(require!(self.expression())) } diff --git a/src/dreammaker/preprocessor.rs b/src/dreammaker/preprocessor.rs index 8f12c1cf..6117b774 100644 --- a/src/dreammaker/preprocessor.rs +++ b/src/dreammaker/preprocessor.rs @@ -554,12 +554,11 @@ impl<'ctx> Preprocessor<'ctx> { return Ok(false); } - let mut parser = ::parser::Parser::new( + let expr = crate::parser::parse_expression( self.context, - self.output.drain(..).map(|token| LocatedToken::new(start, token)), - ); - parser.set_fallback_location(start); - let expr = parser.require_expression()?; + start, + self.output.drain(..).map(|token| LocatedToken::new(start, token)) + )?; Ok(::constants::preprocessor_evaluate(start, expr, &self.defines)?.to_bool()) }