difftreelog
refactor deduplicate operator definitions
in: master
9 files changed
crates/jrsonnet-rowan-parser/jsonnet.ungramdiffbeforeafterboth--- a/crates/jrsonnet-rowan-parser/jsonnet.ungram
+++ b/crates/jrsonnet-rowan-parser/jsonnet.ungram
@@ -136,6 +136,7 @@
| '<<' | '>>'
| '+' | '-'
| '*' | '/' | '%'
+| 'META_OBJECT_APPLY!'
| 'ERROR_NO_OPERATOR!'
UnaryOperator =
crates/jrsonnet-rowan-parser/src/binary.rsdiffbeforeafterboth--- a/crates/jrsonnet-rowan-parser/src/binary.rs
+++ /dev/null
@@ -1,48 +0,0 @@
-#[derive(Clone, Copy, Debug, PartialEq, Eq)]
-pub enum BinaryOperator {
- Mul,
- Div,
- Mod,
- Plus,
- Minus,
- ShiftLeft,
- ShiftRight,
- LessThan,
- GreaterThan,
- LessThanOrEqual,
- GreaterThanOrEqual,
- Equal,
- NotEqual,
- BitAnd,
- BitXor,
- BitOr,
- And,
- Or,
- In,
- ObjectApply,
- #[allow(dead_code)]
- Invalid,
-}
-
-impl BinaryOperator {
- pub fn binding_power(&self) -> (u8, u8) {
- match self {
- Self::ObjectApply => (22, 23),
- Self::Mul | Self::Div | Self::Mod => (20, 21),
- Self::Plus | Self::Minus => (18, 19),
- Self::ShiftLeft | Self::ShiftRight => (16, 17),
- Self::LessThan
- | Self::GreaterThan
- | Self::LessThanOrEqual
- | Self::GreaterThanOrEqual
- | Self::In => (14, 15),
- Self::Equal | Self::NotEqual => (12, 13),
- Self::BitAnd => (10, 11),
- Self::BitXor => (8, 9),
- Self::BitOr => (6, 7),
- Self::And => (4, 5),
- Self::Or => (2, 3),
- Self::Invalid => (0, 1),
- }
- }
-}
crates/jrsonnet-rowan-parser/src/generated/nodes.rsdiffbeforeafterboth--- a/crates/jrsonnet-rowan-parser/src/generated/nodes.rs
+++ b/crates/jrsonnet-rowan-parser/src/generated/nodes.rs
@@ -1005,6 +1005,7 @@
Mul,
Div,
Modulo,
+ MetaObjectApply,
ErrorNoOperator,
}
@@ -2508,101 +2509,51 @@
}
impl AstToken for BinaryOperator {
fn can_cast(kind: SyntaxKind) -> bool {
+ BinaryOperatorKind::can_cast(kind)
+ }
+ fn cast(syntax: SyntaxToken) -> Option<Self> {
+ let kind = BinaryOperatorKind::cast(syntax.kind())?;
+ Some(BinaryOperator { syntax, kind })
+ }
+ fn syntax(&self) -> &SyntaxToken {
+ &self.syntax
+ }
+}
+impl BinaryOperatorKind {
+ fn can_cast(kind: SyntaxKind) -> bool {
match kind {
OR | AND | BIT_OR | BIT_XOR | BIT_AND | EQ | NE | LT | GT | LE | GE | IN_KW | LHS
- | RHS | PLUS | MINUS | MUL | DIV | MODULO | ERROR_NO_OPERATOR => true,
+ | RHS | PLUS | MINUS | MUL | DIV | MODULO | META_OBJECT_APPLY | ERROR_NO_OPERATOR => true,
_ => false,
}
}
- fn cast(syntax: SyntaxToken) -> Option<Self> {
- let res = match syntax.kind() {
- OR => BinaryOperator {
- syntax,
- kind: BinaryOperatorKind::Or,
- },
- AND => BinaryOperator {
- syntax,
- kind: BinaryOperatorKind::And,
- },
- BIT_OR => BinaryOperator {
- syntax,
- kind: BinaryOperatorKind::BitOr,
- },
- BIT_XOR => BinaryOperator {
- syntax,
- kind: BinaryOperatorKind::BitXor,
- },
- BIT_AND => BinaryOperator {
- syntax,
- kind: BinaryOperatorKind::BitAnd,
- },
- EQ => BinaryOperator {
- syntax,
- kind: BinaryOperatorKind::Eq,
- },
- NE => BinaryOperator {
- syntax,
- kind: BinaryOperatorKind::Ne,
- },
- LT => BinaryOperator {
- syntax,
- kind: BinaryOperatorKind::Lt,
- },
- GT => BinaryOperator {
- syntax,
- kind: BinaryOperatorKind::Gt,
- },
- LE => BinaryOperator {
- syntax,
- kind: BinaryOperatorKind::Le,
- },
- GE => BinaryOperator {
- syntax,
- kind: BinaryOperatorKind::Ge,
- },
- IN_KW => BinaryOperator {
- syntax,
- kind: BinaryOperatorKind::InKw,
- },
- LHS => BinaryOperator {
- syntax,
- kind: BinaryOperatorKind::Lhs,
- },
- RHS => BinaryOperator {
- syntax,
- kind: BinaryOperatorKind::Rhs,
- },
- PLUS => BinaryOperator {
- syntax,
- kind: BinaryOperatorKind::Plus,
- },
- MINUS => BinaryOperator {
- syntax,
- kind: BinaryOperatorKind::Minus,
- },
- MUL => BinaryOperator {
- syntax,
- kind: BinaryOperatorKind::Mul,
- },
- DIV => BinaryOperator {
- syntax,
- kind: BinaryOperatorKind::Div,
- },
- MODULO => BinaryOperator {
- syntax,
- kind: BinaryOperatorKind::Modulo,
- },
- ERROR_NO_OPERATOR => BinaryOperator {
- syntax,
- kind: BinaryOperatorKind::ErrorNoOperator,
- },
+ pub fn cast(kind: SyntaxKind) -> Option<Self> {
+ let res = match kind {
+ OR => Self::Or,
+ AND => Self::And,
+ BIT_OR => Self::BitOr,
+ BIT_XOR => Self::BitXor,
+ BIT_AND => Self::BitAnd,
+ EQ => Self::Eq,
+ NE => Self::Ne,
+ LT => Self::Lt,
+ GT => Self::Gt,
+ LE => Self::Le,
+ GE => Self::Ge,
+ IN_KW => Self::InKw,
+ LHS => Self::Lhs,
+ RHS => Self::Rhs,
+ PLUS => Self::Plus,
+ MINUS => Self::Minus,
+ MUL => Self::Mul,
+ DIV => Self::Div,
+ MODULO => Self::Modulo,
+ META_OBJECT_APPLY => Self::MetaObjectApply,
+ ERROR_NO_OPERATOR => Self::ErrorNoOperator,
_ => return None,
};
Some(res)
}
- fn syntax(&self) -> &SyntaxToken {
- &self.syntax
- }
}
impl BinaryOperator {
pub fn kind(&self) -> BinaryOperatorKind {
@@ -2616,31 +2567,31 @@
}
impl AstToken for UnaryOperator {
fn can_cast(kind: SyntaxKind) -> bool {
+ UnaryOperatorKind::can_cast(kind)
+ }
+ fn cast(syntax: SyntaxToken) -> Option<Self> {
+ let kind = UnaryOperatorKind::cast(syntax.kind())?;
+ Some(UnaryOperator { syntax, kind })
+ }
+ fn syntax(&self) -> &SyntaxToken {
+ &self.syntax
+ }
+}
+impl UnaryOperatorKind {
+ fn can_cast(kind: SyntaxKind) -> bool {
match kind {
MINUS | NOT | BIT_NOT => true,
_ => false,
}
}
- fn cast(syntax: SyntaxToken) -> Option<Self> {
- let res = match syntax.kind() {
- MINUS => UnaryOperator {
- syntax,
- kind: UnaryOperatorKind::Minus,
- },
- NOT => UnaryOperator {
- syntax,
- kind: UnaryOperatorKind::Not,
- },
- BIT_NOT => UnaryOperator {
- syntax,
- kind: UnaryOperatorKind::BitNot,
- },
+ pub fn cast(kind: SyntaxKind) -> Option<Self> {
+ let res = match kind {
+ MINUS => Self::Minus,
+ NOT => Self::Not,
+ BIT_NOT => Self::BitNot,
_ => return None,
};
Some(res)
- }
- fn syntax(&self) -> &SyntaxToken {
- &self.syntax
}
}
impl UnaryOperator {
@@ -2655,44 +2606,35 @@
}
impl AstToken for Literal {
fn can_cast(kind: SyntaxKind) -> bool {
+ LiteralKind::can_cast(kind)
+ }
+ fn cast(syntax: SyntaxToken) -> Option<Self> {
+ let kind = LiteralKind::cast(syntax.kind())?;
+ Some(Literal { syntax, kind })
+ }
+ fn syntax(&self) -> &SyntaxToken {
+ &self.syntax
+ }
+}
+impl LiteralKind {
+ fn can_cast(kind: SyntaxKind) -> bool {
match kind {
NULL_KW | TRUE_KW | FALSE_KW | SELF_KW | DOLLAR | SUPER_KW => true,
_ => false,
}
}
- fn cast(syntax: SyntaxToken) -> Option<Self> {
- let res = match syntax.kind() {
- NULL_KW => Literal {
- syntax,
- kind: LiteralKind::NullKw,
- },
- TRUE_KW => Literal {
- syntax,
- kind: LiteralKind::TrueKw,
- },
- FALSE_KW => Literal {
- syntax,
- kind: LiteralKind::FalseKw,
- },
- SELF_KW => Literal {
- syntax,
- kind: LiteralKind::SelfKw,
- },
- DOLLAR => Literal {
- syntax,
- kind: LiteralKind::Dollar,
- },
- SUPER_KW => Literal {
- syntax,
- kind: LiteralKind::SuperKw,
- },
+ pub fn cast(kind: SyntaxKind) -> Option<Self> {
+ let res = match kind {
+ NULL_KW => Self::NullKw,
+ TRUE_KW => Self::TrueKw,
+ FALSE_KW => Self::FalseKw,
+ SELF_KW => Self::SelfKw,
+ DOLLAR => Self::Dollar,
+ SUPER_KW => Self::SuperKw,
_ => return None,
};
Some(res)
}
- fn syntax(&self) -> &SyntaxToken {
- &self.syntax
- }
}
impl Literal {
pub fn kind(&self) -> LiteralKind {
@@ -2706,6 +2648,18 @@
}
impl AstToken for Text {
fn can_cast(kind: SyntaxKind) -> bool {
+ TextKind::can_cast(kind)
+ }
+ fn cast(syntax: SyntaxToken) -> Option<Self> {
+ let kind = TextKind::cast(syntax.kind())?;
+ Some(Text { syntax, kind })
+ }
+ fn syntax(&self) -> &SyntaxToken {
+ &self.syntax
+ }
+}
+impl TextKind {
+ fn can_cast(kind: SyntaxKind) -> bool {
match kind {
STRING_DOUBLE
| ERROR_STRING_DOUBLE_UNTERMINATED
@@ -2724,71 +2678,30 @@
_ => false,
}
}
- fn cast(syntax: SyntaxToken) -> Option<Self> {
- let res = match syntax.kind() {
- STRING_DOUBLE => Text {
- syntax,
- kind: TextKind::StringDouble,
- },
- ERROR_STRING_DOUBLE_UNTERMINATED => Text {
- syntax,
- kind: TextKind::ErrorStringDoubleUnterminated,
- },
- STRING_SINGLE => Text {
- syntax,
- kind: TextKind::StringSingle,
- },
- ERROR_STRING_SINGLE_UNTERMINATED => Text {
- syntax,
- kind: TextKind::ErrorStringSingleUnterminated,
- },
- STRING_DOUBLE_VERBATIM => Text {
- syntax,
- kind: TextKind::StringDoubleVerbatim,
- },
- ERROR_STRING_DOUBLE_VERBATIM_UNTERMINATED => Text {
- syntax,
- kind: TextKind::ErrorStringDoubleVerbatimUnterminated,
- },
- STRING_SINGLE_VERBATIM => Text {
- syntax,
- kind: TextKind::StringSingleVerbatim,
- },
- ERROR_STRING_SINGLE_VERBATIM_UNTERMINATED => Text {
- syntax,
- kind: TextKind::ErrorStringSingleVerbatimUnterminated,
- },
- ERROR_STRING_VERBATIM_MISSING_QUOTES => Text {
- syntax,
- kind: TextKind::ErrorStringVerbatimMissingQuotes,
- },
- STRING_BLOCK => Text {
- syntax,
- kind: TextKind::StringBlock,
- },
- ERROR_STRING_BLOCK_UNEXPECTED_END => Text {
- syntax,
- kind: TextKind::ErrorStringBlockUnexpectedEnd,
- },
- ERROR_STRING_BLOCK_MISSING_NEW_LINE => Text {
- syntax,
- kind: TextKind::ErrorStringBlockMissingNewLine,
- },
- ERROR_STRING_BLOCK_MISSING_TERMINATION => Text {
- syntax,
- kind: TextKind::ErrorStringBlockMissingTermination,
- },
- ERROR_STRING_BLOCK_MISSING_INDENT => Text {
- syntax,
- kind: TextKind::ErrorStringBlockMissingIndent,
- },
+ pub fn cast(kind: SyntaxKind) -> Option<Self> {
+ let res = match kind {
+ STRING_DOUBLE => Self::StringDouble,
+ ERROR_STRING_DOUBLE_UNTERMINATED => Self::ErrorStringDoubleUnterminated,
+ STRING_SINGLE => Self::StringSingle,
+ ERROR_STRING_SINGLE_UNTERMINATED => Self::ErrorStringSingleUnterminated,
+ STRING_DOUBLE_VERBATIM => Self::StringDoubleVerbatim,
+ ERROR_STRING_DOUBLE_VERBATIM_UNTERMINATED => {
+ Self::ErrorStringDoubleVerbatimUnterminated
+ }
+ STRING_SINGLE_VERBATIM => Self::StringSingleVerbatim,
+ ERROR_STRING_SINGLE_VERBATIM_UNTERMINATED => {
+ Self::ErrorStringSingleVerbatimUnterminated
+ }
+ ERROR_STRING_VERBATIM_MISSING_QUOTES => Self::ErrorStringVerbatimMissingQuotes,
+ STRING_BLOCK => Self::StringBlock,
+ ERROR_STRING_BLOCK_UNEXPECTED_END => Self::ErrorStringBlockUnexpectedEnd,
+ ERROR_STRING_BLOCK_MISSING_NEW_LINE => Self::ErrorStringBlockMissingNewLine,
+ ERROR_STRING_BLOCK_MISSING_TERMINATION => Self::ErrorStringBlockMissingTermination,
+ ERROR_STRING_BLOCK_MISSING_INDENT => Self::ErrorStringBlockMissingIndent,
_ => return None,
};
Some(res)
}
- fn syntax(&self) -> &SyntaxToken {
- &self.syntax
- }
}
impl Text {
pub fn kind(&self) -> TextKind {
@@ -2802,6 +2715,18 @@
}
impl AstToken for Number {
fn can_cast(kind: SyntaxKind) -> bool {
+ NumberKind::can_cast(kind)
+ }
+ fn cast(syntax: SyntaxToken) -> Option<Self> {
+ let kind = NumberKind::cast(syntax.kind())?;
+ Some(Number { syntax, kind })
+ }
+ fn syntax(&self) -> &SyntaxToken {
+ &self.syntax
+ }
+}
+impl NumberKind {
+ fn can_cast(kind: SyntaxKind) -> bool {
match kind {
FLOAT
| ERROR_FLOAT_JUNK_AFTER_POINT
@@ -2810,31 +2735,16 @@
_ => false,
}
}
- fn cast(syntax: SyntaxToken) -> Option<Self> {
- let res = match syntax.kind() {
- FLOAT => Number {
- syntax,
- kind: NumberKind::Float,
- },
- ERROR_FLOAT_JUNK_AFTER_POINT => Number {
- syntax,
- kind: NumberKind::ErrorFloatJunkAfterPoint,
- },
- ERROR_FLOAT_JUNK_AFTER_EXPONENT => Number {
- syntax,
- kind: NumberKind::ErrorFloatJunkAfterExponent,
- },
- ERROR_FLOAT_JUNK_AFTER_EXPONENT_SIGN => Number {
- syntax,
- kind: NumberKind::ErrorFloatJunkAfterExponentSign,
- },
+ pub fn cast(kind: SyntaxKind) -> Option<Self> {
+ let res = match kind {
+ FLOAT => Self::Float,
+ ERROR_FLOAT_JUNK_AFTER_POINT => Self::ErrorFloatJunkAfterPoint,
+ ERROR_FLOAT_JUNK_AFTER_EXPONENT => Self::ErrorFloatJunkAfterExponent,
+ ERROR_FLOAT_JUNK_AFTER_EXPONENT_SIGN => Self::ErrorFloatJunkAfterExponentSign,
_ => return None,
};
Some(res)
}
- fn syntax(&self) -> &SyntaxToken {
- &self.syntax
- }
}
impl Number {
pub fn kind(&self) -> NumberKind {
@@ -2848,32 +2758,32 @@
}
impl AstToken for ImportKind {
fn can_cast(kind: SyntaxKind) -> bool {
+ ImportKindKind::can_cast(kind)
+ }
+ fn cast(syntax: SyntaxToken) -> Option<Self> {
+ let kind = ImportKindKind::cast(syntax.kind())?;
+ Some(ImportKind { syntax, kind })
+ }
+ fn syntax(&self) -> &SyntaxToken {
+ &self.syntax
+ }
+}
+impl ImportKindKind {
+ fn can_cast(kind: SyntaxKind) -> bool {
match kind {
IMPORTSTR_KW | IMPORTBIN_KW | IMPORT_KW => true,
_ => false,
}
}
- fn cast(syntax: SyntaxToken) -> Option<Self> {
- let res = match syntax.kind() {
- IMPORTSTR_KW => ImportKind {
- syntax,
- kind: ImportKindKind::ImportstrKw,
- },
- IMPORTBIN_KW => ImportKind {
- syntax,
- kind: ImportKindKind::ImportbinKw,
- },
- IMPORT_KW => ImportKind {
- syntax,
- kind: ImportKindKind::ImportKw,
- },
+ pub fn cast(kind: SyntaxKind) -> Option<Self> {
+ let res = match kind {
+ IMPORTSTR_KW => Self::ImportstrKw,
+ IMPORTBIN_KW => Self::ImportbinKw,
+ IMPORT_KW => Self::ImportKw,
_ => return None,
};
Some(res)
}
- fn syntax(&self) -> &SyntaxToken {
- &self.syntax
- }
}
impl ImportKind {
pub fn kind(&self) -> ImportKindKind {
@@ -2887,31 +2797,31 @@
}
impl AstToken for Visibility {
fn can_cast(kind: SyntaxKind) -> bool {
+ VisibilityKind::can_cast(kind)
+ }
+ fn cast(syntax: SyntaxToken) -> Option<Self> {
+ let kind = VisibilityKind::cast(syntax.kind())?;
+ Some(Visibility { syntax, kind })
+ }
+ fn syntax(&self) -> &SyntaxToken {
+ &self.syntax
+ }
+}
+impl VisibilityKind {
+ fn can_cast(kind: SyntaxKind) -> bool {
match kind {
COLONCOLONCOLON | COLONCOLON | COLON => true,
_ => false,
}
}
- fn cast(syntax: SyntaxToken) -> Option<Self> {
- let res = match syntax.kind() {
- COLONCOLONCOLON => Visibility {
- syntax,
- kind: VisibilityKind::Coloncoloncolon,
- },
- COLONCOLON => Visibility {
- syntax,
- kind: VisibilityKind::Coloncolon,
- },
- COLON => Visibility {
- syntax,
- kind: VisibilityKind::Colon,
- },
+ pub fn cast(kind: SyntaxKind) -> Option<Self> {
+ let res = match kind {
+ COLONCOLONCOLON => Self::Coloncoloncolon,
+ COLONCOLON => Self::Coloncolon,
+ COLON => Self::Colon,
_ => return None,
};
Some(res)
- }
- fn syntax(&self) -> &SyntaxToken {
- &self.syntax
}
}
impl Visibility {
@@ -2926,6 +2836,18 @@
}
impl AstToken for Trivia {
fn can_cast(kind: SyntaxKind) -> bool {
+ TriviaKind::can_cast(kind)
+ }
+ fn cast(syntax: SyntaxToken) -> Option<Self> {
+ let kind = TriviaKind::cast(syntax.kind())?;
+ Some(Trivia { syntax, kind })
+ }
+ fn syntax(&self) -> &SyntaxToken {
+ &self.syntax
+ }
+}
+impl TriviaKind {
+ fn can_cast(kind: SyntaxKind) -> bool {
match kind {
WHITESPACE
| MULTI_LINE_COMMENT
@@ -2936,38 +2858,17 @@
_ => false,
}
}
- fn cast(syntax: SyntaxToken) -> Option<Self> {
- let res = match syntax.kind() {
- WHITESPACE => Trivia {
- syntax,
- kind: TriviaKind::Whitespace,
- },
- MULTI_LINE_COMMENT => Trivia {
- syntax,
- kind: TriviaKind::MultiLineComment,
- },
- ERROR_COMMENT_TOO_SHORT => Trivia {
- syntax,
- kind: TriviaKind::ErrorCommentTooShort,
- },
- ERROR_COMMENT_UNTERMINATED => Trivia {
- syntax,
- kind: TriviaKind::ErrorCommentUnterminated,
- },
- SINGLE_LINE_HASH_COMMENT => Trivia {
- syntax,
- kind: TriviaKind::SingleLineHashComment,
- },
- SINGLE_LINE_SLASH_COMMENT => Trivia {
- syntax,
- kind: TriviaKind::SingleLineSlashComment,
- },
+ pub fn cast(kind: SyntaxKind) -> Option<Self> {
+ let res = match kind {
+ WHITESPACE => Self::Whitespace,
+ MULTI_LINE_COMMENT => Self::MultiLineComment,
+ ERROR_COMMENT_TOO_SHORT => Self::ErrorCommentTooShort,
+ ERROR_COMMENT_UNTERMINATED => Self::ErrorCommentUnterminated,
+ SINGLE_LINE_HASH_COMMENT => Self::SingleLineHashComment,
+ SINGLE_LINE_SLASH_COMMENT => Self::SingleLineSlashComment,
_ => return None,
};
Some(res)
- }
- fn syntax(&self) -> &SyntaxToken {
- &self.syntax
}
}
impl Trivia {
crates/jrsonnet-rowan-parser/src/generated/syntax_kinds.rsdiffbeforeafterboth--- a/crates/jrsonnet-rowan-parser/src/generated/syntax_kinds.rs
+++ b/crates/jrsonnet-rowan-parser/src/generated/syntax_kinds.rs
@@ -163,6 +163,7 @@
ERROR_KW,
#[token("in")]
IN_KW,
+ META_OBJECT_APPLY,
ERROR_NO_OPERATOR,
#[token("null")]
NULL_KW,
crates/jrsonnet-rowan-parser/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-rowan-parser/src/lib.rs
+++ b/crates/jrsonnet-rowan-parser/src/lib.rs
@@ -1,17 +1,16 @@
#![deny(unused_must_use)]
mod ast;
-mod binary;
mod event;
mod generated;
mod language;
mod lex;
mod marker;
mod parser;
+mod precedence;
mod string_block;
mod tests;
mod token_set;
-mod unary;
pub use ast::{AstChildren, AstNode, AstToken};
use event::Sink;
crates/jrsonnet-rowan-parser/src/parser.rsdiffbeforeafterboth4use rowan::{GreenNode, TextRange, TextSize};4use rowan::{GreenNode, TextRange, TextSize};556use crate::{6use crate::{7 binary::BinaryOperator,8 event::Event,7 event::Event,9 lex::Lexeme,8 lex::Lexeme,10 marker::{AsRange, CompletedMarker, Marker, Ranger},9 marker::{AsRange, CompletedMarker, Marker, Ranger},11 nodes::{Literal, Number, Text, Trivia},10 nodes::{BinaryOperatorKind, Literal, Number, Text, Trivia, UnaryOperatorKind},12 token_set::SyntaxKindSet,11 token_set::SyntaxKindSet,13 unary::UnaryOperator,14 AstToken, SyntaxKind,12 AstToken, SyntaxKind,15 SyntaxKind::*,13 SyntaxKind::*,16 SyntaxNode, T, TS,14 SyntaxNode, T, TS,398 Named,396 Named,399 Unnamed,397 Unnamed,400}398}401macro_rules! at_match {402 ($p:ident {403 $($r:expr => $e:expr,)*404 _ => $else:expr $(,)?405 }) => {{406 $(407 if $p.at($r) {$e} else408 )* {409 $else410 }411 }}412}413399414fn expr(p: &mut Parser) -> Option<CompletedMarker> {400fn expr(p: &mut Parser) -> Option<CompletedMarker> {415 expr_binding_power(p, 0)401 expr_binding_power(p, 0)416}402}417fn expr_binding_power(p: &mut Parser, minimum_binding_power: u8) -> Option<CompletedMarker> {403fn expr_binding_power(p: &mut Parser, minimum_binding_power: u8) -> Option<CompletedMarker> {418 let mut lhs = lhs(p)?;404 let mut lhs = lhs(p)?;419405420 loop {406 while let Some(op) = BinaryOperatorKind::cast(p.current())421 let op = at_match!(p {422 T![*] => BinaryOperator::Mul,407 .or_else(|| p.at(T!['{']).then(|| BinaryOperatorKind::MetaObjectApply))423 T![/] => BinaryOperator::Div,424 T![%] => BinaryOperator::Mod,425 T![+] => BinaryOperator::Plus,426 T![-] => BinaryOperator::Minus,427 T![<<] => BinaryOperator::ShiftLeft,428 T![>>] => BinaryOperator::ShiftRight,429 T![<] => BinaryOperator::LessThan,430 T![>] => BinaryOperator::GreaterThan,431 T![<=] => BinaryOperator::LessThanOrEqual,432 T![>=] => BinaryOperator::GreaterThanOrEqual,433 T![==] => BinaryOperator::Equal,434 T![!=] => BinaryOperator::NotEqual,435 T![&] => BinaryOperator::BitAnd,436 T![^] => BinaryOperator::BitXor,437 T![|] => BinaryOperator::BitOr,438 T![&&] => BinaryOperator::And,439 T![||] => BinaryOperator::Or,440 T![in] => BinaryOperator::In,441 T!['{'] => BinaryOperator::ObjectApply,442 _ => break,408 {443 });444 let (left_binding_power, right_binding_power) = op.binding_power();409 let (left_binding_power, right_binding_power) = op.binding_power();445 if left_binding_power < minimum_binding_power {410 if left_binding_power < minimum_binding_power {446 break;411 break;447 }412 }448413449 // Object apply is not a real operator, we dont have something to bump414 // Object apply is not a real operator, we dont have something to bump450 if op != BinaryOperator::ObjectApply {415 if op != BinaryOperatorKind::MetaObjectApply {451 p.bump();416 p.bump();452 }417 }453418454 let m = lhs.wrap(p, LHS_EXPR).precede(p);419 let m = lhs.wrap(p, LHS_EXPR).precede(p);455 let parsed_rhs = expr_binding_power(p, right_binding_power).is_some();420 let parsed_rhs = expr_binding_power(p, right_binding_power).is_some();456 lhs = m.complete(421 lhs = m.complete(457 p,422 p,458 if op == BinaryOperator::ObjectApply {423 if op == BinaryOperatorKind::MetaObjectApply {459 EXPR_OBJ_EXTEND424 EXPR_OBJ_EXTEND460 } else {425 } else {461 EXPR_BINARY426 EXPR_BINARY998 p.bump();963 p.bump();999 text(p);964 text(p);1000 m.complete(p, EXPR_IMPORT)965 m.complete(p, EXPR_IMPORT)1001 } else if p.at(T![-]) || p.at(T![!]) || p.at(T![~]) {966 } else if let Some(op) = UnaryOperatorKind::cast(p.current()) {1002 let op = match p.current() {1003 T![-] => UnaryOperator::Minus,1004 T![!] => UnaryOperator::Not,1005 T![~] => UnaryOperator::BitNegate,1006 _ => unreachable!(),1007 };1008 let ((), right_binding_power) = op.binding_power();967 let ((), right_binding_power) = op.binding_power();10099681010 let m = p.start();969 let m = p.start();crates/jrsonnet-rowan-parser/src/precedence.rsdiffbeforeafterboth--- /dev/null
+++ b/crates/jrsonnet-rowan-parser/src/precedence.rs
@@ -0,0 +1,30 @@
+use crate::nodes::{BinaryOperatorKind, UnaryOperatorKind};
+
+impl BinaryOperatorKind {
+ pub fn binding_power(&self) -> (u8, u8) {
+ match self {
+ Self::MetaObjectApply => (22, 23),
+ Self::Mul | Self::Div | Self::Modulo => (20, 21),
+ Self::Plus | Self::Minus => (18, 19),
+ Self::Lhs | Self::Rhs => (16, 17),
+ Self::Lt | Self::Gt | Self::Le | Self::Ge | Self::InKw => (14, 15),
+ Self::Eq | Self::Ne => (12, 13),
+ Self::BitAnd => (10, 11),
+ Self::BitXor => (8, 9),
+ Self::BitOr => (6, 7),
+ Self::And => (4, 5),
+ Self::Or => (2, 3),
+ Self::ErrorNoOperator => (0, 1),
+ }
+ }
+}
+
+impl UnaryOperatorKind {
+ pub fn binding_power(&self) -> ((), u8) {
+ match self {
+ Self::Minus => ((), 20),
+ Self::Not => ((), 20),
+ Self::BitNot => ((), 20),
+ }
+ }
+}
crates/jrsonnet-rowan-parser/src/unary.rsdiffbeforeafterboth--- a/crates/jrsonnet-rowan-parser/src/unary.rs
+++ /dev/null
@@ -1,15 +0,0 @@
-#[derive(Clone, Copy, Debug, PartialEq, Eq)]
-pub enum UnaryOperator {
- Minus,
- Not,
- BitNegate,
-}
-impl UnaryOperator {
- pub fn binding_power(&self) -> ((), u8) {
- match self {
- UnaryOperator::Minus => ((), 20),
- UnaryOperator::Not => ((), 20),
- UnaryOperator::BitNegate => ((), 20),
- }
- }
-}
xtask/src/sourcegen/mod.rsdiffbeforeafterboth--- a/xtask/src/sourcegen/mod.rs
+++ b/xtask/src/sourcegen/mod.rs
@@ -353,22 +353,30 @@
let ast_node = quote! {
impl AstToken for #name {
fn can_cast(kind: SyntaxKind) -> bool {
+ #kind_name::can_cast(kind)
+ }
+ fn cast(syntax: SyntaxToken) -> Option<Self> {
+ let kind = #kind_name::cast(syntax.kind())?;
+ Some(#name { syntax, kind })
+ }
+ fn syntax(&self) -> &SyntaxToken {
+ &self.syntax
+ }
+ }
+
+ impl #kind_name {
+ fn can_cast(kind: SyntaxKind) -> bool {
match kind {
#(#kinds)|* => true,
_ => false,
}
}
- fn cast(syntax: SyntaxToken) -> Option<Self> {
- let res = match syntax.kind() {
- #(
- #kinds => #name { syntax, kind: #kind_name::#variants },
- )*
+ pub fn cast(kind: SyntaxKind) -> Option<Self> {
+ let res = match kind {
+ #(#kinds => Self::#variants,)*
_ => return None,
};
Some(res)
- }
- fn syntax(&self) -> &SyntaxToken {
- &self.syntax
}
}
};