From 78646ca1af743e5dca8eb61c88d740f27381af4d Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Wed, 20 Dec 2017 11:01:15 +0900 Subject: [PATCH] Support delegate syntax Change-Id: Idc51e11494f164f7ca9abed8d650aad10ff629c5 Signed-off-by: Hwankyu Jhun --- idlc/declaration.cc | 8 ++------ idlc/declaration.h | 13 ++++++++++--- idlc/tidlc.ll | 1 + idlc/tidlc.yy | 23 ++++++++++++++++++++--- unit_tests/declaration_unittest.cc | 7 ++++--- 5 files changed, 37 insertions(+), 15 deletions(-) diff --git a/idlc/declaration.cc b/idlc/declaration.cc index 39c9d83..3c3c650 100644 --- a/idlc/declaration.cc +++ b/idlc/declaration.cc @@ -24,13 +24,13 @@ namespace tidl { Declaration::Declaration(std::string id, BaseType* ret_type, Parameters* params, std::string comments, - unsigned line, bool async) + unsigned line, MethodType mtype) : id_(std::move(id)), ret_type_(ret_type), params_(params), comments_(std::move(comments)), line_(line), - async_(async) {} + mtype_(mtype) {} const std::string& Declaration::GetID() const { return id_; @@ -44,10 +44,6 @@ const Parameters& Declaration::GetParameters() const { return *params_; } -bool Declaration::IsAsync() const { - return async_; -} - const unsigned Declaration::GetLine() const { return line_; } diff --git a/idlc/declaration.h b/idlc/declaration.h index 6a37f0b..6aa09e8 100644 --- a/idlc/declaration.h +++ b/idlc/declaration.h @@ -28,13 +28,20 @@ namespace tidl { class Declaration { public: + enum class MethodType { + SYNC, + ASYNC, + DELEGATE + }; + Declaration(std::string id, BaseType* ret_type, Parameters* params, - std::string comments, unsigned line, bool async = false); + std::string comments, unsigned line, + MethodType mtype = MethodType::SYNC); const std::string& GetID() const; const BaseType& GetType() const; const Parameters& GetParameters() const; - bool IsAsync() const; + MethodType GetMethodType() const { return mtype_; } const unsigned GetLine() const; const std::string& GetComments() const; @@ -44,7 +51,7 @@ class Declaration { std::unique_ptr params_; std::string comments_; unsigned line_; - bool async_; + MethodType mtype_; }; class Declarations { diff --git a/idlc/tidlc.ll b/idlc/tidlc.ll index 3bd17f6..d9b235f 100644 --- a/idlc/tidlc.ll +++ b/idlc/tidlc.ll @@ -129,6 +129,7 @@ return yy::parser::token::T_SB_CLOSE; } "=" { return yy::parser::token::T_EQUAL; } +"delegate" { return yy::parser::token::T_DELEGATE; } %% diff --git a/idlc/tidlc.yy b/idlc/tidlc.yy index fe4c3ce..bc313f7 100644 --- a/idlc/tidlc.yy +++ b/idlc/tidlc.yy @@ -27,7 +27,7 @@ int yylex(yy::parser::semantic_type *, yy::parser::location_type *, void *); %token T_LEFT T_RIGHT T_COMMA T_SEMICOLON T_BRACE_OPEN T_BRACE_CLOSE %token T_IN T_OUT T_REF T_ASYNC %token T_META_OPEN T_META_CLOSE -%token T_EQUAL +%token T_EQUAL T_DELEGATE %start start @@ -273,13 +273,21 @@ declarations: declaration { declaration: base_type T_ID T_LEFT parameter_list T_RIGHT T_SEMICOLON { $$ = new tidl::Declaration($2->ToString(), $1, $4, $1->GetComments(), - @1.begin.line); + @1.begin.line, tidl::Declaration::MethodType::SYNC); delete $2; } | T_VOID T_ID T_LEFT parameter_list T_RIGHT T_ASYNC T_SEMICOLON { $$ = new tidl::Declaration($2->ToString(), new tidl::BaseType("void", $1->GetComments()), $4, - $1->GetComments(), @1.begin.line, true); + $1->GetComments(), @1.begin.line, tidl::Declaration::MethodType::ASYNC); + delete $1; + delete $2; + } + | T_VOID T_ID T_LEFT parameter_list T_RIGHT T_DELEGATE T_SEMICOLON { + $$ = new tidl::Declaration($2->ToString(), + new tidl::BaseType("void", $1->GetComments()), $4, + $1->GetComments(), @1.begin.line, + tidl::Declaration::MethodType::DELEGATE); delete $1; delete $2; } @@ -288,6 +296,11 @@ declaration: base_type T_ID T_LEFT parameter_list T_RIGHT T_SEMICOLON { $$ = NULL; delete $2; } + | base_type T_ID T_LEFT parameter_list T_RIGHT T_DELEGATE T_SEMICOLON { + ps->ReportError("syntax error in method declaration.", @2.begin.line); + $$ = NULL; + delete $2; + } | T_VOID T_ID T_LEFT parameter_list T_RIGHT T_SEMICOLON { ps->ReportError("syntax error. \"No async\".", @6.begin.line); $$ = NULL; @@ -301,6 +314,10 @@ declaration: base_type T_ID T_LEFT parameter_list T_RIGHT T_SEMICOLON { ps->ReportError("syntax error. \"No identifier\".", @2.begin.line); $$ = NULL; } + | T_VOID T_LEFT parameter_list T_RIGHT T_DELEGATE T_SEMICOLON { + ps->ReportError("syntax error. \"No identifier\".", @2.begin.line); + $$ = NULL; + } | base_type error T_SEMICOLON { ps->ReportError("syntax error in method declaration.", @2.begin.line); $$ = NULL; diff --git a/unit_tests/declaration_unittest.cc b/unit_tests/declaration_unittest.cc index 8915e06..2e5c9a4 100644 --- a/unit_tests/declaration_unittest.cc +++ b/unit_tests/declaration_unittest.cc @@ -79,11 +79,12 @@ TEST_F(DeclarationTest, Declaration_GetParameters) { delete decl; } -TEST_F(DeclarationTest, Declaration_IsAsync) { +TEST_F(DeclarationTest, Declaration_GetMethodType) { tidl::Declaration* decl = new tidl::Declaration("test", - new tidl::BaseType("int", ""), params, "", __LINE__); + new tidl::BaseType("int", ""), params, "", __LINE__, + tidl::Declaration::MethodType::DELEGATE); EXPECT_NE(decl, nullptr); - EXPECT_EQ(decl->IsAsync(), false); + EXPECT_EQ(decl->GetMethodType(), tidl::Declaration::MethodType::DELEGATE); delete decl; } -- 2.7.4