diff options
| author | Laurenz <laurmaedje@gmail.com> | 2022-03-18 23:36:18 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2022-03-18 23:43:58 +0100 |
| commit | beca01c826ee51c9ee6d5eadd7e5ef10f7fb9f58 (patch) | |
| tree | e0ebb40b8775bba3b4be7bc47dceda3d349e2ac0 /src/syntax | |
| parent | 77d153d315a2a5909840ebcd47491e4cef14428b (diff) | |
Methods
Diffstat (limited to 'src/syntax')
| -rw-r--r-- | src/syntax/ast.rs | 75 | ||||
| -rw-r--r-- | src/syntax/highlight.rs | 27 | ||||
| -rw-r--r-- | src/syntax/mod.rs | 24 |
3 files changed, 64 insertions, 62 deletions
diff --git a/src/syntax/ast.rs b/src/syntax/ast.rs index b8780590..cb0a99b9 100644 --- a/src/syntax/ast.rs +++ b/src/syntax/ast.rs @@ -234,11 +234,11 @@ pub enum Expr { /// A binary operation: `a + b`. Binary(BinaryExpr), /// An invocation of a function: `f(x, y)`. - Call(CallExpr), + FuncCall(FuncCall), + /// An invocation of a method: `array.push(v)`. + MethodCall(MethodCall), /// A closure expression: `(x, y) => z`. Closure(ClosureExpr), - /// A with expression: `f with (x, y: 1)`. - With(WithExpr), /// A let expression: `let x = 1`. Let(LetExpr), /// A set expression: `set text(...)`. @@ -276,9 +276,9 @@ impl TypedNode for Expr { NodeKind::DictExpr => node.cast().map(Self::Dict), NodeKind::UnaryExpr => node.cast().map(Self::Unary), NodeKind::BinaryExpr => node.cast().map(Self::Binary), - NodeKind::CallExpr => node.cast().map(Self::Call), + NodeKind::FuncCall => node.cast().map(Self::FuncCall), + NodeKind::MethodCall => node.cast().map(Self::MethodCall), NodeKind::ClosureExpr => node.cast().map(Self::Closure), - NodeKind::WithExpr => node.cast().map(Self::With), NodeKind::LetExpr => node.cast().map(Self::Let), NodeKind::SetExpr => node.cast().map(Self::Set), NodeKind::ShowExpr => node.cast().map(Self::Show), @@ -306,9 +306,9 @@ impl TypedNode for Expr { Self::Group(v) => v.as_red(), Self::Unary(v) => v.as_red(), Self::Binary(v) => v.as_red(), - Self::Call(v) => v.as_red(), + Self::FuncCall(v) => v.as_red(), + Self::MethodCall(v) => v.as_red(), Self::Closure(v) => v.as_red(), - Self::With(v) => v.as_red(), Self::Let(v) => v.as_red(), Self::Set(v) => v.as_red(), Self::Show(v) => v.as_red(), @@ -331,7 +331,7 @@ impl Expr { matches!( self, Self::Ident(_) - | Self::Call(_) + | Self::FuncCall(_) | Self::Let(_) | Self::Set(_) | Self::Show(_) @@ -735,19 +735,45 @@ pub enum Associativity { } node! { - /// An invocation of a function: `foo(...)`. - CallExpr: CallExpr + /// An invocation of a function: `f(x, y)`. + FuncCall: FuncCall } -impl CallExpr { +impl FuncCall { /// The function to call. pub fn callee(&self) -> Expr { - self.0.cast_first_child().expect("call is missing callee") + self.0.cast_first_child().expect("function call is missing callee") } /// The arguments to the function. pub fn args(&self) -> CallArgs { - self.0.cast_last_child().expect("call is missing argument list") + self.0 + .cast_last_child() + .expect("function call is missing argument list") + } +} + +node! { + /// An invocation of a method: `array.push(v)`. + MethodCall: MethodCall +} + +impl MethodCall { + /// The value to call the method on. + pub fn receiver(&self) -> Expr { + self.0.cast_first_child().expect("method call is missing callee") + } + + /// The name of the method. + pub fn method(&self) -> Ident { + self.0.cast_last_child().expect("method call is missing name") + } + + /// The arguments to the method. + pub fn args(&self) -> CallArgs { + self.0 + .cast_last_child() + .expect("method call is missing argument list") } } @@ -863,25 +889,6 @@ impl TypedNode for ClosureParam { } node! { - /// A with expression: `f with (x, y: 1)`. - WithExpr -} - -impl WithExpr { - /// The function to apply the arguments to. - pub fn callee(&self) -> Expr { - self.0.cast_first_child().expect("with expression is missing callee") - } - - /// The arguments to apply to the function. - pub fn args(&self) -> CallArgs { - self.0 - .cast_first_child() - .expect("with expression is missing argument list") - } -} - -node! { /// A let expression: `let x = 1`. LetExpr } @@ -891,10 +898,6 @@ impl LetExpr { pub fn binding(&self) -> Ident { match self.0.cast_first_child() { Some(Expr::Ident(binding)) => binding, - Some(Expr::With(with)) => match with.callee() { - Expr::Ident(binding) => binding, - _ => panic!("let .. with callee must be identifier"), - }, Some(Expr::Closure(closure)) => { closure.name().expect("let-bound closure is missing name") } diff --git a/src/syntax/highlight.rs b/src/syntax/highlight.rs index c0e3376e..bad434b9 100644 --- a/src/syntax/highlight.rs +++ b/src/syntax/highlight.rs @@ -11,10 +11,10 @@ pub fn highlight<F>(node: RedRef, range: Range<usize>, f: &mut F) where F: FnMut(Range<usize>, Category), { - for child in node.children() { + for (i, child) in node.children().enumerate() { let span = child.span(); if range.start <= span.end && range.end >= span.start { - if let Some(category) = Category::determine(child, node) { + if let Some(category) = Category::determine(child, node, i) { f(span.to_range(), category); } highlight(child, range.clone(), f); @@ -44,9 +44,9 @@ fn highlight_syntect_impl<F>( return; } - for child in node.children() { + for (i, child) in node.children().enumerate() { let mut scopes = scopes.clone(); - if let Some(category) = Category::determine(child, node) { + if let Some(category) = Category::determine(child, node, i) { scopes.push(Scope::new(category.tm_scope()).unwrap()) } highlight_syntect_impl(child, scopes, highlighter, f); @@ -101,8 +101,9 @@ pub enum Category { } impl Category { - /// Determine the highlighting category of a node given its parent. - pub fn determine(child: RedRef, parent: RedRef) -> Option<Category> { + /// Determine the highlighting category of a node given its parent and its + /// index in its siblings. + pub fn determine(child: RedRef, parent: RedRef, i: usize) -> Option<Category> { match child.kind() { NodeKind::LeftBrace => Some(Category::Bracket), NodeKind::RightBrace => Some(Category::Bracket), @@ -133,7 +134,6 @@ impl Category { NodeKind::Not => Some(Category::Keyword), NodeKind::And => Some(Category::Keyword), NodeKind::Or => Some(Category::Keyword), - NodeKind::With => Some(Category::Keyword), NodeKind::Let => Some(Category::Keyword), NodeKind::Set => Some(Category::Keyword), NodeKind::Show => Some(Category::Keyword), @@ -156,6 +156,7 @@ impl Category { _ => Some(Category::Operator), }, NodeKind::Slash => Some(Category::Operator), + NodeKind::Dot => Some(Category::Operator), NodeKind::PlusEq => Some(Category::Operator), NodeKind::HyphEq => Some(Category::Operator), NodeKind::StarEq => Some(Category::Operator), @@ -176,13 +177,11 @@ impl Category { NodeKind::Auto => Some(Category::Auto), NodeKind::Ident(_) => match parent.kind() { NodeKind::Named => None, - NodeKind::ClosureExpr if child.span().start == parent.span().start => { - Some(Category::Function) - } - NodeKind::WithExpr => Some(Category::Function), + NodeKind::ClosureExpr if i == 0 => Some(Category::Function), NodeKind::SetExpr => Some(Category::Function), NodeKind::ShowExpr => Some(Category::Function), - NodeKind::CallExpr => Some(Category::Function), + NodeKind::FuncCall => Some(Category::Function), + NodeKind::MethodCall if i > 0 => Some(Category::Function), _ => Some(Category::Variable), }, NodeKind::Bool(_) => Some(Category::Bool), @@ -210,12 +209,12 @@ impl Category { NodeKind::Named => None, NodeKind::UnaryExpr => None, NodeKind::BinaryExpr => None, - NodeKind::CallExpr => None, + NodeKind::FuncCall => None, + NodeKind::MethodCall => None, NodeKind::CallArgs => None, NodeKind::Spread => None, NodeKind::ClosureExpr => None, NodeKind::ClosureParams => None, - NodeKind::WithExpr => None, NodeKind::LetExpr => None, NodeKind::SetExpr => None, NodeKind::ShowExpr => None, diff --git a/src/syntax/mod.rs b/src/syntax/mod.rs index e15cfabc..d0920d20 100644 --- a/src/syntax/mod.rs +++ b/src/syntax/mod.rs @@ -509,6 +509,8 @@ pub enum NodeKind { Minus, /// A slash: `/`. Slash, + /// A dot: `.`. + Dot, /// A single equals sign: `=`. Eq, /// Two equals signs: `==`. @@ -537,8 +539,6 @@ pub enum NodeKind { And, /// The `or` operator. Or, - /// The `with` operator. - With, /// Two dots: `..`. Dots, /// An equals sign followed by a greater-than sign: `=>`. @@ -659,7 +659,9 @@ pub enum NodeKind { /// A binary operation: `a + b`. BinaryExpr, /// An invocation of a function: `f(x, y)`. - CallExpr, + FuncCall, + /// An invocation of a method: `array.push(v)`. + MethodCall, /// A function call's argument list: `(x, y)`. CallArgs, /// Spreaded arguments or a parameter sink: `..x`. @@ -668,8 +670,6 @@ pub enum NodeKind { ClosureExpr, /// A closure's parameters: `(x, y)`. ClosureParams, - /// A with expression: `f with (x, y: 1)`. - WithExpr, /// A let expression: `let x = 1`. LetExpr, /// A set expression: `set text(...)`. @@ -802,7 +802,7 @@ impl NodeKind { | Self::WhileExpr | Self::ForExpr | Self::ImportExpr - | Self::CallExpr + | Self::FuncCall | Self::IncludeExpr | Self::LineComment | Self::BlockComment @@ -830,6 +830,7 @@ impl NodeKind { Self::Plus => "plus", Self::Minus => "minus", Self::Slash => "slash", + Self::Dot => "dot", Self::Eq => "assignment operator", Self::EqEq => "equality operator", Self::ExclEq => "inequality operator", @@ -844,7 +845,6 @@ impl NodeKind { Self::Not => "operator `not`", Self::And => "operator `and`", Self::Or => "operator `or`", - Self::With => "operator `with`", Self::Dots => "dots", Self::Arrow => "arrow", Self::None => "`none`", @@ -899,12 +899,12 @@ impl NodeKind { Self::Named => "named argument", Self::UnaryExpr => "unary expression", Self::BinaryExpr => "binary expression", - Self::CallExpr => "call", + Self::FuncCall => "function call", + Self::MethodCall => "method call", Self::CallArgs => "call arguments", Self::Spread => "parameter sink", Self::ClosureExpr => "closure", Self::ClosureParams => "closure parameters", - Self::WithExpr => "`with` expression", Self::LetExpr => "`let` expression", Self::SetExpr => "`set` expression", Self::ShowExpr => "`show` expression", @@ -954,6 +954,7 @@ impl Hash for NodeKind { Self::Plus => {} Self::Minus => {} Self::Slash => {} + Self::Dot => {} Self::Eq => {} Self::EqEq => {} Self::ExclEq => {} @@ -968,7 +969,6 @@ impl Hash for NodeKind { Self::Not => {} Self::And => {} Self::Or => {} - Self::With => {} Self::Dots => {} Self::Arrow => {} Self::None => {} @@ -1023,12 +1023,12 @@ impl Hash for NodeKind { Self::Named => {} Self::UnaryExpr => {} Self::BinaryExpr => {} - Self::CallExpr => {} + Self::FuncCall => {} + Self::MethodCall => {} Self::CallArgs => {} Self::Spread => {} Self::ClosureExpr => {} Self::ClosureParams => {} - Self::WithExpr => {} Self::LetExpr => {} Self::SetExpr => {} Self::ShowExpr => {} |
