本文整理汇总了Java中org.antlr.v4.runtime.misc.Utils类的典型用法代码示例。如果您正苦于以下问题:Java Utils类的具体用法?Java Utils怎么用?Java Utils使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Utils类属于org.antlr.v4.runtime.misc包,在下文中一共展示了Utils类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import org.antlr.v4.runtime.misc.Utils; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
LangDescriptor[] languages = new LangDescriptor[] {
JAVA_DESCR,
JAVA8_DESCR,
JAVA_GUAVA_DESCR,
JAVA8_GUAVA_DESCR,
ANTLR4_DESCR,
SQLITE_CLEAN_DESCR,
TSQL_CLEAN_DESCR,
SQLITE_NOISY_DESCR,
TSQL_NOISY_DESCR,
// QUORUM_DESCR,
};
List<String> corpusDirs = map(languages, l -> l.corpusDir);
String[] dirs = corpusDirs.toArray(new String[languages.length]);
String python = testAllLanguages(languages, dirs, "leave_one_out.pdf");
String fileName = "python/src/leave_one_out.py";
Utils.writeFile(fileName, python);
System.out.println("wrote python code to "+fileName);
}
示例2: format
import org.antlr.v4.runtime.misc.Utils; //导入依赖的package包/类
public static void format(LangDescriptor language,
String testFileName,
String outputFileName)
throws Exception
{
// load all files up front
List<String> allFiles = getFilenames(new File(language.corpusDir), language.fileRegex);
List<InputDocument> documents = load(allFiles, language);
// if in corpus, don't include in corpus
final String path = new File(testFileName).getAbsolutePath();
List<InputDocument> others = filter(documents, d -> !d.fileName.equals(path));
InputDocument testDoc = parse(testFileName, language);
Corpus corpus = new Corpus(others, language);
corpus.train();
Formatter formatter = new Formatter(corpus, language.indentSize, Formatter.DEFAULT_K,
FEATURES_INJECT_WS, FEATURES_HPOS);
String output = formatter.format(testDoc, false);
if ( outputFileName!=null ) {
Utils.writeFile(outputFileName, output);
}
else {
System.out.print(output);
}
}
示例3: toStringTree
import org.antlr.v4.runtime.misc.Utils; //导入依赖的package包/类
/** Print out a whole tree in LISP form. Arg nodeTextProvider is used on the
* node payloads to get the text for the nodes.
*
* @since 4.5.1
*/
public static String toStringTree(Tree t, TreeTextProvider nodeTextProvider) {
if ( t==null ) return "null";
String s = Utils.escapeWhitespace(nodeTextProvider.getText(t), false);
if ( t.getChildCount()==0 ) return s;
StringBuilder buf = new StringBuilder();
buf.append("(");
s = Utils.escapeWhitespace(nodeTextProvider.getText(t), false);
buf.append(s);
buf.append(' ');
for (int i = 0; i<t.getChildCount(); i++) {
if ( i>0 ) buf.append(' ');
buf.append(toStringTree(t.getChild(i), nodeTextProvider));
}
buf.append(")");
return buf.toString();
}
示例4: toStringTree
import org.antlr.v4.runtime.misc.Utils; //导入依赖的package包/类
private String toStringTree(Tree tree, List<String> ruleNames) {
String s = Utils.escapeWhitespace(getNodeText(tree, ruleNames), false);
if(tree.getChildCount() == 0) return s;
StringBuilder buf = new StringBuilder();
buf.append("(");
s = Utils.escapeWhitespace(getNodeText(tree, ruleNames), false);
buf.append(s);
buf.append(' ');
for(int i = 0; i < tree.getChildCount(); i++)
{
if(i > 0)
buf.append(' ');
buf.append(toStringTree(tree.getChild(i), ruleNames));
}
buf.append(")");
return buf.toString();
}
示例5: toStringTree
import org.antlr.v4.runtime.misc.Utils; //导入依赖的package包/类
private String toStringTree(@NotNull final Tree t, @Nullable final List<String> ruleNames) {
if (t.getChildCount() == 0) {
return Utils.escapeWhitespace(getNodeText(t, ruleNames), true);
}
StringBuilder buf = new StringBuilder();
buf.append(" ( ");
String s = Utils.escapeWhitespace(getNodeText(t, ruleNames), true);
buf.append(s);
buf.append(' ');
for (int i = 0; i < t.getChildCount(); i++) {
if (i > 0) {
buf.append(' ');
}
buf.append(toStringTree(t.getChild(i), ruleNames));
}
buf.append(" ) ");
return buf.toString();
}
示例6: toStringTree
import org.antlr.v4.runtime.misc.Utils; //导入依赖的package包/类
/** Print out a whole tree in LISP form. {@link #getNodeText} is used on the
* node payloads to get the text for the nodes. Detect
* parse trees and extract data appropriately.
*/
public static String toStringTree(@NotNull Tree t, @Nullable List<String> ruleNames) {
String s = Utils.escapeWhitespace(getNodeText(t, ruleNames), false);
if ( t.getChildCount()==0 ) return s;
StringBuilder buf = new StringBuilder();
buf.append("(");
s = Utils.escapeWhitespace(getNodeText(t, ruleNames), false);
buf.append(s);
buf.append(' ');
for (int i = 0; i<t.getChildCount(); i++) {
if ( i>0 ) buf.append(' ');
buf.append(toStringTree(t.getChild(i), ruleNames));
}
buf.append(")");
return buf.toString();
}
示例7: getTokenTypeMap
import org.antlr.v4.runtime.misc.Utils; //导入依赖的package包/类
/**
* Get a map from token names to token types.
*
* <p>Used for XPath and tree pattern compilation.</p>
*/
@NotNull
public Map<String, Integer> getTokenTypeMap() {
String[] tokenNames = getTokenNames();
if (tokenNames == null) {
throw new UnsupportedOperationException("The current recognizer does not provide a list of token names.");
}
synchronized (tokenTypeMapCache) {
Map<String, Integer> result = tokenTypeMapCache.get(tokenNames);
if (result == null) {
result = Utils.toMap(tokenNames);
result.put("EOF", Token.EOF);
result = Collections.unmodifiableMap(result);
tokenTypeMapCache.put(tokenNames, result);
}
return result;
}
}
示例8: getRuleIndexMap
import org.antlr.v4.runtime.misc.Utils; //导入依赖的package包/类
/**
* Get a map from rule names to rule indexes.
*
* <p>Used for XPath and tree pattern compilation.</p>
*/
@NotNull
public Map<String, Integer> getRuleIndexMap() {
String[] ruleNames = getRuleNames();
if (ruleNames == null) {
throw new UnsupportedOperationException("The current recognizer does not provide a list of rule names.");
}
synchronized (ruleIndexMapCache) {
Map<String, Integer> result = ruleIndexMapCache.get(ruleNames);
if (result == null) {
result = Collections.unmodifiableMap(Utils.toMap(ruleNames));
ruleIndexMapCache.put(ruleNames, result);
}
return result;
}
}
示例9: checkCExec
import org.antlr.v4.runtime.misc.Utils; //导入依赖的package包/类
public void checkCExec(String filename) throws Exception {
URL testFolderURL = TestCGen.class.getClassLoader().getResource(SAMPLES_DIR);
String testFolder = testFolderURL.getPath();
String workingDir = getWorkingDir();
String J_pathToFile = testFolder+"/"+filename;
String C_filename = basename(filename)+".c";
JTran jTran = new JTran();
String C_code = jTran.translate(J_pathToFile, C_filename, false, false);
Utils.writeFile(workingDir+"/"+C_filename, C_code);
// compile
String[] cc = {"cc", "-o", basename(filename), C_filename};
Triple<Integer, String, String> cc_result = exec(cc, getWorkingDir());
int execCode = cc_result.a;
String stdout = cc_result.b;
String stderr = cc_result.c;
assertEquals("", stdout);
assertEquals("", stderr);
assertEquals(0, execCode);
// execute
String[] exec_cmd = {"./"+basename(filename)};
Triple<Integer, String, String> result = exec(exec_cmd, getWorkingDir());
execCode = result.a;
stdout = result.b;
stderr = result.c;
String expected_output_filename = basename(filename)+".txt";
String expected_output = readFile(testFolder+"/"+expected_output_filename);
assertEquals(expected_output, stdout);
assertEquals("", stderr);
assertEquals(0, execCode);
}
示例10: writePython
import org.antlr.v4.runtime.misc.Utils; //导入依赖的package包/类
public static void writePython(LangDescriptor[] languages, List<Integer> ks, Float[][] medians) throws IOException {
StringBuilder data = new StringBuilder();
StringBuilder plot = new StringBuilder();
for (int i = 0; i<languages.length; i++) {
LangDescriptor language = languages[i];
List<Float> filteredMedians = BuffUtils.filter(Arrays.asList(medians[i]), m -> m!=null);
data.append(language.name+'='+filteredMedians+'\n');
plot.append(String.format("ax.plot(ks, %s, label=\"%s\", marker='%s', color='%s')\n",
language.name, language.name,
nameToGraphMarker.get(language.name),
nameToGraphColor.get(language.name)));
}
String python =
"#\n"+
"# AUTO-GENERATED FILE. DO NOT EDIT\n" +
"# CodeBuff %s '%s'\n" +
"#\n"+
"import numpy as np\n"+
"import matplotlib.pyplot as plt\n\n" +
"%s\n" +
"ks = %s\n"+
"fig = plt.figure()\n"+
"ax = plt.subplot(111)\n"+
"%s"+
"ax.tick_params(axis='both', which='major', labelsize=18)\n" +
"ax.set_xlabel(\"$k$ nearest neighbors\", fontsize=20)\n"+
"ax.set_ylabel(\"Median error rate\", fontsize=20)\n" +
"#ax.set_title(\"k Nearest Neighbors vs\\nLeave-one-out Validation Error Rate\")\n"+
"plt.legend(fontsize=18)\n\n" +
"fig.savefig('images/vary_k.pdf', format='pdf')\n"+
"plt.show()\n";
String code = String.format(python, Tool.version, new Date(), data, ks, plot);
String fileName = "python/src/vary_k.py";
Utils.writeFile(fileName, code);
System.out.println("wrote python code to "+fileName);
}
示例11: main
import org.antlr.v4.runtime.misc.Utils; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
LangDescriptor[] languages = new LangDescriptor[] {
JAVA_DESCR,
JAVA8_DESCR,
JAVA_GUAVA_DESCR,
};
List<String> corpusDirs = map(languages, l -> l.corpusDir);
String[] dirs = corpusDirs.toArray(new String[languages.length]);
String python = testAllLanguages(languages, dirs, "all_java_leave_one_out.pdf");
String fileName = "python/src/all_java_leave_one_out.py";
Utils.writeFile(fileName, python);
System.out.println("wrote python code to "+fileName);
}
示例12: main
import org.antlr.v4.runtime.misc.Utils; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
LangDescriptor[] languages = new LangDescriptor[] {
SQLITE_NOISY_DESCR,
SQLITE_CLEAN_DESCR,
TSQL_NOISY_DESCR,
TSQL_CLEAN_DESCR,
};
List<String> corpusDirs = map(languages, l -> l.corpusDir);
String[] dirs = corpusDirs.toArray(new String[languages.length]);
String python = testAllLanguages(languages, dirs, "all_sql_leave_one_out.pdf");
String fileName = "python/src/all_sql_leave_one_out.py";
Utils.writeFile(fileName, python);
System.out.println("wrote python code to "+fileName);
}
示例13: validate
import org.antlr.v4.runtime.misc.Utils; //导入依赖的package包/类
public static Triple<Formatter,Float,Float> validate(LangDescriptor language,
List<InputDocument> documents,
InputDocument testDoc,
boolean saveOutput,
boolean computeEditDistance)
throws Exception
{
// kNNClassifier.resetCache();
Corpus corpus = new Corpus(documents, language);
corpus.train();
// System.out.printf("%d feature vectors\n", corpus.featureVectors.size());
Formatter formatter = new Formatter(corpus, language.indentSize);
String output = formatter.format(testDoc, false);
float editDistance = 0;
if ( computeEditDistance ) {
editDistance = normalizedLevenshteinDistance(testDoc.content, output);
}
ClassificationAnalysis analysis = new ClassificationAnalysis(testDoc, formatter.getAnalysisPerToken());
// System.out.println(testDoc.fileName+": edit distance = "+editDistance+", error rate = "+analysis.getErrorRate());
if ( saveOutput ) {
File dir = new File(outputDir+"/"+language.name);
if ( saveOutput ) {
dir = new File(outputDir+"/"+language.name);
dir.mkdir();
}
Utils.writeFile(dir.getPath()+"/"+new File(testDoc.fileName).getName(), output);
}
return new Triple<>(formatter, editDistance, analysis.getErrorRate());
}
示例14: visitTerminal
import org.antlr.v4.runtime.misc.Utils; //导入依赖的package包/类
@Override
public void visitTerminal(TerminalNode node) {
if (builder.length() > 0) {
builder.append(' ');
}
builder.append(Utils.escapeWhitespace(Trees.getNodeText(node, ruleNames), false));
}
示例15: visitErrorNode
import org.antlr.v4.runtime.misc.Utils; //导入依赖的package包/类
@Override
public void visitErrorNode(ErrorNode node) {
if (builder.length() > 0) {
builder.append(' ');
}
builder.append(Utils.escapeWhitespace(Trees.getNodeText(node, ruleNames), false));
}