IvannosysGrammar.g4 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  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 | test ) 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. empty_block
  139. : R_COL_E R_COL_D
  140. ;
  141. test
  142. : expression
  143. | base_stmt (R_PT_VIRG expression)?
  144. ;
  145. increment
  146. :base_stmt
  147. ;
  148. for_each
  149. : primary_expr R_VIRGULA primary_expr ( R_EQ | R_SHORT_VAR) R_RANGE range
  150. ;
  151. range
  152. : expression
  153. ;
  154. // Fim Lacos -> Ini Retorno de funcao
  155. return_stmt
  156. : R_RETURN expression_list?
  157. ;
  158. //Regra produz recurssao de parametros em uma funcao.
  159. // Fim Retorno de funcao -> Ini
  160. expression_list
  161. : expression (R_VIRGULA expression)*
  162. ;
  163. expression
  164. // | expression BINARY_OP expression
  165. // | expression ('||' | '&&' | '==' | '!=' | '<' | '<=' | '>' | '>=' | '+' | '-' | '|' | '^' | '*' | '/' | '%' | '<<' | '>>' | '&' | '&^') expression
  166. : R_PAR_E expression R_PAR_D
  167. | unary_expr
  168. | expression op=(
  169. R_OP_MOD| R_END |
  170. R_OP_AST | R_OP_DIV|
  171. R_OP_ADD | R_OP_SUB ) expression
  172. | expression op=(R_SHIFT_LEFT | R_SHIFT_RIGHT) expression
  173. | expression op=(R_BIT_AND | R_BIT_XOR | R_BIT_OR) expression
  174. | expression op=(R_COMP_EQ | R_COMP_DIFF |
  175. R_COMP_LT | R_COMP_LTE |
  176. R_COMP_GT | R_COMP_GTE) expression
  177. | expression op=(R_OR | R_AND) expression
  178. ;
  179. unary_expr
  180. : op=(R_REFERENCE|R_OP_SUB|R_EXCLAMATION|R_OP_AST) primary_expr
  181. | primary_expr
  182. ;
  183. primary_expr returns[String id]
  184. : operand {$id = $operand.Value;} //#expr_operando// acesso de um operando
  185. | literal
  186. | pe = primary_expr index {$id = $pe.id;} //#expr_index// acesso de um index
  187. | pee = primary_expr expression_seletor {$id = $pee.id;} //#expr_selector// acesso de um atributo
  188. | pea = primary_expr arguments {$id = $pea.id;} //#expr_arguments// chamada de uma funcao ou metodo
  189. ;
  190. operand returns[String Value]
  191. : ID {$Value=$ID.text;}
  192. | primitive_value {$Value=$primitive_value.text;}
  193. ;
  194. literal
  195. : func_literal
  196. | composite
  197. ;
  198. base_stmt
  199. :
  200. | inc_dec
  201. | def_assign_op
  202. | dec_var_short
  203. | expression
  204. ;
  205. func_literal
  206. : R_FUNCTION arguments return_dec? def_block
  207. ;
  208. composite
  209. : literalType literal_value
  210. ;
  211. literalType
  212. : typename
  213. | structType
  214. | arrayType
  215. ;
  216. structType
  217. : R_STRUCT R_COL_E fieldDecl+ R_COL_D
  218. ;
  219. fieldDecl
  220. :(id_list type| anon_field) T_STRING?
  221. ;
  222. anon_field
  223. : R_OP_AST? typename
  224. ;
  225. arrayType
  226. : R_CHA_E expression? R_CHA_D typename
  227. ;
  228. inc_dec
  229. : expression op=( R_OP_PP| R_OP_SS)
  230. ;
  231. literal_value
  232. : R_COL_E ( (expression_list | element_list) R_VIRGULA )? R_COL_D
  233. ;
  234. element_list
  235. : keyed_element (R_VIRGULA keyed_element)*
  236. ;
  237. keyed_element
  238. : ID R_COLON expression
  239. ;
  240. expression_seletor
  241. : R_PONTO ID
  242. ;
  243. // Definicao de um bloco de instrucoes
  244. def_block locals[HashMap<String, String> retornos = new HashMap<>();]
  245. : R_COL_E statement* R_COL_D
  246. //|empty_block
  247. ;
  248. callExpr:
  249. | primary_expr arguments
  250. ;
  251. statement
  252. : dec_var_short //R_PT_VIRG
  253. | dec_var //R_PT_VIRG
  254. | def_assign_op //R_PT_VIRG
  255. | dec_const
  256. | inc_dec
  257. | primary_expr
  258. | label_stmt
  259. //| def_read R_PT_VIRG
  260. //| def_print R_PT_VIRG
  261. | return_stmt //R_PT_VIRG
  262. //| def_funcao_call R_PT_VIRG
  263. //| expression //R_PT_VIRG
  264. | delete //R_PT_VIRG
  265. | throwStmt
  266. | fallthroughStmt // dentro de um case do switch avanca para o proximo caso
  267. | def_if
  268. | def_for
  269. | def_switch
  270. | flux_control
  271. | try_catch
  272. ;
  273. // Fim da definicao de um bloco de instrucoes
  274. fallthroughStmt
  275. : R_FALLTHROUGH
  276. ;
  277. label_stmt
  278. : ID R_COLON statement
  279. ;
  280. throwStmt
  281. : R_THROW expression R_PT_VIRG
  282. ;
  283. // Inicio do try cath
  284. try_catch
  285. : R_TRY def_block
  286. catch_exception
  287. finally_try ?
  288. ;
  289. catch_exception
  290. : R_CATCH R_PAR_E dec_var R_PAR_D def_block
  291. ;
  292. finally_try
  293. : R_FINALLY def_block
  294. ;
  295. // Fim do try cath
  296. // Ids de controle de fluxo dentro do codigo
  297. flux_control locals[String control]
  298. : R_GOTO ID {$control = $R_GOTO.text;}
  299. | R_CONTINUE {$control = $R_CONTINUE.text;}
  300. | R_BREAK {$control = $R_BREAK.text;}
  301. ;
  302. // Definicao de new para alocar enderecos de memoria
  303. newstmt
  304. : R_NEW R_PAR_E type (R_VIRGULA T_INTEIRO)? R_PAR_D
  305. ;
  306. // Definicao de delete para liberar areas de memoria
  307. delete
  308. : R_DELETE expression( R_VIRGULA expression)?
  309. ;
  310. // Definicao dos tipos
  311. type returns[String ltype,String keytype,String valuetype]
  312. : typename {$ltype = $typename.ltype;}
  313. | indexedType {$ltype = $indexedType.ltype;}
  314. | mapType {$ltype = "map";}
  315. ;
  316. indexedType returns[String ltype]
  317. : typename {$ltype = $typename.ltype;}
  318. | index it=indexedType {$ltype = $it.ltype;}
  319. ;
  320. typename returns[String ltype, Boolean pointer]
  321. : ID {$ltype = $ID.text;}
  322. | qualifiedId {$ltype = $qualifiedId.text;}
  323. | baseType {$ltype = $baseType.ltype;}
  324. | R_OP_AST tn=typename {$ltype = $tn.ltype;}
  325. ;
  326. qualifiedId
  327. : ID R_PONTO ID
  328. ;
  329. baseType returns[String ltype]
  330. : R_INT {$ltype = $R_INT.text;}
  331. | R_BOOL {$ltype = $R_BOOL.text;}
  332. | R_CHAR {$ltype = $R_CHAR.text;}
  333. | R_STRING {$ltype = $R_STRING.text;}
  334. | R_ERROR {$ltype = $R_ERROR.text;}
  335. ;
  336. mapType returns[String keytype,String valuetype]
  337. : R_MAP R_CHA_E k=type R_CHA_D v=type {
  338. $keytype=$k.text;
  339. $valuetype=$v.text;
  340. }
  341. ;
  342. buildtags
  343. : build_stmt+
  344. ;
  345. build_stmt
  346. : R_AT ID R_COLON buildvalue (R_VIRGULA buildvalue)*
  347. ;
  348. //buildtag
  349. // : BUILD_TARGET
  350. // | BUILD_PROFILE
  351. // | BUILD_CORESETTINGS
  352. // ;
  353. buildvalue
  354. : ID
  355. | R_LIT_STRING
  356. ;
  357. //DEFINICAO DE TOKENS ----------------------------------------------------------
  358. //BUILD_TARGET :'target';
  359. //BUILD_PROFILE :'profile';
  360. //BUILD_CORESETTINGS :'coresettings';
  361. R_BIT_AND : '&';
  362. R_BIT_XOR : '^';
  363. R_BIT_OR : '|';
  364. R_SHIFT_LEFT : '<<';
  365. R_SHIFT_RIGHT : '>>';
  366. R_AT : '@';
  367. R_DBL_SLASH : '//';
  368. R_REFERENCE : '&';
  369. R_INTERFACE : 'interface';
  370. R_RANGE : 'range';
  371. R_STRUCT : 'struct';
  372. R_OP_MOD : '%';
  373. R_VAR : 'var';
  374. R_SHORT_VAR : ':=';
  375. R_GOTO : 'goto';
  376. R_FALLTHROUGH : 'fallthrough';
  377. R_MAP : 'map';
  378. R_ERROR : 'error';
  379. R_NEW : 'new';
  380. R_DELETE : 'delete';
  381. R_TRY : 'try';
  382. R_CATCH : 'catch';
  383. R_FINALLY : 'finally';
  384. R_DEFAULT : 'default';
  385. //R_PRINT : 'print';
  386. //R_READ : 'read';
  387. R_IMPORTE : 'import';
  388. //R_INIT_LIBRARY : 'library';
  389. R_PACKAGE : 'package';
  390. //R_THREAD : 'thread';
  391. R_TRAP : 'trap';
  392. R_INT : 'int';
  393. R_CHAR : 'char';
  394. R_BOOL : 'bool';
  395. R_STRING : 'string';
  396. //R_OBJECT : 'object';
  397. //R_ARRAY : 'array';
  398. //R_EXCEPTION : 'Exception';
  399. R_TIPO : 'type';
  400. R_FUNCTION : 'func';
  401. //R_METHOD : 'method';ARO
  402. R_CONSTANTE : 'const';
  403. R_IN : 'in';
  404. R_SWITCH : 'switch';
  405. R_CASE : 'case';
  406. R_FOR : 'for';
  407. R_RETURN : 'return';
  408. R_CONTINUE : 'continue';
  409. R_BREAK : 'break';
  410. R_THROW : 'throw';
  411. R_ELSE : 'else';
  412. R_IF : 'if';
  413. //R_ASYNC : 'async';
  414. //T_TRAP_SYNC : 'SYNC';
  415. //T_TRAP_EXEC : 'EXEC';
  416. //T_TRAP_SYNEXEC : 'SYNEXEC';
  417. //T_TRAP_SEND : 'SEND';
  418. //T_TRAP_RECEIVE : 'RECEIVE';
  419. R_EXCLAMATION : '!';
  420. R_PT_VIRG : ';';
  421. R_PAR_E : '(';
  422. R_PAR_D : ')';
  423. R_COL_E : '{';
  424. R_COL_D : '}';
  425. R_CHA_E : '[';
  426. R_CHA_D : ']';
  427. R_EQ : '=';
  428. R_VIRGULA : ',';
  429. R_PONTO : '.';
  430. R_COLON : ':';
  431. R_END : '&' ;
  432. R_AND : '&&' ;
  433. R_OR : '||';
  434. //Comparison
  435. R_COMP_EQ : '==' ;
  436. R_COMP_DIFF : '!=';
  437. R_COMP_LTE : '<=' ;
  438. R_COMP_LT : '<' ;
  439. R_COMP_GTE : '>=' ;
  440. R_COMP_GT : '>' ;
  441. //assing
  442. R_ASSIG_EXTEND : '::';
  443. R_ASSIG_COPY : ':';
  444. //arithmetic
  445. R_OP_DIV : '/';
  446. R_OP_ADD : '+';
  447. R_OP_SUB : '-';
  448. R_OP_AST : '*';
  449. R_OP_NEG : '!';
  450. R_OP_SS : '--';
  451. R_OP_PP : '++';
  452. R_UND : '_';
  453. R_INTERROGATION : '?';
  454. T_CHAR : '\'' .? '\'' ;
  455. T_STRING : '"' .*? '"' ;
  456. T_BOOL_TRUE : 'true';
  457. T_BOOL_FALSE : 'false' ;
  458. T_NULL : 'nil';
  459. ID : LETTER ( LETTER | DIGITO )*;
  460. T_INTEIRO : DIGITO+ ;
  461. fragment LETTER : ('a'..'z' | 'A'..'Z' | '_') ;
  462. fragment DIGITO : '0'..'9' ;
  463. //ID : CHARACTER ( CHARACTER | DIGITO )* ;
  464. //fragment CHARACTER : [_a-zA-Z]
  465. // ;
  466. R_LIT_STRING : '`' .*? '`';
  467. R_LINE_COMMENT : '//' .*? '\r'? '\n' -> skip ; // Match "#" stuff '\n'
  468. R_WS : (' '|'\t'|'\r'|'\n')+ -> skip ;
  469. R_COMMENT : '/*' .*? '*/' -> skip ; // Match "/*" stuff "*/"
  470. /*
  471. /*
  472. : ID literal_value #expr_literal
  473. | R_REFERENCE expression #expr_pointer
  474. | R_PAR_E expression R_PAR_D #expr_par
  475. | expression (R_PT_VIRG expression) #expr_chain_operacoes
  476. | expression op=(R_OP_AST | R_OP_DIV) expression #expr_arit_md
  477. | expression op=(R_OP_ADD | R_OP_SUB) expression #expr_arit_as
  478. | expression op=(R_COMP_LT | R_COMP_GT | R_COMP_LTE | R_COMP_GTE) expression #expr_comp_lt
  479. | expression op=(R_COMP_DIFF | R_COMP_EQ) expression #expr_comp_eqd
  480. | expression R_AND expression #expr_and
  481. | expression R_OR expression #expr_or
  482. | expression primitive_value #expr_primitive_value
  483. | expression ( R_OP_PP| R_OP_SS) #expr_inc_dec
  484. | R_OP_SUB expression #expr_negative
  485. | R_PAR_E type R_PAR_D expression #expr_assertion
  486. | R_EXCLAMATION expression #expr_neg_bool
  487. | expression R_INTERROGATION expression R_COLON expression #expr_ternary
  488. | def_function #expr_def_function
  489. | newstmt #expr_allocation
  490. | ID #expr_id
  491. ;
  492. inc_dec_stmt
  493. :
  494. ;
  495. def_object
  496. : R_COL_E (object_prop ( R_VIRGULA object_prop )*)? R_COL_D
  497. ;
  498. object_prop returns[String id]
  499. : ID R_COLON expression {$id = $ID.text;}
  500. ;
  501. address_access_dst
  502. : ID indices? access_prop ?
  503. ;
  504. access_prop
  505. : (R_PONTO ID indices?) access_prop ?
  506. ;
  507. address_access_src
  508. : ID indices? call_assinature? access_prop_src?
  509. ;
  510. access_prop_src
  511. : (R_PONTO ID indices? call_assinature?) access_prop_src?
  512. ;
  513. address
  514. : R_REFERENCE T_INTEIRO
  515. ;
  516. //| R_THREAD R_OP_AST* {$ltype = $R_THREAD.text;}
  517. //| R_FUNCTION R_OP_AST* {$ltype = $R_FUNCTION.text;}
  518. //| R_EXCEPTION R_OP_AST* {$ltype = $R_EXCEPTION.text;}
  519. //| def_method
  520. //| dec_trap
  521. //| dec_thread
  522. //| main
  523. call_from_return
  524. : (call_assinature | indices | access_prop_src) call_from_return ?
  525. ;
  526. boolena_operator
  527. : R_COMP_EQ | R_COMP_DIFF | R_COMP_LTE | R_COMP_GTE | R_COMP_LT | R_COMP_GT
  528. ;
  529. value
  530. : expression #expressionValue
  531. //| def_array #arrayValue
  532. | def_object #objectValue
  533. | address #pureAddressValue
  534. ;
  535. def_array
  536. : R_CHA_E ( value ( R_VIRGULA value )* )? R_CHA_D
  537. ;
  538. gen_list
  539. : def_array
  540. | address_access_src
  541. | def_funcao_call
  542. ;
  543. def_print
  544. : R_PRINT R_PAR_E list_values R_PAR_D
  545. ;
  546. def_read
  547. : R_READ R_PAR_E list_vars R_PAR_D
  548. ;
  549. list_vars
  550. : address_access_src (R_VIRGULA address_access_src)*
  551. ;
  552. param_list_dec
  553. : dec_var (R_PT_VIRG dec_var)*
  554. ;
  555. main
  556. : R_FUNCTION R_MAIN R_PAR_E param_list_dec? R_PAR_D R_COLON return_list def_block
  557. ;
  558. dec_thread
  559. : R_IN ID R_PAR_E param_list_dec? R_PAR_D def_block
  560. ;
  561. dec_trap locals[String label]
  562. : R_TRAP name=trap_id R_PAR_E param_list_dec? R_PAR_D def_block {$label = $name.text;}
  563. ;
  564. trap_id
  565. : (T_TRAP_SYNC |T_TRAP_EXEC |T_TRAP_SYNEXEC |T_TRAP_SEND |T_TRAP_RECEIVE)
  566. ;
  567. */