Skip to content

Commit

Permalink
Using ParseResult in more places
Browse files Browse the repository at this point in the history
  • Loading branch information
Razican committed Nov 5, 2022
1 parent 86efec4 commit 5438162
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 30 deletions.
12 changes: 6 additions & 6 deletions boa_engine/src/syntax/parser/cursor/buffered_lexer/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::syntax::{
lexer::{InputElement, Lexer, Token, TokenKind},
parser::error::ParseError,
parser::{error::ParseError, ParseResult},
};
use boa_ast::Position;
use boa_interner::Interner;
Expand Down Expand Up @@ -83,7 +83,7 @@ where
&mut self,
start: Position,
interner: &mut Interner,
) -> Result<Token, ParseError> {
) -> ParseResult<Token> {
let _timer = Profiler::global().start_event("cursor::lex_regex()", "Parsing");
self.set_goal(InputElement::RegExp);
self.lexer
Expand All @@ -97,7 +97,7 @@ where
&mut self,
start: Position,
interner: &mut Interner,
) -> Result<Token, ParseError> {
) -> ParseResult<Token> {
self.lexer
.lex_template(start, interner)
.map_err(ParseError::from)
Expand All @@ -116,7 +116,7 @@ where
/// Fills the peeking buffer with the next token.
///
/// It will not fill two line terminators one after the other.
fn fill(&mut self, interner: &mut Interner) -> Result<(), ParseError> {
fn fill(&mut self, interner: &mut Interner) -> ParseResult<()> {
debug_assert!(
self.write_index < PEEK_BUF_SIZE,
"write index went out of bounds"
Expand Down Expand Up @@ -171,7 +171,7 @@ where
&mut self,
skip_line_terminators: bool,
interner: &mut Interner,
) -> Result<Option<Token>, ParseError> {
) -> ParseResult<Option<Token>> {
if self.read_index == self.write_index {
self.fill(interner)?;
}
Expand Down Expand Up @@ -217,7 +217,7 @@ where
skip_n: usize,
skip_line_terminators: bool,
interner: &mut Interner,
) -> Result<Option<&Token>, ParseError> {
) -> ParseResult<Option<&Token>> {
assert!(
skip_n <= MAX_PEEK_SKIP,
"you cannot skip more than {} elements",
Expand Down
26 changes: 13 additions & 13 deletions boa_engine/src/syntax/parser/cursor/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Cursor implementation for the parser.
mod buffered_lexer;

use super::{statement::PrivateElement, OrAbrupt, ParseError};
use super::{statement::PrivateElement, OrAbrupt, ParseError, ParseResult};
use crate::syntax::lexer::{InputElement, Lexer, Token, TokenKind};
use boa_ast::{Position, Punctuator};
use boa_interner::{Interner, Sym};
Expand Down Expand Up @@ -54,7 +54,7 @@ where
&mut self,
start: Position,
interner: &mut Interner,
) -> Result<Token, ParseError> {
) -> ParseResult<Token> {
self.buffered_lexer.lex_regex(start, interner)
}

Expand All @@ -63,13 +63,13 @@ where
&mut self,
start: Position,
interner: &mut Interner,
) -> Result<Token, ParseError> {
) -> ParseResult<Token> {
self.buffered_lexer.lex_template(start, interner)
}

/// Advances the cursor and returns the next token.
#[inline]
pub(super) fn next(&mut self, interner: &mut Interner) -> Result<Option<Token>, ParseError> {
pub(super) fn next(&mut self, interner: &mut Interner) -> ParseResult<Option<Token>> {
self.buffered_lexer.next(true, interner)
}

Expand All @@ -95,7 +95,7 @@ where
&mut self,
skip_n: usize,
interner: &mut Interner,
) -> Result<Option<&Token>, ParseError> {
) -> ParseResult<Option<&Token>> {
self.buffered_lexer.peek(skip_n, true, interner)
}

Expand Down Expand Up @@ -136,7 +136,7 @@ where
&mut self,
identifier: Sym,
position: Position,
) -> Result<(), ParseError> {
) -> ParseResult<()> {
if let Some(env) = self.private_environments_stack.last_mut() {
env.entry(identifier).or_insert(position);
Ok(())
Expand All @@ -156,7 +156,7 @@ where
pub(super) fn pop_private_environment(
&mut self,
identifiers: &FxHashMap<Sym, PrivateElement>,
) -> Result<(), ParseError> {
) -> ParseResult<()> {
let last = self
.private_environments_stack
.pop()
Expand All @@ -183,7 +183,7 @@ where
kind: K,
context: &'static str,
interner: &mut Interner,
) -> Result<Token, ParseError>
) -> ParseResult<Token>
where
K: Into<TokenKind>,
{
Expand Down Expand Up @@ -211,7 +211,7 @@ where
pub(super) fn peek_semicolon(
&mut self,
interner: &mut Interner,
) -> Result<SemicolonResult<'_>, ParseError> {
) -> ParseResult<SemicolonResult<'_>> {
match self.buffered_lexer.peek(0, false, interner)? {
Some(tk) => match tk.kind() {
TokenKind::Punctuator(Punctuator::Semicolon | Punctuator::CloseBlock)
Expand All @@ -232,7 +232,7 @@ where
&mut self,
context: &'static str,
interner: &mut Interner,
) -> Result<(), ParseError> {
) -> ParseResult<()> {
match self.peek_semicolon(interner)? {
SemicolonResult::Found(Some(tk)) => match *tk.kind() {
TokenKind::Punctuator(Punctuator::Semicolon) | TokenKind::LineTerminator => {
Expand Down Expand Up @@ -263,7 +263,7 @@ where
skip_n: usize,
context: &'static str,
interner: &mut Interner,
) -> Result<&Token, ParseError> {
) -> ParseResult<&Token> {
if let Some(t) = self.buffered_lexer.peek(skip_n, false, interner)? {
if t.kind() == &TokenKind::LineTerminator {
Err(ParseError::unexpected(
Expand All @@ -285,7 +285,7 @@ where
&mut self,
skip_n: usize,
interner: &mut Interner,
) -> Result<Option<bool>, ParseError> {
) -> ParseResult<Option<bool>> {
if let Some(t) = self.buffered_lexer.peek(skip_n, false, interner)? {
Ok(Some(t.kind() == &TokenKind::LineTerminator))
} else {
Expand All @@ -303,7 +303,7 @@ where
&mut self,
kind: K,
interner: &mut Interner,
) -> Result<Option<Token>, ParseError>
) -> ParseResult<Option<Token>>
where
K: Into<TokenKind>,
{
Expand Down
4 changes: 3 additions & 1 deletion boa_engine/src/syntax/parser/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ pub(crate) trait ErrorContext {
fn context(self, context: &'static str) -> Self;
}

impl<T> ErrorContext for Result<T, ParseError> {
impl<T> ErrorContext for ParseResult<T> {
#[inline]
fn context(self, context: &'static str) -> Self {
self.map_err(|e| e.context(context))
}
}

impl From<LexError> for ParseError {
#[inline]
fn from(e: LexError) -> Self {
Self::lex(e)
}
Expand Down
2 changes: 1 addition & 1 deletion boa_engine/src/syntax/parser/expression/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ expression!(
);

/// Returns an error if `arguments` or `eval` are used as identifier in strict mode.
fn check_strict_arguments_or_eval(ident: Identifier, position: Position) -> Result<(), ParseError> {
fn check_strict_arguments_or_eval(ident: Identifier, position: Position) -> ParseResult<()> {
match ident.sym() {
Sym::ARGUMENTS => Err(ParseError::general(
"unexpected identifier 'arguments' in strict mode",
Expand Down
2 changes: 1 addition & 1 deletion boa_engine/src/syntax/parser/expression/primary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ fn expression_to_formal_parameters(
parameters: &mut Vec<FormalParameter>,
strict: bool,
span: Span,
) -> Result<(), ParseError> {
) -> ParseResult<()> {
match node {
ast::Expression::Identifier(identifier) if strict && *identifier == Sym::EVAL => {
return Err(ParseError::general(
Expand Down
12 changes: 6 additions & 6 deletions boa_engine/src/syntax/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ impl<R> Parser<R> {
/// The resulting `StatementList` can be compiled into boa bytecode and executed in the boa vm.
///
/// [spec]: https://tc39.es/ecma262/#prod-Script
pub fn parse_all(&mut self, context: &mut Context) -> Result<StatementList, ParseError>
pub fn parse_all(&mut self, context: &mut Context) -> ParseResult<StatementList>
where
R: Read,
{
Expand All @@ -146,7 +146,7 @@ impl<R> Parser<R> {
&mut self,
direct: bool,
context: &mut Context,
) -> Result<StatementList, ParseError>
) -> ParseResult<StatementList>
where
R: Read,
{
Expand Down Expand Up @@ -238,7 +238,7 @@ impl<R> Parser<R> {
interner: &mut Interner,
allow_yield: bool,
allow_await: bool,
) -> Result<StatementList, ParseError>
) -> ParseResult<StatementList>
where
R: Read,
{
Expand All @@ -253,7 +253,7 @@ impl<R> Parser<R> {
interner: &mut Interner,
allow_yield: bool,
allow_await: bool,
) -> Result<FormalParameterList, ParseError>
) -> ParseResult<FormalParameterList>
where
R: Read,
{
Expand Down Expand Up @@ -282,7 +282,7 @@ impl Script {
self,
cursor: &mut Cursor<R>,
context: &mut Context,
) -> Result<StatementList, ParseError> {
) -> ParseResult<StatementList> {
let mut strict = cursor.strict_mode();
match cursor.peek(0, context.interner_mut())? {
Some(tok) => {
Expand Down Expand Up @@ -432,7 +432,7 @@ fn name_in_lexically_declared_names(
parameter_list: &FormalParameterList,
names: &[Identifier],
position: Position,
) -> Result<(), ParseError> {
) -> ParseResult<()> {
for parameter in parameter_list.as_ref() {
for name in &parameter.names() {
if names.contains(name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ fn parse_callable_declaration<R: Read, C: CallableDeclaration>(
c: &C,
cursor: &mut Cursor<R>,
interner: &mut Interner,
) -> Result<(Identifier, FormalParameterList, StatementList), ParseError> {
) -> ParseResult<(Identifier, FormalParameterList, StatementList)> {
let next_token = cursor.peek(0, interner).or_abrupt()?;
let name = match next_token.kind() {
TokenKind::Punctuator(Punctuator::OpenParen) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ fn initializer_to_iterable_loop_initializer(
initializer: ForLoopInitializer,
position: Position,
strict: bool,
) -> Result<IterableLoopInitializer, ParseError> {
) -> ParseResult<IterableLoopInitializer> {
match initializer {
ForLoopInitializer::Expression(expr) => match expr {
ast::Expression::Identifier(ident)
Expand Down

0 comments on commit 5438162

Please sign in to comment.