Skip to content

Commit

Permalink
fix(#2737): grammar + listener
Browse files Browse the repository at this point in the history
  • Loading branch information
maxonfjvipon committed Dec 27, 2023
1 parent b7318e3 commit 2ea361f
Show file tree
Hide file tree
Showing 2 changed files with 200 additions and 38 deletions.
78 changes: 66 additions & 12 deletions eo-parser/src/main/antlr4/org/eolang/parser/Eo.g4
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,14 @@ application
// must be horizontal only
happlication
: happlicationHead happlicationTail
| reversed happlicationTailReversed
;

// Extended horizontal application
// The head can contain elements in horizontal or vertical notations
happlicationExtended
: happlicationHeadExtended happlicationTailExtended
| reversed happlicationTailReversedExtended
;

// Head of horizontal application
Expand All @@ -121,7 +123,6 @@ happlicationHeadExtended
applicable
: STAR
| (NAME | AT) COPY?
| reversed
;

// Horizontal application tail
Expand All @@ -130,6 +131,14 @@ happlicationTail
| (SPACE happlicationArg)+
;

happlicationTailReversed
: SPACE happlicationTailReversedFirst happlicationTail?
;

happlicationTailReversedFirst
: happlicationArg
;

// Argument of horizontal application
// Does not contain elements in vertical notation
happlicationArg
Expand All @@ -146,6 +155,14 @@ happlicationTailExtended
| (SPACE happlicationArgExtended)+
;

happlicationTailReversedExtended
: SPACE happlicationTailReversedExtendedFirst happlicationTailExtended?
;

happlicationTailReversedExtendedFirst
: happlicationArgExtended
;

// Extended argument of horizontal application
// Can contain elements in vertical notation
happlicationArgExtended
Expand All @@ -158,6 +175,7 @@ happlicationArgExtended
// Vertical application
vapplication
: vapplicationHeadNamed vapplicationArgs
| reversed oname? vapplicationArgsReversed
;

// Vertical application head
Expand All @@ -183,6 +201,14 @@ vapplicationArgs
UNTAB
;

vapplicationArgsReversed
: EOL
TAB
vapplicationArgUnbinded
((EOL vapplicationArg?) | EOP)
UNTAB
;

vapplicationArg
: (vapplicationArgBinded (EOL | EOP))+
| (vapplicationArgUnbinded (EOL | EOP))+
Expand All @@ -194,6 +220,7 @@ vapplicationArgBinded
| vapplicationArgHanonymBinded // horizontal anonym object
| vapplicationArgHapplicationBinded // horizontal application
| vapplicationHeadAs oname? vapplicationArgs // vertical application
| reversed as oname? vapplicationArgsReversed // reversed vertical application
| just as oname? // Just an object reference with binding
| methodAs oname? // Method with binding
;
Expand All @@ -204,6 +231,7 @@ vapplicationArgUnbinded
| vapplicationArgHanonymUnbinded // horizontal anonym object
| vapplicationArgHapplicationUnbinded // horizontal application
| vapplicationHeadNamed vapplicationArgs // vertical application
| reversed oname? vapplicationArgsReversed // reversed verical application
| justNamed // Just an object reference
| methodNamed // Method
;
Expand All @@ -219,7 +247,7 @@ vapplicationArgHapplicationUnbinded

// Vertical application head with binding
vapplicationHeadAs
: (applicable | hmethodExtended | hmethodExtendedVersioned | versioned) as
: (applicable | hmethodOptional | versioned) as
;

// Vertical anonym object as argument of vertical application
Expand Down Expand Up @@ -284,7 +312,7 @@ methodNamed

// Method with bindning
methodAs
: (hmethodExtended | hmethodExtendedVersioned) as
: hmethodOptional as
;

// Horizontal method
Expand All @@ -294,6 +322,11 @@ hmethod
: hmethodHead methodTail+
;

hmethodOptional
: hmethodExtended
| hmethodExtendedVersioned
;

// Extended horizontal method
// The head can contain elements in vertical notation
hmethodExtended
Expand Down Expand Up @@ -346,18 +379,39 @@ vmethodVersioned
// Head of vertical method can be:
// 1. vertical method
// 2. horizontal method
// 3. vertical application. Here, vertical application is split into 2 parts because
// vapplicationHead contains vmethod which leads to left recursion error.
// 3. vertical application
// 4. horizontal application. The same logic as with a vertical application
// 5. just an object reference
vmethodHead
: vmethodHead (vmethodTail | vmethodTailVersioned) oname? // vmethod
| (hmethodExtended | hmethodExtendedVersioned) oname? // hmethod extended
| vmethodHead (vmethodTail | vmethodTailVersioned) oname? vapplicationArgs oname? // vmethod + vapplication
| (applicable | hmethodExtended | hmethodExtendedVersioned | versioned) oname? vapplicationArgs oname? // vapplication without vmethod in head
| vmethodHead (vmethodTail | vmethodTailVersioned) happlicationTailExtended oname? // vmethod + haplication
| (applicable | hmethodExtended) happlicationTailExtended oname? // happlication without vmethod in head
| justNamed // just
: vmethodHead vmethodTailOptional vmethodHeadApplicationTail
| vmethodHeadHmethodExtended
| vmethodHeadVapplication
| vmethodHeadHapplication
| justNamed
;

vmethodTailOptional
: vmethodTail
| vmethodTailVersioned
;

vmethodHeadApplicationTail
: oname? vapplicationArgs?
| happlicationTailExtended oname?
;

vmethodHeadHmethodExtended
: hmethodOptional oname?
;

vmethodHeadVapplication
: (applicable | hmethodOptional | versioned) oname? vapplicationArgs
| reversed oname? vapplicationArgsReversed
;

vmethodHeadHapplication
: (applicable | hmethodExtended) happlicationTailExtended oname?
| reversed happlicationTailReversedExtended oname?
;

// Tail of vertical method
Expand Down
160 changes: 134 additions & 26 deletions eo-parser/src/main/java/org/eolang/parser/XeEoListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -329,27 +329,25 @@ public void exitHapplicationHeadExtended(
@Override
@SuppressWarnings("PMD.ConfusingTernary")
public void enterApplicable(final EoParser.ApplicableContext ctx) {
if (ctx.reversed() == null) {
this.startObject(ctx);
final String base;
if (ctx.STAR() != null) {
base = "tuple";
this.objects.prop("star");
} else if (ctx.NAME() != null) {
base = ctx.NAME().getText();
} else if (ctx.AT() != null) {
base = "@";
} else {
base = "";
}
if (!base.isEmpty()) {
this.objects.prop("base", base);
}
if (ctx.COPY() != null) {
this.objects.prop("copy");
}
this.objects.leave();
this.startObject(ctx);
final String base;
if (ctx.STAR() != null) {
base = "tuple";
this.objects.prop("star");
} else if (ctx.NAME() != null) {
base = ctx.NAME().getText();
} else if (ctx.AT() != null) {
base = "@";
} else {
base = "";
}
if (!base.isEmpty()) {
this.objects.prop("base", base);
}
if (ctx.COPY() != null) {
this.objects.prop("copy");
}
this.objects.leave();
}

@Override
Expand All @@ -367,6 +365,26 @@ public void exitHapplicationTail(final EoParser.HapplicationTailContext ctx) {
this.objects.leave();
}

@Override
public void enterHapplicationTailReversed(final EoParser.HapplicationTailReversedContext ctx) {
// Nothing here
}

@Override
public void exitHapplicationTailReversed(final EoParser.HapplicationTailReversedContext ctx) {
// Nothing here
}

@Override
public void enterHapplicationTailReversedFirst(final EoParser.HapplicationTailReversedFirstContext ctx) {
this.objects.enter();
}

@Override
public void exitHapplicationTailReversedFirst(final EoParser.HapplicationTailReversedFirstContext ctx) {
this.objects.leave();
}

@Override
public void enterHapplicationArg(final EoParser.HapplicationArgContext ctx) {
// Nothing here
Expand All @@ -391,6 +409,26 @@ public void exitHapplicationTailExtended(
this.objects.leave();
}

@Override
public void enterHapplicationTailReversedExtended(final EoParser.HapplicationTailReversedExtendedContext ctx) {
// Nothing here
}

@Override
public void exitHapplicationTailReversedExtended(final EoParser.HapplicationTailReversedExtendedContext ctx) {
// Nothing here
}

@Override
public void enterHapplicationTailReversedExtendedFirst(final EoParser.HapplicationTailReversedExtendedFirstContext ctx) {
this.objects.enter();
}

@Override
public void exitHapplicationTailReversedExtendedFirst(final EoParser.HapplicationTailReversedExtendedFirstContext ctx) {
this.objects.leave();
}

@Override
public void enterHapplicationArgExtended(
final EoParser.HapplicationArgExtendedContext ctx
Expand Down Expand Up @@ -437,12 +475,22 @@ public void exitVapplicationHeadNamed(final EoParser.VapplicationHeadNamedContex

@Override
public void enterVapplicationArgs(final EoParser.VapplicationArgsContext ctx) {
this.objects.enter();
// Nothing here
}

@Override
public void exitVapplicationArgs(final EoParser.VapplicationArgsContext ctx) {
this.objects.leave();
// Nothing here
}

@Override
public void enterVapplicationArgsReversed(final EoParser.VapplicationArgsReversedContext ctx) {
// Nothing here
}

@Override
public void exitVapplicationArgsReversed(final EoParser.VapplicationArgsReversedContext ctx) {
// Nothing here
}

@Override
Expand All @@ -457,22 +505,22 @@ public void exitVapplicationArg(final EoParser.VapplicationArgContext ctx) {

@Override
public void enterVapplicationArgBinded(final EoParser.VapplicationArgBindedContext ctx) {
// Nothing here
this.objects.enter();
}

@Override
public void exitVapplicationArgBinded(final EoParser.VapplicationArgBindedContext ctx) {
// Nothing here
this.objects.leave();
}

@Override
public void enterVapplicationArgUnbinded(final EoParser.VapplicationArgUnbindedContext ctx) {
// Nothing here
this.objects.enter();
}

@Override
public void exitVapplicationArgUnbinded(final EoParser.VapplicationArgUnbindedContext ctx) {
// Nothing here
this.objects.leave();
}

@Override
Expand Down Expand Up @@ -661,6 +709,16 @@ public void exitHmethod(final EoParser.HmethodContext ctx) {
// Nothing here
}

@Override
public void enterHmethodOptional(final EoParser.HmethodOptionalContext ctx) {
// Nothing here
}

@Override
public void exitHmethodOptional(final EoParser.HmethodOptionalContext ctx) {
// Nothing here
}

@Override
public void enterHmethodExtended(final EoParser.HmethodExtendedContext ctx) {
// Nothing here
Expand Down Expand Up @@ -745,6 +803,56 @@ public void exitVmethodHead(final EoParser.VmethodHeadContext ctx) {
// Nothing here
}

@Override
public void enterVmethodTailOptional(final EoParser.VmethodTailOptionalContext ctx) {
// Nothing here
}

@Override
public void exitVmethodTailOptional(final EoParser.VmethodTailOptionalContext ctx) {
// Nothing here
}

@Override
public void enterVmethodHeadApplicationTail(final EoParser.VmethodHeadApplicationTailContext ctx) {
// Nothing here
}

@Override
public void exitVmethodHeadApplicationTail(final EoParser.VmethodHeadApplicationTailContext ctx) {
// Nothing here
}

@Override
public void enterVmethodHeadHmethodExtended(final EoParser.VmethodHeadHmethodExtendedContext ctx) {
// Nothing here
}

@Override
public void exitVmethodHeadHmethodExtended(final EoParser.VmethodHeadHmethodExtendedContext ctx) {
// Nothing here
}

@Override
public void enterVmethodHeadVapplication(final EoParser.VmethodHeadVapplicationContext ctx) {
// Nothing here
}

@Override
public void exitVmethodHeadVapplication(final EoParser.VmethodHeadVapplicationContext ctx) {
// Nothing here
}

@Override
public void enterVmethodHeadHapplication(final EoParser.VmethodHeadHapplicationContext ctx) {
// Nothing here
}

@Override
public void exitVmethodHeadHapplication(final EoParser.VmethodHeadHapplicationContext ctx) {
// Nothing here
}

@Override
public void enterVmethodTail(final EoParser.VmethodTailContext ctx) {
// Nothing here
Expand Down

0 comments on commit 2ea361f

Please sign in to comment.