Created
March 17, 2023 16:35
-
-
Save faermanj/5593c869cf06dee52464e95f727eaad5 to your computer and use it in GitHub Desktop.
ChatGPT <3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.File; | |
import java.io.FileReader; | |
import java.io.IOException; | |
import java.util.Arrays; | |
import java.util.List; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
/** | |
* write me a java class that, given a File instance pointing to a text file, read all comments in that file. | |
* Consider comments both like in shell, java or XML syntax. | |
* For each comment, if the comment starts with a keyword (declared in a list), call a method corresponding to that keyword. | |
* ... | |
* nice. now extract each comment style to its own method. also use var for declarations when posible. | |
* .... | |
* done :) | |
* | |
*/ | |
public class CommentProcessor { | |
private static final List<String> KEYWORDS = Arrays.asList("TODO", "FIXME"); | |
public static void processComments(File file) throws IOException { | |
try (var reader = new BufferedReader(new FileReader(file))) { | |
String line; | |
while ((line = reader.readLine()) != null) { | |
processLine(line); | |
} | |
} | |
} | |
private static void processLine(String line) { | |
if (processShellComment(line)) { | |
return; | |
} | |
if (processJavaComment(line)) { | |
return; | |
} | |
if (processXmlComment(line)) { | |
return; | |
} | |
} | |
private static boolean processShellComment(String line) { | |
var pattern = Pattern.compile("^\\s*#\\s*(\\w+)\\b(.*)$"); | |
var matcher = pattern.matcher(line); | |
if (matcher.matches()) { | |
var keyword = matcher.group(1); | |
if (KEYWORDS.contains(keyword)) { | |
var comment = matcher.group(2); | |
handleComment(keyword, comment); | |
return true; | |
} | |
} | |
return false; | |
} | |
private static boolean processJavaComment(String line) { | |
var pattern = Pattern.compile("^\\s*//\\s*(\\w+)\\b(.*)$"); | |
var matcher = pattern.matcher(line); | |
if (matcher.matches()) { | |
var keyword = matcher.group(1); | |
if (KEYWORDS.contains(keyword)) { | |
var comment = matcher.group(2); | |
handleComment(keyword, comment); | |
return true; | |
} | |
} | |
return false; | |
} | |
private static boolean processXmlComment(String line) { | |
var pattern = Pattern.compile("^\\s*<!--\\s*(\\w+)\\b(.*)\\s*-->$"); | |
var matcher = pattern.matcher(line); | |
if (matcher.matches()) { | |
var keyword = matcher.group(1); | |
if (KEYWORDS.contains(keyword)) { | |
var comment = matcher.group(2); | |
handleComment(keyword, comment); | |
return true; | |
} | |
} | |
return false; | |
} | |
private static void handleComment(String keyword, String comment) { | |
switch (keyword) { | |
case "TODO": | |
handleTodoComment(comment); | |
break; | |
case "FIXME": | |
handleFixmeComment(comment); | |
break; | |
// Add more cases for additional keywords | |
} | |
} | |
private static void handleTodoComment(String comment) { | |
System.out.println("Found TODO comment: " + comment); | |
// Call the corresponding method to handle the TODO | |
} | |
private static void handleFixmeComment(String comment) { | |
System.out.println("Found FIXME comment: " + comment); | |
// Call the corresponding method to handle the FIXME | |
} | |
// Add more methods for additional keywords | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment