Skip to content

Commit 79a7c6c

Browse files
committed
fix for failing validation test, add syntax check in parser
1 parent 220db49 commit 79a7c6c

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

classes/local/answer_parser.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ public function __construct($tokenlist, array $knownvariables = [], bool $caretm
6969
// If we only have one single token and it is an empty string, we set it to the $EMPTY token.
7070
$firsttoken = reset($tokenlist);
7171
if (count($tokenlist) === 1 && $firsttoken->value === '') {
72-
$tokenlist[0] = new token(token::EMPTY, '$EMPTY', $firsttoken->row, $firsttoken->column);
72+
// FIXME: temporarily disabling this
73+
// $tokenlist[0] = new token(token::EMPTY, '$EMPTY', $firsttoken->row, $firsttoken->column);
7374
}
7475

7576
// Once this is done, we can parse the expression normally.

classes/local/parser.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,17 @@ private function parse_general_expression(?int $stopat = null): expression {
364364
$this->die(get_string('error_invalidrangesep', 'qtype_formulas'), $nexttoken);
365365
}
366366

367+
// We do not allow to a closing paren immediately after an opening paren of the same type.
368+
$openingparen = ($type & token::ANY_OPENING_PAREN);
369+
$nextclosing = ($nexttype & token::ANY_CLOSING_PAREN);
370+
if ($openingparen && $nextclosing) {
371+
$a = (object)[
372+
'open' => $value,
373+
'close' => $nextvalue,
374+
];
375+
$this->die(get_string('error_emptyparens', 'qtype_formulas', $a), $nexttoken);
376+
}
377+
367378
// If we're one token away from the end of the statement, we just read and discard the end-of-statement marker.
368379
if ($nexttype === token::END_OF_STATEMENT || $nexttype === token::END_GROUP) {
369380
$this->read_next();

lang/en/qtype_formulas.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@
141141
$string['error_distribution_outcomes'] = '{$a} expects the number of successful outcomes to be a non-negative integer.';
142142
$string['error_distribution_tries'] = '{$a} expects the number of tries to be a non-negative integer.';
143143
$string['error_divzero'] = 'Division by zero is not defined.';
144+
$string['error_emptyparens'] = 'Parse error: closing {$a->close} immediately after opening {$a->open}.';
144145
$string['error_emptyrange'] = 'Evaluation error: range from {$a->start} to {$a->end} with step {$a->step} will be empty.';
145146
$string['error_emptystack'] = 'Evaluation error: empty stack - did you pass enough arguments for the function or operator?';
146147
$string['error_evaluate_invocation'] = 'Bad invocation of {$a}.';

0 commit comments

Comments
 (0)