Improved Sieve parser validation

This commit is contained in:
the-djmaze 2024-09-23 11:23:48 +02:00
parent c773b852a1
commit 257d9201f6
3 changed files with 23 additions and 8 deletions

View file

@ -45,8 +45,16 @@ export class GrammarString /*extends String*/
/** /**
* abstract * abstract
*/ */
export class GrammarComment extends GrammarString export /*abstract*/ class GrammarComment extends GrammarString
{ {
/*
constructor()
{
if (this.constructor == GrammarComment) {
throw Error("Abstract class can't be instantiated.");
}
}
*/
} }
/** /**

View file

@ -18,6 +18,7 @@ import {
import { import {
GrammarBracketComment, GrammarBracketComment,
GrammarCommand, GrammarCommand,
GrammarComment,
GrammarHashComment, GrammarHashComment,
GrammarMultiLine, GrammarMultiLine,
GrammarNumber, GrammarNumber,
@ -28,7 +29,7 @@ import {
} from 'Sieve/Grammar'; } from 'Sieve/Grammar';
import { availableCommands } from 'Sieve/Commands'; import { availableCommands } from 'Sieve/Commands';
import { ConditionalCommand, RequireCommand } from 'Sieve/Commands/Controls'; import { ConditionalCommand, IfCommand, RequireCommand } from 'Sieve/Commands/Controls';
import { NotTest } from 'Sieve/Commands/Tests'; import { NotTest } from 'Sieve/Commands/Tests';
const const
@ -156,10 +157,17 @@ export const parseScript = (script, name = 'script.sieve') => {
let new_command; let new_command;
if (Commands[value]) { if (Commands[value]) {
if ('elsif' === value || 'else' === value) { if ('elsif' === value || 'else' === value) {
// (prev_command instanceof ConditionalCommand) || error('Not after IF condition'); let valid = false, cmd = (command ? command?.commands : tree), i = cmd?.length;
} while (i) {
if ('allof' === value || 'anyof' === value) { cmd[--i];
// (command instanceof ConditionalCommand || command instanceof NotTest) || error('Test-list not in conditional'); if (cmd[i] instanceof IfCommand) {
valid = true;
break;
} else if (typeof cmd[i] !== 'string' && !(cmd[i] instanceof GrammarComment)) {
break;
}
}
valid || error('Not after IF/ELSIF condition');
} }
new_command = new Commands[value](); new_command = new Commands[value]();
} else { } else {
@ -266,7 +274,6 @@ export const parseScript = (script, name = 'script.sieve') => {
break; break;
case T_BLOCK_END: case T_BLOCK_END:
(command instanceof ConditionalCommand) || error(TokenError[type]); (command instanceof ConditionalCommand) || error(TokenError[type]);
// prev_command = command;
command = levels.up(); command = levels.up();
break; break;

View file

@ -11,7 +11,7 @@
<div class="control-group" data-bind="css: {'error': nameError}, hidden: exists"> <div class="control-group" data-bind="css: {'error': nameError}, hidden: exists">
<input type="text" class="span5" <input type="text" class="span5"
data-bind="value: name, hasfocus: !exists()" data-bind="value: name"
autocorrect="off" autocapitalize="off" autocorrect="off" autocapitalize="off"
data-i18n="[placeholder]GLOBAL/NAME"> data-i18n="[placeholder]GLOBAL/NAME">
</div> </div>