From bc9bc54d302fe07f5cb78be0e404c424ba87fb6b Mon Sep 17 00:00:00 2001 From: David Huynh Date: Mon, 1 Mar 2010 01:50:56 +0000 Subject: [PATCH] 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 --- .../gridworks/browsing/facets/ListFacet.java | 4 +- .../gridworks/browsing/facets/RangeFacet.java | 4 +- .../util/PreviewExpressionCommand.java | 4 +- .../metaweb/gridworks/expr/MetaParser.java | 69 +++++++++++++++++++ .../operations/ColumnAdditionOperation.java | 4 +- .../operations/TextTransformOperation.java | 4 +- 6 files changed, 79 insertions(+), 10 deletions(-) create mode 100644 src/main/java/com/metaweb/gridworks/expr/MetaParser.java diff --git a/src/main/java/com/metaweb/gridworks/browsing/facets/ListFacet.java b/src/main/java/com/metaweb/gridworks/browsing/facets/ListFacet.java index 7d9affbc4..eba8d8443 100644 --- a/src/main/java/com/metaweb/gridworks/browsing/facets/ListFacet.java +++ b/src/main/java/com/metaweb/gridworks/browsing/facets/ListFacet.java @@ -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"); diff --git a/src/main/java/com/metaweb/gridworks/browsing/facets/RangeFacet.java b/src/main/java/com/metaweb/gridworks/browsing/facets/RangeFacet.java index 61469ef87..e7c9cb92e 100644 --- a/src/main/java/com/metaweb/gridworks/browsing/facets/RangeFacet.java +++ b/src/main/java/com/metaweb/gridworks/browsing/facets/RangeFacet.java @@ -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)) { diff --git a/src/main/java/com/metaweb/gridworks/commands/util/PreviewExpressionCommand.java b/src/main/java/com/metaweb/gridworks/commands/util/PreviewExpressionCommand.java index 02d896439..1d588c2ec 100644 --- a/src/main/java/com/metaweb/gridworks/commands/util/PreviewExpressionCommand.java +++ b/src/main/java/com/metaweb/gridworks/commands/util/PreviewExpressionCommand.java @@ -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(); diff --git a/src/main/java/com/metaweb/gridworks/expr/MetaParser.java b/src/main/java/com/metaweb/gridworks/expr/MetaParser.java new file mode 100644 index 000000000..31202ab6b --- /dev/null +++ b/src/main/java/com/metaweb/gridworks/expr/MetaParser.java @@ -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; + } +} diff --git a/src/main/java/com/metaweb/gridworks/operations/ColumnAdditionOperation.java b/src/main/java/com/metaweb/gridworks/operations/ColumnAdditionOperation.java index 07db5097a..c4b09f556 100644 --- a/src/main/java/com/metaweb/gridworks/operations/ColumnAdditionOperation.java +++ b/src/main/java/com/metaweb/gridworks/operations/ColumnAdditionOperation.java @@ -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 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() { diff --git a/src/main/java/com/metaweb/gridworks/operations/TextTransformOperation.java b/src/main/java/com/metaweb/gridworks/operations/TextTransformOperation.java index d929ed387..0a60ab0a0 100644 --- a/src/main/java/com/metaweb/gridworks/operations/TextTransformOperation.java +++ b/src/main/java/com/metaweb/gridworks/operations/TextTransformOperation.java @@ -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 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() {