Skip to content

Instantly share code, notes, and snippets.

@pfmiles
Created August 4, 2012 12:04
Show Gist options
  • Save pfmiles/3257023 to your computer and use it in GitHub Desktop.
Save pfmiles/3257023 to your computer and use it in GitHub Desktop.
Dummy example of dropincc.java
package test;
import com.github.pfmiles.dropincc.Action;
import com.github.pfmiles.dropincc.CC;
import com.github.pfmiles.dropincc.Exe;
import com.github.pfmiles.dropincc.Grule;
import com.github.pfmiles.dropincc.Lang;
import com.github.pfmiles.dropincc.TokenDef;
public class Test {
/**
* <pre>
* S ::= L $
* L ::= Q*
* Q ::= 'a'
* | 'bc'
* | '(' Q ')'
* </pre>
*/
public static void main(String... args) throws Throwable {
Lang lang = new Lang("Dummy");
TokenDef a = lang.newToken("a");
TokenDef bc = lang.newToken("bc");
TokenDef lp = lang.newToken("\\(");
TokenDef rp = lang.newToken("\\)");
Grule L = lang.newGrule();
Grule Q = lang.newGrule();
lang.defineGrule(L, CC.EOF).action(new Action() {
public String act(Object matched) {
return (String) ((Object[]) matched)[0];
}
}); // S ::= L $
L.define(CC.ks(Q)).action(new Action() {
public String act(Object matched) {
Object[] ms = (Object[]) matched;
StringBuilder sb = new StringBuilder();
for (Object m : ms)
sb.append(m);
return sb.toString();
}
});// L ::= Q*
/*
* Q ::= 'a' | 'bc' | '(' Q ')'
*/
Q.define(a).alt(bc).alt(lp, Q, rp).action(new Action() {
public String act(Object matched) {
// returns the middle element matched, that's the returned value
// of the enclosing 'Q' rule
return (String) ((Object[]) matched)[1];
}
});
Exe exe = lang.compile();
System.out.println(exe.eval("a(a)((bc))"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment