2017-03-03 39 views
5

工作我想建立一個簡單的項目,ANTLR的.NET核心使用VS2017 1.0項目。ANTLR的不與VS2017

正在關注https://github.com/sharwell/antlr4cs,向項目中添加了.g4文件。該項目文件看起來像這樣,

<Project Sdk="Microsoft.NET.Sdk">  
    <PropertyGroup> 
    <TargetFramework>netcoreapp1.0</TargetFramework> </PropertyGroup> 

    <ItemGroup> 
    <None Remove="Calculator.g4" /> </ItemGroup> 

    <ItemGroup> 
    <PackageReference Include="Antlr4" Version="4.5.4-beta001" /> </ItemGroup> 

    <ItemGroup> 
    <AdditionalFiles Include="Calculator.g4" /> 
    </ItemGroup> 
</Project> 

但文件說,

根據上表中的MSBuild屬性 列中找到現有的XML元素,或添加一個,如果它不存在。 例如,要爲解析器生成分析樹偵聽器和訪問者接口和基類,請將項目項目 更新爲類似於以下內容。

<Antlr4 Include="CustomLanguage.g4"> 
<Generator>MSBuild:Compile</Generator> 
<CustomToolNamespace>MyProject.Folder</CustomToolNamespace> 
<Listener>True</Listener> <Visitor>True</Visitor> </Antlr4> 

沒有在這個凸出文件中的任何Antlr4標籤。 Antlr不支持VS2017? 是一個很好的例子,我可以遵循使用.net核心ANtlr嗎?

+0

我只是測試4。在幾種情況下6.1-beta001,它工作得很好。 –

+0

@LexLi VS2017? – Dhanuka777

+0

當然在VS2017 RTM。 –

回答

4

這只是我需要在我的.NET標準1.3類庫項目來容納語法文件。

<ItemGroup> 
    <Antlr4 Include="Something.g4"> 
     <Generator>MSBuild:Compile</Generator> 
     <Listener>False</Listener> 
     <Visitor>False</Visitor> 
    </Antlr4> 
    </ItemGroup> 
    <ItemGroup> 
    <PackageReference Include="Antlr4" Version="4.6.1-beta001" /> 
    </ItemGroup> 

注意,你可能會使用一個新的Antlr4包版本是唯一版本4.6.1創建這個答案的時候。

+0

剛剛落地於此,尋找(不存在的)文檔。作爲實驗性功能,4.6.5-beta001是最新版本,它消除了對Java的依賴。您需要添加 True True

1

我只是在尋找同樣的東西,對於.NET Core 2.0。

當前的ANTLR4軟件包版本是4.6.5 Beta 1.在此版本中,C#生成器已移植到C#,因此對Java的依賴性被刪除。這仍處於試驗階段,必須以手動啓用:

<Antlr4UseCSharpGenerator>True</Antlr4UseCSharpGenerator> 

文件時.g4文件被修改也不會產生。它們將在調用dotnet build時生成。

文件匹配按預期工作,但更改設置如<Visitor>false</Visitor>需要在dotnet build之前調用dotnet clean

默認ANTLR的任務選項可以發現in the source

<Antlr4> 
    <Generator>MSBuild:Compile</Generator> 
    <CustomToolNamespace Condition="'$(Antlr4IsSdkProject)' != 'True'">$(RootNamespace)</CustomToolNamespace> 
    <CopyToOutputDirectory>Never</CopyToOutputDirectory> 
    <Encoding>UTF-8</Encoding> 
    <TargetLanguage>CSharp</TargetLanguage> 
    <Listener>true</Listener> 
    <Visitor>true</Visitor> 
    <Abstract>false</Abstract> 
    <ForceAtn>false</ForceAtn> 
</Antlr4> 

通配符的作品,所以如果我想建立所有g4文件,並禁止遊客,我不得不寫的是:

<ItemGroup> 
    <Antlr4 Include="**/*.g4" > 
    <Visitor>false</Visitor> 
    </Antlr4> 
</ItemGroup> 

我的整個csproj文件看起來像這樣:

<Project Sdk="Microsoft.NET.Sdk"> 

    <PropertyGroup> 
    <TargetFramework>netstandard2.0</TargetFramework> 
    <Antlr4UseCSharpGenerator>True</Antlr4UseCSharpGenerator> 
    </PropertyGroup> 


    <ItemGroup> 
    <PackageReference Include="Antlr4"> 
     <Version>4.6.5-beta001</Version> 
    </PackageReference> 
    </ItemGroup> 

    <ItemGroup> 
     <Antlr4 Include="**/*.g4" > 
     <Visitor>false</Visitor> 
     </Antlr4> 
    </ItemGroup> 

</Project>