1<?php
2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
3//
4// All Rights Reserved. See copyright.txt for details and a complete list of authors.
5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
6// $Id$
7
8class Math_Formula_Tokenizer
9{
10	function getTokens($string)
11	{
12		$tokens = [];
13
14		$len = strlen($string);
15		$current = '';
16		$inString = false;
17
18		for ($i = 0; $len > $i; ++$i) {
19			$chr = $string{$i};
20
21			$end = false;
22			$extra = null;
23
24			if ($chr == '"') {
25				$current .= $chr;
26				$inString = ! $inString;
27				$end = ! $inString;
28			} elseif ($inString) {
29				$current .= $chr;
30			} elseif (ctype_space($chr)) {
31				$end = true;
32			} elseif ($chr == '(' || $chr == ')') {
33				$extra = $chr;
34				$end = true;
35			} else {
36				$current .= $chr;
37			}
38
39			if ($end && 0 != strlen($current)) {
40				$tokens[] = $current;
41				$current = '';
42			}
43
44			if ($extra) {
45				$tokens[] = $extra;
46			}
47		}
48
49		if (strlen($current) != 0) {
50			$tokens[] = $current;
51		}
52
53		return $tokens;
54	}
55}
56