Skip to content
Snippets Groups Projects
Commit 06427258 authored by René Schöne's avatar René Schöne
Browse files

Move grammar2uml to separate repository

- also use mkdocs for pages, include ragdoc
parent b9eff203
No related branches found
No related tags found
1 merge request!60.3.6
Pipeline #12727 failed
This commit is part of merge request !6. Comments created here will be created in the context of that merge request.
Showing
with 42 additions and 832 deletions
......@@ -6,3 +6,4 @@
.idea/
.gradle/
/build/
/public/
......@@ -46,16 +46,52 @@ publish_master:
only:
- master
ragdoc_build:
image:
name: "git-st.inf.tu-dresden.de:4567/jastadd/ragdoc-builder"
entrypoint: [""]
stage: ragdoc_build
needs:
- build
script:
- JAVA_FILES=$(find dumpAstWithPlantuml/src/ -name '*.java')
- /ragdoc-builder/start-builder.sh -excludeGenerated -d data/ $JAVA_FILES
artifacts:
paths:
- "data/"
ragdoc_view:
image:
name: "git-st.inf.tu-dresden.de:4567/jastadd/ragdoc-view:relations"
entrypoint: [""]
stage: ragdoc_view
needs:
- ragdoc_build
script:
- DATA_DIR=$(pwd -P)/data
- mkdir -p pages/docs/ragdoc
- OUTPUT_DIR=$(pwd -P)/pages/docs/ragdoc
- cd /ragdoc-view/src/ && rm -rf data && ln -s $DATA_DIR
- /ragdoc-view/build-view.sh --output-path=$OUTPUT_DIR
only:
- dev
- main
artifacts:
paths:
- "pages/docs/ragdoc"
pages:
image: python:3.7-alpine
image: python:3.10.0-bullseye
stage: publish
needs:
- ragdoc_view
- test
before_script:
- pip install -r pages/requirements.txt
script:
- "pip install -U sphinx sphinx-rtd-theme recommonmark sphinxemoji sphinx-markdown-tables"
- "sphinx-build -b html pages/ public"
- cd pages && mkdocs build
artifacts:
paths:
- public
- public/
only:
- master
- main
build
src/gen-res/
src/gen/
out/
*.class
buildscript {
repositories.mavenLocal()
repositories.mavenCentral()
dependencies {
classpath group: 'org.jastadd', name: 'jastaddgradle', version: '1.13.3'
}
}
plugins {
id 'relast2uml.java-jastadd-conventions'
id 'relast2uml.java-application-conventions'
id 'relast2uml.java-publishing-conventions'
}
apply plugin: 'jastadd'
application.mainClassName = 'de.tudresden.inf.st.jastadd.grammar2uml.compiler.Compiler'
jar {
manifest {
attributes "Main-Class": application.mainClassName
}
}
dependencies {
implementation project(':relast.preprocessor')
implementation group: 'com.github.spullara.mustache.java', name: 'compiler', version: "${mustache_java_version}"
runtime group: 'org.jastadd', name: 'jastadd', version: '2.3.4'
}
File preprocessorGrammar = file('../relast.preprocessor/src/main/jastadd/RelAst.relast')
File grammar2umlGrammar = file('./src/main/jastadd/Grammar2Uml.relast')
File intermediateGrammar = file('./src/main/jastadd/MustacheNodes.relast')
task relast(type: JavaExec) {
group = 'Build'
main = "-jar"
doFirst {
delete "src/gen/jastadd/*.ast"
delete "src/gen/jastadd/Grammar2Uml.jadd"
delete "src/gen/jastadd/Grammar2UmlRefResolver.jadd"
delete "src/gen/jastadd/Grammar2UmlResolverStubs.jrag"
mkdir "src/gen/jastadd/"
}
args = [
"../libs/relast.jar",
preprocessorGrammar,
grammar2umlGrammar,
intermediateGrammar,
"--listClass=java.util.ArrayList",
"--jastAddList=JastAddList",
"--useJastAddNames",
"--file",
"--resolverHelper",
"--grammarName=./src/gen/jastadd/Grammar2Uml"
]
inputs.files(file("../libs/relast.jar"),
preprocessorGrammar,
grammar2umlGrammar,
intermediateGrammar)
outputs.files(file("./src/gen/jastadd/Grammar2Uml.ast"),
file("./src/gen/jastadd/Grammar2Uml.jadd"),
file("./src/gen/jastadd/Grammar2UmlRefResolver.jadd"),
file('./src/gen/jastadd/Grammar2UmlResolverStubs.jrag'))
}
jastadd {
configureModuleBuild()
modules {
//noinspection GroovyAssignabilityCheck
module("Grammar2Uml") {
java {
basedir ".."
include "relast.preprocessor/main/**/*.java"
include "relast.preprocessor/gen/**/*.java"
include "grammar2uml/src/main/**/*.java"
include "grammar2uml/src/gen/**/*.java"
}
jastadd {
basedir ".."
include "relast.preprocessor/src/main/jastadd/**/*.ast"
include "relast.preprocessor/src/main/jastadd/**/*.jadd"
include "relast.preprocessor/src/main/jastadd/**/*.jrag"
include "grammar2uml/src/main/jastadd/**/*.ast"
include "grammar2uml/src/main/jastadd/**/*.jadd"
include "grammar2uml/src/main/jastadd/**/*.jrag"
include "grammar2uml/src/gen/jastadd/**/*.ast"
include "grammar2uml/src/gen/jastadd/**/*.jadd"
include "grammar2uml/src/gen/jastadd/**/*.jrag"
}
scanner {
basedir ".."
include "grammar2uml/src/main/jastadd/scanner/Header.flex", [-5]
include "relast.preprocessor/src/main/jastadd/scanner/Preamble.flex", [-4]
include "relast.preprocessor/src/main/jastadd/scanner/Macros.flex", [-3]
include "grammar2uml/src/main/jastadd/scanner/Macros.flex", [-3]
include "relast.preprocessor/src/main/jastadd/scanner/RulesPreamble.flex", [-2]
include "grammar2uml/src/main/jastadd/scanner/MappingContent.flex", [-1]
include "grammar2uml/src/main/jastadd/scanner/Keywords.flex"
include "relast.preprocessor/src/main/jastadd/scanner/Keywords.flex"
include "relast.preprocessor/src/main/jastadd/scanner/Symbols.flex", [1]
include "relast.preprocessor/src/main/jastadd/scanner/RulesPostamble.flex", [2]
}
parser {
basedir ".."
include "grammar2uml/src/main/jastadd/parser/Preamble.parser"
include "relast.preprocessor/src/main/jastadd/parser/RelAst.parser"
include "grammar2uml/src/main/jastadd/parser/Grammar2Uml.parser"
}
}
}
cleanGen.doFirst {
delete "src/gen/java/de"
delete "src/gen-res/BuildInfo.properties"
}
preprocessParser.doFirst {
args += ["--no-beaver-symbol"]
}
module = "Grammar2Uml"
astPackage = 'de.tudresden.inf.st.jastadd.grammar2uml.ast'
parser.name = 'Grammar2UmlParser'
genDir = 'src/gen/java'
buildInfoDir = 'src/gen-res'
scanner.genDir = "src/gen/java/de/tudresden/inf/st/jastadd/grammar2uml/scanner"
parser.genDir = "src/gen/java/de/tudresden/inf/st/jastadd/grammar2uml/parser"
jastaddOptions = ["--lineColumnNumbers", "--List=JastAddList", "--safeLazy", "--visitCheck=true", "--rewrite=cnta", "--cache=all"]
}
generateAst.dependsOn relast
aspect Analysis {
syn lazy Folder GrammarFile.defaultFolder() {
Folder result = new Folder();
result.setName(new java.io.File(getFileName()).getName().replace(".relast", "").replace(".ast", ""));
for (Declaration decl : getDeclarationList()) {
if (decl.isTypeDecl() && !decl.asTypeDecl().hasSourceFolder()) {
result.addType(decl.asTypeDecl());
}
}
return result;
}
}
import java.util.Set;
import java.util.TreeSet;
import java.util.LinkedList;
aspect Errors {
coll Set<ErrorMessage> Grammar2Uml.errors()
[new TreeSet<ErrorMessage>()]
root Grammar2Uml;
}
aspect ErrorMessage {
public class ErrorMessage implements Comparable<ErrorMessage> {
private final ASTNode node;
private final String filename;
private final int line;
private final int col;
private final String message;
public ErrorMessage(ASTNode node, String message) {
this.node = node;
this.filename = node.containedFileName();
this.line = node.getStartLine();
this.col = node.getStartColumn();
this.message = message;
}
public ASTNode getNode() {
return node;
}
public int getLine() {
return line;
}
public int getCol() {
return col;
}
public String getMessage() {
return message;
}
public String toString() {
return filename + " Line " + line + ", column " + col + ": " + message;
}
@Override
public int compareTo(ErrorMessage err) {
int n = filename.compareTo(err.filename);
if (n != 0) {
return n;
}
n = line - err.line;
if (n != 0) {
return n;
}
n = col-err.col;
if (n != 0) {
return n;
}
return message.compareTo(err.message);
}
}
protected ErrorMessage ASTNode.error(String message) {
return new ErrorMessage(this, message);
}
}
Grammar2Uml ::= Program <FileName> Folder* ;
Folder ::= <Name:String> ;
rel Folder.Type* <-> TypeDecl.SourceFolder?;
MGrammar2Uml ::= Folder:MFolder* OtherType:MTypeDecl* Containment:MContainment* Relation:MRelation* Inheritance:MInheritance* ;
MFolder ::= InnerTypeDecl:MTypeDecl*;
MTypeDecl ::= InnerTokenComponent:MTokenComponent*;
MTokenComponent;
abstract MContainment ::= <Label:String> ;
MSingleContainment : MContainment;
MOptContainment : MContainment;
MListContainment : MContainment;
MRelation ::= <Label> <LeftModifier> <RightModifier> <Bidirectional:boolean>;
MInheritance ;
rel MGrammar2Uml.Grammar2Uml -> Grammar2Uml;
rel MFolder.Folder -> Folder;
rel MTypeDecl.Type -> TypeDecl;
rel MTokenComponent.Token -> TokenComponent;
rel MContainment.Type -> TypeDecl;
rel MContainment.Component -> TypeDecl;
rel MRelation.Left -> TypeDecl;
rel MRelation.Right -> TypeDecl;
rel MInheritance.SuperClass -> TypeDecl;
rel MInheritance.SubClass -> TypeDecl;
aspect NameResolutionExtended {
refine NameResolution eq Program.resolveTypeDecl(String name) {
try {
return refined(name);
} catch (RuntimeException e) {
System.err.println(e.getMessage());
TypeDecl dummy = new TypeDecl();
dummy.setName("(" + name + ")");
return dummy;
}
}
}
aspect Navigation {
// --- program ---
eq Grammar2Uml.getChild().program() = getProgram();
eq MGrammar2Uml.getChild().program() = getGrammar2Uml().program();
// --- grammar2uml ---
inh Grammar2Uml ASTNode.grammar2uml();
eq Grammar2Uml.getChild().grammar2uml() = this;
eq MGrammar2Uml.getChild().grammar2uml() = getGrammar2Uml();
// --- containedFile ---
eq Grammar2Uml.getChild().containedFile() = null;
eq MGrammar2Uml.getChild().containedFile() = null;
// --- containedFileName ---
eq Grammar2Uml.getChild().containedFileName() = getFileName();
eq Program.getChild().containedFileName() = null;
eq Grammar.getChild().containedFileName() = null;
eq MGrammar2Uml.getChild().containedFileName() = null;
}
aspect Configuration {
}
aspect GenerationUtils {
public static final String ASTNode.aspectIndent = " ";
public String ASTNode.ind(int n) {
StringBuilder s = new StringBuilder();
for (int i = 0; i < n; i++) {
s.append(aspectIndent);
}
return s.toString();
}
// --- prettyPrint ---
syn String JavaTypeUse.prettyPrint() {
StringBuilder sb = new StringBuilder();
generateAbstractGrammar(sb);
return sb.toString();
}
}
aspect AttributesForMustache {
// --- MFolder ---
syn String MFolder.name() = getFolder().getName();
// --- MTypeDecl ---
eq MTypeDecl.getInnerTokenComponent(int i).isFirst() = i == 0;
eq MTypeDecl.getInnerTokenComponent(int i).isLast() = i == getNumInnerTokenComponent() - 1;
syn boolean MTypeDecl.isAbstract() = getType().getAbstract();
syn String MTypeDecl.name() = getType().getName();
// --- MTokenComponent ---
syn String MTokenComponent.name() = getToken().getName();
inh boolean MTokenComponent.isFirst();
inh boolean MTokenComponent.isLast();
// --- MContainment ---
syn String MContainment.typeName() = getType().getName();
syn String MContainment.componentName() = getComponent().getName();
syn String MContainment.modifier();
eq MSingleContainment.modifier() = "\"1\"";
eq MOptContainment.modifier() = "\"0 .. 1\"";
eq MListContainment.modifier() = "\"*\"";
// --- MRelation ---
syn String MRelation.leftName() = getLeft().getName();
syn String MRelation.rightName() = getRight().getName();
syn boolean MRelation.isBidirectional() = getBidirectional();
// syn String MRelation.modifier();
// eq MSingleRelation.modifier() = "\"1\"";
// eq MOptRelation.modifier() = "\"0 .. 1\"";
// eq MListRelation.modifier() = "\"*\"";
// --- MInheritance ---
syn String MInheritance.superClassName() = getSuperClass().getName();
syn String MInheritance.subClassName() = getSubClass().getName();
// --- toMContainment ---
syn MContainment TypeComponent.toMContainment();
eq NormalComponent.toMContainment() = new MSingleContainment();
eq ListComponent.toMContainment() = new MListContainment();
eq OptComponent.toMContainment() = new MOptContainment();
// --- toMRelation ---
syn MRelation Relation.toMRelation();
eq DirectedRelation.toMRelation() {
MRelation result = new MRelation();
result.setRightModifier(getSource().toMRelationModifier());
result.setBidirectional(false);
return result;
}
eq BidirectionalRelation.toMRelation() {
MRelation result = new MRelation();
result.setLeftModifier(getRight().toMRelationModifier());
result.setRightModifier(getLeft().toMRelationModifier());
result.setBidirectional(true);
return result;
}
// --- toMRelationModifier ---
syn String Role.toMRelationModifier();
eq NormalRole.toMRelationModifier() = "\"1\"";
eq ListRole.toMRelationModifier() = "\"0 .. 1\"";
eq OptRole.toMRelationModifier() = "\"*\"";
eq UnnamedRole.toMRelationModifier() {
throw new RuntimeException("UnnamedRole cannot be converted to MRelation");
}
// --- toMustache ---
syn lazy MGrammar2Uml Grammar2Uml.toMustache() {
MGrammar2Uml result = new MGrammar2Uml();
result.setGrammar2Uml(this);
for (Folder folder : getFolderList()) {
result.addFolder(folder.toMustache());
}
for (TypeDecl typeDecl : getProgram().typeDecls()) {
if (!typeDecl.hasSourceFolder()) {
result.addOtherType(typeDecl.toMustache());
}
for (Component component : typeDecl.getComponentList()) {
if (component.isTypeComponent()) {
TypeComponent typeComponent = component.asTypeComponent();
MContainment containment = typeComponent.toMContainment();
containment.setType(typeDecl);
containment.setComponent(component.asTypeComponent().getTypeDecl());
if (!component.getName().isEmpty() && !component.getName().equals(component.asTypeComponent().getTypeDecl().getName())) {
containment.setLabel(component.getName());
}
result.addContainment(containment);
}
}
if (typeDecl.hasSuperType()) {
MInheritance inheritance = new MInheritance();
inheritance.setSuperClass(typeDecl.getSuperType());
inheritance.setSubClass(typeDecl);
result.addInheritance(inheritance);
}
}
for (Relation relation : getProgram().relations()) {
if (relation.isDirectedRelation()) {
DirectedRelation directedRelation = relation.asDirectedRelation();
MRelation mRelation = directedRelation.toMRelation();
mRelation.setLeft(directedRelation.getSource().getType());
mRelation.setRight(directedRelation.getTarget().getType());
mRelation.setLabel(directedRelation.getSource().getName());
result.addRelation(mRelation);
} else {
BidirectionalRelation bidiRelation = relation.asBidirectionalRelation();
MRelation mRelation = bidiRelation.toMRelation();
mRelation.setLeft(bidiRelation.getLeft().getType());
mRelation.setRight(bidiRelation.getRight().getType());
// mRelation.setLabel(bidiRelation.getSource().getName());
result.addRelation(mRelation);
}
}
return result;
}
syn lazy MFolder Folder.toMustache() {
MFolder result = new MFolder();
result.setFolder(this);
for (TypeDecl typeDecl : getTypeList()) {
result.addInnerTypeDecl(typeDecl.toMustache());
}
return result;
}
syn lazy MTypeDecl TypeDecl.toMustache() {
MTypeDecl result = new MTypeDecl();
result.setType(this);
for (Component component : getComponentList()) {
if (component.isTokenComponent()) {
result.addInnerTokenComponent(component.asTokenComponent().toMustache());
}
}
return result;
}
syn lazy MTokenComponent TokenComponent.toMustache() {
MTokenComponent result = new MTokenComponent();
result.setToken(this);
return result;
}
}
aspect AspectGeneration {
syn String Grammar2Uml.generateAspect() = toMustache().generateAspect();
syn String MGrammar2Uml.generateAspect() {
StringBuilder sb = new StringBuilder();
com.github.mustachejava.reflect.ReflectionObjectHandler roh = new com.github.mustachejava.reflect.ReflectionObjectHandler() {
@Override
public com.github.mustachejava.Binding createBinding(String name, final com.github.mustachejava.TemplateContext tc, com.github.mustachejava.Code code) {
return new com.github.mustachejava.reflect.GuardedBinding(this, name, tc, code) {
@Override
protected synchronized com.github.mustachejava.util.Wrapper getWrapper(String name, java.util.List<Object> scopes) {
com.github.mustachejava.util.Wrapper wrapper = super.getWrapper(name, scopes);
if (wrapper instanceof com.github.mustachejava.reflect.MissingWrapper) {
throw new com.github.mustachejava.MustacheException(name + " not found in " + tc);
}
return wrapper;
}
};
}
};
com.github.mustachejava.DefaultMustacheFactory mf = new com.github.mustachejava.DefaultMustacheFactory();
// mf.setObjectHandler(roh);
com.github.mustachejava.Mustache m = mf.compile("grammar2uml.mustache");
m.execute(new java.io.PrintWriter(new AppendableWriter(sb)), this);
return sb.toString();
}
public class AppendableWriter extends java.io.Writer {
private final StringBuilder sb;
public AppendableWriter(StringBuilder sb) {
this.sb = sb;
}
@Override
public void write(char[] chars, int off, int len) {
sb.append(chars, off, len);
}
@Override
public void write(String str) {
sb.append(str);
}
@Override
public void flush() {}
@Override
public void close() {}
}
}
Grammar2Uml grammar2uml
= folder.f grammar2uml.r {: r.getFolderList().insertChild(f, 0); return r; :}
| {: return new Grammar2Uml(); :}
;
Folder folder
= FOLDER ID.name COL type_use_list.t
{:
Folder result = new Folder();
result.setName(name);
java.util.List<TypeDecl> typeDecls = ((ArrayList<Object>) t).stream().map(x -> TypeDecl.createRef(((Symbol) x).value.toString())).collect(java.util.stream.Collectors.toList());
java.util.Collections.reverse(typeDecls);
for (TypeDecl typeDecl : typeDecls) {
result.addType(typeDecl);
}
return result;
:}
;
ArrayList type_use_list
= ID
| type_use_list COMMA ID
;
%header {:
package de.tudresden.inf.st.jastadd.grammar2uml.parser;
import de.tudresden.inf.st.jastadd.grammar2uml.ast.*;
:};
%goal goal;
%goal grammar2uml;
package de.tudresden.inf.st.jastadd.grammar2uml.scanner;
import de.tudresden.inf.st.jastadd.grammar2uml.parser.Grammar2UmlParser.Terminals;
%%
%public
%final
%class Grammar2UmlScanner
%extends beaver.Scanner
%type beaver.Symbol
%function nextToken
%yylexthrow beaver.Scanner.Exception
%scanerror Grammar2UmlScanner.ScannerError
%x COMMENT
%s DECLARATION
%line
%column
"folder" { yybegin(DECLARATION); return sym(Terminals.FOLDER); }
//MappingContent = [{][:][^:]*[:]+([^:}][^:]*[:]+)*[}]
ID = [:jletter:][:jletterdigit:]*
//{MappingContent} { return sym(Terminals.MAPPING_CONTENT); }
package de.tudresden.inf.st.jastadd.grammar2uml.compiler;
import beaver.Parser;
import org.jastadd.option.BooleanOption;
import org.jastadd.option.ValueOption;
import org.jastadd.relast.compiler.AbstractCompiler;
import org.jastadd.relast.compiler.CompilerException;
import de.tudresden.inf.st.jastadd.grammar2uml.ast.ErrorMessage;
import de.tudresden.inf.st.jastadd.grammar2uml.ast.GrammarFile;
import de.tudresden.inf.st.jastadd.grammar2uml.ast.Program;
import de.tudresden.inf.st.jastadd.grammar2uml.ast.Grammar2Uml;
import de.tudresden.inf.st.jastadd.grammar2uml.parser.Grammar2UmlParser;
import de.tudresden.inf.st.jastadd.grammar2uml.scanner.Grammar2UmlScanner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
public class Compiler extends AbstractCompiler {
private ValueOption optionOutputFile;
private ValueOption optionInputGrammar2Uml;
private BooleanOption optionDefaultFolders;
private BooleanOption optionHelp;
private BooleanOption optionVersion;
private BooleanOption optionVerbose;
public Compiler() {
super("grammar2uml", false);
}
/**
* Reads the version string.
*
* The version string is read from the property file
* src/main/resources/Version.properties. This
* file should be generated during the build process. If it is missing
* then there is some problem in the build script.
*
* @author Jesper Öqvist <jesper.oqvist@cs.lth.se>
* @return the read version string, or <code>version ?</code>
*/
private String readVersion() {
try {
return ResourceBundle.getBundle("grammar2umlVersion").getString("version");
} catch (MissingResourceException e) {
return "version ?";
}
}
public static void main(String[] args) {
System.setProperty("mustache.debug", "true");
try {
new Compiler().run(args);
} catch (CompilerException e) {
System.err.println(e.getMessage());
System.exit(-1);
}
}
private void printMessage(String message) {
System.out.println(message);
}
private void writeToFile(String filename, String str) throws CompilerException {
try {
PrintWriter writer = new PrintWriter(filename);
writer.print(str);
writer.close();
} catch (Exception e) {
throw new CompilerException("Could not write to file " + filename, e);
}
}
protected void initOptions() {
optionOutputFile = addOption(
new ValueOption("output", "target file to be generated.")
.defaultValue("uml.md")
.acceptAnyValue()
.needsValue(false));
optionInputGrammar2Uml = addOption(
new ValueOption("inputGrammar2Uml", "grammar2uml definition file.")
.needsValue(true));
optionDefaultFolders = addOption(
new BooleanOption("defaultFolders",
"Creates a default folder per grammar file.")
.defaultValue(false));
optionHelp = addOption(
new BooleanOption("help", "Print usage and exit.")
.defaultValue(false));
optionVersion = addOption(
new BooleanOption("version", "Print version and exit.")
.defaultValue(false));
optionVerbose = addOption(
new BooleanOption("verbose", "Print more messages while compiling.")
.defaultValue(false));
}
private Grammar2Uml parseProgram() throws CompilerException {
Program program = new Program();
Grammar2Uml grammar2Uml;
for (String inputGrammarFileName : getConfiguration().getFiles()) {
printMessage("Parsing " + inputGrammarFileName);
GrammarFile inputGrammar;
try (BufferedReader reader = Files.newBufferedReader(Paths.get(inputGrammarFileName))) {
Grammar2UmlScanner scanner = new Grammar2UmlScanner(reader);
Grammar2UmlParser parser = new Grammar2UmlParser();
inputGrammar = (GrammarFile) parser.parse(scanner);
if (optionVerbose.value()) {
inputGrammar.dumpTree(System.out);
}
program.addGrammarFile(inputGrammar);
inputGrammar.setFileName(inputGrammarFileName);
} catch (IOException | Parser.Exception e) {
throw new CompilerException("Could not parse grammar file " + inputGrammarFileName, e);
}
}
if (optionInputGrammar2Uml.isMatched()) {
String inputGrammar2UmlFileName = optionInputGrammar2Uml.value();
try (BufferedReader reader = Files.newBufferedReader(Paths.get(inputGrammar2UmlFileName))) {
Grammar2UmlScanner scanner = new Grammar2UmlScanner(reader);
Grammar2UmlParser parser = new Grammar2UmlParser();
grammar2Uml = (Grammar2Uml) parser.parse(scanner, Grammar2UmlParser.AltGoals.grammar2uml);
grammar2Uml.setFileName(inputGrammar2UmlFileName);
} catch (IOException | Parser.Exception e) {
throw new CompilerException("Could not parse grammar2uml file " + inputGrammar2UmlFileName, e);
}
} else {
// no special setting given
grammar2Uml = new Grammar2Uml();
grammar2Uml.setFileName("<none>");
}
grammar2Uml.setProgram(program);
grammar2Uml.treeResolveAll();
if (optionDefaultFolders.value()) {
for (GrammarFile grammarFile : program.getGrammarFileList()) {
grammar2Uml.addFolder(grammarFile.defaultFolder());
}
}
return grammar2Uml;
}
@Override
protected int compile() throws CompilerException {
if (optionVersion.value()) {
System.out.println(readVersion());
return 0;
}
if (optionHelp.value()) {
getConfiguration().printHelp(System.out);
return 0;
}
printMessage("Running grammar2uml " + readVersion());
String output;
if (optionOutputFile.isMatched()) {
output = optionOutputFile.value();
} else {
output = "uml.md";
System.out.println("No output output file is set. Assuming '" + output + "'.");
}
Path parent = Paths.get(optionOutputFile.value()).toAbsolutePath().getParent();
try {
Files.createDirectories(parent);
} catch (IOException e) {
throw new CompilerException("Error creating output dir " + parent, e);
}
if (getConfiguration().getFiles().isEmpty()) {
throw new CompilerException("No input grammars specified!");
}
Grammar2Uml grammar2uml = parseProgram();
if (!grammar2uml.errors().isEmpty()) {
System.err.println("Errors:");
for (ErrorMessage e : grammar2uml.errors()) {
System.err.println(e);
}
System.exit(1);
}
printMessage("Writing output file " + output);
writeToFile(output, grammar2uml.generateAspect());
return 0;
}
}
package de.tudresden.inf.st.jastadd.grammar2uml.compiler;
import beaver.Parser;
import de.tudresden.inf.st.jastadd.grammar2uml.ast.*;
import de.tudresden.inf.st.jastadd.grammar2uml.parser.Grammar2UmlParser;
import de.tudresden.inf.st.jastadd.grammar2uml.scanner.Grammar2UmlScanner;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
* Testing Grammar2Uml without parser.
*
* @author rschoene - Initial contribution
*/
public class Grammar2UmlMain {
public static void main(String[] args) {
// testing();
processManualAST();
}
public static Grammar2Uml createManualAST() {
System.setProperty("java.util.logging.manager", "org.apache.logging.log4j.jul.LogManager");
System.setProperty("mustache.debug", "true");
Grammar2Uml model = new Grammar2Uml();
Path path = Paths.get("grammar2uml.base", "src", "test", "resources", "Example.relast");
System.out.println("path.toFile().getAbsolutePath() = " + path.toFile().getAbsolutePath());
Program program = parseProgram(path);
model.setProgram(program);
Folder folder1 = new Folder();
folder1.setName("Folder1");
folder1.addType(program.resolveTypeDecl("Wert"));
folder1.addType(program.resolveTypeDecl("Quelle"));
model.addFolder(folder1);
return model;
}
private static void processManualAST() {
Grammar2Uml model = createManualAST();
model.treeResolveAll();
for (Folder f : model.getFolderList()) {
System.out.println(f.getName() + ":");
for (TypeDecl typeDecl : f.getTypeList()) {
System.out.println(typeDecl.getName());
}
}
System.out.println(model.generateAspect());
}
public static Program parseProgram(Path path) {
try (BufferedReader reader = Files.newBufferedReader(path)) {
Grammar2UmlScanner scanner = new Grammar2UmlScanner(reader);
Grammar2UmlParser parser = new Grammar2UmlParser();
GrammarFile grammarFile = (GrammarFile) parser.parse(scanner);
Program program = new Program();
program.addGrammarFile(grammarFile);
return program;
} catch (IOException | Parser.Exception e) {
e.printStackTrace();
}
return null;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment