|
8 | 8 | import com.github.javaparser.ast.expr.FieldAccessExpr; |
9 | 9 | import com.github.javaparser.ast.expr.NameExpr; |
10 | 10 | import com.github.javaparser.ast.type.PrimitiveType; |
| 11 | + |
11 | 12 | import edu.wpi.gripgenerator.defaults.DefaultValueCollector; |
12 | 13 | import edu.wpi.gripgenerator.defaults.EnumDefaultValue; |
13 | 14 | import edu.wpi.gripgenerator.defaults.PrimitiveDefaultValue; |
|
24 | 25 | import java.util.Set; |
25 | 26 |
|
26 | 27 | public class FileParser { |
27 | | - /** |
28 | | - * Regex splits the parameter into three distinct capture groups. |
29 | | - * <ol> |
30 | | - * <li>The type and the param with optional varargs.</li> |
31 | | - * <li>The comment that is after the parameter.</li> |
32 | | - * <li>The various ways that the parameter can end.</li> |
33 | | - * </ol> |
34 | | - */ |
35 | | - protected static final String methodReorderPattern = "([A-Za-z1-9]+ (?:\\.\\.\\.)?[a-z][A-Za-z0-9_]*)(/\\*=[^ ]*\\*/)((?:,)|(?:\\s*\\)))"; |
36 | | - |
37 | | - /** |
38 | | - * Reorders the {@link FileParser#methodReorderPattern} capture groups so the JavaParser can correctly |
39 | | - * associate the params with their respective comments. |
40 | | - */ |
41 | | - protected static final String methodNewOrder = "$2$1$3"; |
42 | | - |
43 | | - /** |
44 | | - * There is a bug in the JavaParser that will incorrectly associate comments after a parameter but |
45 | | - * before a comma will incorrectly associate that comment with the next param in the method's params. |
46 | | - * |
47 | | - * @param stream The original input file. |
48 | | - * @return The processed output stream. |
49 | | - * @see <a href="https://github.com/javaparser/javaparser/issues/199">Javaparser Issue:199</a> |
50 | | - */ |
51 | | - private static InputStream preProcessStream(InputStream stream) { |
52 | | - //FIXME: This is a hack around. This should be removed once the above noted issue is resolved. |
53 | | - java.util.Scanner s = new java.util.Scanner(stream, StandardCharsets.UTF_8.name()).useDelimiter("\\A"); |
54 | | - String input = s.hasNext() ? s.next() : ""; |
55 | | - input = input.replaceAll(methodReorderPattern, methodNewOrder); |
56 | | - return new ByteArrayInputStream(input.getBytes(StandardCharsets.UTF_8)); |
57 | | - } |
58 | | - |
59 | | - private static CompilationUnit readFile(URL url) { |
60 | | - try { |
61 | | - return JavaParser.parse(preProcessStream(url.openStream())); |
62 | | - } catch (IOException e) { |
63 | | - throw new IllegalStateException(e); |
64 | | - } catch (ParseException e) { |
65 | | - throw new IllegalStateException(e); |
66 | | - } |
| 28 | + /** |
| 29 | + * Regex splits the parameter into three distinct capture groups. <ol> <li>The type and the param |
| 30 | + * with optional varargs.</li> <li>The comment that is after the parameter.</li> <li>The various |
| 31 | + * ways that the parameter can end.</li> </ol> |
| 32 | + */ |
| 33 | + protected static final String methodReorderPattern = "([A-Za-z1-9]+ (?:\\.\\.\\.)" + |
| 34 | + "?[a-z][A-Za-z0-9_]*)(/\\*=[^ ]*\\*/)((?:,)|(?:\\s*\\)))"; |
| 35 | + |
| 36 | + /** |
| 37 | + * Reorders the {@link FileParser#methodReorderPattern} capture groups so the JavaParser can |
| 38 | + * correctly associate the params with their respective comments. |
| 39 | + */ |
| 40 | + protected static final String methodNewOrder = "$2$1$3"; |
| 41 | + |
| 42 | + /** |
| 43 | + * There is a bug in the JavaParser that will incorrectly associate comments after a parameter but |
| 44 | + * before a comma will incorrectly associate that comment with the next param in the method's |
| 45 | + * params. |
| 46 | + * |
| 47 | + * @param stream The original input file. |
| 48 | + * @return The processed output stream. |
| 49 | + * @see <a href="https://github.com/javaparser/javaparser/issues/199">Javaparser Issue:199</a> |
| 50 | + */ |
| 51 | + private static InputStream preProcessStream(InputStream stream) { |
| 52 | + //FIXME: This is a hack around. This should be removed once the above noted issue is resolved. |
| 53 | + java.util.Scanner s = new java.util.Scanner(stream, StandardCharsets.UTF_8.name()) |
| 54 | + .useDelimiter("\\A"); |
| 55 | + String input = s.hasNext() ? s.next() : ""; |
| 56 | + input = input.replaceAll(methodReorderPattern, methodNewOrder); |
| 57 | + return new ByteArrayInputStream(input.getBytes(StandardCharsets.UTF_8)); |
| 58 | + } |
| 59 | + |
| 60 | + private static CompilationUnit readFile(URL url) { |
| 61 | + try { |
| 62 | + return JavaParser.parse(preProcessStream(url.openStream())); |
| 63 | + } catch (IOException e) { |
| 64 | + throw new IllegalStateException(e); |
| 65 | + } catch (ParseException e) { |
| 66 | + throw new IllegalStateException(e); |
67 | 67 | } |
68 | | - |
69 | | - /** |
70 | | - * Generates all of the source code from the opencv bindings |
71 | | - * |
72 | | - * @return A map of the filename with the compilation units |
73 | | - */ |
74 | | - public static Map<String, CompilationUnit> generateAllSourceCode() { |
75 | | - URL INPUT_URL = FileParser.class.getResource("/org/bytedeco/javacpp/opencv_core.txt"); |
76 | | - CompilationUnit compilationUnit = readFile(INPUT_URL); |
77 | | - Map<String, CompilationUnit> returnMap = new HashMap<>(); |
78 | | - DefaultValueCollector collector = new DefaultValueCollector(); |
79 | | - collector.add(new PrimitiveDefaultValue(new PrimitiveType(PrimitiveType.Primitive.Double)) { |
80 | | - @Override |
81 | | - protected Set<String> getDefaultValues() { |
82 | | - return Collections.singleton("CV_PI"); |
83 | | - } |
84 | | - |
85 | | - @Override |
86 | | - public Expression getDefaultValue(String defaultValue) { |
87 | | - return new FieldAccessExpr( |
88 | | - new NameExpr("Math"), |
89 | | - "PI" |
90 | | - ); |
91 | | - } |
92 | | - }); |
93 | | - |
94 | | - collector.add(new EnumDefaultValue("edu.wpi.grip.core.operations.opencv.enumeration", "FlipCode", "X_AXIS", "Y_AXIS", "BOTH_AXES")); |
95 | | - |
96 | | - OperationList operationList = new OperationList( |
97 | | - new ImportDeclaration(new NameExpr("edu.wpi.grip.generated.opencv_core"), false, true), |
98 | | - new ImportDeclaration(new NameExpr("edu.wpi.grip.generated.opencv_imgproc"), false, true) |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Generates all of the source code from the opencv bindings |
| 72 | + * |
| 73 | + * @return A map of the filename with the compilation units |
| 74 | + */ |
| 75 | + public static Map<String, CompilationUnit> generateAllSourceCode() { |
| 76 | + URL openCvCoreUrl = FileParser.class.getResource("/org/bytedeco/javacpp/opencv_core.txt"); |
| 77 | + CompilationUnit compilationUnit = readFile(openCvCoreUrl); |
| 78 | + Map<String, CompilationUnit> returnMap = new HashMap<>(); |
| 79 | + DefaultValueCollector collector = new DefaultValueCollector(); |
| 80 | + collector.add(new PrimitiveDefaultValue(new PrimitiveType(PrimitiveType.Primitive.Double)) { |
| 81 | + @Override |
| 82 | + protected Set<String> getDefaultValues() { |
| 83 | + return Collections.singleton("CV_PI"); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public Expression getDefaultValue(String defaultValue) { |
| 88 | + return new FieldAccessExpr( |
| 89 | + new NameExpr("Math"), |
| 90 | + "PI" |
99 | 91 | ); |
| 92 | + } |
| 93 | + }); |
100 | 94 |
|
101 | | - if (compilationUnit != null) { |
102 | | - returnMap.putAll(parseOpenCVCore(compilationUnit, collector, operationList)); |
103 | | - } else { |
104 | | - System.err.print("Invalid File input"); |
105 | | - } |
106 | | - URL INPUT_URL2 = FileParser.class.getResource("/org/bytedeco/javacpp/opencv_imgproc.txt"); |
107 | | - compilationUnit = readFile(INPUT_URL2); |
108 | | - if (compilationUnit != null) { |
109 | | - returnMap.putAll(parseOpenImgprc(compilationUnit, collector, operationList)); |
110 | | - |
111 | | - } |
112 | | - return returnMap; |
113 | | - } |
114 | | - |
115 | | - public static Map<String, CompilationUnit> parseOpenImgprc(CompilationUnit imgprocDeclaration, DefaultValueCollector collector, OperationList operations) { |
116 | | - Map<String, CompilationUnit> compilationUnits = new HashMap<>(); |
117 | | - final String baseClassName = "opencv_imgproc"; |
118 | | - |
119 | | - OpenCVEnumVisitor enumVisitor = new OpenCVEnumVisitor(baseClassName, collector); |
120 | | - enumVisitor.visit(imgprocDeclaration, compilationUnits); |
121 | | - compilationUnits.putAll(enumVisitor.generateCompilationUnits()); |
122 | | - return compilationUnits; |
123 | | - } |
| 95 | + collector.add(new EnumDefaultValue("edu.wpi.grip.core.operations.opencv.enumeration", |
| 96 | + "FlipCode", "X_AXIS", "Y_AXIS", "BOTH_AXES")); |
124 | 97 |
|
125 | | - public static Map<String, CompilationUnit> parseOpenCVCore(CompilationUnit coreDeclaration, DefaultValueCollector collector, OperationList operations) { |
126 | | - Map<String, CompilationUnit> compilationUnits = new HashMap<>(); |
127 | | - final String baseClassName = "opencv_core"; |
| 98 | + OperationList operationList = new OperationList( |
| 99 | + new ImportDeclaration(new NameExpr("edu.wpi.grip.generated.opencv_core"), false, true), |
| 100 | + new ImportDeclaration(new NameExpr("edu.wpi.grip.generated.opencv_imgproc"), false, true) |
| 101 | + ); |
128 | 102 |
|
129 | | - OpenCVEnumVisitor enumVisitor = new OpenCVEnumVisitor(baseClassName, collector); |
130 | | - enumVisitor.visit(coreDeclaration, compilationUnits); |
131 | | - compilationUnits.putAll(enumVisitor.generateCompilationUnits()); |
132 | | - |
133 | | - return compilationUnits; |
| 103 | + if (compilationUnit != null) { |
| 104 | + returnMap.putAll(parseOpenCVCore(compilationUnit, collector, operationList)); |
| 105 | + } else { |
| 106 | + System.err.print("Invalid File input"); |
134 | 107 | } |
135 | | - |
136 | | - public static void main(String... args) { |
137 | | - FileParser.generateAllSourceCode(); |
| 108 | + URL openCvImgprocUrl = FileParser.class.getResource("/org/bytedeco/javacpp/opencv_imgproc.txt"); |
| 109 | + compilationUnit = readFile(openCvImgprocUrl); |
| 110 | + if (compilationUnit != null) { |
| 111 | + returnMap.putAll(parseOpenImgprc(compilationUnit, collector, operationList)); |
138 | 112 | } |
| 113 | + return returnMap; |
| 114 | + } |
| 115 | + |
| 116 | + public static Map<String, CompilationUnit> parseOpenImgprc(CompilationUnit imgprocDeclaration, |
| 117 | + DefaultValueCollector collector, |
| 118 | + OperationList operations) { |
| 119 | + Map<String, CompilationUnit> compilationUnits = new HashMap<>(); |
| 120 | + final String baseClassName = "opencv_imgproc"; |
| 121 | + |
| 122 | + OpenCVEnumVisitor enumVisitor = new OpenCVEnumVisitor(baseClassName, collector); |
| 123 | + enumVisitor.visit(imgprocDeclaration, compilationUnits); |
| 124 | + compilationUnits.putAll(enumVisitor.generateCompilationUnits()); |
| 125 | + return compilationUnits; |
| 126 | + } |
| 127 | + |
| 128 | + public static Map<String, CompilationUnit> parseOpenCVCore(CompilationUnit coreDeclaration, |
| 129 | + DefaultValueCollector collector, |
| 130 | + OperationList operations) { |
| 131 | + Map<String, CompilationUnit> compilationUnits = new HashMap<>(); |
| 132 | + final String baseClassName = "opencv_core"; |
| 133 | + |
| 134 | + OpenCVEnumVisitor enumVisitor = new OpenCVEnumVisitor(baseClassName, collector); |
| 135 | + enumVisitor.visit(coreDeclaration, compilationUnits); |
| 136 | + compilationUnits.putAll(enumVisitor.generateCompilationUnits()); |
| 137 | + |
| 138 | + return compilationUnits; |
| 139 | + } |
| 140 | + |
| 141 | + public static void main(String... args) { |
| 142 | + FileParser.generateAllSourceCode(); |
| 143 | + } |
139 | 144 | } |
0 commit comments