[seam-commits] Seam SVN: r13315 - in sandbox/encore: src/main/antlr3/org/jboss/seam/encore/grammar and 15 other directories.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Mon Jun 28 18:12:50 EDT 2010


Author: lincolnthree
Date: 2010-06-28 18:12:50 -0400 (Mon, 28 Jun 2010)
New Revision: 13315

Added:
   sandbox/encore/src/main/antlr3/org/jboss/seam/encore/grammar/xml/
   sandbox/encore/src/main/antlr3/org/jboss/seam/encore/grammar/xml/XMLLexer.g
   sandbox/encore/src/main/antlr3/org/jboss/seam/encore/grammar/xml/XMLParser.g
   sandbox/encore/src/main/antlr3/org/jboss/seam/encore/grammar/xml/XMLTreeParser.g
   sandbox/encore/src/test/java/org/
   sandbox/encore/src/test/java/org/jboss/
   sandbox/encore/src/test/java/org/jboss/seam/
   sandbox/encore/src/test/java/org/jboss/seam/encore/
   sandbox/encore/src/test/java/org/jboss/seam/encore/grammar/
   sandbox/encore/src/test/java/org/jboss/seam/encore/grammar/xml/
   sandbox/encore/src/test/java/org/jboss/seam/encore/grammar/xml/XMLParserTest.java
   sandbox/encore/src/test/resources/org/
   sandbox/encore/src/test/resources/org/jboss/
   sandbox/encore/src/test/resources/org/jboss/seam/
   sandbox/encore/src/test/resources/org/jboss/seam/encore/
   sandbox/encore/src/test/resources/org/jboss/seam/encore/grammar/
   sandbox/encore/src/test/resources/org/jboss/seam/encore/grammar/xml/
   sandbox/encore/src/test/resources/org/jboss/seam/encore/grammar/xml/sample.xml
Modified:
   sandbox/encore/pom.xml
Log:
XML Parser & Test

Modified: sandbox/encore/pom.xml
===================================================================
--- sandbox/encore/pom.xml	2010-06-28 18:06:58 UTC (rev 13314)
+++ sandbox/encore/pom.xml	2010-06-28 22:12:50 UTC (rev 13315)
@@ -39,6 +39,12 @@
 			<version>2.1.1</version>
 			<scope>provided</scope>
 		</dependency>
+  
+		<dependency>
+			<groupId>junit</groupId>
+			<artifactId>junit</artifactId>
+			<version>4.8.1</version>
+		</dependency>
 	</dependencies>
 
 	<build>

Added: sandbox/encore/src/main/antlr3/org/jboss/seam/encore/grammar/xml/XMLLexer.g
===================================================================
--- sandbox/encore/src/main/antlr3/org/jboss/seam/encore/grammar/xml/XMLLexer.g	                        (rev 0)
+++ sandbox/encore/src/main/antlr3/org/jboss/seam/encore/grammar/xml/XMLLexer.g	2010-06-28 22:12:50 UTC (rev 13315)
@@ -0,0 +1,44 @@
+lexer grammar XMLLexer;
+
+ at members {
+    boolean tagMode = false;
+}
+
+ at lexer::header {package org.jboss.seam.encore.grammar.xml;}
+
+TAG_START_OPEN : '<' { tagMode = true; } ;
+TAG_END_OPEN : '</' { tagMode = true; } ;
+TAG_CLOSE : { tagMode }?=> '>' { tagMode = false; } ;
+TAG_EMPTY_CLOSE : { tagMode }?=> '/>' { tagMode = false; } ;
+
+ATTR_EQ : { tagMode }?=> '=' ;
+
+ATTR_VALUE : { tagMode }?=>
+        ( '"' (~'"')* '"'
+        | '\'' (~'\'')* '\''
+        )
+    ;
+
+PCDATA : { !tagMode }?=> (~'<')+ ;
+
+GENERIC_ID
+    : { tagMode }?=>
+      ( LETTER | '_' | ':') (NAMECHAR)*
+    ;
+
+fragment NAMECHAR
+    : LETTER | DIGIT | '.' | '-' | '_' | ':'
+    ;
+
+fragment DIGIT
+    :    '0'..'9'
+    ;
+
+fragment LETTER
+    : 'a'..'z'
+    | 'A'..'Z'
+    ;
+
+WS  :  { tagMode }?=>
+       (' '|'\r'|'\t'|'\u000C'|'\n') {$channel=HIDDEN;}
+    ;

Added: sandbox/encore/src/main/antlr3/org/jboss/seam/encore/grammar/xml/XMLParser.g
===================================================================
--- sandbox/encore/src/main/antlr3/org/jboss/seam/encore/grammar/xml/XMLParser.g	                        (rev 0)
+++ sandbox/encore/src/main/antlr3/org/jboss/seam/encore/grammar/xml/XMLParser.g	2010-06-28 22:12:50 UTC (rev 13315)
@@ -0,0 +1,37 @@
+parser grammar XMLParser;
+
+options {
+    tokenVocab=XMLLexer;
+    output=AST;
+}
+
+tokens {
+    ELEMENT;
+    ATTRIBUTE;
+}
+
+ at parser::header {package org.jboss.seam.encore.grammar.xml;}
+
+document : element ;
+
+element
+    : ( startTag^
+            (element
+            | PCDATA
+            )*
+            endTag!
+        | emptyElement
+        )
+    ;
+
+startTag : TAG_START_OPEN GENERIC_ID attribute* TAG_CLOSE
+        -> ^(ELEMENT GENERIC_ID attribute*)
+    ;
+
+attribute : GENERIC_ID ATTR_EQ ATTR_VALUE -> ^(ATTRIBUTE GENERIC_ID ATTR_VALUE) ;
+
+endTag! : TAG_END_OPEN GENERIC_ID TAG_CLOSE;
+
+emptyElement : TAG_START_OPEN GENERIC_ID attribute* TAG_EMPTY_CLOSE
+        -> ^(ELEMENT GENERIC_ID attribute*)
+    ;

Added: sandbox/encore/src/main/antlr3/org/jboss/seam/encore/grammar/xml/XMLTreeParser.g
===================================================================
--- sandbox/encore/src/main/antlr3/org/jboss/seam/encore/grammar/xml/XMLTreeParser.g	                        (rev 0)
+++ sandbox/encore/src/main/antlr3/org/jboss/seam/encore/grammar/xml/XMLTreeParser.g	2010-06-28 22:12:50 UTC (rev 13315)
@@ -0,0 +1,26 @@
+tree grammar XMLTreeParser;
+
+options { 
+	tokenVocab=XMLLexer;
+	ASTLabelType = Tree;
+}
+
+ at header {package org.jboss.seam.encore.grammar.xml;}
+
+document : element ;
+
+element
+    : ^( ELEMENT name=GENERIC_ID
+            { System.out.print("<"+$name.text); }
+            (
+                ^(ATTRIBUTE attrName=GENERIC_ID value=ATTR_VALUE)
+                { System.out.print(" "+$attrName.text+"="+$value.text); }
+            )*
+            { System.out.println(">"); }
+            (element
+            | text=PCDATA
+                { System.out.print($text.text); }
+            )*
+            { System.out.println("</"+$name.text+">"); }
+        )
+    ;
\ No newline at end of file

Added: sandbox/encore/src/test/java/org/jboss/seam/encore/grammar/xml/XMLParserTest.java
===================================================================
--- sandbox/encore/src/test/java/org/jboss/seam/encore/grammar/xml/XMLParserTest.java	                        (rev 0)
+++ sandbox/encore/src/test/java/org/jboss/seam/encore/grammar/xml/XMLParserTest.java	2010-06-28 22:12:50 UTC (rev 13315)
@@ -0,0 +1,54 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.seam.encore.grammar.xml;
+
+import java.net.URL;
+
+import org.antlr.runtime.ANTLRFileStream;
+import org.antlr.runtime.CommonTokenStream;
+import org.antlr.runtime.tree.CommonTreeNodeStream;
+import org.antlr.runtime.tree.Tree;
+import org.junit.Test;
+
+/**
+ * @author <a href="mailto:lincolnbaxter at gmail.com">Lincoln Baxter, III</a>
+ */
+public class XMLParserTest
+{
+   @Test
+   public void testParses() throws Exception
+   {
+      URL file = getClass().getClassLoader().getResource("org/jboss/seam/encore/grammar/xml/sample.xml");
+
+      XMLLexer lexer = new XMLLexer(new ANTLRFileStream(file.getPath()));
+
+      CommonTokenStream tokens = new CommonTokenStream(lexer);
+      XMLParser parser = new XMLParser(tokens);
+      XMLParser.document_return root = parser.document();
+      System.out.println("tree=" + ((Tree) root.tree).toStringTree());
+      String text = ((Tree) root.tree).getText();
+
+      CommonTreeNodeStream nodes = new CommonTreeNodeStream(root.tree);
+      XMLTreeParser walker = new XMLTreeParser(nodes);
+      walker.document();
+   }
+}

Added: sandbox/encore/src/test/resources/org/jboss/seam/encore/grammar/xml/sample.xml
===================================================================
--- sandbox/encore/src/test/resources/org/jboss/seam/encore/grammar/xml/sample.xml	                        (rev 0)
+++ sandbox/encore/src/test/resources/org/jboss/seam/encore/grammar/xml/sample.xml	2010-06-28 22:12:50 UTC (rev 13315)
@@ -0,0 +1,6 @@
+<root level="0">
+   <child level="1" />
+   <child level="1" >
+      <child level="2"></child>
+   </child >
+</root>
\ No newline at end of file



More information about the seam-commits mailing list