package br.com.caelum.vraptor.panettone.parser.rule;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import br.com.caelum.vraptor.panettone.parser.SourceCode;
import br.com.caelum.vraptor.panettone.parser.TextChunk;
import br.com.caelum.vraptor.panettone.parser.ast.Node;
import br.com.caelum.vraptor.panettone.parser.ast.ScriptletPrintNode;
public class ScriptletPrintRule implements Rule {
@Override
public List<TextChunk> getChunks(SourceCode sc) {
List<TextChunk> chunks = new ArrayList<TextChunk>();
String parameters = "(\\([\\w'\",\\.\\s]*\\))?";
String variableName = "[\\w\\[\\]\"']+";
String dot = "(\\.)?";
Pattern p = Pattern.compile(
"(<%=)\\s*((" + variableName + parameters + ")" + dot + ")+\\s*(%>)");
Matcher matcher = p.matcher(sc.getSource());
while(matcher.find()) {
chunks.add(new TextChunk(matcher.group()));
}
return chunks;
}
@Override
public Node getNode(TextChunk chunk) {
return new ScriptletPrintNode(chunk.getText().replace("<%=", "").replace("%>", "").trim());
}
}