You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by fm...@apache.org on 2014/11/28 11:18:09 UTC
svn commit: r1642281 [4/14] - in /sling/trunk/contrib/scripting/sightly: ./
engine/ engine/src/main/antlr4/org/apache/sling/parser/expr/generated/
engine/src/main/antlr4/org/apache/sling/scripting/
engine/src/main/antlr4/org/apache/sling/scripting/sigh...
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/VariableAnalyzer.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/VariableAnalyzer.java?rev=1642281&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/VariableAnalyzer.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/VariableAnalyzer.java Fri Nov 28 10:18:01 2014
@@ -0,0 +1,222 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ******************************************************************************/
+
+package org.apache.sling.scripting.sightly.impl.compiled;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+
+import org.apache.sling.scripting.sightly.impl.compiler.util.VariableTracker;
+
+/**
+ * Data structure used in the analysis of variables
+ * during the compilation process
+ */
+public class VariableAnalyzer {
+
+ private static final HashSet<String> javaKeywords = new HashSet<String>();
+ private final VariableTracker<VariableDescriptor> tracker = new VariableTracker<VariableDescriptor>();
+ private final List<VariableDescriptor> variables = new ArrayList<VariableDescriptor>();
+ private final HashMap<String, VariableDescriptor> dynamicVariables = new HashMap<String, VariableDescriptor>();
+ private final HashMap<String, VariableDescriptor> staticVariables = new HashMap<String, VariableDescriptor>();
+ private static final String DYNAMIC_PREFIX = "_dynamic_";
+ private static final String GLOBAL_PREFIX = "_global_";
+
+ /**
+ * Mark the declaration of a variable in the Java code
+ * @param originalName - the original name of the variable
+ * @return - a variable descriptor uniquely assigned to this variable
+ */
+ public VariableDescriptor declareVariable(String originalName, Type type) {
+ originalName = originalName.toLowerCase();
+ String assignedName = findSafeName(originalName);
+ VariableDescriptor descriptor = new VariableDescriptor(originalName, assignedName, type, VariableScope.SCOPED);
+ tracker.pushVariable(originalName, descriptor);
+ variables.add(descriptor);
+ return descriptor;
+ }
+
+ /**
+ * Declare a global variable. Redundant declarations are ignored
+ * @param originalName - the original name of the variable
+ * @return a variable descriptor
+ */
+ public VariableDescriptor declareGlobal(String originalName) {
+ originalName = originalName.toLowerCase();
+ VariableDescriptor descriptor = staticVariables.get(originalName);
+ if (descriptor == null) {
+ String assignedName = findGlobalName(originalName);
+ descriptor = new VariableDescriptor(originalName, assignedName, Type.UNKNOWN, VariableScope.GLOBAL);
+ variables.add(descriptor);
+ staticVariables.put(originalName, descriptor);
+ }
+ return descriptor;
+ }
+
+ /**
+ * Mark this variable as a template
+ * @param originalName - the original name of the variable
+ * @return a variable descriptor
+ */
+ public VariableDescriptor declareTemplate(String originalName) {
+ originalName = originalName.toLowerCase();
+ VariableDescriptor descriptor = dynamicDescriptor(originalName);
+ descriptor.markAsTemplate();
+ return descriptor;
+ }
+
+ /**
+ * Mark the end of a variable scope
+ */
+ public VariableDescriptor endVariable() {
+ VariableDescriptor descriptor = tracker.peek().getValue();
+ tracker.popVariable();
+ return descriptor;
+ }
+
+ /**
+ * Get a the descriptor for the given variable
+ * @param name the original lowerName of the variable
+ * @return the variable descriptor. If the variable is not in scope,
+ * then a dynamic variable descriptor is provided
+ */
+ public VariableDescriptor descriptor(String name) {
+ String lowerName = name.toLowerCase();
+ VariableDescriptor descriptor = tracker.get(lowerName);
+ if (descriptor == null) {
+ descriptor = staticVariables.get(lowerName);
+ }
+ if (descriptor == null) {
+ descriptor = dynamicDescriptor(lowerName);
+ }
+ return descriptor;
+ }
+
+ /**
+ * Get the collection of all the variables encountered so far
+ * @return an unmodifiable list of all the variables tracked by this analyzer
+ */
+ public Collection<VariableDescriptor> allVariables() {
+ return Collections.unmodifiableList(variables);
+ }
+
+ /**
+ * Shortcut method that returns the assigned name for the given
+ * variable
+ * @param original the original variable name
+ * @return the assigned name for this compilation process
+ */
+ public String assignedName(String original) {
+ return descriptor(original).getAssignedName();
+ }
+
+ private VariableDescriptor dynamicDescriptor(String original) {
+ VariableDescriptor descriptor = dynamicVariables.get(original);
+ if (descriptor == null) {
+ String dynamicName = findDynamicName(original);
+ descriptor = new VariableDescriptor(original, dynamicName, Type.UNKNOWN, VariableScope.DYNAMIC);
+ dynamicVariables.put(original, descriptor);
+ variables.add(descriptor);
+ }
+ return descriptor;
+ }
+
+ private String findDynamicName(String original) {
+ return DYNAMIC_PREFIX + original;
+ }
+
+ private String findGlobalName(String original) {
+ return GLOBAL_PREFIX + syntaxSafeName(original);
+ }
+
+ private String findSafeName(String original) {
+ int occurrenceCount = tracker.getOccurrenceCount(original);
+ String syntaxSafe = syntaxSafeName(original);
+ if (occurrenceCount == 0) {
+ return syntaxSafe; //no other declarations in scope. Use this very name
+ } else {
+ return original + "_" + occurrenceCount;
+ }
+ }
+
+ private String syntaxSafeName(String original) {
+ if (javaKeywords.contains(original)) {
+ return "_" + original;
+ }
+ return original.replaceAll("-", "_");
+ }
+
+ static {
+ javaKeywords.add("abstract");
+ javaKeywords.add("continue");
+ javaKeywords.add("for");
+ javaKeywords.add("new");
+ javaKeywords.add("switch");
+ javaKeywords.add("assert");
+ javaKeywords.add("default");
+ javaKeywords.add("goto");
+ javaKeywords.add("package");
+ javaKeywords.add("synchronized");
+ javaKeywords.add("boolean");
+ javaKeywords.add("do");
+ javaKeywords.add("if");
+ javaKeywords.add("private");
+ javaKeywords.add("this");
+ javaKeywords.add("break");
+ javaKeywords.add("double");
+ javaKeywords.add("implements");
+ javaKeywords.add("protected");
+ javaKeywords.add("throw");
+ javaKeywords.add("byte");
+ javaKeywords.add("else");
+ javaKeywords.add("import");
+ javaKeywords.add("public");
+ javaKeywords.add("throws");
+ javaKeywords.add("case");
+ javaKeywords.add("enum");
+ javaKeywords.add("instanceof");
+ javaKeywords.add("return");
+ javaKeywords.add("transient");
+ javaKeywords.add("catch");
+ javaKeywords.add("extends");
+ javaKeywords.add("int");
+ javaKeywords.add("short");
+ javaKeywords.add("try");
+ javaKeywords.add("char");
+ javaKeywords.add("final");
+ javaKeywords.add("interface");
+ javaKeywords.add("static");
+ javaKeywords.add("void");
+ javaKeywords.add("class");
+ javaKeywords.add("finally");
+ javaKeywords.add("long");
+ javaKeywords.add("strictfp");
+ javaKeywords.add("volatile");
+ javaKeywords.add("const");
+ javaKeywords.add("float");
+ javaKeywords.add("native");
+ javaKeywords.add("super");
+ javaKeywords.add("while");
+ }
+
+}
Propchange: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/VariableAnalyzer.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/VariableDescriptor.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/VariableDescriptor.java?rev=1642281&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/VariableDescriptor.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/VariableDescriptor.java Fri Nov 28 10:18:01 2014
@@ -0,0 +1,107 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ******************************************************************************/
+
+package org.apache.sling.scripting.sightly.impl.compiled;
+
+/**
+ * Information about a variable during the compilation process
+ */
+public class VariableDescriptor {
+
+ private final String originalName;
+ private final String assignedName;
+ private VariableScope scope;
+ private String listCoercion;
+ private final Type type;
+ private boolean templateVariable;
+
+ public VariableDescriptor(String originalName, String assignedName, Type type, VariableScope scope) {
+ this.originalName = originalName;
+ this.assignedName = assignedName;
+ this.scope = scope;
+ this.type = type;
+ }
+
+ /**
+ * The name of the list coercion variable
+ * @return - the variable that will hold the list coercion
+ */
+ public String requireListCoercion() {
+ if (listCoercion == null) {
+ listCoercion = assignedName + "_list_coerced$";
+ }
+ return listCoercion;
+ }
+
+ public String getListCoercion() {
+ return listCoercion;
+ }
+
+ /**
+ * Get the original name of the variable, as it was provided
+ * by the command that introduced the variable
+ * @return - the original variable name
+ */
+ public String getOriginalName() {
+ return originalName;
+ }
+
+ /**
+ * Get the assigned name for the variable - the name that will
+ * actually be used during compilation
+ * @return - the assigned variable name
+ */
+ public String getAssignedName() {
+ return assignedName;
+ }
+
+ /**
+ * Get the scope of this variable
+ * @return the variable scope
+ */
+ public VariableScope getScope() {
+ return scope;
+ }
+
+ /**
+ * Get the inferred type fot this variable
+ * @return - the class of the variable
+ */
+ public Type getType() {
+ return type;
+ }
+
+ /**
+ * Check whether this is a template variable
+ * @return true if it is a template variable
+ */
+ public boolean isTemplateVariable() {
+ return templateVariable;
+ }
+
+ /**
+ * Signal that this variable stands for a template
+ */
+ public void markAsTemplate() {
+ if (scope != VariableScope.DYNAMIC) {
+ throw new UnsupportedOperationException("Only dynamic variables can be marked as templates");
+ }
+ templateVariable = true;
+ }
+}
Propchange: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/VariableDescriptor.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/VariableScope.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/VariableScope.java?rev=1642281&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/VariableScope.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/VariableScope.java Fri Nov 28 10:18:01 2014
@@ -0,0 +1,27 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ******************************************************************************/
+
+package org.apache.sling.scripting.sightly.impl.compiled;
+
+/**
+ * The scope of a variable in a Sightly script
+ */
+public enum VariableScope {
+ DYNAMIC, SCOPED, GLOBAL
+}
Propchange: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/VariableScope.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/BinaryOpGen.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/BinaryOpGen.java?rev=1642281&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/BinaryOpGen.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/BinaryOpGen.java Fri Nov 28 10:18:01 2014
@@ -0,0 +1,35 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ******************************************************************************/
+
+package org.apache.sling.scripting.sightly.impl.compiled.operator;
+
+import org.apache.sling.scripting.sightly.impl.compiled.ExpressionTranslator;
+import org.apache.sling.scripting.sightly.impl.compiled.JavaSource;
+import org.apache.sling.scripting.sightly.impl.compiled.Type;
+
+/**
+ * Generator for a binary operator
+ */
+public interface BinaryOpGen {
+
+ Type returnType(Type leftType, Type rightType);
+
+ void generate(JavaSource source, ExpressionTranslator visitor, TypedNode left, TypedNode right);
+
+}
Propchange: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/BinaryOpGen.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/ComparisonOpGen.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/ComparisonOpGen.java?rev=1642281&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/ComparisonOpGen.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/ComparisonOpGen.java Fri Nov 28 10:18:01 2014
@@ -0,0 +1,84 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ******************************************************************************/
+
+package org.apache.sling.scripting.sightly.impl.compiled.operator;
+
+import org.apache.sling.scripting.sightly.impl.compiled.ExpressionTranslator;
+import org.apache.sling.scripting.sightly.impl.compiled.JavaSource;
+import org.apache.sling.scripting.sightly.impl.compiled.Type;
+import org.apache.sling.scripting.sightly.impl.compiler.expression.ExpressionNode;
+import org.apache.sling.scripting.sightly.impl.compiler.expression.node.BinaryOperator;
+import org.apache.sling.scripting.sightly.impl.compiler.util.expression.SideEffectVisitor;
+
+/**
+ * Generator for logical operators
+ */
+public class ComparisonOpGen implements BinaryOpGen {
+
+ private final String javaOperator;
+ private final boolean inverted;
+ private final String runtimeMethod;
+
+ public ComparisonOpGen(BinaryOperator operator) {
+ switch (operator) {
+ case LT: runtimeMethod = BinaryOperator.METHOD_LT; inverted = false; javaOperator = "<"; break;
+ case GT: runtimeMethod = BinaryOperator.METHOD_LEQ; inverted = true; javaOperator = ">"; break;
+ case LEQ: runtimeMethod = BinaryOperator.METHOD_LEQ; inverted = false; javaOperator = "<="; break;
+ case GEQ: runtimeMethod = BinaryOperator.METHOD_LT; inverted = true; javaOperator = ">="; break;
+ default: throw new IllegalArgumentException("Operator is not a comparison operator: " + operator);
+ }
+ }
+
+ @Override
+ public Type returnType(Type left, Type right) {
+ return Type.BOOLEAN;
+ }
+
+ @Override
+ public void generate(JavaSource source, ExpressionTranslator visitor, TypedNode left, TypedNode right) {
+ Type type = OpHelper.sameType(left, right);
+ if (OpHelper.isNumericType(type)) {
+ generateWithOperator(source, visitor, left.getNode(), right.getNode());
+ } else {
+ generateGeneric(source, visitor, left.getNode(), right.getNode());
+ }
+ }
+
+ private void generateGeneric(JavaSource source, SideEffectVisitor visitor, ExpressionNode leftNode, ExpressionNode rightNode) {
+ if (inverted) {
+ source.negation().startExpression();
+ }
+ source.startMethodCall(BinaryOperator.OBJECT_NAME, runtimeMethod);
+ leftNode.accept(visitor);
+ source.separateArgument();
+ rightNode.accept(visitor);
+ source.endCall();
+ if (inverted) {
+ source.endExpression();
+ }
+ }
+
+ private void generateWithOperator(JavaSource source, SideEffectVisitor visitor,
+ ExpressionNode leftNode, ExpressionNode rightNode) {
+ leftNode.accept(visitor);
+ source.append(javaOperator);
+ rightNode.accept(visitor);
+ }
+
+}
Propchange: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/ComparisonOpGen.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/ConcatenateOpGen.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/ConcatenateOpGen.java?rev=1642281&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/ConcatenateOpGen.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/ConcatenateOpGen.java Fri Nov 28 10:18:01 2014
@@ -0,0 +1,48 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ******************************************************************************/
+
+package org.apache.sling.scripting.sightly.impl.compiled.operator;
+
+import org.apache.sling.scripting.sightly.impl.compiled.ExpressionTranslator;
+import org.apache.sling.scripting.sightly.impl.compiled.GenHelper;
+import org.apache.sling.scripting.sightly.impl.compiled.JavaSource;
+import org.apache.sling.scripting.sightly.impl.compiled.Type;
+
+/**
+ * Concatenation operator generation
+ */
+public final class ConcatenateOpGen implements BinaryOpGen {
+
+ public static final ConcatenateOpGen INSTANCE = new ConcatenateOpGen();
+
+ private ConcatenateOpGen() {
+ }
+
+ @Override
+ public Type returnType(Type leftType, Type rightType) {
+ return Type.STRING;
+ }
+
+ @Override
+ public void generate(JavaSource source, ExpressionTranslator visitor, TypedNode left, TypedNode right) {
+ GenHelper.typeCoercion(source, visitor, left, Type.STRING);
+ source.append("+");
+ GenHelper.typeCoercion(source, visitor, right, Type.STRING);
+ }
+}
Propchange: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/ConcatenateOpGen.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/EquivalenceOpGen.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/EquivalenceOpGen.java?rev=1642281&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/EquivalenceOpGen.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/EquivalenceOpGen.java Fri Nov 28 10:18:01 2014
@@ -0,0 +1,91 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ******************************************************************************/
+
+package org.apache.sling.scripting.sightly.impl.compiled.operator;
+
+import org.apache.sling.scripting.sightly.impl.compiled.ExpressionTranslator;
+import org.apache.sling.scripting.sightly.impl.compiled.JavaSource;
+import org.apache.sling.scripting.sightly.impl.compiled.Type;
+import org.apache.sling.scripting.sightly.impl.compiler.expression.ExpressionNode;
+import org.apache.sling.scripting.sightly.impl.compiler.util.expression.SideEffectVisitor;
+
+/**
+ * Generator for logical operators
+ */
+public class EquivalenceOpGen implements BinaryOpGen {
+
+ private final boolean negated;
+
+ public EquivalenceOpGen(boolean negated) {
+ this.negated = negated;
+ }
+
+ @Override
+ public Type returnType(Type left, Type right) {
+ return Type.BOOLEAN;
+ }
+
+ @Override
+ public void generate(JavaSource source, ExpressionTranslator visitor, TypedNode left, TypedNode right) {
+ Type type = OpHelper.sameType(left, right);
+ if (type != null && OpHelper.isNumericType(type) || type == Type.BOOLEAN) {
+ generateEqualsOperator(source, visitor, left.getNode(), right.getNode());
+ } else if (type != Type.UNKNOWN) {
+ generateEqualsMethod(source, visitor, left, right);
+ } else {
+ generateCheckedEquals(source, visitor, left, right);
+ }
+ }
+
+ private void generateCheckedEquals(JavaSource source, SideEffectVisitor visitor, TypedNode leftNode, TypedNode rightNode) {
+ source.startExpression();
+ leftNode.getNode().accept(visitor);
+ source.equality().nullLiteral().conditional();
+ rightNode.getNode().accept(visitor);
+ source.equality().nullLiteral();
+ source.conditionalBranchSep();
+ generateEqualsMethod(source, visitor, leftNode, rightNode);
+ }
+
+ private void generateEqualsMethod(JavaSource source, SideEffectVisitor visitor, TypedNode leftNode, TypedNode rightNode) {
+ boolean performCast = leftNode.getType().isPrimitive();
+ if (performCast) {
+ source.startExpression();
+ source.cast(Type.UNKNOWN.getNativeClass());
+ }
+ leftNode.getNode().accept(visitor);
+ if (performCast) {
+ source.endExpression();
+ }
+ source.startCall("equals", true);
+ rightNode.getNode().accept(visitor);
+ source.endCall();
+ }
+
+ private void generateEqualsOperator(JavaSource source, SideEffectVisitor visitor, ExpressionNode leftNode, ExpressionNode rightNode) {
+ leftNode.accept(visitor);
+ source.append(operator());
+ rightNode.accept(visitor);
+ }
+
+ private String operator() {
+ return (negated) ? "!=" : "==";
+ }
+
+}
Propchange: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/EquivalenceOpGen.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/IsWhiteSpaceGen.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/IsWhiteSpaceGen.java?rev=1642281&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/IsWhiteSpaceGen.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/IsWhiteSpaceGen.java Fri Nov 28 10:18:01 2014
@@ -0,0 +1,51 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ******************************************************************************/
+
+package org.apache.sling.scripting.sightly.impl.compiled.operator;
+
+import org.apache.sling.scripting.sightly.impl.compiled.ExpressionTranslator;
+import org.apache.sling.scripting.sightly.impl.compiled.GenHelper;
+import org.apache.sling.scripting.sightly.impl.compiled.JavaSource;
+import org.apache.sling.scripting.sightly.impl.compiled.SourceGenConstants;
+import org.apache.sling.scripting.sightly.impl.compiled.Type;
+
+/**
+ * Generator for IS_WHITESPACE operator
+ */
+public final class IsWhiteSpaceGen implements UnaryOpGen {
+
+ public static final IsWhiteSpaceGen INSTANCE = new IsWhiteSpaceGen();
+
+ private IsWhiteSpaceGen() {
+ }
+
+ @Override
+ public Type returnType(Type operandType) {
+ return Type.BOOLEAN;
+ }
+
+ @Override
+ public void generate(JavaSource source, ExpressionTranslator visitor, TypedNode typedNode) {
+ GenHelper.typeCoercion(source, visitor, typedNode, Type.STRING);
+ source.startCall(SourceGenConstants.TRIM_METHOD, true)
+ .endCall()
+ .startCall(SourceGenConstants.STRING_EMPTY, true)
+ .endCall();
+ }
+}
Propchange: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/IsWhiteSpaceGen.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/LengthOpGen.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/LengthOpGen.java?rev=1642281&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/LengthOpGen.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/LengthOpGen.java Fri Nov 28 10:18:01 2014
@@ -0,0 +1,48 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ******************************************************************************/
+
+package org.apache.sling.scripting.sightly.impl.compiled.operator;
+
+import org.apache.sling.scripting.sightly.impl.compiled.ExpressionTranslator;
+import org.apache.sling.scripting.sightly.impl.compiled.GenHelper;
+import org.apache.sling.scripting.sightly.impl.compiled.JavaSource;
+import org.apache.sling.scripting.sightly.impl.compiled.SourceGenConstants;
+import org.apache.sling.scripting.sightly.impl.compiled.Type;
+
+/**
+ * Generator for the collection length operator
+ */
+public final class LengthOpGen implements UnaryOpGen {
+
+ public static final LengthOpGen INSTANCE = new LengthOpGen();
+
+ private LengthOpGen() {
+ }
+
+ @Override
+ public Type returnType(Type operandType) {
+ return Type.LONG;
+ }
+
+ @Override
+ public void generate(JavaSource source, ExpressionTranslator visitor, TypedNode typedNode) {
+ GenHelper.listCoercion(source, visitor, typedNode);
+ source.startCall(SourceGenConstants.COLLECTION_LENGTH_METHOD, true).endCall();
+ }
+}
Propchange: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/LengthOpGen.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/LogicalOpGen.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/LogicalOpGen.java?rev=1642281&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/LogicalOpGen.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/LogicalOpGen.java Fri Nov 28 10:18:01 2014
@@ -0,0 +1,80 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ******************************************************************************/
+
+package org.apache.sling.scripting.sightly.impl.compiled.operator;
+
+import org.apache.sling.scripting.sightly.impl.compiled.ExpressionTranslator;
+import org.apache.sling.scripting.sightly.impl.compiled.GenHelper;
+import org.apache.sling.scripting.sightly.impl.compiled.JavaSource;
+import org.apache.sling.scripting.sightly.impl.compiled.Type;
+import org.apache.sling.scripting.sightly.impl.compiler.expression.ExpressionNode;
+import org.apache.sling.scripting.sightly.impl.compiler.util.expression.SideEffectVisitor;
+
+/**
+ * Generator for logical operators
+ */
+public abstract class LogicalOpGen implements BinaryOpGen {
+
+ private final String javaOperator;
+
+ protected LogicalOpGen(String javaOperator) {
+ this.javaOperator = javaOperator;
+ }
+
+ public static final LogicalOpGen AND = new LogicalOpGen("&&") {
+ @Override
+ protected void generateGeneric(JavaSource source, SideEffectVisitor visitor, TypedNode left, TypedNode right) {
+ GenHelper.generateTernary(source, visitor, left, right, left);
+ }
+ };
+
+ public static final LogicalOpGen OR = new LogicalOpGen("||") {
+ @Override
+ protected void generateGeneric(JavaSource source, SideEffectVisitor visitor, TypedNode left, TypedNode right) {
+ GenHelper.generateTernary(source, visitor, left, left, right);
+ }
+ };
+
+ @Override
+ public Type returnType(Type leftType, Type rightType) {
+ if (leftType == rightType) {
+ return leftType;
+ }
+ return Type.UNKNOWN;
+ }
+
+ @Override
+ public void generate(JavaSource source, ExpressionTranslator visitor, TypedNode left, TypedNode right) {
+ if (OpHelper.sameType(left, right) == Type.BOOLEAN) {
+ generateWithOperator(source, visitor, left.getNode(), right.getNode());
+ } else {
+ generateGeneric(source, visitor, left, right);
+ }
+ }
+
+ protected abstract void generateGeneric(JavaSource source, SideEffectVisitor visitor,
+ TypedNode left, TypedNode right);
+
+ private void generateWithOperator(JavaSource source, SideEffectVisitor visitor,
+ ExpressionNode leftNode, ExpressionNode rightNode) {
+ leftNode.accept(visitor);
+ source.append(javaOperator);
+ rightNode.accept(visitor);
+ }
+}
Propchange: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/LogicalOpGen.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/LongOpGen.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/LongOpGen.java?rev=1642281&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/LongOpGen.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/LongOpGen.java Fri Nov 28 10:18:01 2014
@@ -0,0 +1,37 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ******************************************************************************/
+
+package org.apache.sling.scripting.sightly.impl.compiled.operator;
+
+import org.apache.sling.scripting.sightly.impl.compiled.Type;
+
+/**
+ * Generator for long numeric operators
+ */
+public class LongOpGen extends NumericOpGen {
+
+ public LongOpGen(String javaOperator) {
+ super(javaOperator);
+ }
+
+ @Override
+ protected Type commonType(Type leftType, Type rightType) {
+ return Type.LONG;
+ }
+}
Propchange: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/LongOpGen.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/NotOpGen.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/NotOpGen.java?rev=1642281&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/NotOpGen.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/NotOpGen.java Fri Nov 28 10:18:01 2014
@@ -0,0 +1,47 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ******************************************************************************/
+
+package org.apache.sling.scripting.sightly.impl.compiled.operator;
+
+import org.apache.sling.scripting.sightly.impl.compiled.ExpressionTranslator;
+import org.apache.sling.scripting.sightly.impl.compiled.GenHelper;
+import org.apache.sling.scripting.sightly.impl.compiled.JavaSource;
+import org.apache.sling.scripting.sightly.impl.compiled.Type;
+
+/**
+ * Generator for the not operator
+ */
+public final class NotOpGen implements UnaryOpGen {
+
+ public static final NotOpGen INSTANCE = new NotOpGen();
+
+ private NotOpGen() {
+ }
+
+ @Override
+ public Type returnType(Type operandType) {
+ return Type.BOOLEAN;
+ }
+
+ @Override
+ public void generate(JavaSource source, ExpressionTranslator visitor, TypedNode typedNode) {
+ source.negation();
+ GenHelper.typeCoercion(source, visitor, typedNode, Type.BOOLEAN);
+ }
+}
Propchange: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/NotOpGen.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/NumericOpGen.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/NumericOpGen.java?rev=1642281&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/NumericOpGen.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/NumericOpGen.java Fri Nov 28 10:18:01 2014
@@ -0,0 +1,57 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ******************************************************************************/
+
+package org.apache.sling.scripting.sightly.impl.compiled.operator;
+
+import org.apache.sling.scripting.sightly.impl.compiled.ExpressionTranslator;
+import org.apache.sling.scripting.sightly.impl.compiled.GenHelper;
+import org.apache.sling.scripting.sightly.impl.compiled.JavaSource;
+import org.apache.sling.scripting.sightly.impl.compiled.Type;
+
+/**
+ * Generator for numeric value
+ */
+public class NumericOpGen implements BinaryOpGen {
+
+ private final String javaOperator;
+
+ public NumericOpGen(String javaOperator) {
+ this.javaOperator = javaOperator;
+ }
+
+ @Override
+ public Type returnType(Type leftType, Type rightType) {
+ return commonType(leftType, rightType);
+ }
+
+ @Override
+ public void generate(JavaSource source, ExpressionTranslator visitor, TypedNode left, TypedNode right) {
+ Type commonType = commonType(left.getType(), right.getType());
+ GenHelper.typeCoercion(source, visitor, left, commonType);
+ source.append(javaOperator);
+ GenHelper.typeCoercion(source, visitor, right, commonType);
+ }
+
+ protected Type commonType(Type leftType, Type rightType) {
+ if (leftType == rightType && leftType == Type.LONG) {
+ return Type.LONG;
+ }
+ return Type.DOUBLE;
+ }
+}
Propchange: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/NumericOpGen.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/OpHelper.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/OpHelper.java?rev=1642281&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/OpHelper.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/OpHelper.java Fri Nov 28 10:18:01 2014
@@ -0,0 +1,39 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ******************************************************************************/
+
+package org.apache.sling.scripting.sightly.impl.compiled.operator;
+
+import org.apache.sling.scripting.sightly.impl.compiled.Type;
+
+/**
+ * Helper class for operator generation
+ */
+public class OpHelper {
+
+ public static Type sameType(TypedNode left, TypedNode right) {
+ if (left.getType().equals(right.getType())) {
+ return left.getType();
+ }
+ return null;
+ }
+
+ public static boolean isNumericType(Type type) {
+ return type == Type.LONG || type == Type.DOUBLE;
+ }
+}
Propchange: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/OpHelper.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/Operators.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/Operators.java?rev=1642281&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/Operators.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/Operators.java Fri Nov 28 10:18:01 2014
@@ -0,0 +1,88 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ******************************************************************************/
+
+package org.apache.sling.scripting.sightly.impl.compiled.operator;
+
+import java.util.EnumMap;
+import java.util.Map;
+
+import org.apache.sling.scripting.sightly.impl.compiler.expression.node.BinaryOperator;
+import org.apache.sling.scripting.sightly.impl.compiler.expression.node.UnaryOperator;
+
+/**
+ * Provides mappings from operators to generators
+ */
+public class Operators {
+ private static final Map<BinaryOperator, BinaryOpGen> representationMap =
+ new EnumMap<BinaryOperator, BinaryOpGen>(BinaryOperator.class);
+
+ private static final Map<UnaryOperator, UnaryOpGen> unaryMapping =
+ new EnumMap<UnaryOperator, UnaryOpGen>(UnaryOperator.class);
+
+ static {
+ representationMap.put(BinaryOperator.AND, LogicalOpGen.AND);
+ representationMap.put(BinaryOperator.OR, LogicalOpGen.OR);
+ representationMap.put(BinaryOperator.CONCATENATE, ConcatenateOpGen.INSTANCE);
+ representationMap.put(BinaryOperator.ADD, new NumericOpGen("+"));
+ representationMap.put(BinaryOperator.SUB, new NumericOpGen("-"));
+ representationMap.put(BinaryOperator.MUL, new NumericOpGen("*"));
+ representationMap.put(BinaryOperator.I_DIV, new LongOpGen("/"));
+ representationMap.put(BinaryOperator.REM, new LongOpGen("%"));
+ representationMap.put(BinaryOperator.DIV, new NumericOpGen("/"));
+ representationMap.put(BinaryOperator.EQ, new EquivalenceOpGen(false));
+ representationMap.put(BinaryOperator.NEQ, new EquivalenceOpGen(true));
+ representationMap.put(BinaryOperator.LT, new ComparisonOpGen(BinaryOperator.LT));
+ representationMap.put(BinaryOperator.LEQ, new ComparisonOpGen(BinaryOperator.LEQ));
+ representationMap.put(BinaryOperator.GT, new ComparisonOpGen(BinaryOperator.GT));
+ representationMap.put(BinaryOperator.GEQ, new ComparisonOpGen(BinaryOperator.GEQ));
+ representationMap.put(BinaryOperator.STRICT_EQ, new StrictEqGenOp(false));
+ representationMap.put(BinaryOperator.STRICT_NEQ, new StrictEqGenOp(true));
+
+ unaryMapping.put(UnaryOperator.LENGTH, LengthOpGen.INSTANCE);
+ unaryMapping.put(UnaryOperator.IS_WHITESPACE, IsWhiteSpaceGen.INSTANCE);
+ unaryMapping.put(UnaryOperator.NOT, NotOpGen.INSTANCE);
+ }
+
+
+ /**
+ * Provide the signature of the given operator
+ * @param operator - the operator
+ * @return - the signature for the operator
+ */
+ public static BinaryOpGen generatorFor(BinaryOperator operator) {
+ return provide(representationMap, operator);
+ }
+
+ /**
+ * Provide the signature of the given operator
+ * @param operator - the operator
+ * @return - the signature for the operator
+ */
+ public static UnaryOpGen generatorFor(UnaryOperator operator) {
+ return provide(unaryMapping, operator);
+ }
+
+ private static <K, V> V provide(Map<K, V> map, K key) {
+ V v = map.get(key);
+ if (v == null) {
+ throw new UnsupportedOperationException("Cannot find generator for operator: " + key);
+ }
+ return v;
+ }
+}
Propchange: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/Operators.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/StrictEqGenOp.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/StrictEqGenOp.java?rev=1642281&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/StrictEqGenOp.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/StrictEqGenOp.java Fri Nov 28 10:18:01 2014
@@ -0,0 +1,54 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ******************************************************************************/
+
+package org.apache.sling.scripting.sightly.impl.compiled.operator;
+
+import org.apache.sling.scripting.sightly.impl.compiled.ExpressionTranslator;
+import org.apache.sling.scripting.sightly.impl.compiled.JavaSource;
+import org.apache.sling.scripting.sightly.impl.compiled.Type;
+import org.apache.sling.scripting.sightly.impl.compiler.expression.node.BinaryOperator;
+
+/**
+ * Generator for strict equality
+ */
+public class StrictEqGenOp implements BinaryOpGen {
+
+ private final boolean negated;
+
+ public StrictEqGenOp(boolean negated) {
+ this.negated = negated;
+ }
+
+ @Override
+ public Type returnType(Type leftType, Type rightType) {
+ return Type.BOOLEAN;
+ }
+
+ @Override
+ public void generate(JavaSource source, ExpressionTranslator visitor, TypedNode left, TypedNode right) {
+ if (negated) {
+ source.negation();
+ }
+ source.startMethodCall(BinaryOperator.OBJECT_NAME, BinaryOperator.METHOD_STRICT_EQ);
+ left.getNode().accept(visitor);
+ source.separateArgument();
+ right.getNode().accept(visitor);
+ source.endCall();
+ }
+}
Propchange: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/StrictEqGenOp.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/TypedNode.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/TypedNode.java?rev=1642281&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/TypedNode.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/TypedNode.java Fri Nov 28 10:18:01 2014
@@ -0,0 +1,44 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ******************************************************************************/
+
+package org.apache.sling.scripting.sightly.impl.compiled.operator;
+
+import org.apache.sling.scripting.sightly.impl.compiled.Type;
+import org.apache.sling.scripting.sightly.impl.compiler.expression.ExpressionNode;
+
+/**
+ * Expression node with type information
+ */
+public class TypedNode {
+ private final ExpressionNode node;
+ private final Type type;
+
+ public TypedNode(ExpressionNode node, Type type) {
+ this.node = node;
+ this.type = type;
+ }
+
+ public ExpressionNode getNode() {
+ return node;
+ }
+
+ public Type getType() {
+ return type;
+ }
+}
Propchange: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/TypedNode.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/UnaryOpGen.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/UnaryOpGen.java?rev=1642281&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/UnaryOpGen.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/UnaryOpGen.java Fri Nov 28 10:18:01 2014
@@ -0,0 +1,35 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ******************************************************************************/
+
+package org.apache.sling.scripting.sightly.impl.compiled.operator;
+
+import org.apache.sling.scripting.sightly.impl.compiled.ExpressionTranslator;
+import org.apache.sling.scripting.sightly.impl.compiled.JavaSource;
+import org.apache.sling.scripting.sightly.impl.compiled.Type;
+
+/**
+ * Interface for generators of unary operators
+ */
+public interface UnaryOpGen {
+
+ Type returnType(Type operandType);
+
+ void generate(JavaSource source, ExpressionTranslator visitor, TypedNode typedNode);
+
+}
Propchange: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/operator/UnaryOpGen.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiler/CompilerBackend.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiler/CompilerBackend.java?rev=1642281&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiler/CompilerBackend.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiler/CompilerBackend.java Fri Nov 28 10:18:01 2014
@@ -0,0 +1,34 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ******************************************************************************/
+package org.apache.sling.scripting.sightly.impl.compiler;
+
+import org.apache.sling.scripting.sightly.impl.compiler.ris.CommandStream;
+
+/**
+ * A compiler backend
+ */
+public interface CompilerBackend {
+
+ /**
+ * Process a stream of commands
+ * @param stream - the stream of commands
+ */
+ void handle(CommandStream stream);
+
+}
Propchange: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiler/CompilerBackend.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiler/CompilerException.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiler/CompilerException.java?rev=1642281&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiler/CompilerException.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiler/CompilerException.java Fri Nov 28 10:18:01 2014
@@ -0,0 +1,38 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ******************************************************************************/
+package org.apache.sling.scripting.sightly.impl.compiler;
+
+public class CompilerException extends RuntimeException {
+
+ public CompilerException() {
+ super();
+ }
+
+ public CompilerException(String message) {
+ super(message);
+ }
+
+ public CompilerException(String message, Throwable throwable) {
+ super(message, throwable);
+ }
+
+ public CompilerException(Throwable throwable) {
+ super(throwable);
+ }
+}
Propchange: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiler/CompilerException.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiler/CompilerFrontend.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiler/CompilerFrontend.java?rev=1642281&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiler/CompilerFrontend.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiler/CompilerFrontend.java Fri Nov 28 10:18:01 2014
@@ -0,0 +1,35 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ******************************************************************************/
+package org.apache.sling.scripting.sightly.impl.compiler;
+
+import org.apache.sling.scripting.sightly.impl.compiler.util.stream.PushStream;
+
+/**
+ * Sightly compiler
+ */
+public interface CompilerFrontend {
+
+ /**
+ * Compile the source code to a stream of commands
+ * @param stream the output stream
+ * @param source the source code
+ */
+ void compile(PushStream stream, String source);
+
+}
Propchange: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiler/CompilerFrontend.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiler/SightlyCompilerService.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiler/SightlyCompilerService.java?rev=1642281&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiler/SightlyCompilerService.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiler/SightlyCompilerService.java Fri Nov 28 10:18:01 2014
@@ -0,0 +1,166 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ******************************************************************************/
+
+package org.apache.sling.scripting.sightly.impl.compiler;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.apache.felix.scr.annotations.ReferencePolicy;
+import org.apache.felix.scr.annotations.References;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.scripting.sightly.impl.compiler.debug.SanityChecker;
+import org.apache.sling.scripting.sightly.impl.compiler.ris.CommandStream;
+import org.apache.sling.scripting.sightly.impl.compiler.util.stream.PushStream;
+import org.apache.sling.scripting.sightly.impl.engine.runtime.RenderContextImpl;
+import org.apache.sling.scripting.sightly.impl.filter.Filter;
+import org.apache.sling.scripting.sightly.impl.html.dom.HtmlParserService;
+import org.apache.sling.scripting.sightly.impl.plugin.Plugin;
+import org.apache.sling.scripting.sightly.impl.compiler.frontend.SimpleFrontend;
+import org.apache.sling.scripting.sightly.impl.compiler.optimization.CoalescingWrites;
+import org.apache.sling.scripting.sightly.impl.compiler.optimization.DeadCodeRemoval;
+import org.apache.sling.scripting.sightly.impl.compiler.optimization.SequenceStreamTransformer;
+import org.apache.sling.scripting.sightly.impl.compiler.optimization.StreamTransformer;
+import org.apache.sling.scripting.sightly.impl.compiler.optimization.SyntheticMapRemoval;
+import org.apache.sling.scripting.sightly.impl.compiler.optimization.UnusedVariableRemoval;
+import org.apache.sling.scripting.sightly.impl.compiler.optimization.reduce.ConstantFolding;
+import org.osgi.service.component.ComponentContext;
+
+/**
+ * Implementation for the Sightly compiler
+ */
+@Component
+@Service(SightlyCompilerService.class)
+@References({
+ @Reference(
+ policy = ReferencePolicy.DYNAMIC,
+ referenceInterface = Filter.class,
+ name = "filterService",
+ cardinality = ReferenceCardinality.OPTIONAL_MULTIPLE
+ ),
+ @Reference(
+ policy = ReferencePolicy.DYNAMIC,
+ referenceInterface = Plugin.class,
+ name = "pluginService",
+ cardinality = ReferenceCardinality.OPTIONAL_MULTIPLE
+ )
+})
+public class SightlyCompilerService {
+
+ private List<Filter> filters = new ArrayList<Filter>();
+ private List<Plugin> plugins = new ArrayList<Plugin>();
+
+ private volatile StreamTransformer optimizer;
+ private volatile CompilerFrontend frontend;
+ private volatile boolean initialised = false;
+
+ @Reference
+ protected HtmlParserService htmlParserService;
+
+ /**
+ * Compile the given markup source and feed it to the given backend
+ * @param source the HTML source code
+ * @param backend the backend that will process the command stream from the source
+ */
+ public void compile(String source, CompilerBackend backend, RenderContextImpl renderContext) {
+ initIfNeeded(renderContext);
+ PushStream stream = new PushStream();
+ SanityChecker.attachChecker(stream);
+ CommandStream optimizedStream = optimizer.transform(stream);
+ //optimizedStream.addHandler(LoggingHandler.INSTANCE);
+ backend.handle(optimizedStream);
+ frontend.compile(stream, source);
+ }
+
+ private void initIfNeeded(RenderContextImpl renderContext) {
+ if (!initialised) {
+ synchronized (this) {
+ if (!initialised) {
+ ArrayList<StreamTransformer> transformers = new ArrayList<StreamTransformer>();
+ transformers.add(ConstantFolding.transformer(renderContext));
+ transformers.add(DeadCodeRemoval.transformer(renderContext));
+ transformers.add(SyntheticMapRemoval.TRANSFORMER);
+ transformers.add(UnusedVariableRemoval.TRANSFORMER);
+ transformers.add(CoalescingWrites.TRANSFORMER);
+ optimizer = new SequenceStreamTransformer(transformers);
+ initialised = true;
+ }
+ }
+ }
+ }
+
+ @Activate
+ protected void activate(ComponentContext context) {
+ initialised = false;
+ reloadFrontend();
+ }
+
+ @SuppressWarnings("UnusedDeclaration")
+ protected void bindFilterService(Filter filter, Map<String, Object> properties) {
+ synchronized(filters) {
+ filters = add(filters, filter);
+ reloadFrontend();
+ }
+ }
+
+ @SuppressWarnings("UnusedDeclaration")
+ protected void unbindFilterService(Filter filter, Map<String, Object> properties) {
+ synchronized (filters) {
+ filters = remove(filters, filter);
+ reloadFrontend();
+ }
+ }
+
+ @SuppressWarnings("UnusedDeclaration")
+ protected void bindPluginService(Plugin plugin, Map<String, Object> properties) {
+ synchronized (plugins) {
+ plugins = add(plugins, plugin);
+ reloadFrontend();
+ }
+ }
+
+ @SuppressWarnings("UnusedDeclaration")
+ protected void unbindPluginService(Plugin plugin, Map<String, Object> properties) {
+ synchronized (plugins) {
+ plugins = remove(plugins, plugin);
+ reloadFrontend();
+ }
+ }
+
+ private void reloadFrontend() {
+ frontend = new SimpleFrontend(htmlParserService, plugins, filters);
+ }
+
+ private static <T> List<T> add(List<T> list, T item) {
+ ArrayList<T> result = new ArrayList<T>(list);
+ result.add(item);
+ return result;
+ }
+
+ private static <T> List<T> remove(List<T> list, T item) {
+ ArrayList<T> result = new ArrayList<T>(list);
+ result.remove(item);
+ return result;
+ }
+}
Propchange: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiler/SightlyCompilerService.java
------------------------------------------------------------------------------
svn:mime-type = text/plain