Skip to content

Commit

Permalink
Avoid returning null from RootContext.
Browse files Browse the repository at this point in the history
To avoid typing errors.
  • Loading branch information
MrCoder committed Nov 8, 2024
1 parent c128c6a commit 5e17e89
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 88 deletions.
166 changes: 79 additions & 87 deletions src/parser/MessageContextListener.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,100 +3,92 @@ import { MessageContextListener } from "./MessageContextListener";
import { RootContext } from "@/parser/index";
import { OwnableMessageType } from "@/parser/OwnableMessage";

describe("MessageListener", () => {
it("can handle Message and Creation", () => {
const code = `
res = A.method(E.m) {
B->C.method
}
var = res
new B
C->D: message
`;
describe("MessageContextListener", () => {
// Helper function to parse code and get results
const parseCode = (code: string) => {
const rootContext = RootContext(code);
const walker = antlr4.tree.ParseTreeWalker.DEFAULT;
const listener = new MessageContextListener();
walker.walk(listener, rootContext);
return listener.result();
};

const messageContextListener = new MessageContextListener();
// @ts-ignore
walker.walk(messageContextListener, rootContext);
describe("message parsing", () => {
it("parses various message types and creation statements", () => {
const code = `
res = A.method(E.m) {
B->C.method
}
var = res
new B
C->D: message
`;

expect(messageContextListener.result()).toStrictEqual([
{
from: "_STARTER_",
label: "method(E.m)",
signature: "method(E.m)",
to: "A",
type: OwnableMessageType.SyncMessage,
},
{
from: "B",
label: "method",
signature: "method",
to: "C",
type: OwnableMessageType.SyncMessage,
},
{
from: "_STARTER_",
label: "var=res",
signature: "res",
to: "_STARTER_",
type: OwnableMessageType.SyncMessage,
},
{
from: "_STARTER_",
label: "«create»",
signature: "«create»",
to: "B",
type: OwnableMessageType.CreationMessage,
},
{
from: "C",
label: " message",
signature: " message",
to: "D",
type: OwnableMessageType.AsyncMessage,
},
]);
});

it("ignores expression in parameters", () => {
const code = `A.m(new B,
C.m)`;
const rootContext = RootContext(code);
const walker = antlr4.tree.ParseTreeWalker.DEFAULT;
expect(parseCode(code)).toStrictEqual([
{
from: "_STARTER_",
label: "method(E.m)",
signature: "method(E.m)",
to: "A",
type: OwnableMessageType.SyncMessage,
},
{
from: "B",
label: "method",
signature: "method",
to: "C",
type: OwnableMessageType.SyncMessage,
},
{
from: "_STARTER_",
label: "var=res",
signature: "res",
to: "_STARTER_",
type: OwnableMessageType.SyncMessage,
},
{
from: "_STARTER_",
label: "«create»",
signature: "«create»",
to: "B",
type: OwnableMessageType.CreationMessage,
},
{
from: "C",
label: " message",
signature: " message",
to: "D",
type: OwnableMessageType.AsyncMessage,
},
]);
});

const messageContextListener = new MessageContextListener();
// @ts-ignore
walker.walk(messageContextListener, rootContext);
it("handles parameter expressions without creating additional messages", () => {
const code = `A.m(new B, C.m)`;

expect(messageContextListener.result()).toStrictEqual([
{
from: "_STARTER_",
signature: "m(new B,C.m)",
label: "m(new B,C.m)",
to: "A",
type: 0,
},
]);
});

it("ignores expression in conditions", () => {
const code = `if(A.isGood()) {B.m}`;
const rootContext = RootContext(code);
const walker = antlr4.tree.ParseTreeWalker.DEFAULT;
expect(parseCode(code)).toStrictEqual([
{
from: "_STARTER_",
signature: "m(new B,C.m)",
label: "m(new B,C.m)",
to: "A",
type: 0,
},
]);
});

const messageContextListener = new MessageContextListener();
// @ts-ignore
walker.walk(messageContextListener, rootContext);
it("parses messages in conditional blocks while ignoring condition expressions", () => {
const code = `if(A.isGood()) {B.m}`;

expect(messageContextListener.result()).toStrictEqual([
{
from: "_STARTER_",
signature: "m",
label: "m",
to: "B",
type: 0,
},
]);
expect(parseCode(code)).toStrictEqual([
{
from: "_STARTER_",
signature: "m",
label: "m",
to: "B",
type: 0,
},
]);
});
});
});
2 changes: 1 addition & 1 deletion src/parser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function rootContext(code) {
const tokens = new antlr4.CommonTokenStream(lexer);
const parser = new sequenceParser(tokens);
parser.addErrorListener(new SeqErrorListener());
return parser._syntaxErrors ? null : parser.prog();
return parser.prog();
}

antlr4.ParserRuleContext.prototype.getFormattedText = function () {
Expand Down

0 comments on commit 5e17e89

Please sign in to comment.