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

FormulasQuestion / moodle-qtype_formulas / 13217446514

08 Feb 2025 04:37PM UTC coverage: 76.899% (+1.9%) from 75.045%
13217446514

Pull #62

github

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

2547 of 3139 new or added lines in 22 files covered. (81.14%)

146 existing lines in 6 files now uncovered.

3006 of 3909 relevant lines covered (76.9%)

438.31 hits per line

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

93.75
/classes/local/input_stream.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
 * Helper class to go through the input, used by the tokenizer.
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 input_stream {
27
    /** @var string */
28
    const EOF = '';
29

30
    /** @var int current position in the input */
31
    private int $position = -1;
32

33
    /** @var int the row (line number) of the current character */
34
    private int $row = 1;
35

36
    /** @var int the column number of the current character */
37
    private int $column = 0;
38

39
    /** @var int the length of the input */
40
    private int $length = 0;
41

42
    /** @var array array containing the input's individual characters */
43
    private array $input = [];
44

45
    /**
46
     * Constructor
47
     *
48
     * @param string $str the raw input
49
     */
50
    public function __construct(string $str) {
51
        // Input can contain unicode characters, so it's better to operate on an array of characters
52
        // rather than a string.
53
        $this->input = mb_str_split($str);
34✔
54
        $this->length = count($this->input);
34✔
55
    }
56

57
    /**
58
     * Return the next character of the stream, without consuming it. The optional
59
     * parameter allows to retrieve characters farther behind, if they exist.
60
     *
61
     * @param int $skip skip a certain number of characters
62
     * @return string
63
     */
64
    public function peek(int $skip = 0): string {
65
        if ($this->position < $this->length - $skip - 1) {
34✔
66
            return $this->input[$this->position + $skip + 1];
34✔
67
        }
68
        return self::EOF;
34✔
69
    }
70

71
    /**
72
     * Return the next character of the stream and move the position index one step forward.
73
     *
74
     * @return string
75
     */
76
    public function read(): string {
77
        $nextchar = $this->peek();
34✔
78
        if ($nextchar !== self::EOF) {
34✔
79
            $this->advance($nextchar === "\n");
34✔
80
        }
81
        return $nextchar;
34✔
82
    }
83

84
    /**
85
     * Advance the position index by one and keep row/column numbers in sync.
86
     *
87
     * @param bool $newline whether we are jumping to the next line
88
     * @return void
89
     */
90
    private function advance(bool $newline): void {
91
        $this->position++;
34✔
92
        $this->column++;
34✔
93
        if ($newline) {
34✔
94
            $this->row++;
17✔
95
            $this->column = 0;
17✔
96
        }
97
    }
98

99
    /**
100
     * Stop processing the input and indicate the human readable position (row/column) where
101
     * the error occurred.
102
     *
103
     * @param string $message error message
104
     * @return void
105
     * @throws Exception
106
     */
107
    public function die(string $message): void {
108
        throw new \Exception($this->row . ':' . $this->column . ':' . $message);
17✔
109
    }
110

111
    /**
112
     * Return the human readable position (row/column) of the current character in the input stream
113
     *
114
     * @return int[] position, associative array with keys 'row' and 'column'
115
     */
116
    public function get_position(): array {
NEW
117
        return ['row' => $this->row, 'column' => $this->column];
×
118
    }
119
}
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