|
|
@@ -1,6 +1,7 @@
|
|
|
package net.ranides.assira.g4;
|
|
|
|
|
|
import net.ranides.assira.io.ResourceUtils;
|
|
|
+import net.ranides.assira.text.StrBuilder;
|
|
|
import org.junit.Test;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
@@ -8,39 +9,86 @@ import java.io.IOException;
|
|
|
public class G4ParserTest {
|
|
|
|
|
|
@Test
|
|
|
- public void simple() throws IOException {
|
|
|
+ public void selector() throws IOException {
|
|
|
+ String calc = ResourceUtils.text("Calculator.g4");
|
|
|
+ G4Parser g4 = G4Parser.newInstance(calc);
|
|
|
+
|
|
|
+ G4Selector<Number> s4num = new G4SelectorBuilder<Number>("number")
|
|
|
+ .mapRule("numberInt", node -> Integer.parseInt(node.text()))
|
|
|
+ .mapRule("numberReal", node -> Double.parseDouble(node.text()))
|
|
|
+ .prepare();
|
|
|
+
|
|
|
+ G4Selector<Number> s4ops = new G4SelectorBuilder<Number>("operators")
|
|
|
+ .inherit(s4num)
|
|
|
+ .mapRule("expression#exprǂ3", (node, that) -> {
|
|
|
+ Number a = that.first(node.child(0).get());
|
|
|
+ Number b = that.first(node.child(2).get());
|
|
|
+
|
|
|
+ switch (node.child(1).get().text()) {
|
|
|
+ case "+": return a.doubleValue() + b.doubleValue();
|
|
|
+ case "-": return a.doubleValue() - b.doubleValue();
|
|
|
+ }
|
|
|
+
|
|
|
+ throw new IllegalArgumentException(node.text());
|
|
|
+ })
|
|
|
+ .mapRule("expression#exprǂ2", (node, that) -> {
|
|
|
+
|
|
|
+ // @todo support for children BY LABEL
|
|
|
+ Number a = that.first(node.children("expression").at(0).get());
|
|
|
+ Number b = that.first(node.children("expression").at(1).get());
|
|
|
+
|
|
|
+ switch (node.children("operator2").single().get().text()) {
|
|
|
+ case "*": return a.doubleValue() * b.doubleValue();
|
|
|
+ case "/": return a.doubleValue() / b.doubleValue();
|
|
|
+ }
|
|
|
+
|
|
|
+ throw new IllegalArgumentException(node.text());
|
|
|
+ })
|
|
|
+ .prepare();
|
|
|
+
|
|
|
+ G4Node expr = g4.parse("expression", "4+2*3");
|
|
|
+
|
|
|
+ System.out.println(s4num.first(expr));
|
|
|
+
|
|
|
+ System.out.println(s4ops.aggregate(g4.parse("expression", "4 + 2 * 3"))); // works: 10
|
|
|
+ System.out.println(s4ops.aggregate(g4.parse("expression", "(4 + 2) * 3"))); // works: 18
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void printer() throws IOException {
|
|
|
String calc = ResourceUtils.text("Calculator.g4");
|
|
|
G4Parser g4 = G4Parser.newInstance(calc);
|
|
|
|
|
|
// G4Node tree1 = g4.parse("expression", "(4 + 2) * 3 -- 3*9! / log(2+7, 6+5)");
|
|
|
-// G4Node tree2 = g4.parse("expression", "(4 + 2) * 3 ");
|
|
|
+ G4Node tree2 = g4.parse("expression", "(4 + 2) * 3 ");
|
|
|
// G4Node tree3 = g4.parse("expression", "<4, 5, 72, 2>");
|
|
|
- G4Node tree4 = g4.parse("expression", "hello(2*2, 3+1, -2)");
|
|
|
+// G4Node tree4 = g4.parse("expression", "hello(2*2, 3+1, -2)");
|
|
|
|
|
|
-// System.out.printf("%s = %s%n", tree1.name(), tree1.value());
|
|
|
-// System.out.printf("%s = %s%n", tree2.name(), tree2.value());
|
|
|
+ System.out.println(paths(tree2) );
|
|
|
+ }
|
|
|
|
|
|
-// dump("1 > ", tree1);
|
|
|
-// dump("2 > ", tree2);
|
|
|
-// dump2(tree3);
|
|
|
- dump2(tree4);
|
|
|
+ static String names(G4Node node) {
|
|
|
+ return names(new StrBuilder(), "", node).toString();
|
|
|
}
|
|
|
|
|
|
- static void dump(String indent, G4Node node) {
|
|
|
- if(node.qualifier() != null) {
|
|
|
- System.out.printf("%s%s<%s> = %s%n", indent, node.name(), node.qualifier(), node.value());
|
|
|
- } else {
|
|
|
- System.out.printf("%s%s = %s%n", indent, node.name(), node.value());
|
|
|
- }
|
|
|
+ static StrBuilder names(StrBuilder writer, String indent, G4Node node) {
|
|
|
+ writer.printf("%s%s = '%s'%n", indent, node.name(), node.text());
|
|
|
node.children().forEach(n -> {
|
|
|
- dump(indent+" ", n);
|
|
|
+ names(writer, indent+" ", n);
|
|
|
});
|
|
|
+ return writer;
|
|
|
+ }
|
|
|
+
|
|
|
+ static String paths(G4Node node) {
|
|
|
+ return paths(new StrBuilder(), node).toString();
|
|
|
}
|
|
|
|
|
|
- static void dump2(G4Node node) {
|
|
|
- System.out.printf("%s%n", node.path());
|
|
|
+ static StrBuilder paths(StrBuilder writer, G4Node node) {
|
|
|
+ writer.printf("%s = '%s'%n", node.path(), node.text());
|
|
|
node.children().forEach(n -> {
|
|
|
- dump2(n);
|
|
|
+ paths(writer, n);
|
|
|
});
|
|
|
+ return writer;
|
|
|
}
|
|
|
}
|