You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

RubyHandlerContext.java 3.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. MIT License
  3. Copyright (c) 2018-2019 Gang ZHANG
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. */
  20. package depends.extractor.ruby;
  21. import depends.entity.Entity;
  22. import depends.entity.PackageEntity;
  23. import depends.entity.repo.EntityRepo;
  24. import depends.extractor.FileParser;
  25. import depends.extractor.HandlerContext;
  26. import depends.extractor.IncludedFileLocator;
  27. import depends.extractor.ParserCreator;
  28. import depends.importtypes.FileImport;
  29. import depends.relations.IBindingResolver;
  30. import multilang.depends.util.file.FileUtil;
  31. import java.util.Collection;
  32. public class RubyHandlerContext extends HandlerContext {
  33. private IncludedFileLocator includedFileLocator;
  34. private ParserCreator parserCreator;
  35. public RubyHandlerContext(EntityRepo entityRepo,
  36. IncludedFileLocator includedFileLocator,
  37. IBindingResolver bindingResolver, ParserCreator parserCreator) {
  38. super(entityRepo, bindingResolver);
  39. this.includedFileLocator = includedFileLocator;
  40. this.parserCreator = parserCreator;
  41. }
  42. public Entity foundNamespace(String nampespaceName, Integer startLine) {
  43. Entity parentEntity = currentFile();
  44. if (latestValidContainer()!=null)
  45. parentEntity = latestValidContainer();
  46. PackageEntity pkgEntity = new PackageEntity(nampespaceName, parentEntity,idGenerator.generateId());
  47. entityRepo.add(pkgEntity);
  48. entityStack.push(pkgEntity);
  49. return pkgEntity;
  50. }
  51. public void processSpecialFuncCall(String methodName, Collection<String> params, Integer startLine) {
  52. // Handle Import relation
  53. if(methodName.equals("require") || methodName.equals("require_relative")) {
  54. for (String importedFilename:params) {
  55. if (!importedFilename.endsWith(".rb")) importedFilename = importedFilename + ".rb";
  56. String dir = FileUtil.getLocatedDir(currentFile().getRawName().uniqName());
  57. String inclFileName = includedFileLocator.uniqFileName(dir,importedFilename);
  58. if (inclFileName==null) {
  59. System.err.println("Warning: cannot found included file " + importedFilename );
  60. continue;
  61. }
  62. FileParser importedParser = parserCreator.createFileParser();
  63. try {
  64. importedParser.parse(inclFileName);
  65. } catch (Exception e) {
  66. System.err.println("parsing error in "+inclFileName);
  67. }
  68. foundNewImport(new FileImport(inclFileName));
  69. }
  70. }
  71. // Handle Extend relation
  72. else if (methodName.equals("extend")) {
  73. for (String moduleName:params) {
  74. foundExtends(moduleName);
  75. }
  76. }
  77. // Handle mixin relation
  78. else if (methodName.equals("include")) {
  79. for (String moduleName:params) {
  80. foundMixin(moduleName);
  81. }
  82. }
  83. // attribute methods
  84. else if (methodName.equals("attr_accessor")||methodName.equals("attr_writer")||methodName.equals("attr_reader")) {
  85. for (String name:params) {
  86. name = name.replace(":", ""); //remove symbol
  87. foundMethodDeclarator(name,startLine);
  88. }
  89. }
  90. }
  91. }

人工智能研发终端

Contributors (2)