To get static checking I converted the version template to a method and also moved the compatibility check into the target so that in the worst-case target developers can override the compatibility check per their needs. I have also decided to throw an exception for the Java target when the code generation and runtime library versions are incompatible. Change the tool message per Sam's request.
This commit is contained in:
parent
a3d4ba53a3
commit
db0830e00d
|
@ -28,14 +28,6 @@
|
|||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/** ANTLR tool checks output templates are compatible with tool code generation.
|
||||
* For now, a simple string match used on x.y of x.y.z scheme.
|
||||
* Must match Tool.VERSION during load to templates.
|
||||
*
|
||||
* REQUIRED.
|
||||
*/
|
||||
VERSION() ::= "4.2" // use just "x.y" and don't include bug fix release number
|
||||
|
||||
javaTypeInitMap ::= [
|
||||
"int":"0",
|
||||
"long":"0",
|
||||
|
|
|
@ -30,24 +30,60 @@
|
|||
|
||||
package org.antlr.v4;
|
||||
|
||||
import org.antlr.runtime.*;
|
||||
import org.antlr.runtime.ANTLRFileStream;
|
||||
import org.antlr.runtime.ANTLRStringStream;
|
||||
import org.antlr.runtime.CharStream;
|
||||
import org.antlr.runtime.CommonTokenStream;
|
||||
import org.antlr.runtime.ParserRuleReturnScope;
|
||||
import org.antlr.runtime.RecognitionException;
|
||||
import org.antlr.v4.analysis.AnalysisPipeline;
|
||||
import org.antlr.v4.automata.ATNFactory;
|
||||
import org.antlr.v4.automata.LexerATNFactory;
|
||||
import org.antlr.v4.automata.ParserATNFactory;
|
||||
import org.antlr.v4.codegen.CodeGenPipeline;
|
||||
import org.antlr.v4.misc.Graph;
|
||||
import org.antlr.v4.parse.*;
|
||||
import org.antlr.v4.parse.ANTLRParser;
|
||||
import org.antlr.v4.parse.GrammarASTAdaptor;
|
||||
import org.antlr.v4.parse.GrammarTreeVisitor;
|
||||
import org.antlr.v4.parse.ToolANTLRLexer;
|
||||
import org.antlr.v4.parse.ToolANTLRParser;
|
||||
import org.antlr.v4.parse.v3TreeGrammarException;
|
||||
import org.antlr.v4.runtime.misc.LogManager;
|
||||
import org.antlr.v4.runtime.misc.Nullable;
|
||||
import org.antlr.v4.semantics.SemanticPipeline;
|
||||
import org.antlr.v4.tool.*;
|
||||
import org.antlr.v4.tool.ast.*;
|
||||
import org.antlr.v4.tool.ANTLRMessage;
|
||||
import org.antlr.v4.tool.ANTLRToolListener;
|
||||
import org.antlr.v4.tool.BuildDependencyGenerator;
|
||||
import org.antlr.v4.tool.DOTGenerator;
|
||||
import org.antlr.v4.tool.DefaultToolListener;
|
||||
import org.antlr.v4.tool.ErrorManager;
|
||||
import org.antlr.v4.tool.ErrorType;
|
||||
import org.antlr.v4.tool.Grammar;
|
||||
import org.antlr.v4.tool.GrammarTransformPipeline;
|
||||
import org.antlr.v4.tool.LexerGrammar;
|
||||
import org.antlr.v4.tool.Rule;
|
||||
import org.antlr.v4.tool.ast.ActionAST;
|
||||
import org.antlr.v4.tool.ast.GrammarAST;
|
||||
import org.antlr.v4.tool.ast.GrammarASTErrorNode;
|
||||
import org.antlr.v4.tool.ast.GrammarRootAST;
|
||||
import org.antlr.v4.tool.ast.RuleAST;
|
||||
import org.antlr.v4.tool.ast.TerminalAST;
|
||||
import org.stringtemplate.v4.STGroup;
|
||||
|
||||
import java.io.*;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
public class Tool {
|
||||
|
|
|
@ -88,12 +88,7 @@ public class CodeGenerator {
|
|||
}
|
||||
|
||||
public STGroup getTemplates() {
|
||||
STGroup templates = getTarget().getTemplates();
|
||||
ST version = templates.getInstanceOf("VERSION");
|
||||
if ( version==null || !version.render().equals(Tool.VERSION) ) {
|
||||
tool.errMgr.toolError(ErrorType.INCOMPATIBLE_TOOL_AND_TEMPLATES, version, Tool.VERSION, language);
|
||||
}
|
||||
return templates;
|
||||
return getTarget().getTemplates();
|
||||
}
|
||||
|
||||
protected void loadLanguageTarget(String language) {
|
||||
|
|
|
@ -30,6 +30,7 @@
|
|||
|
||||
package org.antlr.v4.codegen;
|
||||
|
||||
import org.antlr.v4.Tool;
|
||||
import org.antlr.v4.codegen.model.RuleFunction;
|
||||
import org.antlr.v4.codegen.model.SerializedATN;
|
||||
import org.antlr.v4.misc.Utils;
|
||||
|
@ -87,10 +88,20 @@ public abstract class Target {
|
|||
return language;
|
||||
}
|
||||
|
||||
public STGroup getTemplates() {
|
||||
if (templates == null) {
|
||||
templates = loadTemplates();
|
||||
}
|
||||
/** ANTLR tool should check output templates / target are compatible with tool code generation.
|
||||
* For now, a simple string match used on x.y of x.y.z scheme. We use a method to avoid mismatches
|
||||
* between a template called VERSION. This value is checked against Tool.VERSION during load of templates.
|
||||
*/
|
||||
public abstract String getVersion();
|
||||
|
||||
public STGroup getTemplates() {
|
||||
if (templates == null) {
|
||||
templates = loadTemplates();
|
||||
String version = getVersion();
|
||||
if (version == null || !version.equals(Tool.VERSION)) {
|
||||
gen.tool.errMgr.toolError(ErrorType.INCOMPATIBLE_TOOL_AND_TEMPLATES, version, Tool.VERSION, language);
|
||||
}
|
||||
}
|
||||
|
||||
return templates;
|
||||
}
|
||||
|
|
|
@ -172,9 +172,9 @@ public enum ErrorType {
|
|||
/**
|
||||
* Compiler Error 35.
|
||||
*
|
||||
* <p>templates and tool aren't compatible</p>
|
||||
* <p>templates/target and tool aren't compatible</p>
|
||||
*/
|
||||
INCOMPATIBLE_TOOL_AND_TEMPLATES(35, "<arg3> code generation templates and tool versions disagree: <arg> != <arg2>", ErrorSeverity.ERROR),
|
||||
INCOMPATIBLE_TOOL_AND_TEMPLATES(35, "<arg3> code generation target requires ANTLR <arg2>; it can't be loaded by the current ANTLR <arg>", ErrorSeverity.ERROR),
|
||||
|
||||
/*
|
||||
* Grammar errors
|
||||
|
|
Loading…
Reference in New Issue