Web Inspector: Breakpoint Log action should support template literals
[WebKit-https.git] / Source / WebInspectorUI / UserInterface / Controllers / BreakpointLogMessageLexer.js
1 /*
2  * Copyright (C) 2016 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 WebInspector.BreakpointLogMessageLexer = class BreakpointLogMessageLexer extends WebInspector.Object
27 {
28     constructor()
29     {
30         super();
31
32         this._stateFunctions = {
33             [WebInspector.BreakpointLogMessageLexer.State.Expression]: this._expression,
34             [WebInspector.BreakpointLogMessageLexer.State.PlainText]: this._plainText,
35             [WebInspector.BreakpointLogMessageLexer.State.PossiblePlaceholder]: this._possiblePlaceholder,
36             [WebInspector.BreakpointLogMessageLexer.State.RegExpOrStringLiteral]: this._regExpOrStringLiteral,
37         };
38
39         this.reset();
40     }
41
42     // Public
43
44     tokenize(input)
45     {
46         this.reset();
47         this._input = input;
48
49         while (this._index < this._input.length) {
50             let stateFunction = this._stateFunctions[this._states.lastValue];
51             console.assert(stateFunction);
52             if (!stateFunction) {
53                 this.reset();
54                 return null;
55             }
56
57             stateFunction.call(this);
58         }
59
60         // Needed for trailing plain text.
61         this._finishPlainText();
62
63         return this._tokens;
64     }
65
66     reset()
67     {
68         this._input = "";
69         this._buffer = "";
70
71         this._index = 0;
72         this._states = [WebInspector.BreakpointLogMessageLexer.State.PlainText];
73         this._literalStartCharacter = "";
74         this._curlyBraceDepth = 0;
75         this._tokens = [];
76     }
77
78     // Private
79
80      _finishPlainText()
81     {
82         this._appendToken(WebInspector.BreakpointLogMessageLexer.TokenType.PlainText);
83     }
84
85     _finishExpression()
86     {
87         this._appendToken(WebInspector.BreakpointLogMessageLexer.TokenType.Expression);
88     }
89
90     _appendToken(type)
91     {
92         if (!this._buffer)
93             return;
94
95         this._tokens.push({type, data: this._buffer});
96         this._buffer = "";
97     }
98
99     _consume()
100     {
101         console.assert(this._index < this._input.length);
102
103         let character = this._peek();
104         this._index++;
105         return character;
106     }
107
108     _peek()
109     {
110         return this._input[this._index] || null;
111     }
112
113     // States
114
115     _expression()
116     {
117         let character = this._consume();
118
119         if (character === "}") {
120             if (this._curlyBraceDepth === 0) {
121                 this._finishExpression();
122
123                 console.assert(this._states.lastValue === WebInspector.BreakpointLogMessageLexer.State.Expression);
124                 this._states.pop();
125                 return;
126             }
127
128             this._curlyBraceDepth--;
129         }
130
131         this._buffer += character;
132
133         if (character === "/" || character === "\"" || character === "'") {
134             this._literalStartCharacter = character;
135             this._states.push(WebInspector.BreakpointLogMessageLexer.State.RegExpOrStringLiteral);
136         } else if (character === "{")
137             this._curlyBraceDepth++;
138     }
139
140     _plainText()
141     {
142         let character = this._peek();
143
144         if (character === "$")
145             this._states.push(WebInspector.BreakpointLogMessageLexer.State.PossiblePlaceholder);
146         else {
147             this._buffer += character;
148             this._consume();
149         }
150     }
151
152     _possiblePlaceholder()
153     {
154         let character = this._consume();
155         console.assert(character === "$")
156         let nextCharacter = this._peek();
157
158         console.assert(this._states.lastValue === WebInspector.BreakpointLogMessageLexer.State.PossiblePlaceholder);
159         this._states.pop();
160
161         if (nextCharacter === "{") {
162             this._finishPlainText();
163             this._consume();
164             this._states.push(WebInspector.BreakpointLogMessageLexer.State.Expression);
165         } else
166             this._buffer += character;
167     }
168
169     _regExpOrStringLiteral()
170     {
171         let character = this._consume();
172         this._buffer += character;
173
174         if (character === "\\") {
175             if (this._peek() !== null)
176                 this._buffer += this._consume();
177             return;
178         }
179
180         if (character === this._literalStartCharacter) {
181             console.assert(this._states.lastValue === WebInspector.BreakpointLogMessageLexer.State.RegExpOrStringLiteral);
182             this._states.pop();
183         }
184     }
185 };
186
187 WebInspector.BreakpointLogMessageLexer.State = {
188     Expression: Symbol("expression"),
189     PlainText: Symbol("plain-text"),
190     PossiblePlaceholder: Symbol("possible-placeholder"),
191     RegExpOrStringLiteral: Symbol("regexp-or-string-literal"),
192 };
193
194 WebInspector.BreakpointLogMessageLexer.TokenType = {
195     PlainText: "token-type-plain-text",
196     Expression: "token-type-expression",
197 };