From 2f2b76cf9e50e9b6ab328e7b274119c486ad11c4 Mon Sep 17 00:00:00 2001 From: Steve Cao Date: Wed, 17 Feb 2021 11:13:34 -0800 Subject: [PATCH] don't wrap positioned smart constructor by WithKind smart constructor Summary: parser requires smart constructor node to implement `smart_constructor::NodeType`, `smart_constructor_wrapper::WithKind` is smart constructor, which wraps each node with kind, it lifts any smart constructor to a smart constructor with node implementing `NodeType`. However if a node in a smart constructor already be able provide kind information, this wrapper is redundant, it also improves perf. service lab reports ~2% win Reviewed By: shayne-fletcher Differential Revision: D26419802 fbshipit-source-id: c51edd2d460275f126abe4d3faeee1050eb4c457 --- .../src/parser/api/positioned_by_ref_parser.rs | 2 +- hphp/hack/src/parser/smart_constructors.rs | 72 +++++++++++++++++++++- 2 files changed, 71 insertions(+), 3 deletions(-) diff --git a/hphp/hack/src/parser/api/positioned_by_ref_parser.rs b/hphp/hack/src/parser/api/positioned_by_ref_parser.rs index bfdd12af321..fd66b7186ac 100644 --- a/hphp/hack/src/parser/api/positioned_by_ref_parser.rs +++ b/hphp/hack/src/parser/api/positioned_by_ref_parser.rs @@ -57,7 +57,7 @@ pub fn parse_script<'src, 'arena>( stack_limit: Option<&'src StackLimit>, ) -> (Syntax<'arena>, Vec, State<'arena>) { let tf = TokenFactory { arena }; - let sc = WithKind::new(SmartConstructors::new(State { arena }, tf)); + let sc = SmartConstructors::new(State { arena }, tf); let mut parser = Parser::new(&source, env, sc); let root = parser.parse_script(stack_limit); let errors = parser.errors(); diff --git a/hphp/hack/src/parser/smart_constructors.rs b/hphp/hack/src/parser/smart_constructors.rs index a5fbc18ec47..14720892d7e 100644 --- a/hphp/hack/src/parser/smart_constructors.rs +++ b/hphp/hack/src/parser/smart_constructors.rs @@ -9,8 +9,12 @@ pub mod smart_constructors_wrappers; use ocamlrep_derive::{FromOcamlRep, ToOcamlRep}; -use parser_core_types::syntax_kind::SyntaxKind; -use parser_core_types::token_kind::TokenKind; +use parser_core_types::{ + lexable_token::LexableToken, + syntax_by_ref::{syntax::Syntax, syntax_variant_generated::SyntaxVariant}, + syntax_kind::SyntaxKind, + token_kind::TokenKind, +}; pub use crate::smart_constructors_generated::*; pub use crate::smart_constructors_wrappers::*; @@ -136,3 +140,67 @@ impl NodeType for (SyntaxKind, R) { } } } + +impl<'a, T: LexableToken, V> NodeType for Syntax<'a, T, V> { + type R = Self; + + fn extract(self) -> Self::R { + self + } + + fn is_missing(&self) -> bool { + match self.children { + SyntaxVariant::Missing => true, + SyntaxVariant::SyntaxList(l) if l.is_empty() => true, + _ => false, + } + } + + fn is_abstract(&self) -> bool { + matches!(&self.children, SyntaxVariant::Token(t) if t.kind() == TokenKind::Abstract) + } + + fn is_variable_expression(&self) -> bool { + matches!(self.children, SyntaxVariant::VariableExpression {..}) + } + + fn is_subscript_expression(&self) -> bool { + matches!(self.children, SyntaxVariant::SubscriptExpression {..}) + } + + fn is_member_selection_expression(&self) -> bool { + matches!(self.children, SyntaxVariant::MemberSelectionExpression {..}) + } + + fn is_scope_resolution_expression(&self) -> bool { + matches!(self.children, SyntaxVariant::ScopeResolutionExpression {..}) + } + + fn is_object_creation_expression(&self) -> bool { + matches!(self.children, SyntaxVariant::ObjectCreationExpression {..}) + } + + fn is_qualified_name(&self) -> bool { + matches!(self.children, SyntaxVariant::QualifiedName {..}) + } + + fn is_safe_member_selection_expression(&self) -> bool { + matches!(self.children, SyntaxVariant::SafeMemberSelectionExpression {..}) + } + + fn is_function_call_expression(&self) -> bool { + matches!(self.children, SyntaxVariant::FunctionCallExpression {..}) + } + + fn is_list_expression(&self) -> bool { + matches!(self.children, SyntaxVariant::ListExpression {..}) + } + + fn is_name(&self) -> bool { + matches!(&self.children, SyntaxVariant::Token(t) if t.kind() == TokenKind::Name) + } + + fn is_prefix_unary_expression(&self) -> bool { + matches!(self.children, SyntaxVariant::PrefixUnaryExpression {..}) + } +} -- 2.11.4.GIT