191 lines
4.2 KiB
Plaintext
Executable File
191 lines
4.2 KiB
Plaintext
Executable File
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 <a href="mailto:rhs@mit.edu">rhs@mit.edu</a>
|
|
* @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:
|
|
{
|
|
<SEMI: ";">
|
|
| <ASSIGN: "=">
|
|
| <DOT: ".">
|
|
| <COMMA: ",">
|
|
| <LBRACE: "{">
|
|
| <RBRACE: "}">
|
|
| <TRUE: "true">
|
|
| <FALSE: "false">
|
|
}
|
|
|
|
TOKEN [IGNORE_CASE]:
|
|
{
|
|
<INIT: "init">
|
|
}
|
|
|
|
TOKEN:
|
|
{
|
|
<ID: <CH>(<CH>|<DIGIT>)*>
|
|
| <#CH: ["a" - "z", "A" - "Z", "_", "$"]>
|
|
| <FLOAT: (<DIGIT>)+ <DOT> (<DIGIT>)+>
|
|
| <INT: (<DIGIT>)+>
|
|
| <#DIGIT: ["0" - "9"]>
|
|
| <STRING: "\"" (~["\""] | "\\\"")* "\"">
|
|
}
|
|
|
|
SPECIAL_TOKEN:
|
|
{
|
|
<COMMENT: "//" (~["\n", "\r"])* ("\n"|"\r"|"\r\n")>
|
|
}
|
|
|
|
void parse(Script s) throws InitializationException :
|
|
{
|
|
Initializer i;
|
|
}
|
|
{
|
|
( i = initializer() { if ( !s.addInitializer(i)) return; } )* <EOF>
|
|
}
|
|
|
|
Initializer initializer() throws InitializationException :
|
|
{
|
|
Initializer i;
|
|
Configuration c;
|
|
}
|
|
{
|
|
<INIT> i = initializerClass() { c = i.getConfiguration(); }
|
|
<LBRACE>
|
|
( assignment(c) <SEMI> )*
|
|
<RBRACE>
|
|
{ return i; }
|
|
}
|
|
|
|
Initializer initializerClass() throws InitializationException :
|
|
{
|
|
Token t;
|
|
StringBuffer className = new StringBuffer();
|
|
}
|
|
{
|
|
t = <ID> { className.append(t.image); }
|
|
( <DOT> t = <ID> { 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() <ASSIGN> value = rhs()
|
|
{
|
|
c.setParameter(param, value);
|
|
}
|
|
}
|
|
|
|
String lhs() :
|
|
{
|
|
Token t;
|
|
}
|
|
{
|
|
t = <ID> { return t.image; }
|
|
}
|
|
|
|
Object rhs() :
|
|
{
|
|
Token t;
|
|
Object result;
|
|
}
|
|
{
|
|
t = <STRING> { return t.image.substring(1, t.image.length() - 1); }
|
|
| t = <INT> { return new Integer(t.image); }
|
|
| t = <FLOAT> { return new Float(t.image); }
|
|
| <TRUE> { return Boolean.TRUE; }
|
|
| <FALSE> { return Boolean.FALSE; }
|
|
| result = list() { return result; }
|
|
}
|
|
|
|
|
|
List list() :
|
|
{
|
|
List l = new ArrayList();
|
|
Object o;
|
|
}
|
|
{
|
|
<LBRACE>
|
|
[ o = rhs() { l.add(o); } ( <COMMA> o = rhs() { l.add(o); } )* ]
|
|
<RBRACE>
|
|
{ return l; }
|
|
}
|