options { STATIC = false; DEBUG_PARSER = false; } PARSER_BEGIN(ScriptParser) package com.arsdigita.initializer; import com.arsdigita.util.Classes; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.List; /** * ScriptParser.java * * @author rhs@mit.edu * @version $Revision: #6 $ $Date: 2004/03/01 $ */ public class ScriptParser { public final static String versionId = "$Id: ScriptParser.jj 736 2005-09-01 10:46:05Z sskracic $ by $Author: sskracic $, $DateTime: 2004/03/01 15:52:03 $"; boolean m_configOnly = false; public void setConfigOnly(boolean configOnly) { m_configOnly = configOnly; } } PARSER_END(ScriptParser) SKIP: { " " | "\t" | "\n" | "\r" } TOKEN: { | | | | | | } TOKEN [IGNORE_CASE]: { } TOKEN: { (|)*> | <#CH: ["a" - "z", "A" - "Z", "_", "$"]> | )+ ()+> | )+> | <#DIGIT: ["0" - "9"]> | } SPECIAL_TOKEN: { } void parse(Script s) throws InitializationException : { Initializer i; } { ( i = initializer() { if ( !s.addInitializer(i)) return; } )* } Initializer initializer() throws InitializationException : { Initializer i; Configuration c; } { i = initializerClass() { c = i.getConfiguration(); } ( assignment(c) )* { return i; } } Initializer initializerClass() throws InitializationException : { Token t; StringBuffer className = new StringBuffer(); } { t = { className.append(t.image); } ( t = { className.append("."); className.append(t.image); } ) * { if (m_configOnly) { return (Initializer) Classes.newInstance (GenericInitializer.class, new Class[] {String.class}, new Object[] {className.toString()}); } try { Class cls = Class.forName(className.toString()); Constructor cons = cls.getConstructor(new Class[0]); return (Initializer) cons.newInstance(new Object[0]); } catch (ClassNotFoundException e) { throw new InitializationException("Couldn't find " + className); } catch (NoSuchMethodException e) { throw new InitializationException( className + " must implement com.arsdigita.initializer.Initializer" ); } catch (InstantiationException e) { throw new InitializationException( "Error instantiating " + className + ": " + e.getMessage() ); } catch (ClassCastException e) { throw new InitializationException( className + " must implement com.arsdigita.initializer.Initializer" ); } catch (IllegalAccessException e) { throw new InitializationException(className + " must be public."); } catch (InvocationTargetException e) { throw new InitializationException( "Error instantiating " + className + ": " + e.getTargetException().getMessage() ); } } } void assignment(Configuration c) throws InitializationException : { String param; Object value; } { param = lhs() value = rhs() { c.setParameter(param, value); } } String lhs() : { Token t; } { t = { return t.image; } } Object rhs() : { Token t; Object result; } { t = { return t.image.substring(1, t.image.length() - 1); } | t = { return new Integer(t.image); } | t = { return new Float(t.image); } | { return Boolean.TRUE; } | { return Boolean.FALSE; } | result = list() { return result; } } List list() : { List l = new ArrayList(); Object o; } { [ o = rhs() { l.add(o); } ( o = rhs() { l.add(o); } )* ] { return l; } }