PageRenderTime 50ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/jruby-1.7.3/src/org/jruby/parser/JavaSignatureParser.y

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Happy | 609 lines | 537 code | 72 blank | 0 comment | 0 complexity | 981bf5b7efca010efd786081a4ceb5a9 MD5 | raw file
  1. /*
  2. * We started with a full Java grammar from JavaParser (cup) implementation
  3. * (since it was an LALR grammar already) and then stripped out all bits of
  4. * the grammar unrelated to Java method signature parsing. We also changed the
  5. * grammar to accept signatures which do not specify the parameter names. So,
  6. * 'void foo(int)' is as 'void foo(int name)'. This grammar also only works
  7. * with Jay which is another LALR grammar compiler compiler tool.
  8. *
  9. * The output this tool generates is subject to our tri-license (GPL,LGPL,
  10. * and EPL), but this source file itself is released only under the terms
  11. * of the GPL.
  12. *
  13. * This program is released under the terms of the GPL; see the file
  14. * COPYING for more details. There is NO WARRANTY on this code.
  15. */
  16. %{
  17. package org.jruby.parser;
  18. import java.io.InputStream;
  19. import java.io.IOException;
  20. import java.util.ArrayList;
  21. import java.util.List;
  22. import org.jruby.ast.java_signature.Annotation;
  23. import org.jruby.ast.java_signature.AnnotationExpression;
  24. import org.jruby.ast.java_signature.AnnotationParameter;
  25. import org.jruby.ast.java_signature.ArrayAnnotationExpression;
  26. import org.jruby.ast.java_signature.ArrayTypeNode;
  27. import org.jruby.ast.java_signature.CharacterLiteral;
  28. import org.jruby.ast.java_signature.ConstructorSignatureNode;
  29. import org.jruby.ast.java_signature.DefaultAnnotationParameter;
  30. import org.jruby.ast.java_signature.MethodSignatureNode;
  31. import org.jruby.ast.java_signature.Literal;
  32. import org.jruby.ast.java_signature.Modifier;
  33. import org.jruby.ast.java_signature.ParameterNode;
  34. import org.jruby.ast.java_signature.PrimitiveTypeNode;
  35. import org.jruby.ast.java_signature.ReferenceTypeNode;
  36. import org.jruby.ast.java_signature.SignatureNode;
  37. import org.jruby.ast.java_signature.StringLiteral;
  38. import org.jruby.ast.java_signature.TypeNode;
  39. import org.jruby.lexer.JavaSignatureLexer;
  40. public class JavaSignatureParser {
  41. private static JavaSignatureParser parser = new JavaSignatureParser();
  42. public static SignatureNode parse(InputStream in) throws IOException, ParserSyntaxException {
  43. return (SignatureNode) parser.yyparse(JavaSignatureLexer.create(in));
  44. }
  45. %}
  46. // Primitive types
  47. %token <String> BOOLEAN // 'boolean'
  48. %token <String> BYTE // 'byte'
  49. %token <String> SHORT // 'short'
  50. %token <String> INT // 'int'
  51. %token <String> LONG // 'long'
  52. %token <String> CHAR // 'char'
  53. %token <String> FLOAT // 'float'
  54. %token <String> DOUBLE // 'double'
  55. %token <String> VOID // 'void'
  56. // modifiers
  57. %token <String> PUBLIC // 'public'
  58. %token <String> PROTECTED // 'protected'
  59. %token <String> PRIVATE // 'private'
  60. %token <String> STATIC // 'static'
  61. %token <String> ABSTRACT // 'abstract'
  62. %token <String> FINAL // 'final'
  63. %token <String> NATIVE // 'native'
  64. %token <String> SYNCHRONIZED // 'synchronized'
  65. %token <String> TRANSIENT // 'transient'
  66. %token <String> VOLATILE // 'volatile'
  67. %token <String> STRICTFP // 'strictfp'
  68. // identifier (e.g. setFoo)
  69. %token <String> IDENTIFIER
  70. // syntax markers
  71. %token <String> AND // '&'
  72. %token <String> AT // '@'
  73. %token <String> DOT // '.'
  74. %token <String> COMMA // ','
  75. %token <String> ELLIPSIS // '...' or \u2026
  76. %token <String> EQUAL // '='
  77. %token <String> LCURLY // '{'
  78. %token <String> RCURLY // '}'
  79. %token <String> LPAREN // '('
  80. %token <String> RPAREN // ')'
  81. %token <String> LBRACK // '['
  82. %token <String> RBRACK // ']'
  83. %token <String> QUESTION // '?'
  84. %token <String> LT // '<'
  85. %token <String> GT // '>'
  86. %token <String> THROWS // 'throws'
  87. %token <String> EXTENDS // 'extends'
  88. %token <String> SUPER // 'super'
  89. %token <String> SUPER // 'super'
  90. %token <String> RSHIFT // '>>'
  91. %token <String> URSHIFT // '>>>'
  92. %token <String> QQ // '"'
  93. %token <String> Q // "'"
  94. %token <String> CHARACTER_LITERAL
  95. %token <String> STRING_LITERAL
  96. %type <MethodSignatureNode> method_declarator, method_header
  97. %type <ConstructorSignatureNode> constructor_declarator, constructor_declaration
  98. %type <List> formal_parameter_list_opt, formal_parameter_list // <ParameterNode>
  99. %type <List> modifiers_opt, modifiers, modifiers_none, throws, class_type_list
  100. %type <List> annotation_params_opt, annotation_params, annotation_params_none
  101. %type <ParameterNode> formal_parameter
  102. %type <TypeNode> primitive_type, type
  103. %type <ReferenceTypeNode> class_or_interface, class_or_interface_type, array_type
  104. %type <ReferenceTypeNode> interface_type, class_type, reference_type
  105. %type <String> name, type_variable, variable_declarator_id
  106. %type <String> type_bound_1, additional_bound, additional_bound_list_1
  107. %type <String> wildcard, type_argument, type_argument_list
  108. %type <String> type_argument_1, type_argument_2, type_argument_3
  109. %type <String> wildcard_1, wildcard_2, wildcard_3
  110. %type <String> reference_type_1, reference_type_2, reference_type_3
  111. %type <String> type_argument_list_1, type_argument_list_2, type_argument_list_3
  112. %type <String> type_parameter, type_parameter_1
  113. %type <String> type_parameter_list, type_parameter_list_1,
  114. %type <String> type_bound_opt, type_bound, additional_bound_list, additional_bound_list_opt
  115. %type <String> annotation_name
  116. %type <Object> modifier // Can be either modifier enum or Annotation instance
  117. %type <ArrayTypeNode> dims
  118. %type <Object> none
  119. %type <SignatureNode> program
  120. %type <Annotation> annotation
  121. %type <AnnotationParameter> annotation_param
  122. %type <AnnotationExpression> annotation_value
  123. %type <List> annotation_array_values
  124. %type <Literal> literal
  125. %%
  126. program : method_header {
  127. $$ = $1;
  128. } | constructor_declaration {
  129. $$ = $1;
  130. }
  131. type : primitive_type | reference_type { $$ = $<TypeNode>1; }
  132. // PrimitiveTypeNode
  133. primitive_type : BYTE {
  134. $$ = PrimitiveTypeNode.BYTE;
  135. }
  136. | SHORT {
  137. $$ = PrimitiveTypeNode.SHORT;
  138. }
  139. | INT {
  140. $$ = PrimitiveTypeNode.INT;
  141. }
  142. | LONG {
  143. $$ = PrimitiveTypeNode.LONG;
  144. }
  145. | CHAR {
  146. $$ = PrimitiveTypeNode.CHAR;
  147. }
  148. | BOOLEAN {
  149. $$ = PrimitiveTypeNode.BOOLEAN;
  150. }
  151. | FLOAT {
  152. $$ = PrimitiveTypeNode.FLOAT;
  153. }
  154. | DOUBLE {
  155. $$ = PrimitiveTypeNode.DOUBLE;
  156. }
  157. // ReferenceTypeNode
  158. reference_type : class_or_interface_type {
  159. $$ = $1;
  160. }
  161. | array_type {
  162. $$ = $<ReferenceTypeNode>1;
  163. }
  164. // String
  165. type_variable : IDENTIFIER {
  166. $$ = $1;
  167. }
  168. // ReferenceTypeNode
  169. class_or_interface : name {
  170. $$ = new ReferenceTypeNode($1);
  171. }
  172. | class_or_interface LT type_argument_list_1 DOT name {
  173. String genericTyping = "<" + $3 + "." + $5;
  174. $$ = $1;
  175. $1.setGenericsTyping(genericTyping);
  176. }
  177. // ReferenceTypeNode
  178. class_or_interface_type : class_or_interface
  179. | class_or_interface LT type_argument_list_1 {
  180. String genericTyping = "<" + $3;
  181. $$ = $1;
  182. $1.setGenericsTyping(genericTyping);
  183. }
  184. // ReferenceTypeNode
  185. class_type : class_or_interface_type
  186. // ReferenceTypeNode
  187. interface_type : class_or_interface_type
  188. // ReferenceTypeNode
  189. array_type : primitive_type dims {
  190. $2.setTypeForArray($1);
  191. $$ = $2;
  192. }
  193. | name dims {
  194. $2.setTypeForArray(new ReferenceTypeNode($1));
  195. $$ = $2;
  196. }
  197. | class_or_interface LT type_argument_list_1 DOT name dims {
  198. $1.setGenericsTyping("<" + $3 + "." + $5);
  199. $6.setTypeForArray($1);
  200. $$ = $6;
  201. }
  202. | class_or_interface LT type_argument_list_1 dims {
  203. $1.setGenericsTyping("<" + $3);
  204. $4.setTypeForArray($1);
  205. $$ = $4;
  206. }
  207. // String
  208. wildcard : QUESTION {
  209. $$ = "?";
  210. } | QUESTION EXTENDS reference_type {
  211. $$ = "? extends " + $3.getFullyTypedName();
  212. } | QUESTION SUPER reference_type {
  213. $$ = "? super " + $3.getFullyTypedName();
  214. }
  215. // String
  216. wildcard_1 : QUESTION GT {
  217. $$ = "?>";
  218. } | QUESTION EXTENDS reference_type_1 {
  219. $$ = "? extends " + $3;
  220. } | QUESTION SUPER reference_type_1 {
  221. $$ = "? super " + $3;
  222. }
  223. // String
  224. wildcard_2 : QUESTION RSHIFT {
  225. $$ = "?>>";
  226. } | QUESTION EXTENDS reference_type_2 {
  227. $$ = "? extends " + $3;
  228. } | QUESTION SUPER reference_type_2 {
  229. $$ = "? super " + $3;
  230. }
  231. // String
  232. wildcard_3 : QUESTION URSHIFT {
  233. $$ = "?>>";
  234. } | QUESTION EXTENDS reference_type_3 {
  235. $$ = "? extends " + $3;
  236. } | QUESTION SUPER reference_type_3 {
  237. $$ = "? super " + $3;
  238. }
  239. // String
  240. reference_type_1 : reference_type GT {
  241. $$ = $1.getFullyTypedName() + ">";
  242. } | class_or_interface LT type_argument_list_2 {
  243. $$ = $1.getFullyTypedName() + "<" + $3;
  244. }
  245. // String
  246. reference_type_2 : reference_type RSHIFT {
  247. $$ = $1.getFullyTypedName() + ">>";
  248. } | class_or_interface LT type_argument_list_3 {
  249. $$ = $1.getFullyTypedName() + "<" + $3;
  250. }
  251. // String
  252. reference_type_3 : reference_type URSHIFT {
  253. $$ = $1.getFullyTypedName() + ">>>";
  254. }
  255. // String
  256. type_argument_list : type_argument {
  257. $$ = $1;
  258. }
  259. | type_argument_list COMMA type_argument {
  260. $$ = $1 + ", " + $3;
  261. }
  262. // String
  263. type_argument_list_1 : type_argument_1
  264. | type_argument_list COMMA type_argument_1 {
  265. $$ = $1 + ", " + $3;
  266. }
  267. // String
  268. type_argument_list_2 : type_argument_2
  269. | type_argument_list COMMA type_argument_2 {
  270. $$ = $1 + ", " + $3;
  271. }
  272. // String
  273. type_argument_list_3 : type_argument_3
  274. | type_argument_list COMMA type_argument_3 {
  275. $$ = $1 + ", " + $3;
  276. }
  277. // String
  278. type_argument : reference_type {
  279. $$ = $1.getFullyTypedName();
  280. }
  281. | wildcard
  282. // String
  283. type_argument_1 : reference_type_1 | wildcard_1
  284. // String
  285. type_argument_2 : reference_type_2 | wildcard_2
  286. // String
  287. type_argument_3 : reference_type_3 | wildcard_3
  288. // List<Object>
  289. modifiers_opt : modifiers | modifiers_none
  290. // List<Object>
  291. modifiers : modifier {
  292. $$ = new ArrayList<Object>();
  293. $<List>$.add($1);
  294. }
  295. | modifiers modifier {
  296. $1.add($2);
  297. }
  298. // List<Object> -- This is just so we don't deal with null's.
  299. modifiers_none : { $$ = new ArrayList<Object>(); }
  300. // Object
  301. modifier : PUBLIC { $$ = Modifier.PUBLIC; }
  302. | PROTECTED { $$ = Modifier.PROTECTED; }
  303. | PRIVATE { $$ = Modifier.PRIVATE; }
  304. | STATIC { $$ = Modifier.STATIC; }
  305. | ABSTRACT { $$ = Modifier.ABSTRACT; }
  306. | FINAL { $$ = Modifier.FINAL; }
  307. | NATIVE { $$ = Modifier.NATIVE; }
  308. | SYNCHRONIZED { $$ = Modifier.SYNCHRONIZED; }
  309. | TRANSIENT { $$ = Modifier.TRANSIENT; }
  310. | VOLATILE { $$ = Modifier.VOLATILE; }
  311. | STRICTFP { $$ = Modifier.STRICTFP; }
  312. | annotation { $$ = $1; }
  313. // String
  314. name : IDENTIFIER { $$ = $1; } // Foo (or foo)
  315. | name DOT IDENTIFIER { $$ = $1 + "." + $3; } // foo.Foo
  316. // String -- we do not use this for any info
  317. dims : LBRACK RBRACK {
  318. $$ = new ArrayTypeNode();
  319. } | dims LBRACK RBRACK {
  320. $$ = new ArrayTypeNode($1);
  321. }
  322. // List<TypeNode>
  323. throws : THROWS class_type_list { $$ = $2; }
  324. | /* none */ { $$ = new ArrayList<TypeNode>(); }
  325. // List<TypeNode>
  326. class_type_list : class_type {
  327. $$ = new ArrayList<TypeNode>();
  328. $<List>$.add($1);
  329. }
  330. | class_type_list COMMA class_type {
  331. $<List>1.add($3);
  332. }
  333. // MethodSignatureNode
  334. method_declarator : IDENTIFIER LPAREN formal_parameter_list_opt RPAREN {
  335. $$ = new MethodSignatureNode($1, $3);
  336. }
  337. // List<ParameterNode>
  338. formal_parameter_list_opt : formal_parameter_list
  339. | /* none */ { $$ = new ArrayList<ParameterNode>(); }
  340. // List<ParameterNode>
  341. formal_parameter_list : formal_parameter {
  342. List<ParameterNode> list = new ArrayList<ParameterNode>();
  343. list.add($1);
  344. $$ = list;
  345. }
  346. | formal_parameter_list COMMA formal_parameter {
  347. $1.add($3);
  348. }
  349. // ParameterNode
  350. formal_parameter : type variable_declarator_id {
  351. $$ = new ParameterNode($1, $2);
  352. }
  353. | type {
  354. $$ = new ParameterNode($1, null);
  355. }
  356. | FINAL type variable_declarator_id {
  357. $$ = new ParameterNode($2, $3, true);
  358. }
  359. | FINAL type {
  360. $$ = new ParameterNode($2, null, true);
  361. }
  362. | type ELLIPSIS IDENTIFIER {
  363. $$ = new ParameterNode($1, $3, false, true);
  364. }
  365. | type ELLIPSIS {
  366. $$ = new ParameterNode($1, null, false, true);
  367. }
  368. | FINAL type ELLIPSIS IDENTIFIER {
  369. $$ = new ParameterNode($2, $4, true, true);
  370. }
  371. | FINAL type ELLIPSIS {
  372. $$ = new ParameterNode($2, null, true, true);
  373. }
  374. // String
  375. variable_declarator_id : IDENTIFIER {
  376. $$ = $1;
  377. } | variable_declarator_id LBRACK RBRACK {
  378. // We know this is always preceeded by 'type' production.
  379. $<Object>0 = new ArrayTypeNode($<TypeNode>0);
  380. $$ = $1;
  381. }
  382. // String
  383. type_parameter_list : type_parameter_list COMMA type_parameter {
  384. $$ = $1 + ", " + $3;
  385. } | type_parameter
  386. // String
  387. type_parameter_list_1 : type_parameter_1
  388. | type_parameter_list COMMA type_parameter_1 {
  389. $$ = $1 + ", " + $3;
  390. }
  391. // String
  392. type_parameter : type_variable type_bound_opt {
  393. $$ = $1 + $2;
  394. }
  395. // String
  396. type_parameter_1 : type_variable GT {
  397. $$ = $1 + ">";
  398. }
  399. | type_variable type_bound_1 {
  400. $$ = $1 + $2;
  401. }
  402. // String
  403. type_bound_1 : EXTENDS reference_type_1 {
  404. $$ = " extends " + $1;
  405. }
  406. | EXTENDS reference_type additional_bound_list_1 {
  407. $$ = " extends " + $2.getFullyTypedName() + $3;
  408. }
  409. // String
  410. type_bound_opt : type_bound
  411. | none {
  412. $$ = "";
  413. }
  414. // String
  415. type_bound : EXTENDS reference_type additional_bound_list_opt {
  416. $$ = "extends " + $2.getFullyTypedName() + $3;
  417. }
  418. // String
  419. additional_bound_list_opt : additional_bound_list
  420. | none {
  421. $$ = "";
  422. }
  423. // String
  424. additional_bound_list : additional_bound additional_bound_list {
  425. $$ = $1 + $2;
  426. } | additional_bound
  427. // String
  428. additional_bound_list_1 : additional_bound additional_bound_list_1 {
  429. $$ = $1 + $2;
  430. }
  431. | AND reference_type_1 {
  432. $$ = " & " + $1;
  433. }
  434. // String
  435. additional_bound : AND interface_type {
  436. $$ = " & " + $2.getFullyTypedName();
  437. }
  438. none : { $$ = null; }
  439. constructor_declaration : modifiers_opt constructor_declarator throws {
  440. $$ = $2;
  441. $<ConstructorSignatureNode>$.setModifiers($1);
  442. $<ConstructorSignatureNode>$.setThrows($3);
  443. } | modifiers_opt LT type_parameter_list_1 constructor_declarator throws {
  444. $$ = $4;
  445. $<ConstructorSignatureNode>$.setModifiers($1);
  446. $<ConstructorSignatureNode>$.setExtraTypeInfo("<" + $3);
  447. $<ConstructorSignatureNode>$.setThrows($5);
  448. }
  449. constructor_declarator : name LPAREN formal_parameter_list_opt RPAREN {
  450. $$ = new ConstructorSignatureNode($1, $3);
  451. }
  452. method_header : modifiers_opt type method_declarator throws {
  453. $$ = $3;
  454. $<MethodSignatureNode>$.setModifiers($1);
  455. $<MethodSignatureNode>$.setReturnType($2);
  456. $<MethodSignatureNode>$.setThrows($4);
  457. }
  458. | modifiers_opt LT type_parameter_list_1 type method_declarator throws {
  459. $$ = $5;
  460. $<MethodSignatureNode>$.setModifiers($1);
  461. $<MethodSignatureNode>$.setExtraTypeInfo("<" + $3);
  462. $<MethodSignatureNode>$.setReturnType($4);
  463. $<MethodSignatureNode>$.setThrows($6);
  464. }
  465. | modifiers_opt VOID method_declarator throws {
  466. $$ = $3;
  467. $<MethodSignatureNode>$.setModifiers($1);
  468. $<MethodSignatureNode>$.setReturnType(PrimitiveTypeNode.VOID);
  469. $<MethodSignatureNode>$.setThrows($4);
  470. }
  471. | modifiers_opt LT type_parameter_list_1 VOID method_declarator throws {
  472. $$ = $5;
  473. $<MethodSignatureNode>$.setModifiers($1);
  474. $<MethodSignatureNode>$.setExtraTypeInfo("<" + $3);
  475. $<MethodSignatureNode>$.setReturnType(PrimitiveTypeNode.VOID);
  476. $<MethodSignatureNode>$.setThrows($6);
  477. }
  478. // Annotation
  479. annotation : annotation_name {
  480. $$ = new Annotation($1, new ArrayList<AnnotationParameter>());
  481. }
  482. | annotation_name LPAREN annotation_params_opt RPAREN {
  483. $$ = new Annotation($1, $3);
  484. }
  485. // String
  486. annotation_name : AT name { $$ = $1 + $2; }
  487. // AnnotationParam
  488. annotation_param : type_variable EQUAL annotation_value {
  489. $$ = new AnnotationParameter($1, $3);
  490. }
  491. | annotation_value {
  492. $$ = new DefaultAnnotationParameter($1);
  493. }
  494. // List<AnnotationParameter>
  495. annotation_params : annotation_param {
  496. $$ = new ArrayList<AnnotationParameter>();
  497. $<List>$.add($1);
  498. }
  499. | annotation_params COMMA annotation_param {
  500. $1.add($3);
  501. }
  502. // AnnotationExpression
  503. annotation_value : annotation {
  504. $$ = $<AnnotationExpression>1;
  505. }
  506. | type {
  507. $$ = $<AnnotationExpression>1;
  508. }
  509. | literal {
  510. $$ = $<AnnotationExpression>1;
  511. }
  512. | LCURLY annotation_array_values RCURLY {
  513. $$ = new ArrayAnnotationExpression($2);
  514. }
  515. | LCURLY RCURLY {
  516. $$ = new ArrayAnnotationExpression(new ArrayList<AnnotationExpression>());
  517. }
  518. // List<AnnotationExpression>
  519. annotation_array_values : annotation_value {
  520. $$ = new ArrayList<AnnotationExpression>();
  521. $<List>$.add($1);
  522. }
  523. | annotation_array_values COMMA annotation_value {
  524. $1.add($3);
  525. }
  526. // List<AnnotationParameter> -- This is just so we don't deal with null's.
  527. annotation_params_none : { $$ = new ArrayList<AnnotationParameter>(); }
  528. // List<AnnotationParameter>
  529. annotation_params_opt : annotation_params | annotation_params_none
  530. literal : STRING_LITERAL {
  531. $$ = new StringLiteral($1);
  532. }
  533. | CHARACTER_LITERAL {
  534. $$ = new CharacterLiteral($1);
  535. }
  536. %%
  537. }