Implemented a meta parser that looks for a language prefix and picks the right parser.

git-svn-id: http://google-refine.googlecode.com/svn/trunk@159 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
David Huynh 2010-03-01 01:50:56 +00:00
parent acfa19a683
commit bc9bc54d30
6 changed files with 79 additions and 10 deletions

View File

@ -14,7 +14,7 @@ import com.metaweb.gridworks.browsing.FilteredRows;
import com.metaweb.gridworks.browsing.filters.ExpressionEqualRowFilter;
import com.metaweb.gridworks.browsing.filters.RowFilter;
import com.metaweb.gridworks.expr.Evaluable;
import com.metaweb.gridworks.gel.Parser;
import com.metaweb.gridworks.expr.MetaParser;
import com.metaweb.gridworks.model.Project;
public class ListFacet implements Facet {
@ -74,7 +74,7 @@ public class ListFacet implements Facet {
_columnName = o.getString("columnName");
_cellIndex = project.columnModel.getColumnByName(_columnName).getCellIndex();
_eval = new Parser(_expression).getExpression();
_eval = MetaParser.parse(_expression);
_selection.clear();
JSONArray a = o.getJSONArray("selection");

View File

@ -10,7 +10,7 @@ import com.metaweb.gridworks.browsing.FilteredRows;
import com.metaweb.gridworks.browsing.filters.ExpressionNumberComparisonRowFilter;
import com.metaweb.gridworks.browsing.filters.RowFilter;
import com.metaweb.gridworks.expr.Evaluable;
import com.metaweb.gridworks.gel.Parser;
import com.metaweb.gridworks.expr.MetaParser;
import com.metaweb.gridworks.model.Column;
import com.metaweb.gridworks.model.Project;
@ -80,7 +80,7 @@ public class RangeFacet implements Facet {
_columnName = o.getString("columnName");
_cellIndex = project.columnModel.getColumnByName(_columnName).getCellIndex();
_eval = new Parser(_expression).getExpression();
_eval = MetaParser.parse(_expression);
_mode = o.getString("mode");
if ("min".equals(_mode)) {

View File

@ -16,8 +16,8 @@ import com.metaweb.gridworks.expr.EvalError;
import com.metaweb.gridworks.expr.Evaluable;
import com.metaweb.gridworks.expr.ExpressionUtils;
import com.metaweb.gridworks.expr.HasFields;
import com.metaweb.gridworks.expr.MetaParser;
import com.metaweb.gridworks.expr.ParsingException;
import com.metaweb.gridworks.gel.Parser;
import com.metaweb.gridworks.model.Cell;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.model.Row;
@ -50,7 +50,7 @@ public class PreviewExpressionCommand extends Command {
writer.object();
try {
Evaluable eval = new Parser(expression).getExpression();
Evaluable eval = MetaParser.parse(expression);
writer.key("code"); writer.value("ok");
writer.key("results"); writer.array();

View File

@ -0,0 +1,69 @@
package com.metaweb.gridworks.expr;
import com.metaweb.gridworks.gel.Parser;
abstract public class MetaParser {
static public Evaluable parse(String s) throws ParsingException {
String language = "gel";
int colon = s.indexOf(':');
if (colon >= 0) {
language = s.substring(0, colon);
}
if ("jython".equalsIgnoreCase(language)) {
return parseJython(s.substring(colon + 1));
} else if ("clojure".equalsIgnoreCase(language)) {
return parseClojure(s.substring(colon + 1));
} else if ("gel".equalsIgnoreCase(language)) {
return parseGEL(s.substring(colon + 1));
} else {
return parseGEL(s);
}
}
static protected Evaluable parseGEL(String s) throws ParsingException {
Parser parser = new Parser(s);
return parser.getExpression();
}
static protected Evaluable parseJython(String s) throws ParsingException {
return null;
}
static protected Evaluable parseClojure(String s) throws ParsingException {
/*
try {
IFn fn = (IFn) clojure.lang.Compiler.load(new StringReader(
"(fn [value row cells] " + s + ")"
));
return new Evaluable() {
final private IFn _fn;
public Evaluable init(IFn fn) {
_fn = fn;
return this;
}
public Object evaluate(Properties bindings) {
try {
return _fn.invoke(
bindings.get("value"),
bindings.get("row"),
bindings.get("cells")
);
} catch (Exception e) {
return new EvalError(e.getMessage());
}
}
}.init(fn);
} catch (Exception e) {
return new ParsingException(e);
}
*/
return null;
}
}

View File

@ -13,7 +13,7 @@ import com.metaweb.gridworks.browsing.FilteredRows;
import com.metaweb.gridworks.browsing.RowVisitor;
import com.metaweb.gridworks.expr.Evaluable;
import com.metaweb.gridworks.expr.ExpressionUtils;
import com.metaweb.gridworks.gel.Parser;
import com.metaweb.gridworks.expr.MetaParser;
import com.metaweb.gridworks.history.Change;
import com.metaweb.gridworks.history.HistoryEntry;
import com.metaweb.gridworks.model.AbstractOperation;
@ -113,7 +113,7 @@ public class ColumnAdditionOperation extends EngineDependentOperation {
protected RowVisitor createRowVisitor(Project project, List<CellAtRow> cellsAtRows) throws Exception {
Column column = project.columnModel.getColumnByName(_baseColumnName);
Evaluable eval = new Parser(_expression).getExpression();
Evaluable eval = MetaParser.parse(_expression);
Properties bindings = ExpressionUtils.createBindings(project);
return new RowVisitor() {

View File

@ -10,7 +10,7 @@ import org.json.JSONWriter;
import com.metaweb.gridworks.browsing.RowVisitor;
import com.metaweb.gridworks.expr.Evaluable;
import com.metaweb.gridworks.expr.ExpressionUtils;
import com.metaweb.gridworks.gel.Parser;
import com.metaweb.gridworks.expr.MetaParser;
import com.metaweb.gridworks.model.AbstractOperation;
import com.metaweb.gridworks.model.Cell;
import com.metaweb.gridworks.model.Column;
@ -64,7 +64,7 @@ public class TextTransformOperation extends EngineDependentMassCellOperation {
protected RowVisitor createRowVisitor(Project project, List<CellChange> cellChanges) throws Exception {
Column column = project.columnModel.getColumnByName(_columnName);
Evaluable eval = new Parser(_expression).getExpression();
Evaluable eval = MetaParser.parse(_expression);
Properties bindings = ExpressionUtils.createBindings(project);
return new RowVisitor() {