IvannosysGrammar.g4 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. grammar IvannosysGrammar;
  7. @header {
  8. import java.util.HashMap;
  9. import java.util.Stack;
  10. }
  11. @members {
  12. HashMap<String, String> decfunc = new HashMap<>();
  13. HashMap<String, String> decmet = new HashMap<>();
  14. HashMap<String, String> decvars = new HashMap<>();
  15. HashMap<String, String> operandos;
  16. HashMap<String, String> boolExpr;
  17. String travaOperandos = null;
  18. }
  19. // Estrutura de um programa
  20. init
  21. : buildtags? R_PACKAGE ID R_PT_VIRG imports* programPart+ EOF
  22. ;
  23. // Estrutura de um import
  24. imports
  25. : R_IMPORTE R_PAR_E import_part (import_part )* R_PAR_D
  26. | R_IMPORTE import_part
  27. ;
  28. import_part
  29. : ID? T_STRING
  30. ;
  31. // Fim do importe -> Ini partes do programa
  32. programPart
  33. : dec_const
  34. | dec_var
  35. | def_type
  36. | def_interface
  37. | def_function
  38. ;
  39. // Fim partes do programa -> Ini Declaracao de tipo
  40. def_type
  41. : R_TIPO ID ( structType | type )
  42. ;
  43. def_interface
  44. : R_TIPO ID R_INTERFACE R_COL_E func_spec+ R_COL_D
  45. ;
  46. func_spec
  47. : ID arguments return_dec
  48. ;
  49. // Fim Declaracao de tipo -> Ini
  50. dec_var
  51. : R_VAR R_PAR_E dec_var_part+ R_PAR_D
  52. | R_VAR dec_var_part
  53. ;
  54. dec_var_part
  55. : id_list (type (R_EQ expression_list)? | R_EQ expression_list)
  56. ;
  57. dec_var_short
  58. : id_list R_SHORT_VAR expression_list // Declaracao reconhecendo o tipo do valor atribuido
  59. ;
  60. id_list
  61. : ID (R_VIRGULA ID)*
  62. ;
  63. // Fim Declaracao de variavel -> Ini Operacao de atribuicao
  64. def_assign_op
  65. : expression_list assign_modify? R_EQ expression_list
  66. ;
  67. assign_modify
  68. : R_OP_ADD | R_OP_SUB | R_OP_AST | R_OP_DIV | R_ASSIG_EXTEND | R_ASSIG_COPY
  69. ;
  70. // Fim Operacao de atribuicao -> Ini
  71. index
  72. : R_CHA_E expression R_CHA_D
  73. ;
  74. arguments
  75. // expression_list -> chamadas de funcao
  76. // expression_list_typed -> declaracao de funcao
  77. : R_PAR_E (expression_list | expression_list_typed (R_VIRGULA expression_list_typed)*)? R_PAR_D
  78. ;
  79. expression_list_typed
  80. : id_list? type
  81. ;
  82. // Ini - Constantes
  83. dec_const
  84. : R_CONSTANTE dec_const_part
  85. | R_CONSTANTE R_PAR_E dec_const_part (dec_const_part)* R_PAR_D
  86. ;
  87. dec_const_part
  88. : ID type? R_EQ primitive_value
  89. ;
  90. // Fim - Constantes -> Ini Funcoes
  91. def_function
  92. : R_FUNCTION receive_type? (name=ID)? arguments return_dec? def_block
  93. ;
  94. return_dec
  95. : type (R_VIRGULA type)*
  96. ;
  97. receive_type
  98. : R_PAR_E ID R_OP_AST? type R_PAR_D
  99. ;
  100. // Fim - Funcoes -> Ini Switch
  101. def_switch
  102. : R_SWITCH test R_COL_E def_case+ R_COL_D
  103. ;
  104. def_case
  105. : R_CASE expression_list R_COLON statement_list R_BREAK?
  106. | R_DEFAULT R_COLON statement_list
  107. ;
  108. statement_list
  109. : statement+
  110. ;
  111. // Fim - Switch -> Ini Valores
  112. primitive_value
  113. : T_INTEIRO
  114. | (T_BOOL_TRUE | T_BOOL_FALSE)
  115. | T_CHAR
  116. ;
  117. // Fim - Valores -> Ini Chamada de funcao
  118. //def_funcao_call locals[String functionName]
  119. // :ID call_assinature {$functionName = $ID.text; } //funcao
  120. // ;
  121. // Fim Chamada de funcao -> Ini If
  122. def_if locals[int ltipo]
  123. : def_if_block (R_ELSE def_if_block)* (R_ELSE def_block)?
  124. ;
  125. def_if_block
  126. : R_IF test def_block
  127. ;
  128. // Fim If -> Ini Lacos
  129. def_for
  130. : R_FOR ( for_clause | for_each )? def_block // Like for
  131. ;
  132. for_clause
  133. : (initialization R_PT_VIRG)? test (R_PT_VIRG increment)?
  134. ;
  135. initialization
  136. :base_stmt
  137. ;
  138. test
  139. : expression
  140. | base_stmt (R_PT_VIRG expression)?
  141. ;
  142. increment
  143. :base_stmt
  144. ;
  145. for_each
  146. : primary_expr R_VIRGULA primary_expr ( R_EQ | R_SHORT_VAR) R_RANGE range
  147. ;
  148. range
  149. : expression
  150. ;
  151. // Fim Lacos -> Ini Retorno de funcao
  152. return_stmt
  153. : R_RETURN expression_list?
  154. ;
  155. //Regra produz recurssao de parametros em uma funcao.
  156. // Fim Retorno de funcao -> Ini
  157. expression_list
  158. : expression (R_VIRGULA expression)*
  159. ;
  160. expression
  161. // | expression BINARY_OP expression
  162. // | expression ('||' | '&&' | '==' | '!=' | '<' | '<=' | '>' | '>=' | '+' | '-' | '|' | '^' | '*' | '/' | '%' | '<<' | '>>' | '&' | '&^') expression
  163. : R_PAR_E expression R_PAR_D
  164. | unary_expr
  165. | expression op=(
  166. R_OP_MOD| R_END |
  167. R_OP_AST | R_OP_DIV|
  168. R_OP_ADD | R_OP_SUB ) expression
  169. | expression op=(R_SHIFT_LEFT | R_SHIFT_RIGHT) expression
  170. | expression op=(R_BIT_AND | R_BIT_XOR | R_BIT_OR) expression
  171. | expression op=(R_COMP_EQ | R_COMP_DIFF |
  172. R_COMP_LT | R_COMP_LTE |
  173. R_COMP_GT | R_COMP_GTE) expression
  174. | expression op=(R_OR | R_AND) expression
  175. ;
  176. unary_expr
  177. : op=(R_REFERENCE|R_OP_SUB|R_EXCLAMATION|R_OP_AST) primary_expr
  178. | primary_expr
  179. ;
  180. primary_expr returns[String id]
  181. : operand {$id = $operand.Value;} //#expr_operando// acesso de um operando
  182. | literal
  183. | pe = primary_expr index {$id = $pe.id;} //#expr_index// acesso de um index
  184. | pee = primary_expr expression_seletor {$id = $pee.id;} //#expr_selector// acesso de um atributo
  185. | pea = primary_expr arguments {$id = $pea.id;} //#expr_arguments// chamada de uma funcao ou metodo
  186. ;
  187. operand returns[String Value]
  188. : ID {$Value=$ID.text;}
  189. | primitive_value {$Value=$primitive_value.text;}
  190. ;
  191. literal
  192. : func_literal
  193. | composite
  194. ;
  195. base_stmt
  196. :
  197. | inc_dec
  198. | def_assign_op
  199. | dec_var_short
  200. | expression
  201. ;
  202. func_literal
  203. : R_FUNCTION arguments return_dec? def_block
  204. ;
  205. composite
  206. : literalType literal_value
  207. ;
  208. literalType
  209. : typename
  210. | structType
  211. | arrayType
  212. ;
  213. structType
  214. : R_STRUCT R_COL_E fieldDecl+ R_COL_D
  215. ;
  216. fieldDecl
  217. :(id_list type| anon_field) T_STRING?
  218. ;
  219. anon_field
  220. : R_OP_AST? typename
  221. ;
  222. arrayType
  223. : R_CHA_E expression? R_CHA_D typename
  224. ;
  225. inc_dec
  226. : expression op=( R_OP_PP| R_OP_SS)
  227. ;
  228. literal_value
  229. : R_COL_E ( (expression_list | element_list) R_VIRGULA )? R_COL_D
  230. ;
  231. element_list
  232. : keyed_element (R_VIRGULA keyed_element)*
  233. ;
  234. keyed_element
  235. : ID R_COLON expression
  236. ;
  237. expression_seletor
  238. : R_PONTO ID
  239. ;
  240. // Definicao de um bloco de instrucoes
  241. def_block locals[HashMap<String, String> retornos = new HashMap<>();]
  242. : R_COL_E statement+ R_COL_D
  243. ;
  244. callExpr:
  245. | primary_expr arguments
  246. ;
  247. statement
  248. : dec_var_short //R_PT_VIRG
  249. | dec_var //R_PT_VIRG
  250. | def_assign_op //R_PT_VIRG
  251. | dec_const
  252. | inc_dec
  253. | primary_expr
  254. | label_stmt
  255. //| def_read R_PT_VIRG
  256. //| def_print R_PT_VIRG
  257. | return_stmt //R_PT_VIRG
  258. //| def_funcao_call R_PT_VIRG
  259. //| expression //R_PT_VIRG
  260. | delete //R_PT_VIRG
  261. | throwStmt
  262. | fallthroughStmt // dentro de um case do switch avanca para o proximo caso
  263. | def_if
  264. | def_for
  265. | def_switch
  266. | flux_control
  267. | try_catch
  268. ;
  269. // Fim da definicao de um bloco de instrucoes
  270. fallthroughStmt
  271. : R_FALLTHROUGH
  272. ;
  273. label_stmt
  274. : ID R_COLON statement
  275. ;
  276. throwStmt
  277. : R_THROW expression R_PT_VIRG
  278. ;
  279. // Inicio do try cath
  280. try_catch
  281. : R_TRY def_block
  282. catch_exception
  283. finally_try ?
  284. ;
  285. catch_exception
  286. : R_CATCH R_PAR_E dec_var R_PAR_D def_block
  287. ;
  288. finally_try
  289. : R_FINALLY def_block
  290. ;
  291. // Fim do try cath
  292. // Ids de controle de fluxo dentro do codigo
  293. flux_control locals[String control]
  294. : R_GOTO ID {$control = $R_GOTO.text;}
  295. | R_CONTINUE {$control = $R_CONTINUE.text;}
  296. | R_BREAK {$control = $R_BREAK.text;}
  297. ;
  298. // Definicao de new para alocar enderecos de memoria
  299. newstmt
  300. : R_NEW R_PAR_E type (R_VIRGULA T_INTEIRO)? R_PAR_D
  301. ;
  302. // Definicao de delete para liberar areas de memoria
  303. delete
  304. : R_DELETE expression( R_VIRGULA expression)?
  305. ;
  306. // Definicao dos tipos
  307. type returns[String ltype,String keytype,String valuetype]
  308. : typename {$ltype = $typename.ltype;}
  309. | indexedType {$ltype = $indexedType.ltype;}
  310. | mapType {$ltype = "map";}
  311. ;
  312. indexedType returns[String ltype]
  313. : typename {$ltype = $typename.ltype;}
  314. | index it=indexedType {$ltype = $it.ltype;}
  315. ;
  316. typename returns[String ltype, Boolean pointer]
  317. : ID {$ltype = $ID.text;}
  318. | qualifiedId {$ltype = $qualifiedId.text;}
  319. | baseType {$ltype = $baseType.ltype;}
  320. | R_OP_AST tn=typename {$ltype = $tn.ltype;}
  321. ;
  322. qualifiedId
  323. : ID R_PONTO ID
  324. ;
  325. baseType returns[String ltype]
  326. : R_INT {$ltype = $R_INT.text;}
  327. | R_BOOL {$ltype = $R_BOOL.text;}
  328. | R_CHAR {$ltype = $R_CHAR.text;}
  329. | R_STRING {$ltype = $R_STRING.text;}
  330. | R_ERROR {$ltype = $R_ERROR.text;}
  331. ;
  332. mapType returns[String keytype,String valuetype]
  333. : R_MAP R_CHA_E k=type R_CHA_D v=type {
  334. $keytype=$k.text;
  335. $valuetype=$v.text;
  336. }
  337. ;
  338. buildtags
  339. : build_stmt+
  340. ;
  341. build_stmt
  342. : R_AT buildtag R_COLON buildvalue (R_VIRGULA buildvalue)*
  343. ;
  344. buildtag
  345. : BUILD_TARGET
  346. | BUILD_PROFILE
  347. | BUILD_CORESETTINGS
  348. ;
  349. buildvalue
  350. : ID
  351. | R_LIT_STRING
  352. ;
  353. //DEFINICAO DE TOKENS ----------------------------------------------------------
  354. BUILD_TARGET :'target';
  355. BUILD_PROFILE :'profile';
  356. BUILD_CORESETTINGS :'coresettings';
  357. R_BIT_AND : '&';
  358. R_BIT_XOR : '^';
  359. R_BIT_OR : '|';
  360. R_SHIFT_LEFT : '<<';
  361. R_SHIFT_RIGHT : '>>';
  362. R_AT : '@';
  363. R_DBL_SLASH : '//';
  364. R_REFERENCE : '&';
  365. R_INTERFACE : 'interface';
  366. R_RANGE : 'range';
  367. R_STRUCT : 'struct';
  368. R_OP_MOD : '%';
  369. R_VAR : 'var';
  370. R_SHORT_VAR : ':=';
  371. R_GOTO : 'goto';
  372. R_FALLTHROUGH : 'fallthrough';
  373. R_MAP : 'map';
  374. R_ERROR : 'error';
  375. R_NEW : 'new';
  376. R_DELETE : 'delete';
  377. R_TRY : 'try';
  378. R_CATCH : 'catch';
  379. R_FINALLY : 'finally';
  380. R_DEFAULT : 'default';
  381. //R_PRINT : 'print';
  382. //R_READ : 'read';
  383. R_IMPORTE : 'import';
  384. //R_INIT_LIBRARY : 'library';
  385. R_PACKAGE : 'package';
  386. //R_THREAD : 'thread';
  387. R_TRAP : 'trap';
  388. R_INT : 'int';
  389. R_CHAR : 'char';
  390. R_BOOL : 'bool';
  391. R_STRING : 'string';
  392. //R_OBJECT : 'object';
  393. //R_ARRAY : 'array';
  394. //R_EXCEPTION : 'Exception';
  395. R_TIPO : 'type';
  396. R_FUNCTION : 'func';
  397. //R_METHOD : 'method';ARO
  398. R_CONSTANTE : 'const';
  399. R_IN : 'in';
  400. R_SWITCH : 'switch';
  401. R_CASE : 'case';
  402. R_FOR : 'for';
  403. R_RETURN : 'return';
  404. R_CONTINUE : 'continue';
  405. R_BREAK : 'break';
  406. R_THROW : 'throw';
  407. R_ELSE : 'else';
  408. R_IF : 'if';
  409. //R_ASYNC : 'async';
  410. //T_TRAP_SYNC : 'SYNC';
  411. //T_TRAP_EXEC : 'EXEC';
  412. //T_TRAP_SYNEXEC : 'SYNEXEC';
  413. //T_TRAP_SEND : 'SEND';
  414. //T_TRAP_RECEIVE : 'RECEIVE';
  415. R_EXCLAMATION : '!';
  416. R_PT_VIRG : ';';
  417. R_PAR_E : '(';
  418. R_PAR_D : ')';
  419. R_COL_E : '{';
  420. R_COL_D : '}';
  421. R_CHA_E : '[';
  422. R_CHA_D : ']';
  423. R_EQ : '=';
  424. R_VIRGULA : ',';
  425. R_PONTO : '.';
  426. R_COLON : ':';
  427. R_END : '&' ;
  428. R_AND : '&&' ;
  429. R_OR : '||';
  430. //Comparison
  431. R_COMP_EQ : '==' ;
  432. R_COMP_DIFF : '!=';
  433. R_COMP_LTE : '<=' ;
  434. R_COMP_LT : '<' ;
  435. R_COMP_GTE : '>=' ;
  436. R_COMP_GT : '>' ;
  437. //assing
  438. R_ASSIG_EXTEND : '::';
  439. R_ASSIG_COPY : ':';
  440. //arithmetic
  441. R_OP_DIV : '/';
  442. R_OP_ADD : '+';
  443. R_OP_SUB : '-';
  444. R_OP_AST : '*';
  445. R_OP_NEG : '!';
  446. R_OP_SS : '--';
  447. R_OP_PP : '++';
  448. R_UND : '_';
  449. R_INTERROGATION : '?';
  450. T_CHAR : '\'' .? '\'' ;
  451. T_STRING : '"' .*? '"' ;
  452. T_BOOL_TRUE : 'true';
  453. T_BOOL_FALSE : 'false' ;
  454. T_NULL : 'nil';
  455. ID : LETTER ( LETTER | DIGITO )*;
  456. T_INTEIRO : DIGITO+ ;
  457. fragment LETTER : ('a'..'z' | 'A'..'Z' | '_') ;
  458. fragment DIGITO : '0'..'9' ;
  459. //ID : CHARACTER ( CHARACTER | DIGITO )* ;
  460. //fragment CHARACTER : [_a-zA-Z]
  461. // ;
  462. R_LIT_STRING : '`' .*? '`';
  463. R_LINE_COMMENT : '//' .*? '\r'? '\n' -> skip ; // Match "#" stuff '\n'
  464. R_WS : (' '|'\t'|'\r'|'\n')+ -> skip ;
  465. R_COMMENT : '/*' .*? '*/' -> skip ; // Match "/*" stuff "*/"
  466. /*
  467. /*
  468. : ID literal_value #expr_literal
  469. | R_REFERENCE expression #expr_pointer
  470. | R_PAR_E expression R_PAR_D #expr_par
  471. | expression (R_PT_VIRG expression) #expr_chain_operacoes
  472. | expression op=(R_OP_AST | R_OP_DIV) expression #expr_arit_md
  473. | expression op=(R_OP_ADD | R_OP_SUB) expression #expr_arit_as
  474. | expression op=(R_COMP_LT | R_COMP_GT | R_COMP_LTE | R_COMP_GTE) expression #expr_comp_lt
  475. | expression op=(R_COMP_DIFF | R_COMP_EQ) expression #expr_comp_eqd
  476. | expression R_AND expression #expr_and
  477. | expression R_OR expression #expr_or
  478. | expression primitive_value #expr_primitive_value
  479. | expression ( R_OP_PP| R_OP_SS) #expr_inc_dec
  480. | R_OP_SUB expression #expr_negative
  481. | R_PAR_E type R_PAR_D expression #expr_assertion
  482. | R_EXCLAMATION expression #expr_neg_bool
  483. | expression R_INTERROGATION expression R_COLON expression #expr_ternary
  484. | def_function #expr_def_function
  485. | newstmt #expr_allocation
  486. | ID #expr_id
  487. ;
  488. inc_dec_stmt
  489. :
  490. ;
  491. def_object
  492. : R_COL_E (object_prop ( R_VIRGULA object_prop )*)? R_COL_D
  493. ;
  494. object_prop returns[String id]
  495. : ID R_COLON expression {$id = $ID.text;}
  496. ;
  497. address_access_dst
  498. : ID indices? access_prop ?
  499. ;
  500. access_prop
  501. : (R_PONTO ID indices?) access_prop ?
  502. ;
  503. address_access_src
  504. : ID indices? call_assinature? access_prop_src?
  505. ;
  506. access_prop_src
  507. : (R_PONTO ID indices? call_assinature?) access_prop_src?
  508. ;
  509. address
  510. : R_REFERENCE T_INTEIRO
  511. ;
  512. //| R_THREAD R_OP_AST* {$ltype = $R_THREAD.text;}
  513. //| R_FUNCTION R_OP_AST* {$ltype = $R_FUNCTION.text;}
  514. //| R_EXCEPTION R_OP_AST* {$ltype = $R_EXCEPTION.text;}
  515. //| def_method
  516. //| dec_trap
  517. //| dec_thread
  518. //| main
  519. call_from_return
  520. : (call_assinature | indices | access_prop_src) call_from_return ?
  521. ;
  522. boolena_operator
  523. : R_COMP_EQ | R_COMP_DIFF | R_COMP_LTE | R_COMP_GTE | R_COMP_LT | R_COMP_GT
  524. ;
  525. value
  526. : expression #expressionValue
  527. //| def_array #arrayValue
  528. | def_object #objectValue
  529. | address #pureAddressValue
  530. ;
  531. def_array
  532. : R_CHA_E ( value ( R_VIRGULA value )* )? R_CHA_D
  533. ;
  534. gen_list
  535. : def_array
  536. | address_access_src
  537. | def_funcao_call
  538. ;
  539. def_print
  540. : R_PRINT R_PAR_E list_values R_PAR_D
  541. ;
  542. def_read
  543. : R_READ R_PAR_E list_vars R_PAR_D
  544. ;
  545. list_vars
  546. : address_access_src (R_VIRGULA address_access_src)*
  547. ;
  548. param_list_dec
  549. : dec_var (R_PT_VIRG dec_var)*
  550. ;
  551. main
  552. : R_FUNCTION R_MAIN R_PAR_E param_list_dec? R_PAR_D R_COLON return_list def_block
  553. ;
  554. dec_thread
  555. : R_IN ID R_PAR_E param_list_dec? R_PAR_D def_block
  556. ;
  557. dec_trap locals[String label]
  558. : R_TRAP name=trap_id R_PAR_E param_list_dec? R_PAR_D def_block {$label = $name.text;}
  559. ;
  560. trap_id
  561. : (T_TRAP_SYNC |T_TRAP_EXEC |T_TRAP_SYNEXEC |T_TRAP_SEND |T_TRAP_RECEIVE)
  562. ;
  563. */