Skip to content

Commit

Permalink
fix(objectionary#2930): simplified phi grammar + fixed bug with dispa…
Browse files Browse the repository at this point in the history
…tch after formation
  • Loading branch information
maxonfjvipon committed Mar 12, 2024
1 parent e8ae097 commit e64f050
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 93 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
eo: |
# This is the default 64+ symbols comment in front of abstract object.
[] > test
([] (^ > x)).plus > s
int
phi: |
{
test ↦ ⟦
s ↦ ⟦
x ↦ ξ.ρ
⟧.plus(
α0 ↦ Φ.org.eolang.int
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
tests:
- //o[@name='test' and count(o)=2]
- //o[@name='test']/o[position()=1 and @abstract and not(@name)]
- //o[@name='test']/o[last() and @base='.plus' and @method]
phi: |
{
test ↦ ⟦
s ↦ ⟦⟧.plus(
α0 ↦ Φ.org.eolang.int
)
}
33 changes: 11 additions & 22 deletions eo-parser/src/main/antlr4/org/eolang/parser/Phi.g4
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,20 @@ program
;

object
: formation
| application
| dispatch
: formation applicationsOrDispatches
| scoped (dispatch applicationsOrDispatches)?
| termination
;

formation
: LSB bindings RSB
;

scoped
: XI
| HOME
;

bindings
: binding?
| binding (COMMA binding)*
Expand Down Expand Up @@ -66,29 +70,14 @@ FUNCTION
;
application
: (formation | dispatch | termination) bnds
;
bnds: (LB bindings RB)+
;
dispatch
: (formation | termination) bnds? attr+ disp
| (HOME | XI) (attr+ disp)?
;
disp:
| dispBnds attr+ disp
: LB bindings RB
;
// The rule was separately because it's used as
// marker where it's needed to enter the <o> object
// in order to make application right
dispBnds
: bnds
dispatch: DOT attribute
;
attr: DOT attribute
applicationsOrDispatches
: (application | dispatch)*
;
termination
Expand Down
123 changes: 52 additions & 71 deletions eo-parser/src/main/java/org/eolang/parser/XePhiListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@
import org.eolang.parser.xmir.XmirInfo;
import org.xembly.Directive;
import org.xembly.Directives;
import org.xembly.Xembler;

/**
* The PHI-CALCULUS grammar listener for ANTLR4 walker.
*
* @checkstyle CyclomaticComplexityCheck (500 lines)
* @checkstyle ClassFanOutComplexityCheck (500 lines)
* @checkstyle MethodCountCheck (1300 lines)
* @checkstyle NestedIfDepthCheck (1300 lines)
* @since 0.34.0
*/
@SuppressWarnings({
Expand Down Expand Up @@ -133,8 +133,8 @@ public void enterProgram(final PhiParser.ProgramContext ctx) {
.add("sheets").up()
.add("license").up()
.add("metas").up();
if (ctx.object() != null && ctx.object().formation() == null) {
this.objects().start(ctx.getStart().getLine(), ctx.getStart().getCharPositionInLine());
if (ctx.object() == null || ctx.object().formation() == null) {
this.objects().start();
}
}

Expand All @@ -149,7 +149,7 @@ public void exitProgram(final PhiParser.ProgramContext ctx) {
.add("part").set(pckg).up()
.up().up();
}
if (ctx.object() != null && ctx.object().formation() == null) {
if (ctx.object() == null || ctx.object().formation() == null) {
this.objects().leave();
}
this.dirs.add("objects")
Expand All @@ -176,16 +176,25 @@ public void enterFormation(final PhiParser.FormationContext ctx) {
@Override
public void exitFormation(final PhiParser.FormationContext ctx) {
this.properties.pop();
if (!XePhiListener.hasLambdaPackage(ctx.bindings())
&& !this.attributes.empty()
&& this.objs.size() > this.packages.size()
) {
this.objects()
.prop("abstract")
.prop(this.properties.peek(), this.attributes.pop());
if (!this.properties.empty() && !XePhiListener.hasLambdaPackage(ctx.bindings())) {
this.objects().prop("abstract").leave();
}
}

@Override
public void enterScoped(PhiParser.ScopedContext ctx) {
if (ctx.HOME() != null) {
this.objects().prop("base", "Q").leave();
} else if (ctx.XI() != null) {
this.objects().prop("base", "$").leave();
}
}

@Override
public void exitScoped(PhiParser.ScopedContext ctx) {
// Nothing here
}

@Override
public void enterBindings(final PhiParser.BindingsContext ctx) {
if (XePhiListener.hasLambdaPackage(ctx)) {
Expand All @@ -199,33 +208,40 @@ public void exitBindings(final PhiParser.BindingsContext ctx) {
if (XePhiListener.hasLambdaPackage(ctx)) {
this.objs.poll();
}
if (this.properties.peek().equals("as") && ctx.binding().size() == 0) {
this.objects().prop("copy");
}
}

@Override
public void enterBinding(final PhiParser.BindingContext ctx) {
if (ctx.alphaBinding() != null || ctx.emptyBinding() != null) {
this.objects().start(ctx.getStart().getLine(), ctx.getStart().getCharPositionInLine());
if (ctx.alphaBinding() != null) {
if (ctx.alphaBinding().attribute().VTX() != null) {
this.objs.add(new Objects.ObjXembly());
}
this.objects().start();
}
if (ctx.emptyBinding() != null) {
if (ctx.emptyBinding().attribute().VTX() != null) {
this.objs.add(new Objects.ObjXembly());
}
this.objects().start();
}
}

@Override
@SuppressWarnings("PMD.ConfusingTernary")
public void exitBinding(final PhiParser.BindingContext ctx) {
if (this.objs.size() > this.packages.size()) {
if (ctx.alphaBinding() != null) {
if (ctx.alphaBinding().attribute().VTX() != null) {
this.objects().remove();
this.attributes.pop();
this.objs.removeLast();
} else {
this.objects().leave();
this.objects().enter().prop(this.properties.peek(), this.attributes.pop()).leave();
}
} else if (ctx.emptyBinding() != null) {
if (ctx.emptyBinding().attribute().VTX() != null) {
this.objects().remove();
this.attributes.pop();
this.objs.removeLast();
} else {
this.objects().leave();
this.objects().enter().prop(this.properties.peek(), this.attributes.pop()).leave();
}
}
}
Expand Down Expand Up @@ -285,13 +301,13 @@ public void enterEmptyBinding(final PhiParser.EmptyBindingContext ctx) {

@Override
public void exitEmptyBinding(final PhiParser.EmptyBindingContext ctx) {
this.objects().prop("name", this.attributes.pop());
this.objects().leave();
}

@Override
public void enterDeltaBidning(final PhiParser.DeltaBidningContext ctx) {
this.objects()
.start(ctx.getStart().getLine(), ctx.getStart().getCharPositionInLine())
.start()
.prop("data", "bytes")
.prop("base", "org.eolang.bytes");
if (!ctx.BYTES().getText().equals("--")) {
Expand All @@ -308,7 +324,7 @@ public void exitDeltaBidning(final PhiParser.DeltaBidningContext ctx) {
@Override
public void enterLambdaBidning(final PhiParser.LambdaBidningContext ctx) {
if (!ctx.FUNCTION().getText().equals(XePhiListener.LAMBDA_PACKAGE)) {
this.objects().prop("atom", "?");
this.objects().prop("atom");
}
}

Expand All @@ -319,76 +335,41 @@ public void exitLambdaBidning(final PhiParser.LambdaBidningContext ctx) {

@Override
public void enterApplication(final PhiParser.ApplicationContext ctx) {
// Nothing here
}

@Override
public void exitApplication(final PhiParser.ApplicationContext ctx) {
// Nothing here
}

@Override
public void enterBnds(final PhiParser.BndsContext ctx) {
this.properties.push("as");
this.objects().enter();
if (ctx.bindings().binding().size() == 0) {
this.objects().prop("copy");
}
}

@Override
public void exitBnds(final PhiParser.BndsContext ctx) {
public void exitApplication(final PhiParser.ApplicationContext ctx) {
this.objects().leave();
this.properties.pop();
}


@Override
@SuppressWarnings("PMD.ConfusingTernary")
public void enterDispatch(final PhiParser.DispatchContext ctx) {
if (ctx.HOME() != null) {
this.objects().prop("base", "Q").leave();
} else if (ctx.XI() != null) {
this.objects().prop("base", "$").leave();
}
this.objects().start().prop("method");
}

@Override
public void exitDispatch(final PhiParser.DispatchContext ctx) {
this.objects().enter();
final String attribute = this.attributes.pop();
if (!attribute.isEmpty()) {
this.objects().prop(this.properties.peek(), attribute);
}
this.objects().prop("base", String.format(".%s", this.attributes.pop())).leave();
}

@Override
public void enterDisp(final PhiParser.DispContext ctx) {
public void enterApplicationsOrDispatches(PhiParser.ApplicationsOrDispatchesContext ctx) {
// Nothing here
}

@Override
public void exitDisp(final PhiParser.DispContext ctx) {
public void exitApplicationsOrDispatches(PhiParser.ApplicationsOrDispatchesContext ctx) {
// Nothing here
}

@Override
public void enterDispBnds(final PhiParser.DispBndsContext ctx) {
this.objects().enter();
}

@Override
public void exitDispBnds(final PhiParser.DispBndsContext ctx) {
this.objects().leave();
}

@Override
public void enterAttr(final PhiParser.AttrContext ctx) {
this.objects().start(ctx.getStart().getLine(), ctx.getStart().getCharPositionInLine());
}

@Override
public void exitAttr(final PhiParser.AttrContext ctx) {
this.objects()
.prop("method")
.prop("base", String.format(".%s", this.attributes.pop()))
.leave();
}

@Override
public void enterTermination(final PhiParser.TerminationContext ctx) {
// Nothing here
Expand Down Expand Up @@ -442,7 +423,7 @@ private static boolean hasLambdaPackage(final PhiParser.BindingsContext ctx) {
.stream()
.anyMatch(
context -> context.lambdaBidning() != null
&& context.lambdaBidning().FUNCTION().getText().equals(XePhiListener.LAMBDA_PACKAGE)
&& context.lambdaBidning().FUNCTION().getText().equals(XePhiListener.LAMBDA_PACKAGE)
);
}
}

0 comments on commit e64f050

Please sign in to comment.