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.

AbstractLangProcessor.java 7.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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;
  21. import depends.entity.Entity;
  22. import depends.entity.FileEntity;
  23. import depends.entity.repo.BuiltInType;
  24. import depends.entity.repo.EntityRepo;
  25. import depends.entity.repo.InMemoryEntityRepo;
  26. import depends.relations.ImportLookupStrategy;
  27. import depends.relations.IBindingResolver;
  28. import multilang.depends.util.file.FileTraversal;
  29. import multilang.depends.util.file.FileUtil;
  30. import org.codehaus.plexus.util.FileUtils;
  31. import org.slf4j.Logger;
  32. import org.slf4j.LoggerFactory;
  33. import java.io.File;
  34. import java.io.IOException;
  35. import java.lang.management.ManagementFactory;
  36. import java.util.ArrayList;
  37. import java.util.HashSet;
  38. import java.util.List;
  39. import java.util.Set;
  40. abstract public class AbstractLangProcessor {
  41. /**
  42. * The name of the lang
  43. *
  44. * @return
  45. */
  46. public abstract String supportedLanguage();
  47. /**
  48. * The file suffixes in the lang
  49. *
  50. * @return
  51. */
  52. public abstract String[] fileSuffixes();
  53. /**
  54. * Strategy of how to lookup types and entities in the lang.
  55. *
  56. * @return
  57. */
  58. public abstract ImportLookupStrategy getImportLookupStrategy();
  59. /**
  60. * The builtInType of the lang.
  61. *
  62. * @return
  63. */
  64. public abstract BuiltInType getBuiltInType();
  65. /**
  66. * The language specific file parser
  67. *
  68. * @param fileFullPath
  69. * @return
  70. */
  71. public abstract FileParser createFileParser();
  72. public IBindingResolver bindingResolver;
  73. protected EntityRepo entityRepo;
  74. protected String inputSrcPath;
  75. public String[] includeDirs;
  76. private Set<UnsolvedBindings> potentialExternalDependencies;
  77. private List<String> includePaths;
  78. private static Logger logger = LoggerFactory.getLogger(AbstractLangProcessor.class);
  79. public AbstractLangProcessor() {
  80. entityRepo = new InMemoryEntityRepo();
  81. }
  82. /**
  83. * The process steps of build dependencies. Step 1: parse all files, add
  84. * entities and expression into repositories Step 2: resolve bindings of files
  85. * (if not resolved yet) Step 3: identify dependencies
  86. *
  87. * @param inputDir
  88. * @param includeDir
  89. * @param bindingResolver
  90. * @return
  91. */
  92. public EntityRepo buildDependencies(String inputDir, String[] includeDir, IBindingResolver bindingResolver) {
  93. this.inputSrcPath = inputDir;
  94. this.includeDirs = includeDir;
  95. this.bindingResolver = bindingResolver;
  96. logger.info("Start parsing files...");
  97. parseAllFiles();
  98. markAllEntitiesScope();
  99. if (logger.isInfoEnabled()) {
  100. logger.info("Resolve types and bindings of variables, methods and expressions.... " + this.inputSrcPath);
  101. logger.info("Heap Information: " + ManagementFactory.getMemoryMXBean().getHeapMemoryUsage());
  102. }
  103. resolveBindings();
  104. if (logger.isInfoEnabled()) {
  105. System.gc();
  106. logger.info("Heap Information: " + ManagementFactory.getMemoryMXBean().getHeapMemoryUsage());
  107. }
  108. return entityRepo;
  109. }
  110. private void markAllEntitiesScope() {
  111. entityRepo.getFileEntities().stream().forEach(entity -> {
  112. Entity file = entity.getAncestorOfType(FileEntity.class);
  113. try {
  114. if (!file.getQualifiedName().startsWith(this.inputSrcPath)) {
  115. entity.setInScope(false);
  116. }
  117. } catch (Exception e) {
  118. }
  119. });
  120. }
  121. /**
  122. * @return unsolved bindings
  123. */
  124. public void resolveBindings() {
  125. System.out.println("Resolve types and bindings of variables, methods and expressions....");
  126. this.potentialExternalDependencies = bindingResolver.resolveAllBindings(this.isEagerExpressionResolve());
  127. if (getExternalDependencies().size() > 0) {
  128. System.out.println("There are " + getExternalDependencies().size() + " items are potential external dependencies.");
  129. }
  130. System.out.println("types and bindings resolved successfully...");
  131. }
  132. private final void parseAllFiles() {
  133. System.out.println("Start parsing files...");
  134. Set<String> phase2Files = new HashSet<>();
  135. FileTraversal fileTransversal = new FileTraversal(new FileTraversal.IFileVisitor() {
  136. @Override
  137. public void visit(File file) {
  138. String fileFullPath = file.getAbsolutePath();
  139. if (!fileFullPath.startsWith(inputSrcPath)) {
  140. return;
  141. }
  142. parseFile(fileFullPath, phase2Files);
  143. }
  144. });
  145. fileTransversal.extensionFilter(this.fileSuffixes());
  146. fileTransversal.travers(this.inputSrcPath);
  147. for (String f : phase2Files) {
  148. parseFile(f, phase2Files);
  149. }
  150. System.out.println("all files procceed successfully...");
  151. }
  152. protected void parseFile(String fileFullPath, Set<String> phase2Files) {
  153. FileParser fileParser = createFileParser();
  154. try {
  155. if (fileParser.isPhase2Files(fileFullPath)){
  156. phase2Files.add(fileFullPath);
  157. }else {
  158. fileParser.parse(fileFullPath);
  159. }
  160. } catch (IOException e) {
  161. e.printStackTrace();
  162. } catch (Exception e) {
  163. System.err.println("error occoured during parse file " + fileFullPath);
  164. e.printStackTrace();
  165. }
  166. }
  167. public List<String> includePaths() {
  168. if (this.includePaths ==null) {
  169. this.includePaths = buildIncludePath();
  170. }
  171. return includePaths;
  172. }
  173. private List<String> buildIncludePath() {
  174. includePaths = new ArrayList<String>();
  175. for (String path : includeDirs) {
  176. if (FileUtils.fileExists(path)) {
  177. path = FileUtil.uniqFilePath(path);
  178. if (!includePaths.contains(path))
  179. includePaths.add(path);
  180. }
  181. path = this.inputSrcPath + File.separator + path;
  182. if (FileUtils.fileExists(path)) {
  183. path = FileUtil.uniqFilePath(path);
  184. if (!includePaths.contains(path))
  185. includePaths.add(path);
  186. }
  187. }
  188. return includePaths;
  189. }
  190. public EntityRepo getEntityRepo() {
  191. return this.entityRepo;
  192. }
  193. public abstract List<String> supportedRelations();
  194. public Set<UnsolvedBindings> getExternalDependencies() {
  195. return potentialExternalDependencies;
  196. }
  197. public String getRelationMapping(String relation) {
  198. return relation;
  199. }
  200. /**
  201. * Whether to resolve expression immediately during parse
  202. * @return
  203. */
  204. public boolean isEagerExpressionResolve(){
  205. return false;
  206. }
  207. /**
  208. * Call as Impl:
  209. * implicit call (for example polymorphic in cpp)
  210. * @return
  211. */
  212. public boolean supportCallAsImpl(){return false;};
  213. }

人工智能研发终端

Contributors (2)