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,