Search in sources :

Example 11 with IVisitor

use of org.whole.lang.visitors.IVisitor in project whole by wholeplatform.

the class ReusablesLanguageDeployer method deploy.

public void deploy(ReflectionFactory platform) {
    platform.addLanguageKit(new ReusablesLanguageKit());
    platform.addOperationFactory(ReusablesLanguageKit.URI, GenericBuilderAdapterOperation.ID, new IBuilderFactory() {

        public IBuilder create(IBuilder strategy, IEntityContext entityContext) {
            return new ReusablesGenericBuilderAdapter((IReusablesBuilder) strategy, entityContext);
        }
    });
    platform.addOperationFactory(ReusablesLanguageKit.URI, SpecificBuilderAdapterOperation.ID, new IBuilderFactory() {

        public IBuilder create(IBuilder strategy, IEntityContext entityContext) {
            return new ReusablesSpecificBuilderAdapter(strategy, entityContext);
        }
    });
    platform.addOperationFactory(ReusablesLanguageKit.URI, InterpreterOperation.ID, new IVisitorFactory() {

        public IVisitor create(IOperation operation, int stage) {
            if (stage == 0)
                return new ReusablesInterpreterVisitor();
            else
                return null;
        }
    });
    platform.addOperationFactory(ReusablesLanguageKit.URI, DynamicCompilerOperation.ID, new IVisitorFactory() {

        public IVisitor create(IOperation operation, int stage) {
            if (stage == 0)
                return new ReusablesDynamicCompilerVisitor();
            else
                return null;
        }
    });
    platform.addOperationFactory(ReusablesLanguageKit.URI, ArtifactsGeneratorOperation.ID, new IVisitorFactory() {

        public IVisitor create(IOperation operation, int stage) {
            return new ReusablesArtifactsGeneratorVisitor();
        }
    });
// platform.addReactionsHandler(ReusablesLanguageKit.URI, new ReusablesReactionsHandler());
}
Also used : IBuilder(org.whole.lang.builders.IBuilder) IOperation(org.whole.lang.operations.IOperation) ReusablesDynamicCompilerVisitor(org.whole.lang.reusables.visitors.ReusablesDynamicCompilerVisitor) IVisitor(org.whole.lang.visitors.IVisitor) IEntityContext(org.whole.lang.contexts.IEntityContext) ReusablesGenericBuilderAdapter(org.whole.lang.reusables.builders.ReusablesGenericBuilderAdapter) IBuilderFactory(org.whole.lang.builders.IBuilderFactory) ReusablesSpecificBuilderAdapter(org.whole.lang.reusables.builders.ReusablesSpecificBuilderAdapter) IVisitorFactory(org.whole.lang.visitors.IVisitorFactory) ReusablesArtifactsGeneratorVisitor(org.whole.lang.reusables.visitors.ReusablesArtifactsGeneratorVisitor) IReusablesBuilder(org.whole.lang.reusables.builders.IReusablesBuilder) ReusablesInterpreterVisitor(org.whole.lang.reusables.visitors.ReusablesInterpreterVisitor)

Example 12 with IVisitor

use of org.whole.lang.visitors.IVisitor in project whole by wholeplatform.

the class AttributesMatcherFactory method matchNamespaceDeclaration.

public static IVisitor matchNamespaceDeclaration(IBindingManager bm) {
    IVisitor ov = GenericTraversalFactory.instance.one(GenericMatcherFactory.instance.matchInScope(getNsDeclPattern()), GenericMatcherFactory.instance.matchInScope(getDefaultNsDeclPattern()));
    ov.setBindings(bm);
    return ov;
}
Also used : IVisitor(org.whole.lang.visitors.IVisitor)

Example 13 with IVisitor

use of org.whole.lang.visitors.IVisitor in project whole by wholeplatform.

the class ScriptsInterpreterVisitor method visit.

@Override
public void visit(Script entity) {
    Writer outputWriter = op().getWriter();
    IBindingManager bm = getBindings();
    BindingsAdapter bindingsAdapter = new BindingsAdapter(bm, true);
    LanguageName languageName = entity.getLanguageName();
    /*
		 * TODO
		 * if contains an upper level fragment fail
		 */
    Source source = entity.getSource();
    IEntity sourceRoot = EntityUtils.isFragment(source) ? source.wGetRoot() : source;
    IEntity result0 = bm.getResult();
    IVisitor ov = op().setVisitor(sourceRoot, 0, op().getVisitor(sourceRoot, 1));
    op().stagedVisit(source);
    IEntity sourceModel = bm.getResult();
    // workaround restore previous value (also for self and queryIterator?)
    bm.setResult(result0);
    op().setVisitor(sourceRoot, 0, ov);
    String sourceString = toPrettyPrintString(sourceModel);
    ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
    ScriptEngine scriptEngine = scriptEngineManager.getEngineByName(languageName.getValue());
    if (scriptEngine == null)
        throw new VisitException("Cannot find an interpreter for the language: " + languageName);
    scriptEngine.setBindings(bindingsAdapter, ScriptContext.ENGINE_SCOPE);
    PrintWriter printWriter = new PrintWriter(outputWriter);
    scriptEngine.getContext().setWriter(printWriter);
    scriptEngine.getContext().setErrorWriter(printWriter);
    Object result = null;
    bm.wEnterScope();
    do {
        try {
            result = scriptEngine.eval(sourceString);
            break;
        } catch (ScriptException e) {
            bm.wExitScope();
            if (!bindingsAdapter.isAutoboxing())
                throw new VisitException("Script Interpreter failure.", e);
            bindingsAdapter.setAutoboxing(false);
            bm.wEnterScope();
        }
    } while (true);
    bm.wExitScope(true);
    if (result instanceof IEntity)
        bm.setResult((IEntity) result);
    else if (result != null)
        bm.setResult(BindingManagerFactory.instance.createSpecificValue(result));
}
Also used : IEntity(org.whole.lang.model.IEntity) IVisitor(org.whole.lang.visitors.IVisitor) VisitException(org.whole.lang.visitors.VisitException) ScriptEngineManager(javax.script.ScriptEngineManager) BindingsAdapter(org.whole.lang.scripts.bindings.BindingsAdapter) PrettyPrinterOperation.toPrettyPrintString(org.whole.lang.operations.PrettyPrinterOperation.toPrettyPrintString) Source(org.whole.lang.scripts.model.Source) ScriptEngine(javax.script.ScriptEngine) ScriptException(javax.script.ScriptException) IBindingManager(org.whole.lang.bindings.IBindingManager) LanguageName(org.whole.lang.scripts.model.LanguageName) PrintWriter(java.io.PrintWriter) Writer(java.io.Writer) PrintWriter(java.io.PrintWriter)

Example 14 with IVisitor

use of org.whole.lang.visitors.IVisitor in project whole by wholeplatform.

the class RewriteTest method testRewrite.

@Test
public void testRewrite() {
    IEntity program = buildImpProgram();
    PrettyPrinterOperation.prettyPrint(program);
    GenericMatcherFactory mf = GenericMatcherFactory.instance;
    GenericTraversalFactory tf = GenericTraversalFactory.instance;
    IVisitor v = tf.innermost(tf.concurrentSome(mf.rewrite(IfElseEmptyPattern(), IfElseEmptyRewrite(), false), mf.rewrite(IfTruePattern(), IfTrueRewrite(), false), mf.rewrite(IfTrueElsePattern(), IfTrueRewrite(), false), mf.rewrite(IfNotPattern(), IfNotRewrite(), false)));
    v.visit(program);
    PrettyPrinterOperation.prettyPrint(program);
}
Also used : IEntity(org.whole.lang.model.IEntity) IVisitor(org.whole.lang.visitors.IVisitor) GenericMatcherFactory(org.whole.lang.matchers.GenericMatcherFactory) GenericTraversalFactory(org.whole.lang.visitors.GenericTraversalFactory) Test(org.junit.Test)

Example 15 with IVisitor

use of org.whole.lang.visitors.IVisitor in project whole by wholeplatform.

the class ModelsLanguageDeployer method deploy.

public void deploy(ReflectionFactory platform) {
    platform.addLanguageKit(new ModelsLanguageKit());
    platform.addOperationFactory(ModelsLanguageKit.URI, GenericBuilderAdapterOperation.ID, new IBuilderFactory() {

        public IBuilder create(IBuilder strategy, IEntityContext entityContext) {
            return new ModelsGenericBuilderAdapter((IModelsBuilder) strategy, entityContext);
        }
    });
    platform.addOperationFactory(ModelsLanguageKit.URI, SpecificBuilderAdapterOperation.ID, new IBuilderFactory() {

        public IBuilder create(IBuilder strategy, IEntityContext entityContext) {
            return new ModelsSpecificBuilderAdapter(strategy, entityContext);
        }
    });
    platform.addOperationFactory(ModelsLanguageKit.URI, ExportTexOperation.ID, new IVisitorFactory() {

        public IVisitor create(IOperation operation, int stage) {
            return new TeXGeneratorVisitor((ExportTexOperation) operation);
        }
    });
    platform.addOperationFactory(ModelsLanguageKit.URI, PrettyPrinterOperation.ID, new IVisitorFactory() {

        public IVisitor create(IOperation operation, int stage) {
            return new ModelsPrettyPrinterVisitor((PrettyPrinterOperation) operation);
        }
    });
    platform.addOperationFactory(ModelsLanguageKit.URI, ValidatorOperation.ID, new IVisitorFactory() {

        public IVisitor create(IOperation operation, int stage) {
            return new ModelsValidatorVisitor();
        }
    });
    platform.addOperationFactory(ModelsLanguageKit.URI, InterpreterOperation.ID, new IVisitorFactory() {

        public IVisitor create(IOperation operation, int stage) {
            if (stage == 0)
                return new ModelsInterpreterVisitor();
            else
                return null;
        }
    });
    platform.addOperationFactory(ModelsLanguageKit.URI, NormalizerOperation.ID, new IVisitorFactory() {

        public IVisitor create(IOperation operation, int stage) {
            return new ModelsNormalizerVisitor();
        }
    });
}
Also used : IBuilder(org.whole.lang.builders.IBuilder) IOperation(org.whole.lang.operations.IOperation) PrettyPrinterOperation(org.whole.lang.operations.PrettyPrinterOperation) IVisitor(org.whole.lang.visitors.IVisitor) IEntityContext(org.whole.lang.contexts.IEntityContext) TeXGeneratorVisitor(org.whole.lang.models.visitors.TeXGeneratorVisitor) IModelsBuilder(org.whole.lang.models.builders.IModelsBuilder) IBuilderFactory(org.whole.lang.builders.IBuilderFactory) IVisitorFactory(org.whole.lang.visitors.IVisitorFactory) ModelsSpecificBuilderAdapter(org.whole.lang.models.builders.ModelsSpecificBuilderAdapter) ModelsPrettyPrinterVisitor(org.whole.lang.models.visitors.ModelsPrettyPrinterVisitor) ExportTexOperation(org.whole.lang.operations.ExportTexOperation) ModelsGenericBuilderAdapter(org.whole.lang.models.builders.ModelsGenericBuilderAdapter) ModelsInterpreterVisitor(org.whole.lang.models.visitors.ModelsInterpreterVisitor) ModelsNormalizerVisitor(org.whole.lang.models.visitors.ModelsNormalizerVisitor) ModelsValidatorVisitor(org.whole.lang.models.visitors.ModelsValidatorVisitor)

Aggregations

IVisitor (org.whole.lang.visitors.IVisitor)63 IOperation (org.whole.lang.operations.IOperation)34 IVisitorFactory (org.whole.lang.visitors.IVisitorFactory)34 IBuilder (org.whole.lang.builders.IBuilder)20 IBuilderFactory (org.whole.lang.builders.IBuilderFactory)20 IEntityContext (org.whole.lang.contexts.IEntityContext)20 PrettyPrinterOperation (org.whole.lang.operations.PrettyPrinterOperation)13 IEntity (org.whole.lang.model.IEntity)9 ILanguageKit (org.whole.lang.reflect.ILanguageKit)4 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)3 IBindingManager (org.whole.lang.bindings.IBindingManager)3 Test (org.junit.Test)2 ITransactionScope (org.whole.lang.bindings.ITransactionScope)2 GenericMatcherFactory (org.whole.lang.matchers.GenericMatcherFactory)2 AbstractOperation (org.whole.lang.operations.AbstractOperation)2 PrettyPrinterOperation.toPrettyPrintString (org.whole.lang.operations.PrettyPrinterOperation.toPrettyPrintString)2 VisitException (org.whole.lang.visitors.VisitException)2 PrintWriter (java.io.PrintWriter)1 Writer (java.io.Writer)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1