Issues (12)

src/JsonParser.php (1 issue)

1
<?php
2
3
namespace petitparser;
4
5
/**
6
 * Example JSON parser.
7
 */
8
class JsonParser extends CompositeParser
9
{
10 1
    protected function initialize()
11
    {
12 1
        $this->defineGrammar();
13 1
        $this->defineActions();
14 1
    }
15
16
    static $escapeTable = array(
17
        "\\" => "\\",
18
        "/"  => "/",
19
        '"'  => '"',
20
        "b"  => "\b",
21
        "f"  => "\f",
22
        "n"  => "\n",
23
        "r"  => "\r",
24
        "t"  => "\t",
25
    );
26
27 1
    private function defineGrammar()
28
    {
29 1
        $this->def('start', $this->ref('value')->end_());
30
31 1
        $this->def('array',
32 1
            char('[')
33 1
                ->trim()
34 1
                ->seq($this->ref('elements')->optional())
35 1
                ->seq(char(']')->trim()));
36
37 1
        $this->def('elements',
38 1
            $this->ref('value')->separatedBy(char(',')->trim(), false));
39
40 1
        $this->def('members',
41 1
            $this->ref('pair')->separatedBy(char(',')->trim(), false));
42
43 1
        $this->def('object',
44 1
            char('{')
45 1
                ->trim()
46 1
                ->seq($this->ref('members')->optional())
47 1
                ->seq(char('}')->trim()));
48
49 1
        $this->def('pair',
50 1
            $this->ref('stringToken')
51 1
                ->seq(char(':')->trim())
52 1
                ->seq($this->ref('value')));
53
54 1
        $this->def('value',
55 1
            $this->ref('stringToken')
56 1
                ->or_($this->ref('numberToken'))
57 1
                ->or_($this->ref('object'))
58 1
                ->or_($this->ref('array'))
59 1
                ->or_($this->ref('trueToken'))
60 1
                ->or_($this->ref('falseToken'))
61 1
                ->or_($this->ref('nullToken')));
62
63 1
        $this->def('trueToken', string('true')->flatten()->trim());
64 1
        $this->def('falseToken', string('false')->flatten()->trim());
65 1
        $this->def('nullToken', string('null')->flatten()->trim());
66 1
        $this->def('stringToken', $this->ref('stringPrimitive')->flatten()->trim());
67 1
        $this->def('numberToken', $this->ref('numberPrimitive')->flatten()->trim());
68
69 1
        $this->def('characterPrimitive',
70 1
            $this->ref('characterEscape')
71 1
                ->or_($this->ref('characterOctal'))
72 1
                ->or_($this->ref('characterNormal')));
73
74 1
        $this->def('characterEscape',
75 1
            char("\\")->seq(anyIn(array_keys(JsonParser::$escapeTable))));
0 ignored issues
show
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
76
77 1
        $this->def('characterNormal',
78 1
            anyIn("\"\\")->neg());
79
80 1
        $this->def('characterOctal',
81 1
            string("\\u")->seq(pattern("0-9A-Fa-f")->times(4)->flatten()));
82
83 1
        $this->def('numberPrimitive',
84 1
            char('-')->optional()
85 1
                ->seq(char('0')->or_(digit()->plus()))
86 1
                ->seq(char('.')->seq(digit()->plus())->optional())
87 1
                ->seq(anyIn('eE')->seq(anyIn('-+')->optional())->seq(digit()->plus())->optional()));
88
89 1
        $this->def('stringPrimitive',
90 1
            char('"')
91 1
                ->seq($this->ref('characterPrimitive')->star())
92 1
                ->seq(char('"')));
93 1
    }
94
95 1
    private function defineActions()
96
    {
97 1
        $this->action(
98 1
            'array',
99
            function ($each) {
100 1
                return $each[1] !== null ? $each[1] : array();
101
            }
102 1
        );
103
104 1
        $this->action(
105 1
            'object',
106
            function ($each) {
107 1
                $result = array();
108 1
                if ($each[1] !== null) {
109 1
                    foreach ($each[1] as $element) {
110 1
                        $result[$element[0]] = $element[2];
111 1
                    }
112 1
                }
113 1
                return $result;
114
            }
115 1
        );
116
117 1
        $this->action(
118 1
            'trueToken',
119
            function ($each) {
120 1
                return true;
121
            }
122 1
        );
123
124 1
        $this->action(
125 1
            'falseToken',
126
            function ($each) {
127 1
                return false;
128
            }
129 1
        );
130
131 1
        $this->action(
132 1
            'nullToken',
133
            function ($each) {
134 1
                return null;
135
            }
136 1
        );
137
138 1
        $self = $this;
139
140 1
        $this->redef(
141 1
            'stringToken',
142
            function (Parser $parser) use ($self) {
143 1
                return $self->ref('stringPrimitive')->trim();
144
            }
145 1
        );
146
147 1
        $this->action(
148 1
            'numberToken',
149
            function ($each) {
150 1
                $float = floatval($each);
151 1
                $int = intval($float);
152
153 1
                if (($float == $int) && (false === strpos($each, '.'))) {
154 1
                    return $int;
155
                } else {
156 1
                    return $float;
157
                }
158
            }
159 1
        );
160
161 1
        $this->action(
162 1
            'stringPrimitive',
163
            function ($each) {
164 1
                return implode('', $each[1]);
165
            }
166 1
        );
167
168 1
        $this->action(
169 1
            'characterEscape',
170
            function ($each) {
171 1
                return JsonParser::$escapeTable[$each[1]];
172
            }
173 1
        );
174
175 1
        $this->action(
176 1
            'characterOctal',
177
            function ($each) {
178
                throw new \Exception('Support for octal characters not implemented');
179
            }
180 1
        );
181 1
    }
182
}
183