Skip to content

Commit

Permalink
Merge branch 'tagging' into 'dev'
Browse files Browse the repository at this point in the history
DSL-specific tagging

See merge request monticore/monticore!707
  • Loading branch information
MisterErwin committed Oct 13, 2023
2 parents 08214bf + 29e1eb5 commit c9b7afc
Show file tree
Hide file tree
Showing 91 changed files with 3,998 additions and 12 deletions.
8 changes: 4 additions & 4 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ build:
stage: build
script:
- "cd monticore-generator && gradle build $GRADLE_OPTS -Pci -i"
- "cd .. && gradle buildMC $GRADLE_OPTS -Pci -PgenTR=true -PgenEMF=true -i"
- "cd .. && gradle buildMC $GRADLE_OPTS -Pci -PgenTR=true -PgenEMF=true -PgenTagging=true -i"
artifacts:
paths:
- ".gradle"
Expand Down Expand Up @@ -121,7 +121,7 @@ test_grammar_it:
dependencies:
- build
script:
- "gradle -p monticore-test/monticore-grammar-it build -PgenEMF=true $GRADLE_OPTS -Pci"
- "gradle -p monticore-test/monticore-grammar-it build $GRADLE_OPTS -PgenEMF=true -Pci"
only:
- merge_requests
- branches
Expand All @@ -133,7 +133,7 @@ test_montitrans:
dependencies:
- build
script:
- "gradle -p monticore-test/montitrans build $GRADLE_OPTS -PgenTR=true -PgenEMF=true -Pci"
- "gradle -p monticore-test/montitrans build $GRADLE_OPTS -PgenTR=true -PgenEMF=true -PgenTagging=true -Pci"
only:
- merge_requests
- branches
Expand All @@ -145,7 +145,7 @@ deploy:
dependencies:
- build
script:
- "gradle deployMC -PmavenPassword=$password -PmavenUser=$username $GRADLE_OPTS -PgenTR=true -PgenEMF=true -Pci"
- "gradle deployMC -PmavenPassword=$password -PmavenUser=$username $GRADLE_OPTS -PgenTR=true -PgenEMF=true -PgenTagging=true -Pci"
only:
- dev
except:
Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ useLocalRepo=false
showTestOutput=false
genTR=false
genEMF=false
genTagging=false
org.gradle.jvmargs=-Xms3072m -Dfile.encoding=UTF-8


Expand Down
3 changes: 3 additions & 0 deletions monticore-generator/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ dependencies {
testImplementation 'com.github.javaparser:javaparser-symbol-solver-core:3.24.2'
testImplementation "junit:junit:$junit_version"
testImplementation 'org.mockito:mockito-core:4.5.1'

implementation "de.monticore:tagging:7.6.0-SNAPSHOT" // TODO: Remove both with next release, once the tagging grammars have been added to monticore-grammar
grammar "de.monticore:tagging:7.6.0-SNAPSHOT:grammars"
}

sourceSets {
Expand Down
44 changes: 42 additions & 2 deletions monticore-generator/src/main/java/de/monticore/MCTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

import com.google.common.collect.Iterables;
import de.monticore.cli.MontiCoreTool;
import de.monticore.codegen.cd2java._tagging.TaggingConstants;
import de.monticore.mcbasics.MCBasicsMill;
import de.monticore.dstlgen.util.DSTLPathUtil;
import de.monticore.symboltable.serialization.json.*;
import de.monticore.tagging.TagGenerator;
import de.se_rwth.commons.logging.Finding;
import de.se_rwth.commons.logging.Log;
import org.apache.commons.io.FileUtils;
Expand Down Expand Up @@ -119,7 +122,10 @@ public MCTask() {

// Whether the grammar is a TR grammar and transformation related artifacts should be generated
public boolean isDSTL = false;


// Whether tagging related artifacts should be generated (the grammar must be a tagdef or tagschema grammar)
public boolean genTag = false;

@OutputDirectory
public DirectoryProperty getOutputDir() {
return outputDir;
Expand Down Expand Up @@ -243,6 +249,11 @@ public boolean isDSTL() {
return isDSTL;
}

@Input
public boolean getGenTag() {
return genTag;
}

public void handcodedPath(String... paths) {
getHandcodedPath().addAll(List.of(paths));
}
Expand Down Expand Up @@ -380,6 +391,8 @@ protected String[] getParameters(Function<File, String> printPath){
}
params.add("-genDST");
params.add(Boolean.toString(isDSTL));
params.add("-genTag");
params.add(Boolean.toString(genTag));
if (configTemplate != null) {
String cfgTemplateStr = configTemplate.toString();
params.add("-ct");
Expand Down Expand Up @@ -465,7 +478,7 @@ public boolean incCheck(String grammar) throws IOException {
String base = this.getProject().getProjectDir().toPath().toAbsolutePath().toString();
return IncChecker.incCheck(inout, grammar, getLogger(), "mc4", base);
}
/**
/**
* Returns the path to the TR grammar.
* Please ensure that the outputDir is previously set
* @param originalGrammar the original grammar file
Expand All @@ -479,6 +492,33 @@ public File getTRFile(File originalGrammar, @Nullable File outputDir) {
modelPath.isEmpty() ? List.of(getProject().getLayout().getProjectDirectory().file("src/main/grammars").toString()) : modelPath,
originalGrammar).toString());
}
/**
* Returns the path to the Tag Def grammar.
* Please ensure that the outputDir is previously set
* @param originalGrammar the original grammar file
* @return the tag def grammar
*/
public File getTagDefinitionFile(File originalGrammar, @Nullable File outputDir) {
return new File((outputDir == null ? this.outputDir.get().getAsFile() : outputDir.toString())
+ "/"
+ TagGenerator.getTagGrammar(
modelPath.isEmpty() ? List.of(getProject().getLayout().getProjectDirectory().file("src/main/grammars").toString()) : modelPath,
originalGrammar, TaggingConstants.TAGDEFINITION_SUFFIX).toString());
}
/**
* Returns the path to the Tag Def grammar.
* Please ensure that the outputDir is previously set
* @param originalGrammar the original grammar file
* @return the tag def grammar
*/
public File getTagSchemaFile(File originalGrammar, @Nullable File outputDir) {
return new File((outputDir == null ? this.outputDir.get().getAsFile() : outputDir.toString())
+ "/"
+ TagGenerator.getTagGrammar(
modelPath.isEmpty() ? List.of(getProject().getLayout().getProjectDirectory().file("src/main/grammars").toString()) : modelPath,
originalGrammar, TaggingConstants.TAGSCHEMA_SUFFIX).toString());
}

protected File fromBasePath(String filePath) {
File file = new File(filePath);
return !file.isAbsolute()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public final class MontiCoreConfiguration implements Configuration {
public static final String REPORT_LONG = "report";
public static final String REPORT_BASE_LONG = "report_base";
public static final String GENDST_LONG = "genDST";
public static final String GENTAG_LONG = "genTag";
public static final String HELP_LONG = "help";

protected final CommandLine cmdConfig;
Expand Down Expand Up @@ -389,6 +390,15 @@ public Optional<Boolean> getGenDST() {
return getAsBoolean(GENDST_LONG);
}

/**
* Getter for the optional tagging generation.
*
* @return Optional boolean for the tagging generation
*/
public Optional<Boolean> getGenTag() {
return getAsBoolean(GENTAG_LONG);
}

/**
* @param files as String names to convert
* @return list of files by creating file objects from the Strings
Expand Down Expand Up @@ -432,6 +442,14 @@ public JsonElement getStatJson() {
result.putMember(GENDST_LONG, dstlGen);
}

// GenTag
{
JsonElement tagGen = this.getGenTag()
.map(p-> (JsonElement) new JsonBoolean(p))
.orElse(new JsonNull());
result.putMember(GENTAG_LONG, tagGen);
}

// Custom Script set?
result.putMember(SCRIPT_LONG, new JsonBoolean(cmdConfig.hasOption(SCRIPT)));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import de.monticore.codegen.cd2java._symboltable.serialization.Symbols2JsonDecorator;
import de.monticore.codegen.cd2java._symboltable.symbol.*;
import de.monticore.codegen.cd2java._symboltable.symbol.symbolsurrogatemutator.MandatoryMutatorSymbolSurrogateDecorator;
import de.monticore.codegen.cd2java._tagging.*;
import de.monticore.codegen.cd2java._visitor.*;
import de.monticore.codegen.cd2java.cli.CDCLIDecorator;
import de.monticore.codegen.cd2java.cli.CLIDecorator;
Expand Down Expand Up @@ -109,6 +110,7 @@
import de.monticore.io.paths.MCPath;
import de.monticore.symbols.basicsymbols.BasicSymbolsMill;
import de.monticore.symbols.basicsymbols._symboltable.DiagramSymbol;
import de.monticore.tagging.TagGenerator;
import de.se_rwth.commons.Joiners;
import de.se_rwth.commons.Names;
import de.se_rwth.commons.configuration.Configuration;
Expand Down Expand Up @@ -694,7 +696,14 @@ public ASTCDCompilationUnit decorateCD(GlobalExtensionManagement glex, ICD4Analy
return decoratedCD;
}

public void decorateForSymbolTablePackage(GlobalExtensionManagement glex, ICD4AnalysisScope cdScope,
public ASTCDCompilationUnit decorateTagCD(GlobalExtensionManagement glex, ICD4AnalysisScope cdScope,
List<ASTCDCompilationUnit> cds, MCPath handCodedPath, ASTCDCompilationUnit decoratedCD, ASTMCGrammar astGrammar) {
decorateTagging(glex, cdScope, cds.get(0), decoratedCD, handCodedPath, astGrammar);
return decoratedCD;
}


public void decorateForSymbolTablePackage(GlobalExtensionManagement glex, ICD4AnalysisScope cdScope,
ASTCDCompilationUnit astClassDiagram, ASTCDCompilationUnit symbolClassDiagramm,
ASTCDCompilationUnit scopeClassDiagramm,
ASTCDCompilationUnit decoratedCD, MCPath handCodedPath) {
Expand Down Expand Up @@ -895,6 +904,52 @@ protected void generateAuxiliary(ASTCDCompilationUnit cd, ASTCDCompilationUnit d
cdAuxiliaryDecorator.decorate(cd, decoratedCD);
}

public void decorateTagging(GlobalExtensionManagement glex, ICD4AnalysisScope cdScope,
ASTCDCompilationUnit cd, ASTCDCompilationUnit decoratedCD,
MCPath handCodedPath, ASTMCGrammar astTagGrammar){
generateTagging(cd, decoratedCD, glex, handCodedPath, astTagGrammar);
}

protected void generateTagging(ASTCDCompilationUnit cd, ASTCDCompilationUnit decoratedCD,
GlobalExtensionManagement glex, MCPath handCodedPath, ASTMCGrammar astTagGrammar) {
ASTMCGrammar originalGrammar = getOriginalGrammarFromTagGrammar(astTagGrammar).get();

SymbolTableService symbolTableService = new SymbolTableService(cd);
VisitorService visitorService = new VisitorService(cd);
ParserService parserService = new ParserService(cd);
AbstractService abstractService = new AbstractService<>(cd);

TaggerDecorator taggerDecorator = new TaggerDecorator(glex, abstractService, visitorService, originalGrammar);
TagConformsToSchemaCoCoDecorator tagCoCoDecorator = new TagConformsToSchemaCoCoDecorator(glex, abstractService, visitorService, originalGrammar);
CDTaggingDecorator taggingDecorator = new CDTaggingDecorator(glex, taggerDecorator, tagCoCoDecorator);

taggingDecorator.decorate(cd, decoratedCD);
}

protected ASTCDCompilationUnit deriveTaggingCD(ASTMCGrammar astGrammar, ICD4AnalysisGlobalScope cdScope ) {
return new MC2CDTaggingTranslation(cdScope).apply(getOriginalGrammarFromTagGrammar(astGrammar).get());
}

// Find & load the grammar A for given ATagDefinition grammar
protected Optional<ASTMCGrammar> getOriginalGrammarFromTagGrammar(ASTMCGrammar astTagGrammar) {
String originalGrammarName;
if (astTagGrammar.getSymbol().getName().endsWith(TaggingConstants.TAGDEFINITION_SUFFIX)) {
originalGrammarName = astTagGrammar.getSymbol().getFullName().substring(0, astTagGrammar.getSymbol().getFullName().length() - TaggingConstants.TAGDEFINITION_SUFFIX.length());
}else if (astTagGrammar.getSymbol().getName().endsWith(TaggingConstants.TAGSCHEMA_SUFFIX)) {
originalGrammarName = astTagGrammar.getSymbol().getFullName().substring(0, astTagGrammar.getSymbol().getFullName().length() - TaggingConstants.TAGSCHEMA_SUFFIX.length());
}else{
Log.error("0xA1018 Unable to generate Tagging infrastructure on non TagSchema/TagDef Grammar:" + astTagGrammar.getSymbol().getFullName());
return Optional.empty();
}
Optional<MCGrammarSymbol> originalGrammarOpt = GrammarFamilyMill.globalScope().resolveMCGrammar(originalGrammarName);
if (originalGrammarOpt.isEmpty()){
Log.error("0xA1026 Failed to resolve original grammar " + originalGrammarName + " of tag grammar " + astTagGrammar.getSymbol().getFullName());
return Optional.empty();
}
return Optional.of(originalGrammarOpt.get().getAstNode());
}


public void decoratePrettyPrinter(GlobalExtensionManagement glex, ASTCDCompilationUnit input, ICD4AnalysisScope cdScope,
ASTCDCompilationUnit prettyPrintCD, ASTCDCompilationUnit decoratedCD,
MCPath handCodedPath) {
Expand Down Expand Up @@ -1306,6 +1361,13 @@ public void generateDSTInfrastructure(ASTMCGrammar astTRGrammar, File out, MCPat
dstlgenUtil.generateTFGenToolClass(astGrammar, dstlGenerator, modelPathHC);
}

/**
* Generate the TagSchema and TagDefinition grammars and output them as mc4 files
*/
public void generateTaggingLanguages(ASTMCGrammar astGrammar, File outputDirectory, MCPath modelPathHC) throws IOException {
TagGenerator.generateTaggingLanguages(astGrammar, outputDirectory, modelPathHC);
}

/**
* Instantiates the glex and initializes it with all available default
* options based on the current configuration.
Expand Down Expand Up @@ -1408,6 +1470,7 @@ protected void doRun(String script, Configuration configuration) {
builder.addVariable(MODELPATH_LONG, modelPath);
builder.addVariable(HANDCODEDMODELPATH_LONG, handcodedModelPath);
builder.addVariable(GENDST_LONG, mcConfig.getGenDST().orElse(false)); // no transformation infrastructure generation by default
builder.addVariable(GENTAG_LONG, mcConfig.getGenTag().orElse(false)); // no tagging generation by default
builder.addVariable(OUT_LONG, mcConfig.getOut());
builder.addVariable(TOOL_JAR_NAME_LONG, mcConfig.getToolName());
builder.addVariable(REPORT_LONG, mcConfig.getReport());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,13 @@ protected Options initOptions() {
.desc("Specifies if transformation infrastructure should be generated for the given TR grammar.")
.build());

// toggle tagging generation
options.addOption(Option.builder(GENTAG_LONG)
.argName("boolean")
.hasArg(true)
.desc("Specifies if tagging infrastructure should be generated for the given tagging grammar.")
.build());

// help dialog
options.addOption(Option.builder(HELP)
.longOpt(HELP_LONG)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* (c) https://github.com/MontiCore/monticore */
package de.monticore.codegen.cd2java._tagging;

import de.monticore.cdbasis._ast.ASTCDCompilationUnit;
import de.monticore.cdbasis._ast.ASTCDPackage;
import de.monticore.codegen.cd2java.AbstractDecorator;
import de.monticore.generating.templateengine.GlobalExtensionManagement;
import de.se_rwth.commons.Joiners;
import de.se_rwth.commons.Names;

public class CDTaggingDecorator extends AbstractDecorator {
protected final TaggerDecorator taggerDecorator;
protected final TagConformsToSchemaCoCoDecorator coCoDecorator;

public CDTaggingDecorator(GlobalExtensionManagement glex, TaggerDecorator taggerDecorator, TagConformsToSchemaCoCoDecorator coCoDecorator) {
super(glex);
this.taggerDecorator = taggerDecorator;
this.coCoDecorator = coCoDecorator;
}

public void decorate(ASTCDCompilationUnit input, ASTCDCompilationUnit decoratedCD) {
ASTCDPackage taggingPackage = getPackage(input, decoratedCD, TaggingConstants.TAGGING_PACKAGE);

taggingPackage.addAllCDElements(taggerDecorator.decorate());
taggingPackage.addAllCDElements(coCoDecorator.decorate());
}

@Override
protected String getPackageName(ASTCDCompilationUnit origCD, String subPackage) {
String cdDefName = origCD.getCDDefinition().getName();
// Note: We change from the TagDef/TagSchema package into the original grammars package
if (cdDefName.toLowerCase().endsWith(TaggingConstants.TAGDEFINITION_SUFFIX.toLowerCase()))
cdDefName = cdDefName.substring(0, cdDefName.length() - TaggingConstants.TAGDEFINITION_SUFFIX.length());
if (cdDefName.toLowerCase().endsWith(TaggingConstants.TAGSCHEMA_SUFFIX.toLowerCase()))
cdDefName = cdDefName.substring(0, cdDefName.length() - TaggingConstants.TAGSCHEMA_SUFFIX.length());
// end changes
String origPackage = Names.constructQualifiedName(origCD.getCDPackageList(), cdDefName);
return (subPackage.isEmpty() ? origPackage : Joiners.DOT.join(origPackage, subPackage)).toLowerCase();
}
}
Loading

0 comments on commit c9b7afc

Please sign in to comment.