Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PoC] Stylefunction #52

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.metaborg.spoofax.shell.functions.InputFunction;
import org.metaborg.spoofax.shell.functions.PTransformFunction;
import org.metaborg.spoofax.shell.functions.ParseFunction;
import org.metaborg.spoofax.shell.functions.StyleFunction;
import org.metaborg.spoofax.shell.invoker.ICommandInvoker;
import org.metaborg.spoofax.shell.invoker.SpoofaxCommandInvoker;
import org.metaborg.spoofax.shell.output.AnalyzeResult;
Expand All @@ -37,6 +38,7 @@
import org.metaborg.spoofax.shell.output.ISpoofaxTermResult;
import org.metaborg.spoofax.shell.output.InputResult;
import org.metaborg.spoofax.shell.output.ParseResult;
import org.metaborg.spoofax.shell.output.StyleResult;
import org.metaborg.spoofax.shell.output.TransformResult;

import com.google.common.io.Files;
Expand Down Expand Up @@ -107,6 +109,8 @@ protected void bindFactories() {
PTransformFunction.class)
.implement(new TypeLiteral<FailableFunction<AnalyzeResult, TransformResult, IResult>>() { },
ATransformFunction.class)
.implement(new TypeLiteral<FailableFunction<ParseResult, StyleResult, IResult>>() { },
StyleFunction.class)
.implement(new TypeLiteral<FailableFunction<ISpoofaxTermResult<?>,
EvaluateResult, IResult>>() { },
EvaluateFunction.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.metaborg.spoofax.shell.output.IResult;
import org.metaborg.spoofax.shell.output.InputResult;
import org.metaborg.spoofax.shell.output.ParseResult;
import org.metaborg.spoofax.shell.output.StyleResult;
import org.metaborg.spoofax.shell.output.TransformResult;

import com.google.inject.assistedinject.Assisted;
Expand Down Expand Up @@ -127,6 +128,10 @@ private FailableFunction<String, EvaluateResult, IResult> aEvaluateFunction() {
.kleisliCompose(functionFactory.createEvaluateFunction(project, lang));
}

private FailableFunction<String, StyleResult, IResult> styleFunction() {
return parseFunction().kleisliCompose(functionFactory.createStyleFunction(project, lang));
}

/**
* Returns a function that creates an {@link InputResult} from a String.
*
Expand Down Expand Up @@ -194,6 +199,10 @@ public CommandBuilder<EvaluateResult> evalAnalyzed() {
return function(aEvaluateFunction());
}

public CommandBuilder<StyleResult> styleParsed() {
return function(styleFunction());
}

/**
* Set the function for a new builder with the current parameters. Discards the current function
* of this builder.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ private void loadCommands(ILanguageImpl lang) {

invoker.resetCommands();
invoker.addCommand("parse", builder.parse().description("Parse the expression").build());
invoker.addCommand("style", builder.styleParsed().description("Style the expression").build());
if (analyze) {
invoker.addCommand("analyze", builder.analyze()
.description("Analyze the expression").build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.metaborg.spoofax.shell.output.ISpoofaxTermResult;
import org.metaborg.spoofax.shell.output.InputResult;
import org.metaborg.spoofax.shell.output.ParseResult;
import org.metaborg.spoofax.shell.output.StyleResult;
import org.metaborg.spoofax.shell.output.TransformResult;

/**
Expand Down Expand Up @@ -92,4 +93,6 @@ public interface IFunctionFactory {
* @return an {@link CommandBuilder}
*/
CommandBuilder<?> createBuilder(IProject project, ILanguageImpl lang);

FailableFunction<ParseResult, StyleResult, IResult> createStyleFunction(IProject project, ILanguageImpl lang);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.metaborg.spoofax.shell.functions;

import org.metaborg.core.language.ILanguageImpl;
import org.metaborg.core.project.IProject;
import org.metaborg.core.style.IRegionCategory;
import org.metaborg.core.style.IRegionStyle;
import org.metaborg.core.syntax.ParseException;
import org.metaborg.spoofax.core.style.ISpoofaxCategorizerService;
import org.metaborg.spoofax.core.style.ISpoofaxStylerService;
import org.metaborg.spoofax.core.unit.ISpoofaxParseUnit;
import org.metaborg.spoofax.shell.output.FailOrSuccessResult;
import org.metaborg.spoofax.shell.output.IResult;
import org.metaborg.spoofax.shell.output.IResultFactory;
import org.metaborg.spoofax.shell.output.ParseResult;
import org.metaborg.spoofax.shell.output.StyleResult;
import org.spoofax.interpreter.terms.IStrategoTerm;

import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted;

public class StyleFunction extends AbstractSpoofaxFunction<ParseResult, StyleResult> {
private ISpoofaxCategorizerService categorizerService;
private ISpoofaxStylerService stylerService;

@Inject
public StyleFunction(ISpoofaxCategorizerService categorizerService, ISpoofaxStylerService stylerService,
IResultFactory resultFactory, @Assisted IProject project,
@Assisted ILanguageImpl lang) {
super(resultFactory, project, lang);
this.categorizerService = categorizerService;
this.stylerService = stylerService;
}

@Override
protected FailOrSuccessResult<StyleResult, IResult> applyThrowing(ParseResult a)
throws ParseException {
ISpoofaxParseUnit unit = a.unit();
Iterable<IRegionCategory<IStrategoTerm>> categorized = categorizerService.categorize(lang, unit);
Iterable<IRegionStyle<IStrategoTerm>> styled = stylerService.styleParsed(lang, categorized);

return FailOrSuccessResult.ofSpoofaxResult(resultFactory.createStyleResult(unit, styled));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.apache.commons.vfs2.FileObject;
import org.metaborg.core.language.ILanguageImpl;
import org.metaborg.core.style.IRegionStyle;
import org.metaborg.core.syntax.IInputUnit;
import org.metaborg.spoofax.core.syntax.JSGLRParserConfiguration;
import org.metaborg.spoofax.core.unit.ISpoofaxAnalyzeUnit;
Expand Down Expand Up @@ -84,4 +85,7 @@ InputResult createInputResult(ILanguageImpl lang, FileObject file, String source
EvaluateResult createEvaluateResult(ISpoofaxTermResult<?> inputTermResult,
IStrategoTerm result);

StyleResult createStyleResult(ISpoofaxParseUnit unit,
Iterable<IRegionStyle<IStrategoTerm>> styled);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package org.metaborg.spoofax.shell.output;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import org.metaborg.core.context.IContext;
import org.metaborg.core.messages.IMessage;
import org.metaborg.core.style.IRegionStyle;
import org.metaborg.core.style.RegionStyle;
import org.metaborg.spoofax.core.stratego.IStrategoCommon;
import org.metaborg.spoofax.core.unit.ISpoofaxParseUnit;
import org.spoofax.interpreter.terms.IStrategoTerm;

import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted;

public class StyleResult extends ParseResult {

private Iterable<IRegionStyle<IStrategoTerm>> styled;

@Inject
public StyleResult(IStrategoCommon common, @Assisted ISpoofaxParseUnit unit,
@Assisted Iterable<IRegionStyle<IStrategoTerm>> styled) {
super(common, unit);
this.styled = styled;
}

@Override
public Optional<IContext> context() {
return Optional.empty();
}

@Override
public List<IMessage> messages() {
return StreamSupport.stream(unit().messages().spliterator(), false)
.collect(Collectors.toList());
}

@Override
public StyledText styled() {
Iterable<IRegionStyle<String>> collect = StreamSupport.stream(styled.spliterator(), false)
.map(e -> new RegionStyle<String>(e.region(), e.style(),
sourceText().substring(e.region().startOffset(),
e.region().endOffset() + 1)))
.collect(Collectors.toList());

return new StyledText(collect);
}

@Override
public String sourceText() {
return unit().input().text();
}

@Override
public boolean valid() {
return unit().valid() && unit().success();
}

@Override
public void accept(IResultVisitor visitor) {
visitor.visitTermResult(this);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,10 @@ public void keyPressed(KeyEvent event) {

@Override
public void modifyText(ModifyEvent event) {
// TODO: text has been modified, send it to get syntax highlighting.
String doc = document.get();
observers.forEach(e -> {
e.onNext(String.format(":style %s", doc));
});
}

}