add new stuff

[git-p4: depot-paths = "//depot/code/antlr4/main/": change = 6624]
This commit is contained in:
parrt 2010-01-27 18:12:44 -08:00
parent f49b99ec3e
commit 493177c04e
11 changed files with 1018 additions and 1 deletions

View File

@ -58,11 +58,11 @@ public class Tool {
private static boolean exitNow = false;
// The internal options are for my use on the command line during dev
//
public static boolean internalOption_PrintGrammarTree = false;
public static boolean internalOption_PrintDFA = false;
public static boolean internalOption_ShowNFAConfigsInDFA = false;
public static boolean internalOption_watchNFAConversion = false;
public static boolean internalOption_saveTempLexer = false;
protected Map<String, Grammar> grammars = new HashMap<String, Grammar>();
@ -207,6 +207,9 @@ public class Tool {
else if (args[i].equals("-Xdfaverbose")) {
internalOption_ShowNFAConfigsInDFA = true;
}
else if (args[i].equals("-Xsavelexer")) {
internalOption_saveTempLexer = true;
}
else if (args[i].equals("-Xwatchconversion")) {
internalOption_watchNFAConversion = true;
}
@ -346,6 +349,7 @@ public class Tool {
System.err.println(" -Xwatchconversion print a message for each NFA before converting");
System.err.println(" -XdbgST put tags at start/stop of all templates in output");
System.err.println(" -Xnfastates for nondeterminisms, list NFA states for each path");
System.err.println(" -Xsavelexer save temp lexer file created for combined grammars");
/*
System.err.println(" -Xm m max number of rule invocations during conversion [" + NFAContext.MAX_SAME_RULE_INVOCATIONS_PER_NFA_CONFIG_STACK + "]");
System.err.println(" -Xmaxdfaedges m max \"comfortable\" number of edges for single DFA state [" + DFA.MAX_STATE_TRANSITIONS_FOR_TABLE + "]");

View File

@ -0,0 +1,415 @@
/*
[The "BSD license"]
Copyright (c) 2010 Terence Parr
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/** Check the basic semantics of the input. We check for:
FILE_AND_GRAMMAR_NAME_DIFFER
RULE_REDEFINITION(MessageSeverity.ERROR, true, true),
LEXER_RULES_NOT_ALLOWED(MessageSeverity.ERROR, true, true),
PARSER_RULES_NOT_ALLOWED(MessageSeverity.ERROR, true, true),
UNDEFINED_RULE_REF(MessageSeverity.ERROR, true, true),
CANNOT_ALIAS_TOKENS_IN_LEXER(MessageSeverity.ERROR, true, true),
INVALID_RULE_PARAMETER_REF(MessageSeverity.ERROR, true, true),
SYMBOL_CONFLICTS_WITH_GLOBAL_SCOPE(MessageSeverity.ERROR, true, true),
LABEL_CONFLICTS_WITH_RULE(MessageSeverity.ERROR, true, true),
LABEL_CONFLICTS_WITH_TOKEN(MessageSeverity.ERROR, true, true),
LABEL_TYPE_CONFLICT(MessageSeverity.ERROR, true, true),
MISSING_RULE_ARGS(MessageSeverity.ERROR, true, true),
RULE_HAS_NO_ARGS(MessageSeverity.ERROR, true, true),
ARGS_ON_TOKEN_REF(MessageSeverity.ERROR, true, true),
ILLEGAL_OPTION(MessageSeverity.ERROR, true, true),
UNDEFINED_TOKEN_REF_IN_REWRITE(MessageSeverity.ERROR, true, true),
REWRITE_ELEMENT_NOT_PRESENT_ON_LHS(MessageSeverity.ERROR, true, true),
UNDEFINED_LABEL_REF_IN_REWRITE(MessageSeverity.ERROR, true, true),
EMPTY_COMPLEMENT(MessageSeverity.ERROR, true, true),
ACTION_REDEFINITION(MessageSeverity.ERROR, true, true),
REWRITE_OR_OP_WITH_NO_OUTPUT_OPTION(MessageSeverity.ERROR, true, true),
NO_RULES(MessageSeverity.ERROR, true, true),
REWRITE_FOR_MULTI_ELEMENT_ALT(MessageSeverity.ERROR, true, true),
RULE_INVALID_SET(MessageSeverity.ERROR, true, true),
HETERO_ILLEGAL_IN_REWRITE_ALT(MessageSeverity.ERROR, true, true),
NO_SUCH_RULE_IN_SCOPE(MessageSeverity.ERROR, true, true),
TOKEN_ALIAS_CONFLICT(MessageSeverity.ERROR, true, true),
TOKEN_ALIAS_REASSIGNMENT(MessageSeverity.ERROR, true, true),
TOKEN_VOCAB_IN_DELEGATE(MessageSeverity.ERROR, true, true),
INVALID_IMPORT(MessageSeverity.ERROR, true, true),
IMPORTED_TOKENS_RULE_EMPTY(MessageSeverity.ERROR, true, true),
IMPORT_NAME_CLASH(MessageSeverity.ERROR, true, true),
AST_OP_WITH_NON_AST_OUTPUT_OPTION(MessageSeverity.ERROR, true, true),
AST_OP_IN_ALT_WITH_REWRITE(MessageSeverity.ERROR, true, true),
WILDCARD_AS_ROOT(MessageSeverity.ERROR, true, true),
CONFLICTING_OPTION_IN_TREE_FILTER(MessageSeverity.ERROR, true, true),
*
*/
tree grammar BasicSemanticsChecker;
options {
language = Java;
tokenVocab = ANTLRParser;
ASTLabelType = ASTGrammar;
filter = true;
}
// Include the copyright in this source and also the generated source
@header {
/*
[The "BSD license"]
Copyright (c) 2010 Terence Parr
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.antlr.v4.parse;
import org.antlr.v4.tool.*;
}
topdown
:
;
grammarSpec
: ^(grammarType ID DOC_COMMENT? prequelConstruct* rules)
;
grammarType
: LEXER_GRAMMAR | PARSER_GRAMMAR | TREE_GRAMMAR | COMBINED_GRAMMAR
;
prequelConstruct
: optionsSpec
| delegateGrammars
| tokensSpec
| attrScope
| action
;
optionsSpec
: ^(OPTIONS option+)
;
option
: ^(ASSIGN ID optionValue)
;
optionValue
: ID
| STRING_LITERAL
| CHAR_LITERAL
| INT
| STAR
;
delegateGrammars
: ^(IMPORT delegateGrammar+)
;
delegateGrammar
: ^(ASSIGN ID ID)
| ID
;
tokensSpec
: ^(TOKENS tokenSpec+)
;
tokenSpec
: ^(ASSIGN TOKEN_REF STRING_LITERAL)
| ^(ASSIGN TOKEN_REF CHAR_LITERAL)
| TOKEN_REF
| RULE_REF
;
attrScope
: ^(SCOPE ID ACTION)
;
action
: ^(AT ID? ID ACTION)
;
rules
: ^(RULES rule*)
;
rule: ^( RULE ID DOC_COMMENT? ruleModifiers? ARG_ACTION?
ruleReturns? rulePrequel* altListAsBlock exceptionGroup
)
;
exceptionGroup
: exceptionHandler* finallyClause?
;
exceptionHandler
: ^(CATCH ARG_ACTION ACTION)
;
finallyClause
: ^(FINALLY ACTION)
;
rulePrequel
: throwsSpec
| ruleScopeSpec
| optionsSpec
| ruleAction
;
ruleReturns
: ^(RETURNS ARG_ACTION)
;
throwsSpec
: ^(THROWS ID+)
;
ruleScopeSpec
: ^(SCOPE ACTION)
| ^(SCOPE ID+)
;
ruleAction
: ^(AT ID ACTION)
;
ruleModifiers
: ^(RULEMODIFIERS ruleModifier+)
;
ruleModifier
: PUBLIC
| PRIVATE
| PROTECTED
| FRAGMENT
;
altList
: alternative+
;
altListAsBlock
: ^(BLOCK altList)
;
alternative
: ^(ALT_REWRITE alternative rewrite)
| ^(ALT EPSILON)
| elements
;
elements
: ^(ALT element+)
;
element
: labeledElement
| atom
| ebnf
| ACTION
| SEMPRED
| GATED_SEMPRED
| treeSpec
;
labeledElement
: ^(ASSIGN ID (atom|block))
| ^(PLUS_ASSIGN ID (atom|block))
;
treeSpec
: ^(TREE_BEGIN element+)
;
ebnf: ^(blockSuffix block)
| block
;
blockSuffix
: ebnfSuffix
| ROOT
| IMPLIES
| BANG
;
ebnfSuffix
: OPTIONAL
| CLOSURE
| POSITIVE_CLOSURE
;
atom: ^(ROOT range)
| ^(BANG range)
| ^(ROOT notSet)
| ^(BANG notSet)
| range
| ^(DOT ID terminal)
| ^(DOT ID ruleref)
| terminal
| ruleref
;
notSet
: ^(NOT notTerminal)
| ^(NOT block)
;
notTerminal
: CHAR_LITERAL
| TOKEN_REF
| STRING_LITERAL
;
block
: ^(BLOCK optionsSpec? ruleAction* ACTION? altList)
;
ruleref
: ^(ROOT RULE_REF ARG_ACTION?)
| ^(BANG RULE_REF ARG_ACTION?)
| ^(RULE_REF ARG_ACTION?)
;
range
: ^(RANGE rangeElement rangeElement)
;
rangeElement
: CHAR_LITERAL
| STRING_LITERAL
| RULE_REF
| TOKEN_REF
;
terminal
: ^(CHAR_LITERAL elementOptions)
| CHAR_LITERAL
| ^(STRING_LITERAL elementOptions)
| STRING_LITERAL
| ^(TOKEN_REF elementOptions)
| TOKEN_REF
| ^(WILDCARD elementOptions)
| WILDCARD
| ^(ROOT terminal)
| ^(BANG terminal)
;
elementOptions
: ^(ELEMENT_OPTIONS elementOption+)
;
elementOption
: ID
| ^(ASSIGN ID ID)
| ^(ASSIGN ID STRING_LITERAL)
;
rewrite
: predicatedRewrite* nakedRewrite
;
predicatedRewrite
: ^(ST_RESULT SEMPRED rewriteAlt)
| ^(RESULT SEMPRED rewriteAlt)
;
nakedRewrite
: ^(ST_RESULT rewriteAlt)
| ^(RESULT rewriteAlt)
;
rewriteAlt
: rewriteTemplate
| rewriteTreeAlt
| ETC
| EPSILON
;
rewriteTreeAlt
: ^(ALT rewriteTreeElement+)
;
rewriteTreeElement
: rewriteTreeAtom
| rewriteTree
| rewriteTreeEbnf
;
rewriteTreeAtom
: CHAR_LITERAL
| ^(TOKEN_REF ARG_ACTION)
| TOKEN_REF
| RULE_REF
| STRING_LITERAL
| LABEL
| ACTION
;
rewriteTreeEbnf
: ^(ebnfSuffix ^(BLOCK rewriteTreeAlt))
;
rewriteTree
: ^(TREE_BEGIN rewriteTreeAtom rewriteTreeElement* )
;
rewriteTemplate
: ^(TEMPLATE rewriteTemplateArgs? DOUBLE_QUOTE_STRING_LITERAL)
| ^(TEMPLATE rewriteTemplateArgs? DOUBLE_ANGLE_STRING_LITERAL)
| rewriteTemplateRef
| rewriteIndirectTemplateHead
| ACTION
;
rewriteTemplateRef
: ^(TEMPLATE ID rewriteTemplateArgs?)
;
rewriteIndirectTemplateHead
: ^(TEMPLATE ACTION rewriteTemplateArgs?)
;
rewriteTemplateArgs
: ^(ARGLIST rewriteTemplateArg+)
;
rewriteTemplateArg
: ^(ARG ID ACTION)
;

View File

@ -0,0 +1,14 @@
package org.antlr.v4.tool;
/** Defines behavior of object able to handle error messages from ANTLR including
* both tool errors like "can't write file" and grammar ambiguity warnings.
* To avoid having to change tools that use ANTLR (like GUIs), I am
* wrapping error data in Message objects and passing them to the listener.
* In this way, users of this interface are less sensitive to changes in
* the info I need for error messages.
*/
public interface ANTLRErrorListener {
public void info(String msg);
public void error(Message msg);
public void warning(Message msg);
}

View File

@ -0,0 +1,152 @@
package org.antlr.v4.tool;
import org.antlr.runtime.CommonToken;
import org.antlr.runtime.tree.CommonTree;
import org.antlr.tool.ToolMessage;
import org.stringtemplate.ST;
import org.stringtemplate.STGroup;
/** Defines all the errors ANTLR can generator for both the tool and for
* issues with a grammar.
*
* Here is a list of language names:
*
* http://ftp.ics.uci.edu/pub/ietf/http/related/iso639.txt
*
* Here is a list of country names:
*
* http://www.chemie.fu-berlin.de/diverse/doc/ISO_3166.html
*
* I use constants not strings to identify messages as the compiler will
* find any errors/mismatches rather than leaving a mistyped string in
* the code to be found randomly in the future. Further, Intellij can
* do field name expansion to save me some typing. I have to map
* int constants to template names, however, which could introduce a mismatch.
* Someone could provide a .stg file that had a template name wrong. When
* I load the group, then, I must verify that all messages are there.
*
* This is essentially the functionality of the resource bundle stuff Java
* has, but I don't want to load a property file--I want to load a template
* group file and this is so simple, why mess with their junk.
*
* I use the default Locale as defined by java to compute a group file name
* in the org/antlr/tool/templates/messages dir called en_US.stg and so on.
*
* Normally we want to use the default locale, but often a message file will
* not exist for it so we must fall back on the US local.
*
* During initialization of this class, all errors go straight to System.err.
* There is no way around this. If I have not set up the error system, how
* can I do errors properly? For example, if the string template group file
* full of messages has an error, how could I print to anything but System.err?
*/
public class ErrorManager {
/** The group of templates that represent all possible ANTLR errors. */
private static STGroup messages;
/** The group of templates that represent the current message format. */
private static STGroup format;
static ANTLRErrorListener theDefaultErrorListener = new ANTLRErrorListener() {
public void info(String msg) {
if (formatWantsSingleLineMessage()) {
msg = msg.replaceAll("\n", " ");
}
System.err.println(msg);
}
public void error(Message msg) {
String outputMsg = msg.toString();
if (formatWantsSingleLineMessage()) {
outputMsg = outputMsg.replaceAll("\n", " ");
}
System.err.println(outputMsg);
}
public void warning(Message msg) {
String outputMsg = msg.toString();
if (formatWantsSingleLineMessage()) {
outputMsg = outputMsg.replaceAll("\n", " ");
}
System.err.println(outputMsg);
}
};
public static ANTLRErrorListener getErrorListener() {
return theDefaultErrorListener;
}
/** Return a StringTemplate that refers to the current format used for
* emitting messages.
*/
public static ST getLocationFormat() {
return format.getInstanceOf("location");
}
public static ST getReportFormat() {
return format.getInstanceOf("report");
}
public static ST getMessageFormat() {
return format.getInstanceOf("message");
}
public static boolean formatWantsSingleLineMessage() {
return format.getInstanceOf("wantsSingleLineMessage").toString().equals("true");
}
public static void resetErrorState() {
}
public static void info(Object... args) {
}
/**
* Raise a predefined message with some number of paramters for the StringTemplate but for which there
* is no location information possible.
* @param msg The Message Descriptor
* @param args The arguments to pass to the StringTemplate
*/
public static void msg(Msg msg, Object... args) {
}
/**
* Raise a predefined message with some number of parameters for the StringTemplate
* with error information supplied explicitly.
*
* @param msg The Message Descriptor
* @param args The arguments to pass to the StringTemplate
*/
public static void msg(Msg msg, int line, int column, int absOffset, int endLine, int endColumn, int endAbsOffset, Object... args) {
}
/**
* Raise a predefined message with some number of paramters for the StringTemplate, for which there is a CommonToken
* that can give us the location information.
* @param msg The message descriptor.
* @param t The token that contains our location information
* @param args The varargs array of values that will be set in the StrngTemplate as arg0, arg1, ... argn
*/
public static void msg(Msg msg, CommonToken t, Object... args) {
}
/**
* Construct a message when we have a node stream and AST node that we can extract location
* from and possbily some arguments for the StringTemplate that will construct this message.
*
* @param msg The message descriptor
* @param node The node that gives us the information we need
* @param args The varargs array of values that will be set in the StrngTemplate as arg0, arg1, ... argn
*/
public static void msg(Msg msg, CommonTree node, Object... args) {
}
/** Process a new message by sending it on to the error listener associated with the current thread
* and recording any information we need in the error state for the current thread.
*/
private static void processMessage() {
}
public static int getNumErrors() {
return 0;
}
}

View File

@ -0,0 +1,69 @@
package org.antlr.v4.tool;
import org.antlr.runtime.*;
import org.antlr.runtime.tree.CommonTree;
import org.antlr.v4.Tool;
import org.antlr.v4.parse.ANTLRLexer;
import org.antlr.v4.parse.ANTLRParser;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Grammar {
public Tool tool;
public String name;
public GrammarAST ast;
public String text; // testing only
protected List<Grammar> importedGrammars;
protected Map<String, Rule> rules = new HashMap<String, Rule>();
public Grammar(Tool tool, String name, GrammarAST ast) {
this.tool = tool;
this.name = name;
this.ast = ast;
}
/** For testing */
public Grammar(String grammarText) throws RecognitionException {
this.text = grammarText;
ANTLRStringStream in = new ANTLRStringStream(grammarText);
ANTLRLexer lexer = new ANTLRLexer(in);
CommonTokenStream tokens = new CommonTokenStream(lexer);
ANTLRParser p = new ANTLRParser(tokens);
ParserRuleReturnScope r = p.grammarSpec();
ast = (GrammarAST) r.getTree();
System.out.println(ast.toStringTree());
}
public void loadImportedGrammars() {
CommonTree i = (CommonTree)ast.getFirstChildWithType(ANTLRParser.IMPORT);
if ( i==null ) return;
importedGrammars = new ArrayList<Grammar>();
for (Object c : i.getChildren()) {
CommonTree t = (CommonTree)c;
String importedGrammarName = null;
if ( t.getType()==ANTLRParser.ASSIGN ) {
importedGrammarName = t.getChild(1).getText();
System.out.println("import "+ importedGrammarName);
}
else if ( t.getType()==ANTLRParser.ID ) {
importedGrammarName = t.getText();
System.out.println("import "+t.getText());
}
try {
Grammar g = tool.load(importedGrammarName+".g");
importedGrammars.add(g);
}
catch (Exception e) {
System.err.println("can't load grammar "+importedGrammarName);
}
}
}
public Rule getRule(String name) {
return null;
}
}

View File

@ -0,0 +1,27 @@
package org.antlr.v4.tool;
import org.antlr.runtime.Token;
import org.antlr.runtime.tree.CommonTree;
import org.antlr.runtime.tree.Tree;
import org.antlr.v4.gui.ASTViewer;
public class GrammarAST extends CommonTree {
public GrammarAST() {;}
public GrammarAST(Token t) { super(t); }
public GrammarAST(GrammarAST node) { super(node); }
public void inspect() {
ASTViewer viewer = new ASTViewer(this);
viewer.open();
}
@Override
public Tree dupNode() {
return new GrammarAST(this);
}
@Override
public String toString() {
return super.toString();
}
}

View File

@ -0,0 +1,23 @@
package org.antlr.v4.tool;
import org.antlr.runtime.RecognitionException;
import org.antlr.runtime.Token;
import org.antlr.runtime.TokenStream;
import org.antlr.runtime.tree.CommonErrorNode;
/** A node representing erroneous token range in token stream */
public class GrammarASTErrorNode extends GrammarAST {
CommonErrorNode delegate;
public GrammarASTErrorNode(TokenStream input, Token start, Token stop,
RecognitionException e)
{
delegate = new CommonErrorNode(input,start,stop,e);
}
public boolean isNil() { return delegate.isNil(); }
public int getType() { return delegate.getType(); }
public String getText() { return delegate.getText(); }
public String toString() { return delegate.toString(); }
}

View File

@ -0,0 +1,5 @@
package org.antlr.v4.tool;
/** */
public class Message {
}

View File

@ -0,0 +1,79 @@
/*
[The "BSD licence"]
Copyright (c) 2005-2008 Terence Parr
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.antlr.v4.tool;
/**
* Abstracts away the definition of Message severity and the text that should
* display to represent that severity if there is no StringTemplate available
* to do it.
*
* @author Jim Idle - Temporal Wave LLC (jimi@temporal-wave.com)
*/
public enum MessageSeverity {
/**
* How to describe informational messages
*/
INFO ("Info"),
/**
* How to describe warning messages
*/
WARNING ("Warning"),
/**
* How to describe error messages
*/
ERROR ("Error"),
;
/**
* The text version of the ENUM value, used for display purposes
*/
private final String text;
/**
* Standard getter method for the text that should be displayed in order to
* represent the severity to humans and product modelers.
*
* @return The human readable string representing the severity level
*/
public String getText() {
return text;
}
/**
* Standard constructor to build an instance of the Enum entries
*
* @param text The human readable string representing the serverity level
*/
private MessageSeverity(String text) {
this.text = text;
}
}

View File

@ -0,0 +1,222 @@
/*
[The "BSD licence"]
Copyright (c) 2005-2008 Terence Parr
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.antlr.v4.tool;
/**
* A complex enumeration of all the error messages that the tool can issue.
*
* When adding error messages, also add a description of the message to the
* Wiki with a location under the Wiki page
* <a href="http://www.antlr.org/wiki/display/ANTLR3/Errors+Reported+by+the+ANTLR+Tool">Errors Reported by the ANTLR Tool</a>.
*
*
* @author Jim Idle <jimi@temporal-wave.com>
* @since 4.0
*/
public enum Msg {
CANNOT_WRITE_FILE(MessageSeverity.ERROR, true, true),
CANNOT_CLOSE_FILE(MessageSeverity.ERROR, true, true),
CANNOT_FIND_TOKENS_FILE(MessageSeverity.ERROR, true, true),
ERROR_READING_TOKENS_FILE(MessageSeverity.ERROR, true, true),
DIR_NOT_FOUND(MessageSeverity.ERROR, true, true),
OUTPUT_DIR_IS_FILE(MessageSeverity.ERROR, true, true),
CANNOT_OPEN_FILE(MessageSeverity.ERROR, true, true),
FILE_AND_GRAMMAR_NAME_DIFFER(MessageSeverity.ERROR, true, true),
FILENAME_EXTENSION_ERROR(MessageSeverity.ERROR, true, true),
INTERNAL_ERROR(MessageSeverity.ERROR, true, true),
INTERNAL_WARNING(MessageSeverity.ERROR, true, true),
ERROR_CREATING_ARTIFICIAL_RULE(MessageSeverity.ERROR, true, true),
TOKENS_FILE_SYNTAX_ERROR(MessageSeverity.ERROR, true, true),
CANNOT_GEN_DOT_FILE(MessageSeverity.ERROR, true, true),
BAD_AST_STRUCTURE(MessageSeverity.ERROR, true, true),
// Code generation errors
MISSING_CODE_GEN_TEMPLATES(MessageSeverity.ERROR, false, true),
CANNOT_CREATE_TARGET_GENERATOR(MessageSeverity.ERROR, false, true),
// Grammar errors
SYNTAX_ERROR(MessageSeverity.ERROR, true, true),
RULE_REDEFINITION(MessageSeverity.ERROR, true, true),
LEXER_RULES_NOT_ALLOWED(MessageSeverity.ERROR, true, true),
PARSER_RULES_NOT_ALLOWED(MessageSeverity.ERROR, true, true),
CANNOT_FIND_ATTRIBUTE_NAME_IN_DECL(MessageSeverity.ERROR, true, true),
NO_TOKEN_DEFINITION(MessageSeverity.ERROR, true, true),
UNDEFINED_RULE_REF(MessageSeverity.ERROR, true, true),
LITERAL_NOT_ASSOCIATED_WITH_LEXER_RULE(MessageSeverity.ERROR, true, true),
CANNOT_ALIAS_TOKENS_IN_LEXER(MessageSeverity.ERROR, true, true),
ATTRIBUTE_REF_NOT_IN_RULE(MessageSeverity.ERROR, true, true),
INVALID_RULE_SCOPE_ATTRIBUTE_REF(MessageSeverity.ERROR, true, true),
UNKNOWN_SIMPLE_ATTRIBUTE(MessageSeverity.ERROR, true, true),
INVALID_RULE_PARAMETER_REF(MessageSeverity.ERROR, true, true),
UNKNOWN_RULE_ATTRIBUTE(MessageSeverity.ERROR, true, true),
ISOLATED_RULE_SCOPE(MessageSeverity.ERROR, true, true),
SYMBOL_CONFLICTS_WITH_GLOBAL_SCOPE(MessageSeverity.ERROR, true, true),
LABEL_CONFLICTS_WITH_RULE(MessageSeverity.ERROR, true, true),
LABEL_CONFLICTS_WITH_TOKEN(MessageSeverity.ERROR, true, true),
LABEL_CONFLICTS_WITH_RULE_SCOPE_ATTRIBUTE(MessageSeverity.ERROR, true, true),
LABEL_CONFLICTS_WITH_RULE_ARG_RETVAL(MessageSeverity.ERROR, true, true),
ATTRIBUTE_CONFLICTS_WITH_RULE(MessageSeverity.ERROR, true, true),
ATTRIBUTE_CONFLICTS_WITH_RULE_ARG_RETVAL(MessageSeverity.ERROR, true, true),
LABEL_TYPE_CONFLICT(MessageSeverity.ERROR, true, true),
ARG_RETVAL_CONFLICT(MessageSeverity.ERROR, true, true),
NONUNIQUE_REF(MessageSeverity.ERROR, true, true),
FORWARD_ELEMENT_REF(MessageSeverity.ERROR, true, true),
MISSING_RULE_ARGS(MessageSeverity.ERROR, true, true),
RULE_HAS_NO_ARGS(MessageSeverity.ERROR, true, true),
ARGS_ON_TOKEN_REF(MessageSeverity.ERROR, true, true),
RULE_REF_AMBIG_WITH_RULE_IN_ALT(MessageSeverity.ERROR, true, true),
ILLEGAL_OPTION(MessageSeverity.ERROR, true, true),
LIST_LABEL_INVALID_UNLESS_RETVAL_STRUCT(MessageSeverity.ERROR, true, true),
UNDEFINED_TOKEN_REF_IN_REWRITE(MessageSeverity.ERROR, true, true),
REWRITE_ELEMENT_NOT_PRESENT_ON_LHS(MessageSeverity.ERROR, true, true),
UNDEFINED_LABEL_REF_IN_REWRITE(MessageSeverity.ERROR, true, true),
NO_GRAMMAR_START_RULE(MessageSeverity.ERROR, true, true),
EMPTY_COMPLEMENT(MessageSeverity.ERROR, true, true),
UNKNOWN_DYNAMIC_SCOPE(MessageSeverity.ERROR, true, true),
UNKNOWN_DYNAMIC_SCOPE_ATTRIBUTE(MessageSeverity.ERROR, true, true),
ISOLATED_RULE_ATTRIBUTE(MessageSeverity.ERROR, true, true),
INVALID_ACTION_SCOPE(MessageSeverity.ERROR, true, true),
ACTION_REDEFINITION(MessageSeverity.ERROR, true, true),
INVALID_TEMPLATE_ACTION(MessageSeverity.ERROR, true, true),
ARG_INIT_VALUES_ILLEGAL(MessageSeverity.ERROR, true, true),
REWRITE_OR_OP_WITH_NO_OUTPUT_OPTION(MessageSeverity.ERROR, true, true),
NO_RULES(MessageSeverity.ERROR, true, true),
WRITE_TO_READONLY_ATTR(MessageSeverity.ERROR, true, true),
MISSING_AST_TYPE_IN_TREE_GRAMMAR(MessageSeverity.ERROR, true, true),
REWRITE_FOR_MULTI_ELEMENT_ALT(MessageSeverity.ERROR, true, true),
RULE_INVALID_SET(MessageSeverity.ERROR, true, true),
HETERO_ILLEGAL_IN_REWRITE_ALT(MessageSeverity.ERROR, true, true),
NO_SUCH_GRAMMAR_SCOPE(MessageSeverity.ERROR, true, true),
NO_SUCH_RULE_IN_SCOPE(MessageSeverity.ERROR, true, true),
TOKEN_ALIAS_CONFLICT(MessageSeverity.ERROR, true, true),
TOKEN_ALIAS_REASSIGNMENT(MessageSeverity.ERROR, true, true),
TOKEN_VOCAB_IN_DELEGATE(MessageSeverity.ERROR, true, true),
INVALID_IMPORT(MessageSeverity.ERROR, true, true),
IMPORTED_TOKENS_RULE_EMPTY(MessageSeverity.ERROR, true, true),
IMPORT_NAME_CLASH(MessageSeverity.ERROR, true, true),
AST_OP_WITH_NON_AST_OUTPUT_OPTION(MessageSeverity.ERROR, true, true),
AST_OP_IN_ALT_WITH_REWRITE(MessageSeverity.ERROR, true, true),
WILDCARD_AS_ROOT(MessageSeverity.ERROR, true, true),
CONFLICTING_OPTION_IN_TREE_FILTER(MessageSeverity.ERROR, true, true),
/** Documentation comment is unterminated */
UNTERMINATED_DOC_COMMENT(MessageSeverity.ERROR, true, true),
// Dependency sorting errors
//
/** t1.g -> t2.g -> t3.g ->t1.g */
CIRCULAR_DEPENDENCY(MessageSeverity.ERROR, true, true),
// Simple informational messages
//
/** A standby generic message that jsut spits out the arguments it is given */
GENERIC_INFO(MessageSeverity.INFO, false, false),
/** How to print out the version of the ANTLR tool that we are */
ANTLR_VERSION(MessageSeverity.INFO, false, false),
// Command line tool errors/warnings
/** -fo option was incorrectly formed */
MISSING_OUTPUT_FO(MessageSeverity.WARNING, false, false),
/** -lib option is missing a directory argument */
MISSING_LIBDIR(MessageSeverity.WARNING, false, false),
/** -format option was not given the name of a message format */
MISSING_FORMAT(MessageSeverity.WARNING, false, false),
/** Max state count missing from the option */
MISSING_MAXSTATES(MessageSeverity.WARNING, false, false),
/** Max labels in a switch argument is missing */
MISSING_MAXSWITCH(MessageSeverity.WARNING, false, false),
/** Min labels in a switch argument is missing */
MISSING_MINSWITCH(MessageSeverity.WARNING, false, false),
/** Missing recursion limit argument */
MISSING_MAXRECUR(MessageSeverity.WARNING, false, false),
/** Missing max edges argument */
MISSING_MAXEDGE(MessageSeverity.WARNING, false, false),
/** Misng ms timeout argument */
MISSING_MAXTIME(MessageSeverity.WARNING, false, false),
// Help messages
HELP_USAGE(MessageSeverity.INFO, false, false),
HELP_EXTENDED(MessageSeverity.INFO, false, false),
;
/**
* Local storage for the severity level of the message
*/
private MessageSeverity severity;
/**
* Returns the severity level of this message
* @return
*/
public MessageSeverity getSeverity() {
return severity;
}
/**
* Internal storage for the flag that indicates whether this particular message
* should abort the analysis phase or not.
*/
private Boolean abortsAnalysis;
/**
* Indicates whether the raising of this error messsage should abort the
* analysis phase (or prevent it from starting).
*
* @return true if this message should abort the analysis phase
*/
public Boolean abortsAnalysis() {
return abortsAnalysis;
}
/**
* Indicates whether the raising of this error message aborts code
* generation or not.
*/
private Boolean abortsCodegen;
/**
* Indicates whether the raising of this error message aborts code
* generation or not.
*
* @return true if this message should abort code generation
*/
public Boolean abortsCodegen() {
return abortsCodegen;
}
/**
* Local constructor produces an instance of the entries in this Enum
*/
private Msg(MessageSeverity severity, boolean abortsAnalysis, boolean abortsCodegen) {
this.severity = severity;
this.abortsAnalysis = abortsAnalysis;
}
}

View File

@ -0,0 +1,7 @@
package org.antlr.v4.tool;
import org.antlr.runtime.tree.CommonTree;
public class Rule {
public CommonTree tree;
}