| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 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 |
|
| 45 |
|
| 46 |
|
| 47 |
private JsonML jsonml; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 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 |
|
| 71 |
|
| 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 |
|
| 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 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 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 |
|
| 145 |
if (childno < element.childrenSize()) { |
| 146 |
stack.push(new WalkHelper(element, childno + 1)); |
| 147 |
|
| 148 |
i++; |
| 149 |
element = element.getChild(childno); |
| 150 |
|
| 151 |
if (i == n) { |
| 152 |
return element; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
stack.push(new WalkHelper(element, 0)); |
| 157 |
} |
| 158 |
} |
| 159 |
return null; |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
private static class WalkHelper { |
| 166 |
|
| 167 |
final JsonML element; |
| 168 |
|
| 169 |
|
| 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 |
} |