• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

FormulasQuestion / moodle-qtype_formulas / 13200038469

07 Feb 2025 12:40PM UTC coverage: 76.583% (+1.5%) from 75.045%
13200038469

Pull #62

github

web-flow
Merge 27bf7cac9 into acd272945
Pull Request #62: Rewrite the parser

2517 of 3116 new or added lines in 22 files covered. (80.78%)

146 existing lines in 6 files now uncovered.

2976 of 3886 relevant lines covered (76.58%)

431.97 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/classes/local/random_parser.php
1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16

17
namespace qtype_formulas\local;
18

19
/**
20
 * Parser for random variables for qtype_formulas
21
 *
22
 * @package    qtype_formulas
23
 * @copyright  2022 Philipp Imhof
24
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25
 */
26
class random_parser extends parser {
27
    /**
28
     * Construct a new parser class for random variables. The main difference compared to the
29
     * general parser is that the assignment '=' operator is modified in order for the evaluator
30
     * to be able to distinguish random variables and normal variables. It also simplifies the
31
     * creation of shuffled (randomized) arrays by making the usage shuffle() optional.
32
     *
33
     * @param [type] $tokenlist list of tokens as returned from the lexer or input string
34
     * @param [type] $knownvariables
35
     */
36
    public function __construct($tokenlist, array $knownvariables = []) {
37
        // If the input is given as a string, run it through the lexer first.
NEW
38
        if (is_string($tokenlist)) {
×
NEW
39
            $lexer = new lexer($tokenlist);
×
NEW
40
            $tokenlist = $lexer->get_tokens();
×
41
        }
42

43
        // We scan all tokens in order to make a few modifications that are specific
44
        // to random variables.
NEW
45
        foreach ($tokenlist as $token) {
×
46
            // When parsing random variables, we change the assignment operator from '=' to 'r=' in order
47
            // for the evaluator to know it has to store them as random variables.
NEW
48
            if ($token->type === token::OPERATOR && $token->value === '=') {
×
NEW
49
                $token->value = 'r=';
×
50
            }
51

52
            // In legacy code, arrays in random variables always had to be used together with
53
            // the shuffle() function, e.g. ar = shuffle([1,2,3]) and shuffle() could *only* be
54
            // used to define random variables. We keep this syntax, but allow defining shuffled
55
            // arrays without actually writing the function, because if the user did not want the
56
            // array to be shuffled, they would define it in the global section rather than the
57
            // random section. Therefore, we silently drop the function while parsing.
NEW
58
            if ($token->type === token::IDENTIFIER && $token->value === 'shuffle') {
×
NEW
59
                $token->value = '';
×
NEW
60
                $token->type = token::FUNCTION;
×
61
            }
62
        }
63

64
        // Once this is done, we can parse the expression normally.
NEW
65
        parent::__construct($tokenlist, $knownvariables);
×
66
    }
67
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc