Repository: casey/just
File: src/parser.rs (Rust)

Code before:
/// The `presume_*` methods are used when the token stream has been inspected in
/// some other way, and thus encountering an unexpected token is a bug in Just,
/// and not a syntax error.
///
/// All methods starting with `parse_*` parse and return a language construct.
///
/// The parser tracks an expected set of tokens as it parses. This set contains
/// all tokens which would have been accepted at the current point in the parse.
/// Whenever the parser tests for a token that would be accepted, but does not
/// find it, it adds that token to the set. When the parser accepts a token, the
/// set is cleared. If the parser finds a token which is unexpected, the
/// contents of the set is printed in the resultant error message.
pub(crate) struct Parser<'tokens, 'src> {
  /// Source tokens
  tokens: &'tokens [Token<'src>],
  /// Index of the next un-parsed token
  next: usize,
  /// Current expected tokens
  expected: BTreeSet<TokenKind>,
  /// Current recursion depth
  depth: u8,
}

impl<'tokens, 'src> Parser<'tokens, 'src> {
  const RECURSION_LIMIT: u8 = u8::MAX;

  /// Parse `tokens` into an `Ast`
  pub(crate) fn parse(tokens: &'tokens [Token<'src>]) -> CompileResult<'src, Ast<'src>> {
    Self::new(tokens).parse_ast()
  }

  /// Construct a new Parser from a token stream
  fn new(tokens: &'tokens [Token<'src>]) -> Parser<'tokens, 'src> {
    Parser {
      next: 0,
      expected: BTreeSet::new(),
      tokens,
      depth: 0,
    }
  }

  fn error(&self, kind: CompileErrorKind<'src>) -> CompileResult<'src, CompileError<'src>> {
    Ok(self.next()?.error(kind))
  }

  /// Construct an unexpected token error with the token returned by
  /// `Parser::next`
  fn unexpected_token(&self) -> CompileResult<'src, CompileError<'src>> {
    self.error(CompileErrorKind::UnexpectedToken {
      expected: self
        .expected

Review comment (suggestion):
I think this is probably clearer just inlining.

```suggestion
```

Code after:
/// The `presume_*` methods are used when the token stream has been inspected in
/// some other way, and thus encountering an unexpected token is a bug in Just,
/// and not a syntax error.
///
/// All methods starting with `parse_*` parse and return a language construct.
///
/// The parser tracks an expected set of tokens as it parses. This set contains
/// all tokens which would have been accepted at the current point in the parse.
/// Whenever the parser tests for a token that would be accepted, but does not
/// find it, it adds that token to the set. When the parser accepts a token, the
/// set is cleared. If the parser finds a token which is unexpected, the
/// contents of the set is printed in the resultant error message.
pub(crate) struct Parser<'tokens, 'src> {
  /// Source tokens
  tokens: &'tokens [Token<'src>],
  /// Index of the next un-parsed token
  next: usize,
  /// Current expected tokens
  expected: BTreeSet<TokenKind>,
  /// Current recursion depth
  depth: u8,
}

impl<'tokens, 'src> Parser<'tokens, 'src> {
  /// Parse `tokens` into an `Ast`
  pub(crate) fn parse(tokens: &'tokens [Token<'src>]) -> CompileResult<'src, Ast<'src>> {
    Self::new(tokens).parse_ast()
  }

  /// Construct a new Parser from a token stream
  fn new(tokens: &'tokens [Token<'src>]) -> Parser<'tokens, 'src> {
    Parser {
      next: 0,
      expected: BTreeSet::new(),
      tokens,
      depth: 0,
    }
  }

  fn error(&self, kind: CompileErrorKind<'src>) -> CompileResult<'src, CompileError<'src>> {
    Ok(self.next()?.error(kind))
  }

  /// Construct an unexpected token error with the token returned by
  /// `Parser::next`
  fn unexpected_token(&self) -> CompileResult<'src, CompileError<'src>> {
    self.error(CompileErrorKind::UnexpectedToken {
      expected: self
        .expected
        .iter()
        .copied()