Skip to content

Commit

Permalink
Finalize the grammar
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite committed Oct 2, 2024
1 parent 45ad222 commit bcc6663
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
16 changes: 16 additions & 0 deletions compiler/noirc_frontend/src/parser/parser/type_expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use noirc_errors::Span;
use super::Parser;

impl<'a> Parser<'a> {
/// TypeExpression= AddOrSubtractTypeExpression
pub(crate) fn parse_type_expression(
&mut self,
) -> Result<UnresolvedTypeExpression, ParserError> {
Expand All @@ -23,6 +24,8 @@ impl<'a> Parser<'a> {
}
}

/// AddOrSubtractTypeExpression
/// = MultiplyOrDivideOrModuloTypeExpression (('+' | '-') MultiplyOrDivideOrModuloTypeExpression)*
fn parse_add_or_subtract_type_expression(&mut self) -> Option<UnresolvedTypeExpression> {
let start_span = self.current_token_span;
let lhs = self.parse_multiply_or_divide_or_modulo_type_expression()?;
Expand Down Expand Up @@ -62,6 +65,8 @@ impl<'a> Parser<'a> {
lhs
}

/// MultiplyOrDivideOrModuloTypeExpression
/// = TermTypeExpression (('*' | '/' | '%') TermTypeExpression)*
fn parse_multiply_or_divide_or_modulo_type_expression(
&mut self,
) -> Option<UnresolvedTypeExpression> {
Expand Down Expand Up @@ -106,6 +111,9 @@ impl<'a> Parser<'a> {
lhs
}

/// TermTypeExpression
/// = '- TermTypeExpression
/// | AtomTypeExpression
fn parse_term_type_expression(&mut self) -> Option<UnresolvedTypeExpression> {
let start_span = self.current_token_span;
if self.eat(Token::Minus) {
Expand All @@ -131,6 +139,10 @@ impl<'a> Parser<'a> {
self.parse_atom_type_expression()
}

/// AtomTypeExpression
/// = ConstantTypeExpression
/// | VariableTypeExpression
/// | ParenthesizedTypeExpression
fn parse_atom_type_expression(&mut self) -> Option<UnresolvedTypeExpression> {
if let Some(type_expr) = self.parse_constant_type_expression() {
return Some(type_expr);
Expand All @@ -147,6 +159,7 @@ impl<'a> Parser<'a> {
None
}

/// ConstantTypeExpression = int
fn parse_constant_type_expression(&mut self) -> Option<UnresolvedTypeExpression> {
let Some(int) = self.eat_int() else {
return None;
Expand All @@ -169,11 +182,13 @@ impl<'a> Parser<'a> {
Some(UnresolvedTypeExpression::Constant(int, self.previous_token_span))
}

/// VariableTypeExpression = Path
fn parse_variable_type_expression(&mut self) -> Option<UnresolvedTypeExpression> {
let path = self.parse_path()?;
Some(UnresolvedTypeExpression::Variable(path))
}

/// ParenthesizedTypeExpression = '(' TypeExpression ')'
fn parse_parenthesized_type_expression(&mut self) -> Option<UnresolvedTypeExpression> {
// Make sure not to parse `()` as a parenthesized expression
if self.at(Token::LeftParen) && !self.next_is(Token::RightParen) {
Expand All @@ -194,6 +209,7 @@ impl<'a> Parser<'a> {
}
}

/// TypeOrTypeExpression = Type | TypeExpression
pub(crate) fn parse_type_or_type_expression(&mut self) -> Option<UnresolvedType> {
let typ = self.parse_add_or_subtract_type_or_type_expression()?;
let span = typ.span;
Expand Down
3 changes: 3 additions & 0 deletions compiler/noirc_frontend/src/parser/parser/use_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ use crate::{
use super::Parser;

impl<'a> Parser<'a> {
/// Use = 'use' PathKind PathNoTurbofish UseTree
///
/// UseTree = PathNoTurbofish ('::' '{' (UseTree ',')? '}')?
pub(super) fn parse_use_tree(&mut self) -> UseTree {
let start_span = self.current_token_span;

Expand Down

0 comments on commit bcc6663

Please sign in to comment.