Skip to content

Commit

Permalink
#2230: formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Jul 11, 2023
1 parent f665ef1 commit 1e2036c
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 52 deletions.
26 changes: 13 additions & 13 deletions eo-parser/src/main/antlr4/org/eolang/parser/Program.g4
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@ program

license
:
(COMMENT SINGLE_EOL)*
COMMENT DOUBLE_EOL
(COMMENT EOL)*
COMMENT EOP
;

metas
:
(META SINGLE_EOL)*
META DOUBLE_EOL
(META EOL)*
META EOP
;

objects
:
(
(COMMENT SINGLE_EOL)*
(COMMENT EOL)*
object
(SINGLE_EOL | DOUBLE_EOL)
(EOL | EOP)
)+
;

Expand All @@ -40,7 +40,7 @@ object
)
tail?
(
SINGLE_EOL
EOL
method
htail?
suffix?
Expand All @@ -50,7 +50,7 @@ object

abstraction
:
(COMMENT SINGLE_EOL)*
(COMMENT EOL)*
attributes
(
(suffix (SPACE SLASH (NAME | QUESTION))?)
Expand Down Expand Up @@ -89,9 +89,9 @@ label

tail
:
SINGLE_EOL
EOL
TAB
(object (SINGLE_EOL | DOUBLE_EOL))+
(object (EOL | EOP))+
UNTAB
;

Expand Down Expand Up @@ -289,13 +289,13 @@ fragment LINEBREAK:
('\n' | '\r\n')
;

SINGLE_EOL
EOL
:
LINEBREAK
INDENT*
;

DOUBLE_EOL
EOP
:
LINEBREAK
LINEBREAK
Expand All @@ -309,7 +309,7 @@ fragment LINE_BYTES : BYTE (MINUS BYTE)+;
BYTES:
EMPTY_BYTES
| BYTE MINUS
| LINE_BYTES (MINUS SINGLE_EOL LINE_BYTES)*;
| LINE_BYTES (MINUS EOL LINE_BYTES)*;

BOOL: 'TRUE' | 'FALSE';
STRING: '"' (~["\\\r\n] | ESCAPE_SEQUENCE)* '"';
Expand Down
6 changes: 3 additions & 3 deletions eo-parser/src/main/java/org/eolang/parser/EoLexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ public Token nextToken() {
*/
private void lookAhead() {
final Token token = super.nextToken();
if (token.getType() == ProgramParser.SINGLE_EOL
|| token.getType() == ProgramParser.DOUBLE_EOL) {
if (token.getType() == ProgramParser.EOL
|| token.getType() == ProgramParser.EOP) {
final int tabs = this.getText().replaceAll("[\r\n]", "").length() / 2;
final int last = this.indent.peekLast();
final int shift = tabs - last;
Expand Down Expand Up @@ -116,7 +116,7 @@ private void emitIndent(final int shift) {
private void emitDedent(final int shift) {
for (int idx = 0; idx < shift; ++idx) {
this.emitToken(ProgramParser.UNTAB);
this.emitToken(ProgramParser.SINGLE_EOL);
this.emitToken(ProgramParser.EOL);
}
}

Expand Down
103 changes: 67 additions & 36 deletions eo-parser/src/test/resources/org/eolang/parser/ebnf/to-ebnf.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,57 @@ SOFTWARE.
<xsl:output method="text" encoding="UTF-8"/>
<xsl:function name="eo:escape" as="xs:string">
<xsl:param name="s" as="xs:string"/>
<xsl:variable name="r1" select="replace(replace(replace(replace(replace(replace($s, &quot;\\&quot;, &quot;\\textbackslash{}&quot;), &quot;&amp;&quot;, &quot;\\&amp;&quot;), &quot; &quot;, &quot;\\textvisiblespace{}&quot;), &quot;\^&quot;, &quot;\\^{}&quot;), &quot;\$&quot;, &quot;\\textdollar&quot;), &quot;#&quot;, &quot;\\#&quot;)"/>
<xsl:variable name="r2" select="replace($r1, '_', '\\_')"/>
<xsl:variable name="r3" select="replace($r2, '&quot;', '\\lq\\lq')"/>
<xsl:value-of select="$r3"/>
<xsl:variable name="r0" select="$s"/>
<xsl:variable name="r1" select="replace($r0, '\\', '\\textbackslash{}')"/>
<xsl:variable name="r2" select="replace($r1, '\{', '\\char`\\{')"/>
<xsl:variable name="r3" select="replace($r2, '\}', '\\char`\\}')"/>
<xsl:variable name="r4" select="replace($r3, '&amp;', '\\&amp;')"/>
<xsl:variable name="r5" select="replace($r4, ' ', '\\textvisiblespace{}')"/>
<xsl:variable name="r6" select="replace($r5, '\^', '\\^{}')"/>
<xsl:variable name="r7" select="replace($r6, '\$', '\\textdollar')"/>
<xsl:variable name="r8" select="replace($r7, '#', '\\#')"/>
<xsl:variable name="r9" select="replace($r8, '_', '\\_')"/>
<xsl:variable name="r10" select="replace($r9, '&quot;', '\\lq\\lq')"/>
<xsl:value-of select="$r10"/>
</xsl:function>
<xsl:function name="eo:term" as="xs:string">
<xsl:param name="t" as="xs:string"/>
<xsl:variable name="r">
<xsl:choose>
<xsl:when test="$t = 'eol'">
<xsl:text>'EOL'</xsl:text>
</xsl:when>
<xsl:when test="$t = 'eop'">
<xsl:text>'EOP'</xsl:text>
</xsl:when>
<xsl:when test="$t = 'tab'">
<xsl:text>'TAB'</xsl:text>
</xsl:when>
<xsl:when test="$t = 'untab'">
<xsl:text>'UNTAB'</xsl:text>
</xsl:when>
<xsl:when test="$t = 'eof'">
<xsl:text>'EOF'</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>&lt;</xsl:text>
<xsl:value-of select="$t"/>
<xsl:text>&gt;</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:value-of select="$r"/>
</xsl:function>
<xsl:template match="g:grammar">
<xsl:element name="ebnf">
<xsl:text>% Use native-enbf LaTeX package to render this</xsl:text>
<xsl:text>
</xsl:text>
<xsl:apply-templates select="g:production"/>
<xsl:apply-templates select="g:production[@name!='eof' and @name!='eol' and @name!='eop']"/>
</xsl:element>
</xsl:template>
<xsl:template match="g:production">
<xsl:text>&lt;</xsl:text>
<xsl:value-of select="eo:escape(@name)"/>
<xsl:text>&gt;</xsl:text>
<xsl:value-of select="eo:term(eo:escape(@name))"/>
<xsl:text> := </xsl:text>
<xsl:apply-templates select="g:*"/>
<xsl:text> \\
Expand Down Expand Up @@ -80,41 +114,40 @@ SOFTWARE.
</xsl:if>
</xsl:template>
<xsl:template match="g:choice">
<xsl:text> ( </xsl:text>
<xsl:for-each select="g:*">
<xsl:if test="position() &gt; 1">
<xsl:text> | </xsl:text>
</xsl:if>
<xsl:apply-templates select="."/>
</xsl:for-each>
<xsl:text> ) </xsl:text>
<xsl:variable name="txt">
<xsl:for-each select="g:*">
<xsl:if test="position() &gt; 1">
<xsl:text> | </xsl:text>
</xsl:if>
<xsl:apply-templates select="."/>
</xsl:for-each>
</xsl:variable>
<xsl:choose>
<xsl:when test="ancestor::g:production[count(child::*) = 1]">
<xsl:value-of select="$txt"/>
</xsl:when>
<xsl:otherwise>
<xsl:text> ( </xsl:text>
<xsl:value-of select="$txt"/>
<xsl:text> ) </xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="g:string">
<xsl:text> "</xsl:text>
<xsl:value-of select="eo:escape(text())"/>
<xsl:text>" </xsl:text>
</xsl:template>
<xsl:template match="g:complement">
<xsl:text> /</xsl:text>
<xsl:apply-templates select="g:*"/>
<xsl:text>/ </xsl:text>
</xsl:template>
<xsl:template match="g:charClass">
<xsl:variable name="txt">
<xsl:text>[</xsl:text>
<xsl:apply-templates select="g:*"/>
<xsl:text>]</xsl:text>
</xsl:variable>
<xsl:choose>
<xsl:when test="ancestor::g:complement">
<xsl:value-of select="$txt"/>
</xsl:when>
<xsl:otherwise>
<xsl:text> /</xsl:text>
<xsl:value-of select="$txt"/>
<xsl:text>/ </xsl:text>
</xsl:otherwise>
</xsl:choose>
<xsl:text> /[</xsl:text>
<xsl:if test="ancestor::g:complement">
<xsl:text>\^{}</xsl:text>
</xsl:if>
<xsl:apply-templates select="g:*"/>
<xsl:text>]/ </xsl:text>
</xsl:template>
<xsl:template match="g:charRange">
<xsl:value-of select="@minChar"/>
Expand Down Expand Up @@ -163,9 +196,7 @@ SOFTWARE.
<xsl:text>'</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text> &lt;</xsl:text>
<xsl:value-of select="eo:escape(@name)"/>
<xsl:text>&gt; </xsl:text>
<xsl:value-of select="eo:term(eo:escape(@name))"/>
</xsl:otherwise>
</xsl:choose>
<xsl:text> </xsl:text>
Expand Down

0 comments on commit 1e2036c

Please sign in to comment.