Skip to content

Commit

Permalink
bug(objectionary#3946): grammar + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maxonfjvipon committed Feb 26, 2025
1 parent e81fe61 commit 26e0a05
Show file tree
Hide file tree
Showing 22 changed files with 217 additions and 153 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,36 @@
<!-- Get name for special attributes -->
<xsl:function name="eo:attr-name" as="xs:string">
<xsl:param name="n" as="xs:string"/>
<xsl:param name="wrap" select="false()" as="xs:boolean"/>
<xsl:variable name="alpha" select="starts-with($n, $eo:alpha)"/>
<xsl:variable name="name">
<xsl:choose>
<xsl:when test="$n='@'">
<xsl:value-of select="$eo:phi"/>
</xsl:when>
<xsl:when test="$alpha">
<xsl:value-of select="substring-after($n, $eo:alpha)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$n"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:choose>
<xsl:when test="$n='@'">
<xsl:value-of select="$eo:phi"/>
<xsl:when test="$wrap">
<xsl:choose>
<xsl:when test="$alpha">
<xsl:value-of select="$name"/>
</xsl:when>
<xsl:otherwise>
<xsl:text>"</xsl:text>
<xsl:value-of select="$name"/>
<xsl:text>"</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$n"/>
<xsl:value-of select="$name"/>
</xsl:otherwise>
</xsl:choose>
</xsl:function>
Expand Down Expand Up @@ -138,9 +162,9 @@
<xsl:otherwise>
<xsl:text>new PhMethod(</xsl:text>
<xsl:value-of select="$base"/>
<xsl:text>, "</xsl:text>
<xsl:value-of select="eo:attr-name($mtd)"/>
<xsl:text>");</xsl:text>
<xsl:text>, </xsl:text>
<xsl:value-of select="eo:attr-name($mtd, true())"/>
<xsl:text>);</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:function>
Expand Down Expand Up @@ -296,7 +320,7 @@
<xsl:param name="indent"/>
<xsl:param name="parent"/>
<xsl:param name="context"/>
<xsl:variable name="name" select="eo:attr-name(@name)"/>
<xsl:variable name="name" select="eo:attr-name(@name, false())"/>
<xsl:if test="not(@name)">
<xsl:message terminate="yes">
<xsl:text>Unnamed attribute found in </xsl:text>
Expand Down Expand Up @@ -492,12 +516,12 @@
<xsl:variable name="parts" select="tokenize(@base, '\.')"/>
<xsl:choose>
<!-- Little optimization -->
<xsl:when test="starts-with(@base, 'Q.org.eolang') and not(contains(@base, '^'))">
<xsl:when test="starts-with(@base, 'Q.org.eolang')">
<xsl:value-of select="eo:fqn-start($parts[1], $rho)"/>
<xsl:for-each select="$parts[position()&gt;1]">
<xsl:text>.take("</xsl:text>
<xsl:value-of select="eo:attr-name(.)"/>
<xsl:text>")</xsl:text>
<xsl:text>.take(</xsl:text>
<xsl:value-of select="eo:attr-name(., true())"/>
<xsl:text>)</xsl:text>
</xsl:for-each>
<xsl:if test="./value">
<xsl:text>.copy()</xsl:text>
Expand Down Expand Up @@ -618,16 +642,7 @@
<xsl:text>, </xsl:text>
<xsl:choose>
<xsl:when test="@as">
<xsl:choose>
<xsl:when test="starts-with(@as, $eo:alpha)">
<xsl:value-of select="eo:attr-name(substring-after(@as, $eo:alpha))"/>
</xsl:when>
<xsl:otherwise>
<xsl:text>"</xsl:text>
<xsl:value-of select="eo:attr-name(@as)"/>
<xsl:text>"</xsl:text>
</xsl:otherwise>
</xsl:choose>
<xsl:value-of select="eo:attr-name(@as, true())"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="position() - 1"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ asserts:
- //java[contains(text(), 'Phi rb1 = Phi.Φ.take("org").take("eolang").take("number");')]
- //java[contains(text(), 'Phi rb11 = Phi.Φ.take("org").take("eolang").take("bytes").copy();')]
- //java[contains(text(), 'Phi r = new PhMethod(rb, "and");')]
- //java[contains(text(), 'Phi r = new PhMethod(rb, 0);')]
input: |
# Comment.
[] > foo
Expand All @@ -25,3 +26,4 @@ input: |
x.
$
2
q.~0 > w
8 changes: 5 additions & 3 deletions eo-parser/src/main/antlr4/org/eolang/parser/Eo.g4
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ vmethodHeadVapplication

// Tail of method
methodTail
: DOT finisher
: DOT (finisher | TILDA INT)
;

// Can be at the beginning of the statement
Expand All @@ -391,7 +391,7 @@ finisher
// Reversed notation
// Only finisher can be used in reversed notation
reversed
: finisher DOT
: (finisher | TILDA INT) DOT
;

// Formation name
Expand Down Expand Up @@ -438,6 +438,7 @@ COMMENTARY
: HASH
| (HASH ~[\r\n]* ~[\r\n\t ])
;

META: PLUS NAME (SPACE ~[\t\r\n ]+)*
;

Expand Down Expand Up @@ -488,7 +489,8 @@ RHO : '^'
;
HASH: '#'
;
BAR : '|'
TILDA
: '~'
;

fragment INDENT
Expand Down
102 changes: 37 additions & 65 deletions eo-parser/src/main/java/org/eolang/parser/Objects.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,113 +12,85 @@
* Object tree.
* @since 0.1
*/
interface Objects extends Iterable<Directive> {
final class Objects implements Iterable<Directive> {

/**
* Collected directives.
*/
private final Directives dirs = new Directives();

/**
* Start new object.
* @param line At line.
* @param pos At position.
* @return Self.
*/
Objects start(int line, int pos);
public Objects start(final int line, final int pos) {
this.dirs.add("o");
return this.prop("line", line).prop("pos", pos);
}

/**
* Add data.
* @param data Data.
* @return Self.
*/
Objects data(String data);
Objects data(final String data) {
this.dirs.set(data);
return this;
}

/**
* Property.
* @param key Key.
* @param value Value.
* @param type Type.
* @return Self.
*/
Objects prop(String key, Object value);
Objects prop(final String key, final Object type) {
this.dirs.attr(key, type);
return this;
}

/**
* Empty property.
* @param key Key.
* @return Self.
*/
Objects prop(String key);
Objects prop(final String key) {
return this.prop(key, "");
}

/**
* Change property by given xpath.
* @param key Key.
* @param xpath Xpath.
* @return Self.
*/
Objects xprop(String key, Object xpath);
Objects xprop(final String key, final Object xpath) {
this.dirs.xattr(key, xpath);
return this;
}

/**
* Enter last object.
* @return Self.
*/
Objects enter();
Objects enter() {
this.dirs.xpath("o[last()]").strict(1);
return this;
}

/**
* Leave current object.
* @return Self.
*/
Objects leave();

/**
* Xembly object tree.
* @since 0.1
*/
final class ObjXembly implements Objects {

/**
* Collected directives.
*/
private final Directives dirs = new Directives();

@Override
public Objects start(final int line, final int pos) {
this.dirs.add("o");
return this.prop("line", line).prop("pos", pos);
}

@Override
public Objects data(final String data) {
this.dirs.set(data);
return this;
}

@Override
public Objects prop(final String key, final Object type) {
this.dirs.attr(key, type);
return this;
}

@Override
public Objects prop(final String key) {
return this.prop(key, "");
}

@Override
public Objects xprop(final String key, final Object xpath) {
this.dirs.xattr(key, xpath);
return this;
}

@Override
public Objects enter() {
this.dirs.xpath("o[last()]").strict(1);
return this;
}

@Override
public Objects leave() {
this.dirs.up();
return this;
}
Objects leave() {
this.dirs.up();
return this;
}

@Override
public Iterator<Directive> iterator() {
return this.dirs.iterator();
}
@Override
public Iterator<Directive> iterator() {
return this.dirs.iterator();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*
* @since 0.1
*/
public final class ParsingException extends RuntimeException {
final class ParsingException extends RuntimeException {

/**
* Serialization marker.
Expand Down Expand Up @@ -70,7 +70,7 @@ public final class ParsingException extends RuntimeException {
* Get the place.
* @return Line
*/
public int line() {
int line() {
return this.place;
}
}
Loading

0 comments on commit 26e0a05

Please sign in to comment.