Debian Codegraph @

src/com/google/javascript/jscomp/jsonml/JsonMLAst.java

raw symbols SCIP
1 /*
2 * Copyright 2010 The Closure Compiler Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.google.javascript.jscomp.jsonml;
18
19 import com.google.common.base.Preconditions;
20 import com.google.javascript.jscomp.AbstractCompiler;
21 import com.google.javascript.jscomp.AstValidator;
22 import com.google.javascript.jscomp.SourceAst;
23 import com.google.javascript.jscomp.SourceFile;
24 import com.google.javascript.rhino.InputId;
25 import com.google.javascript.rhino.Node;
26
27 import java.util.ArrayDeque;
28 import java.util.Deque;
29
30 /**
31 * Generates an AST from a JsonML source file.
32 *
33 * JsonML format for representation of JavaScript is specified
34 * <a href="http://code.google.com/p/es-lab/wiki/JsonMLASTFormat">here.</a>
35 *
36 * @author [email protected] (Daniel Hans)
37 *
38 */
39 public class JsonMLAst implements SourceAst {
40 private static final long serialVersionUID = 1L;
41 private static final String DEFAULT_SOURCE_NAME = "[[jsonmlsource]]";
42
43 /*
44 * Root element of JavaScript source which is represented by a JsonML tree.
45 * See JsonML class for more details.
46 */
47 private JsonML jsonml;
48
49 /*
50 * Root node of internal JS Compiler AST which represents the same source.
51 * In order to get the tree, getAstRoot() has to be called.
52 */
53 private Node root;
54
55 private final SourceFile sourceFile;
56 private final InputId inputId;
57
58 public JsonMLAst(JsonML jsonml) {
59 this.jsonml = jsonml;
60 this.inputId = new InputId(getSourceName());
61 this.sourceFile = new SourceFile(getSourceName());
62 }
63
64 @Override
65 public void clearAst() {
66 root = null;
67 }
68
69 /**
70 * Generates AST based on AST representation
71 * @see com.google.javascript.jscomp.SourceAst#getAstRoot(AbstractCompiler)
72 */
73 @Override
74 public Node getAstRoot(AbstractCompiler compiler) {
75 if (root == null) {
76 createAst(compiler);
77 }
78 return root;
79 }
80
81 @Override
82 public SourceFile getSourceFile() {
83 return null;
84 }
85
86 @Override
87 public void setSourceFile(SourceFile file) {
88 throw new UnsupportedOperationException(
89 "JsonMLAst cannot be associated with a SourceFile instance.");
90 }
91
92 public String getSourceName() {
93 Object obj = jsonml.getAttribute(TagAttr.SOURCE);
94 if (obj instanceof String) {
95 return (String) obj;
96 } else {
97 return DEFAULT_SOURCE_NAME;
98 }
99 }
100
101 private void createAst(AbstractCompiler compiler) {
102 Reader translator = new Reader();
103 translator.setRootElement(jsonml);
104 try {
105 root = translator.parse(compiler);
106 root.setInputId(inputId);
107 root.setStaticSourceFile(sourceFile);
108 new AstValidator().validateScript(root);
109 } catch (JsonMLException e) {
110 // compiler should already have JSErrors
111 }
112 }
113
114 public JsonML convertToJsonML () {
115 if (root != null) {
116 Writer converter = new Writer();
117 return converter.processAst(root);
118 }
119 return null;
120 }
121
122 /**
123 * Returns a JsonML element with the specified number from the tree in
124 * pre-order walk.
125 *
126 * @return nth node or null if the node does not exists
127 */
128 public JsonML getElementPreOrder(int n) {
129 Preconditions.checkState(jsonml != null);
130
131 if (n == 0) {
132 return jsonml;
133 }
134
135 Deque<WalkHelper> stack =
136 new ArrayDeque<WalkHelper>();
137 stack.push(new WalkHelper(jsonml, 0));
138 int i = 0;
139 while (i <= n && !stack.isEmpty()) {
140 WalkHelper current = stack.pop();
141 JsonML element = current.element;
142 Integer childno = current.childno;
143
144 // not all the children of this node have been visited
145 if (childno < element.childrenSize()) {
146 stack.push(new WalkHelper(element, childno + 1));
147 // we visit the next child
148 i++;
149 element = element.getChild(childno);
150
151 if (i == n) {
152 return element;
153 }
154
155 // put the next child on the stack to preserve pre-order
156 stack.push(new WalkHelper(element, 0));
157 }
158 }
159 return null;
160 }
161
162 /*
163 * Represents a walk step while the JsonML tree is traversed.
164 */
165 private static class WalkHelper {
166 // JsonML element that corresponds to this step
167 final JsonML element;
168
169 // number of children of the element which has already been visited
170 final int childno;
171
172 WalkHelper(JsonML element, int childno) {
173 this.element = element;
174 this.childno = childno;
175 }
176 }
177
178 @Override
179 public InputId getInputId() {
180 return inputId;
181 }
182 }