From abaef074c6a5e88f7c887bcda358074cebebe10c Mon Sep 17 00:00:00 2001 From: clatisus Date: Thu, 16 Dec 2021 17:34:23 +0800 Subject: [PATCH] feat. support Airbnb type & annotation --- .gitignore | 1 + .../thrift/lang/lexer/ThriftTokenTypes.java | 4 +++ .../thrift/lang/parser/ThriftParser.java | 29 +++++++++++++----- .../lang/psi/ThriftSimpleAirbnbType.java | 10 +++++++ .../thrift/lang/psi/ThriftSimpleBaseType.java | 3 ++ .../thrift/lang/psi/ThriftVisitor.java | 4 +++ .../psi/impl/ThriftSimpleAirbnbTypeImpl.java | 30 +++++++++++++++++++ .../psi/impl/ThriftSimpleBaseTypeImpl.java | 6 ++++ thrift/src/main/grammar/Thrift.bnf | 5 ++-- .../plugins/thrift/util/ThriftUtils.java | 15 ++++++++-- 10 files changed, 95 insertions(+), 12 deletions(-) create mode 100644 thrift/src/main/gen/com/intellij/plugins/thrift/lang/psi/ThriftSimpleAirbnbType.java create mode 100644 thrift/src/main/gen/com/intellij/plugins/thrift/lang/psi/impl/ThriftSimpleAirbnbTypeImpl.java diff --git a/.gitignore b/.gitignore index a4a98a92..51108ce9 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ .gradle build +bin diff --git a/thrift/src/main/gen/com/intellij/plugins/thrift/lang/lexer/ThriftTokenTypes.java b/thrift/src/main/gen/com/intellij/plugins/thrift/lang/lexer/ThriftTokenTypes.java index e460c4b3..f975b999 100644 --- a/thrift/src/main/gen/com/intellij/plugins/thrift/lang/lexer/ThriftTokenTypes.java +++ b/thrift/src/main/gen/com/intellij/plugins/thrift/lang/lexer/ThriftTokenTypes.java @@ -44,6 +44,7 @@ public interface ThriftTokenTypes { IElementType SERVICE_BODY = new ThriftElementType("SERVICE_BODY"); IElementType SERVICE_SUPER_NAME = new ThriftElementType("SERVICE_SUPER_NAME"); IElementType SET_TYPE = new ThriftElementType("SET_TYPE"); + IElementType SIMPLE_AIRBNB_TYPE = new ThriftElementType("SIMPLE_AIRBNB_TYPE"); IElementType SIMPLE_BASE_TYPE = new ThriftElementType("SIMPLE_BASE_TYPE"); IElementType STRUCT = new ThriftElementType("STRUCT"); IElementType THROWS = new ThriftElementType("THROWS"); @@ -189,6 +190,9 @@ else if (type == SERVICE_SUPER_NAME) { else if (type == SET_TYPE) { return new ThriftSetTypeImpl(node); } + else if (type == SIMPLE_AIRBNB_TYPE) { + return new ThriftSimpleAirbnbTypeImpl(node); + } else if (type == SIMPLE_BASE_TYPE) { return new ThriftSimpleBaseTypeImpl(node); } diff --git a/thrift/src/main/gen/com/intellij/plugins/thrift/lang/parser/ThriftParser.java b/thrift/src/main/gen/com/intellij/plugins/thrift/lang/parser/ThriftParser.java index 8b097664..41974e93 100644 --- a/thrift/src/main/gen/com/intellij/plugins/thrift/lang/parser/ThriftParser.java +++ b/thrift/src/main/gen/com/intellij/plugins/thrift/lang/parser/ThriftParser.java @@ -785,7 +785,21 @@ private static boolean SetType_1(PsiBuilder b, int l) { } /* ********************************************************** */ - // 'bool' | 'byte' | 'i8' | 'i16' | 'i32' | 'i64' | 'double' | 'string' | 'binary' | 'slist' + // 'date_time' | 'date' | 'json_object' | 'id' + public static boolean SimpleAirbnbType(PsiBuilder b, int l) { + if (!recursion_guard_(b, l, "SimpleAirbnbType")) return false; + boolean r; + Marker m = enter_section_(b, l, _NONE_, SIMPLE_AIRBNB_TYPE, ""); + r = consumeToken(b, "date_time"); + if (!r) r = consumeToken(b, "date"); + if (!r) r = consumeToken(b, "json_object"); + if (!r) r = consumeToken(b, "id"); + exit_section_(b, l, m, r, false, null); + return r; + } + + /* ********************************************************** */ + // 'bool' | 'byte' | 'i8' | 'i16' | 'i32' | 'i64' | 'double' | 'string' | 'binary' | 'slist' | SimpleAirbnbType public static boolean SimpleBaseType(PsiBuilder b, int l) { if (!recursion_guard_(b, l, "SimpleBaseType")) return false; boolean r; @@ -800,6 +814,7 @@ public static boolean SimpleBaseType(PsiBuilder b, int l) { if (!r) r = consumeToken(b, "string"); if (!r) r = consumeToken(b, "binary"); if (!r) r = consumeToken(b, "slist"); + if (!r) r = SimpleAirbnbType(b, l + 1); exit_section_(b, l, m, r, false, null); return r; } @@ -863,7 +878,7 @@ public static boolean Throws(PsiBuilder b, int l) { } /* ********************************************************** */ - // Identifier ('=' Literal ListSeparator?)? + // Identifier ('=' Literal)? ListSeparator? public static boolean TypeAnnotation(PsiBuilder b, int l) { if (!recursion_guard_(b, l, "TypeAnnotation")) return false; if (!nextTokenIs(b, IDENTIFIER)) return false; @@ -871,31 +886,31 @@ public static boolean TypeAnnotation(PsiBuilder b, int l) { Marker m = enter_section_(b); r = consumeToken(b, IDENTIFIER); r = r && TypeAnnotation_1(b, l + 1); + r = r && TypeAnnotation_2(b, l + 1); exit_section_(b, m, TYPE_ANNOTATION, r); return r; } - // ('=' Literal ListSeparator?)? + // ('=' Literal)? private static boolean TypeAnnotation_1(PsiBuilder b, int l) { if (!recursion_guard_(b, l, "TypeAnnotation_1")) return false; TypeAnnotation_1_0(b, l + 1); return true; } - // '=' Literal ListSeparator? + // '=' Literal private static boolean TypeAnnotation_1_0(PsiBuilder b, int l) { if (!recursion_guard_(b, l, "TypeAnnotation_1_0")) return false; boolean r; Marker m = enter_section_(b); r = consumeTokens(b, 0, EQUALS, LITERAL); - r = r && TypeAnnotation_1_0_2(b, l + 1); exit_section_(b, m, null, r); return r; } // ListSeparator? - private static boolean TypeAnnotation_1_0_2(PsiBuilder b, int l) { - if (!recursion_guard_(b, l, "TypeAnnotation_1_0_2")) return false; + private static boolean TypeAnnotation_2(PsiBuilder b, int l) { + if (!recursion_guard_(b, l, "TypeAnnotation_2")) return false; ListSeparator(b, l + 1); return true; } diff --git a/thrift/src/main/gen/com/intellij/plugins/thrift/lang/psi/ThriftSimpleAirbnbType.java b/thrift/src/main/gen/com/intellij/plugins/thrift/lang/psi/ThriftSimpleAirbnbType.java new file mode 100644 index 00000000..62852a47 --- /dev/null +++ b/thrift/src/main/gen/com/intellij/plugins/thrift/lang/psi/ThriftSimpleAirbnbType.java @@ -0,0 +1,10 @@ +// This is a generated file. Not intended for manual editing. +package com.intellij.plugins.thrift.lang.psi; + +import java.util.List; +import org.jetbrains.annotations.*; +import com.intellij.psi.PsiElement; + +public interface ThriftSimpleAirbnbType extends ThriftPsiCompositeElement { + +} diff --git a/thrift/src/main/gen/com/intellij/plugins/thrift/lang/psi/ThriftSimpleBaseType.java b/thrift/src/main/gen/com/intellij/plugins/thrift/lang/psi/ThriftSimpleBaseType.java index d5efd301..32e33865 100644 --- a/thrift/src/main/gen/com/intellij/plugins/thrift/lang/psi/ThriftSimpleBaseType.java +++ b/thrift/src/main/gen/com/intellij/plugins/thrift/lang/psi/ThriftSimpleBaseType.java @@ -7,4 +7,7 @@ public interface ThriftSimpleBaseType extends ThriftPsiCompositeElement { + @Nullable + ThriftSimpleAirbnbType getSimpleAirbnbType(); + } diff --git a/thrift/src/main/gen/com/intellij/plugins/thrift/lang/psi/ThriftVisitor.java b/thrift/src/main/gen/com/intellij/plugins/thrift/lang/psi/ThriftVisitor.java index 3bdd3527..35335a38 100644 --- a/thrift/src/main/gen/com/intellij/plugins/thrift/lang/psi/ThriftVisitor.java +++ b/thrift/src/main/gen/com/intellij/plugins/thrift/lang/psi/ThriftVisitor.java @@ -129,6 +129,10 @@ public void visitSetType(@NotNull ThriftSetType o) { visitPsiCompositeElement(o); } + public void visitSimpleAirbnbType(@NotNull ThriftSimpleAirbnbType o) { + visitPsiCompositeElement(o); + } + public void visitSimpleBaseType(@NotNull ThriftSimpleBaseType o) { visitPsiCompositeElement(o); } diff --git a/thrift/src/main/gen/com/intellij/plugins/thrift/lang/psi/impl/ThriftSimpleAirbnbTypeImpl.java b/thrift/src/main/gen/com/intellij/plugins/thrift/lang/psi/impl/ThriftSimpleAirbnbTypeImpl.java new file mode 100644 index 00000000..bb0114ca --- /dev/null +++ b/thrift/src/main/gen/com/intellij/plugins/thrift/lang/psi/impl/ThriftSimpleAirbnbTypeImpl.java @@ -0,0 +1,30 @@ +// This is a generated file. Not intended for manual editing. +package com.intellij.plugins.thrift.lang.psi.impl; + +import java.util.List; +import org.jetbrains.annotations.*; +import com.intellij.lang.ASTNode; +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiElementVisitor; +import com.intellij.psi.util.PsiTreeUtil; +import static com.intellij.plugins.thrift.lang.lexer.ThriftTokenTypes.*; +import com.intellij.plugins.thrift.lang.psi.*; +import com.intellij.plugins.thrift.util.ThriftPsiUtil; + +public class ThriftSimpleAirbnbTypeImpl extends ThriftPsiCompositeElementImpl implements ThriftSimpleAirbnbType { + + public ThriftSimpleAirbnbTypeImpl(@NotNull ASTNode node) { + super(node); + } + + public void accept(@NotNull ThriftVisitor visitor) { + visitor.visitSimpleAirbnbType(this); + } + + @Override + public void accept(@NotNull PsiElementVisitor visitor) { + if (visitor instanceof ThriftVisitor) accept((ThriftVisitor)visitor); + else super.accept(visitor); + } + +} diff --git a/thrift/src/main/gen/com/intellij/plugins/thrift/lang/psi/impl/ThriftSimpleBaseTypeImpl.java b/thrift/src/main/gen/com/intellij/plugins/thrift/lang/psi/impl/ThriftSimpleBaseTypeImpl.java index 3654eec3..e6e52172 100644 --- a/thrift/src/main/gen/com/intellij/plugins/thrift/lang/psi/impl/ThriftSimpleBaseTypeImpl.java +++ b/thrift/src/main/gen/com/intellij/plugins/thrift/lang/psi/impl/ThriftSimpleBaseTypeImpl.java @@ -27,4 +27,10 @@ public void accept(@NotNull PsiElementVisitor visitor) { else super.accept(visitor); } + @Override + @Nullable + public ThriftSimpleAirbnbType getSimpleAirbnbType() { + return findChildByClass(ThriftSimpleAirbnbType.class); + } + } diff --git a/thrift/src/main/grammar/Thrift.bnf b/thrift/src/main/grammar/Thrift.bnf index e42eb646..c4357a37 100644 --- a/thrift/src/main/grammar/Thrift.bnf +++ b/thrift/src/main/grammar/Thrift.bnf @@ -138,7 +138,8 @@ Throws ::= 'throws' '(' fieldsWithBraceRecovery ')' {pin=1} FieldType ::= BaseType | ContainerType | CustomType BaseType ::= SimpleBaseType TypeAnnotations? -SimpleBaseType ::= 'bool' | 'byte' | 'i8' | 'i16' | 'i32' | 'i64' | 'double' | 'string' | 'binary' | 'slist' +SimpleBaseType ::= 'bool' | 'byte' | 'i8' | 'i16' | 'i32' | 'i64' | 'double' | 'string' | 'binary' | 'slist' | SimpleAirbnbType +SimpleAirbnbType::= 'date_time' | 'date' | 'json_object' | 'id' ContainerType ::= SimpleContainerType TypeAnnotations? private SimpleContainerType ::= MapType | SetType | ListType MapType ::= 'map' CppType? GenericType {pin=1} @@ -163,6 +164,6 @@ ListSeparator ::= ',' | ';' TypeAnnotations ::= '(' TypeAnnotationList ')' TypeAnnotationList ::= TypeAnnotation* {recoverWhile=braceRecovery} -TypeAnnotation ::= Identifier ('=' Literal ListSeparator?)? +TypeAnnotation ::= Identifier ('=' Literal)? ListSeparator? private braceRecovery ::= !(')') \ No newline at end of file diff --git a/thrift/src/main/java/com/intellij/plugins/thrift/util/ThriftUtils.java b/thrift/src/main/java/com/intellij/plugins/thrift/util/ThriftUtils.java index c6a94ebb..44d99112 100644 --- a/thrift/src/main/java/com/intellij/plugins/thrift/util/ThriftUtils.java +++ b/thrift/src/main/java/com/intellij/plugins/thrift/util/ThriftUtils.java @@ -3,6 +3,8 @@ import com.intellij.util.containers.ContainerUtil; import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; /** * Created by fkorotkov. @@ -20,7 +22,14 @@ public class ThriftUtils { "javame", "json", "netstd", "nodejs", "nodets", "ocaml", "swift", "ts", "xml" ); - public static Set getKeywords() { - return keywords; - } + private static final Set keywordsWithAirbnb = + Stream.concat( + keywords.stream(), + ContainerUtil.immutableSet( + "date_time", "date", "json_object", "id").stream() + ).collect(Collectors.toSet()); + + public static Set getKeywords() { + return keywordsWithAirbnb; + } }