summaryrefslogtreecommitdiff
path: root/app/Core/Lexer.php
blob: ca2ef8957aa3510628b0bc4d5e73af99970769e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php

namespace Kanboard\Core;

/**
 * Lexer
 *
 * @package  core
 * @author   Frederic Guillot
 */
class Lexer
{
    /**
     * Current position
     *
     * @access private
     * @var integer
     */
    private $offset = 0;

    /**
     * Token map
     *
     * @access private
     * @var array
     */
    private $tokenMap = array(
        "/^(assignee:)/"                                 => 'T_ASSIGNEE',
        "/^(color:)/"                                    => 'T_COLOR',
        "/^(due:)/"                                      => 'T_DUE',
        "/^(updated:)/"                                  => 'T_UPDATED',
        "/^(modified:)/"                                 => 'T_UPDATED',
        "/^(created:)/"                                  => 'T_CREATED',
        "/^(status:)/"                                   => 'T_STATUS',
        "/^(description:)/"                              => 'T_DESCRIPTION',
        "/^(category:)/"                                 => 'T_CATEGORY',
        "/^(column:)/"                                   => 'T_COLUMN',
        "/^(project:)/"                                  => 'T_PROJECT',
        "/^(swimlane:)/"                                 => 'T_SWIMLANE',
        "/^(ref:)/"                                      => 'T_REFERENCE',
        "/^(reference:)/"                                => 'T_REFERENCE',
        "/^(\s+)/"                                       => 'T_WHITESPACE',
        '/^([<=>]{0,2}[0-9]{4}-[0-9]{2}-[0-9]{2})/'      => 'T_DATE',
        '/^(yesterday|tomorrow|today)/'                  => 'T_DATE',
        '/^("(.*?)")/'                                   => 'T_STRING',
        "/^(\w+)/"                                       => 'T_STRING',
        "/^(#\d+)/"                                      => 'T_STRING',
    );

    /**
     * Tokenize input string
     *
     * @access public
     * @param  string  $input
     * @return array
     */
    public function tokenize($input)
    {
        $tokens = array();
        $this->offset = 0;

        while (isset($input[$this->offset])) {
            $result = $this->match(substr($input, $this->offset));

            if ($result === false) {
                return array();
            }

            $tokens[] = $result;
        }

        return $tokens;
    }

    /**
     * Find a token that match and move the offset
     *
     * @access public
     * @param  string  $string
     * @return array|boolean
     */
    public function match($string)
    {
        foreach ($this->tokenMap as $pattern => $name) {
            if (preg_match($pattern, $string, $matches)) {
                $this->offset += strlen($matches[1]);

                return array(
                    'match' => trim($matches[1], '"'),
                    'token' => $name,
                );
            }
        }

        return false;
    }

    /**
     * Change the output of tokenizer to be easily parsed by the database filter
     *
     * Example: ['T_ASSIGNEE' => ['user1', 'user2'], 'T_TITLE' => 'task title']
     *
     * @access public
     * @param  array  $tokens
     * @return array
     */
    public function map(array $tokens)
    {
        $map = array(
            'T_TITLE' => '',
        );

        while (false !== ($token = current($tokens))) {
            switch ($token['token']) {
                case 'T_ASSIGNEE':
                case 'T_COLOR':
                case 'T_CATEGORY':
                case 'T_COLUMN':
                case 'T_PROJECT':
                case 'T_SWIMLANE':
                    $next = next($tokens);

                    if ($next !== false && $next['token'] === 'T_STRING') {
                        $map[$token['token']][] = $next['match'];
                    }

                    break;

                case 'T_STATUS':
                case 'T_DUE':
                case 'T_UPDATED':
                case 'T_CREATED':
                case 'T_DESCRIPTION':
                case 'T_REFERENCE':
                    $next = next($tokens);

                    if ($next !== false && ($next['token'] === 'T_DATE' || $next['token'] === 'T_STRING')) {
                        $map[$token['token']] = $next['match'];
                    }

                    break;

                default:
                    $map['T_TITLE'] .= $token['match'];
                    break;
            }

            next($tokens);
        }

        $map['T_TITLE'] = trim($map['T_TITLE']);

        if (empty($map['T_TITLE'])) {
            unset($map['T_TITLE']);
        }

        return $map;
    }
}