Skip to content

Commit

Permalink
packaging source added, useless encoding param del, un-thread-safe me…
Browse files Browse the repository at this point in the history
…ssage format usage del
  • Loading branch information
pfmiles committed Aug 15, 2013
1 parent 5bcc7d3 commit f95ae64
Show file tree
Hide file tree
Showing 25 changed files with 114 additions and 114 deletions.
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
15 changes: 1 addition & 14 deletions src/main/java/com/github/pfmiles/dropincc/Lang.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@ public class Lang implements Serializable {
// warn messages generated while analyzing
private String warnings;

// compilation encoding
private String encoding = "UTF-8";

/**
* Create language object with a name
*
Expand Down Expand Up @@ -112,7 +109,7 @@ public ConstructingGrule defineGrule(Object... eles) {
*/
public Exe compile() {
checkIfAnyEmptyGrule(this.grules);
AnalyzedLang cl = new AnalyzedLang(this.name, this.tokens, this.grules, this.whiteSpaceSensitive, this.encoding);
AnalyzedLang cl = new AnalyzedLang(this.name, this.tokens, this.grules, this.whiteSpaceSensitive);
cl.compile();
this.debugMsgs = cl.getDebugMsgs();
this.warnings = cl.getWarnings();
Expand Down Expand Up @@ -200,14 +197,4 @@ public String getWarnings() {
return warnings;
}

/**
* Set the encoding used during the compilation progress. Defaults to
* 'UTF-8' if not set.
*
* @param encoding
*/
public void setEncoding(String encoding) {
this.encoding = encoding;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,8 @@ public class AnalyzedLang {
private String debugMsgs;
private String warnings;

// compilation encoding
private String encoding;

public AnalyzedLang(String name, List<TokenDef> tokens, List<Grule> grules, boolean whitespaceSensitive, String encoding) {
public AnalyzedLang(String name, List<TokenDef> tokens, List<Grule> grules, boolean whitespaceSensitive) {
this.langName = name;
this.encoding = encoding;
// build token -> tokenType mapping
this.tokens = tokens;
// Gathering instant tokenDefs...
Expand Down Expand Up @@ -166,8 +162,7 @@ public void compile() {
this.parserCode = parserCodeGenResult.getCode();

// 7.compile and maintain the code in a separate classloader
CompilationResult result = HotCompileUtil.compile("com.github.pfmiles.dropincc.impl.runtime.gen." + this.langName, this.parserCode,
this.encoding);
CompilationResult result = HotCompileUtil.compile("com.github.pfmiles.dropincc.impl.runtime.gen." + this.langName, this.parserCode);
if (!result.isSucceed()) {
throw new DropinccException("Parser code compilation failed. Reason: " + result.getErrMsg());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ public class DotGenerator {

private Collection<GeneratingState> states = null;
// [name, length, width, finals, transitions]
private static MessageFormat graphTemp = new MessageFormat("digraph {0} '{'\n" + "rankdir=LR;\n" + "size=\"{1},{2}\"\n" + "node [shape = doublecircle]; {3};\n"
+ "node [shape = circle];\n" + "{4}\n" + "'}'\n");
private static String graphTemp = "digraph {0} '{'\n" + "rankdir=LR;\n" + "size=\"{1},{2}\"\n" + "node [shape = doublecircle]; {3};\n"
+ "node [shape = circle];\n" + "{4}\n" + "'}'\n";

// [start, end, edge]
private static MessageFormat transitionTemp = new MessageFormat("{0} -> {1} [ label = \"{2}\" ];");
private static String transitionTemp = "{0} -> {1} [ label = \"{2}\" ];";

/**
* Construct a dot generator, passed in all states contained in the graph.
Expand Down Expand Up @@ -61,14 +61,14 @@ public String toDotString(String name, int length, int width) {
}
String finalsStr = Util.join(" ", finals);
String transitionsStr = Util.join("\n", renderTransitionStrs(transitions));
return graphTemp.format(new String[] { name, String.valueOf(length), String.valueOf(width), finalsStr, transitionsStr });
return MessageFormat.format(graphTemp, name, String.valueOf(length), String.valueOf(width), finalsStr, transitionsStr);
}

private List<String> renderTransitionStrs(List<String[]> transitions) {
List<String> ret = new ArrayList<String>();
for (String[] trans : transitions) {
trans[2] = trans[2].replaceAll("\\\"", "\\");
ret.add(transitionTemp.format(trans));
ret.add(MessageFormat.format(transitionTemp, (Object[]) trans));
}
return ret;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
package com.github.pfmiles.dropincc.impl.hotcompile;

import java.io.StringWriter;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -44,7 +43,7 @@ public class HotCompileUtil {
* @return The resulting java class object and its corresponding class
* loader.
*/
public static CompilationResult compile(String qualifiedName, String sourceCode, String encoding) {
public static CompilationResult compile(String qualifiedName, String sourceCode) {
JavaStringSource source = new JavaStringSource(qualifiedName, sourceCode);
List<JavaStringSource> ss = Arrays.asList(source);
List<String> options = Arrays.asList("-classpath", HotCompileConstants.CLASSPATH);
Expand All @@ -53,7 +52,7 @@ public static CompilationResult compile(String qualifiedName, String sourceCode,
JavaFileManager fileManager = null;
Map<String, JavaMemCls> clses = new HashMap<String, JavaMemCls>();
try {
fileManager = new MemClsFileManager(compiler.getStandardFileManager(null, null, Charset.forName(encoding)), clses);
fileManager = new MemClsFileManager(compiler.getStandardFileManager(null, null, null), clses);
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
StringWriter out = new StringWriter();
CompilationTask task = compiler.getTask(out, fileManager, diagnostics, options, null, ss);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ public StatelessParserPrototype(Class<? extends Parser> cls, ParserCodeGenResult
allFields.addAll(this.fieldTokenTypeMapping.keySet());
for (String fname : allFields) {
try {
this.parserFieldsCache.put(fname, cls.getField(fname));
Field f = cls.getField(fname);
f.setAccessible(true);// this boosts reflection
this.parserFieldsCache.put(fname, f);
} catch (Exception e) {
throw new DropinccException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
public class AltsActionsGen extends CodeGen {

// [actionCls, ruleName, altIndex]
private static final MessageFormat fmt = new MessageFormat("public {0} {1}Action{2};// {1} rule alt {2} action");
private static final String fmt = "public {0} {1}Action{2};// {1} rule alt {2} action";

// list([grule, altIndex, actionObj])
private List<Object[]> actionInfos;
Expand Down Expand Up @@ -53,7 +53,7 @@ public String render(CodeGenContext context) {
String fname = ruleName + "Action" + altIndex;
context.fieldAltsActionMapping.put(fname, action);
context.actionFieldMapping.put(action, fname);
sb.append(fmt.format(new String[] { actionCls, ruleName, altIndex })).append("\n");
sb.append(MessageFormat.format(fmt, actionCls, ruleName, altIndex)).append("\n");
}
return sb.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

import java.io.IOException;
import java.io.InputStream;
import java.text.MessageFormat;

import com.github.pfmiles.dropincc.DropinccException;
import com.github.pfmiles.dropincc.impl.util.ByteAppender;
Expand All @@ -26,8 +25,8 @@
public abstract class CodeGen {

// use java built-in messageFormats as templates
protected static MessageFormat getTemplate(String name, Class<? extends CodeGen> cls) {
return new MessageFormat(readStrStream(cls.getResourceAsStream(name)));
protected static String getTemplate(String name, Class<? extends CodeGen> cls) {
return readStrStream(cls.getResourceAsStream(name));
}

private static String readStrStream(InputStream stm) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
public class KleeneDfasGen extends CodeGen {

// kleene name {0}
private static final MessageFormat fmt = new MessageFormat("public RunningDfaState {0}DfaStart;");
private static final String fmt = "public RunningDfaState {0}DfaStart;";

private List<PredictingKleene> pks;

Expand All @@ -44,7 +44,7 @@ public String render(CodeGenContext context) {
// backtrack kleene have no DFA
if (pk.isBacktrack())
continue;
sb.append(fmt.format(new String[] { pk.getKleeneType().toCodeGenStr() })).append('\n');
sb.append(MessageFormat.format(fmt, pk.getKleeneType().toCodeGenStr())).append('\n');
String fieldName = pk.getKleeneType().toCodeGenStr() + "DfaStart";
context.fieldKleeneDfaMapping.put(fieldName, toPredictingDfa(pk.getDfa()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*/
public class ParserClsGen extends CodeGen {

private static final MessageFormat fmt = getTemplate("parserCls.dt", ParserClsGen.class);
private static final String fmt = getTemplate("parserCls.dt", ParserClsGen.class);

private String parserClsName;// {0}
private TokenTypesGen tokenTypes; // {1}
Expand Down Expand Up @@ -60,7 +60,8 @@ public ParserClsGen(String parserClsName, TokenTypesGen tokenTypes, AltsActionsG

@SuppressWarnings("unchecked")
public String render(CodeGenContext context) {
return fmt.format(new String[] { this.parserClsName, this.tokenTypes.render(context), this.actions.render(context), this.preds.render(context),
this.ruleAltsPredictingDfa.render(context), this.kleenePreds.render(context), this.startRule.toCodeGenStr(), this.ruleMethods.render(context) });
return MessageFormat.format(fmt, this.parserClsName, this.tokenTypes.render(context), this.actions.render(context),
this.preds.render(context), this.ruleAltsPredictingDfa.render(context), this.kleenePreds.render(context),
this.startRule.toCodeGenStr(), this.ruleMethods.render(context));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
public class PredsGen extends CodeGen {

// [ruleName, altIndex]
private static final MessageFormat fmt = new MessageFormat("public Predicate<?> {0}Pred{1};// {0} rule pred {1}");
private static final String fmt = "public Predicate<?> {0}Pred{1};// {0} rule pred {1}";

// list([grule, altInfex, predObj])
private List<Object[]> predInfos;
Expand All @@ -39,7 +39,7 @@ public String render(CodeGenContext context) {
String ruleName = ((GruleType) predInfo[0]).toCodeGenStr();
String altIndex = String.valueOf(predInfo[1]);
context.fieldPredsMapping.put(ruleName + "Pred" + altIndex, (Predicate<?>) predInfo[2]);
sb.append(fmt.format(new String[] { ruleName, altIndex })).append("\n");
sb.append(MessageFormat.format(fmt, ruleName, altIndex)).append("\n");
}
return sb.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@
public class RuleDfasGen extends CodeGen {

// ruleName {0}
private static final MessageFormat fmt = new MessageFormat(
"public RunningDfaState {0}DfaStart;// {0} rule look ahead dfa start state");
private static final String fmt = "public RunningDfaState {0}DfaStart;// {0} rule look ahead dfa start state";

private List<PredictingGrule> pgs;

Expand All @@ -46,7 +45,7 @@ public String render(CodeGenContext context) {
if (p.getDfa() == null)
continue;
String ruleName = p.getGruleType().toCodeGenStr();
sb.append(fmt.format(new String[] { ruleName })).append('\n');
sb.append(MessageFormat.format(fmt, ruleName)).append('\n');
String dfaName = ruleName + "DfaStart";
RunningDfaState start = toPredictingDfa(p.getDfa());
context.fieldRuleDfaMapping.put(dfaName, start);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class RuleMethodsGen extends CodeGen {
// method skeleton -> only string code
// 0: gruleName
// 1: methodContent
private static final MessageFormat ruleMethodSkeleton = getTemplate("ruleMethodSkeleton.dt", RuleMethodsGen.class);
private static final String ruleMethodSkeleton = getTemplate("ruleMethodSkeleton.dt", RuleMethodsGen.class);

private List<PredictingGrule> pgs;

Expand All @@ -43,7 +43,7 @@ public String render(CodeGenContext context) {
context.varSeq = new SeqGen();
context.curGrule = p.getGruleType();
String ruleName = p.getGruleType().toCodeGenStr();
sb.append(ruleMethodSkeleton.format(new String[] { ruleName, new MethodContent(p).render(context) })).append('\n');
sb.append(MessageFormat.format(ruleMethodSkeleton, ruleName, new MethodContent(p).render(context))).append('\n');
}
return sb.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/
public class TokenTypesGen extends CodeGen {
// [typeName, comment]
private static final MessageFormat fmt = new MessageFormat("public TokenType {0};// {1}");
private static final String fmt = "public TokenType {0};// {1}";
private Collection<TokenType> types;

public TokenTypesGen(Collection<TokenType> types) {
Expand All @@ -38,7 +38,7 @@ public String render(CodeGenContext context) {
for (TokenType t : types) {
String name = t.toCodeGenStr();
context.fieldTokenTypeMapping.put(name, t);
sb.append(fmt.format(new String[] { t.toCodeGenStr(), t.toString() })).append("\n");
sb.append(MessageFormat.format(fmt, t.toCodeGenStr(), t.toString())).append("\n");
}
return sb.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class MethodContent extends CodeGen {
// 0: ruleNum
// 1: matchCode
// 2: matchCode is backtracking
private static final MessageFormat onBacktrackPathFmt = getTemplate("methodContentOnBacktrackPath.dt", MethodContent.class);
private static final String onBacktrackPathFmt = getTemplate("methodContentOnBacktrackPath.dt", MethodContent.class);

private PredictingGrule pg;

Expand Down Expand Up @@ -65,7 +65,7 @@ public String render(CodeGenContext context) {
matchCodeOnPath = new MultiAltMatchCodeGen(this.pg, true).render(context);
}
}
return onBacktrackPathFmt.format(new String[] { String.valueOf(pg.getGruleType().getDefIndex()), matchCode, matchCodeOnPath });
return MessageFormat.format(onBacktrackPathFmt, String.valueOf(pg.getGruleType().getDefIndex()), matchCode, matchCodeOnPath);
} else {
return matchCode;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ public class MultiAltMatchBacktrackGen extends CodeGen {
// 0: ruleName
// 1: backtracksCode
// 2: cleanCacheCode
private static final MessageFormat fmt = getTemplate("multiAltBacktrackMatchCode.dt", MultiAltMatchBacktrackGen.class);
private static final String fmt = getTemplate("multiAltBacktrackMatchCode.dt", MultiAltMatchBacktrackGen.class);
// altBacktrack -> only string code
// 0: ruleNum
// 1: elements code
// 2: elements var
// 3: actionName
private static final MessageFormat altBacktrackFmt = getTemplate("altBacktrack.dt", MultiAltMatchBacktrackGen.class);
private static final MessageFormat altBacktrackOnPathFmt = getTemplate("altBacktrackOnPath.dt", MultiAltMatchBacktrackGen.class);
private static final String altBacktrackFmt = getTemplate("altBacktrack.dt", MultiAltMatchBacktrackGen.class);
private static final String altBacktrackOnPathFmt = getTemplate("altBacktrackOnPath.dt", MultiAltMatchBacktrackGen.class);

private PredictingGrule pg;
private boolean generatingBacktrackCode;
Expand All @@ -60,15 +60,17 @@ public String render(CodeGenContext context) {
actionName = context.actionFieldMapping.get(alt.getAction());
}
if (this.generatingBacktrackCode) {
backtrackCode.append(altBacktrackOnPathFmt.format(new String[] { ruleNum, varAndCode.getRight(), varAndCode.getLeft(), actionName })).append('\n');
backtrackCode.append(MessageFormat.format(altBacktrackOnPathFmt, ruleNum, varAndCode.getRight(), varAndCode.getLeft(), actionName))
.append('\n');
} else {
backtrackCode.append(altBacktrackFmt.format(new String[] { ruleNum, varAndCode.getRight(), varAndCode.getLeft(), actionName })).append('\n');
backtrackCode.append(MessageFormat.format(altBacktrackFmt, ruleNum, varAndCode.getRight(), varAndCode.getLeft(), actionName)).append(
'\n');
}
}
String cleanCacheCode = "";
if (!this.generatingBacktrackCode)
cleanCacheCode = "cleanCache();";
return fmt.format(new String[] { ruleName, backtrackCode.toString(), cleanCacheCode });
return MessageFormat.format(fmt, ruleName, backtrackCode.toString(), cleanCacheCode);
}

}
Loading

0 comments on commit f95ae64

Please sign in to comment.