Permalink
Browse files

API change to version 0.1.2: added statement-syntax extension and mov…

…ed some compilation constants to separate class
  • Loading branch information...
Sebastian Baechle
Sebastian Baechle committed Mar 29, 2012
1 parent 465656b commit 34db812bb9dacedc2121baaa45c23fccdd45ffd2
View
@@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.brackit</groupId>
<artifactId>brackit</artifactId>
<version>0.1.1</version>
<version>0.1.2</version>
<name>Brackit Engine</name>
<url>http://brackit.org</url>
<properties>
@@ -0,0 +1,67 @@
/*
* [New BSD License]
* Copyright (c) 2011-2012, Brackit Project Team <info@brackit.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the Brackit Project Team nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.brackit.xquery.compiler;
import static org.brackit.xquery.module.Namespaces.ERR_NSURI;
import org.brackit.xquery.atomic.QNm;
/**
* Collection of brackit-related constants (names, error codes).
*
* @author Sebastian Baechle
*
*/
public class Bits {
/*
* Variables names for internal compilation processes
*/
public static final String FS_NSURI = "http://brackit.org/ns/InternalUseOnly";
public static final String FS_PREFIX = "fs";
public static final QNm FS_DOT = new QNm(FS_NSURI, FS_PREFIX, "dot");
public static final QNm FS_LAST = new QNm(FS_NSURI, FS_PREFIX, "last");
public static final QNm FS_POSITION = new QNm(FS_NSURI, FS_PREFIX,
"position");
public static final QNm FS_PARENT = new QNm(FS_NSURI, FS_PREFIX, "parent");
public static final QNm FS_FOO = new QNm(FS_NSURI, FS_PREFIX, "foo");
/*
* Error codes for runtime errors and built-in expressions.
*/
public static final QNm BIT_ILLEGAL_RECORD_FIELD = new QNm(ERR_NSURI, "bit",
"BITS0001");
public static final QNm BIT_DUPLICATE_RECORD_FIELD = new QNm(ERR_NSURI, "bit",
"BITS0002");
}
@@ -36,6 +36,7 @@
import org.brackit.xquery.atomic.QNm;
import org.brackit.xquery.atomic.Str;
import org.brackit.xquery.compiler.AST;
import org.brackit.xquery.compiler.Bits;
import org.brackit.xquery.compiler.XQ;
import org.brackit.xquery.expr.Variable;
import org.brackit.xquery.module.Functions;
@@ -947,8 +948,8 @@ protected boolean replaceFunctionCall(AST expr, QNm name)
}
// change expr to variable reference
expr.setType(XQ.VariableRef);
QNm newName = name.equals(Functions.FN_POSITION) ? Namespaces.FS_POSITION
: Namespaces.FS_LAST;
QNm newName = name.equals(Functions.FN_POSITION) ? Bits.FS_POSITION
: Bits.FS_LAST;
expr.setValue(newName);
return true;
}
@@ -42,9 +42,9 @@
import org.brackit.xquery.XQuery;
import org.brackit.xquery.atomic.QNm;
import org.brackit.xquery.compiler.AST;
import org.brackit.xquery.compiler.Bits;
import org.brackit.xquery.compiler.XQ;
import org.brackit.xquery.compiler.optimizer.walker.Walker;
import org.brackit.xquery.module.Namespaces;
import org.brackit.xquery.util.dot.DotContext;
import org.brackit.xquery.util.dot.DotNode;
import org.brackit.xquery.util.dot.DotUtil;
@@ -944,9 +944,9 @@ private void filterExpr(AST expr) {
walkInspect(expr.getChild(0), true, false);
for (int i = 1; i < expr.getChildCount(); i++) {
table.openScope(expr.getChild(i), false);
table.bind(Namespaces.FS_DOT, ONE_ITEM);
table.bind(Namespaces.FS_POSITION, ONE_INTEGER);
table.bind(Namespaces.FS_LAST, ONE_INTEGER);
table.bind(Bits.FS_DOT, ONE_ITEM);
table.bind(Bits.FS_POSITION, ONE_INTEGER);
table.bind(Bits.FS_LAST, ONE_INTEGER);
walkInspect(expr.getChild(i), true, false);
table.closeScope();
}
@@ -957,9 +957,9 @@ private void stepExpr(AST expr) {
walkInspect(expr.getChild(1), true, false);
for (int i = 2; i < expr.getChildCount(); i++) {
table.openScope(expr.getChild(i), false);
table.bind(Namespaces.FS_DOT, ONE_ITEM);
table.bind(Namespaces.FS_POSITION, ONE_INTEGER);
table.bind(Namespaces.FS_LAST, ONE_INTEGER);
table.bind(Bits.FS_DOT, ONE_ITEM);
table.bind(Bits.FS_POSITION, ONE_INTEGER);
table.bind(Bits.FS_LAST, ONE_INTEGER);
walkInspect(expr.getChild(i), true, false);
table.closeScope();
}
@@ -970,9 +970,9 @@ private void pathExpr(AST expr) {
for (int i = 0; i < expr.getChildCount(); i++) {
table.openScope(expr.getChild(i), false);
if (i > 0) {
table.bind(Namespaces.FS_DOT, ONE_ITEM);
table.bind(Namespaces.FS_POSITION, ONE_INTEGER);
table.bind(Namespaces.FS_LAST, ONE_INTEGER);
table.bind(Bits.FS_DOT, ONE_ITEM);
table.bind(Bits.FS_POSITION, ONE_INTEGER);
table.bind(Bits.FS_LAST, ONE_INTEGER);
}
walkInspect(expr.getChild(i), true, false);
table.closeScope();
@@ -982,7 +982,7 @@ private void pathExpr(AST expr) {
private void documentExpr(AST node) {
table.openScope(node, false);
table.bind(Namespaces.FS_PARENT, new SequenceType(DocumentType.DOC,
table.bind(Bits.FS_PARENT, new SequenceType(DocumentType.DOC,
Cardinality.One));
walkInspect(node.getChild(0), true, false);
table.closeScope();
@@ -995,7 +995,7 @@ private void elementExpr(AST node) {
}
walkInspect(node.getChild(pos++), true, false);
table.openScope(node, false);
table.bind(Namespaces.FS_PARENT, new SequenceType(ElementType.ELEMENT,
table.bind(Bits.FS_PARENT, new SequenceType(ElementType.ELEMENT,
Cardinality.One));
// visit content sequence
walkInspect(node.getChild(pos++), true, false);
@@ -39,6 +39,7 @@
import org.brackit.xquery.atomic.QNm;
import org.brackit.xquery.atomic.Str;
import org.brackit.xquery.compiler.AST;
import org.brackit.xquery.compiler.Bits;
import org.brackit.xquery.compiler.XQ;
import org.brackit.xquery.module.Functions;
import org.brackit.xquery.util.log.Logger;
@@ -815,7 +816,50 @@ private AST queryBody() throws TokenizerException {
return body;
}
// Begin Custom scripting syntax
private AST expr() throws TokenizerException {
AST first = assignmentClause();
if (first == null) {
first = concatExpr();
if (laSkipWS(";") == null) {
return first;
}
AST c = new AST(XQ.LetClause);
AST bnd = new AST(XQ.TypedVariableBinding);
bnd.addChild(new AST(XQ.Variable, Bits.FS_FOO));
c.addChild(bnd);
c.addChild(first);
first = c;
} else {
if (laSkipWS(";") == null) {
return first.getChild(1);
}
}
QNm lastVar = (QNm) first.getChild(0).getChild(0).getValue();
AST sequenceExpr = new AST(XQ.FlowrExpr);
sequenceExpr.addChild(first);
while (attemptSkipWS(";")) {
AST c = assignmentClause();
if (c == null) {
c = new AST(XQ.LetClause);
AST bnd = new AST(XQ.TypedVariableBinding);
bnd.addChild(new AST(XQ.Variable, Bits.FS_FOO));
c.addChild(bnd);
c.addChild(concatExpr());
lastVar = Bits.FS_FOO;
} else {
lastVar = (QNm) c.getChild(0).getChild(0).getValue();
}
sequenceExpr.addChild(c);
}
AST returnClause = new AST(XQ.ReturnClause);
returnClause.addChild(new AST(XQ.VariableRef, lastVar));
sequenceExpr.addChild(returnClause);
return sequenceExpr;
}
// vanilla XQuery 3.0 expr()
private AST concatExpr() throws TokenizerException {
AST first = exprSingle();
if (!attemptSkipWS(",")) {
return first;
@@ -829,6 +873,8 @@ private AST expr() throws TokenizerException {
return sequenceExpr;
}
// End Custom scripting syntax
private AST exprSingle() throws TokenizerException {
AST expr = flowrExpr();
expr = (expr != null) ? expr : quantifiedExpr();
@@ -1291,7 +1337,7 @@ private AST groupByClause() throws TokenizerException {
groupByClause.addChild(gs);
} while (attemptSkipWS(","));
// per default all non-grouping variables
// in the current flwor are grouped as a sequences
// in the current flwor are grouped as a sequences
AST dftAggregate = new AST(XQ.DftAggregateSpec);
dftAggregate.addChild(new AST(XQ.SequenceAgg));
groupByClause.addChild(dftAggregate);
@@ -1385,7 +1431,7 @@ private AST quantifiedExpr() throws TokenizerException {
if (laSkipWS("$") == null) {
return null;
}
AST qExpr = new AST(XQ.QuantifiedExpr);
AST qExpr = new AST(XQ.QuantifiedExpr);
qExpr.addChild(quantifier);
AST qBinding = new AST(XQ.QuantifiedBinding);
qBinding.addChild(typedVarBinding());
@@ -2184,14 +2230,15 @@ private AST stepExpr() throws TokenizerException {
return tmp;
}
// vanilla XQuery 3.0 step expr
// vanilla XQuery 3.0 step expr
private AST origStepExpr() throws TokenizerException {
AST expr = postFixExpr();
if (expr != null) {
return expr;
}
return axisStep();
}
// END Custom record syntax
private AST axisStep() throws TokenizerException {
@@ -2910,7 +2957,7 @@ private AST dirElemConstructor(boolean nested) throws TokenizerException {
|| (laSkipWS("<!") != null) || (!attemptSkipWS("<"))) {
return null;
}
}
}
// name is expanded after (possible) declaration of in-scope namespaces
AST stag = qnameLiteral(false, false);
QNm name = (QNm) stag.getValue();
@@ -3580,7 +3627,7 @@ private AST pragmaContent() throws TokenizerException {
consume(la);
return (la == null) ? null : new AST(XQ.PragmaContent, la.string());
}
// BEGIN Custom array syntax
private AST index() throws TokenizerException {
if (!attemptSkipWS("[[")) {
@@ -3590,7 +3637,7 @@ private AST index() throws TokenizerException {
consumeSkipWS("]]");
return index;
}
private AST arrayConstructor() throws TokenizerException {
if (!attemptSkipWS("[")) {
return null;
@@ -3643,18 +3690,13 @@ private AST recordConstructor() throws TokenizerException {
AST record = new AST(XQ.RecordConstructor);
do {
AST f;
Token la = laStringSkipWS(true);
EQNameToken la2;
if (la != null) {
Token la;
if (((la = laStringSkipWS(true)) != null)
|| ((la = laNCNameSkipWS()) != null)) {
consume(la);
f = new AST(XQ.KeyValueField);
f.addChild(new AST(XQ.QNm, new QNm(null, null, la.string())));
f.addChild(recordValue());
} else if ((la2 = laEQNameSkipWS(true)) != null) {
consume(la2);
f = new AST(XQ.KeyValueField);
f.addChild(new AST(XQ.QNm, la2.qname()));
f.addChild(recordValue());
} else {
f = new AST(XQ.RecordField);
f.addChild(exprSingle());
@@ -3693,7 +3735,8 @@ private AST recordValue() throws TokenizerException {
EQNameToken la2;
if (la != null) {
consume(la);
args = add(args, new AST(XQ.QNm, new QNm(null, null, la.string())));
args = add(args,
new AST(XQ.QNm, new QNm(null, null, la.string())));
} else if ((la2 = laEQNameSkipWS(true)) != null) {
consume(la2);
args = add(args, new AST(XQ.QNm, la2.qname()));
@@ -3704,8 +3747,40 @@ private AST recordValue() throws TokenizerException {
consumeSkipWS("}");
return args;
}
// END Custom record syntax
// Begin Custom scripting syntax
private AST assignmentClause() throws TokenizerException {
Token la = laSkipWS("$");
if (la == null) {
return null;
}
EQNameToken la2 = laEQName(la, true);
if (la2 == null) {
return null;
}
if ((laSymSkipWS(la2, "as") == null) && (laSkipWS(la2, ":=") == null)) {
return null;
}
consume(la);
consume(la2);
AST ass = new AST(XQ.LetClause);
AST binding = new AST(XQ.TypedVariableBinding);
binding.addChild(new AST(XQ.Variable, la2.qname()));
AST typeDecl = typeDeclaration();
if (typeDecl != null) {
binding.addChild(typeDecl);
}
ass.addChild(binding);
consumeSkipWS(":=");
ass.addChild(concatExpr());
return ass;
}
// End Custom scripting syntax
private AST[] add(AST[] asts, AST ast) {
int len = asts.length;
asts = Arrays.copyOf(asts, len + 1);
Oops, something went wrong.

0 comments on commit 34db812

Please sign in to comment.