Search in sources :

Example 1 with ParseException

use of com.yahoo.vespa.indexinglanguage.parser.ParseException in project vespa by vespa-engine.

the class SDField method parseIndexingScript.

public void parseIndexingScript(String script, Linguistics linguistics) {
    try {
        ScriptParserContext config = new ScriptParserContext(linguistics);
        config.setInputStream(new IndexingInput(script));
        setIndexingScript(ScriptExpression.newInstance(config));
    } catch (ParseException e) {
        throw new RuntimeException("Failed to parser script '" + script + "'.", e);
    }
}
Also used : IndexingInput(com.yahoo.vespa.indexinglanguage.parser.IndexingInput) ParseException(com.yahoo.vespa.indexinglanguage.parser.ParseException) ScriptParserContext(com.yahoo.vespa.indexinglanguage.ScriptParserContext)

Example 2 with ParseException

use of com.yahoo.vespa.indexinglanguage.parser.ParseException in project vespa by vespa-engine.

the class ScriptParserTestCase method requireThatExpressionParserCanBeInvoked.

@Test
public void requireThatExpressionParserCanBeInvoked() throws ParseException {
    try {
        ScriptParser.parseExpression(newContext("foo"));
    } catch (ParseException e) {
        assertException(e, "Encountered \" <IDENTIFIER> \"foo \"\" at line 1, column 1.");
    }
    assertEquals(new InputExpression("foo"), ScriptParser.parseExpression(newContext("input foo")));
    assertEquals(new StatementExpression(new InputExpression("foo"), new EchoExpression()), ScriptParser.parseExpression(newContext("input foo | echo")));
    assertEquals(new ScriptExpression(new StatementExpression(new InputExpression("foo")), new StatementExpression(new EchoExpression())), ScriptParser.parseExpression(newContext("{ input foo; echo }")));
}
Also used : StatementExpression(com.yahoo.vespa.indexinglanguage.expressions.StatementExpression) EchoExpression(com.yahoo.vespa.indexinglanguage.expressions.EchoExpression) ParseException(com.yahoo.vespa.indexinglanguage.parser.ParseException) InputExpression(com.yahoo.vespa.indexinglanguage.expressions.InputExpression) ScriptExpression(com.yahoo.vespa.indexinglanguage.expressions.ScriptExpression) Test(org.junit.Test)

Example 3 with ParseException

use of com.yahoo.vespa.indexinglanguage.parser.ParseException in project vespa by vespa-engine.

the class ScriptParser method parse.

private static <T extends Expression> T parse(ScriptParserContext config, ParserMethod<T> method) throws ParseException {
    CharStream input = config.getInputStream();
    IndexingParser parser = new IndexingParser(input);
    parser.setAnnotatorConfig(config.getAnnotatorConfig());
    parser.setDefaultFieldName(config.getDefaultFieldName());
    parser.setLinguistics(config.getLinguistcs());
    try {
        return method.call(parser);
    } catch (ParseException e) {
        if (!(input instanceof FastCharStream)) {
            throw e;
        }
        throw new ParseException(((FastCharStream) input).formatException(e.getMessage()));
    } finally {
        if (parser.token != null && parser.token.next != null) {
            input.backup(parser.token.next.image.length());
        }
    }
}
Also used : IndexingParser(com.yahoo.vespa.indexinglanguage.parser.IndexingParser) FastCharStream(com.yahoo.javacc.FastCharStream) ParseException(com.yahoo.vespa.indexinglanguage.parser.ParseException) CharStream(com.yahoo.vespa.indexinglanguage.parser.CharStream) FastCharStream(com.yahoo.javacc.FastCharStream)

Example 4 with ParseException

use of com.yahoo.vespa.indexinglanguage.parser.ParseException in project vespa by vespa-engine.

the class AssertIndexingScript method assertIndexing.

public static void assertIndexing(List<String> expected, Iterable<Expression> actual) {
    List<String> parsedExpected = new LinkedList<>();
    for (String str : expected) {
        try {
            parsedExpected.add(Expression.fromString(str).toString());
        } catch (ParseException e) {
            fail(e.getMessage());
        }
    }
    for (Expression actualExp : actual) {
        String str = actualExp.toString();
        assertTrue("Unexpected: " + str, parsedExpected.remove(str));
    }
    assertTrue("Missing: " + parsedExpected.toString(), parsedExpected.isEmpty());
}
Also used : Expression(com.yahoo.vespa.indexinglanguage.expressions.Expression) ParseException(com.yahoo.vespa.indexinglanguage.parser.ParseException) LinkedList(java.util.LinkedList)

Example 5 with ParseException

use of com.yahoo.vespa.indexinglanguage.parser.ParseException in project vespa by vespa-engine.

the class ScriptParserTestCase method requireThatStatementParserCanBeInvoked.

@Test
public void requireThatStatementParserCanBeInvoked() throws ParseException {
    try {
        ScriptParser.parseStatement(newContext("foo"));
    } catch (ParseException e) {
        assertException(e, "Encountered \" <IDENTIFIER> \"foo \"\" at line 1, column 1.");
    }
    assertEquals(new StatementExpression(new InputExpression("foo")), ScriptParser.parseStatement(newContext("input foo")));
    assertEquals(new StatementExpression(new InputExpression("foo"), new EchoExpression()), ScriptParser.parseStatement(newContext("input foo | echo")));
    assertEquals(new StatementExpression(new ScriptExpression(new StatementExpression(new InputExpression("foo")), new StatementExpression(new EchoExpression()))), ScriptParser.parseStatement(newContext("{ input foo; echo }")));
}
Also used : StatementExpression(com.yahoo.vespa.indexinglanguage.expressions.StatementExpression) EchoExpression(com.yahoo.vespa.indexinglanguage.expressions.EchoExpression) ParseException(com.yahoo.vespa.indexinglanguage.parser.ParseException) InputExpression(com.yahoo.vespa.indexinglanguage.expressions.InputExpression) ScriptExpression(com.yahoo.vespa.indexinglanguage.expressions.ScriptExpression) Test(org.junit.Test)

Aggregations

ParseException (com.yahoo.vespa.indexinglanguage.parser.ParseException)5 EchoExpression (com.yahoo.vespa.indexinglanguage.expressions.EchoExpression)2 InputExpression (com.yahoo.vespa.indexinglanguage.expressions.InputExpression)2 ScriptExpression (com.yahoo.vespa.indexinglanguage.expressions.ScriptExpression)2 StatementExpression (com.yahoo.vespa.indexinglanguage.expressions.StatementExpression)2 Test (org.junit.Test)2 FastCharStream (com.yahoo.javacc.FastCharStream)1 ScriptParserContext (com.yahoo.vespa.indexinglanguage.ScriptParserContext)1 Expression (com.yahoo.vespa.indexinglanguage.expressions.Expression)1 CharStream (com.yahoo.vespa.indexinglanguage.parser.CharStream)1 IndexingInput (com.yahoo.vespa.indexinglanguage.parser.IndexingInput)1 IndexingParser (com.yahoo.vespa.indexinglanguage.parser.IndexingParser)1 LinkedList (java.util.LinkedList)1