/* * Copyright (C) 2016, The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ %{ #include "aidl_language.h" #include "parser.h" #include "aidl_language_y-module.h" #include "logging.h" #include #include #include #include #include #include int yylex(yy::parser::semantic_type *, yy::parser::location_type *, void *); AidlLocation loc(const yy::parser::location_type& begin, const yy::parser::location_type& end) { CHECK(begin.begin.filename == begin.end.filename); CHECK(begin.end.filename == end.begin.filename); CHECK(end.begin.filename == end.end.filename); AidlLocation::Point begin_point { .line = begin.begin.line, .column = begin.begin.column, }; AidlLocation::Point end_point { .line = end.end.line, .column = end.end.column, }; return AidlLocation(*begin.begin.filename, begin_point, end_point, AidlLocation::Source::EXTERNAL); } AidlLocation loc(const yy::parser::location_type& l) { return loc(l, l); } #define lex_scanner ps->Scanner() %} %initial-action { @$.begin.filename = @$.end.filename = const_cast(&ps->FileName()); } %parse-param { Parser* ps } %lex-param { void *lex_scanner } %glr-parser %skeleton "glr.cc" %expect-rr 0 %define parse.error verbose %locations %union { AidlToken* token; char character; std::string *str; AidlAnnotation* annotation; AidlAnnotationParameter* param; std::map>* param_list; std::vector* annotation_list; AidlTypeSpecifier* type; AidlArgument* arg; AidlArgument::Direction direction; AidlConstantValue* const_expr; AidlEnumerator* enumerator; std::vector>* enumerators; AidlEnumDeclaration* enum_decl; std::vector>* constant_value_list; std::vector>* arg_list; AidlVariableDeclaration* variable; std::vector>* variable_list; AidlMethod* method; AidlMember* constant; std::vector>* interface_members; AidlInterface* interface; AidlParcelable* parcelable; AidlDefinedType* declaration; std::vector>* type_args; std::vector* type_params; std::vector>* imports; AidlImport* import; std::vector>* declarations; } %destructor { } %destructor { } %destructor { delete ($$); } <*> %token ANNOTATION "annotation" %token C_STR "string literal" %token IDENTIFIER "identifier" %token INTERFACE "interface" %token PARCELABLE "parcelable" %token ONEWAY "oneway" %token ENUM "enum" %token CONST "const" %token CHARVALUE "char literal" %token FLOATVALUE "float literal" %token HEXVALUE "hex literal" %token INTVALUE "int literal" %token '(' ')' ',' '=' '[' ']' '.' '{' '}' ';' %token UNKNOWN "unrecognized character" %token CPP_HEADER "cpp_header (which can also be used as an identifier)" %token IMPORT "import" %token IN "in" %token INOUT "inout" %token OUT "out" %token PACKAGE "package" %token TRUE_LITERAL "true" %token FALSE_LITERAL "false" /* Operator precedence and associativity, as per * http://en.cppreference.com/w/cpp/language/operator_precedence */ /* Precedence level 13 - 14, LTR, logical operators*/ %left LOGICAL_OR %left LOGICAL_AND /* Precedence level 10 - 12, LTR, bitwise operators*/ %left '|' %left '^' %left '&' /* Precedence level 9, LTR */ %left EQUALITY NEQ /* Precedence level 8, LTR */ %left '<' '>' LEQ GEQ /* Precedence level 7, LTR */ %left LSHIFT RSHIFT /* Precedence level 6, LTR */ %left '+' '-' /* Precedence level 5, LTR */ %left '*' '/' '%' /* Precedence level 3, RTL; but we have to use %left here */ %right UNARY_PLUS UNARY_MINUS '!' '~' %type decl %type variable_decls %type variable_decl %type interface_members %type unannotated_decl %type interface_decl %type parcelable_decl %type method_decl %type constant_decl %type enumerator %type enumerators enum_decl_body %type enum_decl %type parameter %type parameter_list %type parameter_non_empty_list %type annotation %typeannotation_list %type type %type unannotated_type %type arg_list arg_non_empty_list %type arg %type direction %type type_args %type type_params %type const_expr %type constant_value_list %type constant_value_non_empty_list %type imports %type import %type decls %type identifier error qualified_name %% document : package imports decls { ps->SetDocument(std::make_unique(loc(@1), *$2, std::move(*$3))); delete $2; delete $3; } /* A couple of tokens that are keywords elsewhere are identifiers when * occurring in the identifier position. Therefore identifier is a * non-terminal, which is either an IDENTIFIER token, or one of the * aforementioned keyword tokens. */ identifier : IDENTIFIER { $$ = $1; } | CPP_HEADER { $$ = new AidlToken("cpp_header", ""); } ; package : {} | PACKAGE qualified_name ';' { ps->SetPackage($2->GetText()); delete $2; } imports : { $$ = new std::vector>(); } | imports import { $$ = $1; auto it = std::find_if($$->begin(), $$->end(), [&](const auto& i) { return $2->GetNeededClass() == i->GetNeededClass(); }); if (it == $$->end()) { $$->emplace_back($2); } else { delete $2; } } import : IMPORT qualified_name ';' { $$ = new AidlImport(loc(@2), $2->GetText()); delete $2; }; qualified_name : identifier { $$ = $1; } | qualified_name '.' identifier { $$ = new AidlToken($1->GetText() + "." + $3->GetText(), $1->GetComments()); delete $1; delete $3; }; decls : decl { $$ = new std::vector>(); if ($1 != nullptr) { $$->emplace_back($1); } } | decls decl { $$ = $1; if ($2 != nullptr) { $$->emplace_back($2); } } decl : annotation_list unannotated_decl { $$ = $2; if ($1->size() > 0 && $$ != nullptr) { // copy comments from annotation to decl $$->SetComments($1->begin()->GetComments()); $$->Annotate(std::move(*$1)); } delete $1; } ; unannotated_decl : parcelable_decl { $$ = $1; } | interface_decl { $$ = $1; } | enum_decl { $$ = $1; } ; type_params : identifier { $$ = new std::vector(); $$->emplace_back($1->GetText()); delete $1; } | type_params ',' identifier { $1->emplace_back($3->GetText()); $$ = $1; delete $3; }; parcelable_decl : PARCELABLE qualified_name ';' { $$ = new AidlParcelable(loc(@2), $2->GetText(), ps->Package(), $1->GetComments()); delete $1; delete $2; } | PARCELABLE qualified_name '<' type_params '>' ';' { $$ = new AidlParcelable(loc(@2), $2->GetText(), ps->Package(), $1->GetComments(), "", $4); delete $1; delete $2; } | PARCELABLE qualified_name CPP_HEADER C_STR ';' { $$ = new AidlParcelable(loc(@2), $2->GetText(), ps->Package(), $1->GetComments(), $4->GetText()); delete $1; delete $2; delete $4; } | PARCELABLE identifier '{' variable_decls '}' { $$ = new AidlStructuredParcelable(loc(@2), $2->GetText(), ps->Package(), $1->GetComments(), $4); delete $1; delete $2; delete $4; } | PARCELABLE error ';' { ps->AddError(); $$ = nullptr; delete $1; }; variable_decls : /* empty */ { $$ = new std::vector>; } | variable_decls variable_decl { $$ = $1; if ($2 != nullptr) { $$->push_back(std::unique_ptr($2)); } }; variable_decl : type identifier ';' { $$ = new AidlVariableDeclaration(loc(@2), $1, $2->GetText()); delete $2; } | type identifier '=' const_expr ';' { // TODO(b/123321528): Support enum type default assignments (TestEnum foo = TestEnum.FOO). $$ = new AidlVariableDeclaration(loc(@2), $1, $2->GetText(), $4); delete $2; } | error ';' { ps->AddError(); $$ = nullptr; } interface_decl : INTERFACE identifier '{' interface_members '}' { $$ = new AidlInterface(loc(@1), $2->GetText(), $1->GetComments(), false, $4, ps->Package()); delete $1; delete $2; } | ONEWAY INTERFACE identifier '{' interface_members '}' { $$ = new AidlInterface(loc(@2), $3->GetText(), $1->GetComments(), true, $5, ps->Package()); delete $1; delete $2; delete $3; } | INTERFACE error '{' interface_members '}' { ps->AddError(); $$ = nullptr; delete $1; delete $4; }; interface_members : { $$ = new std::vector>(); } | interface_members method_decl { $1->push_back(std::unique_ptr($2)); $$ = $1; } | interface_members constant_decl { $1->push_back(std::unique_ptr($2)); $$ = $1; } | interface_members error ';' { ps->AddError(); $$ = $1; }; const_expr : TRUE_LITERAL { $$ = AidlConstantValue::Boolean(loc(@1), true); } | FALSE_LITERAL { $$ = AidlConstantValue::Boolean(loc(@1), false); } | CHARVALUE { $$ = AidlConstantValue::Character(loc(@1), $1); } | INTVALUE { $$ = AidlConstantValue::Integral(loc(@1), $1->GetText()); if ($$ == nullptr) { std::cerr << "ERROR: Could not parse integer: " << $1->GetText() << " at " << @1 << ".\n"; ps->AddError(); $$ = AidlConstantValue::Integral(loc(@1), "0"); } delete $1; } | FLOATVALUE { $$ = AidlConstantValue::Floating(loc(@1), $1->GetText()); delete $1; } | HEXVALUE { $$ = AidlConstantValue::Integral(loc(@1), $1->GetText()); if ($$ == nullptr) { std::cerr << "ERROR: Could not parse hexvalue: " << $1->GetText() << " at " << @1 << ".\n"; ps->AddError(); $$ = AidlConstantValue::Integral(loc(@1), "0"); } delete $1; } | C_STR { $$ = AidlConstantValue::String(loc(@1), $1->GetText()); delete $1; } | '{' constant_value_list '}' { $$ = AidlConstantValue::Array(loc(@1), std::unique_ptr>>($2)); } | const_expr LOGICAL_OR const_expr { $$ = new AidlBinaryConstExpression(loc(@1), std::unique_ptr($1), "||", std::unique_ptr($3)); } | const_expr LOGICAL_AND const_expr { $$ = new AidlBinaryConstExpression(loc(@1), std::unique_ptr($1), "&&", std::unique_ptr($3)); } | const_expr '|' const_expr { $$ = new AidlBinaryConstExpression(loc(@1), std::unique_ptr($1), "|" , std::unique_ptr($3)); } | const_expr '^' const_expr { $$ = new AidlBinaryConstExpression(loc(@1), std::unique_ptr($1), "^" , std::unique_ptr($3)); } | const_expr '&' const_expr { $$ = new AidlBinaryConstExpression(loc(@1), std::unique_ptr($1), "&" , std::unique_ptr($3)); } | const_expr EQUALITY const_expr { $$ = new AidlBinaryConstExpression(loc(@1), std::unique_ptr($1), "==", std::unique_ptr($3)); } | const_expr NEQ const_expr { $$ = new AidlBinaryConstExpression(loc(@1), std::unique_ptr($1), "!=", std::unique_ptr($3)); } | const_expr '<' const_expr { $$ = new AidlBinaryConstExpression(loc(@1), std::unique_ptr($1), "<" , std::unique_ptr($3)); } | const_expr '>' const_expr { $$ = new AidlBinaryConstExpression(loc(@1), std::unique_ptr($1), ">" , std::unique_ptr($3)); } | const_expr LEQ const_expr { $$ = new AidlBinaryConstExpression(loc(@1), std::unique_ptr($1), "<=", std::unique_ptr($3)); } | const_expr GEQ const_expr { $$ = new AidlBinaryConstExpression(loc(@1), std::unique_ptr($1), ">=", std::unique_ptr($3)); } | const_expr LSHIFT const_expr { $$ = new AidlBinaryConstExpression(loc(@1), std::unique_ptr($1), "<<", std::unique_ptr($3)); } | const_expr RSHIFT const_expr { $$ = new AidlBinaryConstExpression(loc(@1), std::unique_ptr($1), ">>", std::unique_ptr($3)); } | const_expr '+' const_expr { $$ = new AidlBinaryConstExpression(loc(@1), std::unique_ptr($1), "+" , std::unique_ptr($3)); } | const_expr '-' const_expr { $$ = new AidlBinaryConstExpression(loc(@1), std::unique_ptr($1), "-" , std::unique_ptr($3)); } | const_expr '*' const_expr { $$ = new AidlBinaryConstExpression(loc(@1), std::unique_ptr($1), "*" , std::unique_ptr($3)); } | const_expr '/' const_expr { $$ = new AidlBinaryConstExpression(loc(@1), std::unique_ptr($1), "/" , std::unique_ptr($3)); } | const_expr '%' const_expr { $$ = new AidlBinaryConstExpression(loc(@1), std::unique_ptr($1), "%" , std::unique_ptr($3)); } | '+' const_expr %prec UNARY_PLUS { $$ = new AidlUnaryConstExpression(loc(@1), "+", std::unique_ptr($2)); } | '-' const_expr %prec UNARY_MINUS { $$ = new AidlUnaryConstExpression(loc(@1), "-", std::unique_ptr($2)); } | '!' const_expr { $$ = new AidlUnaryConstExpression(loc(@1), "!", std::unique_ptr($2)); } | '~' const_expr { $$ = new AidlUnaryConstExpression(loc(@1), "~", std::unique_ptr($2)); } | '(' const_expr ')' { $$ = $2; } | '(' error ')' { std::cerr << "ERROR: invalid const expression within parenthesis at " << @1 << ".\n"; ps->AddError(); // to avoid segfaults $$ = AidlConstantValue::Integral(loc(@1), "0"); } ; constant_value_list : /* empty */ { $$ = new std::vector>; } | constant_value_non_empty_list { $$ = $1; } | constant_value_non_empty_list ',' { $$ = $1; } ; constant_value_non_empty_list : const_expr { $$ = new std::vector>; $$->push_back(std::unique_ptr($1)); } | constant_value_non_empty_list ',' const_expr { $$ = $1; $$->push_back(std::unique_ptr($3)); } ; constant_decl : CONST type identifier '=' const_expr ';' { $2->SetComments($1->GetComments()); $$ = new AidlConstantDeclaration(loc(@3), $2, $3->GetText(), $5); delete $1; delete $3; } ; enumerator : identifier '=' const_expr { $$ = new AidlEnumerator(loc(@1), $1->GetText(), $3, $1->GetComments()); delete $1; } | identifier { $$ = new AidlEnumerator(loc(@1), $1->GetText(), nullptr, $1->GetComments()); delete $1; } ; enumerators : enumerator { $$ = new std::vector>(); $$->push_back(std::unique_ptr($1)); } | enumerators ',' enumerator { $1->push_back(std::unique_ptr($3)); $$ = $1; } ; enum_decl_body : '{' enumerators '}' { $$ = $2; } | '{' enumerators ',' '}' { $$ = $2; } ; enum_decl : ENUM identifier enum_decl_body { $$ = new AidlEnumDeclaration(loc(@2), $2->GetText(), $3, ps->Package(), $1->GetComments()); delete $1; delete $2; delete $3; } ; method_decl : type identifier '(' arg_list ')' ';' { $$ = new AidlMethod(loc(@2), false, $1, $2->GetText(), $4, $1->GetComments()); delete $2; } | annotation_list ONEWAY type identifier '(' arg_list ')' ';' { const std::string& comments = ($1->size() > 0) ? $1->begin()->GetComments() : $2->GetComments(); $$ = new AidlMethod(loc(@4), true, $3, $4->GetText(), $6, comments); $3->Annotate(std::move(*$1)); delete $1; delete $2; delete $4; } | type identifier '(' arg_list ')' '=' INTVALUE ';' { int32_t serial = 0; if (!android::base::ParseInt($7->GetText(), &serial)) { AIDL_ERROR(loc(@7)) << "Could not parse int value: " << $7->GetText(); ps->AddError(); } $$ = new AidlMethod(loc(@2), false, $1, $2->GetText(), $4, $1->GetComments(), serial); delete $2; delete $7; } | annotation_list ONEWAY type identifier '(' arg_list ')' '=' INTVALUE ';' { const std::string& comments = ($1->size() > 0) ? $1->begin()->GetComments() : $2->GetComments(); int32_t serial = 0; if (!android::base::ParseInt($9->GetText(), &serial)) { AIDL_ERROR(loc(@9)) << "Could not parse int value: " << $9->GetText(); ps->AddError(); } $$ = new AidlMethod(loc(@4), true, $3, $4->GetText(), $6, comments, serial); $3->Annotate(std::move(*$1)); delete $1; delete $2; delete $4; delete $9; }; arg_non_empty_list : arg { $$ = new std::vector>(); $$->push_back(std::unique_ptr($1)); } | arg_non_empty_list ',' arg { $$ = $1; $$->push_back(std::unique_ptr($3)); }; arg_list : /*empty*/ { $$ = new std::vector>(); } | arg_non_empty_list { $$ = $1; } ; arg : direction type identifier { $$ = new AidlArgument(loc(@3), $1, $2, $3->GetText()); delete $3; } | type identifier { $$ = new AidlArgument(loc(@2), $1, $2->GetText()); delete $2; } ; unannotated_type : qualified_name { $$ = new AidlTypeSpecifier(loc(@1), $1->GetText(), false, nullptr, $1->GetComments()); ps->DeferResolution($$); delete $1; } | qualified_name '[' ']' { $$ = new AidlTypeSpecifier(loc(@1), $1->GetText(), true, nullptr, $1->GetComments()); ps->DeferResolution($$); delete $1; } | qualified_name '<' type_args '>' { $$ = new AidlTypeSpecifier(loc(@1), $1->GetText(), false, $3, $1->GetComments()); ps->DeferResolution($$); delete $1; }; type : annotation_list unannotated_type { $$ = $2; if ($1->size() > 0) { // copy comments from annotation to type $2->SetComments($1->begin()->GetComments()); } $2->Annotate(std::move(*$1)); delete $1; }; type_args : unannotated_type { $$ = new std::vector>(); $$->emplace_back($1); } | type_args ',' unannotated_type { $1->emplace_back($3); $$ = $1; }; annotation_list : { $$ = new std::vector(); } | annotation_list annotation { if ($2 != nullptr) { $1->emplace_back(std::move(*$2)); delete $2; } $$ = $1; }; parameter : identifier '=' const_expr { $$ = new AidlAnnotationParameter{$1->GetText(), std::unique_ptr($3)}; delete $1; }; parameter_list : /*empty*/{ $$ = new std::map>(); } | parameter_non_empty_list { $$ = $1; }; parameter_non_empty_list : parameter { $$ = new std::map>(); $$->emplace(std::move($1->name), $1->value.release()); delete $1; } | parameter_non_empty_list ',' parameter { $$ = $1; if ($$->find($3->name) != $$->end()) { AIDL_ERROR(loc(@3)) << "Trying to redefine parameter " << $3->name << "."; ps->AddError(); } $$->emplace(std::move($3->name), std::move($3->value)); delete $3; }; annotation : ANNOTATION { $$ = AidlAnnotation::Parse(loc(@1), $1->GetText(), nullptr); if ($$) { $$->SetComments($1->GetComments()); } else { ps->AddError(); } delete $1; }; | ANNOTATION '(' parameter_list ')' { $$ = AidlAnnotation::Parse(loc(@1, @4), $1->GetText(), $3); if ($$) { $$->SetComments($1->GetComments()); } else { ps->AddError(); } delete $1; delete $3; } direction : IN { $$ = AidlArgument::IN_DIR; } | OUT { $$ = AidlArgument::OUT_DIR; } | INOUT { $$ = AidlArgument::INOUT_DIR; }; %% #include #include void yy::parser::error(const yy::parser::location_type& l, const std::string& errstr) { AIDL_ERROR(loc(l)) << errstr; // parser will return error value }