From a9ee8eb66c089dbc91254499e1581a0e04c5b729 Mon Sep 17 00:00:00 2001 From: Kai-Philipp Nosper Date: Fri, 28 Jan 2022 14:00:51 +0100 Subject: [PATCH] Update grammar definition --- src/parser.rs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index 5bd88ef..30c0cea 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -47,10 +47,14 @@ pub enum Ast { /* ## Grammar ### Expressions -`expr_primary = LITERAL` \ -`expr_mul = expr_primary (("*" | "/") expr_primary)*` \ -`expr_add = expr_mul (("+" | "-") expr_mul)*` \ -`expr = expr_add` \ +expr_primary = LITERAL +expr_mul = expr_primary (("*" | "/" | "%") expr_primary)* +expr_add = expr_mul (("+" | "-") expr_mul)* +expr_shift = expr_add ((">>" | "<<") expr_add)* +expr_band = expr_shift ("&" expr_shift)* +expr_bxor = expr_band ("^") expr_band)* +expr_bor = expr_bxor ("|" expr_bxor)* +expr = expr_bor */ struct Parser> { @@ -129,6 +133,10 @@ pub fn parse, A: IntoIterator>(tokens: A impl BinOpType { /// Get the precedence for a binary operator. Higher value means the OP is stronger binding. /// For example Multiplication is stronger than addition, so Mul has higher precedence than Add. + /// + /// The operator precedences are derived from the C language operator precedences. While not all + /// C operators are included or the exact same, the precedence oder is the same. + /// See: https://en.cppreference.com/w/c/language/operator_precedence fn precedence(&self) -> u8 { match self { BinOpType::BOr => 0,