From ec9f0c7af648f3a69cc2c73919a0d355ad4d264d Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Sat, 28 Apr 2018 13:20:40 -0500 Subject: [PATCH] Fix code generation for rules named 'keys' and 'values' Fixes #280 --- .../tool/templates/codegen/CSharp/CSharp.stg | 6 ++++ .../antlr/v4/codegen/target/CSharpTarget.java | 29 +++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/tool/resources/org/antlr/v4/tool/templates/codegen/CSharp/CSharp.stg b/tool/resources/org/antlr/v4/tool/templates/codegen/CSharp/CSharp.stg index b40276ef..bf014113 100644 --- a/tool/resources/org/antlr/v4/tool/templates/codegen/CSharp/CSharp.stg +++ b/tool/resources/org/antlr/v4/tool/templates/codegen/CSharp/CSharp.stg @@ -1108,5 +1108,11 @@ csIdentifier ::= [ "void" : "@void", "volatile" : "@volatile", "while" : "@while", + + // These are not keywords, but including them explicitly prevents StringTemplate from treating them as keywords. + // https://github.com/tunnelvisionlabs/antlr4cs/issues/280 + "keys": "keys", + "values": "values", + default : key ] diff --git a/tool/src/org/antlr/v4/codegen/target/CSharpTarget.java b/tool/src/org/antlr/v4/codegen/target/CSharpTarget.java index cbd2ebfc..42a084bf 100644 --- a/tool/src/org/antlr/v4/codegen/target/CSharpTarget.java +++ b/tool/src/org/antlr/v4/codegen/target/CSharpTarget.java @@ -29,16 +29,21 @@ */ package org.antlr.v4.codegen.target; +import java.util.Map; import org.antlr.v4.codegen.CodeGenerator; import org.antlr.v4.codegen.Target; import org.antlr.v4.tool.ErrorType; import org.antlr.v4.tool.ast.GrammarAST; +import org.stringtemplate.v4.Interpreter; import org.stringtemplate.v4.NumberRenderer; +import org.stringtemplate.v4.ST; import org.stringtemplate.v4.STErrorListener; import org.stringtemplate.v4.STGroup; import org.stringtemplate.v4.STGroupFile; import org.stringtemplate.v4.StringRenderer; +import org.stringtemplate.v4.misc.MapModelAdaptor; import org.stringtemplate.v4.misc.STMessage; +import org.stringtemplate.v4.misc.STNoSuchPropertyException; public abstract class CSharpTarget extends Target { @@ -138,6 +143,30 @@ protected boolean visibleGrammarSymbolCausesIssueInGeneratedCode(GrammarAST idNo protected STGroup loadTemplates() { // override the superclass behavior to put all C# templates in the same folder STGroup result = new STGroupFile(CodeGenerator.TEMPLATE_ROOT+"/CSharp/"+getLanguage()+STGroup.GROUP_FILE_EXTENSION); + result.registerModelAdaptor(Map.class, new MapModelAdaptor() { + @Override + public Object getProperty(Interpreter interp, ST self, Object o, Object property, String propertyName) throws STNoSuchPropertyException { + // Override the default behavior to fix handling of 'keys' and 'values'. + // https://github.com/tunnelvisionlabs/antlr4cs/issues/280 + Map map = (Map)o; + Object value; + if (map.containsKey(property)) { + value = map.get(property); + } + else if (map.containsKey(propertyName)) { + value = map.get(propertyName); + } + else { + return super.getProperty(interp, self, o, property, propertyName); + } + + if (value == STGroup.DICT_KEY) { + value = property; + } + + return value; + } + }); result.registerRenderer(Integer.class, new NumberRenderer()); result.registerRenderer(String.class, new StringRenderer()); result.setListener(new STErrorListener() {