From 7a2cc3e7d29d16c5cf9b5a93a688e14da93c8662 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Tue, 19 Apr 2022 16:37:16 +0200 Subject: Field access --- src/syntax/ast.rs | 21 +++++++++++++++++++++ src/syntax/highlight.rs | 1 + src/syntax/mod.rs | 4 ++++ 3 files changed, 26 insertions(+) (limited to 'src/syntax') diff --git a/src/syntax/ast.rs b/src/syntax/ast.rs index 60856691..8af359bf 100644 --- a/src/syntax/ast.rs +++ b/src/syntax/ast.rs @@ -237,6 +237,8 @@ pub enum Expr { Unary(UnaryExpr), /// A binary operation: `a + b`. Binary(BinaryExpr), + /// A field access: `properties.age`. + FieldAccess(FieldAccess), /// An invocation of a function: `f(x, y)`. FuncCall(FuncCall), /// An invocation of a method: `array.push(v)`. @@ -280,6 +282,7 @@ 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::FieldAccess => node.cast().map(Self::FieldAccess), NodeKind::FuncCall => node.cast().map(Self::FuncCall), NodeKind::MethodCall => node.cast().map(Self::MethodCall), NodeKind::ClosureExpr => node.cast().map(Self::Closure), @@ -310,6 +313,7 @@ impl TypedNode for Expr { Self::Group(v) => v.as_red(), Self::Unary(v) => v.as_red(), Self::Binary(v) => v.as_red(), + Self::FieldAccess(v) => v.as_red(), Self::FuncCall(v) => v.as_red(), Self::MethodCall(v) => v.as_red(), Self::Closure(v) => v.as_red(), @@ -789,6 +793,23 @@ pub enum Associativity { Right, } +node! { + /// A field access: `properties.age`. + FieldAccess: FieldAccess +} + +impl FieldAccess { + /// The object with the field. + pub fn object(&self) -> Expr { + self.0.cast_first_child().expect("field access is missing object") + } + + /// The name of the field. + pub fn field(&self) -> Ident { + self.0.cast_last_child().expect("field access call is missing name") + } +} + node! { /// An invocation of a function: `f(x, y)`. FuncCall: FuncCall diff --git a/src/syntax/highlight.rs b/src/syntax/highlight.rs index 004ff957..10dfce69 100644 --- a/src/syntax/highlight.rs +++ b/src/syntax/highlight.rs @@ -209,6 +209,7 @@ impl Category { NodeKind::Named => None, NodeKind::UnaryExpr => None, NodeKind::BinaryExpr => None, + NodeKind::FieldAccess => None, NodeKind::FuncCall => None, NodeKind::MethodCall => None, NodeKind::CallArgs => None, diff --git a/src/syntax/mod.rs b/src/syntax/mod.rs index d18b6a3d..71646cb2 100644 --- a/src/syntax/mod.rs +++ b/src/syntax/mod.rs @@ -653,6 +653,8 @@ pub enum NodeKind { UnaryExpr, /// A binary operation: `a + b`. BinaryExpr, + /// A field access: `properties.age`. + FieldAccess, /// An invocation of a function: `f(x, y)`. FuncCall, /// An invocation of a method: `array.push(v)`. @@ -898,6 +900,7 @@ impl NodeKind { Self::Named => "named argument", Self::UnaryExpr => "unary expression", Self::BinaryExpr => "binary expression", + Self::FieldAccess => "field access", Self::FuncCall => "function call", Self::MethodCall => "method call", Self::CallArgs => "call arguments", @@ -1021,6 +1024,7 @@ impl Hash for NodeKind { Self::Named => {} Self::UnaryExpr => {} Self::BinaryExpr => {} + Self::FieldAccess => {} Self::FuncCall => {} Self::MethodCall => {} Self::CallArgs => {} -- cgit v1.2.3