You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ode.apache.org by mr...@apache.org on 2007/12/07 21:13:06 UTC
svn commit: r602202 - in /ode/sandbox/simpel/src:
main/antlr/org/apache/ode/simpel/antlr/ main/java/org/apache/ode/simpel/
main/java/org/apache/ode/simpel/util/ test/resources/
Author: mriou
Date: Fri Dec 7 12:13:05 2007
New Revision: 602202
URL: http://svn.apache.org/viewvc?rev=602202&view=rev
Log:
Correlation with more test cases, found a way to deal with Javascript functions as pseudo island grammars.
Added:
ode/sandbox/simpel/src/main/java/org/apache/ode/simpel/util/JSHelper.java
Modified:
ode/sandbox/simpel/src/main/antlr/org/apache/ode/simpel/antlr/SimPEL.g
ode/sandbox/simpel/src/main/antlr/org/apache/ode/simpel/antlr/SimPELWalker.g
ode/sandbox/simpel/src/main/java/org/apache/ode/simpel/SimPELCompiler.java
ode/sandbox/simpel/src/test/resources/compile-tests-ok.simpel
Modified: ode/sandbox/simpel/src/main/antlr/org/apache/ode/simpel/antlr/SimPEL.g
URL: http://svn.apache.org/viewvc/ode/sandbox/simpel/src/main/antlr/org/apache/ode/simpel/antlr/SimPEL.g?rev=602202&r1=602201&r2=602202&view=diff
==============================================================================
--- ode/sandbox/simpel/src/main/antlr/org/apache/ode/simpel/antlr/SimPEL.g (original)
+++ ode/sandbox/simpel/src/main/antlr/org/apache/ode/simpel/antlr/SimPEL.g Fri Dec 7 12:13:05 2007
@@ -8,7 +8,7 @@
tokens {
PROCESS; PICK; SEQUENCE; FLOW; IF; ELSEIF; ELSE; WHILE; UNTIL; FOREACH; FORALL; INVOKE;
RECEIVE; REPLY; ASSIGN; THROW; WAIT; EXIT; TIMEOUT; TRY; CATCH; CATCH_ALL; SCOPE; EVENT;
- ALARM; COMPENSATION; COMPENSATE;
+ ALARM; COMPENSATION; COMPENSATE; CORRELATION; CORR_MAP;
EXPR; EXT_EXPR; XML_LITERAL;
}
@parser::header {
@@ -18,6 +18,7 @@
import uk.co.badgersinfoil.e4x.antlr.LinkedListTree;
import uk.co.badgersinfoil.e4x.E4XHelper;
import org.apache.ode.simpel.ErrorListener;
+import org.apache.ode.simpel.util.JSHelper;
}
@lexer::header {
package org.apache.ode.simpel.antlr;
@@ -54,6 +55,10 @@
private LinkedListTree parseXMLLiteral() throws RecognitionException {
return E4XHelper.parseXMLLiteral(lexer, cs, (LinkedListTokenStream)input);
}
+ /** Handle 'island grammar' for embeded JavaScript-literal elements. */
+ private LinkedListTree parseJSLiteral() throws RecognitionException {
+ return JSHelper.parseJSLiteral(lexer, cs, (LinkedListTokenStream)input);
+ }
public void displayRecognitionError(String[] tokenNames, RecognitionException e) {
el.reportRecognitionError(tokenNames, e.line, getErrorMessage(e, tokenNames), e);
@@ -92,7 +97,7 @@
until_ex: 'do' block 'until' '(' expr ')' -> ^(UNTIL expr block);
foreach : 'for' '(' ID '=' init=expr ';' cond=expr ';' assign ')' block -> ^(FOREACH ID $init $cond assign block);
-forall : 'forall' '(' ID '=' from=expr '..' to=expr ')' block -> ^(FORALL ID $from $to block);
+forall : 'forall' '(' ID '=' from=expr 'to' to=expr ')' block -> ^(FORALL ID $from $to block);
try_ex : 'try' tb=block catch_ex* ('catch' '(' ID ')' cb=block)? -> ^(TRY $tb catch_ex* ^(CATCH_ALL ID $cb)?);
@@ -110,7 +115,7 @@
// Simple activities
invoke : 'invoke' '(' p=ID ',' o=ID (',' in=ID)? ')' -> ^(INVOKE $p $o $in?);
-receive : 'receive' '(' p=ID ',' o=ID (',' m=ID)? ')' block? -> ^(RECEIVE $p $o $m? block?);
+receive : 'receive' '(' p=ID ',' o=ID (',' m=ID)? (',' correlation)? ')' block? -> ^(RECEIVE $p $o $m? correlation? block?);
reply : 'reply' '(' ID ')' -> ^(REPLY ID);
@@ -127,8 +132,15 @@
exit : 'exit' -> ^(EXIT);
+
+// Others
// TODO This will not work for any function whose code contains braces
-funct : 'function'^ f=ID '(' ID? (','! ID)* ')' '{'! (options {greedy=false;} : .)* '}'!;
+correlation
+ : '{' corr_mapping (',' corr_mapping)* '}' -> ^(CORRELATION corr_mapping*);
+corr_mapping
+ : f1=ID ':' f2=ID '(' v=ID ')' -> ^(CORR_MAP $f1 $f2 $v);
+
+funct : 'function'^ f=ID '(' ID? (','! ID)* ')' js_block;
// Expressions
expr : s_expr | EXT_EXPR;
@@ -144,14 +156,16 @@
// In-line XML
xml_literal
-@init {
- LinkedListTree xml = null;
-}
+@init { LinkedListTree xml = null; }
: // We have to have the LT in the outer grammar for lookahead
// in AS3Parser to be able to predict that the xmlLiteral rule
// should be used.
'<' { xml=parseXMLLiteral(); } -> { xml };
+js_block
+@init { LinkedListTree js = null; }
+ : '{' { js=parseJSLiteral(); } -> { js };
+
EXT_EXPR
: '[' (options {greedy=false;} : .)* ']';
@@ -166,8 +180,6 @@
: ('#'|'//') .* CR { $channel = HIDDEN; };
CR : ('\r' | '\n' )+ { $channel = HIDDEN; };
WS : ( ' ' | '\t' )+ { skip(); };
-fragment NAMECHAR
- : LETTER | DIGIT | '.' | '-' | '_' | ':';
fragment DIGIT
: '0'..'9';
fragment LETTER
Modified: ode/sandbox/simpel/src/main/antlr/org/apache/ode/simpel/antlr/SimPELWalker.g
URL: http://svn.apache.org/viewvc/ode/sandbox/simpel/src/main/antlr/org/apache/ode/simpel/antlr/SimPELWalker.g?rev=602202&r1=602201&r2=602202&view=diff
==============================================================================
--- ode/sandbox/simpel/src/main/antlr/org/apache/ode/simpel/antlr/SimPELWalker.g (original)
+++ ode/sandbox/simpel/src/main/antlr/org/apache/ode/simpel/antlr/SimPELWalker.g Fri Dec 7 12:13:05 2007
@@ -73,7 +73,7 @@
// Simple activities
invoke : ^(INVOKE p=ID o=ID in=ID?);
-receive : ^(RECEIVE p=ID o=ID m=ID? block?);
+receive : ^(RECEIVE p=ID o=ID m=ID? correlation? block?);
reply : ^(REPLY ID);
@@ -86,6 +86,12 @@
wait_ex : ^(WAIT expr);
exit : EXIT;
+
+// Other
+correlation
+ : ^(CORRELATION corr_mapping*);
+corr_mapping
+ : ^(CORR_MAP ID ID ID);
function: ^(FUNCTION ID);
Modified: ode/sandbox/simpel/src/main/java/org/apache/ode/simpel/SimPELCompiler.java
URL: http://svn.apache.org/viewvc/ode/sandbox/simpel/src/main/java/org/apache/ode/simpel/SimPELCompiler.java?rev=602202&r1=602201&r2=602202&view=diff
==============================================================================
--- ode/sandbox/simpel/src/main/java/org/apache/ode/simpel/SimPELCompiler.java (original)
+++ ode/sandbox/simpel/src/main/java/org/apache/ode/simpel/SimPELCompiler.java Fri Dec 7 12:13:05 2007
@@ -55,7 +55,7 @@
walker.setErrorListener(el);
HashMap<Integer, Integer> tokenMapping = buildTokenMap(E4XParser.tokenNames, E4XLexer.class, SimPELWalker.class);
rewriteTokens(tokenMapping, E4XParser.tokenNames, (LinkedListTree) t, walker, false);
- // System.out.println("\n"+t.toStringTree()); // print out the tree
+// System.out.println("\n"+t.toStringTree()); // print out the tree
nodes.setTokenStream(tokenStream);
walker.program();
Added: ode/sandbox/simpel/src/main/java/org/apache/ode/simpel/util/JSHelper.java
URL: http://svn.apache.org/viewvc/ode/sandbox/simpel/src/main/java/org/apache/ode/simpel/util/JSHelper.java?rev=602202&view=auto
==============================================================================
--- ode/sandbox/simpel/src/main/java/org/apache/ode/simpel/util/JSHelper.java (added)
+++ ode/sandbox/simpel/src/main/java/org/apache/ode/simpel/util/JSHelper.java Fri Dec 7 12:13:05 2007
@@ -0,0 +1,48 @@
+package org.apache.ode.simpel.util;
+
+import org.antlr.runtime.CharStream;
+import org.antlr.runtime.RecognitionException;
+import org.antlr.runtime.TokenSource;
+import org.apache.ode.simpel.antlr.SimPELLexer;
+import uk.co.badgersinfoil.e4x.antlr.LinkedListTokenStream;
+import uk.co.badgersinfoil.e4x.antlr.LinkedListTree;
+import uk.co.badgersinfoil.e4x.antlr.LinkedListToken;
+import uk.co.badgersinfoil.e4x.antlr.LinkedListTokenSource;
+
+/**
+ * @author Matthieu Riou <mr...@apache.org>
+ */
+public class JSHelper {
+
+ public static LinkedListTree parseJSLiteral(TokenSource lexer, CharStream cs, LinkedListTokenStream stream)
+ throws RecognitionException {
+ String tail = cs.substring(cs.index(), cs.size()-1);
+ int closingIndex = findClosingBracket(tail);
+ // Skipping
+ cs.seek(cs.index() + closingIndex);
+ LinkedListTokenSource source = (LinkedListTokenSource)stream.getTokenSource();
+ stream.setTokenSource(source); // cause any remembered E4X state to be dropped
+ stream.scrub(1); // erase the subsequent token that the E4X parser got from this stream
+ source.setDelegate(lexer);
+
+ LinkedListToken current = (LinkedListToken)stream.get(stream.size());
+ LinkedListToken tok = new LinkedListToken(SimPELLexer.T80, tail.substring(0, closingIndex));
+ current.setNext(tok);
+ tok.setPrev(current);
+ return new LinkedListTree(tok);
+ }
+
+ private static int findClosingBracket(String tail) {
+ int count = 1;
+ int pos = 0;
+ char[] chars = tail.toCharArray();
+ for (; pos < chars.length; pos++) {
+ char ch = chars[pos];
+ if (ch == '}') count--;
+ if (ch == '{') count++;
+ if (count == 0) break;
+ }
+ return pos;
+ }
+
+}
Modified: ode/sandbox/simpel/src/test/resources/compile-tests-ok.simpel
URL: http://svn.apache.org/viewvc/ode/sandbox/simpel/src/test/resources/compile-tests-ok.simpel?rev=602202&r1=602201&r2=602202&view=diff
==============================================================================
--- ode/sandbox/simpel/src/test/resources/compile-tests-ok.simpel (original)
+++ ode/sandbox/simpel/src/test/resources/compile-tests-ok.simpel Fri Dec 7 12:13:05 2007
@@ -120,3 +120,47 @@
reply(resp)
}
}
+
+#=============================================================
+# Simple correlation
+#
+
+function orderId(orderMsg) {
+ return msg.order.orderId;
+}
+
+process SimpleCorrel {
+ orderMsg = receive(my_pl, start_op)
+
+ # The correlation implies orderId(incoming_msg) == orderId(init_msg)
+ receive(my_pl, corr_op, {orderId: orderId(orderMsg)})
+}
+
+#=============================================================
+# Two "properties", two message types
+#
+
+function orderIdFromOrder(orderMsg) {
+ return orderMsg.order.orderId;
+}
+function shipIdFromOrder(shipMsg) {
+ return shipMsg.order.shippmentId;
+}
+function orderIdFromShippment(orderMsg) {
+ return orderMsg.shippment.orderId;
+}
+function shipIdFromShippment(shipMsg) {
+ return shipMsg.shippment.shippmentId;
+}
+
+process DoubleCorrel {
+ orderMsg = receive(my_pl, start_op)
+
+ # The correlation implies orderIdFromShippment(incoming_msg) == orderIdFromOrder(order_msg)
+ # and shipIdFromShippment(incoming_msg) == shipIdFromOrder(order_msg),
+ receive(my_pl, corr_op, {
+ orderIdFromShippment: orderIdFromOrder(order_msg),
+ shipIdFromShippment: shipIdFromOrder(order_msg)
+ })
+}
+