From 8d9c3a8b572dbb84959e4aedfc06d863255fc886 Mon Sep 17 00:00:00 2001 From: Kuba Ober Date: Thu, 29 Aug 2019 10:28:36 -0400 Subject: [PATCH] Fix by renaming enumerants that happen to be keywords. This fixes e.g. the compilation of capnp/schema.capnp. --- capnpc-csharp/Model/Enumerant.cs | 6 +++++- capnpc-csharp/Model/IdentifierRenamer.cs | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 capnpc-csharp/Model/IdentifierRenamer.cs diff --git a/capnpc-csharp/Model/Enumerant.cs b/capnpc-csharp/Model/Enumerant.cs index 99d53fc..96d9137 100644 --- a/capnpc-csharp/Model/Enumerant.cs +++ b/capnpc-csharp/Model/Enumerant.cs @@ -2,8 +2,12 @@ { class Enumerant { + string _literal; public TypeDefinition TypeDefinition { get; set; } - public string Literal { get; set; } + public string Literal { + get => _literal; + set => _literal = IdentifierRenamer.ToNonKeyword(value); + } public ushort? Ordinal { get; set; } public int CodeOrder { get; set; } } diff --git a/capnpc-csharp/Model/IdentifierRenamer.cs b/capnpc-csharp/Model/IdentifierRenamer.cs new file mode 100644 index 0000000..9c6ff07 --- /dev/null +++ b/capnpc-csharp/Model/IdentifierRenamer.cs @@ -0,0 +1,22 @@ +using Microsoft.CodeAnalysis.CSharp; +using System; +using System.Collections.Generic; +using System.Text; + +namespace CapnpC.Model +{ + public class IdentifierRenamer + { + public static bool IsAnyKeyword(string str) + { + return SyntaxFacts.GetKeywordKind(str) != SyntaxKind.None + || SyntaxFacts.GetContextualKeywordKind(str) != SyntaxKind.None; + } + public static string ToNonKeyword(string str) + { + // Capnp schema identifiers should be already valid, but could be a keyword + if (IsAnyKeyword(str)) return $"@{str}"; + return str; + } + } +}