123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107 |
- /**
- * Tencent is pleased to support the open source community by making Tars available.
- *
- * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
- *
- * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
- * in compliance with the License. You may obtain a copy of the License at
- *
- * https://opensource.org/licenses/BSD-3-Clause
- *
- * 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 <iostream>
- #include <memory>
- #include <cassert>
- using namespace std;
- #define YYSTYPE GrammarBasePtr
- #include "parse.h"
- #define YYDEBUG 1
- #define YYINITDEPTH 10000
- %}
- %defines
- %debug
- //keyword token
- %token TARS_VOID
- %token TARS_STRUCT
- %token TARS_BOOL
- %token TARS_BYTE
- %token TARS_SHORT
- %token TARS_INT
- %token TARS_DOUBLE
- %token TARS_FLOAT
- %token TARS_LONG
- %token TARS_STRING
- %token TARS_VECTOR
- %token TARS_MAP
- %token TARS_NAMESPACE
- %token TARS_INTERFACE
- %token TARS_IDENTIFIER
- %token TARS_OUT
- %token TARS_OP
- %token TARS_KEY
- %token TARS_ROUTE_KEY
- %token TARS_REQUIRE
- %token TARS_OPTIONAL
- %token TARS_CONST_INTEGER
- %token TARS_CONST_FLOAT
- %token TARS_FALSE
- %token TARS_TRUE
- %token TARS_STRING_LITERAL
- %token TARS_SCOPE_DELIMITER
- %token TARS_CONST
- %token TARS_ENUM
- %token TARS_UNSIGNED
- %token BAD_CHAR
- %%
- start: definitions
- ;
- // ----------------------------------------------------------------------
- definitions
- // ----------------------------------------------------------------------
- : definition
- {
- }
- ';' definitions
- | error ';'
- {
- yyerrok;
- }
- definitions
- | definition
- {
- g_parse->error("`;' missing after definition");
- }
- |
- {
- }
- ;
- // ----------------------------------------------------------------------
- definition
- // ----------------------------------------------------------------------
- : namespace_def
- {
- assert($1 == 0 || NamespacePtr::dynamicCast($1));
- }
- | interface_def
- {
- assert($1 == 0 || InterfacePtr::dynamicCast($1));
- }
- | struct_def
- {
- assert($1 == 0 || StructPtr::dynamicCast($1));
- }
- | key_def
- {
- }
- | enum_def
- {
- assert($1 == 0 || EnumPtr::dynamicCast($1));
- }
- | const_def
- {
- assert($1 == 0 || ConstPtr::dynamicCast($1));
- }
- ;
- // ----------------------------------------------------------------------
- enum_def
- // ----------------------------------------------------------------------
- : enum_id
- {
- $$ = $1;
- }
- '{' enumerator_list '}'
- {
- if($3)
- {
- g_parse->popContainer();
- $$ = $3;
- }
- else
- {
- $$ = 0;
- }
- $$ = $2;
- }
- ;
- // ----------------------------------------------------------------------
- enum_id
- // ----------------------------------------------------------------------
- : TARS_ENUM TARS_IDENTIFIER
- {
- NamespacePtr c = NamespacePtr::dynamicCast(g_parse->currentContainer());
- if(!c)
- {
- g_parse->error("enum must define in namespace");
- }
- StringGrammarPtr ident = StringGrammarPtr::dynamicCast($2);
- EnumPtr e = c->createEnum(ident->v);
- g_parse->pushContainer(e);
- $$ = e;
- }
- | TARS_ENUM keyword
- {
- StringGrammarPtr ident = StringGrammarPtr::dynamicCast($2);
- g_parse->error("keyword `" + ident->v + "' cannot be used as enumeration name");
- $$ = $2;
- }
- ;
- // ----------------------------------------------------------------------
- enumerator_list
- // ----------------------------------------------------------------------
- : enumerator ',' enumerator_list
- {
- $$ = $2;
- }
- | enumerator
- {
- }
- ;
- // ----------------------------------------------------------------------
- enumerator
- // ----------------------------------------------------------------------
- : TARS_IDENTIFIER
- {
- TypePtr type = TypePtr::dynamicCast(g_parse->createBuiltin(Builtin::KindLong));
- StringGrammarPtr ident = StringGrammarPtr::dynamicCast($1);
- TypeIdPtr tPtr = new TypeId(type, ident->v);
- tPtr->disableDefault();
- EnumPtr e = EnumPtr::dynamicCast(g_parse->currentContainer());
- assert(e);
- e->addMember(tPtr);
- $$ = e;
- }
- | keyword
- {
- StringGrammarPtr ident = StringGrammarPtr::dynamicCast($1);
- g_parse->error("keyword `" + ident->v + "' cannot be used as enumerator");
- }
- | TARS_IDENTIFIER '=' const_initializer
- {
- TypePtr type = TypePtr::dynamicCast(g_parse->createBuiltin(Builtin::KindLong));
- StringGrammarPtr ident = StringGrammarPtr::dynamicCast($1);
- TypeIdPtr tPtr = new TypeId(type, ident->v);
- ConstGrammarPtr sPtr = ConstGrammarPtr::dynamicCast($3);
- g_parse->checkConstValue(tPtr, sPtr->t);
- tPtr->setDefault(sPtr->v);
- EnumPtr e = EnumPtr::dynamicCast(g_parse->currentContainer());
- assert(e);
- e->addMember(tPtr);
- $$ = e;
- }
- |
- {
- }
- ;
- // ----------------------------------------------------------------------
- namespace_def
- // ----------------------------------------------------------------------
- : TARS_NAMESPACE TARS_IDENTIFIER
- {
- StringGrammarPtr ident = StringGrammarPtr::dynamicCast($2);
- ContainerPtr c = g_parse->currentContainer();
- NamespacePtr n = c->createNamespace(ident->v);
- if(n)
- {
- g_parse->pushContainer(n);
- $$ = GrammarBasePtr::dynamicCast(n);
- }
- else
- {
- $$ = 0;
- }
- }
- '{' definitions '}'
- {
- if($3)
- {
- g_parse->popContainer();
- $$ = $3;
- }
- else
- {
- $$ = 0;
- }
- }
- ;
- //key------------------------------------------------------
- key_def
- // ----------------------------------------------------------------------
- : TARS_KEY '[' scoped_name ','
- {
- StringGrammarPtr ident = StringGrammarPtr::dynamicCast($3);
- StructPtr sp = StructPtr::dynamicCast(g_parse->findUserType(ident->v));
- if(!sp)
- {
- g_parse->error("struct '" + ident->v + "' undefined!");
- }
- g_parse->setKeyStruct(sp);
- }
- key_members ']'
- {
- }
- ;
- //key------------------------------------------------------
- key_members
- // ----------------------------------------------------------------------
- : TARS_IDENTIFIER
- {
- StringGrammarPtr ident = StringGrammarPtr::dynamicCast($1);
- StructPtr np = g_parse->getKeyStruct();
- if(np)
- {
- np->addKey(ident->v);
- }
- else
- {
- $$ = 0;
- }
- }
- | key_members ',' TARS_IDENTIFIER
- {
- StringGrammarPtr ident = StringGrammarPtr::dynamicCast($3);
- StructPtr np = g_parse->getKeyStruct();
- if(np)
- {
- np->addKey(ident->v);
- }
- else
- {
- $$ = 0;
- }
- }
- ;
- // ----------------------------------------------------------------------
- interface_def
- // ----------------------------------------------------------------------
- : interface_id
- {
- StringGrammarPtr ident = StringGrammarPtr::dynamicCast($1);
- NamespacePtr c = NamespacePtr::dynamicCast(g_parse->currentContainer());
- InterfacePtr cl = c->createInterface(ident->v);
- if(cl)
- {
- g_parse->pushContainer(cl);
- $$ = GrammarBasePtr::dynamicCast(cl);
- }
- else
- {
- $$ = 0;
- }
- }
- '{' interface_exports '}'
- {
- if($2)
- {
- g_parse->popContainer();
- $$ = GrammarBasePtr::dynamicCast($2);
- }
- else
- {
- $$ = 0;
- }
- }
- ;
- // ----------------------------------------------------------------------
- interface_id
- // ----------------------------------------------------------------------
- : TARS_INTERFACE TARS_IDENTIFIER
- {
- $$ = $2;
- }
- | TARS_INTERFACE keyword
- {
- StringGrammarPtr ident = StringGrammarPtr::dynamicCast($2);
- g_parse->error("keyword `" + ident->v + "' cannot be used as interface name");
- $$ = $2;
- }
- ;
- // ----------------------------------------------------------------------
- interface_exports
- // ----------------------------------------------------------------------
- : interface_export ';' interface_exports
- {
- }
- | error ';' interface_exports
- {
- }
- | interface_export
- {
- g_parse->error("`;' missing after definition");
- }
- |
- {
- }
- ;
- // ----------------------------------------------------------------------
- interface_export
- // ----------------------------------------------------------------------
- : operation
- ;
- // ----------------------------------------------------------------------
- operation
- // ----------------------------------------------------------------------
- : operation_preamble parameters ')'
- {
- if($1)
- {
- g_parse->popContainer();
- $$ = GrammarBasePtr::dynamicCast($1);
- }
- else
- {
- $$ = 0;
- }
- }
- ;
- // ----------------------------------------------------------------------
- operation_preamble
- // ----------------------------------------------------------------------
- : return_type TARS_OP
- {
- TypePtr returnType = TypePtr::dynamicCast($1);
- StringGrammarPtr ident = StringGrammarPtr::dynamicCast($2);
- string name = ident->v;
- InterfacePtr cl = InterfacePtr::dynamicCast(g_parse->currentContainer());
- if(cl)
- {
- OperationPtr op = cl->createOperation(name, returnType);
- if(op)
- {
- g_parse->pushContainer(op);
- $$ = GrammarBasePtr::dynamicCast(op);
- }
- else
- {
- $$ = 0;
- }
- }
- else
- {
- $$ = 0;
- }
- }
- ;
- // ----------------------------------------------------------------------
- return_type
- // ----------------------------------------------------------------------
- : type
- | TARS_VOID
- {
- $$ = 0;
- }
- ;
- // ----------------------------------------------------------------------
- parameters
- // ----------------------------------------------------------------------
- : // empty
- {
- }
- | type_id
- {
- TypeIdPtr tsp = TypeIdPtr::dynamicCast($1);
- OperationPtr op = OperationPtr::dynamicCast(g_parse->currentContainer());
- assert(op);
- if(op)
- {
- op->createParamDecl(tsp, false, false);
- }
- }
- | parameters ',' type_id
- {
- TypeIdPtr tsp = TypeIdPtr::dynamicCast($3);
- OperationPtr op = OperationPtr::dynamicCast(g_parse->currentContainer());
- assert(op);
- if(op)
- {
- op->createParamDecl(tsp, false, false);
- }
- }
- | out_qualifier type_id
- {
- BoolGrammarPtr isOutParam = BoolGrammarPtr::dynamicCast($1);
- TypeIdPtr tsp = TypeIdPtr::dynamicCast($2);
- OperationPtr op = OperationPtr::dynamicCast(g_parse->currentContainer());
- assert(op);
- if(op)
- {
- op->createParamDecl(tsp, isOutParam->v, false);
- }
- }
- | parameters ',' out_qualifier type_id
- {
- BoolGrammarPtr isOutParam = BoolGrammarPtr::dynamicCast($3);
- TypeIdPtr tsp = TypeIdPtr::dynamicCast($4);
- OperationPtr op = OperationPtr::dynamicCast(g_parse->currentContainer());
- assert(op);
- if(op)
- {
- op->createParamDecl(tsp, isOutParam->v, false);
- }
- }
- | routekey_qualifier type_id
- {
- BoolGrammarPtr isRouteKeyParam = BoolGrammarPtr::dynamicCast($1);
- TypeIdPtr tsp = TypeIdPtr::dynamicCast($2);
- OperationPtr op = OperationPtr::dynamicCast(g_parse->currentContainer());
- assert(op);
- if(op)
- {
- op->createParamDecl(tsp, false, isRouteKeyParam->v);
- }
- }
- | parameters ',' routekey_qualifier type_id
- {
- BoolGrammarPtr isRouteKeyParam = BoolGrammarPtr::dynamicCast($3);
- TypeIdPtr tsp = TypeIdPtr::dynamicCast($4);
- OperationPtr op = OperationPtr::dynamicCast(g_parse->currentContainer());
- assert(op);
- if(op)
- {
- op->createParamDecl(tsp, false, isRouteKeyParam->v);
- }
- }
- | out_qualifier
- {
- g_parse->error("'out' must be defined with a type");
- }
- | routekey_qualifier
- {
- g_parse->error("'routekey' must be defined with a type");
- }
- ;
- // ----------------------------------------------------------------------
- routekey_qualifier
- // ----------------------------------------------------------------------
- : TARS_ROUTE_KEY
- {
- BoolGrammarPtr routekey = new BoolGrammar;
- routekey->v = true;
- $$ = GrammarBasePtr::dynamicCast(routekey);
- }
- ;
- // ----------------------------------------------------------------------
- out_qualifier
- // ----------------------------------------------------------------------
- : TARS_OUT
- {
- BoolGrammarPtr out = new BoolGrammar;
- out->v = true;
- $$ = GrammarBasePtr::dynamicCast(out);
- }
- ;
- // struct--------------------------------------------------------------
- struct_def
- // ----------------------------------------------------------------------
- : struct_id
- {
- StringGrammarPtr ident = StringGrammarPtr::dynamicCast($1);
- NamespacePtr np = NamespacePtr::dynamicCast(g_parse->currentContainer());
- if(np)
- {
- StructPtr sp = np->createStruct(ident->v);
- if(sp)
- {
- g_parse->pushContainer(sp);
- $$ = GrammarBasePtr::dynamicCast(sp);
- }
- else
- {
- $$ = 0;
- }
- }
- else
- {
- g_parse->error("struct '" + ident->v + "' must definition in namespace");
- }
- }
- '{' struct_exports '}'
- {
- if($2)
- {
- g_parse->popContainer();
- }
- $$ = $2;
- StructPtr st = StructPtr::dynamicCast($$);
- assert(st);
- if(st->getAllMemberPtr().size() == 0)
- {
- g_parse->error("struct `" + st->getSid() + "' must have at least one member");
- }
- }
- ;
- // struct name----------------------------------------------------------
- struct_id
- // ----------------------------------------------------------------------
- : TARS_STRUCT TARS_IDENTIFIER
- {
- $$ = $2;
- }
- | TARS_STRUCT keyword
- {
- StringGrammarPtr ident = StringGrammarPtr::dynamicCast($2);
- g_parse->error("keyword `" + ident->v + "' cannot be used as struct name");
- }
- | TARS_STRUCT error
- {
- g_parse->error("abstract declarator '<anonymous struct>' used as declaration");
- }
- ;
- // struct members------------------------------------------------------
- struct_exports
- // ----------------------------------------------------------------------
- : data_member ';' struct_exports
- {
- }
- | data_member
- {
- g_parse->error("';' missing after definition");
- }
- |
- {
- }
- ;
- //
- //
- // data member--------------------------------------------------------------
- data_member
- // ----------------------------------------------------------------------
- : struct_type_id
- {
- $$ = GrammarBasePtr::dynamicCast($1);
- }
- ;
- // struct member id--------------------------------------------------------------
- struct_type_id
- // ----------------------------------------------------------------------
- : TARS_CONST_INTEGER TARS_REQUIRE type_id
- {
- StructPtr np = StructPtr::dynamicCast(g_parse->currentContainer());
- if(np)
- {
- IntergerGrammarPtr iPtr = IntergerGrammarPtr::dynamicCast($1);
- g_parse->checkTag(iPtr->v);
- TypeIdPtr tPtr = TypeIdPtr::dynamicCast($3);
- tPtr->setRequire(iPtr->v);
- np->addTypeId(tPtr);
- $$ = GrammarBasePtr::dynamicCast($3);
- }
- else
- {
- $$ = 0;
- }
- }
- | TARS_CONST_INTEGER TARS_REQUIRE type_id '=' const_initializer
- {
- StructPtr np = StructPtr::dynamicCast(g_parse->currentContainer());
- if(np)
- {
- IntergerGrammarPtr iPtr = IntergerGrammarPtr::dynamicCast($1);
- g_parse->checkTag(iPtr->v);
- TypeIdPtr tPtr = TypeIdPtr::dynamicCast($3);
- ConstGrammarPtr sPtr = ConstGrammarPtr::dynamicCast($5);
- g_parse->checkConstValue(tPtr, sPtr->t);
- tPtr->setRequire(iPtr->v);
- tPtr->setDefault(sPtr->v);
- np->addTypeId(tPtr);
- $$ = GrammarBasePtr::dynamicCast($3);
- }
- else
- {
- $$ = 0;
- }
- }
- | TARS_CONST_INTEGER TARS_OPTIONAL type_id '=' const_initializer
- {
- StructPtr np = StructPtr::dynamicCast(g_parse->currentContainer());
- if(np)
- {
- IntergerGrammarPtr iPtr = IntergerGrammarPtr::dynamicCast($1);
- g_parse->checkTag(iPtr->v);
- TypeIdPtr tPtr = TypeIdPtr::dynamicCast($3);
- ConstGrammarPtr sPtr = ConstGrammarPtr::dynamicCast($5);
- g_parse->checkConstValue(tPtr, sPtr->t);
- tPtr->setOptional(iPtr->v);
- tPtr->setDefault(sPtr->v);
- np->addTypeId(tPtr);
- $$ = GrammarBasePtr::dynamicCast($3);
- }
- else
- {
- $$ = 0;
- }
- }
- | TARS_CONST_INTEGER TARS_OPTIONAL type_id
- {
- StructPtr np = StructPtr::dynamicCast(g_parse->currentContainer());
- if(np)
- {
- IntergerGrammarPtr iPtr = IntergerGrammarPtr::dynamicCast($1);
- g_parse->checkTag(iPtr->v);
- TypeIdPtr tPtr = TypeIdPtr::dynamicCast($3);
- tPtr->setOptional(iPtr->v);
- np->addTypeId(tPtr);
- $$ = GrammarBasePtr::dynamicCast($3);
- }
- else
- {
- $$ = 0;
- }
- }
- | TARS_REQUIRE type_id
- {
- g_parse->error("struct member need 'tag'");
- }
- | TARS_OPTIONAL type_id
- {
- g_parse->error("struct member need 'tag'");
- }
- | TARS_CONST_INTEGER type_id
- {
- g_parse->error("struct member need 'require' or 'optional'");
- }
- | type_id
- {
- g_parse->error("struct member need 'tag' or 'require' or 'optional'");
- }
- ;
- // ----------------------------------------------------------------------
- const_initializer
- // ----------------------------------------------------------------------
- : TARS_CONST_INTEGER
- {
- IntergerGrammarPtr intVal = IntergerGrammarPtr::dynamicCast($1);
- ostringstream sstr;
- sstr << intVal->v;
- ConstGrammarPtr c = new ConstGrammar();
- c->t = ConstGrammar::VALUE;
- c->v = sstr.str();
- $$ = c;
- }
- | TARS_CONST_FLOAT
- {
- FloatGrammarPtr floatVal = FloatGrammarPtr::dynamicCast($1);
- ostringstream sstr;
- sstr << floatVal->v;
- ConstGrammarPtr c = new ConstGrammar();
- c->t = ConstGrammar::VALUE;
- c->v = sstr.str();
- $$ = c;
- }
- | TARS_STRING_LITERAL
- {
- StringGrammarPtr ident = StringGrammarPtr::dynamicCast($1);
- ConstGrammarPtr c = new ConstGrammar();
- c->t = ConstGrammar::STRING;
- c->v = ident->v;
- $$ = c;
- }
- | TARS_FALSE
- {
- StringGrammarPtr ident = StringGrammarPtr::dynamicCast($1);
- ConstGrammarPtr c = new ConstGrammar();
- c->t = ConstGrammar::BOOL;
- c->v = ident->v;
- $$ = c;
- }
- | TARS_TRUE
- {
- StringGrammarPtr ident = StringGrammarPtr::dynamicCast($1);
- ConstGrammarPtr c = new ConstGrammar();
- c->t = ConstGrammar::BOOL;
- c->v = ident->v;
- $$ = c;
- }
- | TARS_IDENTIFIER
- {
- StringGrammarPtr ident = StringGrammarPtr::dynamicCast($1);
- if (g_parse->checkEnum(ident->v) == false)
- {
- g_parse->error("error enum default value, not defined yet");
- }
- ConstGrammarPtr c = new ConstGrammar();
- c->t = ConstGrammar::ENUM;
- c->v = ident->v;
- $$ = c;
- }
- | scoped_name TARS_SCOPE_DELIMITER TARS_IDENTIFIER
- {
- StringGrammarPtr scoped = StringGrammarPtr::dynamicCast($1);
- StringGrammarPtr ident = StringGrammarPtr::dynamicCast($3);
-
- if (g_parse->checkEnum(ident->v) == false)
- {
- g_parse->error("error enum default value, not defined yet");
- }
- ConstGrammarPtr c = new ConstGrammar();
- c->t = ConstGrammar::ENUM;
- c->v = scoped->v + "::" + ident->v;
- $$ = c;
- }
- ;
- // const--------------------------------------------------------------
- const_def
- // ----------------------------------------------------------------------
- : TARS_CONST type_id '=' const_initializer
- {
- NamespacePtr np = NamespacePtr::dynamicCast(g_parse->currentContainer());
- if(!np)
- {
- g_parse->error("const type must define in namespace");
- }
- TypeIdPtr t = TypeIdPtr::dynamicCast($2);
- ConstGrammarPtr c = ConstGrammarPtr::dynamicCast($4);
- ConstPtr cPtr = np->createConst(t, c);
- $$ = cPtr;
- }
- ;
- // type--------------------------------------------------------------
- type_id
- // ----------------------------------------------------------------------
- : type TARS_IDENTIFIER
- {
- TypePtr type = TypePtr::dynamicCast($1);
- StringGrammarPtr ident = StringGrammarPtr::dynamicCast($2);
- TypeIdPtr typeIdPtr = new TypeId(type, ident->v);
- $$ = GrammarBasePtr::dynamicCast(typeIdPtr);
- }
- |type TARS_IDENTIFIER '[' TARS_CONST_INTEGER ']'
- {
- TypePtr type = g_parse->createVector(TypePtr::dynamicCast($1));
- IntergerGrammarPtr iPtrSize = IntergerGrammarPtr::dynamicCast($4);
- g_parse->checkArrayVaid(type,iPtrSize->v);
- type->setArray(iPtrSize->v);
- StringGrammarPtr ident = StringGrammarPtr::dynamicCast($2);
- TypeIdPtr typeIdPtr = new TypeId(type, ident->v);
- $$ = GrammarBasePtr::dynamicCast(typeIdPtr);
- }
- |type '*' TARS_IDENTIFIER
- {
- TypePtr type = g_parse->createVector(TypePtr::dynamicCast($1));
- //IntergerGrammarPtr iPtrSize = IntergerGrammarPtr::dynamicCast($4);
- g_parse->checkPointerVaid(type);
- type->setPointer(true);
- StringGrammarPtr ident = StringGrammarPtr::dynamicCast($3);
- TypeIdPtr typeIdPtr = new TypeId(type, ident->v);
- $$ = GrammarBasePtr::dynamicCast(typeIdPtr);
- }
- |type TARS_IDENTIFIER ':' TARS_CONST_INTEGER
- {
- TypePtr type = TypePtr::dynamicCast($1);
- StringGrammarPtr ident = StringGrammarPtr::dynamicCast($2);
- TypeIdPtr typeIdPtr = new TypeId(type, ident->v);
- IntergerGrammarPtr iPtrSize = IntergerGrammarPtr::dynamicCast($3);
- g_parse->checkArrayVaid(type,iPtrSize->v);
- $$ = GrammarBasePtr::dynamicCast(typeIdPtr);
- }
- | type keyword
- {
- StringGrammarPtr ident = StringGrammarPtr::dynamicCast($2);
- g_parse->error("keyword `" + ident->v + "' cannot be used as data member name");
- }
- | type
- {
- g_parse->error("missing data member name");
- }
- | error
- {
- g_parse->error("unkown type");
- }
- ;
- // type------------------------------------------------------------------
- type
- // ----------------------------------------------------------------------
- : type_no ':' TARS_CONST_INTEGER
- {
- TypePtr type = TypePtr::dynamicCast($1);
- IntergerGrammarPtr iPtrSize = IntergerGrammarPtr::dynamicCast($3);
- g_parse->checkArrayVaid(type,iPtrSize->v);
- type->setArray(iPtrSize->v);
- $$ = type;
- }
- | type_no
- {
- $$ = $1;
- }
- | type_no ':' error
- {
- g_parse->error("array missing size");
- }
- ;
- // type------------------------------------------------------------------
- type_no
- // ----------------------------------------------------------------------
- : TARS_BOOL
- {
- $$ = g_parse->createBuiltin(Builtin::KindBool);
- }
- | TARS_BYTE
- {
- $$ = g_parse->createBuiltin(Builtin::KindByte);
- }
- | TARS_UNSIGNED TARS_BYTE //unsigned char -> short
- {
- $$ = g_parse->createBuiltin(Builtin::KindShort,true);
- }
- | TARS_SHORT
- {
- $$ = g_parse->createBuiltin(Builtin::KindShort);
- }
- | TARS_UNSIGNED TARS_SHORT
- {
- $$ = g_parse->createBuiltin(Builtin::KindInt,true);
- }
- | TARS_INT
- {
- $$ = g_parse->createBuiltin(Builtin::KindInt);
- }
- | TARS_UNSIGNED TARS_INT
- {
- $$ = g_parse->createBuiltin(Builtin::KindLong,true);
- }
- | TARS_LONG
- {
- $$ = g_parse->createBuiltin(Builtin::KindLong);
- }
- | TARS_FLOAT
- {
- $$ = g_parse->createBuiltin(Builtin::KindFloat);
- }
- | TARS_DOUBLE
- {
- $$ = g_parse->createBuiltin(Builtin::KindDouble);
- }
- | TARS_STRING
- {
- $$ = g_parse->createBuiltin(Builtin::KindString);
- }
- | vector
- {
- $$ = GrammarBasePtr::dynamicCast($1);
- }
- | map
- {
- $$ = GrammarBasePtr::dynamicCast($1);
- }
- | scoped_name
- {
- StringGrammarPtr ident = StringGrammarPtr::dynamicCast($1);
- TypePtr sp = g_parse->findUserType(ident->v);
- if(sp)
- {
- $$ = GrammarBasePtr::dynamicCast(sp);
- }
- else
- {
- g_parse->error("'" + ident->v + "' undefined!");
- }
- }
- ;
- // vector----------------------------------------------------------------
- vector
- // ----------------------------------------------------------------------
- : TARS_VECTOR '<' type '>'
- {
- $$ = GrammarBasePtr::dynamicCast(g_parse->createVector(TypePtr::dynamicCast($3)));
- }
- | TARS_VECTOR '<' error
- {
- g_parse->error("vector error");
- }
- | TARS_VECTOR '<' type error
- {
- g_parse->error("vector missing '>'");
- }
- | TARS_VECTOR error
- {
- g_parse->error("vector missing type");
- }
- ;
- // map----------------------------------------------------------------
- map
- // ----------------------------------------------------------------------
- : TARS_MAP '<' type ',' type '>'
- {
- $$ = GrammarBasePtr::dynamicCast(g_parse->createMap(TypePtr::dynamicCast($3), TypePtr::dynamicCast($5)));
- }
- | TARS_MAP '<' error
- {
- g_parse->error("map error");
- }
- ;
- // ----------------------------------------------------------------------
- scoped_name
- // ----------------------------------------------------------------------
- : TARS_IDENTIFIER
- {
- }
- | TARS_SCOPE_DELIMITER TARS_IDENTIFIER
- {
- StringGrammarPtr ident = StringGrammarPtr::dynamicCast($2);
- ident->v = "::" + ident->v;
- $$ = GrammarBasePtr::dynamicCast(ident);
- }
- | scoped_name TARS_SCOPE_DELIMITER TARS_IDENTIFIER
- {
- StringGrammarPtr scoped = StringGrammarPtr::dynamicCast($1);
- StringGrammarPtr ident = StringGrammarPtr::dynamicCast($3);
- scoped->v += "::";
- scoped->v += ident->v;
- $$ = GrammarBasePtr::dynamicCast(scoped);
- }
- ;
- // key----------------------------------------------------------------
- keyword
- // ----------------------------------------------------------------------
- : TARS_STRUCT
- {
- }
- | TARS_VOID
- {
- }
- | TARS_BOOL
- {
- }
- | TARS_BYTE
- {
- }
- | TARS_SHORT
- {
- }
- | TARS_INT
- {
- }
- | TARS_FLOAT
- {
- }
- | TARS_DOUBLE
- {
- }
- | TARS_STRING
- {
- }
- | TARS_VECTOR
- {
- }
- | TARS_KEY
- {
- }
- | TARS_MAP
- {
- }
- | TARS_NAMESPACE
- {
- }
- | TARS_INTERFACE
- {
- }
- | TARS_OUT
- {
- }
- | TARS_REQUIRE
- {
- }
- | TARS_OPTIONAL
- {
- }
- | TARS_CONST_INTEGER
- {
- }
- | TARS_CONST_FLOAT
- {
- }
- | TARS_FALSE
- {
- }
- | TARS_TRUE
- {
- }
- | TARS_STRING_LITERAL
- {
- }
- | TARS_CONST
- {
- }
- | TARS_ENUM
- {
- }
- | TARS_UNSIGNED
- {
- }
- ;
- %%
|