Skip to content
Browse files

added typerefs and existential predicate suffix in arrays

  • Loading branch information...
1 parent 25dbf39 commit baea25e7f5fdf3a6879edac223fa9bac1d5c4ed4 @michelegonella committed Nov 9, 2012
View
12 zen-dataobject/src/main/antlr3/com/nominanuda/dataobject/schema/Jcl.g
@@ -2,13 +2,13 @@ grammar Jcl;
options {
output = AST;
-//backtrack=true;
+backtrack=true;
//k=10;
}
tokens {
-OBJECT;ARRAY;NAME;ENTRY;PRIMITIVE;VALUECHOICE;VALUESEQ;ENTRYSEQ;TYPEDEF;TYPEREF;EXISTENTIAL;
+OBJECT;ARRAY;NAME;ENTRY;PRIMITIVE;VALUECHOICE;VALUESEQ;ENTRYSEQ;TYPEDEF;TYPEREF;EXISTENTIAL;ARRAYVAL;
}
@header {
@@ -77,7 +77,13 @@ array : '[' elements? ']'
-> ^(ARRAY elements?)
;
-elements : valueseq | (value (','! value)* (','! valueseq)?)
+existentialValue : value ExistentialTkn
+ -> ^(ARRAYVAL value ^(EXISTENTIAL ExistentialTkn))
+ | value
+ -> ^(ARRAYVAL value EXISTENTIAL)
+ ;
+
+elements : valueseq | (existentialValue (','! existentialValue)* (','! valueseq)?)
//elements : value (','! value)*
;
View
24 zen-dataobject/src/main/java/com/nominanuda/dataobject/schema/JclValidatorFactory.java
@@ -16,8 +16,6 @@
package com.nominanuda.dataobject.schema;
import java.io.StringReader;
-import java.nio.channels.IllegalSelectorException;
-import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
@@ -32,7 +30,6 @@
import org.antlr.runtime.tree.Tree;
import com.nominanuda.code.Nullable;
-import com.nominanuda.dataobject.JsonContentHandler;
import com.nominanuda.dataobject.WrappingRecognitionException;
import com.nominanuda.dataobject.transform.BaseJsonTransformer;
import com.nominanuda.dataobject.transform.JsonTransformer;
@@ -166,10 +163,19 @@ private EventConsumer makeConsumer(Tree node, Stack<EventConsumer> stack) {
return new ArrayConsumer(stack, elementsConsumers);
case PRIMITIVE:
return new PrimitiveConsumer(stack, makePrimitiveValidator(node));
+ case ARRAYVAL:
+ Tree primitive = node.getChild(0);
+ Tree existential = node.getChild(1);
+ return new PrimitiveConsumer(stack, makePrimitiveValidator(primitive), tokenToPredicate(existential));
+ case TYPEREF:
+ String tname = node.getChild(0).getText();
+ Tree typedef = Check.illegalargument.assertNotNull(
+ tMap.get(tname), "type not found "+tname);
+ return makeConsumer(typedef, stack);
default:
return Check.illegalstate.fail();
}
- }
+ }
private ExistentialPredicate tokenToPredicate(Tree predTnkNode) {
String p = predTnkNode.getChildCount() > 0 ? predTnkNode.getChild(0).getText() : null;
@@ -178,10 +184,10 @@ private ExistentialPredicate tokenToPredicate(Tree predTnkNode) {
private Fun1<Object, String> makePrimitiveValidator(Tree node) {
Tree t = node.getChild(0);
- String typeDef = t == null
+ String valDef = t == null
? DefaultPrimitiveValidatorFactory.ANYPRIMITIVE
: t.getText();
- return primitiveValidatorFactory.create(typeDef);
+ return primitiveValidatorFactory.create(valDef);
}
////////////////////////////////////////
public class ObjectConsumer extends EventConsumer {
@@ -411,12 +417,18 @@ public PrimitiveConsumer(Stack<EventConsumer> stack, Fun1<Object, String> valida
this.validator = validator;
}
+ public PrimitiveConsumer(Stack<EventConsumer> stack, Fun1<Object, String> validator, ExistentialPredicate pred) {
+ super(stack, pred);
+ this.validator = validator;
+ }
+
@Override
public boolean primitive(Object value) throws RuntimeException {
if(value == null) {
if(! isNullable()) {
throw new ValidationException("null value of not-nul property");
} else {
+ pop();
return true;
}
} else {
View
13 zen-dataobject/src/test/java/com/nominanuda/dataobject/schema/JclValidatorTest.java
@@ -17,24 +17,14 @@
import static org.junit.Assert.*;
-import java.io.StringReader;
-
-import org.junit.Ignore;
import org.junit.Test;
-import com.nominanuda.dataobject.DataObjectImpl;
import com.nominanuda.dataobject.DataStruct;
import com.nominanuda.dataobject.DataStructHelper;
import com.nominanuda.dataobject.transform.JsonPipeline;
-import com.nominanuda.dataobject.transform.JsonTransformer;
-import com.nominanuda.dataobject.transform.StringValuesJsonTransformer;
-import com.nominanuda.lang.Check;
-import com.nominanuda.lang.InstanceFactory;
-//@Ignore
public class JclValidatorTest {
-
@Test
public void testValid() throws Exception {
valid("{}", "{}");
@@ -59,6 +49,9 @@ public void testValid() throws Exception {
valid("[b,b,*]", "[true,true,true,1]");
valid("MyType@[b] MyType2@[b]", "[true]");
valid("MyType@[b] MyType2@[b]", "MyType2", "[true]");
+ valid("MyType1@{x:@MyType2} MyType2@[b!]", "{x:[true]}");
+ valid("MyType1@{x:@MyType2} MyType2@[b]", "{x:[null]}");
+ invalid("MyType1@{x:@MyType2} MyType2@[b!]", "{x:[null]}");
}
private void invalid(String schema, String instance) throws Exception {

0 comments on commit baea25e

Please sign in to comment.
Something went wrong with that request. Please try again.