Repository: HigherOrderCO/Bend
File: src/term/parser/parser.rs (Rust)

Code before:
use super::lexer::{LexingError, Token};
use crate::term::{Adt, Book, LetPat, Name, Op, Rule, RulePat, Term};
use chumsky::{
  extra,
  input::{SpannedInput, Stream, ValueInput},
  prelude::{Input, Rich},
  primitive::{choice, just},
  recursive::recursive,
  select,
  span::SimpleSpan,
  IterParser, Parser,
};
use logos::{Logos, SpannedIter};
use std::{iter::Map, ops::Range};

/// <Book>    ::= <TopLevel>*
/// <TopLevel> ::= (<Def> | <Data>)
/// <Def>     ::= \n* <Rule> (\n+ <Rule>)* \n*
/// <Data>    ::= "data" \n* <Name> \n* "=" \n* (<Name> | "(" \n* <Name> (\n* <Name>)* \n* ")")+
/// <Rule>    ::= ("(" <Name> <Pattern>* ")" | <Name> <Pattern>*) \n* "=" \n* (<InlineNumOp> | <InlineApp>)
/// <Pattern> ::= "(" <Name> <Pattern>* ")" | <NameEra> | <Number>
/// <Term>    ::= <Var> | <GlobalVar> | <Number> | <Lam> | <GlobalLam> | <Dup> | <Tup> | <Let> | <NumOp> | <App>
/// <Lam>     ::= ("λ"|"@") \n* <NameEra> \n* <Term>
/// <GlobalLam> ::= ("λ"|"@") "$" <Name> \n* <Term>
/// <Dup>    ::= "dup" \n* <Tag>? \n* <NameEra> \n* <NameEra> \n* "=" \n* <Term> (\n+ | \n* ";") \n* <Term>
/// <Tup>    ::= "(" \n* <Term> \n* "," \n* <Term> \n* ")"
/// <Let>    ::= "let" \n* <LetPat> \n* "=" \n* <Term> (\n+ | \n* ";") \n* <Term>
/// <LetPat> ::= <Name> | "(" \n* <NameEra> \n* "," \n* <NameEra> \n* ")"
/// <NumOp>  ::= "(" \n* <numop_token> \n* <Term> \n* <Term> \n* ")"
/// <App>    ::= "(" \n* <Term> (\n* <Term>)* \n* ")"
/// <Var>    ::= <Name>
/// <GlobalVar> ::= "$" <Name>
/// <NameEra> ::= <Name> | "*"
/// <Name>   ::= <name_token> // [_a-zA-Z][_a-zA-Z0-9]{0..7}
/// <Number> ::= <number_token> // [0-9]+
/// <Tag>    ::= "#" \n* <Name>
pub fn parse_definition_book(code: &str) -> Result<Book, Vec<Rich<Token>>> {
  book().parse(token_stream(code)).into_result()
}

pub fn parse_term(code: &str) -> Result<Term, Vec<Rich<Token>>> {
  // TODO: Make a function that calls a parser. I couldn't figure out how to type it correctly.
  term().parse(token_stream(code)).into_result()

Review comment (suggestion):
Since you're fixing this, would you mind removing all the `\n*` here? They're not syntax relevant anymore

Code after:
use super::lexer::{LexingError, Token};
use crate::term::{Adt, Book, LetPat, Name, Op, Rule, RulePat, Term};
use chumsky::{
  extra,
  input::{SpannedInput, Stream, ValueInput},
  prelude::{Input, Rich},
  primitive::{choice, just},
  recursive::recursive,
  select,
  span::SimpleSpan,
  IterParser, Parser,
};
use logos::{Logos, SpannedIter};
use std::{iter::Map, ops::Range};

/// <Book>    ::= <TopLevel>*
/// <TopLevel> ::= (<Def> | <Data>)
/// <Def>     ::= <Rule> (<Rule>)*
/// <Data>    ::= "data" <Name> "=" (<Name> | "(" <Name> (<Name>)* ")")+
/// <Rule>    ::= ("(" <Name> <Pattern>* ")" | <Name> <Pattern>*) "=" (<InlineNumOp> | <InlineApp>)
/// <Pattern> ::= "(" <Name> <Pattern>* ")" | <NameEra> | <Number>
/// <Term>    ::= <Var> | <GlobalVar> | <Number> | <Lam> | <GlobalLam> | <Dup> | <Tup> | <Let> | <NumOp> | <App>
/// <Lam>     ::= ("λ"|"@") <NameEra> <Term>
/// <GlobalLam> ::= ("λ"|"@") "$" <Name> <Term>
/// <Dup>    ::= "dup" <Tag>? <NameEra> <NameEra> "=" <Term> ";" <Term>
/// <Tup>    ::= "(" <Term> "," <Term> ")"
/// <Let>    ::= "let" <LetPat> "=" <Term> ";" <Term>
/// <LetPat> ::= <Name> | "(" <NameEra> "," <NameEra> ")"
/// <NumOp>  ::= "(" <numop_token> <Term> <Term> ")"
/// <App>    ::= "(" <Term> (<Term>)* ")"
/// <Var>    ::= <Name>
/// <GlobalVar> ::= "$" <Name>
/// <NameEra> ::= <Name> | "*"
/// <Name>   ::= <name_token> // [_a-zA-Z][_a-zA-Z0-9]{0..7}
/// <Number> ::= <number_token> // [0-9]+
/// <Tag>    ::= "#" <Name>
pub fn parse_definition_book(code: &str) -> Result<Book, Vec<Rich<Token>>> {
  book().parse(token_stream(code)).into_result()
}

pub fn parse_term(code: &str) -> Result<Term, Vec<Rich<Token>>> {
  // TODO: Make a function that calls a parser. I couldn't figure out how to type it correctly.
  term().parse(token_stream(code)).into_result()