org.modelcc.language.syntax.ObjectBuilder 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.syntax;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import org.modelcc.language.Builder;
import org.modelcc.language.LanguageException;
import org.modelcc.language.factory.SymbolIdentifier;
import org.modelcc.language.metamodel.CompositeLanguageElement;
import org.modelcc.language.metamodel.LanguageElement;
import org.modelcc.language.metamodel.LanguageModel;
import org.modelcc.language.metamodel.SimpleLanguageElement;
import org.modelcc.lexer.recognizer.PatternRecognizer;
/**
* Rule builder: Object instantiation from grammar rules.
*
* @author Fernando Berzal ([email protected])
*/
public class ObjectBuilder extends Builder
{
private ReferencePostBuilder referencePostBuilder;
/**
* Constructor
* @param model Language model
*/
public ObjectBuilder (LanguageModel model)
{
super(model);
referencePostBuilder = new ReferencePostBuilder(model);
}
/**
* Build a symbol, fill it, and validate it.
* @param t symbol to be built.
* @param data parser metadata.
* @return true if the symbol is valid, false if not.
*/
public boolean build (Rule r, Symbol t, ParserMetadata data)
{
SymbolBuilder builder = r.getBuilder();
if (builder!=null) {
return r.getBuilder().build(t, data);
} else {
try {
instantiate(r,t,data);
return true;
} catch (LanguageException error) {
// error.printStackTrace(System.err);
return false;
}
}
}
public Object instantiate (Rule r, Symbol t, ParserMetadata data)
throws LanguageException
{
Object object = null;
switch (r.getType()) {
case COMPOSITION:
object = instantiateComposition(t,data);
break;
case SELECTION:
object = instantiateSelection(t,data);
break;
case REFERENCE:
object = instantiateReference(r,t,data);
break;
case COPY:
object = t.getContent(0).getUserData();
break;
case REPETITION:
object = instantiateCollection(r,t,data);
break;
default:
if (t.getType() instanceof SymbolIdentifier) {
LanguageElement element = languageElement(t);
if (element instanceof SimpleLanguageElement) {
object = instantiateSelection(t,data);
if ((object==null) && ((SimpleLanguageElement) element).matchesEmptyString()) {
// Empty symbol builder
object = instantiate((SimpleLanguageElement)element, "");
}
}
}
}
if (r.getType()!=Rule.Type.REFERENCE)
t.setUserData(object);
return object;
}
private LanguageElement languageElement (Symbol t)
{
SymbolIdentifier id = (SymbolIdentifier) t.getType();
return id.getElement();
}
private int symbolIdentifier (Symbol t)
{
int target = -1;
for ( int i=0; i=0)
object = t.getContent(target).getUserData();
return object;
}
private Object instantiateComposition (Symbol t, ParserMetadata data)
throws LanguageException
{
CompositeLanguageElement ce = (CompositeLanguageElement) languageElement(t);
Object obj = instantiate(ce, t);
// Keys
if ((obj!=null) && !ce.getKeyMembers().isEmpty()) {
Class cls = ce.getElementClass();
Map> ids = data.getKeys();
Map