| 23 | } |
| 24 | |
| 25 | private static boolean testFile(String filePath) throws Exception { |
| 26 | // Detect mode based on file extension |
| 27 | String mode = filePath.endsWith(".yul") ? "yul" : "sol"; |
| 28 | String content = new String(Files.readAllBytes(Paths.get(filePath))); |
| 29 | |
| 30 | // Check if file expects parser error |
| 31 | boolean expectsError = content.contains("// ParserError"); |
| 32 | |
| 33 | CharStream input; |
| 34 | if (mode.equals("sol")) { |
| 35 | // Remove ExternalSource lines for Solidity files |
| 36 | content = content.replaceAll("(?m)^==== ExternalSource:.*$", ""); |
| 37 | input = CharStreams.fromString(content); |
| 38 | } else { |
| 39 | // Wrap Yul in assembly statement |
| 40 | input = CharStreams.fromString("assembly " + content); |
| 41 | } |
| 42 | |
| 43 | SolidityLexer lexer = new SolidityLexer(input); |
| 44 | CommonTokenStream tokens = new CommonTokenStream(lexer); |
| 45 | SolidityParser parser = new SolidityParser(tokens); |
| 46 | |
| 47 | // Remove default error listeners and add custom one |
| 48 | parser.removeErrorListeners(); |
| 49 | lexer.removeErrorListeners(); |
| 50 | |
| 51 | ErrorCollector errorCollector = new ErrorCollector(); |
| 52 | parser.addErrorListener(errorCollector); |
| 53 | lexer.addErrorListener(errorCollector); |
| 54 | |
| 55 | // Parse based on mode |
| 56 | if (mode.equals("sol")) { |
| 57 | parser.sourceUnit(); |
| 58 | } else { |
| 59 | parser.assemblyStatement(); |
| 60 | } |
| 61 | |
| 62 | boolean hasErrors = errorCollector.hasErrors(); |
| 63 | |
| 64 | // Output result |
| 65 | if (expectsError) { |
| 66 | if (hasErrors) { |
| 67 | System.out.println("PASS:" + filePath + ":FAILED_AS_EXPECTED"); |
| 68 | } else { |
| 69 | System.out.println("FAIL:" + filePath + ":SUCCEEDED_DESPITE_PARSER_ERROR"); |
| 70 | return false; |
| 71 | } |
| 72 | } else { |
| 73 | if (!hasErrors) { |
| 74 | System.out.println("PASS:" + filePath + ":OK"); |
| 75 | } else { |
| 76 | System.out.println("FAIL:" + filePath + ":" + errorCollector.getErrors()); |
| 77 | return false; |
| 78 | } |
| 79 | } |
| 80 | return true; |
| 81 | } |
| 82 | |