Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Output Kythe mapping metadata in the J2ObjC header generator.
Change on 2018/06/15 by duffin <duffin@google.com> ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=200768666
- Loading branch information
duffin
authored and
Tom Ball
committed
Jul 31, 2018
1 parent
b3ce6b7
commit 4f6025d
Showing
12 changed files
with
435 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
translator/src/main/java/com/google/devtools/j2objc/gen/GeneratedSourceMappings.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.devtools.j2objc.gen; | ||
|
||
import com.google.devtools.j2objc.ast.MethodDeclaration; | ||
import com.google.devtools.j2objc.ast.Name; | ||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
/** | ||
* Contains byte-offset mappings between identifiers in a source .java file and their equivalents in | ||
* a generated Objective-C header file. | ||
* | ||
* <p>These mappings may be converted to {@link | ||
* com.google.devtools.j2objc.gen.KytheIndexingMetadata} objects to support cross-language links in | ||
* <a href="http://kythe.io">Kythe.</a> | ||
*/ | ||
public class GeneratedSourceMappings { | ||
|
||
/** Maps an identifier from a byte range in a source file and target file. */ | ||
public static class Mapping { | ||
private final String identifier; | ||
private final int sourceBegin; | ||
private final int sourceEnd; | ||
private final int targetBegin; | ||
private final int targetEnd; | ||
|
||
private Mapping( | ||
String identifier, int sourceBegin, int sourceEnd, int targetBegin, int targetEnd) { | ||
this.identifier = identifier; | ||
this.sourceBegin = sourceBegin; | ||
this.sourceEnd = sourceEnd; | ||
this.targetBegin = targetBegin; | ||
this.targetEnd = targetEnd; | ||
} | ||
|
||
static Mapping fromMethodDeclaration( | ||
MethodDeclaration methodDeclaration, int targetBegin, int targetEnd) { | ||
assert methodDeclaration.getName() != null; | ||
Name name = methodDeclaration.getName(); | ||
int sourceBegin = name.getStartPosition(); | ||
int sourceEnd = sourceBegin + name.getLength(); | ||
return new Mapping(name.toString(), sourceBegin, sourceEnd, targetBegin, targetEnd); | ||
} | ||
|
||
public String getIdentifier() { | ||
return identifier; | ||
} | ||
|
||
public int getSourceBegin() { | ||
return sourceBegin; | ||
} | ||
|
||
public int getSourceEnd() { | ||
return sourceEnd; | ||
} | ||
|
||
public int getTargetBegin() { | ||
return targetBegin; | ||
} | ||
|
||
public int getTargetEnd() { | ||
return targetEnd; | ||
} | ||
} | ||
|
||
private final Set<Mapping> mappings = new HashSet<>(); | ||
private int targetOffset = 0; | ||
|
||
public void addMethodMapping(MethodDeclaration methodDeclaration, int targetBegin, int length) { | ||
mappings.add( | ||
Mapping.fromMethodDeclaration(methodDeclaration, targetBegin, targetBegin + length)); | ||
} | ||
|
||
public Set<Mapping> getMappings() { | ||
return Collections.unmodifiableSet(mappings); | ||
} | ||
|
||
public void setTargetOffset(int targetOffset) { | ||
this.targetOffset = targetOffset; | ||
} | ||
|
||
public int getTargetOffset() { | ||
return targetOffset; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
translator/src/main/java/com/google/devtools/j2objc/gen/KytheIndexingMetadata.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.devtools.j2objc.gen; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Metadata required to create links for indexing in <a href="http://kythe.io">Kythe.</a> | ||
* | ||
* <p>The JSON objects produced by this class conform to the format expected by Kythe's | ||
* postprocessing pipeline; this allows Kythe to create links from source .java files to generated | ||
* .h files. | ||
*/ | ||
public class KytheIndexingMetadata { | ||
|
||
/** | ||
* A Vname is a unique identifier for a node in a Kythe semantic graph. | ||
* | ||
* <p>For more information, see <a href="https://kythe.io/docs/kythe-uri-spec.html">the Kythe | ||
* documentation.</a> | ||
*/ | ||
static class VName { | ||
private final String corpus; | ||
private final String path; | ||
|
||
private VName(String corpus, String path) { | ||
this.corpus = corpus; | ||
this.path = path; | ||
} | ||
|
||
private String toJson() { | ||
return String.format("{\"corpus\":\"%s\",\"path\":\"%s\"}", corpus, path); | ||
} | ||
} | ||
|
||
static class AnchorAnchorMetadata { | ||
private final String type = "anchor_anchor"; | ||
private final int sourceBegin; | ||
private final int sourceEnd; | ||
private final int targetBegin; | ||
private final int targetEnd; | ||
private final String edge = "/kythe/edge/imputes"; | ||
private final VName sourceVName; | ||
|
||
AnchorAnchorMetadata( | ||
int sourceBegin, | ||
int sourceEnd, | ||
int targetBegin, | ||
int targetEnd, | ||
String corpus, | ||
String path) { | ||
this.sourceBegin = sourceBegin; | ||
this.sourceEnd = sourceEnd; | ||
this.targetBegin = targetBegin; | ||
this.targetEnd = targetEnd; | ||
this.sourceVName = new VName(corpus, path); | ||
} | ||
|
||
private String toJson() { | ||
return String.format( | ||
"{\"type\":\"%s\",\"source_begin\":%d,\"source_end\":%d,\"target_begin\":%d," | ||
+ "\"target_end\":%d,\"edge\":\"%s\",\"source_vname\":%s}", | ||
type, sourceBegin, sourceEnd, targetBegin, targetEnd, edge, sourceVName.toJson()); | ||
} | ||
} | ||
|
||
private final String type = "kythe0"; | ||
private final List<AnchorAnchorMetadata> meta = new ArrayList<>(); | ||
|
||
public void addAnchorAnchor( | ||
int sourceBegin, | ||
int sourceEnd, | ||
int targetBegin, | ||
int targetEnd, | ||
String sourceCorpus, | ||
String sourcePath) { | ||
meta.add( | ||
new AnchorAnchorMetadata( | ||
sourceBegin, sourceEnd, targetBegin, targetEnd, sourceCorpus, sourcePath)); | ||
} | ||
|
||
public boolean isEmpty() { | ||
return meta.isEmpty(); | ||
} | ||
|
||
public String toJson() { | ||
StringBuilder stringBuilder = new StringBuilder(); | ||
stringBuilder.append(String.format("{\"type\":\"%s\",\"meta\":[", type)); | ||
stringBuilder.append( | ||
meta.stream().map(AnchorAnchorMetadata::toJson).collect(Collectors.joining(","))); | ||
stringBuilder.append("]}"); | ||
return stringBuilder.toString(); | ||
} | ||
} |
Oops, something went wrong.