tars.y 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107
  1. /**
  2. * Tencent is pleased to support the open source community by making Tars available.
  3. *
  4. * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
  5. *
  6. * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  7. * in compliance with the License. You may obtain a copy of the License at
  8. *
  9. * https://opensource.org/licenses/BSD-3-Clause
  10. *
  11. * Unless required by applicable law or agreed to in writing, software distributed
  12. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  13. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  14. * specific language governing permissions and limitations under the License.
  15. */
  16. %{
  17. #include <iostream>
  18. #include <memory>
  19. #include <cassert>
  20. using namespace std;
  21. #define YYSTYPE GrammarBasePtr
  22. #include "parse.h"
  23. #define YYDEBUG 1
  24. #define YYINITDEPTH 10000
  25. %}
  26. %defines
  27. %debug
  28. //keyword token
  29. %token TARS_VOID
  30. %token TARS_STRUCT
  31. %token TARS_BOOL
  32. %token TARS_BYTE
  33. %token TARS_SHORT
  34. %token TARS_INT
  35. %token TARS_DOUBLE
  36. %token TARS_FLOAT
  37. %token TARS_LONG
  38. %token TARS_STRING
  39. %token TARS_VECTOR
  40. %token TARS_MAP
  41. %token TARS_NAMESPACE
  42. %token TARS_INTERFACE
  43. %token TARS_IDENTIFIER
  44. %token TARS_OUT
  45. %token TARS_OP
  46. %token TARS_KEY
  47. %token TARS_ROUTE_KEY
  48. %token TARS_REQUIRE
  49. %token TARS_OPTIONAL
  50. %token TARS_CONST_INTEGER
  51. %token TARS_CONST_FLOAT
  52. %token TARS_FALSE
  53. %token TARS_TRUE
  54. %token TARS_STRING_LITERAL
  55. %token TARS_SCOPE_DELIMITER
  56. %token TARS_CONST
  57. %token TARS_ENUM
  58. %token TARS_UNSIGNED
  59. %token BAD_CHAR
  60. %%
  61. start: definitions
  62. ;
  63. // ----------------------------------------------------------------------
  64. definitions
  65. // ----------------------------------------------------------------------
  66. : definition
  67. {
  68. }
  69. ';' definitions
  70. | error ';'
  71. {
  72. yyerrok;
  73. }
  74. definitions
  75. | definition
  76. {
  77. g_parse->error("`;' missing after definition");
  78. }
  79. |
  80. {
  81. }
  82. ;
  83. // ----------------------------------------------------------------------
  84. definition
  85. // ----------------------------------------------------------------------
  86. : namespace_def
  87. {
  88. assert($1 == 0 || NamespacePtr::dynamicCast($1));
  89. }
  90. | interface_def
  91. {
  92. assert($1 == 0 || InterfacePtr::dynamicCast($1));
  93. }
  94. | struct_def
  95. {
  96. assert($1 == 0 || StructPtr::dynamicCast($1));
  97. }
  98. | key_def
  99. {
  100. }
  101. | enum_def
  102. {
  103. assert($1 == 0 || EnumPtr::dynamicCast($1));
  104. }
  105. | const_def
  106. {
  107. assert($1 == 0 || ConstPtr::dynamicCast($1));
  108. }
  109. ;
  110. // ----------------------------------------------------------------------
  111. enum_def
  112. // ----------------------------------------------------------------------
  113. : enum_id
  114. {
  115. $$ = $1;
  116. }
  117. '{' enumerator_list '}'
  118. {
  119. if($3)
  120. {
  121. g_parse->popContainer();
  122. $$ = $3;
  123. }
  124. else
  125. {
  126. $$ = 0;
  127. }
  128. $$ = $2;
  129. }
  130. ;
  131. // ----------------------------------------------------------------------
  132. enum_id
  133. // ----------------------------------------------------------------------
  134. : TARS_ENUM TARS_IDENTIFIER
  135. {
  136. NamespacePtr c = NamespacePtr::dynamicCast(g_parse->currentContainer());
  137. if(!c)
  138. {
  139. g_parse->error("enum must define in namespace");
  140. }
  141. StringGrammarPtr ident = StringGrammarPtr::dynamicCast($2);
  142. EnumPtr e = c->createEnum(ident->v);
  143. g_parse->pushContainer(e);
  144. $$ = e;
  145. }
  146. | TARS_ENUM keyword
  147. {
  148. StringGrammarPtr ident = StringGrammarPtr::dynamicCast($2);
  149. g_parse->error("keyword `" + ident->v + "' cannot be used as enumeration name");
  150. $$ = $2;
  151. }
  152. ;
  153. // ----------------------------------------------------------------------
  154. enumerator_list
  155. // ----------------------------------------------------------------------
  156. : enumerator ',' enumerator_list
  157. {
  158. $$ = $2;
  159. }
  160. | enumerator
  161. {
  162. }
  163. ;
  164. // ----------------------------------------------------------------------
  165. enumerator
  166. // ----------------------------------------------------------------------
  167. : TARS_IDENTIFIER
  168. {
  169. TypePtr type = TypePtr::dynamicCast(g_parse->createBuiltin(Builtin::KindLong));
  170. StringGrammarPtr ident = StringGrammarPtr::dynamicCast($1);
  171. TypeIdPtr tPtr = new TypeId(type, ident->v);
  172. tPtr->disableDefault();
  173. EnumPtr e = EnumPtr::dynamicCast(g_parse->currentContainer());
  174. assert(e);
  175. e->addMember(tPtr);
  176. $$ = e;
  177. }
  178. | keyword
  179. {
  180. StringGrammarPtr ident = StringGrammarPtr::dynamicCast($1);
  181. g_parse->error("keyword `" + ident->v + "' cannot be used as enumerator");
  182. }
  183. | TARS_IDENTIFIER '=' const_initializer
  184. {
  185. TypePtr type = TypePtr::dynamicCast(g_parse->createBuiltin(Builtin::KindLong));
  186. StringGrammarPtr ident = StringGrammarPtr::dynamicCast($1);
  187. TypeIdPtr tPtr = new TypeId(type, ident->v);
  188. ConstGrammarPtr sPtr = ConstGrammarPtr::dynamicCast($3);
  189. g_parse->checkConstValue(tPtr, sPtr->t);
  190. tPtr->setDefault(sPtr->v);
  191. EnumPtr e = EnumPtr::dynamicCast(g_parse->currentContainer());
  192. assert(e);
  193. e->addMember(tPtr);
  194. $$ = e;
  195. }
  196. |
  197. {
  198. }
  199. ;
  200. // ----------------------------------------------------------------------
  201. namespace_def
  202. // ----------------------------------------------------------------------
  203. : TARS_NAMESPACE TARS_IDENTIFIER
  204. {
  205. StringGrammarPtr ident = StringGrammarPtr::dynamicCast($2);
  206. ContainerPtr c = g_parse->currentContainer();
  207. NamespacePtr n = c->createNamespace(ident->v);
  208. if(n)
  209. {
  210. g_parse->pushContainer(n);
  211. $$ = GrammarBasePtr::dynamicCast(n);
  212. }
  213. else
  214. {
  215. $$ = 0;
  216. }
  217. }
  218. '{' definitions '}'
  219. {
  220. if($3)
  221. {
  222. g_parse->popContainer();
  223. $$ = $3;
  224. }
  225. else
  226. {
  227. $$ = 0;
  228. }
  229. }
  230. ;
  231. //key------------------------------------------------------
  232. key_def
  233. // ----------------------------------------------------------------------
  234. : TARS_KEY '[' scoped_name ','
  235. {
  236. StringGrammarPtr ident = StringGrammarPtr::dynamicCast($3);
  237. StructPtr sp = StructPtr::dynamicCast(g_parse->findUserType(ident->v));
  238. if(!sp)
  239. {
  240. g_parse->error("struct '" + ident->v + "' undefined!");
  241. }
  242. g_parse->setKeyStruct(sp);
  243. }
  244. key_members ']'
  245. {
  246. }
  247. ;
  248. //key------------------------------------------------------
  249. key_members
  250. // ----------------------------------------------------------------------
  251. : TARS_IDENTIFIER
  252. {
  253. StringGrammarPtr ident = StringGrammarPtr::dynamicCast($1);
  254. StructPtr np = g_parse->getKeyStruct();
  255. if(np)
  256. {
  257. np->addKey(ident->v);
  258. }
  259. else
  260. {
  261. $$ = 0;
  262. }
  263. }
  264. | key_members ',' TARS_IDENTIFIER
  265. {
  266. StringGrammarPtr ident = StringGrammarPtr::dynamicCast($3);
  267. StructPtr np = g_parse->getKeyStruct();
  268. if(np)
  269. {
  270. np->addKey(ident->v);
  271. }
  272. else
  273. {
  274. $$ = 0;
  275. }
  276. }
  277. ;
  278. // ----------------------------------------------------------------------
  279. interface_def
  280. // ----------------------------------------------------------------------
  281. : interface_id
  282. {
  283. StringGrammarPtr ident = StringGrammarPtr::dynamicCast($1);
  284. NamespacePtr c = NamespacePtr::dynamicCast(g_parse->currentContainer());
  285. InterfacePtr cl = c->createInterface(ident->v);
  286. if(cl)
  287. {
  288. g_parse->pushContainer(cl);
  289. $$ = GrammarBasePtr::dynamicCast(cl);
  290. }
  291. else
  292. {
  293. $$ = 0;
  294. }
  295. }
  296. '{' interface_exports '}'
  297. {
  298. if($2)
  299. {
  300. g_parse->popContainer();
  301. $$ = GrammarBasePtr::dynamicCast($2);
  302. }
  303. else
  304. {
  305. $$ = 0;
  306. }
  307. }
  308. ;
  309. // ----------------------------------------------------------------------
  310. interface_id
  311. // ----------------------------------------------------------------------
  312. : TARS_INTERFACE TARS_IDENTIFIER
  313. {
  314. $$ = $2;
  315. }
  316. | TARS_INTERFACE keyword
  317. {
  318. StringGrammarPtr ident = StringGrammarPtr::dynamicCast($2);
  319. g_parse->error("keyword `" + ident->v + "' cannot be used as interface name");
  320. $$ = $2;
  321. }
  322. ;
  323. // ----------------------------------------------------------------------
  324. interface_exports
  325. // ----------------------------------------------------------------------
  326. : interface_export ';' interface_exports
  327. {
  328. }
  329. | error ';' interface_exports
  330. {
  331. }
  332. | interface_export
  333. {
  334. g_parse->error("`;' missing after definition");
  335. }
  336. |
  337. {
  338. }
  339. ;
  340. // ----------------------------------------------------------------------
  341. interface_export
  342. // ----------------------------------------------------------------------
  343. : operation
  344. ;
  345. // ----------------------------------------------------------------------
  346. operation
  347. // ----------------------------------------------------------------------
  348. : operation_preamble parameters ')'
  349. {
  350. if($1)
  351. {
  352. g_parse->popContainer();
  353. $$ = GrammarBasePtr::dynamicCast($1);
  354. }
  355. else
  356. {
  357. $$ = 0;
  358. }
  359. }
  360. ;
  361. // ----------------------------------------------------------------------
  362. operation_preamble
  363. // ----------------------------------------------------------------------
  364. : return_type TARS_OP
  365. {
  366. TypePtr returnType = TypePtr::dynamicCast($1);
  367. StringGrammarPtr ident = StringGrammarPtr::dynamicCast($2);
  368. string name = ident->v;
  369. InterfacePtr cl = InterfacePtr::dynamicCast(g_parse->currentContainer());
  370. if(cl)
  371. {
  372. OperationPtr op = cl->createOperation(name, returnType);
  373. if(op)
  374. {
  375. g_parse->pushContainer(op);
  376. $$ = GrammarBasePtr::dynamicCast(op);
  377. }
  378. else
  379. {
  380. $$ = 0;
  381. }
  382. }
  383. else
  384. {
  385. $$ = 0;
  386. }
  387. }
  388. ;
  389. // ----------------------------------------------------------------------
  390. return_type
  391. // ----------------------------------------------------------------------
  392. : type
  393. | TARS_VOID
  394. {
  395. $$ = 0;
  396. }
  397. ;
  398. // ----------------------------------------------------------------------
  399. parameters
  400. // ----------------------------------------------------------------------
  401. : // empty
  402. {
  403. }
  404. | type_id
  405. {
  406. TypeIdPtr tsp = TypeIdPtr::dynamicCast($1);
  407. OperationPtr op = OperationPtr::dynamicCast(g_parse->currentContainer());
  408. assert(op);
  409. if(op)
  410. {
  411. op->createParamDecl(tsp, false, false);
  412. }
  413. }
  414. | parameters ',' type_id
  415. {
  416. TypeIdPtr tsp = TypeIdPtr::dynamicCast($3);
  417. OperationPtr op = OperationPtr::dynamicCast(g_parse->currentContainer());
  418. assert(op);
  419. if(op)
  420. {
  421. op->createParamDecl(tsp, false, false);
  422. }
  423. }
  424. | out_qualifier type_id
  425. {
  426. BoolGrammarPtr isOutParam = BoolGrammarPtr::dynamicCast($1);
  427. TypeIdPtr tsp = TypeIdPtr::dynamicCast($2);
  428. OperationPtr op = OperationPtr::dynamicCast(g_parse->currentContainer());
  429. assert(op);
  430. if(op)
  431. {
  432. op->createParamDecl(tsp, isOutParam->v, false);
  433. }
  434. }
  435. | parameters ',' out_qualifier type_id
  436. {
  437. BoolGrammarPtr isOutParam = BoolGrammarPtr::dynamicCast($3);
  438. TypeIdPtr tsp = TypeIdPtr::dynamicCast($4);
  439. OperationPtr op = OperationPtr::dynamicCast(g_parse->currentContainer());
  440. assert(op);
  441. if(op)
  442. {
  443. op->createParamDecl(tsp, isOutParam->v, false);
  444. }
  445. }
  446. | routekey_qualifier type_id
  447. {
  448. BoolGrammarPtr isRouteKeyParam = BoolGrammarPtr::dynamicCast($1);
  449. TypeIdPtr tsp = TypeIdPtr::dynamicCast($2);
  450. OperationPtr op = OperationPtr::dynamicCast(g_parse->currentContainer());
  451. assert(op);
  452. if(op)
  453. {
  454. op->createParamDecl(tsp, false, isRouteKeyParam->v);
  455. }
  456. }
  457. | parameters ',' routekey_qualifier type_id
  458. {
  459. BoolGrammarPtr isRouteKeyParam = BoolGrammarPtr::dynamicCast($3);
  460. TypeIdPtr tsp = TypeIdPtr::dynamicCast($4);
  461. OperationPtr op = OperationPtr::dynamicCast(g_parse->currentContainer());
  462. assert(op);
  463. if(op)
  464. {
  465. op->createParamDecl(tsp, false, isRouteKeyParam->v);
  466. }
  467. }
  468. | out_qualifier
  469. {
  470. g_parse->error("'out' must be defined with a type");
  471. }
  472. | routekey_qualifier
  473. {
  474. g_parse->error("'routekey' must be defined with a type");
  475. }
  476. ;
  477. // ----------------------------------------------------------------------
  478. routekey_qualifier
  479. // ----------------------------------------------------------------------
  480. : TARS_ROUTE_KEY
  481. {
  482. BoolGrammarPtr routekey = new BoolGrammar;
  483. routekey->v = true;
  484. $$ = GrammarBasePtr::dynamicCast(routekey);
  485. }
  486. ;
  487. // ----------------------------------------------------------------------
  488. out_qualifier
  489. // ----------------------------------------------------------------------
  490. : TARS_OUT
  491. {
  492. BoolGrammarPtr out = new BoolGrammar;
  493. out->v = true;
  494. $$ = GrammarBasePtr::dynamicCast(out);
  495. }
  496. ;
  497. // struct--------------------------------------------------------------
  498. struct_def
  499. // ----------------------------------------------------------------------
  500. : struct_id
  501. {
  502. StringGrammarPtr ident = StringGrammarPtr::dynamicCast($1);
  503. NamespacePtr np = NamespacePtr::dynamicCast(g_parse->currentContainer());
  504. if(np)
  505. {
  506. StructPtr sp = np->createStruct(ident->v);
  507. if(sp)
  508. {
  509. g_parse->pushContainer(sp);
  510. $$ = GrammarBasePtr::dynamicCast(sp);
  511. }
  512. else
  513. {
  514. $$ = 0;
  515. }
  516. }
  517. else
  518. {
  519. g_parse->error("struct '" + ident->v + "' must definition in namespace");
  520. }
  521. }
  522. '{' struct_exports '}'
  523. {
  524. if($2)
  525. {
  526. g_parse->popContainer();
  527. }
  528. $$ = $2;
  529. StructPtr st = StructPtr::dynamicCast($$);
  530. assert(st);
  531. if(st->getAllMemberPtr().size() == 0)
  532. {
  533. g_parse->error("struct `" + st->getSid() + "' must have at least one member");
  534. }
  535. }
  536. ;
  537. // struct name----------------------------------------------------------
  538. struct_id
  539. // ----------------------------------------------------------------------
  540. : TARS_STRUCT TARS_IDENTIFIER
  541. {
  542. $$ = $2;
  543. }
  544. | TARS_STRUCT keyword
  545. {
  546. StringGrammarPtr ident = StringGrammarPtr::dynamicCast($2);
  547. g_parse->error("keyword `" + ident->v + "' cannot be used as struct name");
  548. }
  549. | TARS_STRUCT error
  550. {
  551. g_parse->error("abstract declarator '<anonymous struct>' used as declaration");
  552. }
  553. ;
  554. // struct members------------------------------------------------------
  555. struct_exports
  556. // ----------------------------------------------------------------------
  557. : data_member ';' struct_exports
  558. {
  559. }
  560. | data_member
  561. {
  562. g_parse->error("';' missing after definition");
  563. }
  564. |
  565. {
  566. }
  567. ;
  568. //
  569. //
  570. // data member--------------------------------------------------------------
  571. data_member
  572. // ----------------------------------------------------------------------
  573. : struct_type_id
  574. {
  575. $$ = GrammarBasePtr::dynamicCast($1);
  576. }
  577. ;
  578. // struct member id--------------------------------------------------------------
  579. struct_type_id
  580. // ----------------------------------------------------------------------
  581. : TARS_CONST_INTEGER TARS_REQUIRE type_id
  582. {
  583. StructPtr np = StructPtr::dynamicCast(g_parse->currentContainer());
  584. if(np)
  585. {
  586. IntergerGrammarPtr iPtr = IntergerGrammarPtr::dynamicCast($1);
  587. g_parse->checkTag(iPtr->v);
  588. TypeIdPtr tPtr = TypeIdPtr::dynamicCast($3);
  589. tPtr->setRequire(iPtr->v);
  590. np->addTypeId(tPtr);
  591. $$ = GrammarBasePtr::dynamicCast($3);
  592. }
  593. else
  594. {
  595. $$ = 0;
  596. }
  597. }
  598. | TARS_CONST_INTEGER TARS_REQUIRE type_id '=' const_initializer
  599. {
  600. StructPtr np = StructPtr::dynamicCast(g_parse->currentContainer());
  601. if(np)
  602. {
  603. IntergerGrammarPtr iPtr = IntergerGrammarPtr::dynamicCast($1);
  604. g_parse->checkTag(iPtr->v);
  605. TypeIdPtr tPtr = TypeIdPtr::dynamicCast($3);
  606. ConstGrammarPtr sPtr = ConstGrammarPtr::dynamicCast($5);
  607. g_parse->checkConstValue(tPtr, sPtr->t);
  608. tPtr->setRequire(iPtr->v);
  609. tPtr->setDefault(sPtr->v);
  610. np->addTypeId(tPtr);
  611. $$ = GrammarBasePtr::dynamicCast($3);
  612. }
  613. else
  614. {
  615. $$ = 0;
  616. }
  617. }
  618. | TARS_CONST_INTEGER TARS_OPTIONAL type_id '=' const_initializer
  619. {
  620. StructPtr np = StructPtr::dynamicCast(g_parse->currentContainer());
  621. if(np)
  622. {
  623. IntergerGrammarPtr iPtr = IntergerGrammarPtr::dynamicCast($1);
  624. g_parse->checkTag(iPtr->v);
  625. TypeIdPtr tPtr = TypeIdPtr::dynamicCast($3);
  626. ConstGrammarPtr sPtr = ConstGrammarPtr::dynamicCast($5);
  627. g_parse->checkConstValue(tPtr, sPtr->t);
  628. tPtr->setOptional(iPtr->v);
  629. tPtr->setDefault(sPtr->v);
  630. np->addTypeId(tPtr);
  631. $$ = GrammarBasePtr::dynamicCast($3);
  632. }
  633. else
  634. {
  635. $$ = 0;
  636. }
  637. }
  638. | TARS_CONST_INTEGER TARS_OPTIONAL type_id
  639. {
  640. StructPtr np = StructPtr::dynamicCast(g_parse->currentContainer());
  641. if(np)
  642. {
  643. IntergerGrammarPtr iPtr = IntergerGrammarPtr::dynamicCast($1);
  644. g_parse->checkTag(iPtr->v);
  645. TypeIdPtr tPtr = TypeIdPtr::dynamicCast($3);
  646. tPtr->setOptional(iPtr->v);
  647. np->addTypeId(tPtr);
  648. $$ = GrammarBasePtr::dynamicCast($3);
  649. }
  650. else
  651. {
  652. $$ = 0;
  653. }
  654. }
  655. | TARS_REQUIRE type_id
  656. {
  657. g_parse->error("struct member need 'tag'");
  658. }
  659. | TARS_OPTIONAL type_id
  660. {
  661. g_parse->error("struct member need 'tag'");
  662. }
  663. | TARS_CONST_INTEGER type_id
  664. {
  665. g_parse->error("struct member need 'require' or 'optional'");
  666. }
  667. | type_id
  668. {
  669. g_parse->error("struct member need 'tag' or 'require' or 'optional'");
  670. }
  671. ;
  672. // ----------------------------------------------------------------------
  673. const_initializer
  674. // ----------------------------------------------------------------------
  675. : TARS_CONST_INTEGER
  676. {
  677. IntergerGrammarPtr intVal = IntergerGrammarPtr::dynamicCast($1);
  678. ostringstream sstr;
  679. sstr << intVal->v;
  680. ConstGrammarPtr c = new ConstGrammar();
  681. c->t = ConstGrammar::VALUE;
  682. c->v = sstr.str();
  683. $$ = c;
  684. }
  685. | TARS_CONST_FLOAT
  686. {
  687. FloatGrammarPtr floatVal = FloatGrammarPtr::dynamicCast($1);
  688. ostringstream sstr;
  689. sstr << floatVal->v;
  690. ConstGrammarPtr c = new ConstGrammar();
  691. c->t = ConstGrammar::VALUE;
  692. c->v = sstr.str();
  693. $$ = c;
  694. }
  695. | TARS_STRING_LITERAL
  696. {
  697. StringGrammarPtr ident = StringGrammarPtr::dynamicCast($1);
  698. ConstGrammarPtr c = new ConstGrammar();
  699. c->t = ConstGrammar::STRING;
  700. c->v = ident->v;
  701. $$ = c;
  702. }
  703. | TARS_FALSE
  704. {
  705. StringGrammarPtr ident = StringGrammarPtr::dynamicCast($1);
  706. ConstGrammarPtr c = new ConstGrammar();
  707. c->t = ConstGrammar::BOOL;
  708. c->v = ident->v;
  709. $$ = c;
  710. }
  711. | TARS_TRUE
  712. {
  713. StringGrammarPtr ident = StringGrammarPtr::dynamicCast($1);
  714. ConstGrammarPtr c = new ConstGrammar();
  715. c->t = ConstGrammar::BOOL;
  716. c->v = ident->v;
  717. $$ = c;
  718. }
  719. | TARS_IDENTIFIER
  720. {
  721. StringGrammarPtr ident = StringGrammarPtr::dynamicCast($1);
  722. if (g_parse->checkEnum(ident->v) == false)
  723. {
  724. g_parse->error("error enum default value, not defined yet");
  725. }
  726. ConstGrammarPtr c = new ConstGrammar();
  727. c->t = ConstGrammar::ENUM;
  728. c->v = ident->v;
  729. $$ = c;
  730. }
  731. | scoped_name TARS_SCOPE_DELIMITER TARS_IDENTIFIER
  732. {
  733. StringGrammarPtr scoped = StringGrammarPtr::dynamicCast($1);
  734. StringGrammarPtr ident = StringGrammarPtr::dynamicCast($3);
  735. if (g_parse->checkEnum(ident->v) == false)
  736. {
  737. g_parse->error("error enum default value, not defined yet");
  738. }
  739. ConstGrammarPtr c = new ConstGrammar();
  740. c->t = ConstGrammar::ENUM;
  741. c->v = scoped->v + "::" + ident->v;
  742. $$ = c;
  743. }
  744. ;
  745. // const--------------------------------------------------------------
  746. const_def
  747. // ----------------------------------------------------------------------
  748. : TARS_CONST type_id '=' const_initializer
  749. {
  750. NamespacePtr np = NamespacePtr::dynamicCast(g_parse->currentContainer());
  751. if(!np)
  752. {
  753. g_parse->error("const type must define in namespace");
  754. }
  755. TypeIdPtr t = TypeIdPtr::dynamicCast($2);
  756. ConstGrammarPtr c = ConstGrammarPtr::dynamicCast($4);
  757. ConstPtr cPtr = np->createConst(t, c);
  758. $$ = cPtr;
  759. }
  760. ;
  761. // type--------------------------------------------------------------
  762. type_id
  763. // ----------------------------------------------------------------------
  764. : type TARS_IDENTIFIER
  765. {
  766. TypePtr type = TypePtr::dynamicCast($1);
  767. StringGrammarPtr ident = StringGrammarPtr::dynamicCast($2);
  768. TypeIdPtr typeIdPtr = new TypeId(type, ident->v);
  769. $$ = GrammarBasePtr::dynamicCast(typeIdPtr);
  770. }
  771. |type TARS_IDENTIFIER '[' TARS_CONST_INTEGER ']'
  772. {
  773. TypePtr type = g_parse->createVector(TypePtr::dynamicCast($1));
  774. IntergerGrammarPtr iPtrSize = IntergerGrammarPtr::dynamicCast($4);
  775. g_parse->checkArrayVaid(type,iPtrSize->v);
  776. type->setArray(iPtrSize->v);
  777. StringGrammarPtr ident = StringGrammarPtr::dynamicCast($2);
  778. TypeIdPtr typeIdPtr = new TypeId(type, ident->v);
  779. $$ = GrammarBasePtr::dynamicCast(typeIdPtr);
  780. }
  781. |type '*' TARS_IDENTIFIER
  782. {
  783. TypePtr type = g_parse->createVector(TypePtr::dynamicCast($1));
  784. //IntergerGrammarPtr iPtrSize = IntergerGrammarPtr::dynamicCast($4);
  785. g_parse->checkPointerVaid(type);
  786. type->setPointer(true);
  787. StringGrammarPtr ident = StringGrammarPtr::dynamicCast($3);
  788. TypeIdPtr typeIdPtr = new TypeId(type, ident->v);
  789. $$ = GrammarBasePtr::dynamicCast(typeIdPtr);
  790. }
  791. |type TARS_IDENTIFIER ':' TARS_CONST_INTEGER
  792. {
  793. TypePtr type = TypePtr::dynamicCast($1);
  794. StringGrammarPtr ident = StringGrammarPtr::dynamicCast($2);
  795. TypeIdPtr typeIdPtr = new TypeId(type, ident->v);
  796. IntergerGrammarPtr iPtrSize = IntergerGrammarPtr::dynamicCast($3);
  797. g_parse->checkArrayVaid(type,iPtrSize->v);
  798. $$ = GrammarBasePtr::dynamicCast(typeIdPtr);
  799. }
  800. | type keyword
  801. {
  802. StringGrammarPtr ident = StringGrammarPtr::dynamicCast($2);
  803. g_parse->error("keyword `" + ident->v + "' cannot be used as data member name");
  804. }
  805. | type
  806. {
  807. g_parse->error("missing data member name");
  808. }
  809. | error
  810. {
  811. g_parse->error("unkown type");
  812. }
  813. ;
  814. // type------------------------------------------------------------------
  815. type
  816. // ----------------------------------------------------------------------
  817. : type_no ':' TARS_CONST_INTEGER
  818. {
  819. TypePtr type = TypePtr::dynamicCast($1);
  820. IntergerGrammarPtr iPtrSize = IntergerGrammarPtr::dynamicCast($3);
  821. g_parse->checkArrayVaid(type,iPtrSize->v);
  822. type->setArray(iPtrSize->v);
  823. $$ = type;
  824. }
  825. | type_no
  826. {
  827. $$ = $1;
  828. }
  829. | type_no ':' error
  830. {
  831. g_parse->error("array missing size");
  832. }
  833. ;
  834. // type------------------------------------------------------------------
  835. type_no
  836. // ----------------------------------------------------------------------
  837. : TARS_BOOL
  838. {
  839. $$ = g_parse->createBuiltin(Builtin::KindBool);
  840. }
  841. | TARS_BYTE
  842. {
  843. $$ = g_parse->createBuiltin(Builtin::KindByte);
  844. }
  845. | TARS_UNSIGNED TARS_BYTE //unsigned char -> short
  846. {
  847. $$ = g_parse->createBuiltin(Builtin::KindShort,true);
  848. }
  849. | TARS_SHORT
  850. {
  851. $$ = g_parse->createBuiltin(Builtin::KindShort);
  852. }
  853. | TARS_UNSIGNED TARS_SHORT
  854. {
  855. $$ = g_parse->createBuiltin(Builtin::KindInt,true);
  856. }
  857. | TARS_INT
  858. {
  859. $$ = g_parse->createBuiltin(Builtin::KindInt);
  860. }
  861. | TARS_UNSIGNED TARS_INT
  862. {
  863. $$ = g_parse->createBuiltin(Builtin::KindLong,true);
  864. }
  865. | TARS_LONG
  866. {
  867. $$ = g_parse->createBuiltin(Builtin::KindLong);
  868. }
  869. | TARS_FLOAT
  870. {
  871. $$ = g_parse->createBuiltin(Builtin::KindFloat);
  872. }
  873. | TARS_DOUBLE
  874. {
  875. $$ = g_parse->createBuiltin(Builtin::KindDouble);
  876. }
  877. | TARS_STRING
  878. {
  879. $$ = g_parse->createBuiltin(Builtin::KindString);
  880. }
  881. | vector
  882. {
  883. $$ = GrammarBasePtr::dynamicCast($1);
  884. }
  885. | map
  886. {
  887. $$ = GrammarBasePtr::dynamicCast($1);
  888. }
  889. | scoped_name
  890. {
  891. StringGrammarPtr ident = StringGrammarPtr::dynamicCast($1);
  892. TypePtr sp = g_parse->findUserType(ident->v);
  893. if(sp)
  894. {
  895. $$ = GrammarBasePtr::dynamicCast(sp);
  896. }
  897. else
  898. {
  899. g_parse->error("'" + ident->v + "' undefined!");
  900. }
  901. }
  902. ;
  903. // vector----------------------------------------------------------------
  904. vector
  905. // ----------------------------------------------------------------------
  906. : TARS_VECTOR '<' type '>'
  907. {
  908. $$ = GrammarBasePtr::dynamicCast(g_parse->createVector(TypePtr::dynamicCast($3)));
  909. }
  910. | TARS_VECTOR '<' error
  911. {
  912. g_parse->error("vector error");
  913. }
  914. | TARS_VECTOR '<' type error
  915. {
  916. g_parse->error("vector missing '>'");
  917. }
  918. | TARS_VECTOR error
  919. {
  920. g_parse->error("vector missing type");
  921. }
  922. ;
  923. // map----------------------------------------------------------------
  924. map
  925. // ----------------------------------------------------------------------
  926. : TARS_MAP '<' type ',' type '>'
  927. {
  928. $$ = GrammarBasePtr::dynamicCast(g_parse->createMap(TypePtr::dynamicCast($3), TypePtr::dynamicCast($5)));
  929. }
  930. | TARS_MAP '<' error
  931. {
  932. g_parse->error("map error");
  933. }
  934. ;
  935. // ----------------------------------------------------------------------
  936. scoped_name
  937. // ----------------------------------------------------------------------
  938. : TARS_IDENTIFIER
  939. {
  940. }
  941. | TARS_SCOPE_DELIMITER TARS_IDENTIFIER
  942. {
  943. StringGrammarPtr ident = StringGrammarPtr::dynamicCast($2);
  944. ident->v = "::" + ident->v;
  945. $$ = GrammarBasePtr::dynamicCast(ident);
  946. }
  947. | scoped_name TARS_SCOPE_DELIMITER TARS_IDENTIFIER
  948. {
  949. StringGrammarPtr scoped = StringGrammarPtr::dynamicCast($1);
  950. StringGrammarPtr ident = StringGrammarPtr::dynamicCast($3);
  951. scoped->v += "::";
  952. scoped->v += ident->v;
  953. $$ = GrammarBasePtr::dynamicCast(scoped);
  954. }
  955. ;
  956. // key----------------------------------------------------------------
  957. keyword
  958. // ----------------------------------------------------------------------
  959. : TARS_STRUCT
  960. {
  961. }
  962. | TARS_VOID
  963. {
  964. }
  965. | TARS_BOOL
  966. {
  967. }
  968. | TARS_BYTE
  969. {
  970. }
  971. | TARS_SHORT
  972. {
  973. }
  974. | TARS_INT
  975. {
  976. }
  977. | TARS_FLOAT
  978. {
  979. }
  980. | TARS_DOUBLE
  981. {
  982. }
  983. | TARS_STRING
  984. {
  985. }
  986. | TARS_VECTOR
  987. {
  988. }
  989. | TARS_KEY
  990. {
  991. }
  992. | TARS_MAP
  993. {
  994. }
  995. | TARS_NAMESPACE
  996. {
  997. }
  998. | TARS_INTERFACE
  999. {
  1000. }
  1001. | TARS_OUT
  1002. {
  1003. }
  1004. | TARS_REQUIRE
  1005. {
  1006. }
  1007. | TARS_OPTIONAL
  1008. {
  1009. }
  1010. | TARS_CONST_INTEGER
  1011. {
  1012. }
  1013. | TARS_CONST_FLOAT
  1014. {
  1015. }
  1016. | TARS_FALSE
  1017. {
  1018. }
  1019. | TARS_TRUE
  1020. {
  1021. }
  1022. | TARS_STRING_LITERAL
  1023. {
  1024. }
  1025. | TARS_CONST
  1026. {
  1027. }
  1028. | TARS_ENUM
  1029. {
  1030. }
  1031. | TARS_UNSIGNED
  1032. {
  1033. }
  1034. ;
  1035. %%