Correction de bug #1

This commit is contained in:
2025-03-15 17:36:13 +01:00
parent bf2ef906c8
commit 635ff0a728

View File

@@ -48,12 +48,6 @@ public class BakefileParser {
*/ */
private static final Pattern PHONY_PATTERN = Pattern.compile("^\\.PHONY:\\s*([^#]*?)\\s*(?:#.*)?$"); private static final Pattern PHONY_PATTERN = Pattern.compile("^\\.PHONY:\\s*([^#]*?)\\s*(?:#.*)?$");
/**
* Regex pour détecter les lignes de continuation.
* Format : " gcc -o program program.c \"
*/
private static final Pattern CONTINUATION_PATTERN = Pattern.compile("^(.*)\\\\\\s*$");
/** /**
* Regex pour détecter les références de variables. * Regex pour détecter les références de variables.
* Format : "${VAR}" ou "$(VAR)" * Format : "${VAR}" ou "$(VAR)"
@@ -79,34 +73,6 @@ public class BakefileParser {
firstTarget = null; firstTarget = null;
} }
/**
* Gérer les lignes de continuation.
* @param lines Liste des lignes du fichier Bakefile
* @param startIndex Index de la première ligne de continuation
* @return La ligne combinée
*/
private String handleContinuationLines(List<String> lines, int startIndex) {
StringBuilder combinedLine = new StringBuilder();
int i = startIndex;
while (i < lines.size()) {
String line = lines.get(i);
Matcher contMatcher = CONTINUATION_PATTERN.matcher(line);
if (contMatcher.matches()) {
// Ajouter la ligne sans le backslash
combinedLine.append(contMatcher.group(1).trim()).append(" ");
i++;
} else {
// Ajouter la dernière ligne et sortir
combinedLine.append(line.trim());
break;
}
}
return combinedLine.toString();
}
/** /**
* Remplacer les variables dans une chaîne. * Remplacer les variables dans une chaîne.
* @param input Chaîne à traiter * @param input Chaîne à traiter
@@ -199,112 +165,150 @@ public class BakefileParser {
* @return Liste des règles extraites * @return Liste des règles extraites
*/ */
public List<Rule> parse() { public List<Rule> parse() {
List<Rule> rules = new ArrayList<>(); List<Rule> rules = new ArrayList<>();
Set<String> phonyTargets = new HashSet<>(); Set<String> phonyTargets = new HashSet<>();
if (!Files.exists(Paths.get(filename))) { if (!Files.exists(Paths.get(filename))) {
System.out.println("*** No targets specified and no makefile found. Stop."); System.out.println("*** No targets specified and no makefile found. Stop.");
System.exit(2); System.exit(2);
} }
try { try {
List<String> lines = Files.readAllLines(Paths.get(filename)); List<String> lines = Files.readAllLines(Paths.get(filename));
List<String> currentTargets = null; List<String> currentTargets = null;
List<String> dependencies = new ArrayList<>(); List<String> dependencies = new ArrayList<>();
List<String> commands = new ArrayList<>(); List<String> commands = new ArrayList<>();
boolean inContinuedCommand = false;
StringBuilder continuedCommand = new StringBuilder();
for (int i = 0; i < lines.size(); i++) { for (int i = 0; i < lines.size(); i++) {
String line = lines.get(i); String line = lines.get(i).replace("\r", "");
// Vérifier si la ligne a un caractère de continuation // Ignorer les lignes vides
Matcher contMatcher = CONTINUATION_PATTERN.matcher(line); if (line.trim().isEmpty()) {
if (contMatcher.matches()) { continue;
// Récupérer toute la définition multi-ligne }
line = handleContinuationLines(lines, i);
// Ajuster i pour sauter les lignes traitées
while (i + 1 < lines.size() &&
CONTINUATION_PATTERN.matcher(lines.get(i)).matches()) {
i++;
}
}
if (line.trim().isEmpty()) { // Gérer les erreurs de format (espaces au lieu de tabulations)
continue; if (line.matches("^ +.*$") && !inContinuedCommand) {
} System.err.println(filename + ":" + (i+1) + ": *** missing separator. Stop.");
System.exit(2);
}
if (line.matches("^ +.*$")) { // Si nous sommes en train de traiter une ligne continuée
System.err.println(filename + ":" + (i+1) + ": *** missing separator. Stop."); if (inContinuedCommand) {
System.exit(2); if (line.endsWith("\\")) {
} // Encore une continuation
continuedCommand.append(" ").append(line.substring(0, line.length() - 1).trim());
} else {
// Fin de la continuation
continuedCommand.append(" ").append(line.trim());
commands.add(continuedCommand.toString().trim());
inContinuedCommand = false;
continuedCommand = new StringBuilder();
}
continue;
}
Matcher varMatcher = VARIABLE_PATTERN.matcher(line); // Matcher pour les déclarations .PHONY
Matcher targetMatcher = TARGET_PATTERN.matcher(line); Matcher phonyMatcher = PHONY_PATTERN.matcher(line);
Matcher commandMatcher = COMMAND_PATTERN.matcher(line); if (phonyMatcher.matches()) {
Matcher phonyMatcher = PHONY_PATTERN.matcher(line); String[] phonies = phonyMatcher.group(1).trim().split("\\s+");
Collections.addAll(phonyTargets, phonies);
continue;
}
if (phonyMatcher.matches()) { // Matcher pour les déclarations de variables
String[] phonies = phonyMatcher.group(1).trim().split("\\s+"); Matcher varMatcher = VARIABLE_PATTERN.matcher(line);
Collections.addAll(phonyTargets, phonies); if (varMatcher.matches()) {
continue; String varName = varMatcher.group(1);
} String varValue = varMatcher.group(2).trim();
// Évaluer les variables référencées dans la valeur
varValue = replaceVariables(varValue);
variables.put(varName, varValue);
continue;
}
if (varMatcher.matches()) { // Matcher pour les cibles et dépendances
String varName = varMatcher.group(1); Matcher targetMatcher = TARGET_PATTERN.matcher(line);
String varValue = varMatcher.group(2).trim(); if (targetMatcher.matches()) {
// Évaluer les variables référencées dans la valeur // Si nous avions des cibles pcédentes, créons les règles correspondantes
varValue = replaceVariables(varValue); if (currentTargets != null) {
variables.put(varName, varValue); // Créer une règle pour chaque cible avec les mêmes dépendances et commandes
} else if (targetMatcher.matches()) { for (String target : currentTargets) {
if (currentTargets != null) { String resolvedTarget = replaceVariables(target.trim());
// Créer une règle pour chaque cible avec les mêmes dépendances et commandes rules.add(new Rule(
for (String target : currentTargets) { resolvedTarget,
String resolvedTarget = replaceVariables(target.trim()); replaceVariablesInList(dependencies),
rules.add(new Rule( replaceVariablesInList(commands),
resolvedTarget, phonyTargets.contains(resolvedTarget)
splitDependencies(dependencies.stream() ));
.collect(Collectors.joining(" "))),
replaceVariablesInList(commands),
phonyTargets.contains(resolvedTarget)
));
if (firstTarget == null) { if (firstTarget == null) {
firstTarget = resolvedTarget; firstTarget = resolvedTarget;
} }
} }
} }
String targetStr = targetMatcher.group(1); // Configuration pour les nouvelles cibles
currentTargets = splitTargets(targetStr); String targetStr = targetMatcher.group(1);
String depStr = targetMatcher.group(2); currentTargets = splitTargets(targetStr);
dependencies = splitDependencies(depStr);
commands = new ArrayList<>();
} else if (commandMatcher.matches()) {
commands.add(commandMatcher.group(1));
}
}
if (currentTargets != null) { String depStr = targetMatcher.group(2);
// Créer une règle pour chaque cible avec les mêmes dépendances et commandes dependencies = splitDependencies(depStr);
for (String target : currentTargets) { commands = new ArrayList<>();
String resolvedTarget = replaceVariables(target.trim()); continue;
rules.add(new Rule( }
resolvedTarget,
replaceVariablesInList(dependencies),
replaceVariablesInList(commands),
phonyTargets.contains(resolvedTarget)
));
if (firstTarget == null) { // Matcher pour les lignes de commande
firstTarget = resolvedTarget; Matcher commandMatcher = COMMAND_PATTERN.matcher(line);
} if (commandMatcher.matches()) {
} String command = commandMatcher.group(1);
}
} catch (IOException e) { // Gérer la continuation de ligne
e.printStackTrace(); if (command.endsWith("\\")) {
} inContinuedCommand = true;
return rules; continuedCommand = new StringBuilder(command.substring(0, command.length() - 1).trim());
} } else {
commands.add(command);
}
}
}
// Traiter les dernières cibles
if (currentTargets != null) {
// Créer une règle pour chaque cible avec les mêmes dépendances et commandes
for (String target : currentTargets) {
String resolvedTarget = replaceVariables(target.trim());
rules.add(new Rule(
resolvedTarget,
replaceVariablesInList(dependencies),
replaceVariablesInList(commands),
phonyTargets.contains(resolvedTarget)
));
if (firstTarget == null) {
firstTarget = resolvedTarget;
}
}
}
if (BakeCLI.isDebug()) {
System.out.println("Debug: Parsed " + rules.size() + " rules.");
for (Rule rule : rules) {
System.out.println("Debug: Rule: " + rule.getName());
System.out.println("Debug: Commands: " + rule.getCommands().size());
for (String cmd : rule.getCommands()) {
System.out.println("Debug: [" + cmd + "]");
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
return rules;
}
/** /**
* Récupérer la première cible * Récupérer la première cible