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

compile types to xml #151

Merged
merged 2 commits into from
Feb 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions eo-compiler/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,21 @@ SOFTWARE.
<version>2.5.0-beta-1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jcabi</groupId>
<artifactId>jcabi-xml</artifactId>
<version>0.21.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jcabi.incubator</groupId>
<artifactId>xembly</artifactId>
<version>0.22</version>
</dependency>
<dependency>
<groupId>com.jcabi</groupId>
<artifactId>jcabi-matchers</artifactId>
<version>1.4</version>
</dependency>
</dependencies>
</project>
21 changes: 13 additions & 8 deletions eo-compiler/src/main/java/org/eolang/compiler/Program.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public final class Program {

/**
* Ctor.
*
* @param ipt Input text
* @param dir Directory to write to
*/
Expand All @@ -79,6 +80,7 @@ public Program(final Input ipt, final Path dir) {

/**
* Ctor.
*
* @param ipt Input text
* @param tgt Target
*/
Expand All @@ -89,6 +91,7 @@ public Program(final Input ipt, final Func<String, Output> tgt) {

/**
* Compile it to Java and save.
*
* @throws IOException If fails
*/
public void compile() throws IOException {
Expand All @@ -109,28 +112,30 @@ public void syntaxError(final Recognizer<?, ?> recognizer,
);
}
};
final ProgramLexer lexer = new ProgramLexer(
CharStreams.fromStream(this.input.stream())
);
final org.eolang.compiler.ProgramLexer lexer =
new org.eolang.compiler.ProgramLexer(
CharStreams.fromStream(this.input.stream())
);
lexer.removeErrorListeners();
lexer.addErrorListener(errors);
final ProgramParser parser = new ProgramParser(
new CommonTokenStream(lexer)
);
final org.eolang.compiler.ProgramParser parser =
new org.eolang.compiler.ProgramParser(
new CommonTokenStream(lexer)
);
parser.removeErrorListeners();
parser.addErrorListener(errors);
final Tree tree = parser.program().ret;
new IoCheckedScalar<>(
new And(
tree.java().entrySet(),
path -> {
new LengthOf(
new TeeInput(
path.getValue(),
this.target.apply(path.getKey())
)
).value();
}
},
tree.java().entrySet()
)
).value();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ public Input code() {
new JoinedText(
"\n ",
new Mapped<>(
this.methods,
mtd -> new UncheckedText(
new FormattedText("%s;", mtd.java())
).asString()
).asString(),
this.methods
)
)
).asString().replace("\n", "\n ")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,21 +89,21 @@ public String code() {
new JoinedText(
", ",
new Mapped<>(
this.attributes,
attr -> attr.java(
PrimaryConstructor.CTOR_PARAM_FORMAT
)
),
this.attributes
)
)
).asString(),
new UncheckedText(
new JoinedText(
"\n",
new Mapped<>(
this.attributes,
attr -> attr.java(
PrimaryConstructor.CTOR_INIT_FORMAT
)
),
this.attributes
)
)
).asString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
*/
package org.eolang.compiler.syntax;

import org.eolang.compiler.xml.XmlAttribute;
import org.xembly.Directive;

/**
* Object attribute.
*
Expand Down Expand Up @@ -62,4 +65,12 @@ public Attribute(final String type, final String name) {
public String java(final AttributeFormat format) {
return format.code(this.type, this.name);
}

/**
* As XML.
* @return Directives
*/
public Iterable<Directive> xml() {
return new XmlAttribute(this.name, this.type);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public CpObject(final String obj, final List<Argument> args) {

/**
* Object copying arguments.
*
* @return An argument list
*/
public List<Argument> arguments() {
Expand All @@ -80,7 +81,7 @@ public String java() {
new UncheckedText(
new JoinedText(
", ",
new Mapped<>(this.args, Argument::java)
new Mapped<>(Argument::java, this.args)
)
).asString()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ public Ctor(
new UncheckedText(
new JoinedText(
", ",
new Mapped<>(parameters, Parameter::java)
new Mapped<>(Parameter::java, parameters)
)
).asString(),
new UncheckedText(
new JoinedText(
", ",
new Mapped<>(arguments, Argument::java)
new Mapped<>(Argument::java, arguments)
)
).asString()
)
Expand Down
15 changes: 13 additions & 2 deletions eo-compiler/src/main/java/org/eolang/compiler/syntax/Method.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import org.cactoos.text.FormattedText;
import org.cactoos.text.JoinedText;
import org.cactoos.text.UncheckedText;
import org.eolang.compiler.xml.XmlMethodDef;
import org.xembly.Directive;

/**
* Method.
Expand Down Expand Up @@ -55,6 +57,7 @@ public final class Method {

/**
* Ctor.
*
* @param mtd Method name
* @param params Parameters
* @param rtp Return type
Expand All @@ -68,6 +71,7 @@ public Method(final String mtd, final Collection<Parameter> params,

/**
* Convert it to Java.
*
* @return Java code
*/
public String java() {
Expand All @@ -80,13 +84,20 @@ public String java() {
new JoinedText(
", ",
new Mapped<>(
this.parameters,
Parameter::java
Parameter::java,
this.parameters
)
)
).asString()
)
).asString();
}

/**
* As XML.
* @return Directives
*/
public Iterable<Directive> xml() {
return new XmlMethodDef(this.name, this.type, this.parameters);
}
}
17 changes: 17 additions & 0 deletions eo-compiler/src/main/java/org/eolang/compiler/syntax/Object.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import java.util.Collection;
import org.eolang.compiler.java.JavaClass;
import org.eolang.compiler.java.JavaFile;
import org.xembly.Directive;
import org.xembly.Directives;

/**
* EO Object.
Expand Down Expand Up @@ -80,4 +82,19 @@ public JavaFile java() {
this.body
);
}

@Override
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public Iterable<Directive> xml() {
final Directives dir = new Directives()
.add("object")
.attr("name", this.name);
dir.add("types");
for (final String type : this.types) {
dir.add("type").attr("name", type).up();
}
dir.up();
dir.append(this.body.xml());
return dir.up();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import org.cactoos.text.JoinedText;
import org.cactoos.text.UncheckedText;
import org.eolang.compiler.java.PrimaryConstructor;
import org.xembly.Directive;
import org.xembly.Directives;

/**
* Object body.
Expand Down Expand Up @@ -90,25 +92,38 @@ public String java(final String name) {
new JoinedText(
"\n",
new Mapped<>(
this.attrs,
attr -> attr.java(ObjectBody.FIELD_FORMAT)
attr -> attr.java(ObjectBody.FIELD_FORMAT),
this.attrs
)
)
).asString(),
new UncheckedText(
new JoinedText(
"\n",
new Mapped<>(this.ctors, ctor -> ctor.java(name))
new Mapped<>(ctor -> ctor.java(name), this.ctors)
)
).asString(),
new PrimaryConstructor(name, this.attrs).code(),
new UncheckedText(
new JoinedText(
"\n",
new Mapped<>(this.methods, MethodImpl::java)
new Mapped<>(MethodImpl::java, this.methods)
)
).asString()
)
).asString();
}

/**
* As XML.
* @return Directives
*/
public Iterable<Directive> xml() {
final Directives dir = new Directives();
dir.add("attributes");
for (final Attribute attr : this.attrs) {
dir.append(attr.xml());
}
return dir;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

import org.cactoos.text.FormattedText;
import org.cactoos.text.UncheckedText;
import org.eolang.compiler.xml.XmlParam;
import org.xembly.Directive;

/**
* Parameter.
Expand Down Expand Up @@ -69,4 +71,11 @@ public String java() {
).asString();
}

/**
* As XML.
* @return Directives
*/
public Iterable<Directive> xml() {
return new XmlParam(this.name, this.type);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package org.eolang.compiler.syntax;

import org.eolang.compiler.java.JavaFile;
import org.xembly.Directive;

/**
* AST root node. A part of {@link Tree}.
Expand All @@ -40,4 +41,10 @@ public interface RootNode {
* @return Java code and path
*/
JavaFile java();

/**
* As xml.
* @return Directives
*/
Iterable<Directive> xml();
}
21 changes: 17 additions & 4 deletions eo-compiler/src/main/java/org/eolang/compiler/syntax/Tree.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
import java.util.Collection;
import java.util.Map;
import org.cactoos.Input;
import org.cactoos.iterable.MapEntry;
import org.cactoos.iterable.Mapped;
import org.cactoos.iterable.StickyMap;
import org.cactoos.map.MapEntry;
import org.cactoos.map.StickyMap;
import org.eolang.compiler.xml.XmlModule;
import org.xembly.Directive;

/**
* AST.
Expand All @@ -46,6 +48,7 @@ public final class Tree {

/**
* Ctor.
*
* @param list All AST root nodes.
*/
public Tree(final Collection<RootNode> list) {
Expand All @@ -54,16 +57,26 @@ public Tree(final Collection<RootNode> list) {

/**
* Compile it to Java files.
*
* @return Java files (path, content)
*/
public Map<String, Input> java() {
return new StickyMap<>(
new Mapped<>(
new Mapped<>(this.nodes, RootNode::java),
javaFile -> new MapEntry<>(
javaFile.path(), javaFile.code()
)
),
new Mapped<>(RootNode::java, this.nodes)
)
);
}

/**
* Compile syntax tree to xml.
*
* @return XML
*/
public Iterable<Directive> xml() {
return new XmlModule(this.nodes);
}
}