Skip to content

Commit

Permalink
Merge pull request #3958 from maxonfjvipon/bug/#3946/take-attr-by-pos
Browse files Browse the repository at this point in the history
bug(#3946): take attribute by position
  • Loading branch information
yegor256 authored Feb 26, 2025
2 parents fbe48f9 + 93cee82 commit 6beb471
Show file tree
Hide file tree
Showing 25 changed files with 232 additions and 160 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,39 @@
<!-- 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" 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:variable name="quoted">
<xsl:text>"</xsl:text>
<xsl:value-of select="$name"/>
<xsl:text>"</xsl:text>
</xsl:variable>
<xsl:value-of select="$quoted"/>
</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 +165,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 +323,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 +519,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 @@ -620,16 +647,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(), 'r = new PhMethod(r, 2);')]
input: |
# Comment.
[] > foo
Expand All @@ -25,3 +26,4 @@ input: |
x.
$
2
q.~2 > 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 | TILDE 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 | TILDE 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 : '|'
TILDE
: '~'
;

fragment INDENT
Expand Down
104 changes: 38 additions & 66 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();

@Override
public Iterator<Directive> iterator() {
return this.dirs.iterator();
}

/**
* Start new object.
* @param line At line.
* @param pos At position.
* @return Self.
*/
Objects start(int line, int pos);
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;
}

@Override
public Iterator<Directive> iterator() {
return this.dirs.iterator();
}
Objects leave() {
this.dirs.up();
return this;
}
}
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 6beb471

Please sign in to comment.