[jboss-svn-commits] JBL Code SVN: r20068 - labs/jbossrules/branches/mattgeis/drools-compiler/src/main/resources/org/drools/lang/dsl.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Mon May 19 16:56:16 EDT 2008


Author: mattgeis
Date: 2008-05-19 16:56:16 -0400 (Mon, 19 May 2008)
New Revision: 20068

Added:
   labs/jbossrules/branches/mattgeis/drools-compiler/src/main/resources/org/drools/lang/dsl/DSLMapWalker.g
Modified:
   labs/jbossrules/branches/mattgeis/drools-compiler/src/main/resources/org/drools/lang/dsl/DSLMap.g
Log:
Adding new ANTLR grammars for DSL parsing

Modified: labs/jbossrules/branches/mattgeis/drools-compiler/src/main/resources/org/drools/lang/dsl/DSLMap.g
===================================================================
--- labs/jbossrules/branches/mattgeis/drools-compiler/src/main/resources/org/drools/lang/dsl/DSLMap.g	2008-05-19 20:48:40 UTC (rev 20067)
+++ labs/jbossrules/branches/mattgeis/drools-compiler/src/main/resources/org/drools/lang/dsl/DSLMap.g	2008-05-19 20:56:16 UTC (rev 20068)
@@ -53,8 +53,25 @@
 		return validateLT(1, text);
 	}
 	
+	protected void mismatch(IntStream input, int ttype, BitSet follow)
+		throws RecognitionException{
+		throw new MismatchedTokenException(ttype, input);
+	}
+
+	public void recoverFromMismatchedSet(IntStream input,
+		RecognitionException e, BitSet follow) throws RecognitionException{
+		throw e;
+	}
+	
 }
 
+
+ at rulecatch {
+	catch (RecognitionException e) {
+		throw e;
+	}
+}
+
 // PARSER RULES
 mapping_file
 	: statement* 
@@ -62,10 +79,12 @@
 	;
 
 statement
-	: entry
-	| comment
-	| EOL!	
-	;//bang at end of EOL means to not put it into the AST
+	: entry 	
+	| comment 
+	| EOL! 
+	;
+	//! after EOL means to not put it into the AST
+	
 
 comment	: LINE_COMMENT 
 	-> ^(VT_COMMENT[$LINE_COMMENT, "COMMENT"] LINE_COMMENT )
@@ -101,14 +120,12 @@
 	-> ^(VT_ENTRY_KEY key_sentence+ )
 	;
  
-key_sentence //problem: if you have foo is {foo:\d{3}}, because the WS is hidden, it rebuilds back to 
-//		foo is\d{3}  when the key pattern is created.  Somehow we need to capture the 
-//trailing WS in the key_chunk
+key_sentence 
 @init {
         String text = "";
 }	
-	: variable_definition 
-	| cb=key_chunk { text = $cb.text; }
+	: variable_definition
+	| cb=key_chunk { text = $cb.text;}
 	-> VT_LITERAL[$cb.start, text]
 	;		
 
@@ -135,9 +152,11 @@
 	;	
 	
 literal 
-	: ( LITERAL | LEFT_SQUARE | RIGHT_SQUARE | COLON)
+	//: ( LITERAL | LEFT_SQUARE | RIGHT_SQUARE | COLON) //trying to remove [] from literals
+	: ( LITERAL | COLON)
 	;	
-	
+
+
 variable_definition
 @init {
         String text = "";
@@ -150,6 +169,15 @@
 	-> {hasSpace}?	VT_SPACE ^(VT_VAR_DEF $name ) //do we need to build a VT_LITERAL token for $name?
 	-> ^(VT_VAR_DEF $name ) //do we need to build a VT_LITERAL token for $name?
 	;
+	
+variable_definition2
+ at init {
+        String text = "";
+}	
+	: LEFT_CURLY name=LITERAL ( COLON pat=pattern {text = $pat.text;} )? RIGHT_CURLY
+	-> {!"".equals(text)}? ^(VT_VAR_DEF $name VT_PATTERN[$pat.start, text] ) //pat can be null if there's no pattern here
+	->	^(VT_VAR_DEF $name ) //do we need to build a VT_LITERAL token for $name?
+	;
 
 
 pattern 
@@ -160,10 +188,18 @@
 	;	
 	
 
-	
+variable_reference
+ at init {
+        boolean hasSpace = false;
+}	
+	: lc=LEFT_CURLY { if( ((CommonToken)input.LT(-2)).getStopIndex() < ((CommonToken)lc).getStartIndex() -1 ) hasSpace = true; } 
+	name=LITERAL RIGHT_CURLY
+	-> {hasSpace}? VT_SPACE ^(VT_VAR_REF $name )
+	->  ^(VT_VAR_REF $name )
+	;
 
 	
-variable_reference 
+variable_reference2 
 	: LEFT_CURLY name=LITERAL RIGHT_CURLY
 	-> ^(VT_VAR_REF $name )
 	;	
@@ -249,8 +285,11 @@
 LINE_COMMENT	
 	:	POUND ( options{greedy=false;} : .)* EOL /* ('\r')? '\n'  */
 	;
-	
 
+//META_LITERAL
+//	: ('a'..'z'|'A'..'Z'|'_'|'0'..'9'|'*'|DOT)+
+//	;
+
 LITERAL	
 	:	('a'..'z'|'A'..'Z'|'_'|'0'..'9'|'\u00c0'..'\u00ff'|MISC|EscapeSequence|DOT)+
 	;

Added: labs/jbossrules/branches/mattgeis/drools-compiler/src/main/resources/org/drools/lang/dsl/DSLMapWalker.g
===================================================================
--- labs/jbossrules/branches/mattgeis/drools-compiler/src/main/resources/org/drools/lang/dsl/DSLMapWalker.g	                        (rev 0)
+++ labs/jbossrules/branches/mattgeis/drools-compiler/src/main/resources/org/drools/lang/dsl/DSLMapWalker.g	2008-05-19 20:56:16 UTC (rev 20068)
@@ -0,0 +1,160 @@
+tree grammar DSLMapWalker;
+
+options {
+	tokenVocab=DSLMap;
+	ASTLabelType=CommonTree;
+}
+
+ at treeparser::header {
+	package org.drools.lang.dsl;
+	
+	import java.util.Map;
+	import java.util.HashMap;
+	
+}
+
+
+mapping_file returns [DSLMapping mapping]
+scope {
+	DSLMapping retval;
+}
+ at init {
+	$mapping_file::retval = new DefaultDSLMapping() ;
+}
+	: ^(VT_DSL_GRAMMAR entry*)
+	{
+		$mapping = $mapping_file::retval;
+	}
+	;
+	
+mapping_entry 
+	: ent=entry
+	{
+		$mapping_file::retval.addEntry(ent);
+	}
+	;
+	
+valid_entry returns [DSLMappingEntry mappingEntry]
+	: ent=entry {$mappingEntry = ent;}
+	| VT_COMMENT {$mappingEntry = null;}
+	;
+
+
+entry returns [DSLMappingEntry mappingEntry]
+scope {
+	Map variables;
+	DefaultDSLMappingEntry retval;
+	int counter;
+	StringBuffer keybuffer;
+	StringBuffer valuebuffer;
+}
+ at init {
+	$entry::retval = new DefaultDSLMappingEntry() ;
+	$entry::variables = new HashMap();
+	$entry::keybuffer = new StringBuffer();
+	$entry::valuebuffer = new StringBuffer();
+}
+	: ^(VT_ENTRY scope_section meta_section key_section value_section)
+	{
+		$mappingEntry = $entry::retval;
+	}
+	;
+
+
+scope_section 
+	: ^(thescope=VT_SCOPE condition_key? consequence_key? keyword_key? any_key?)
+	;
+
+
+	
+meta_section
+	: ^(VT_META metalit=LITERAL?)
+	{
+		if ( $metalit == null || $metalit.text == null || $metalit.text.length() == 0 ) {
+			$entry::retval.setMetaData(DSLMappingEntry.EMPTY_METADATA);
+		} else {
+        		$entry::retval.setMetaData(new DSLMappingEntry.DefaultDSLEntryMetaData( $metalit.text ));
+	        }
+	}
+	;
+
+key_section
+	: ^(VT_ENTRY_KEY key_sentence+ )
+	{
+		$entry::retval.setMappingKey($entry::keybuffer.toString());
+	}
+	;
+ 
+key_sentence
+	: variable_definition
+	| vtl=VT_LITERAL 
+	{
+		$entry::keybuffer.append($vtl.text);
+	}
+	| VT_SPACE
+	{
+		$entry::keybuffer.append("\\s+");
+	}
+	;		
+
+value_section
+	: ^(VT_ENTRY_VAL value_sentence+ )
+	{
+		$entry::retval.setMappingValue($entry::valuebuffer.toString());
+	}
+	;
+	
+value_sentence 	
+	: variable_reference
+	| vtl=VT_LITERAL
+	{
+		$entry::valuebuffer.append($vtl.text);
+	}
+	| VT_SPACE
+	{
+		$entry::valuebuffer.append("\\s+");
+	}
+	;	
+
+literal 
+	: theliteral=VT_LITERAL
+	;	
+
+
+variable_definition
+
+	:   ^(VT_VAR_DEF varname=LITERAL pattern=VT_PATTERN? )
+	{
+		$entry::counter++;
+		$entry::variables.put($varname.text, new Integer($entry::counter));
+		$entry::keybuffer.append($pattern != null? "(" + $pattern.text + ")" : "(.*?)");
+	}
+	;
+
+
+variable_reference 
+	: ^(varref=VT_VAR_REF lit=LITERAL ) 
+	{
+		$entry::valuebuffer.append("\\$" + $entry::variables.get($lit.text));
+	}
+	;	
+
+condition_key
+	: VT_CONDITION
+	{$entry::retval.setSection(DSLMappingEntry.CONDITION);}
+	;
+
+consequence_key 
+	: VT_CONSEQUENCE
+	{$entry::retval.setSection(DSLMappingEntry.CONSEQUENCE);}
+	;
+
+keyword_key 
+	: VT_KEYWORD
+	{$entry::retval.setSection(DSLMappingEntry.KEYWORD);}
+	;
+
+any_key 
+	: VT_ANY
+	{$entry::retval.setSection(DSLMappingEntry.ANY);}
+	;




More information about the jboss-svn-commits mailing list