Created an ast package for gridworks expression language abstract syntax tree nodes. Moved parsing exception class out to its own file.

git-svn-id: http://google-refine.googlecode.com/svn/trunk@156 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
David Huynh 2010-03-01 01:14:21 +00:00
parent 983be19e14
commit 7c38fbb945
17 changed files with 68 additions and 35 deletions

View File

@ -10,7 +10,7 @@ import com.metaweb.gridworks.browsing.FilteredRows;
import com.metaweb.gridworks.browsing.filters.ExpressionStringComparisonRowFilter; import com.metaweb.gridworks.browsing.filters.ExpressionStringComparisonRowFilter;
import com.metaweb.gridworks.browsing.filters.RowFilter; import com.metaweb.gridworks.browsing.filters.RowFilter;
import com.metaweb.gridworks.expr.Evaluable; import com.metaweb.gridworks.expr.Evaluable;
import com.metaweb.gridworks.expr.VariableExpr; import com.metaweb.gridworks.expr.ast.VariableExpr;
import com.metaweb.gridworks.model.Project; import com.metaweb.gridworks.model.Project;
public class TextSearchFacet implements Facet { public class TextSearchFacet implements Facet {

View File

@ -17,7 +17,7 @@ import com.metaweb.gridworks.expr.Evaluable;
import com.metaweb.gridworks.expr.ExpressionUtils; import com.metaweb.gridworks.expr.ExpressionUtils;
import com.metaweb.gridworks.expr.HasFields; import com.metaweb.gridworks.expr.HasFields;
import com.metaweb.gridworks.expr.Parser; import com.metaweb.gridworks.expr.Parser;
import com.metaweb.gridworks.expr.Parser.ParserException; import com.metaweb.gridworks.expr.ParsingException;
import com.metaweb.gridworks.model.Cell; import com.metaweb.gridworks.model.Cell;
import com.metaweb.gridworks.model.Project; import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.model.Row; import com.metaweb.gridworks.model.Row;
@ -85,7 +85,7 @@ public class PreviewExpressionCommand extends Command {
} }
} }
writer.endArray(); writer.endArray();
} catch (ParserException e) { } catch (ParsingException e) {
writer.key("code"); writer.value("error"); writer.key("code"); writer.value("error");
writer.key("type"); writer.value("parser"); writer.key("type"); writer.value("parser");
writer.key("message"); writer.value(e.getMessage()); writer.key("message"); writer.value(e.getMessage());

View File

@ -6,25 +6,23 @@ import java.util.List;
import com.metaweb.gridworks.expr.Scanner.NumberToken; import com.metaweb.gridworks.expr.Scanner.NumberToken;
import com.metaweb.gridworks.expr.Scanner.Token; import com.metaweb.gridworks.expr.Scanner.Token;
import com.metaweb.gridworks.expr.Scanner.TokenType; import com.metaweb.gridworks.expr.Scanner.TokenType;
import com.metaweb.gridworks.expr.ast.ControlCallExpr;
import com.metaweb.gridworks.expr.ast.FieldAccessorExpr;
import com.metaweb.gridworks.expr.ast.FunctionCallExpr;
import com.metaweb.gridworks.expr.ast.LiteralExpr;
import com.metaweb.gridworks.expr.ast.OperatorCallExpr;
import com.metaweb.gridworks.expr.ast.VariableExpr;
public class Parser { public class Parser {
protected Scanner _scanner; protected Scanner _scanner;
protected Token _token; protected Token _token;
protected Evaluable _root; protected Evaluable _root;
static public class ParserException extends Exception { public Parser(String s) throws ParsingException {
private static final long serialVersionUID = 155004505172098755L;
protected ParserException(String message) {
super(message);
}
}
public Parser(String s) throws ParserException {
this(s, 0, s.length()); this(s, 0, s.length());
} }
public Parser(String s, int from, int to) throws ParserException { public Parser(String s, int from, int to) throws ParsingException {
_scanner = new Scanner(s, from, to); _scanner = new Scanner(s, from, to);
_token = _scanner.next(); _token = _scanner.next();
@ -39,13 +37,13 @@ public class Parser {
_token = _scanner.next(); _token = _scanner.next();
} }
protected ParserException makeException(String desc) { protected ParsingException makeException(String desc) {
int index = _token != null ? _token.start : _scanner.getIndex(); int index = _token != null ? _token.start : _scanner.getIndex();
return new ParserException("Parsing error at offset " + index + ": " + desc); return new ParsingException("Parsing error at offset " + index + ": " + desc);
} }
protected Evaluable parseExpression() throws ParserException { protected Evaluable parseExpression() throws ParsingException {
Evaluable sub = parseSubExpression(); Evaluable sub = parseSubExpression();
while (_token != null && while (_token != null &&
@ -64,7 +62,7 @@ public class Parser {
return sub; return sub;
} }
protected Evaluable parseSubExpression() throws ParserException { protected Evaluable parseSubExpression() throws ParsingException {
Evaluable sub = parseTerm(); Evaluable sub = parseTerm();
while (_token != null && while (_token != null &&
@ -83,7 +81,7 @@ public class Parser {
return sub; return sub;
} }
protected Evaluable parseTerm() throws ParserException { protected Evaluable parseTerm() throws ParsingException {
Evaluable factor = parseFactor(); Evaluable factor = parseFactor();
while (_token != null && while (_token != null &&
@ -102,7 +100,7 @@ public class Parser {
return factor; return factor;
} }
protected Evaluable parseFactor() throws ParserException { protected Evaluable parseFactor() throws ParsingException {
if (_token == null) { if (_token == null) {
throw makeException("Expression ends too early"); throw makeException("Expression ends too early");
} }
@ -207,7 +205,7 @@ public class Parser {
return eval; return eval;
} }
protected List<Evaluable> parseExpressionList(String closingDelimiter) throws ParserException { protected List<Evaluable> parseExpressionList(String closingDelimiter) throws ParsingException {
List<Evaluable> l = new LinkedList<Evaluable>(); List<Evaluable> l = new LinkedList<Evaluable>();
if (_token != null && if (_token != null &&

View File

@ -0,0 +1,16 @@
/**
*
*/
package com.metaweb.gridworks.expr;
public class ParsingException extends Exception {
private static final long serialVersionUID = 155004505172098755L;
public ParsingException(String message) {
super(message);
}
public ParsingException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@ -1,7 +1,10 @@
package com.metaweb.gridworks.expr; package com.metaweb.gridworks.expr.ast;
import java.util.Properties; import java.util.Properties;
import com.metaweb.gridworks.expr.Control;
import com.metaweb.gridworks.expr.Evaluable;
public class ControlCallExpr implements Evaluable { public class ControlCallExpr implements Evaluable {
final protected Evaluable[] _args; final protected Evaluable[] _args;
final protected Control _control; final protected Control _control;

View File

@ -1,7 +1,12 @@
package com.metaweb.gridworks.expr; package com.metaweb.gridworks.expr.ast;
import java.util.Properties; import java.util.Properties;
import com.metaweb.gridworks.expr.EvalError;
import com.metaweb.gridworks.expr.Evaluable;
import com.metaweb.gridworks.expr.ExpressionUtils;
import com.metaweb.gridworks.expr.HasFields;
public class FieldAccessorExpr implements Evaluable { public class FieldAccessorExpr implements Evaluable {
final protected Evaluable _inner; final protected Evaluable _inner;
final protected String _fieldName; final protected String _fieldName;

View File

@ -1,7 +1,11 @@
package com.metaweb.gridworks.expr; package com.metaweb.gridworks.expr.ast;
import java.util.Properties; import java.util.Properties;
import com.metaweb.gridworks.expr.Evaluable;
import com.metaweb.gridworks.expr.ExpressionUtils;
import com.metaweb.gridworks.expr.Function;
public class FunctionCallExpr implements Evaluable { public class FunctionCallExpr implements Evaluable {
final protected Evaluable[] _args; final protected Evaluable[] _args;
final protected Function _function; final protected Function _function;

View File

@ -1,9 +1,11 @@
package com.metaweb.gridworks.expr; package com.metaweb.gridworks.expr.ast;
import java.util.Properties; import java.util.Properties;
import org.json.JSONObject; import org.json.JSONObject;
import com.metaweb.gridworks.expr.Evaluable;
public class LiteralExpr implements Evaluable { public class LiteralExpr implements Evaluable {
final protected Object _value; final protected Object _value;

View File

@ -1,7 +1,10 @@
package com.metaweb.gridworks.expr; package com.metaweb.gridworks.expr.ast;
import java.util.Properties; import java.util.Properties;
import com.metaweb.gridworks.expr.Evaluable;
import com.metaweb.gridworks.expr.ExpressionUtils;
public class OperatorCallExpr implements Evaluable { public class OperatorCallExpr implements Evaluable {
final protected Evaluable[] _args; final protected Evaluable[] _args;
final protected String _op; final protected String _op;

View File

@ -1,7 +1,9 @@
package com.metaweb.gridworks.expr; package com.metaweb.gridworks.expr.ast;
import java.util.Properties; import java.util.Properties;
import com.metaweb.gridworks.expr.Evaluable;
public class VariableExpr implements Evaluable { public class VariableExpr implements Evaluable {
final protected String _name; final protected String _name;

View File

@ -12,7 +12,7 @@ import com.metaweb.gridworks.expr.ControlFunctionRegistry;
import com.metaweb.gridworks.expr.EvalError; import com.metaweb.gridworks.expr.EvalError;
import com.metaweb.gridworks.expr.Evaluable; import com.metaweb.gridworks.expr.Evaluable;
import com.metaweb.gridworks.expr.ExpressionUtils; import com.metaweb.gridworks.expr.ExpressionUtils;
import com.metaweb.gridworks.expr.VariableExpr; import com.metaweb.gridworks.expr.ast.VariableExpr;
public class ForEach implements Control { public class ForEach implements Control {
public String checkArguments(Evaluable[] args) { public String checkArguments(Evaluable[] args) {

View File

@ -9,7 +9,7 @@ import com.metaweb.gridworks.expr.Control;
import com.metaweb.gridworks.expr.ControlFunctionRegistry; import com.metaweb.gridworks.expr.ControlFunctionRegistry;
import com.metaweb.gridworks.expr.Evaluable; import com.metaweb.gridworks.expr.Evaluable;
import com.metaweb.gridworks.expr.ExpressionUtils; import com.metaweb.gridworks.expr.ExpressionUtils;
import com.metaweb.gridworks.expr.VariableExpr; import com.metaweb.gridworks.expr.ast.VariableExpr;
public class ForNonBlank implements Control { public class ForNonBlank implements Control {
public String checkArguments(Evaluable[] args) { public String checkArguments(Evaluable[] args) {

View File

@ -8,7 +8,7 @@ import org.json.JSONWriter;
import com.metaweb.gridworks.expr.Control; import com.metaweb.gridworks.expr.Control;
import com.metaweb.gridworks.expr.ControlFunctionRegistry; import com.metaweb.gridworks.expr.ControlFunctionRegistry;
import com.metaweb.gridworks.expr.Evaluable; import com.metaweb.gridworks.expr.Evaluable;
import com.metaweb.gridworks.expr.VariableExpr; import com.metaweb.gridworks.expr.ast.VariableExpr;
public class With implements Control { public class With implements Control {
public String checkArguments(Evaluable[] args) { public String checkArguments(Evaluable[] args) {

View File

@ -5,9 +5,9 @@ import java.util.Properties;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONWriter; import org.json.JSONWriter;
import com.metaweb.gridworks.expr.CalendarParser;
import com.metaweb.gridworks.expr.CalendarParserException;
import com.metaweb.gridworks.expr.Function; import com.metaweb.gridworks.expr.Function;
import com.metaweb.gridworks.expr.util.CalendarParser;
import com.metaweb.gridworks.expr.util.CalendarParserException;
public class ToDate implements Function { public class ToDate implements Function {

View File

@ -7,9 +7,9 @@ import org.apache.commons.lang.StringUtils;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONWriter; import org.json.JSONWriter;
import com.metaweb.gridworks.expr.CalendarParser;
import com.metaweb.gridworks.expr.CalendarParserException;
import com.metaweb.gridworks.expr.Function; import com.metaweb.gridworks.expr.Function;
import com.metaweb.gridworks.expr.util.CalendarParser;
import com.metaweb.gridworks.expr.util.CalendarParserException;
public class Diff implements Function { public class Diff implements Function {

View File

@ -1,4 +1,4 @@
package com.metaweb.gridworks.expr; package com.metaweb.gridworks.expr.util;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;

View File

@ -1,4 +1,4 @@
package com.metaweb.gridworks.expr; package com.metaweb.gridworks.expr.util;
// Taken from http://icecube.wisc.edu/~dglo/software/calparse/index.html // Taken from http://icecube.wisc.edu/~dglo/software/calparse/index.html
// Copyright Dave Glowacki. Released under the BSD license. // Copyright Dave Glowacki. Released under the BSD license.