You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@chemistry.apache.org by fg...@apache.org on 2009/06/30 18:57:38 UTC
svn commit: r789827 - in
/incubator/chemistry/trunk/chemistry/chemistry-commons/src:
main/antlr3/org/apache/chemistry/cmissql/CmisSqlLexer.g
main/antlr3/org/apache/chemistry/cmissql/CmisSqlParser.g
test/gunit/org/apache/chemistry/cmissql/CmisSql.testsuite
Author: fguillaume
Date: Tue Jun 30 16:57:37 2009
New Revision: 789827
URL: http://svn.apache.org/viewvc?rev=789827&view=rev
Log:
Refactored tree generation for better trees. Allow colon in identifiers
Modified:
incubator/chemistry/trunk/chemistry/chemistry-commons/src/main/antlr3/org/apache/chemistry/cmissql/CmisSqlLexer.g
incubator/chemistry/trunk/chemistry/chemistry-commons/src/main/antlr3/org/apache/chemistry/cmissql/CmisSqlParser.g
incubator/chemistry/trunk/chemistry/chemistry-commons/src/test/gunit/org/apache/chemistry/cmissql/CmisSql.testsuite
Modified: incubator/chemistry/trunk/chemistry/chemistry-commons/src/main/antlr3/org/apache/chemistry/cmissql/CmisSqlLexer.g
URL: http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-commons/src/main/antlr3/org/apache/chemistry/cmissql/CmisSqlLexer.g?rev=789827&r1=789826&r2=789827&view=diff
==============================================================================
--- incubator/chemistry/trunk/chemistry/chemistry-commons/src/main/antlr3/org/apache/chemistry/cmissql/CmisSqlLexer.g (original)
+++ incubator/chemistry/trunk/chemistry/chemistry-commons/src/main/antlr3/org/apache/chemistry/cmissql/CmisSqlLexer.g Tue Jun 30 16:57:37 2009
@@ -21,6 +21,15 @@
lexer grammar CmisSqlLexer;
tokens {
+ COL;
+ LIST;
+ FUNC;
+ UN_OP;
+ BIN_OP;
+ BIN_OP_ANY;
+ NOT_IN;
+ IS_NULL;
+ IS_NOT_NULL;
ORDER_BY;
}
@@ -94,21 +103,13 @@
LTEQ : '<=';
GTEQ : '>=';
-ID
- : ('a'..'z'|'A'..'Z'|'_')('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
- ;
-
-SIGNED_NUMERIC_LITERAL
- : '0'
- | '-'? ('1'..'9')('0'..'9')*
- ;
-
-CHARACTER_STRING_LITERAL
- : '\'' ( ~'\'' | '\'\'')* '\''
- ;
-
-WS : ( ' ' | '\t' | '\r'? '\n' )+ { $channel=HIDDEN; }
+ID :
+ ('a'..'z'|'A'..'Z'|'_')
+ ('a'..'z'|'A'..'Z'|'_'|'0'..'9'|':')*
;
+NUM_LIT : '0' | '-'? ('1'..'9')('0'..'9')*;
+STRING_LIT : '\'' (~'\''|'\'\'')* '\'';
+WS : ( ' ' | '\t' | '\r'? '\n' )+ { $channel=HIDDEN; };
Modified: incubator/chemistry/trunk/chemistry/chemistry-commons/src/main/antlr3/org/apache/chemistry/cmissql/CmisSqlParser.g
URL: http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-commons/src/main/antlr3/org/apache/chemistry/cmissql/CmisSqlParser.g?rev=789827&r1=789826&r2=789827&view=diff
==============================================================================
--- incubator/chemistry/trunk/chemistry/chemistry-commons/src/main/antlr3/org/apache/chemistry/cmissql/CmisSqlParser.g (original)
+++ incubator/chemistry/trunk/chemistry/chemistry-commons/src/main/antlr3/org/apache/chemistry/cmissql/CmisSqlParser.g Tue Jun 30 16:57:37 2009
@@ -22,7 +22,6 @@
options {
tokenVocab = CmisSqlLexer;
- language = Java;
output = AST;
}
@@ -59,9 +58,8 @@
select_list
: STAR
- -> STAR
| select_sublist ( COMMA select_sublist )*
- -> ^(select_sublist select_sublist*)
+ -> ^(LIST select_sublist+)
;
select_sublist
@@ -77,24 +75,28 @@
;
column_reference:
- ( qualifier DOT )? column_name;
+ ( qualifier DOT )? column_name
+ -> ^(COL qualifier? column_name)
+ ;
multi_valued_column_reference:
- ( qualifier DOT )? multi_valued_column_name;
+ ( qualifier DOT )? multi_valued_column_name
+ -> ^(COL qualifier? multi_valued_column_name)
+ ;
string_value_function:
- ( UPPER | LOWER )? LPAR! column_reference RPAR!;
+ ( f=UPPER | f=LOWER ) LPAR column_reference RPAR
+ -> ^(FUNC $f column_reference)
+ ;
numeric_value_function:
- SCORE LPAR RPAR;
+ f=SCORE LPAR RPAR -> ^(FUNC $f);
qualifier:
table_name
//| correlation_name
;
-// FROM stuff
-
from_clause: FROM^ table_reference;
// Use same trick as http://antlr.org/grammar/1057936474293/DmlSQL2.g to
@@ -114,16 +116,14 @@
join_specification:
ON^ LPAR! column_reference EQ! column_reference RPAR!;
-// WHERE stuff
-
where_clause: WHERE^ search_condition;
-// Rewritten from the spec to avoid left-recursion
search_condition:
+ // not a BIN_OP
boolean_term ( OR^ boolean_term )*;
-// Rewritten from the spec to avoid left-recursion
boolean_term:
+ // not a BIN_OP
boolean_factor ( AND^ boolean_factor )*;
boolean_factor:
@@ -146,42 +146,63 @@
;
comparison_predicate:
- value_expression comp_op^ literal;
+ value_expression comp_op literal
+ -> ^(BIN_OP comp_op value_expression literal)
+ ;
comp_op:
EQ | NEQ | LT | GT | LTEQ | GTEQ;
literal:
- SIGNED_NUMERIC_LITERAL
- | CHARACTER_STRING_LITERAL
+ NUM_LIT
+ | STRING_LIT
;
in_predicate:
- column_reference NOT? IN^ LPAR! in_value_list RPAR!;
+ column_reference IN LPAR in_value_list RPAR
+ -> ^(BIN_OP IN column_reference in_value_list)
+ | column_reference NOT IN LPAR in_value_list RPAR
+ -> ^(BIN_OP NOT_IN column_reference in_value_list)
+ ;
in_value_list:
- literal ( COMMA! literal )*;
+ literal ( COMMA literal )*
+ -> ^(LIST literal+)
+ ;
like_predicate:
- column_reference NOT? LIKE^ CHARACTER_STRING_LITERAL;
+ column_reference NOT? LIKE^ STRING_LIT;
-null_predicate
+null_predicate:
// second alternative commented out to remove left recursion for now.
//( column_reference | multi_valued_column_reference ) 'IS' 'NOT'? 'NULL';
- : column_reference IS^ NOT? NULL
+ column_reference IS
+ ( NOT NULL -> ^(UN_OP IS_NOT_NULL column_reference)
+ | NULL -> ^(UN_OP IS_NULL column_reference)
+ )
;
quantified_comparison_predicate:
- literal comp_op^ ANY multi_valued_column_reference;
+ literal comp_op ANY multi_valued_column_reference
+ -> ^(BIN_OP_ANY comp_op literal multi_valued_column_reference)
+ ;
quantified_in_predicate:
- ANY multi_valued_column_reference NOT? IN^ LPAR! in_value_list RPAR!;
+ ANY multi_valued_column_reference
+ ( NOT IN LPAR in_value_list RPAR
+ -> ^(BIN_OP_ANY NOT_IN multi_valued_column_reference in_value_list)
+ | IN LPAR in_value_list RPAR
+ -> ^(BIN_OP_ANY IN multi_valued_column_reference in_value_list)
+ )
+ ;
text_search_predicate:
CONTAINS^ LPAR! qualifier? COMMA! text_search_expression RPAR!;
folder_predicate:
- ( IN_FOLDER | IN_TREE )^ LPAR qualifier? COMMA folder_id RPAR;
+ ( f=IN_FOLDER | f=IN_TREE ) LPAR qualifier? COMMA? folder_id RPAR
+ -> ^(FUNC $f qualifier? folder_id)
+ ;
order_by_clause:
ORDER BY sort_specification ( COMMA sort_specification )*
@@ -189,7 +210,9 @@
;
sort_specification:
- column_name ( ASC | DESC )?;
+ column_name -> ASC column_name
+ | column_name ( ord=ASC | ord=DESC ) -> $ord column_name
+ ;
correlation_name:
ID;
@@ -204,7 +227,7 @@
ID;
folder_id:
- CHARACTER_STRING_LITERAL;
+ STRING_LIT;
text_search_expression:
- CHARACTER_STRING_LITERAL;
+ STRING_LIT;
Modified: incubator/chemistry/trunk/chemistry/chemistry-commons/src/test/gunit/org/apache/chemistry/cmissql/CmisSql.testsuite
URL: http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-commons/src/test/gunit/org/apache/chemistry/cmissql/CmisSql.testsuite?rev=789827&r1=789826&r2=789827&view=diff
==============================================================================
--- incubator/chemistry/trunk/chemistry/chemistry-commons/src/test/gunit/org/apache/chemistry/cmissql/CmisSql.testsuite (original)
+++ incubator/chemistry/trunk/chemistry/chemistry-commons/src/test/gunit/org/apache/chemistry/cmissql/CmisSql.testsuite Tue Jun 30 16:57:37 2009
@@ -21,7 +21,7 @@
package org.apache.chemistry.cmissql;
}
-// Lexer tests
+// ----- Lexer tests -----
ID:
"a" OK
@@ -29,12 +29,15 @@
"toto" OK
"toto123" OK
"toto123_" OK
+"_foo" OK
+"foo:bar" OK
"123" FAIL
"123abc" FAIL
-SIGNED_NUMERIC_LITERAL:
+NUM_LIT:
"123" OK
"0" OK
+"-0" FAIL
"1" OK
"-1" OK
"-123" OK
@@ -42,13 +45,14 @@
"-0123" FAIL
"123abc" FAIL
-CHARACTER_STRING_LITERAL:
+STRING_LIT:
"'abc'" OK
"'a''bc'" OK
"'abc" FAIL
"abc'" FAIL
+"'ab'c'" FAIL
-// Parser tests
+// ----- Parser tests -----
literal:
"123" OK
@@ -60,40 +64,50 @@
"'abc'" OK
order_by_clause:
-"ORDER BY toto" -> (ORDER_BY toto)
-"ORDER BY titi ASC" -> (ORDER_BY titi ASC)
-"ORDER BY tutu DESC" -> (ORDER_BY tutu DESC)
+"ORDER BY foo" -> (ORDER_BY ASC foo)
+"ORDER BY foo ASC" -> (ORDER_BY ASC foo)
+"ORDER BY foo DESC" -> (ORDER_BY DESC foo)
+"ORDER BY foo, bar DESC" -> (ORDER_BY ASC foo DESC bar)
column_reference:
-"toto" -> "toto"
-"toto.titi" -> "toto . titi"
+"foo" -> (COL foo)
+"bar.foo" -> (COL bar foo)
in_predicate:
-"toto IN ( 'a', 'b', 'c')" -> (IN toto 'a' 'b' 'c')
-"toto IN ( 1, 2, 3)" -> (IN toto 1 2 3)
+"foo IN ( 'a', 'b', 'c')" -> (BIN_OP IN (COL foo) (LIST 'a' 'b' 'c'))
+"foo NOT IN ( 1, 2, 3)" -> (BIN_OP NOT_IN (COL foo) (LIST 1 2 3))
+
+quantified_in_predicate:
+"ANY foo IN ('a', 1)" -> (BIN_OP_ANY IN (COL foo) (LIST 'a' 1))
comparison_predicate:
-"a = 1" -> (= a 1)
-"a <> 1" -> (<> a 1)
+"foo = 1" -> (BIN_OP = (COL foo) 1)
+"foo <> 1" -> (BIN_OP <> (COL foo) 1)
predicate:
-"a = 1" -> (= a 1)
-"b IN ('test')" -> (IN b 'test')
-"c IS NULL" -> (IS c NULL)
-"c IS NOT NULL" -> (IS c NOT NULL)
-"1 = ANY d" -> (= 1 ANY d)
-"LOWER(type) = 'email'" -> (= LOWER type 'email')
+"foo = 1" -> (BIN_OP = (COL foo) 1)
+"foo IN ('bar')" -> (BIN_OP IN (COL foo) (LIST 'bar'))
+"foo IS NULL" -> (UN_OP IS_NULL (COL foo))
+"foo IS NOT NULL" -> (UN_OP IS_NOT_NULL (COL foo))
+"1 = ANY foo" -> (BIN_OP_ANY = 1 (COL foo))
+"LOWER(foo) = 'bar'" -> (BIN_OP = (FUNC LOWER (COL foo)) 'bar')
+
+folder_predicate:
+"IN_FOLDER(foo,'ID123')" -> (FUNC IN_FOLDER foo 'ID123')
+"IN_FOLDER(,'ID123')" -> (FUNC IN_FOLDER 'ID123')
+"IN_FOLDER('ID123')" -> (FUNC IN_FOLDER 'ID123')
+"IN_TREE(foo,'ID123')" -> (FUNC IN_TREE foo 'ID123')
+"IN_TREE(,'ID123')" -> (FUNC IN_TREE 'ID123')
+"IN_TREE('ID123')" -> (FUNC IN_TREE 'ID123')
search_condition:
-"toto = 1" -> (= toto 1)
-"a = 1 AND b <> 2 OR c >= 3 AND NOT d <= 4" -> (OR (AND (= a 1) (<> b 2)) (AND (>= c 3) (NOT (<= d 4))))
-
-// ---------------------------------------------------------------
+"foo = 1" -> (BIN_OP = (COL foo) 1)
+"a = 1 AND b <> 2 OR c >= 3 AND NOT d <= 4" -> (OR (AND (BIN_OP = (COL a) 1) (BIN_OP <> (COL b) 2)) (AND (BIN_OP >= (COL c) 3) (NOT (BIN_OP <= (COL d) 4))))
query:
"SELECT * FROM Document" -> (SELECT * (FROM Document))
-"SELECT a, b, c FROM Document" -> (SELECT (a b c) (FROM Document))
-"SELECT a, b, c FROM Document ORDER BY a, b, c" -> (SELECT (a b c) (FROM Document)) (ORDER_BY a b c)
+"SELECT a, b, c FROM Document" -> (SELECT (LIST (COL a) (COL b) (COL c)) (FROM Document))
+"SELECT a, b FROM Document ORDER BY a, b" -> (SELECT (LIST (COL a) (COL b)) (FROM Document)) (ORDER_BY ASC a ASC b)
// Examples from the specs.
@@ -102,7 +116,7 @@
SELECT TITLE, AUTHORS, DATE
FROM WHITE_PAPER
WHERE ( IN_TREE( , 'ID00093854763') ) AND ( 'SMITH' = ANY AUTHORS )
->> -> (SELECT (TITLE AUTHORS DATE) (FROM WHITE_PAPER) (WHERE (AND (IN_TREE ( , 'ID00093854763' )) (= 'SMITH' ANY AUTHORS))))
+>> -> (SELECT (LIST (COL TITLE) (COL AUTHORS) (COL DATE)) (FROM WHITE_PAPER) (WHERE (AND (FUNC IN_TREE 'ID00093854763') (BIN_OP_ANY = 'SMITH' (COL AUTHORS)))))
<<
SELECT OBJECT_ID, SCORE() AS X, DESTINATION, DEPARTURE_DATES