Search in sources :

Example 1 with ParsingException

use of com.google.refine.expr.ParsingException in project OpenRefine by OpenRefine.

the class ListFacet method initializeFromJSON.

@Override
public void initializeFromJSON(Project project, JSONObject o) throws JSONException {
    _name = o.getString("name");
    _expression = o.getString("expression");
    _columnName = o.getString("columnName");
    _invert = o.has("invert") && o.getBoolean("invert");
    if (_columnName.length() > 0) {
        Column column = project.columnModel.getColumnByName(_columnName);
        if (column != null) {
            _cellIndex = column.getCellIndex();
        } else {
            _errorMessage = "No column named " + _columnName;
        }
    } else {
        _cellIndex = -1;
    }
    try {
        _eval = MetaParser.parse(_expression);
    } catch (ParsingException e) {
        _errorMessage = e.getMessage();
    }
    _selection.clear();
    JSONArray a = o.getJSONArray("selection");
    int length = a.length();
    for (int i = 0; i < length; i++) {
        JSONObject oc = a.getJSONObject(i);
        JSONObject ocv = oc.getJSONObject("v");
        DecoratedValue decoratedValue = new DecoratedValue(ocv.get("v"), ocv.getString("l"));
        NominalFacetChoice nominalFacetChoice = new NominalFacetChoice(decoratedValue);
        nominalFacetChoice.selected = true;
        _selection.add(nominalFacetChoice);
    }
    _omitBlank = JSONUtilities.getBoolean(o, "omitBlank", false);
    _omitError = JSONUtilities.getBoolean(o, "omitError", false);
    _selectBlank = JSONUtilities.getBoolean(o, "selectBlank", false);
    _selectError = JSONUtilities.getBoolean(o, "selectError", false);
}
Also used : JSONObject(org.json.JSONObject) Column(com.google.refine.model.Column) ParsingException(com.google.refine.expr.ParsingException) JSONArray(org.json.JSONArray) DecoratedValue(com.google.refine.browsing.DecoratedValue)

Example 2 with ParsingException

use of com.google.refine.expr.ParsingException in project OpenRefine by OpenRefine.

the class RangeFacet method initializeFromJSON.

@Override
public void initializeFromJSON(Project project, JSONObject o) throws JSONException {
    _name = o.getString("name");
    _expression = o.getString("expression");
    _columnName = o.getString("columnName");
    if (_columnName.length() > 0) {
        Column column = project.columnModel.getColumnByName(_columnName);
        if (column != null) {
            _cellIndex = column.getCellIndex();
        } else {
            _errorMessage = "No column named " + _columnName;
        }
    } else {
        _cellIndex = -1;
    }
    try {
        _eval = MetaParser.parse(_expression);
    } catch (ParsingException e) {
        _errorMessage = e.getMessage();
    }
    if (o.has(FROM) || o.has(TO)) {
        _from = o.has(FROM) ? o.getDouble(FROM) : _min;
        _to = o.has(TO) ? o.getDouble(TO) : _max;
        _selected = true;
    }
    _selectNumeric = JSONUtilities.getBoolean(o, "selectNumeric", true);
    _selectNonNumeric = JSONUtilities.getBoolean(o, "selectNonNumeric", true);
    _selectBlank = JSONUtilities.getBoolean(o, "selectBlank", true);
    _selectError = JSONUtilities.getBoolean(o, "selectError", true);
    if (!_selectNumeric || !_selectNonNumeric || !_selectBlank || !_selectError) {
        _selected = true;
    }
}
Also used : Column(com.google.refine.model.Column) ParsingException(com.google.refine.expr.ParsingException)

Example 3 with ParsingException

use of com.google.refine.expr.ParsingException in project OpenRefine by OpenRefine.

the class TimeRangeFacet method initializeFromJSON.

@Override
public void initializeFromJSON(Project project, JSONObject o) throws JSONException {
    _name = o.getString("name");
    _expression = o.getString("expression");
    _columnName = o.getString("columnName");
    if (_columnName.length() > 0) {
        Column column = project.columnModel.getColumnByName(_columnName);
        if (column != null) {
            _cellIndex = column.getCellIndex();
        } else {
            _errorMessage = "No column named " + _columnName;
        }
    } else {
        _cellIndex = -1;
    }
    try {
        _eval = MetaParser.parse(_expression);
    } catch (ParsingException e) {
        _errorMessage = e.getMessage();
    }
    if (o.has(FROM) || o.has(TO)) {
        _from = o.has(FROM) ? o.getDouble(FROM) : _min;
        _to = o.has(TO) ? o.getDouble(TO) : _max;
        _selected = true;
    }
    _selectTime = JSONUtilities.getBoolean(o, "selectTime", true);
    _selectNonTime = JSONUtilities.getBoolean(o, "selectNonTime", true);
    _selectBlank = JSONUtilities.getBoolean(o, "selectBlank", true);
    _selectError = JSONUtilities.getBoolean(o, "selectError", true);
    if (!_selectTime || !_selectNonTime || !_selectBlank || !_selectError) {
        _selected = true;
    }
}
Also used : Column(com.google.refine.model.Column) ParsingException(com.google.refine.expr.ParsingException)

Example 4 with ParsingException

use of com.google.refine.expr.ParsingException in project OpenRefine by OpenRefine.

the class GrelTests method testEvalError.

@Test
public void testEvalError() {
    String[] tests = { //                "1=1", // TODO: Throws NullPointerException
    "a.value" };
    for (String test : tests) {
        try {
            Evaluable eval = MetaParser.parse("grel:" + test);
            Object result = eval.evaluate(bindings);
            Assert.assertTrue(result instanceof EvalError);
        } catch (ParsingException e) {
            Assert.fail("Unexpected parse failure: " + test);
        }
    }
}
Also used : Evaluable(com.google.refine.expr.Evaluable) ParsingException(com.google.refine.expr.ParsingException) EvalError(com.google.refine.expr.EvalError) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest) RefineTest(com.google.refine.tests.RefineTest)

Example 5 with ParsingException

use of com.google.refine.expr.ParsingException in project OpenRefine by OpenRefine.

the class Parser method parseFactor.

/**
 *  <term> := <term-start> ( <path-segment> )*
 *  <term-start> :=
 *      <string> | <number> | - <number> | <regex> | <identifier> |
 *      <identifier> ( <expression-list> )
 *
 *  <path-segment> := "[" <expression-list> "]"
 *                  | "." <identifier>
 *                  | "." <identifier> "(" <expression-list> ")"
 */
protected Evaluable parseFactor() throws ParsingException {
    if (_token == null) {
        throw makeException("Expecting something more at end of expression");
    }
    Evaluable eval = null;
    if (_token.type == TokenType.String) {
        eval = new LiteralExpr(_token.text);
        next(false);
    } else if (_token.type == TokenType.Regex) {
        RegexToken t = (RegexToken) _token;
        try {
            Pattern pattern = Pattern.compile(_token.text, t.caseInsensitive ? Pattern.CASE_INSENSITIVE : 0);
            eval = new LiteralExpr(pattern);
            next(false);
        } catch (Exception e) {
            throw makeException("Bad regular expression (" + e.getMessage() + ")");
        }
    } else if (_token.type == TokenType.Number) {
        eval = new LiteralExpr(((NumberToken) _token).value);
        next(false);
    } else if (_token.type == TokenType.Operator && _token.text.equals("-")) {
        // unary minus?
        next(true);
        if (_token != null && _token.type == TokenType.Number) {
            Number n = ((NumberToken) _token).value;
            eval = new LiteralExpr(n instanceof Long ? -n.longValue() : -n.doubleValue());
            next(false);
        } else {
            throw makeException("Bad negative number");
        }
    } else if (_token.type == TokenType.Identifier) {
        String text = _token.text;
        next(false);
        if (_token == null || _token.type != TokenType.Delimiter || !_token.text.equals("(")) {
            eval = "null".equals(text) ? new LiteralExpr(null) : new VariableExpr(text);
        } else if ("PI".equals(text)) {
            eval = new LiteralExpr(Math.PI);
            next(false);
        } else {
            Function f = ControlFunctionRegistry.getFunction(text);
            Control c = ControlFunctionRegistry.getControl(text);
            if (f == null && c == null) {
                throw makeException("Unknown function or control named " + text);
            }
            // swallow (
            next(true);
            List<Evaluable> args = parseExpressionList(")");
            if (c != null) {
                Evaluable[] argsA = makeArray(args);
                String errorMessage = c.checkArguments(argsA);
                if (errorMessage != null) {
                    throw makeException(errorMessage);
                }
                eval = new ControlCallExpr(argsA, c);
            } else {
                eval = new FunctionCallExpr(makeArray(args), f);
            }
        }
    } else if (_token.type == TokenType.Delimiter && _token.text.equals("(")) {
        next(true);
        eval = parseExpression();
        if (_token != null && _token.type == TokenType.Delimiter && _token.text.equals(")")) {
            next(false);
        } else {
            throw makeException("Missing )");
        }
    } else if (_token.type == TokenType.Delimiter && _token.text.equals("[")) {
        // [ ... ] array
        // swallow [
        next(true);
        List<Evaluable> args = parseExpressionList("]");
        eval = new FunctionCallExpr(makeArray(args), new ArgsToArray());
    } else {
        throw makeException("Missing number, string, identifier, regex, or parenthesized expression");
    }
    while (_token != null) {
        if (_token.type == TokenType.Error) {
            throw makeException("Unknown function or control named" + _token.text);
        } else if (_token.type == TokenType.Operator && _token.text.equals(".")) {
            // swallow .
            next(false);
            if (_token == null || _token.type != TokenType.Identifier) {
                throw makeException("Missing function name");
            }
            String identifier = _token.text;
            next(false);
            if (_token != null && _token.type == TokenType.Delimiter && _token.text.equals("(")) {
                // swallow (
                next(true);
                Function f = ControlFunctionRegistry.getFunction(identifier);
                if (f == null) {
                    throw makeException("Unknown function " + identifier);
                }
                List<Evaluable> args = parseExpressionList(")");
                args.add(0, eval);
                eval = new FunctionCallExpr(makeArray(args), f);
            } else {
                eval = new FieldAccessorExpr(eval, identifier);
            }
        } else if (_token.type == TokenType.Delimiter && _token.text.equals("[")) {
            // swallow [
            next(true);
            List<Evaluable> args = parseExpressionList("]");
            args.add(0, eval);
            eval = new FunctionCallExpr(makeArray(args), ControlFunctionRegistry.getFunction("get"));
        } else {
            break;
        }
    }
    return eval;
}
Also used : Pattern(java.util.regex.Pattern) ControlCallExpr(com.google.refine.grel.ast.ControlCallExpr) ParsingException(com.google.refine.expr.ParsingException) Evaluable(com.google.refine.expr.Evaluable) FunctionCallExpr(com.google.refine.grel.ast.FunctionCallExpr) ArgsToArray(com.google.refine.expr.functions.arrays.ArgsToArray) NumberToken(com.google.refine.grel.Scanner.NumberToken) LiteralExpr(com.google.refine.grel.ast.LiteralExpr) FieldAccessorExpr(com.google.refine.grel.ast.FieldAccessorExpr) VariableExpr(com.google.refine.grel.ast.VariableExpr) List(java.util.List) LinkedList(java.util.LinkedList) RegexToken(com.google.refine.grel.Scanner.RegexToken)

Aggregations

ParsingException (com.google.refine.expr.ParsingException)17 Evaluable (com.google.refine.expr.Evaluable)9 Column (com.google.refine.model.Column)8 NumericBinIndex (com.google.refine.browsing.util.NumericBinIndex)6 EvalError (com.google.refine.expr.EvalError)4 Test (org.testng.annotations.Test)4 RefineTest (com.google.refine.RefineTest)3 FilteredRows (com.google.refine.browsing.FilteredRows)3 Color (java.awt.Color)3 BeforeTest (org.testng.annotations.BeforeTest)3 ScatterplotDrawingRowVisitor (com.google.refine.browsing.facets.ScatterplotDrawingRowVisitor)2 ExpressionBasedRowEvaluable (com.google.refine.browsing.util.ExpressionBasedRowEvaluable)2 NumericBinRowIndex (com.google.refine.browsing.util.NumericBinRowIndex)2 Project (com.google.refine.model.Project)2 BufferedImage (java.awt.image.BufferedImage)2 IOException (java.io.IOException)2 List (java.util.List)2 Properties (java.util.Properties)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 DecoratedValue (com.google.refine.browsing.DecoratedValue)1