1 | // Copyright (C) 2008 Google Inc. |
2 | // |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | // you may not use this file except in compliance with the License. |
5 | // You may obtain a copy of the License at |
6 | // |
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | // |
9 | // Unless required by applicable law or agreed to in writing, software |
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | // See the License for the specific language governing permissions and |
13 | // limitations under the License. |
14 | |
15 | package com.google.caja.parser.quasiliteral; |
16 | |
17 | import com.google.caja.lexer.FilePosition; |
18 | import com.google.caja.parser.ParseTreeNode; |
19 | import com.google.caja.parser.js.Identifier; |
20 | |
21 | import java.util.List; |
22 | import java.util.Map; |
23 | |
24 | /** |
25 | * A quasi-hole that matches an identifier with a null value. |
26 | * @author mikesamuel@gmail.com |
27 | */ |
28 | final class SingleOptionalIdentifierQuasiNode extends QuasiNode { |
29 | private final QuasiNode qn; |
30 | |
31 | SingleOptionalIdentifierQuasiNode(QuasiNode qn) { this.qn = qn; } |
32 | |
33 | @Override |
34 | protected boolean consumeSpecimens( |
35 | List<ParseTreeNode> specimens, Map<String, ParseTreeNode> bindings) { |
36 | if (specimens.isEmpty()) { return false; } |
37 | ParseTreeNode specimen = specimens.get(0); |
38 | if (specimen instanceof Identifier && null == specimen.getValue()) { |
39 | specimens.remove(0); |
40 | return true; |
41 | } |
42 | return qn.consumeSpecimens(specimens, bindings); |
43 | } |
44 | |
45 | @Override |
46 | protected boolean createSubstitutes( |
47 | List<ParseTreeNode> substitutes, Map<String, ParseTreeNode> bindings) { |
48 | // Do we need to back out changes to bindings? |
49 | if (qn.createSubstitutes(substitutes, bindings)) { |
50 | return true; |
51 | } else { |
52 | substitutes.add(new Identifier(FilePosition.UNKNOWN, null)); |
53 | return true; |
54 | } |
55 | } |
56 | } |