SHARE
TWEET

Untitled

a guest Apr 20th, 2019 88 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. diff --git a/src/parser.rs b/src/parser.rs
  2. index 0900ea9..e31dc55 100644
  3. --- a/src/parser.rs
  4. +++ b/src/parser.rs
  5. @@ -1,5 +1,542 @@
  6.  use crate::lexer;
  7.  
  8. +// TODO:
  9. +// Trying to implement a better parser to support c11 full standard
  10. +// assmue the the lexer can contains all keywords in the c11 standard,
  11. +// and assumes it can manipulate the constants (integer, float) correctly.
  12. +//    Keywords:
  13. +
  14. +//    Identifiers
  15. +
  16. +//    Constants
  17. +
  18. +//    Punctuators
  19. +
  20. +//    Operators
  21. +
  22. +// TOKEN TYPE SHOULD CONTAINS:
  23. +//1.  IDENTIFIER
  24. +//2.  CONSTANT_TYPE:
  25. +//3.  I_CONSTANT F_CONSTANT STRING_LITERAL FUNC_NAME SIZEOF
  26. +//    PTR_OP INC_OP DEC_OP LEFT_OP RIGHT_OP LE_OP GE_OP EQ_OP NE_OP
  27. +//    AND_OP OR_OP MUL_ASSIGN DIV_ASSIGN MOD_ASSIGN ADD_ASSIGN
  28. +//    SUB_ASSIGN LEFT_ASSIGN RIGHT_ASSIGN AND_ASSIGN
  29. +//    XOR_ASSIGN OR_ASSIGN
  30. +//    TYPEDEF_NAME ENUMERATION_CONSTANT
  31. +//    TYPEDEF EXTERN STATIC AUTO REGISTER INLINE
  32. +//    CONST RESTRICT VOLATILE
  33. +//    BOOL CHAR SHORT INT LONG SIGNED UNSIGNED FLOAT DOUBLE VOID
  34. +//    COMPLEX IMAGINARY
  35. +//    STRUCT UNION ENUM ELLIPSIS
  36. +//    CASE DEFAULT IF ELSE SWITCH WHILE DO FOR GOTO CONTINUE BREAK RETURN
  37. +//    ALIGNAS ALIGNOF ATOMIC GENERIC NORETURN STATIC_ASSERT THREAD_LOCAL
  38. +
  39. +
  40. +// primary_expression
  41. +//     : IDENTIFIER
  42. +//     | constant
  43. +//     | string
  44. +//     | '(' expression ')'
  45. +//     | generic_selection
  46. +//     ;
  47. +
  48. +// constant
  49. +//     : I_CONSTANT        /* includes character_constant */
  50. +//     | F_CONSTANT
  51. +//     | ENUMERATION_CONSTANT  /* after it has been defined as such */
  52. +//     ;
  53. +
  54. +// enumeration_constant        /* before it has been defined as such */
  55. +//     : IDENTIFIER
  56. +//     ;
  57. +
  58. +// string
  59. +//     : STRING_LITERAL
  60. +//     | FUNC_NAME
  61. +//     ;
  62. +
  63. +// generic_selection
  64. +//     : GENERIC '(' assignment_expression ',' generic_assoc_list ')'
  65. +//     ;
  66. +
  67. +// generic_assoc_list
  68. +//     : generic_association
  69. +//     | generic_assoc_list ',' generic_association
  70. +//     ;
  71. +
  72. +// generic_association
  73. +//     : type_name ':' assignment_expression
  74. +//     | DEFAULT ':' assignment_expression
  75. +//     ;
  76. +
  77. +// postfix_expression
  78. +//     : primary_expression
  79. +//     | postfix_expression '[' expression ']'
  80. +//     | postfix_expression '(' ')'
  81. +//     | postfix_expression '(' argument_expression_list ')'
  82. +//     | postfix_expression '.' IDENTIFIER
  83. +//     | postfix_expression PTR_OP IDENTIFIER
  84. +//     | postfix_expression INC_OP
  85. +//     | postfix_expression DEC_OP
  86. +//     | '(' type_name ')' '{' initializer_list '}'
  87. +//     | '(' type_name ')' '{' initializer_list ',' '}'
  88. +//     ;
  89. +
  90. +// argument_expression_list
  91. +//     : assignment_expression
  92. +//     | argument_expression_list ',' assignment_expression
  93. +//     ;
  94. +
  95. +// unary_expression
  96. +//     : postfix_expression
  97. +//     | INC_OP unary_expression
  98. +//     | DEC_OP unary_expression
  99. +//     | unary_operator cast_expression
  100. +//     | SIZEOF unary_expression
  101. +//     | SIZEOF '(' type_name ')'
  102. +//     | ALIGNOF '(' type_name ')'
  103. +//     ;
  104. +
  105. +// unary_operator
  106. +//     : '&'
  107. +//     | '*'
  108. +//     | '+'
  109. +//     | '-'
  110. +//     | '~'
  111. +//     | '!'
  112. +//     ;
  113. +
  114. +// cast_expression
  115. +//     : unary_expression
  116. +//     | '(' type_name ')' cast_expression
  117. +//     ;
  118. +
  119. +// multiplicative_expression
  120. +//     : cast_expression
  121. +//     | multiplicative_expression '*' cast_expression
  122. +//     | multiplicative_expression '/' cast_expression
  123. +//     | multiplicative_expression '%' cast_expression
  124. +//     ;
  125. +
  126. +// additive_expression
  127. +//     : multiplicative_expression
  128. +//     | additive_expression '+' multiplicative_expression
  129. +//     | additive_expression '-' multiplicative_expression
  130. +//     ;
  131. +
  132. +// shift_expression
  133. +//     : additive_expression
  134. +//     | shift_expression LEFT_OP additive_expression
  135. +//     | shift_expression RIGHT_OP additive_expression
  136. +//     ;
  137. +
  138. +// relational_expression
  139. +//     : shift_expression
  140. +//     | relational_expression '<' shift_expression
  141. +//     | relational_expression '>' shift_expression
  142. +//     | relational_expression LE_OP shift_expression
  143. +//     | relational_expression GE_OP shift_expression
  144. +//     ;
  145. +
  146. +// equality_expression
  147. +//     : relational_expression
  148. +//     | equality_expression EQ_OP relational_expression
  149. +//     | equality_expression NE_OP relational_expression
  150. +//     ;
  151. +
  152. +// and_expression
  153. +//     : equality_expression
  154. +//     | and_expression '&' equality_expression
  155. +//     ;
  156. +
  157. +// exclusive_or_expression
  158. +//     : and_expression
  159. +//     | exclusive_or_expression '^' and_expression
  160. +//     ;
  161. +
  162. +// inclusive_or_expression
  163. +//     : exclusive_or_expression
  164. +//     | inclusive_or_expression '|' exclusive_or_expression
  165. +//     ;
  166. +
  167. +// logical_and_expression
  168. +//     : inclusive_or_expression
  169. +//     | logical_and_expression AND_OP inclusive_or_expression
  170. +//     ;
  171. +
  172. +// logical_or_expression
  173. +//     : logical_and_expression
  174. +//     | logical_or_expression OR_OP logical_and_expression
  175. +//     ;
  176. +
  177. +// conditional_expression
  178. +//     : logical_or_expression
  179. +//     | logical_or_expression '?' expression ':' conditional_expression
  180. +//     ;
  181. +
  182. +// assignment_expression
  183. +//     : conditional_expression
  184. +//     | unary_expression assignment_operator assignment_expression
  185. +//     ;
  186. +
  187. +// assignment_operator
  188. +//     : '='
  189. +//     | MUL_ASSIGN
  190. +//     | DIV_ASSIGN
  191. +//     | MOD_ASSIGN
  192. +//     | ADD_ASSIGN
  193. +//     | SUB_ASSIGN
  194. +//     | LEFT_ASSIGN
  195. +//     | RIGHT_ASSIGN
  196. +//     | AND_ASSIGN
  197. +//     | XOR_ASSIGN
  198. +//     | OR_ASSIGN
  199. +//     ;
  200. +
  201. +// expression
  202. +//     : assignment_expression
  203. +//     | expression ',' assignment_expression
  204. +//     ;
  205. +
  206. +// constant_expression
  207. +//     : conditional_expression    /* with constraints */
  208. +//     ;
  209. +
  210. +// declaration
  211. +//     : declaration_specifiers ';'
  212. +//     | declaration_specifiers init_declarator_list ';'
  213. +//     | static_assert_declaration
  214. +//     ;
  215. +
  216. +// declaration_specifiers
  217. +//     : storage_class_specifier declaration_specifiers
  218. +//     | storage_class_specifier
  219. +//     | type_specifier declaration_specifiers
  220. +//     | type_specifier
  221. +//     | type_qualifier declaration_specifiers
  222. +//     | type_qualifier
  223. +//     | function_specifier declaration_specifiers
  224. +//     | function_specifier
  225. +//     | alignment_specifier declaration_specifiers
  226. +//     | alignment_specifier
  227. +//     ;
  228. +
  229. +// init_declarator_list
  230. +//     : init_declarator
  231. +//     | init_declarator_list ',' init_declarator
  232. +//     ;
  233. +
  234. +// init_declarator
  235. +//     : declarator '=' initializer
  236. +//     | declarator
  237. +//     ;
  238. +
  239. +// storage_class_specifier
  240. +//     : TYPEDEF   /* identifiers must be flagged as TYPEDEF_NAME */
  241. +//     | EXTERN
  242. +//     | STATIC
  243. +//     | THREAD_LOCAL
  244. +//     | AUTO
  245. +//     | REGISTER
  246. +//     ;
  247. +
  248. +// type_specifier
  249. +//     : VOID
  250. +//     | CHAR
  251. +//     | SHORT
  252. +//     | INT
  253. +//     | LONG
  254. +//     | FLOAT
  255. +//     | DOUBLE
  256. +//     | SIGNED
  257. +//     | UNSIGNED
  258. +//     | BOOL
  259. +//     | COMPLEX
  260. +//     | IMAGINARY     /* non-mandated extension */
  261. +//     | atomic_type_specifier
  262. +//     | struct_or_union_specifier
  263. +//     | enum_specifier
  264. +//     | TYPEDEF_NAME      /* after it has been defined as such */
  265. +//     ;
  266. +
  267. +// struct_or_union_specifier
  268. +//     : struct_or_union '{' struct_declaration_list '}'
  269. +//     | struct_or_union IDENTIFIER '{' struct_declaration_list '}'
  270. +//     | struct_or_union IDENTIFIER
  271. +//     ;
  272. +
  273. +// struct_or_union
  274. +//     : STRUCT
  275. +//     | UNION
  276. +//     ;
  277. +
  278. +// struct_declaration_list
  279. +//     : struct_declaration
  280. +//     | struct_declaration_list struct_declaration
  281. +//     ;
  282. +
  283. +// struct_declaration
  284. +//     : specifier_qualifier_list ';'  /* for anonymous struct/union */
  285. +//     | specifier_qualifier_list struct_declarator_list ';'
  286. +//     | static_assert_declaration
  287. +//     ;
  288. +
  289. +// specifier_qualifier_list
  290. +//     : type_specifier specifier_qualifier_list
  291. +//     | type_specifier
  292. +//     | type_qualifier specifier_qualifier_list
  293. +//     | type_qualifier
  294. +//     ;
  295. +
  296. +// struct_declarator_list
  297. +//     : struct_declarator
  298. +//     | struct_declarator_list ',' struct_declarator
  299. +//     ;
  300. +
  301. +// struct_declarator
  302. +//     : ':' constant_expression
  303. +//     | declarator ':' constant_expression
  304. +//     | declarator
  305. +//     ;
  306. +
  307. +// enum_specifier
  308. +//     : ENUM '{' enumerator_list '}'
  309. +//     | ENUM '{' enumerator_list ',' '}'
  310. +//     | ENUM IDENTIFIER '{' enumerator_list '}'
  311. +//     | ENUM IDENTIFIER '{' enumerator_list ',' '}'
  312. +//     | ENUM IDENTIFIER
  313. +//     ;
  314. +
  315. +// enumerator_list
  316. +//     : enumerator
  317. +//     | enumerator_list ',' enumerator
  318. +//     ;
  319. +
  320. +// enumerator  /* identifiers must be flagged as ENUMERATION_CONSTANT */
  321. +//     : enumeration_constant '=' constant_expression
  322. +//     | enumeration_constant
  323. +//     ;
  324. +
  325. +// atomic_type_specifier
  326. +//     : ATOMIC '(' type_name ')'
  327. +//     ;
  328. +
  329. +// type_qualifier
  330. +//     : CONST
  331. +//     | RESTRICT
  332. +//     | VOLATILE
  333. +//     | ATOMIC
  334. +//     ;
  335. +
  336. +// function_specifier
  337. +//     : INLINE
  338. +//     | NORETURN
  339. +//     ;
  340. +
  341. +// alignment_specifier
  342. +//     : ALIGNAS '(' type_name ')'
  343. +//     | ALIGNAS '(' constant_expression ')'
  344. +//     ;
  345. +
  346. +// declarator
  347. +//     : pointer direct_declarator
  348. +//     | direct_declarator
  349. +//     ;
  350. +
  351. +// direct_declarator
  352. +//     : IDENTIFIER
  353. +//     | '(' declarator ')'
  354. +//     | direct_declarator '[' ']'
  355. +//     | direct_declarator '[' '*' ']'
  356. +//     | direct_declarator '[' STATIC type_qualifier_list assignment_expression ']'
  357. +//     | direct_declarator '[' STATIC assignment_expression ']'
  358. +//     | direct_declarator '[' type_qualifier_list '*' ']'
  359. +//     | direct_declarator '[' type_qualifier_list STATIC assignment_expression ']'
  360. +//     | direct_declarator '[' type_qualifier_list assignment_expression ']'
  361. +//     | direct_declarator '[' type_qualifier_list ']'
  362. +//     | direct_declarator '[' assignment_expression ']'
  363. +//     | direct_declarator '(' parameter_type_list ')'
  364. +//     | direct_declarator '(' ')'
  365. +//     | direct_declarator '(' identifier_list ')'
  366. +//     ;
  367. +
  368. +// pointer
  369. +//     : '*' type_qualifier_list pointer
  370. +//     | '*' type_qualifier_list
  371. +//     | '*' pointer
  372. +//     | '*'
  373. +//     ;
  374. +
  375. +// type_qualifier_list
  376. +//     : type_qualifier
  377. +//     | type_qualifier_list type_qualifier
  378. +//     ;
  379. +
  380. +// parameter_type_list
  381. +//     : parameter_list ',' ELLIPSIS
  382. +//     | parameter_list
  383. +//     ;
  384. +
  385. +// parameter_list
  386. +//     : parameter_declaration
  387. +//     | parameter_list ',' parameter_declaration
  388. +//     ;
  389. +
  390. +// parameter_declaration
  391. +//     : declaration_specifiers declarator
  392. +//     | declaration_specifiers abstract_declarator
  393. +//     | declaration_specifiers
  394. +//     ;
  395. +
  396. +// identifier_list
  397. +//     : IDENTIFIER
  398. +//     | identifier_list ',' IDENTIFIER
  399. +//     ;
  400. +
  401. +// type_name
  402. +//     : specifier_qualifier_list abstract_declarator
  403. +//     | specifier_qualifier_list
  404. +//     ;
  405. +
  406. +// abstract_declarator
  407. +//     : pointer direct_abstract_declarator
  408. +//     | pointer
  409. +//     | direct_abstract_declarator
  410. +//     ;
  411. +
  412. +// direct_abstract_declarator
  413. +//     : '(' abstract_declarator ')'
  414. +//     | '[' ']'
  415. +//     | '[' '*' ']'
  416. +//     | '[' STATIC type_qualifier_list assignment_expression ']'
  417. +//     | '[' STATIC assignment_expression ']'
  418. +//     | '[' type_qualifier_list STATIC assignment_expression ']'
  419. +//     | '[' type_qualifier_list assignment_expression ']'
  420. +//     | '[' type_qualifier_list ']'
  421. +//     | '[' assignment_expression ']'
  422. +//     | direct_abstract_declarator '[' ']'
  423. +//     | direct_abstract_declarator '[' '*' ']'
  424. +//     | direct_abstract_declarator '[' STATIC type_qualifier_list assignment_expression ']'
  425. +//     | direct_abstract_declarator '[' STATIC assignment_expression ']'
  426. +//     | direct_abstract_declarator '[' type_qualifier_list assignment_expression ']'
  427. +//     | direct_abstract_declarator '[' type_qualifier_list STATIC assignment_expression ']'
  428. +//     | direct_abstract_declarator '[' type_qualifier_list ']'
  429. +//     | direct_abstract_declarator '[' assignment_expression ']'
  430. +//     | '(' ')'
  431. +//     | '(' parameter_type_list ')'
  432. +//     | direct_abstract_declarator '(' ')'
  433. +//     | direct_abstract_declarator '(' parameter_type_list ')'
  434. +//     ;
  435. +
  436. +// initializer
  437. +//     : '{' initializer_list '}'
  438. +//     | '{' initializer_list ',' '}'
  439. +//     | assignment_expression
  440. +//     ;
  441. +
  442. +// initializer_list
  443. +//     : designation initializer
  444. +//     | initializer
  445. +//     | initializer_list ',' designation initializer
  446. +//     | initializer_list ',' initializer
  447. +//     ;
  448. +
  449. +// designation
  450. +//     : designator_list '='
  451. +//     ;
  452. +
  453. +// designator_list
  454. +//     : designator
  455. +//     | designator_list designator
  456. +//     ;
  457. +
  458. +// designator
  459. +//     : '[' constant_expression ']'
  460. +//     | '.' IDENTIFIER
  461. +//     ;
  462. +
  463. +// static_assert_declaration
  464. +//     : STATIC_ASSERT '(' constant_expression ',' STRING_LITERAL ')' ';'
  465. +//     ;
  466. +
  467. +// statement
  468. +//     : labeled_statement
  469. +//     | compound_statement
  470. +//     | expression_statement
  471. +//     | selection_statement
  472. +//     | iteration_statement
  473. +//     | jump_statement
  474. +//     ;
  475. +
  476. +// labeled_statement
  477. +//     : IDENTIFIER ':' statement
  478. +//     | CASE constant_expression ':' statement
  479. +//     | DEFAULT ':' statement
  480. +//     ;
  481. +
  482. +// compound_statement
  483. +//     : '{' '}'
  484. +//     | '{'  block_item_list '}'
  485. +//     ;
  486. +
  487. +// block_item_list
  488. +//     : block_item
  489. +//     | block_item_list block_item
  490. +//     ;
  491. +
  492. +// block_item
  493. +//     : declaration
  494. +//     | statement
  495. +//     ;
  496. +
  497. +// expression_statement
  498. +//     : ';'
  499. +//     | expression ';'
  500. +//     ;
  501. +
  502. +// selection_statement
  503. +//     : IF '(' expression ')' statement ELSE statement
  504. +//     | IF '(' expression ')' statement
  505. +//     | SWITCH '(' expression ')' statement
  506. +//     ;
  507. +
  508. +// iteration_statement
  509. +//     : WHILE '(' expression ')' statement
  510. +//     | DO statement WHILE '(' expression ')' ';'
  511. +//     | FOR '(' expression_statement expression_statement ')' statement
  512. +//     | FOR '(' expression_statement expression_statement expression ')' statement
  513. +//     | FOR '(' declaration expression_statement ')' statement
  514. +//     | FOR '(' declaration expression_statement expression ')' statement
  515. +//     ;
  516. +
  517. +// jump_statement
  518. +//     : GOTO IDENTIFIER ';'
  519. +//     | CONTINUE ';'
  520. +//     | BREAK ';'
  521. +//     | RETURN ';'
  522. +//     | RETURN expression ';'
  523. +//     ;
  524. +
  525. +// translation_unit
  526. +//     : external_declaration
  527. +//     | translation_unit external_declaration
  528. +//     ;
  529. +
  530. +// external_declaration
  531. +//     : function_definition
  532. +//     | declaration
  533. +//     ;
  534. +
  535. +// function_definition
  536. +//     : declaration_specifiers declarator declaration_list compound_statement
  537. +//     | declaration_specifiers declarator compound_statement
  538. +//     ;
  539. +
  540. +// declaration_list
  541. +//     : declaration
  542. +//     | declaration_list declaration
  543. +//     ;
  544. +
  545.  #[derive(Eq, PartialEq, Clone, Debug)]
  546.  pub enum NodeType {
  547.      Prog(String),
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top