org.modelcc.language.Builder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ModelCC Show documentation
Show all versions of ModelCC Show documentation
ModelCC is a model-based parser generator (a.k.a. compiler compiler) that decouples language specification from language processing, avoiding some of the problems caused by grammar-driven parser generators. ModelCC receives a conceptual model as input, along with constraints that annotate it. It is then able to create a parser for the desired textual language and the generated parser fully automates the instantiation of the language conceptual model. ModelCC also includes a built-in reference resolution mechanism that results in abstract syntax graphs, rather than mere abstract syntax trees.
The newest version!
package org.modelcc.language;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;
import org.modelcc.ModelCCProcess;
import org.modelcc.io.java.Reflection;
import org.modelcc.language.metamodel.CompositeLanguageElement;
import org.modelcc.language.metamodel.LanguageElement;
import org.modelcc.language.metamodel.LanguageMember;
import org.modelcc.language.metamodel.LanguageModel;
import org.modelcc.language.metamodel.MemberCollection;
import org.modelcc.language.metamodel.SimpleLanguageElement;
import org.modelcc.language.syntax.ObjectCollection;
import org.modelcc.language.syntax.RuleSymbol;
import org.modelcc.language.syntax.Symbol;
public abstract class Builder extends ModelCCProcess
{
protected LanguageModel model;
/**
* Constructor
*
* @param model Language model
*/
public Builder (LanguageModel model)
{
this.model = model;
}
/**
* Get language model
*
* @return The language model
*/
public final LanguageModel getModel()
{
return model;
}
// Temporary methods
protected Object instantiate (SimpleLanguageElement element, Object value)
throws LanguageException
{
Class c = element.getElementClass();
Object obj = null;
try {
obj = c.newInstance();
if (element.getValueField() != null) {
Field fld = Reflection.findField(c,element.getValueField());
if (fld != null) {
fld.setAccessible(true);
fld.set(obj, cast(fld.getType(), value));
}
}
} catch (Exception error) {
log(Level.SEVERE, "Token instantiation exception", error);
throw new LanguageException("[ModelCC] Object instantiation exception: Unable to create token instance", error);
}
runSetupMethods(obj,element);
if (!checkConstraints(obj,element))
throw new LanguageException("[ModelCC] Object instantiation exception: Unsatisfied constraints");
return obj;
}
protected Object instantiate(CompositeLanguageElement ce, Symbol t)
throws LanguageException
{
Object obj = null;
Class c = ce.getElementClass();
try {
obj = c.newInstance();
} catch (Exception error) {
throw new LanguageException("[ModelCC] Object instantiation exception: Unable to create instance", error);
}
Set filled = fill(c, t, obj);
fillOptionalFields(obj,getModel(),filled);
runSetupMethods(obj,ce);
if (!checkConstraints(obj,ce))
throw new LanguageException("[ModelCC] Object instantiation exception: Unsatisfied constraints");
return obj;
}
protected Object instantiateReference (CompositeLanguageElement ce, Symbol t)
throws LanguageException
{
Object obj = null;
Class c = ce.getElementClass();
try {
obj = c.newInstance();
} catch (Exception error) {
throw new LanguageException("[ModelCC] Object instantiation exception: Unable to create instance");
}
fill(c, t, obj);
return obj;
}
private Set fill(Class c, Symbol t, Object obj)
throws LanguageException
{
Set proc = new HashSet();
Set filled = new HashSet();
try {
for (int i=0; imc.getMaximumMultiplicity())
throw new LanguageException("[ModelCC] Object instantiation exception: Unsatisfied maximum multiplicity constraint.");
fillCollectionField(obj, fld, mc, listData);
}
}
}
}
} catch (Exception error) {
throw new LanguageException("[ModelCC] Object instantiation exception", error);
}
return filled;
}
private void fillCollectionField (Object obj, Field fld, MemberCollection mc, Object[] listData)
throws LanguageException
{
Object list;
Method add;
try {
fld.setAccessible(true);
switch (mc.getCollection()) {
case LIST:
if (fld.getType().isInterface())
list = ArrayList.class.newInstance();
else
list = fld.getType().newInstance();
add = list.getClass().getDeclaredMethod("add",Object.class);
for (int j=0; j filled)
throws LanguageException
{
try {
if (m.getClassToElement().get(o.getClass()).getClass().equals(SimpleLanguageElement.class)) {
// SimpleLanguageElement
Class c = o.getClass();
SimpleLanguageElement be = (SimpleLanguageElement) m.getClassToElement().get(c);
if (be.getValueField() != null) {
Field fld = Reflection.findField(c,be.getValueField());
if (fld != null) {
fld.setAccessible(true);
if (fld.getType().equals(String.class))
fld.set(o, cast(fld.getType(), ""));
}
}
} else {
// CompositeLanguageElement
CompositeLanguageElement ce = (CompositeLanguageElement)m.getClassToElement().get(o.getClass());
Field[] fields = o.getClass().getDeclaredFields();
for (int i=0; i