Skip to content

Commit

Permalink
Implement singleton class MacroProcessor #420
Browse files Browse the repository at this point in the history
  • Loading branch information
walterxie committed Nov 29, 2023
1 parent c8677f1 commit 1e9983e
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions lphy/src/main/java/lphy/core/io/MacroProcessor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package lphy.core.io;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Inspired by templating languages e.g. Mustache.
* Only support {{identifier = default_value}}
*/
public class MacroProcessor {

// {{n = 10}}
public static final String CONST_MACRO = "\\{\\{\\s*(\\w+)\\s*=\\s*(.*?)\\s*\\}\\}";
private final Pattern pattern;

public MacroProcessor(String regex) {
this.pattern = Pattern.compile(regex);
}

public MacroProcessor() {
this(CONST_MACRO);
}

// Singleton
private static MacroProcessor INSTANCE;
public static MacroProcessor getInstance() {
if(INSTANCE == null) {
INSTANCE = new MacroProcessor();
}
return INSTANCE;
}

public static String process(String input) {
Map<String, String> macroMap = getInstance().getMacroMap(input);
// TODO modify the macroMap based on user input?
return getInstance().getResult(input, macroMap);
}

public Map<String, String> getMacroMap(String input) {
Map<String, String> macroMap = new HashMap<>();

// Populate the macroMap based on the initial matches
Matcher matcher = pattern.matcher(input);

while (matcher.find()) {
String identifier = matcher.group(1);
String literal = matcher.group(2);
macroMap.put(identifier, literal);
}
return macroMap;
}

public String getResult(String input, Map<String, String> macroMap) {
Matcher replaceMatcher = pattern.matcher(input);
StringBuilder stringBuffer = new StringBuilder();

while (replaceMatcher.find()) {
String replacement = macroMap.get(replaceMatcher.group(1));
if (replacement != null) {
// The String produced will match the sequence of characters in s treated as a literal sequence.
// Slashes ('\') and dollar signs ('$') will be given no special meaning.
replacement = Matcher.quoteReplacement(replacement);
replaceMatcher.appendReplacement(stringBuffer, replacement);
}
}
replaceMatcher.appendTail(stringBuffer);
// Result contains the modified string
return stringBuffer.toString();
}

public static void main(String[] args) {
String input = "{{identifier1 = value1}} some text {{identifier2 = value2}}";

String result = MacroProcessor.process(input);

System.out.println(result);
}

}

0 comments on commit 1e9983e

Please sign in to comment.