From d3dac46edceeeaf89743ea23319687e85942adb7 Mon Sep 17 00:00:00 2001 From: Ramesh Reddy Date: Fri, 19 Sep 2014 10:49:14 -0500 Subject: [PATCH 1/3] OLINGO-434, OLINGO-435: Adding support to navigate Unary and Alias segements of parsed URL in UriParseTreeVisitor Signed-off-by: mibo --- .../core/uri/parser/UriParseTreeVisitor.java | 22 +++++++++++++++++++ .../core/uri/antlr/TestUriParserImpl.java | 7 ++++++ 2 files changed, 29 insertions(+) diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/UriParseTreeVisitor.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/UriParseTreeVisitor.java index c23ae6860..ff19f3e0c 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/UriParseTreeVisitor.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/UriParseTreeVisitor.java @@ -18,6 +18,7 @@ */ package org.apache.olingo.server.core.uri.parser; +import org.antlr.v4.runtime.misc.NotNull; import org.antlr.v4.runtime.misc.ParseCancellationException; import org.antlr.v4.runtime.tree.ParseTree; import org.apache.olingo.commons.api.edm.Edm; @@ -45,6 +46,7 @@ import org.apache.olingo.server.api.uri.UriResource; import org.apache.olingo.server.api.uri.UriResourcePartTyped; import org.apache.olingo.server.api.uri.queryoption.expression.BinaryOperatorKind; import org.apache.olingo.server.api.uri.queryoption.expression.MethodKind; +import org.apache.olingo.server.api.uri.queryoption.expression.UnaryOperatorKind; import org.apache.olingo.server.core.uri.UriInfoImpl; import org.apache.olingo.server.core.uri.UriParameterImpl; import org.apache.olingo.server.core.uri.UriResourceActionImpl; @@ -68,6 +70,7 @@ import org.apache.olingo.server.core.uri.UriResourceValueImpl; import org.apache.olingo.server.core.uri.UriResourceWithKeysImpl; import org.apache.olingo.server.core.uri.antlr.UriLexer; import org.apache.olingo.server.core.uri.antlr.UriParserBaseVisitor; +import org.apache.olingo.server.core.uri.antlr.UriParserParser; import org.apache.olingo.server.core.uri.antlr.UriParserParser.AllEOFContext; import org.apache.olingo.server.core.uri.antlr.UriParserParser.AllExprContext; import org.apache.olingo.server.core.uri.antlr.UriParserParser.AltAddContext; @@ -84,6 +87,7 @@ import org.apache.olingo.server.core.uri.antlr.UriParserParser.BatchEOFContext; import org.apache.olingo.server.core.uri.antlr.UriParserParser.BooleanNonCaseContext; import org.apache.olingo.server.core.uri.antlr.UriParserParser.CastExprContext; import org.apache.olingo.server.core.uri.antlr.UriParserParser.CeilingMethodCallExprContext; +import org.apache.olingo.server.core.uri.antlr.UriParserParser.CommonExprContext; import org.apache.olingo.server.core.uri.antlr.UriParserParser.ConcatMethodCallExprContext; import org.apache.olingo.server.core.uri.antlr.UriParserParser.ConstSegmentContext; import org.apache.olingo.server.core.uri.antlr.UriParserParser.ContainsMethodCallExprContext; @@ -153,6 +157,7 @@ import org.apache.olingo.server.core.uri.antlr.UriParserParser.TopContext; import org.apache.olingo.server.core.uri.antlr.UriParserParser.TotalOffsetMinutesMethodCallExprContext; import org.apache.olingo.server.core.uri.antlr.UriParserParser.TotalsecondsMethodCallExprContext; import org.apache.olingo.server.core.uri.antlr.UriParserParser.TrimMethodCallExprContext; +import org.apache.olingo.server.core.uri.antlr.UriParserParser.UnaryContext; import org.apache.olingo.server.core.uri.antlr.UriParserParser.YearMethodCallExprContext; import org.apache.olingo.server.core.uri.queryoption.CountOptionImpl; import org.apache.olingo.server.core.uri.queryoption.ExpandItemImpl; @@ -168,6 +173,7 @@ import org.apache.olingo.server.core.uri.queryoption.SkipOptionImpl; import org.apache.olingo.server.core.uri.queryoption.SkipTokenOptionImpl; import org.apache.olingo.server.core.uri.queryoption.SystemQueryOptionImpl; import org.apache.olingo.server.core.uri.queryoption.TopOptionImpl; +import org.apache.olingo.server.core.uri.queryoption.expression.AliasImpl; import org.apache.olingo.server.core.uri.queryoption.expression.BinaryImpl; import org.apache.olingo.server.core.uri.queryoption.expression.EnumerationImpl; import org.apache.olingo.server.core.uri.queryoption.expression.ExpressionImpl; @@ -175,6 +181,7 @@ import org.apache.olingo.server.core.uri.queryoption.expression.LiteralImpl; import org.apache.olingo.server.core.uri.queryoption.expression.MemberImpl; import org.apache.olingo.server.core.uri.queryoption.expression.MethodImpl; import org.apache.olingo.server.core.uri.queryoption.expression.TypeLiteralImpl; +import org.apache.olingo.server.core.uri.queryoption.expression.UnaryImpl; import java.util.ArrayList; import java.util.List; @@ -2156,4 +2163,19 @@ public class UriParseTreeVisitor extends UriParserBaseVisitor { return new ParseCancellationException(uriParserException); } + @Override + public ExpressionImpl visitAltUnary(@NotNull UriParserParser.AltUnaryContext ctx) { + UnaryImpl unary = new UnaryImpl(); + unary.setOperator(ctx.unary().NOT() == null?UnaryOperatorKind.MINUS:UnaryOperatorKind.NOT); + unary.setOperand((ExpressionImpl)ctx.commonExpr().accept(this)); + return unary; + } + + @Override + public ExpressionImpl visitAltAlias(@NotNull UriParserParser.AltAliasContext ctx) { + AliasImpl alias = new AliasImpl(); + alias.setParameter("@"+ctx.odataIdentifier().getChild(0).getText()); + return alias; + } + } diff --git a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/antlr/TestUriParserImpl.java b/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/antlr/TestUriParserImpl.java index 844fdab3e..1ccebbb5b 100644 --- a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/antlr/TestUriParserImpl.java +++ b/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/antlr/TestUriParserImpl.java @@ -576,6 +576,7 @@ public class TestUriParserImpl { testFilter.runESabc("a eq b or c and e eq f").isCompr("<< eq > or < and < eq >>>"); testFilter.runESabc("a eq b or c eq d and e ").isCompr("<< eq > or << eq > and >>"); testFilter.runESabc("a eq b or c eq d and e eq f").isCompr("<< eq > or << eq > and < eq >>>"); + testFilter.runESabc("not a").isCompr(">"); } @Test @@ -1052,6 +1053,12 @@ public class TestUriParserImpl { testUri.run("FICRTCollCTTwoPrimParam(ParameterInt16=1,ParameterString='2')/olingo.odata.test1.CTBase"); } + @Test + public void testAlias() throws Exception { + testUri.run("ESAllPrim?$filter=PropertyInt16 eq @p1&@p1=1)") + .goFilter().is("< eq <@p1>>"); + } + @Test public void testLambda() throws Exception { testUri.run("ESTwoKeyNav", "$filter=CollPropertyComp/all( l : true )") From 4a4b110f9995fc82125405e71d17087e84c25331 Mon Sep 17 00:00:00 2001 From: Michael Bolz Date: Tue, 7 Oct 2014 15:28:52 +0200 Subject: [PATCH 2/3] [OLINGO-434] Fixed wrong MINUS position in Antlr file --- .../org/apache/olingo/server/core/uri/antlr/UriLexer.g4 | 6 ++++-- .../server/core/uri/parser/UriParseTreeVisitor.java | 5 ++--- .../apache/olingo/server/core/uri/antlr/TestLexer.java | 5 +++++ .../olingo/server/core/uri/antlr/TestUriParserImpl.java | 9 +++++++-- 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/lib/server-core/src/main/antlr4/org/apache/olingo/server/core/uri/antlr/UriLexer.g4 b/lib/server-core/src/main/antlr4/org/apache/olingo/server/core/uri/antlr/UriLexer.g4 index eee648602..737ad4b6b 100644 --- a/lib/server-core/src/main/antlr4/org/apache/olingo/server/core/uri/antlr/UriLexer.g4 +++ b/lib/server-core/src/main/antlr4/org/apache/olingo/server/core/uri/antlr/UriLexer.g4 @@ -132,7 +132,9 @@ TRUE : 'true'; FALSE : 'false'; BOOLEAN : T R U E | F A L S E; PLUS : '+'; -SIGN : PLUS | '%2B' |'-'; + +MINUS : '-'; +SIGN : PLUS | '%2B' | '-'; INT : SIGN? DIGITS; DECIMAL : INT '.' DIGITS (('e'|'E') SIGN? DIGITS)?; NANINFINITY : 'NaN' | '-INF' | 'INF'; @@ -184,7 +186,7 @@ OR : 'or'; NOT : 'not'; -MINUS :'-'; + IT : '$it'; ITSLASH : '$it/'; diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/UriParseTreeVisitor.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/UriParseTreeVisitor.java index ff19f3e0c..2c3d87c6d 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/UriParseTreeVisitor.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/UriParseTreeVisitor.java @@ -2166,8 +2166,8 @@ public class UriParseTreeVisitor extends UriParserBaseVisitor { @Override public ExpressionImpl visitAltUnary(@NotNull UriParserParser.AltUnaryContext ctx) { UnaryImpl unary = new UnaryImpl(); - unary.setOperator(ctx.unary().NOT() == null?UnaryOperatorKind.MINUS:UnaryOperatorKind.NOT); - unary.setOperand((ExpressionImpl)ctx.commonExpr().accept(this)); + unary.setOperator(ctx.unary().NOT() == null? UnaryOperatorKind.MINUS: UnaryOperatorKind.NOT); + unary.setOperand((ExpressionImpl) ctx.commonExpr().accept(this)); return unary; } @@ -2177,5 +2177,4 @@ public class UriParseTreeVisitor extends UriParserBaseVisitor { alias.setParameter("@"+ctx.odataIdentifier().getChild(0).getText()); return alias; } - } diff --git a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/antlr/TestLexer.java b/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/antlr/TestLexer.java index e8aa9cecd..76c11aa59 100644 --- a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/antlr/TestLexer.java +++ b/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/antlr/TestLexer.java @@ -48,6 +48,11 @@ public class TestLexer { // ; 0. URI // ;------------------------------------------------------------------------------ + @Test + public void testUnary() { + test.run("- a eq a").isAllInput(); + } + @Test public void testUriTokens() { test.globalMode(UriLexer.MODE_QUERY); diff --git a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/antlr/TestUriParserImpl.java b/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/antlr/TestUriParserImpl.java index 1ccebbb5b..30e1b0da4 100644 --- a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/antlr/TestUriParserImpl.java +++ b/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/antlr/TestUriParserImpl.java @@ -566,6 +566,12 @@ public class TestUriParserImpl { } + @Test + public void testUnary() throws UriParserException { + testFilter.runESabc("not a").isCompr(">"); + testFilter.runESabc("- a eq a").isCompr("<<- > eq >"); + } + @Test public void testFilterComplexMixedPriority() throws UriParserException { testFilter.runESabc("a or c and e ").isCompr("< or < and >>"); @@ -576,7 +582,6 @@ public class TestUriParserImpl { testFilter.runESabc("a eq b or c and e eq f").isCompr("<< eq > or < and < eq >>>"); testFilter.runESabc("a eq b or c eq d and e ").isCompr("<< eq > or << eq > and >>"); testFilter.runESabc("a eq b or c eq d and e eq f").isCompr("<< eq > or << eq > and < eq >>>"); - testFilter.runESabc("not a").isCompr(">"); } @Test @@ -1055,7 +1060,7 @@ public class TestUriParserImpl { @Test public void testAlias() throws Exception { - testUri.run("ESAllPrim?$filter=PropertyInt16 eq @p1&@p1=1)") + testUri.run("ESAllPrim", "$filter=PropertyInt16 eq @p1&@p1=1)") .goFilter().is("< eq <@p1>>"); } From 5c237fbe57510e40806e24bb3bbc3cd21b3c324b Mon Sep 17 00:00:00 2001 From: Michael Bolz Date: Tue, 7 Oct 2014 15:43:04 +0200 Subject: [PATCH 3/3] [OLINGO-434] Added optional WSP between MINUS and property --- .../antlr4/org/apache/olingo/server/core/uri/antlr/UriParser.g4 | 2 +- .../apache/olingo/server/core/uri/antlr/TestUriParserImpl.java | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/server-core/src/main/antlr4/org/apache/olingo/server/core/uri/antlr/UriParser.g4 b/lib/server-core/src/main/antlr4/org/apache/olingo/server/core/uri/antlr/UriParser.g4 index 404c04fc5..1ff58394f 100644 --- a/lib/server-core/src/main/antlr4/org/apache/olingo/server/core/uri/antlr/UriParser.g4 +++ b/lib/server-core/src/main/antlr4/org/apache/olingo/server/core/uri/antlr/UriParser.g4 @@ -191,7 +191,7 @@ contextFragment : REST; // the context fragment is only required on the clie commonExpr : OPEN commonExpr CLOSE #altPharenthesis | vE1=commonExpr (WSP HAS WSP) vE2=commonExpr #altHas | methodCallExpr #altMethod - | ( unary WSP ) commonExpr #altUnary + | ( unary WSP? ) commonExpr #altUnary | anyExpr #altAny | allExpr #altAll | memberExpr #altMember diff --git a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/antlr/TestUriParserImpl.java b/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/antlr/TestUriParserImpl.java index 30e1b0da4..1dce3df88 100644 --- a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/antlr/TestUriParserImpl.java +++ b/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/antlr/TestUriParserImpl.java @@ -570,6 +570,7 @@ public class TestUriParserImpl { public void testUnary() throws UriParserException { testFilter.runESabc("not a").isCompr(">"); testFilter.runESabc("- a eq a").isCompr("<<- > eq >"); + testFilter.runESabc("-a eq a").isCompr("<<- > eq >"); } @Test