-
Notifications
You must be signed in to change notification settings - Fork 56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor command-line handling #335
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,15 +69,10 @@ function formatResult(action: string, targetRoomId: string, recentJoins: Join[], | |
// - attempts to execute action; | ||
// - in case of success, returns `{ok: undefined}`, in case of error, returns `{error: string}`. | ||
async function execSinceCommandAux(destinationRoomId: string, event: any, mjolnir: Mjolnir, lexer: Lexer): Promise<Result<undefined>> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rather than passing the lexer to commands, wouldn't it be better if commands declared what arguments they are expecting (can be part of the same api to register with the handler)? This means that they don't need to know implementation detail of the lexer There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would like to move towards what you suggest. I'm not sure I'll succeed in a single pass, though. |
||
console.debug("YORIC", "lexer", lexer); | ||
|
||
// Attempt to parse `<date/duration>` as a date or duration. | ||
let dateOrDurationToken: Date | number; | ||
try { | ||
dateOrDurationToken = lexer.alternatives( | ||
() => lexer.consume("date"), | ||
() => lexer.consume("duration") | ||
); | ||
dateOrDurationToken = lexer.token("dateOrDuration").value; | ||
} catch (ex) { | ||
return { error: "Invalid <date/duration>" }; | ||
} | ||
|
@@ -93,7 +88,7 @@ async function execSinceCommandAux(destinationRoomId: string, event: any, mjolni | |
} | ||
|
||
// Attempt to parse `<action>` as Action. | ||
let actionToken = lexer.consume("id").text; | ||
let actionToken = lexer.token("id").text; | ||
let action: Action | null = null; | ||
for (let key in Action) { | ||
const maybeAction = Action[key as keyof typeof Action]; | ||
|
@@ -108,16 +103,16 @@ async function execSinceCommandAux(destinationRoomId: string, event: any, mjolni | |
console.debug("YORIC", "action", action); | ||
|
||
// Attempt to parse `<limit>` as a number. | ||
const maxEntries = lexer.consume("int").value as number; | ||
const maxEntries = lexer.token("int").value as number; | ||
console.debug("YORIC", "maxEntries", maxEntries); | ||
|
||
// Now list affected rooms. | ||
const rooms: Set</* room id */string> = new Set(); | ||
do { | ||
try { | ||
let token = lexer.alternatives( | ||
() => lexer.consume("STAR"), | ||
() => lexer.consume("roomAliasOrID"), | ||
() => lexer.token("STAR"), | ||
() => lexer.token("roomAliasOrID"), | ||
); | ||
if (token.type == "STAR") { | ||
for (let roomId of Object.keys(mjolnir.protectedRooms)) { | ||
|
@@ -147,8 +142,8 @@ async function execSinceCommandAux(destinationRoomId: string, event: any, mjolni | |
|
||
// Parse everything else as `<reason>`, stripping quotes if any have been added. | ||
const reason = lexer.alternatives( | ||
() => lexer.consume("string"), | ||
() => lexer.consume("EVERYTHING ELSE") | ||
() => lexer.token("string"), | ||
() => lexer.token("EVERYTHING ELSE") | ||
).text; | ||
console.debug("YORIC", "reason", reason); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what happens when the lexer fails then? how different is it from what happens now?