com.shapesecurity.shift.scope.ScopeSerializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of shift Show documentation
Show all versions of shift Show documentation
Shift format ECMAScript 6 AST tooling
package com.shapesecurity.shift.scope;
import com.shapesecurity.functional.Pair;
import com.shapesecurity.functional.data.HashTable;
import com.shapesecurity.functional.data.ImmutableList;
import com.shapesecurity.shift.ast.BindingIdentifier;
import com.shapesecurity.shift.ast.IdentifierExpression;
import com.shapesecurity.shift.ast.Node;
import java.util.*;
public class ScopeSerializer {
private Map nodeToID = new IdentityHashMap<>();
private int currentID = 0;
public String serializeScope(Scope scope) {
String serialized = "{";
serialized += "\"node\": \"" + serializeNode(scope.astNode) + "\"";
serialized += ", \"type\": \"" + scope.type + "\"";
serialized += ", \"isDynamic\": " + scope.dynamic;
serialized += ", \"through\": " + serializeReferenceList(collectThrough(scope.through));
serialized += ", \"variables\": " + serializeVariableList(scope.variables());
serialized += ", \"children\": " + serializeScopeList(scope.children);
return serialized + "}";
}
private String serializeNode(Node node) {
if (!nodeToID.containsKey(node)) {
nodeToID.put(node, currentID);
currentID++;
}
if (node instanceof IdentifierExpression) {
return node.getClass().getSimpleName() + "(" + ((IdentifierExpression) node).name + ")_" + nodeToID.get(node);
} else if (node instanceof BindingIdentifier) {
return node.getClass().getSimpleName() + "(" + ((BindingIdentifier) node).name + ")_" + nodeToID.get(node);
} else {
return node.getClass().getSimpleName() + "_" + nodeToID.get(node);
}
}
private ImmutableList collectThrough(HashTable> through) {
List references = new ArrayList<>();
for (Pair> entry : through.entries()) {
for (Reference reference : entry.b) {
references.add(reference);
}
}
return ImmutableList.from(references);
}
private String serializeScopeList(ImmutableList scopes) {
String serialized = "[";
for (Scope scope : scopes) {
serialized += serializeScope(scope) + ", ";
}
if (scopes.length > 0) {
serialized = serialized.substring(0, serialized.length() - 2);
}
serialized += "]";
return serialized;
}
private String serializeReference(Reference reference) {
String serialized = "{";
serialized += "\"node\": \"" + reference.node.either(this::serializeNode, this::serializeNode) + "\"";
serialized += ", \"accessibility\": \"" + reference.accessibility + "\"";
serialized += "}";
return serialized;
}
private String serializeReferenceList(ImmutableList references) {
String serialized = "[";
for (Reference reference : references) {
serialized += serializeReference(reference) + ", ";
}
if (references.length > 0) {
serialized = serialized.substring(0, serialized.length() - 2);
}
serialized += "]";
return serialized;
}
private String serializeDeclaration(Declaration declaration) {
String serialized = "{";
serialized += "\"node\": \"" + serializeNode(declaration.node) + "\"";
serialized += ", \"kind\": \"" + declaration.kind + "\"";
serialized += "}";
return serialized;
}
private String serializeDeclarationList(ImmutableList declarations) {
String serialized = "[";
for (Declaration declaration : declarations) {
serialized += serializeDeclaration(declaration) + ", ";
}
if (declarations.length > 0) {
serialized = serialized.substring(0, serialized.length() - 2);
}
serialized += "]";
return serialized;
}
private String serializeVariable(Variable variable) {
String serialized = "{";
serialized += "\"name\": \"" + variable.name + "\"";
serialized += ", \"references\": " + serializeReferenceList(variable.references);
serialized += ", \"declarations\": " + serializeDeclarationList(variable.declarations);
serialized += "}";
return serialized;
}
private String serializeVariableList(Collection variables) {
String serialized = "[";
for (Variable variable : variables) {
serialized += serializeVariable(variable) + ", ";
}
if (variables.size() > 0) {
serialized = serialized.substring(0, serialized.length() - 2);
}
serialized += "]";
return serialized;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy