Skip to content

Commit

Permalink
[#241] Merge branch 'markdown' into 'master'
Browse files Browse the repository at this point in the history
  • Loading branch information
mikir committed Sep 28, 2020
2 parents 4e42d3d + 8fd4c30 commit b5bf02a
Show file tree
Hide file tree
Showing 101 changed files with 3,119 additions and 3,893 deletions.
Binary file added 3rdparty/java/autolink-0.10.0.jar
Binary file not shown.
Binary file added 3rdparty/java/commonmark-0.15.2.jar
Binary file not shown.
Binary file added 3rdparty/java/commonmark-ext-autolink-0.15.2.jar
Binary file not shown.
Binary file not shown.
Binary file not shown.
3 changes: 2 additions & 1 deletion compiler/core/antlr/ZserioLexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ WS : [\r\n\f\t ] -> skip ;

// comments
DOC_COMMENT : '/**' .*? '*/' -> channel(DOC) ;
MARKDOWN_COMMENT : '/*!' .*? '!'?'*/' -> channel(DOC) ;
BLOCK_COMMENT : '/*' .*? '*/' -> channel(HIDDEN) ;
LINE_COMMENT : '//' ~[\r\n\f]* -> channel(HIDDEN) ;

Expand All @@ -113,7 +114,7 @@ fragment STRING_CHARACTER
| '\\' ["\\rnft]
| '\\u' HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT
| '\\x' HEX_DIGIT HEX_DIGIT
| '\\0' [0-3]? OCTAL_DIGIT OCTAL_DIGIT?
| '\\0' [0-3]? OCTAL_DIGIT OCTAL_DIGIT?
;
STRING_LITERAL : '"' STRING_CHARACTER* '"' ;

Expand Down
27 changes: 1 addition & 26 deletions compiler/core/src/zserio/ast/Constant.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package zserio.ast;

import zserio.tools.HashUtil;

/**
* AST node for Constants.
*/
public class Constant extends DocumentableAstNode implements Comparable<Constant>
public class Constant extends DocumentableAstNode
{
/**
* Constructor.
Expand Down Expand Up @@ -43,29 +41,6 @@ public void visitChildren(ZserioAstVisitor visitor)
valueExpression.accept(visitor);
}

@Override
public int compareTo(Constant other)
{
return getName().compareTo(other.getName());
}

@Override
public boolean equals(Object other)
{
if ( !(other instanceof Constant) )
return false;

return (this == other) || compareTo((Constant)other) == 0;
}

@Override
public int hashCode()
{
int hash = HashUtil.HASH_SEED;
hash = HashUtil.hash(hash, getName());
return hash;
}

/**
* Gets the package in which this constant is defined.
*
Expand Down
37 changes: 3 additions & 34 deletions compiler/core/src/zserio/ast/DocComment.java
Original file line number Diff line number Diff line change
@@ -1,48 +1,17 @@
package zserio.ast;

import java.util.Collections;
import java.util.List;

/**
* Class representing a single documentation comment.
* Base class representing a single documentation comment.
*/
public class DocComment extends AstNodeBase
public abstract class DocComment extends AstNodeBase
{
/**
* Constructor.
*
* @param location AST node location.
* @param paragraphs Doc comment paragraphs.
*/
public DocComment(AstLocation location, List<DocParagraph> paragraphs)
public DocComment(AstLocation location)
{
super(location);

this.paragraphs = paragraphs;
}

@Override
public void accept(ZserioAstVisitor visitor)
{
visitor.visitDocComment(this);
}

@Override
public void visitChildren(ZserioAstVisitor visitor)
{
for (DocParagraph paragraph : paragraphs)
paragraph.accept(visitor);
}

/**
* Gets doc comment paragraphs.
*
* @return List of paragraphs.
*/
public List<DocParagraph> getParagraphs()
{
return Collections.unmodifiableList(paragraphs);
}

private final List<DocParagraph> paragraphs;
}
2 changes: 1 addition & 1 deletion compiler/core/src/zserio/ast/DocCommentAstBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public Object visitDocComment(DocCommentParser.DocCommentContext ctx)
{
visitChildren(ctx);

return new DocComment(new AstLocation(docCommentToken), paragraphs);
return new DocCommentClassic(new AstLocation(docCommentToken), paragraphs);
}

@Override
Expand Down
48 changes: 48 additions & 0 deletions compiler/core/src/zserio/ast/DocCommentClassic.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package zserio.ast;

import java.util.Collections;
import java.util.List;

/**
* Class representing a single documentation comment in classic style.
*/
public class DocCommentClassic extends DocComment
{
/**
* Constructor.
*
* @param location AST node location.
* @param paragraphs Doc comment paragraphs.
*/
public DocCommentClassic(AstLocation location, List<DocParagraph> paragraphs)
{
super(location);

this.paragraphs = paragraphs;
}

@Override
public void accept(ZserioAstVisitor visitor)
{
visitor.visitDocCommentClassic(this);
}

@Override
public void visitChildren(ZserioAstVisitor visitor)
{
for (DocParagraph paragraph : paragraphs)
paragraph.accept(visitor);
}

/**
* Gets doc comment paragraphs.
*
* @return List of paragraphs.
*/
public List<DocParagraph> getParagraphs()
{
return Collections.unmodifiableList(paragraphs);
}

private final List<DocParagraph> paragraphs;
}
18 changes: 17 additions & 1 deletion compiler/core/src/zserio/ast/DocCommentManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,22 @@ private Token findDocTokenBefore(Token token)
}

private DocComment parseDocComment(Token docCommentToken)
{
if (docCommentToken.getType() == ZserioLexer.MARKDOWN_COMMENT)
return parseDocCommentMarkdown(docCommentToken);
else
return parseDocCommentClassic(docCommentToken);
}

private DocCommentMarkdown parseDocCommentMarkdown(Token docCommentToken)
{
final String markdown = docCommentToken.getText()
.replaceAll("^/\\*!\\s*", "") // strip from beginning
.replaceAll("\\s*!?\\*/$", ""); // strip from the end
return new DocCommentMarkdown(new AstLocation(docCommentToken), markdown);
}

private DocCommentClassic parseDocCommentClassic(Token docCommentToken)
{
try
{
Expand Down Expand Up @@ -173,7 +189,7 @@ private DocComment parseDocComment(Token docCommentToken)
}

final DocCommentAstBuilder docCommentAstBuilder = new DocCommentAstBuilder(docCommentToken);
final DocComment docComment = (DocComment)docCommentAstBuilder.visit(tree);
final DocCommentClassic docComment = (DocCommentClassic)docCommentAstBuilder.visit(tree);

return docComment;
}
Expand Down
43 changes: 43 additions & 0 deletions compiler/core/src/zserio/ast/DocCommentMarkdown.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package zserio.ast;

/**
* Class representing a single documentation comment in markdown style.
*/
public class DocCommentMarkdown extends DocComment
{
/**
* Constructor.
*
* @param location AST node location.
* @param markdown Markdown documentation.
*/
public DocCommentMarkdown(AstLocation location, String markdown)
{
super(location);

this.markdown = markdown;
}

@Override
public void accept(ZserioAstVisitor visitor)
{
visitor.visitDocCommentMarkdown(this);
}

@Override
public void visitChildren(ZserioAstVisitor visitor)
{
}

/**
* Gets markdown documentation.
*
* @return Markdown documentation.
*/
public String getMarkdown()
{
return markdown;
}

final String markdown;
}
2 changes: 1 addition & 1 deletion compiler/core/src/zserio/ast/DocumentableAstNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public DocumentableAstNode(AstLocation location, DocComment docComment)
public void visitChildren(ZserioAstVisitor visitor)
{
if (docComment != null)
visitor.visitDocComment(docComment);
docComment.accept(visitor);
}

/**
Expand Down
6 changes: 5 additions & 1 deletion compiler/core/src/zserio/ast/ZserioAstDefaultVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,11 @@ public void visitInstantiateType(InstantiateType templateInstantiation)
}

@Override
public void visitDocComment(DocComment docComment)
public void visitDocCommentClassic(DocCommentClassic docComment)
{}

@Override
public void visitDocCommentMarkdown(DocCommentMarkdown docComment)
{}

@Override
Expand Down
13 changes: 10 additions & 3 deletions compiler/core/src/zserio/ast/ZserioAstVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -286,11 +286,18 @@ public interface ZserioAstVisitor
void visitInstantiateType(InstantiateType templateInstantiation);

/**
* Visits a documentation comment.
* Visits a classic-style documentation comment.
*
* @param docComment Documentation comment AST node.
* @param docComment Classic-style documentation comment AST node.
*/
void visitDocComment(DocComment docComment);
void visitDocCommentClassic(DocCommentClassic docComment);

/**
* Visits a markdown-style documentation comment.
*
* @param docComment Markdown-style documentation comment AST node.
*/
void visitDocCommentMarkdown(DocCommentMarkdown docComment);

/**
* Visits documentation paragraph.
Expand Down
8 changes: 7 additions & 1 deletion compiler/core/src/zserio/ast/ZserioAstWalker.java
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,13 @@ public void visitInstantiateType(InstantiateType templateInstantiation)
}

@Override
public void visitDocComment(DocComment docComment)
public void visitDocCommentClassic(DocCommentClassic docComment)
{
docComment.visitChildren(this);
}

@Override
public void visitDocCommentMarkdown(DocCommentMarkdown docComment)
{
docComment.visitChildren(this);
}
Expand Down
23 changes: 23 additions & 0 deletions compiler/core/src/zserio/emit/common/FileUtil.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package zserio.emit.common;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

/**
* File utilities for Zserio emitters.
Expand Down Expand Up @@ -29,4 +31,25 @@ public static void createOutputDirectory(File outputFile) throws ZserioEmitExcep
throw new ZserioEmitException("Can't create output directory: " + parentDir.toString());
}
}

/**
* Creates writer for given file.
*
* @param outputFile Output file for which to create writer.
*
* @return Created writer.
*
* @throws ZserioEmitException Throws if writer cannot be created.
*/
public static PrintWriter createWriter(File outputFile) throws ZserioEmitException
{
try
{
return new PrintWriter(outputFile, "UTF-8");
}
catch (IOException exception)
{
throw new ZserioEmitException(exception.getMessage());
}
}
}
Loading

0 comments on commit b5bf02a

Please sign in to comment.