1 | // Copyright (C) 2006 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.js; |
16 | |
17 | import com.google.caja.parser.ParseTreeNode; |
18 | import com.google.caja.util.SyntheticAttributeKey; |
19 | |
20 | // TODO(erights,ihab): Remove the concept of "synthetic" entirely. |
21 | /** |
22 | * Defines a Synthetic Attribute that marks a node as having been generated |
23 | * as part of the compilation process. |
24 | * <p> |
25 | * Such nodes can be exempted from scrutiny by other nodes. For example, |
26 | * javascript identifiers in the {@code *__} namespace are reserved for |
27 | * use by Caja runtime checks. |
28 | * {@link com.google.caja.parser.js.Reference}s and |
29 | * {@link com.google.caja.parser.js.Declaration}s |
30 | * generated by the Caja compile with names like that should be marked synthetic |
31 | * so that they won't be rejected by the validator. But since user code is |
32 | * not synthetic, they will be rejected by the validator. |
33 | * |
34 | * @author mikesamuel@gmail.com |
35 | */ |
36 | public final class SyntheticNodes { |
37 | /** |
38 | * Indicates that a node breaks the caja rules in some way, but that this |
39 | * is OK since it comes from a privileged source, not user code. |
40 | */ |
41 | public static final SyntheticAttributeKey<Boolean> SYNTHETIC |
42 | = new SyntheticAttributeKey<Boolean>(Boolean.class, "synthetic"); |
43 | |
44 | public static boolean isSynthesizable(ParseTreeNode node) { |
45 | return (node instanceof Identifier && node.getValue() != null) |
46 | || node instanceof FunctionConstructor |
47 | || node instanceof UncajoledModule; |
48 | } |
49 | |
50 | /** |
51 | * A convenience function used to mark {@link #isSynthesizable synthesizable} |
52 | * nodes created during source->javascript translation as |
53 | * {@link #SYNTHETIC synthetic}. Nodes corresponsing to javascript |
54 | * embedded in the original will not be synthetic. |
55 | * <p> |
56 | * This is meant to be imported statically. |
57 | */ |
58 | public static <T extends ParseTreeNode> T s(T t) { |
59 | if (isSynthesizable(t)) { |
60 | t.getAttributes().set(SYNTHETIC, Boolean.TRUE); |
61 | } |
62 | return t; |
63 | } |
64 | |
65 | /** A synthetic identifier may occupy the <code>*__</code> namespace. */ |
66 | public static Identifier s(Identifier t) { |
67 | t.getAttributes().set(SYNTHETIC, Boolean.TRUE); |
68 | return t; |
69 | } |
70 | |
71 | /** |
72 | * Temporaries created as a result of rewriting operations inside a synthetic |
73 | * function are actually declared in the closest non-synthetic enclosing |
74 | * function. |
75 | */ |
76 | public static FunctionConstructor s(FunctionConstructor t) { |
77 | t.getAttributes().set(SYNTHETIC, Boolean.TRUE); |
78 | return t; |
79 | } |
80 | |
81 | private SyntheticNodes() { /* uninstantiable */ } |
82 | } |