From d00ec9d2274abf0b5efb39baa67d4eed7a2f664c Mon Sep 17 00:00:00 2001 From: Terence Parr Date: Thu, 16 Feb 2012 17:31:30 -0800 Subject: [PATCH] got -no prefix options working. rm'd dup error from toolError(); rm dbg print --- tool/playground/A.g4 | 6 ------ tool/playground/AVisitor.java | 6 ++++-- tool/src/org/antlr/v4/Tool.java | 7 ++++++- tool/src/org/antlr/v4/codegen/CodeGenPipeline.java | 4 +++- .../org/antlr/v4/semantics/BasicSemanticChecks.java | 3 --- tool/src/org/antlr/v4/tool/ErrorManager.java | 12 ++++++++---- tool/src/org/antlr/v4/tool/ErrorType.java | 3 ++- 7 files changed, 23 insertions(+), 18 deletions(-) diff --git a/tool/playground/A.g4 b/tool/playground/A.g4 index ce79583fc..9c2d6ea73 100644 --- a/tool/playground/A.g4 +++ b/tool/playground/A.g4 @@ -8,11 +8,5 @@ e : e '*' e -> Mult | '(' e ')' -> Parens ; -x : A -> Foo - | B - ; - -y : Y -> Mult ; - INT : [0-9]+ ; WS : [ \t\n]+ -> skip ; diff --git a/tool/playground/AVisitor.java b/tool/playground/AVisitor.java index 4bd72a0f0..5730fa2d6 100644 --- a/tool/playground/AVisitor.java +++ b/tool/playground/AVisitor.java @@ -1,8 +1,10 @@ +import org.antlr.v4.runtime.tree.*; +import org.antlr.v4.runtime.Token; + public interface AVisitor { T visit(AParser.MultContext ctx); T visit(AParser.ParensContext ctx); T visit(AParser.sContext ctx); - T visit(AParser.FooContext ctx); T visit(AParser.AddContext ctx); T visit(AParser.IntContext ctx); -} +} \ No newline at end of file diff --git a/tool/src/org/antlr/v4/Tool.java b/tool/src/org/antlr/v4/Tool.java index b7a11c82d..34172f8f3 100644 --- a/tool/src/org/antlr/v4/Tool.java +++ b/tool/src/org/antlr/v4/Tool.java @@ -186,8 +186,10 @@ public class Tool { grammarFiles.add(arg); continue; } + boolean found = false; for (Option o : optionDefs) { if ( arg.equals(o.name) ) { + found = true; String argValue = null; if ( o.argType==OptionArgType.STRING ) { argValue = args[i]; @@ -198,7 +200,7 @@ public class Tool { try { Field f = c.getField(o.fieldName); if ( argValue==null ) { - if ( o.fieldName.startsWith("-no-") ) f.setBoolean(this, false); + if ( arg.startsWith("-no-") ) f.setBoolean(this, false); else f.setBoolean(this, true); } else f.set(this, argValue); @@ -208,6 +210,9 @@ public class Tool { } } } + if ( !found ) { + errMgr.toolError(ErrorType.INVALID_CMDLINE_ARG, arg); + } } if ( outputDirectory!=null ) { if (outputDirectory.endsWith("/") || diff --git a/tool/src/org/antlr/v4/codegen/CodeGenPipeline.java b/tool/src/org/antlr/v4/codegen/CodeGenPipeline.java index d89443fcd..6b2286738 100644 --- a/tool/src/org/antlr/v4/codegen/CodeGenPipeline.java +++ b/tool/src/org/antlr/v4/codegen/CodeGenPipeline.java @@ -46,9 +46,11 @@ public class CodeGenPipeline { } else { gen.writeRecognizer(gen.generateParser()); - if ( g.tool.gen_listener) { + if ( g.tool.gen_listener ) { gen.writeListener(gen.generateListener()); gen.writeBaseListener(gen.generateBaseListener()); + } + if ( g.tool.gen_visitor ) { gen.writeVisitor(gen.generateVisitor()); gen.writeBaseVisitor(gen.generateBaseVisitor()); } diff --git a/tool/src/org/antlr/v4/semantics/BasicSemanticChecks.java b/tool/src/org/antlr/v4/semantics/BasicSemanticChecks.java index d39b46cf0..137326bbf 100644 --- a/tool/src/org/antlr/v4/semantics/BasicSemanticChecks.java +++ b/tool/src/org/antlr/v4/semantics/BasicSemanticChecks.java @@ -250,7 +250,6 @@ public class BasicSemanticChecks extends GrammarTreeVisitor { for (int i=0; i< nalts; i++) { AltAST altAST = (AltAST)blk.getChild(i); if ( altAST.altLabel!=null ) { - System.out.println("alt label "+altAST.altLabel); ruleToAltLabels.map(rule.getRuleName(), altAST.altLabel); String altLabel = altAST.altLabel.getText(); String prevRuleForLabel = altLabelToRuleName.get(altLabel); @@ -266,11 +265,9 @@ public class BasicSemanticChecks extends GrammarTreeVisitor { } } } - System.out.println(rule.getRuleName()+" has "+ nalts +" alts"); List altLabels = ruleToAltLabels.get(rule.getRuleName()); int numAltLabels = 0; if ( altLabels!=null ) numAltLabels = altLabels.size(); - System.out.println("labels="+altLabels); if ( numAltLabels>0 && nalts != numAltLabels ) { g.tool.errMgr.grammarError(ErrorType.RULE_WITH_TOO_FEW_ALT_LABELS, g.fileName, rule.getToken(), rule.getRuleName()); diff --git a/tool/src/org/antlr/v4/tool/ErrorManager.java b/tool/src/org/antlr/v4/tool/ErrorManager.java index 8f6a884e7..1d5d09ad9 100644 --- a/tool/src/org/antlr/v4/tool/ErrorManager.java +++ b/tool/src/org/antlr/v4/tool/ErrorManager.java @@ -30,11 +30,16 @@ package org.antlr.v4.tool; import org.antlr.v4.Tool; -import org.stringtemplate.v4.*; -import org.stringtemplate.v4.misc.*; +import org.stringtemplate.v4.ST; +import org.stringtemplate.v4.STErrorListener; +import org.stringtemplate.v4.STGroup; +import org.stringtemplate.v4.STGroupFile; +import org.stringtemplate.v4.misc.ErrorBuffer; +import org.stringtemplate.v4.misc.STMessage; import java.net.URL; -import java.util.*; +import java.util.Collection; +import java.util.Locale; public class ErrorManager { public static final String FORMATS_DIR = "org/antlr/v4/tool/templates/messages/formats/"; @@ -190,7 +195,6 @@ public class ErrorManager { public void toolError(ErrorType errorType, Object... args) { ToolMessage msg = new ToolMessage(errorType, args); emit(errorType, msg); - tool.error(msg); } public void toolError(ErrorType errorType, Throwable e, Object... args) { diff --git a/tool/src/org/antlr/v4/tool/ErrorType.java b/tool/src/org/antlr/v4/tool/ErrorType.java index 4b9f831b7..94ecfbef4 100644 --- a/tool/src/org/antlr/v4/tool/ErrorType.java +++ b/tool/src/org/antlr/v4/tool/ErrorType.java @@ -43,8 +43,9 @@ package org.antlr.v4.tool; public enum ErrorType { INVALID(0, "", ErrorSeverity.ERROR), + // Tool errors CANNOT_WRITE_FILE(1, "cannot write file : ", ErrorSeverity.ERROR), -// CANNOT_CLOSE_FILE(2, "cannot close file ", ErrorSeverity.ERROR), + INVALID_CMDLINE_ARG(2, "unknown command-line option ", ErrorSeverity.ERROR), CANNOT_FIND_TOKENS_FILE(3, "cannot find tokens file ", ErrorSeverity.ERROR), ERROR_READING_TOKENS_FILE(4, "cannot find tokens file : ", ErrorSeverity.ERROR), DIR_NOT_FOUND(5, "directory not found: ", ErrorSeverity.ERROR),