Author: nbelaevski
Date: 2007-07-08 09:31:55 -0400 (Sun, 08 Jul 2007)
New Revision: 1536
Added:
branches/3.0.2/richfaces/common/src/main/javacc/
branches/3.0.2/richfaces/common/src/main/javacc/RichMacroDefinition.jj
Removed:
branches/3.0.2/richfaces/common/src/main/antlr/
Modified:
branches/3.0.2/richfaces/common/pom.xml
branches/3.0.2/richfaces/common/src/main/java/org/richfaces/renderkit/MacroDefinitionJSContentHandler.java
branches/3.0.2/richfaces/common/src/test/java/org/richfaces/renderkit/TemplateUtilTest.java
Log:
Migration form ANTLR to JavaCC
Modified: branches/3.0.2/richfaces/common/pom.xml
===================================================================
--- branches/3.0.2/richfaces/common/pom.xml 2007-07-07 18:25:58 UTC (rev 1535)
+++ branches/3.0.2/richfaces/common/pom.xml 2007-07-08 13:31:55 UTC (rev 1536)
@@ -10,38 +10,19 @@
<name>common classes</name>
<build>
<plugins>
- <plugin>
- <!-- temporary solution, until maven-antlr-plugin will not become mature
-->
- <artifactId>maven-antrun-plugin</artifactId>
- <executions>
- <execution>
- <phase>generate-sources</phase>
- <configuration>
- <tasks>
- <java classname="org.antlr.Tool"
- classpathref="maven.plugin.classpath"
- fork="true"
- dir="src/main/antlr">
- <arg value="RichMacroDefinition.g"/>
- <arg line="-o
${project.build.directory}/generated-sources/antlr/org/richfaces"/>
- </java>
- </tasks>
-
<sourceRoot>${project.build.directory}/generated-sources/antlr</sourceRoot>
- </configuration>
- <goals>
- <goal>run</goal>
- </goals>
- </execution>
- </executions>
- <dependencies>
- <dependency>
- <groupId>org.antlr</groupId>
- <artifactId>antlr</artifactId>
- <version>3.0</version>
- <scope>compile</scope>
- </dependency>
- </dependencies>
- </plugin>
+ <plugin>
+ <groupId>org.codehaus.mojo</groupId>
+ <artifactId>javacc-maven-plugin</artifactId>
+ <version>2.1</version>
+ <executions>
+ <execution>
+ <goals>
+ <goal>javacc</goal>
+ </goals>
+ </execution>
+ </executions>
+ <configuration><packageName>org.richfaces.javacc</packageName></configuration>
+ </plugin>
</plugins>
</build>
@@ -56,7 +37,7 @@
<configuration>
<excludes combine.children="append">
<exclude>
- **/RichMacroDefinition*.java
+ **/javacc/**
</exclude>
</excludes>
</configuration>
Modified:
branches/3.0.2/richfaces/common/src/main/java/org/richfaces/renderkit/MacroDefinitionJSContentHandler.java
===================================================================
---
branches/3.0.2/richfaces/common/src/main/java/org/richfaces/renderkit/MacroDefinitionJSContentHandler.java 2007-07-07
18:25:58 UTC (rev 1535)
+++
branches/3.0.2/richfaces/common/src/main/java/org/richfaces/renderkit/MacroDefinitionJSContentHandler.java 2007-07-08
13:31:55 UTC (rev 1536)
@@ -4,16 +4,12 @@
package org.richfaces.renderkit;
import java.io.IOException;
+import java.io.StringReader;
import java.io.Writer;
import java.util.Iterator;
import java.util.List;
-import org.antlr.runtime.ANTLRStringStream;
-import org.antlr.runtime.CommonTokenStream;
-import org.antlr.runtime.RecognitionException;
-import org.richfaces.RichMacroDefinitionLexer;
-import org.richfaces.RichMacroDefinitionParser;
-import org.richfaces.RichMacroDefinitionParser.expression_return;
+import org.richfaces.javacc.RichMacroDefinition;
import org.richfaces.json.JSContentHandler;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
@@ -34,16 +30,11 @@
}
private List parseExpressiion(String expressionString) throws SAXException {
- ANTLRStringStream stream = new ANTLRStringStream(expressionString);
- RichMacroDefinitionLexer lexer = new RichMacroDefinitionLexer(stream);
- RichMacroDefinitionParser macroParser = new RichMacroDefinitionParser(
- new CommonTokenStream(lexer));
try {
- expression_return expression = macroParser.expression();
- List result = expression.result;
+ List result = new RichMacroDefinition(new
StringReader(expressionString)).expression();
return result;
- } catch (RecognitionException e) {
+ } catch (Exception e) {
throw new SAXException(e.getMessage(), e);
}
}
Added: branches/3.0.2/richfaces/common/src/main/javacc/RichMacroDefinition.jj
===================================================================
--- branches/3.0.2/richfaces/common/src/main/javacc/RichMacroDefinition.jj
(rev 0)
+++ branches/3.0.2/richfaces/common/src/main/javacc/RichMacroDefinition.jj 2007-07-08
13:31:55 UTC (rev 1536)
@@ -0,0 +1,125 @@
+/**
+ * JavaCC file
+ */
+
+options {
+ JDK_VERSION="1.3";
+ STATIC = false;
+}
+
+PARSER_BEGIN(RichMacroDefinition)
+package org.richfaces.javacc;
+
+import java.util.*;
+import java.io.*;
+
+public class RichMacroDefinition {
+
+ private static String unescape(String arg) {
+ StringBuffer result = new StringBuffer(arg);
+ int idx = 0;
+ while ((idx = result.indexOf("\\", idx)) != -1) {
+ char ch = result.charAt(idx + 1);
+ result.replace(idx, idx + 2, String.valueOf(ch));
+
+ idx ++;
+ }
+
+ return result.toString();
+ }
+
+ public static void main(String args[]) throws ParseException, IOException {
+ System.out.println("Reading from standard input...");
+ String line = new BufferedReader(new InputStreamReader(System.in)).readLine();
+ RichMacroDefinition parser = new RichMacroDefinition(new StringReader(line));
+ Object def = parser.expression();
+ System.out.println(def);
+ }
+}
+PARSER_END(RichMacroDefinition)
+
+TOKEN :
+{
+
+ < SLASH: "\\" > |
+ < LEFT_BRACKET: "{" > |
+ < RIGHT_BRACKET: "}" > |
+ <LITERAL: ((~["{", "\\", "}"])+) | ("\\"
["{", "\\", "}"]) >
+}
+
+List expression():
+{
+ String m = null;
+ String t = null;
+ List elems = new ArrayList();
+}
+{
+
+ (
+ (
+ t = text() {
+ elems.add(t);
+ t = null;
+ }
+ |
+ m = macrodef () {
+ elems.add(new org.richfaces.renderkit.Expression(m));
+ m = null;
+ }
+ )
+ )*
+
+ {
+ return elems;
+ }
+}
+
+String text():
+{
+ String result = null;
+ StringBuffer sb = null;
+ Token t = null;
+}
+{
+ ( t = <LITERAL>
+ {
+ if (result == null) {
+ result = unescape(t.image);
+ } else {
+ if (sb == null) {
+ sb = new StringBuffer(result);
+ }
+
+ sb.append(unescape(t.image));
+ }
+
+ t = null;
+ }
+ )+
+
+ {
+ return (sb != null ? sb.toString() : result);
+ }
+}
+
+String macrodef():
+{
+ StringBuffer result = new StringBuffer();
+ String m = null;
+ String t = null;
+}
+{
+ //macrodef:
+ //LEFT_BRACKET (macrodefpart | text)+ RIGHT_BRACKET;
+ <LEFT_BRACKET> (
+ t = text () {
+ result.append(t); t = null;
+ }
+ |
+ m = macrodef() {
+ result.append("{" + m + "}"); m = null;
+ }
+
+ )+ <RIGHT_BRACKET>
+ { return result.toString(); }
+}
\ No newline at end of file
Modified:
branches/3.0.2/richfaces/common/src/test/java/org/richfaces/renderkit/TemplateUtilTest.java
===================================================================
---
branches/3.0.2/richfaces/common/src/test/java/org/richfaces/renderkit/TemplateUtilTest.java 2007-07-07
18:25:58 UTC (rev 1535)
+++
branches/3.0.2/richfaces/common/src/test/java/org/richfaces/renderkit/TemplateUtilTest.java 2007-07-08
13:31:55 UTC (rev 1536)
@@ -21,17 +21,12 @@
package org.richfaces.renderkit;
-import java.io.IOException;
+import java.io.StringReader;
import java.util.List;
import junit.framework.TestCase;
-import org.antlr.runtime.ANTLRStringStream;
-import org.antlr.runtime.CommonTokenStream;
-import org.antlr.runtime.tree.Tree;
-import org.richfaces.RichMacroDefinitionLexer;
-import org.richfaces.RichMacroDefinitionParser;
-import org.richfaces.RichMacroDefinitionParser.expression_return;
+import org.richfaces.javacc.RichMacroDefinition;
/**
* @author Nick Belaevski - mailto:nbelaevski@exadel.com
@@ -40,29 +35,9 @@
*/
public class TemplateUtilTest extends TestCase {
- private String doWrite(String in) throws IOException {
- return in;
- }
- private void printTree(Tree tree, int indent) {
- int childCount = tree.getChildCount();
- for (int i = 0; i < indent; i++) {
- System.out.print('\t');
- System.out.print(tree.getText() + " : " + tree.getType());
- System.out.println();
- }
- for (int j = 1; j < childCount; j++) {
- printTree(tree.getChild(j), ++indent);
- }
- }
-
-
public void testAntlr() throws Exception {
- ANTLRStringStream stream = new ANTLRStringStream("{aa{b\\}}a}\\\\
a\\}b\\{c");
- RichMacroDefinitionLexer lexer = new RichMacroDefinitionLexer(stream);
- RichMacroDefinitionParser macroParser = new RichMacroDefinitionParser(new
CommonTokenStream(lexer));
- expression_return expression = macroParser.expression();
- List result = expression.result;
+ List result = new RichMacroDefinition(new StringReader("{aa{b\\}}a}\\\\
a\\}b\\{c")).expression();
Expression holder = (Expression) result.get(0);
assertEquals("aa{b}}a", holder.getExpression());
assertEquals("\\ a}b{c", result.get(1));
Show replies by date