forked from jasder/antlr
Merge branch 'master-upstream'
This commit is contained in:
commit
47d639e7b7
|
@ -6,7 +6,7 @@ set -euo pipefail
|
|||
sudo sh -c 'echo "deb [arch=amd64] https://apt-mo.trafficmanager.net/repos/dotnet-release/ trusty main" > /etc/apt/sources.list.d/dotnetdev.list'
|
||||
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 417A0893
|
||||
sudo apt-get update
|
||||
sudo apt-get install dotnet-dev-1.0.1
|
||||
sudo apt-get install dotnet-dev-1.0.3
|
||||
|
||||
# install mvn
|
||||
wget http://apache.mirrors.lucidnetworks.net/maven/maven-3/3.3.9/binaries/apache-maven-3.3.9-bin.tar.gz && \
|
||||
|
|
|
@ -12,10 +12,10 @@ ln -s /usr/local/opt/openssl/lib/libcrypto.1.0.0.dylib /usr/local/lib/
|
|||
ln -s /usr/local/opt/openssl/lib/libssl.1.0.0.dylib /usr/local/lib/
|
||||
|
||||
# download dotnet core
|
||||
curl https://download.microsoft.com/download/8/F/9/8F9659B9-E628-4D1A-B6BF-C3004C8C954B/dotnet-1.1.1-sdk-osx-x64.pkg -o /tmp/dotnet-1.1.1-sdk-osx-x64.pkg
|
||||
curl https://download.microsoft.com/download/1/1/4/114223DE-0AD6-4B8A-A8FB-164E5862AF6E/dotnet-dev-osx-x64.1.0.3.pkg -o /tmp/dotnet-dev-osx-x64.1.0.3.pkg
|
||||
|
||||
# install dotnet core
|
||||
sudo installer -pkg /tmp/dotnet-1.1.1-sdk-osx-x64.pkg -target /
|
||||
sudo installer -pkg /tmp/dotnet-dev-osx-x64.1.0.3.pkg -target /
|
||||
|
||||
# make the link
|
||||
ln -s /usr/local/share/dotnet/dotnet /usr/local/bin/
|
||||
|
|
|
@ -1,4 +1,12 @@
|
|||
#!/bin/bash
|
||||
|
||||
mvn -q -Dparallel=methods -DthreadCount=4 -Dtest=csharp.* -Dantlr-csharp-netstandard=true test
|
||||
# we need to build the runtime before test run, since we used "--no-dependencies"
|
||||
# when we call dotnet cli for restore and build, in order to speed up
|
||||
|
||||
dotnet restore ../runtime/CSharp/runtime/CSharp/Antlr4.Runtime/Antlr4.Runtime.dotnet.csproj
|
||||
dotnet build -c Release ../runtime/CSharp/runtime/CSharp/Antlr4.Runtime/Antlr4.Runtime.dotnet.csproj
|
||||
|
||||
# call test
|
||||
|
||||
mvn -q -Dparallel=classes -DthreadCount=4 -Dtest=csharp.* -Dantlr-csharp-netstandard=true test
|
||||
|
||||
|
|
|
@ -144,3 +144,7 @@ YYYY/MM/DD, github id, Full name, email
|
|||
2017/03/15, robertvanderhulst, Robert van der Hulst, robert@xsharp.eu
|
||||
2017/03/28, cmd-johnson, Jonas Auer, jonas.auer.94@gmail.com
|
||||
2017/04/12, lys0716, Yishuang Lu, luyscmu@gmail.com
|
||||
2017/04/30, shravanrn, Shravan Narayan, shravanrn@gmail.com
|
||||
2017/05/11, jimallman, Jim Allman, jim@ibang.com
|
||||
2017/05/26, waf, Will Fuqua, wafuqua@gmail.com
|
||||
2017/05/29, kosak, Corey Kosak, kosak@kosak.com
|
||||
|
|
|
@ -63,9 +63,9 @@ int main(int argc, const char* argv[]) {
|
|||
CommonTokenStream tokens(&lexer);
|
||||
MyGrammarParser parser(&tokens);
|
||||
|
||||
Ref<tree::ParseTree> tree = parser.key();
|
||||
Ref<TreeShapeListener> listener(new TreeShapeListener());
|
||||
tree::ParseTreeWalker::DEFAULT->walk(listener, tree);
|
||||
tree::ParseTree *tree = parser.key();
|
||||
TreeShapeListener listener;
|
||||
tree::ParseTreeWalker::DEFAULT->walk(&listener, tree);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -88,7 +88,7 @@ func (this *TreeShapeListener) EnterEveryRule(ctx antlr.ParserRuleContext) {
|
|||
}
|
||||
|
||||
func main() {
|
||||
input := antlr.NewFileStream(os.Args[1])
|
||||
input, _ := antlr.NewFileStream(os.Args[1])
|
||||
lexer := parser.NewJSONLexer(input)
|
||||
stream := antlr.NewCommonTokenStream(lexer,0)
|
||||
p := parser.NewJSONParser(stream)
|
||||
|
|
|
@ -247,16 +247,13 @@ popd
|
|||
|
||||
*Publishing to Nuget from Linux/MacOSX*
|
||||
|
||||
**Getting ready to run Nuget**
|
||||
**Install the pre-requisites**
|
||||
|
||||
Of course you need Mono and `nuget` to be installed. On mac:
|
||||
|
||||
```bash
|
||||
brew install mono
|
||||
brew install nuget
|
||||
```
|
||||
|
||||
Or, you can [download nuget.exe](https://dist.nuget.org/win-x86-commandline/latest/nuget.exe).
|
||||
- mono - on mac, `brew install mono`
|
||||
- nuget - on mac, `brew install nuget` or you can [download nuget.exe](https://dist.nuget.org/win-x86-commandline/latest/nuget.exe)
|
||||
- dotnet - follow [the instructions here](https://www.microsoft.com/net/core)
|
||||
|
||||
From the shell on mac, you can check all is ok by typing
|
||||
|
||||
|
@ -266,36 +263,23 @@ nuget
|
|||
|
||||
This should display the nuget help.
|
||||
|
||||
**Creating the assembly**
|
||||
**Creating and packaging the assembly**
|
||||
|
||||
```bash
|
||||
$ cd runtime/CSharp/runtime/CSharp/Antlr4.Runtime
|
||||
$ xbuild /p:Configuration=Release Antlr4.Runtime.mono.csproj
|
||||
$ cd runtime/CSharp/runtime/CSharp
|
||||
$ ./build-nuget-package.sh
|
||||
...
|
||||
Copying file from '/Users/parrt/antlr/code/antlr4/runtime/CSharp/runtime/CSharp/Antlr4.Runtime/obj/net20/Release/Antlr4.Runtime.Standard.dll' to '/Users/parrt/antlr/code/antlr4/runtime/CSharp/runtime/CSharp/Antlr4.Runtime/lib/Release/Antlr4.Runtime.Standard.dll'
|
||||
Done building project "/Users/parrt/antlr/code/antlr4/runtime/CSharp/runtime/CSharp/Antlr4.Runtime/Antlr4.Runtime.mono.csproj".
|
||||
```
|
||||
|
||||
Alternately, you may want to build ANTLR using Xamarin Studio Community (free).
|
||||
|
||||
**Packaging for NuGet**
|
||||
|
||||
```bash
|
||||
cd runtime/CSharp/runtime/CSharp
|
||||
```
|
||||
|
||||
which is where the `Package.nuspec` file resides.
|
||||
|
||||
Type the following command:
|
||||
|
||||
```bash
|
||||
$ nuget pack Package.nuspec
|
||||
Build succeeded.
|
||||
0 Warning(s)
|
||||
0 Error(s)
|
||||
Attempting to build package from 'Package.nuspec'.
|
||||
Successfully created package '/Users/parrt/antlr/code/antlr4/runtime/CSharp/runtime/CSharp/Antlr4.Runtime.Standard.4.7.0.nupkg'.
|
||||
Successfully created package '/path/to/antlr/.../Antlr4.Runtime.Standard.4.7.0.nupkg'.
|
||||
```
|
||||
|
||||
This should display: Successfully created package *<package-path>*
|
||||
|
||||
Alternately, you may want to build ANTLR using Xamarin Studio Community (free).
|
||||
|
||||
**Publishing to NuGet**
|
||||
|
||||
You need to be a NuGet owner for "ANTLR 4 Standard Runtime"
|
||||
|
|
|
@ -127,19 +127,6 @@ public class BaseCSharpTest implements RuntimeTestSupport /*, SpecialRuntimeTest
|
|||
/** Errors found while running antlr */
|
||||
protected StringBuilder antlrToolErrors;
|
||||
|
||||
@org.junit.Rule
|
||||
public final TestRule testWatcher = new TestWatcher() {
|
||||
|
||||
@Override
|
||||
protected void succeeded(Description description) {
|
||||
// remove tmpdir if no error.
|
||||
if (!PRESERVE_TEST_DIR) {
|
||||
eraseTempDir();
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
@Override
|
||||
public void testSetUp() throws Exception {
|
||||
if (CREATE_PER_TEST_DIRECTORIES) {
|
||||
|
@ -150,7 +137,7 @@ public class BaseCSharpTest implements RuntimeTestSupport /*, SpecialRuntimeTest
|
|||
else {
|
||||
tmpdir = new File(BASE_TEST_DIR).getAbsolutePath();
|
||||
if (!PRESERVE_TEST_DIR && new File(tmpdir).exists()) {
|
||||
eraseFiles();
|
||||
eraseDirectory(new File(tmpdir));
|
||||
}
|
||||
}
|
||||
antlrToolErrors = new StringBuilder();
|
||||
|
@ -523,7 +510,8 @@ public class BaseCSharpTest implements RuntimeTestSupport /*, SpecialRuntimeTest
|
|||
args = new String[] {
|
||||
dotnetcli,
|
||||
"restore",
|
||||
"Antlr4.Test.dotnet.csproj"
|
||||
"Antlr4.Test.dotnet.csproj",
|
||||
"--no-dependencies"
|
||||
};
|
||||
success = runProcess(args, tmpdir);
|
||||
|
||||
|
@ -533,7 +521,8 @@ public class BaseCSharpTest implements RuntimeTestSupport /*, SpecialRuntimeTest
|
|||
"build",
|
||||
"Antlr4.Test.dotnet.csproj",
|
||||
"-c",
|
||||
"Release"
|
||||
"Release",
|
||||
"--no-dependencies"
|
||||
};
|
||||
success = runProcess(args, tmpdir);
|
||||
}
|
||||
|
@ -797,25 +786,30 @@ public class BaseCSharpTest implements RuntimeTestSupport /*, SpecialRuntimeTest
|
|||
}
|
||||
}
|
||||
|
||||
protected void eraseFiles() {
|
||||
if (tmpdir == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
File tmpdirF = new File(tmpdir);
|
||||
String[] files = tmpdirF.list();
|
||||
if(files!=null) for(String file : files) {
|
||||
new File(tmpdir+"/"+file).delete();
|
||||
}
|
||||
protected void eraseDirectory(File dir) {
|
||||
File[] files = dir.listFiles();
|
||||
if (files != null) {
|
||||
for (File file : files) {
|
||||
if (file.isDirectory()) {
|
||||
eraseDirectory(file);
|
||||
}
|
||||
else {
|
||||
file.delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
dir.delete();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void eraseTempDir() {
|
||||
File tmpdirF = new File(tmpdir);
|
||||
if ( tmpdirF.exists() ) {
|
||||
eraseFiles();
|
||||
tmpdirF.delete();
|
||||
}
|
||||
if (!PRESERVE_TEST_DIR) {
|
||||
File tmpdirF = new File(tmpdir);
|
||||
if ( tmpdirF.exists() ) {
|
||||
eraseDirectory(tmpdirF);
|
||||
tmpdirF.delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public String getFirstLineOfException() {
|
||||
|
|
|
@ -674,6 +674,7 @@ public class BaseGoTest implements RuntimeTestSupport {
|
|||
"import (\n"
|
||||
+" \"github.com/antlr/antlr4/runtime/Go/antlr\"\n"
|
||||
+" \"./parser\"\n"
|
||||
+" \"fmt\"\n"
|
||||
+" \"os\"\n"
|
||||
+")\n"
|
||||
+ "\n"
|
||||
|
@ -696,7 +697,11 @@ public class BaseGoTest implements RuntimeTestSupport {
|
|||
+ "}\n"
|
||||
+ "\n"
|
||||
+ "func main() {\n"
|
||||
+ " input := antlr.NewFileStream(os.Args[1])\n"
|
||||
+ " input, err := antlr.NewFileStream(os.Args[1])\n"
|
||||
+ " if err != nil {\n"
|
||||
+ " fmt.Printf(\"Failed to find file: %v\", err)\n"
|
||||
+ " return\n"
|
||||
+ " }\n"
|
||||
+ " lexer := parser.New<lexerName>(input)\n"
|
||||
+ " stream := antlr.NewCommonTokenStream(lexer,0)\n"
|
||||
+ "<createParser>"
|
||||
|
@ -734,7 +739,11 @@ public class BaseGoTest implements RuntimeTestSupport {
|
|||
+ ")\n"
|
||||
+ "\n"
|
||||
+ "func main() {\n"
|
||||
+ " input := antlr.NewFileStream(os.Args[1])\n"
|
||||
+ " input, err := antlr.NewFileStream(os.Args[1])\n"
|
||||
+ " if err != nil {\n"
|
||||
+ " fmt.Printf(\"Failed to find file: %v\", err)\n"
|
||||
+ " return\n"
|
||||
+ " }\n"
|
||||
+ " lexer := parser.New<lexerName>(input)\n"
|
||||
+ " stream := antlr.NewCommonTokenStream(lexer,0)\n"
|
||||
+ " stream.Fill()\n"
|
||||
|
|
|
@ -1,16 +1,27 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<VersionPrefix>4.7</VersionPrefix>
|
||||
<Company>The ANTLR Organization</Company>
|
||||
<Version>4.7.1</Version>
|
||||
<TargetFramework>netstandard1.3</TargetFramework>
|
||||
<DefineConstants>$(DefineConstants);DOTNETCORE;NET35PLUS;NET40PLUS;NET45PLUS</DefineConstants>
|
||||
<NoWarn>$(NoWarn);CS1591;CS1574;CS1580</NoWarn>
|
||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||
<AssemblyName>Antlr4.Runtime.Standard</AssemblyName>
|
||||
<AssemblyName>Antlr4.Runtime.Core</AssemblyName>
|
||||
<AssemblyOriginatorKeyFile>../../Antlr4.snk</AssemblyOriginatorKeyFile>
|
||||
<SignAssembly>true</SignAssembly>
|
||||
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
|
||||
<PackageId>Antlr4.Runtime.Standard</PackageId>
|
||||
<PackageId>Antlr4.Runtime.Core</PackageId>
|
||||
<Title>ANTLR 4 .NET Core Runtime</Title>
|
||||
<Authors>Eric Vergnaud, Terence Parr, Sam Harwell</Authors>
|
||||
<Description>The .NET Core C# ANTLR 4 runtime from the ANTLR Organization</Description>
|
||||
<Copyright>Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.</Copyright>
|
||||
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
|
||||
<PackageLicenseUrl>https://github.com/antlr/antlr4/blob/master/LICENSE.txt</PackageLicenseUrl>
|
||||
<PackageProjectUrl>https://github.com/antlr/antlr4</PackageProjectUrl>
|
||||
<PackageIconUrl>https://raw.github.com/antlr/website-antlr4/master/images/icons/antlr.png</PackageIconUrl>
|
||||
<PackageReleaseNotes>https://github.com/antlr/antlr4/releases</PackageReleaseNotes>
|
||||
<PackageTags>antlr parsing grammar</PackageTags>
|
||||
<NetStandardImplicitPackageVersion>1.6.1</NetStandardImplicitPackageVersion>
|
||||
<GenerateAssemblyTitleAttribute>false</GenerateAssemblyTitleAttribute>
|
||||
<GenerateAssemblyDescriptionAttribute>false</GenerateAssemblyDescriptionAttribute>
|
||||
|
@ -22,5 +33,15 @@
|
|||
<GenerateAssemblyFileVersionAttribute>false</GenerateAssemblyFileVersionAttribute>
|
||||
<GenerateAssemblyInformationalVersionAttribute>false</GenerateAssemblyInformationalVersionAttribute>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>lib\Debug</OutputPath>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>lib\Release</OutputPath>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
|
||||
<metadata>
|
||||
<id>Antlr4.Runtime.Standard</id>
|
||||
<version>4.7</version>
|
||||
<version>4.7.1</version>
|
||||
<language>en-us</language>
|
||||
<title>ANTLR 4 Standard Runtime</title>
|
||||
<description>The standard C# ANTLR 4 runtime from the ANTLR Organization</description>
|
||||
|
@ -20,5 +20,6 @@
|
|||
</metadata>
|
||||
<files>
|
||||
<file src="Antlr4.Runtime/lib/Release/Antlr4.Runtime.Standard.dll" target="lib/net35/"/>
|
||||
<file src="Antlr4.Runtime/lib/Release/netstandard1.3/Antlr4.Runtime.Core.dll" target="lib/netstandard/"/>
|
||||
</files>
|
||||
</package>
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
#!/bin/sh
|
||||
|
||||
# Build a .NET 3.5 compatible DLL using mono
|
||||
# This step can be done by the `dotnet` cli once https://github.com/Microsoft/msbuild/issues/1333 is resolved.
|
||||
echo "Step 1: Building .NET 3.5 DLL"
|
||||
xbuild /p:Configuration=Release Antlr4.mono.sln
|
||||
|
||||
# Build a .NET core DLL using the `dotnet` cli from microsoft
|
||||
echo "Step 2: Building .NET Core DLL"
|
||||
dotnet restore Antlr4.dotnet.sln
|
||||
dotnet build -c Release Antlr4.dotnet.sln
|
||||
|
||||
echo "Step 3: Packaging both DLLs into a single nuget package"
|
||||
nuget pack Package.nuspec
|
|
@ -35,9 +35,9 @@ void ANTLRInputStream::load(const std::string &input) {
|
|||
// Remove the UTF-8 BOM if present.
|
||||
const char bom[4] = "\xef\xbb\xbf";
|
||||
if (input.compare(0, 3, bom, 3) == 0)
|
||||
_data = antlrcpp::utfConverter.from_bytes(input.data() + 3, input.data() + input.size());
|
||||
_data = antlrcpp::utf8_to_utf32(input.data() + 3, input.data() + input.size());
|
||||
else
|
||||
_data = antlrcpp::utfConverter.from_bytes(input);
|
||||
_data = antlrcpp::utf8_to_utf32(input.data(), input.data() + input.size());
|
||||
p = 0;
|
||||
}
|
||||
|
||||
|
@ -136,7 +136,7 @@ std::string ANTLRInputStream::getText(const Interval &interval) {
|
|||
return "";
|
||||
}
|
||||
|
||||
return antlrcpp::utfConverter.to_bytes(_data.substr(start, count));
|
||||
return antlrcpp::utf32_to_utf8(_data.substr(start, count));
|
||||
}
|
||||
|
||||
std::string ANTLRInputStream::getSourceName() const {
|
||||
|
@ -147,7 +147,7 @@ std::string ANTLRInputStream::getSourceName() const {
|
|||
}
|
||||
|
||||
std::string ANTLRInputStream::toString() const {
|
||||
return antlrcpp::utfConverter.to_bytes(_data);
|
||||
return antlrcpp::utf32_to_utf8(_data);
|
||||
}
|
||||
|
||||
void ANTLRInputStream::InitializeInstanceFields() {
|
||||
|
|
|
@ -9,12 +9,11 @@
|
|||
|
||||
using namespace antlr4;
|
||||
|
||||
CommonTokenStream::CommonTokenStream(TokenSource *tokenSource) : BufferedTokenStream(tokenSource) {
|
||||
InitializeInstanceFields();
|
||||
CommonTokenStream::CommonTokenStream(TokenSource *tokenSource) : CommonTokenStream(tokenSource, Token::DEFAULT_CHANNEL) {
|
||||
}
|
||||
|
||||
CommonTokenStream::CommonTokenStream(TokenSource *tokenSource, int channel) : BufferedTokenStream(tokenSource) {
|
||||
this->channel = channel;
|
||||
CommonTokenStream::CommonTokenStream(TokenSource *tokenSource, size_t channel)
|
||||
: BufferedTokenStream(tokenSource), channel(channel) {
|
||||
}
|
||||
|
||||
ssize_t CommonTokenStream::adjustSeekIndex(size_t i) {
|
||||
|
@ -77,7 +76,3 @@ int CommonTokenStream::getNumberOfOnChannelTokens() {
|
|||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
void CommonTokenStream::InitializeInstanceFields() {
|
||||
channel = Token::DEFAULT_CHANNEL;
|
||||
}
|
||||
|
|
|
@ -34,16 +34,6 @@ namespace antlr4 {
|
|||
* channel.</p>
|
||||
*/
|
||||
class ANTLR4CPP_PUBLIC CommonTokenStream : public BufferedTokenStream {
|
||||
protected:
|
||||
/**
|
||||
* Specifies the channel to use for filtering tokens.
|
||||
*
|
||||
* <p>
|
||||
* The default value is {@link Token#DEFAULT_CHANNEL}, which matches the
|
||||
* default channel assigned to tokens created by the lexer.</p>
|
||||
*/
|
||||
size_t channel;
|
||||
|
||||
public:
|
||||
/**
|
||||
* Constructs a new {@link CommonTokenStream} using the specified token
|
||||
|
@ -63,21 +53,27 @@ namespace antlr4 {
|
|||
* @param tokenSource The token source.
|
||||
* @param channel The channel to use for filtering tokens.
|
||||
*/
|
||||
CommonTokenStream(TokenSource *tokenSource, int channel);
|
||||
CommonTokenStream(TokenSource *tokenSource, size_t channel);
|
||||
|
||||
protected:
|
||||
virtual ssize_t adjustSeekIndex(size_t i) override;
|
||||
|
||||
virtual Token* LB(size_t k) override;
|
||||
|
||||
public:
|
||||
virtual Token* LT(ssize_t k) override;
|
||||
|
||||
/// Count EOF just once.
|
||||
virtual int getNumberOfOnChannelTokens();
|
||||
|
||||
protected:
|
||||
/**
|
||||
* Specifies the channel to use for filtering tokens.
|
||||
*
|
||||
* <p>
|
||||
* The default value is {@link Token#DEFAULT_CHANNEL}, which matches the
|
||||
* default channel assigned to tokens created by the lexer.</p>
|
||||
*/
|
||||
size_t channel;
|
||||
|
||||
virtual ssize_t adjustSeekIndex(size_t i) override;
|
||||
|
||||
virtual Token* LB(size_t k) override;
|
||||
|
||||
private:
|
||||
void InitializeInstanceFields();
|
||||
};
|
||||
|
||||
} // namespace antlr4
|
||||
|
|
|
@ -195,7 +195,7 @@ std::string UnbufferedCharStream::getText(const misc::Interval &interval) {
|
|||
}
|
||||
// convert from absolute to local index
|
||||
size_t i = interval.a - bufferStartIndex;
|
||||
return utfConverter.to_bytes(_data.substr(i, interval.length()));
|
||||
return utf32_to_utf8(_data.substr(i, interval.length()));
|
||||
}
|
||||
|
||||
size_t UnbufferedCharStream::getBufferStartIndex() const {
|
||||
|
|
|
@ -9,13 +9,36 @@
|
|||
|
||||
namespace antlrcpp {
|
||||
// For all conversions utf8 <-> utf32.
|
||||
// VS 2015 has a bug in std::codecvt_utf8<char32_t> (VS 2013 works fine).
|
||||
#if defined(_MSC_VER) && _MSC_VER == 1900
|
||||
// VS 2015 and VS 2017 have different bugs in std::codecvt_utf8<char32_t> (VS 2013 works fine).
|
||||
#if defined(_MSC_VER) && _MSC_VER >= 1900 && _MSC_VER < 2000
|
||||
static std::wstring_convert<std::codecvt_utf8<__int32>, __int32> utfConverter;
|
||||
#else
|
||||
static std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> utfConverter;
|
||||
#endif
|
||||
|
||||
//the conversion functions fails in VS2017, so we explicitly use a workaround
|
||||
template<typename T>
|
||||
inline std::string utf32_to_utf8(T _data)
|
||||
{
|
||||
#if _MSC_VER > 1900 && _MSC_VER < 2000
|
||||
auto p = reinterpret_cast<const int32_t *>(_data.data());
|
||||
return antlrcpp::utfConverter.to_bytes(p, p + _data.size());
|
||||
#else
|
||||
return antlrcpp::utfConverter.to_bytes(_data);
|
||||
#endif
|
||||
}
|
||||
|
||||
inline auto utf8_to_utf32(const char* first, const char* last)
|
||||
{
|
||||
#if _MSC_VER > 1900 && _MSC_VER < 2000
|
||||
auto r = antlrcpp::utfConverter.from_bytes(first, last);
|
||||
std::u32string s = reinterpret_cast<const char32_t *>(r.data());
|
||||
return s;
|
||||
#else
|
||||
return antlrcpp::utfConverter.from_bytes(first, last);
|
||||
#endif
|
||||
}
|
||||
|
||||
void replaceAll(std::string& str, const std::string& from, const std::string& to);
|
||||
|
||||
// string <-> wstring conversion (UTF-16), e.g. for use with Window's wide APIs.
|
||||
|
|
|
@ -14,7 +14,8 @@
|
|||
using namespace antlr4::tree;
|
||||
using namespace antlrcpp;
|
||||
|
||||
ParseTreeWalker ParseTreeWalker::DEFAULT = IterativeParseTreeWalker();
|
||||
static IterativeParseTreeWalker defaultWalker;
|
||||
ParseTreeWalker &ParseTreeWalker::DEFAULT = defaultWalker;
|
||||
|
||||
void ParseTreeWalker::walk(ParseTreeListener *listener, ParseTree *t) const {
|
||||
if (is<ErrorNode *>(t)) {
|
||||
|
|
|
@ -12,7 +12,7 @@ namespace tree {
|
|||
|
||||
class ANTLR4CPP_PUBLIC ParseTreeWalker {
|
||||
public:
|
||||
static ParseTreeWalker DEFAULT;
|
||||
static ParseTreeWalker &DEFAULT;
|
||||
|
||||
virtual ~ParseTreeWalker() {};
|
||||
|
||||
|
|
|
@ -28,9 +28,9 @@ func NewFileStream(fileName string) (*FileStream, error) {
|
|||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
err = io.Copy(buf, f)
|
||||
_, err = io.Copy(buf, f)
|
||||
if err != nil {
|
||||
return nil, er
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fs := new(FileStream)
|
||||
|
|
|
@ -18,7 +18,7 @@ var fs = isNodeJs ? require("fs") : null;
|
|||
var CharStreams = {
|
||||
// Creates an InputStream from a string.
|
||||
fromString: function(str) {
|
||||
return InputStream(str, true);
|
||||
return new InputStream(str, true);
|
||||
},
|
||||
|
||||
// Asynchronously creates an InputStream from a blob given the
|
||||
|
@ -30,7 +30,7 @@ var CharStreams = {
|
|||
fromBlob: function(blob, encoding, onLoad, onError) {
|
||||
var reader = FileReader();
|
||||
reader.onload = function(e) {
|
||||
var is = InputStream(e.target.result, true);
|
||||
var is = new InputStream(e.target.result, true);
|
||||
onLoad(is);
|
||||
};
|
||||
reader.onerror = onError;
|
||||
|
@ -41,7 +41,7 @@ var CharStreams = {
|
|||
// encoding of the bytes in that buffer (defaults to 'utf8' if
|
||||
// encoding is null).
|
||||
fromBuffer: function(buffer, encoding) {
|
||||
return InputStream(buffer.toString(encoding), true);
|
||||
return new InputStream(buffer.toString(encoding), true);
|
||||
},
|
||||
|
||||
// Asynchronously creates an InputStream from a file on disk given
|
||||
|
@ -53,7 +53,7 @@ var CharStreams = {
|
|||
fs.readFile(path, encoding, function(err, data) {
|
||||
var is = null;
|
||||
if (data !== null) {
|
||||
is = InputStream(data, true);
|
||||
is = new InputStream(data, true);
|
||||
}
|
||||
callback(err, is);
|
||||
});
|
||||
|
@ -64,7 +64,7 @@ var CharStreams = {
|
|||
// 'utf8' if encoding is null).
|
||||
fromPathSync: function(path, encoding) {
|
||||
var data = fs.readFileSync(path, encoding);
|
||||
return InputStream(data, true);
|
||||
return new InputStream(data, true);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -1,7 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Workspace
|
||||
version = "1.0">
|
||||
<FileRef
|
||||
location = "self:">
|
||||
</FileRef>
|
||||
</Workspace>
|
|
@ -1,30 +0,0 @@
|
|||
{
|
||||
"DVTSourceControlWorkspaceBlueprintPrimaryRemoteRepositoryKey" : "F87DC9FBC9E282B88EC1892E49DCE765631D3152",
|
||||
"DVTSourceControlWorkspaceBlueprintWorkingCopyRepositoryLocationsKey" : {
|
||||
|
||||
},
|
||||
"DVTSourceControlWorkspaceBlueprintWorkingCopyStatesKey" : {
|
||||
"481D6A1A63AAA84E22CA41443AC34F2761E57E20" : 9223372036854775807,
|
||||
"F87DC9FBC9E282B88EC1892E49DCE765631D3152" : 9223372036854775807
|
||||
},
|
||||
"DVTSourceControlWorkspaceBlueprintIdentifierKey" : "194AE0A2-18FF-4519-BB3D-2C7F6F175940",
|
||||
"DVTSourceControlWorkspaceBlueprintWorkingCopyPathsKey" : {
|
||||
"481D6A1A63AAA84E22CA41443AC34F2761E57E20" : "..",
|
||||
"F87DC9FBC9E282B88EC1892E49DCE765631D3152" : "Swift\/"
|
||||
},
|
||||
"DVTSourceControlWorkspaceBlueprintNameKey" : "Antlr4",
|
||||
"DVTSourceControlWorkspaceBlueprintVersion" : 204,
|
||||
"DVTSourceControlWorkspaceBlueprintRelativePathToProjectKey" : "Antlr4.xcodeproj",
|
||||
"DVTSourceControlWorkspaceBlueprintRemoteRepositoriesKey" : [
|
||||
{
|
||||
"DVTSourceControlWorkspaceBlueprintRemoteRepositoryURLKey" : "https:\/\/github.com\/hanjoes\/antlr4.git",
|
||||
"DVTSourceControlWorkspaceBlueprintRemoteRepositorySystemKey" : "com.apple.dt.Xcode.sourcecontrol.Git",
|
||||
"DVTSourceControlWorkspaceBlueprintRemoteRepositoryIdentifierKey" : "481D6A1A63AAA84E22CA41443AC34F2761E57E20"
|
||||
},
|
||||
{
|
||||
"DVTSourceControlWorkspaceBlueprintRemoteRepositoryURLKey" : "https:\/\/github.com\/hanjoes\/antlr4-swift-runtime.git",
|
||||
"DVTSourceControlWorkspaceBlueprintRemoteRepositorySystemKey" : "com.apple.dt.Xcode.sourcecontrol.Git",
|
||||
"DVTSourceControlWorkspaceBlueprintRemoteRepositoryIdentifierKey" : "F87DC9FBC9E282B88EC1892E49DCE765631D3152"
|
||||
}
|
||||
]
|
||||
}
|
|
@ -1,80 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Scheme
|
||||
LastUpgradeVersion = "0820"
|
||||
version = "1.3">
|
||||
<BuildAction
|
||||
parallelizeBuildables = "YES"
|
||||
buildImplicitDependencies = "YES">
|
||||
<BuildActionEntries>
|
||||
<BuildActionEntry
|
||||
buildForTesting = "YES"
|
||||
buildForRunning = "YES"
|
||||
buildForProfiling = "YES"
|
||||
buildForArchiving = "YES"
|
||||
buildForAnalyzing = "YES">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "F9A422F71BCCD47300A9CD35"
|
||||
BuildableName = "Antlr4.framework"
|
||||
BlueprintName = "Antlr4 IOS"
|
||||
ReferencedContainer = "container:Antlr4.xcodeproj">
|
||||
</BuildableReference>
|
||||
</BuildActionEntry>
|
||||
</BuildActionEntries>
|
||||
</BuildAction>
|
||||
<TestAction
|
||||
buildConfiguration = "Debug"
|
||||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
|
||||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
|
||||
shouldUseLaunchSchemeArgsEnv = "YES">
|
||||
<Testables>
|
||||
</Testables>
|
||||
<AdditionalOptions>
|
||||
</AdditionalOptions>
|
||||
</TestAction>
|
||||
<LaunchAction
|
||||
buildConfiguration = "Debug"
|
||||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
|
||||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
|
||||
launchStyle = "0"
|
||||
useCustomWorkingDirectory = "NO"
|
||||
ignoresPersistentStateOnLaunch = "NO"
|
||||
debugDocumentVersioning = "YES"
|
||||
debugServiceExtension = "internal"
|
||||
allowLocationSimulation = "YES">
|
||||
<MacroExpansion>
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "F9A422F71BCCD47300A9CD35"
|
||||
BuildableName = "Antlr4.framework"
|
||||
BlueprintName = "Antlr4 IOS"
|
||||
ReferencedContainer = "container:Antlr4.xcodeproj">
|
||||
</BuildableReference>
|
||||
</MacroExpansion>
|
||||
<AdditionalOptions>
|
||||
</AdditionalOptions>
|
||||
</LaunchAction>
|
||||
<ProfileAction
|
||||
buildConfiguration = "Release"
|
||||
shouldUseLaunchSchemeArgsEnv = "YES"
|
||||
savedToolIdentifier = ""
|
||||
useCustomWorkingDirectory = "NO"
|
||||
debugDocumentVersioning = "YES">
|
||||
<MacroExpansion>
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "F9A422F71BCCD47300A9CD35"
|
||||
BuildableName = "Antlr4.framework"
|
||||
BlueprintName = "Antlr4 IOS"
|
||||
ReferencedContainer = "container:Antlr4.xcodeproj">
|
||||
</BuildableReference>
|
||||
</MacroExpansion>
|
||||
</ProfileAction>
|
||||
<AnalyzeAction
|
||||
buildConfiguration = "Debug">
|
||||
</AnalyzeAction>
|
||||
<ArchiveAction
|
||||
buildConfiguration = "Release"
|
||||
revealArchiveInOrganizer = "YES">
|
||||
</ArchiveAction>
|
||||
</Scheme>
|
|
@ -1,103 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Scheme
|
||||
LastUpgradeVersion = "0820"
|
||||
version = "1.7">
|
||||
<BuildAction
|
||||
parallelizeBuildables = "YES"
|
||||
buildImplicitDependencies = "YES">
|
||||
<BuildActionEntries>
|
||||
<BuildActionEntry
|
||||
buildForTesting = "YES"
|
||||
buildForRunning = "YES"
|
||||
buildForProfiling = "YES"
|
||||
buildForArchiving = "YES"
|
||||
buildForAnalyzing = "YES">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "F9A4211B1BCCC35000A9CD35"
|
||||
BuildableName = "Antlr4.framework"
|
||||
BlueprintName = "Antlr4 OSX"
|
||||
ReferencedContainer = "container:Antlr4.xcodeproj">
|
||||
</BuildableReference>
|
||||
</BuildActionEntry>
|
||||
</BuildActionEntries>
|
||||
</BuildAction>
|
||||
<TestAction
|
||||
buildConfiguration = "Debug"
|
||||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
|
||||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
|
||||
shouldUseLaunchSchemeArgsEnv = "YES">
|
||||
<Testables>
|
||||
<TestableReference
|
||||
skipped = "NO">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "F9A424A71BCD348C00A9CD35"
|
||||
BuildableName = "Antlr4Tests.xctest"
|
||||
BlueprintName = "Antlr4Tests"
|
||||
ReferencedContainer = "container:Antlr4.xcodeproj">
|
||||
</BuildableReference>
|
||||
<LocationScenarioReference
|
||||
identifier = "com.apple.dt.IDEFoundation.CurrentLocationScenarioIdentifier"
|
||||
referenceType = "1">
|
||||
</LocationScenarioReference>
|
||||
</TestableReference>
|
||||
</Testables>
|
||||
<MacroExpansion>
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "F9A4211B1BCCC35000A9CD35"
|
||||
BuildableName = "Antlr4.framework"
|
||||
BlueprintName = "Antlr4 OSX"
|
||||
ReferencedContainer = "container:Antlr4.xcodeproj">
|
||||
</BuildableReference>
|
||||
</MacroExpansion>
|
||||
<AdditionalOptions>
|
||||
</AdditionalOptions>
|
||||
</TestAction>
|
||||
<LaunchAction
|
||||
buildConfiguration = "Debug"
|
||||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
|
||||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
|
||||
launchStyle = "0"
|
||||
useCustomWorkingDirectory = "NO"
|
||||
ignoresPersistentStateOnLaunch = "NO"
|
||||
debugDocumentVersioning = "YES"
|
||||
debugServiceExtension = "internal"
|
||||
allowLocationSimulation = "YES">
|
||||
<MacroExpansion>
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "F9A4211B1BCCC35000A9CD35"
|
||||
BuildableName = "Antlr4.framework"
|
||||
BlueprintName = "Antlr4 OSX"
|
||||
ReferencedContainer = "container:Antlr4.xcodeproj">
|
||||
</BuildableReference>
|
||||
</MacroExpansion>
|
||||
<AdditionalOptions>
|
||||
</AdditionalOptions>
|
||||
</LaunchAction>
|
||||
<ProfileAction
|
||||
buildConfiguration = "Release"
|
||||
shouldUseLaunchSchemeArgsEnv = "YES"
|
||||
savedToolIdentifier = ""
|
||||
useCustomWorkingDirectory = "NO"
|
||||
debugDocumentVersioning = "YES">
|
||||
<MacroExpansion>
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "F9A4211B1BCCC35000A9CD35"
|
||||
BuildableName = "Antlr4.framework"
|
||||
BlueprintName = "Antlr4 OSX"
|
||||
ReferencedContainer = "container:Antlr4.xcodeproj">
|
||||
</BuildableReference>
|
||||
</MacroExpansion>
|
||||
</ProfileAction>
|
||||
<AnalyzeAction
|
||||
buildConfiguration = "Debug">
|
||||
</AnalyzeAction>
|
||||
<ArchiveAction
|
||||
buildConfiguration = "Release"
|
||||
revealArchiveInOrganizer = "YES">
|
||||
</ArchiveAction>
|
||||
</Scheme>
|
|
@ -0,0 +1,192 @@
|
|||
#! /usr/bin/python
|
||||
|
||||
"""
|
||||
Find all .g4 files and generate parsers in the same directory.
|
||||
the antlr used should be the one located at user's mvn directory
|
||||
the filename is antlr4-ANTLR_VERSION-SNAPSHOT.jar. You can get it
|
||||
by running: "mvn install"
|
||||
|
||||
NOTE: In case of customized location of .m2 folder, you can change the
|
||||
USER_M2 constant below.
|
||||
|
||||
the java version is used according to environment variable $JAVA_HOME.
|
||||
"""
|
||||
import glob
|
||||
import shutil
|
||||
import argparse
|
||||
import fnmatch
|
||||
import os.path
|
||||
import time
|
||||
from subprocess import call
|
||||
|
||||
|
||||
# ANTLR Version, here we only care about major version.
|
||||
MAJOR_VERSION = "4"
|
||||
|
||||
# Note: User defines their own M2_HOME should change this variable.
|
||||
USER_M2 = os.path.expanduser("~") + "/.m2"
|
||||
TMP_FOLDER = "/tmp/"
|
||||
|
||||
# The directory contains this script.
|
||||
DIR = os.path.dirname(os.path.realpath(__file__))
|
||||
|
||||
# The following two are using glob syntax.
|
||||
ANTLR4_FOLDER = USER_M2 + "/repository/org/antlr/antlr4/*-SNAPSHOT"
|
||||
ANTLR4 = ANTLR4_FOLDER + "/antlr4-*-SNAPSHOT-complete.jar"
|
||||
|
||||
# Colors
|
||||
RED = "\033[91;1m"
|
||||
GREEN = "\033[32;1m"
|
||||
YELLOW = "\033[93;1m"
|
||||
CYAN = "\033[36;1m"
|
||||
GREY = "\033[38;2;127;127;127m"
|
||||
RESET = "\033[0m"
|
||||
|
||||
|
||||
def find_a4_jar():
|
||||
"""
|
||||
Finds the antlr4 jar.
|
||||
"""
|
||||
matches = glob.glob(ANTLR4)
|
||||
if len(matches) == 0:
|
||||
return None
|
||||
sorted(matches, reverse=True)
|
||||
return matches[0]
|
||||
|
||||
|
||||
def find_g4():
|
||||
"""
|
||||
Find all g4 files and return a list of them.
|
||||
The recursive search starts from the directory containing
|
||||
this python file.
|
||||
"""
|
||||
file_path = os.path.realpath(__file__)
|
||||
parent_folder = file_path[0:file_path.rindex("/") + 1]
|
||||
res = []
|
||||
for cur, _, filenames in os.walk(parent_folder):
|
||||
cur_files = fnmatch.filter(filenames, "*.g4")
|
||||
res += [cur + "/" + cur_file for cur_file in cur_files]
|
||||
return res
|
||||
|
||||
|
||||
def gen_parser(grammar, a4):
|
||||
"""
|
||||
Generate parser for the input g4 file.
|
||||
:param grammar: grammar file
|
||||
:param a4: antlr4 runtime
|
||||
:return: None
|
||||
"""
|
||||
grammar_folder = grammar[0:grammar.rindex("/") + 1]
|
||||
java_home = os.environ["JAVA_HOME"]
|
||||
java = java_home + "/bin/java"
|
||||
if not os.path.exists(java):
|
||||
antlr_complains("Cannot find java. Check your JAVA_HOME setting.")
|
||||
return
|
||||
|
||||
call([java, "-jar", a4,
|
||||
"-Dlanguage=Swift", grammar, "-visitor",
|
||||
"-o", grammar_folder + "/gen"])
|
||||
|
||||
|
||||
def swift_test():
|
||||
"""
|
||||
Run unit tests.
|
||||
"""
|
||||
generate_parser()
|
||||
call(["swift", "test"])
|
||||
|
||||
|
||||
def get_argument_parser():
|
||||
"""
|
||||
Initialize argument parser.
|
||||
:return: the argument parser
|
||||
"""
|
||||
p = argparse.ArgumentParser(description="Helper script for ANTLR4 Swift target. "
|
||||
"<DEVELOPER> flag means the command is mostly used by a developer. "
|
||||
"<USER> flag means the command should be used by user. ")
|
||||
p.add_argument("--gen-spm-module",
|
||||
action="store_true",
|
||||
help="<USER> Generates a Swift Package Manager flavored module. "
|
||||
"Use this command if you want to include ANTLR4 as SPM dependency.", )
|
||||
p.add_argument("--gen-xcodeproj",
|
||||
action="store_true",
|
||||
help="<DEVELOPER> Generates an Xcode project for ANTLR4 Swift runtime. "
|
||||
"This directive will generate all the required parsers for the project. "
|
||||
"Feel free to re-run whenever you updated the test grammar files.")
|
||||
p.add_argument("--test",
|
||||
action="store_true",
|
||||
help="<DEVELOPER> Run unit tests.")
|
||||
return p
|
||||
|
||||
|
||||
def generate_spm_module(in_folder=TMP_FOLDER):
|
||||
"""
|
||||
Generate spm module in the specified folder, default
|
||||
to the system's tmp folder.
|
||||
|
||||
After generation, user can simply use the prompt SPM
|
||||
code to include the ANTLR4 Swift runtime package.
|
||||
:param in_folder: the folder where we generate the SPM module.
|
||||
:return: None
|
||||
"""
|
||||
|
||||
tmp_antlr_folder = in_folder + "Antlr4-tmp-" + str(int(time.time()))
|
||||
os.mkdir(tmp_antlr_folder)
|
||||
|
||||
# Copy folders and SPM manifest file.
|
||||
dirs_to_copy = ["Sources", "Tests"]
|
||||
for dir_to_copy in dirs_to_copy:
|
||||
shutil.copytree(DIR + "/" + dir_to_copy, tmp_antlr_folder + "/" + dir_to_copy)
|
||||
|
||||
shutil.copy("Package.swift", tmp_antlr_folder)
|
||||
|
||||
os.chdir(tmp_antlr_folder)
|
||||
call(["git", "init"])
|
||||
call(["git", "add", "*"])
|
||||
call(["git", "commit", "-m", "Initial commit."])
|
||||
call(["git", "tag", "{}.0.0".format(MAJOR_VERSION)])
|
||||
|
||||
antlr_says("Created local repository.")
|
||||
antlr_says("Put .Package(url: \"{}\", majorVersion: {}) in Package.swift.".format(os.getcwd(), MAJOR_VERSION))
|
||||
|
||||
|
||||
def generate_xcodeproj():
|
||||
"""
|
||||
Generates the ANTLR4 Swift runtime Xcode project.
|
||||
|
||||
This method will also generate parsers required by
|
||||
the runtime tests.
|
||||
:return:
|
||||
"""
|
||||
generate_parser()
|
||||
call(["swift", "package", "generate-xcodeproj"])
|
||||
|
||||
|
||||
def generate_parser():
|
||||
antlr = find_a4_jar()
|
||||
if antlr is None:
|
||||
antlr_complains("Run \"mvn install\" in antlr4 project root first or check mvn settings")
|
||||
exit()
|
||||
|
||||
_ = [gen_parser(f, antlr) for f in find_g4()]
|
||||
|
||||
|
||||
def antlr_says(msg):
|
||||
print GREEN + "[ANTLR] " + msg + RESET
|
||||
|
||||
|
||||
def antlr_complains(msg):
|
||||
print RED + "[ANTLR] " + msg + RESET
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
parser = get_argument_parser()
|
||||
args = parser.parse_args()
|
||||
if args.gen_spm_module:
|
||||
generate_spm_module()
|
||||
elif args.gen_xcodeproj:
|
||||
generate_xcodeproj()
|
||||
elif args.test:
|
||||
swift_test()
|
||||
else:
|
||||
parser.print_help()
|
|
@ -1,77 +0,0 @@
|
|||
#! /usr/bin/python
|
||||
|
||||
"""
|
||||
Find all .g4 files and generate parsers in the same directory.
|
||||
the antlr used should be the one located at user's mvn directory
|
||||
the filename is antlr4-ANTLR_VERSION-SNAPSHOT.jar. You can get it
|
||||
by running: "mvn install"
|
||||
|
||||
NOTE: In case of customized location of .m2 folder, you can change the
|
||||
USER_M2 constant below.
|
||||
|
||||
the java version is used according to environment variable $JAVA_HOME.
|
||||
"""
|
||||
|
||||
import fnmatch
|
||||
import os.path
|
||||
from subprocess import call
|
||||
|
||||
ANTLR_VERSION = '4.7'
|
||||
USER_M2 = os.path.expanduser('~') + '/.m2/'
|
||||
ANTLR4_FOLDER = USER_M2 + 'repository/org/antlr/antlr4/' + ANTLR_VERSION + '-SNAPSHOT/'
|
||||
ANTLR4_JAR = ANTLR4_FOLDER + 'antlr4-' + ANTLR_VERSION + '-SNAPSHOT-complete.jar'
|
||||
|
||||
|
||||
def jar_exists():
|
||||
"""
|
||||
Finds the antlr4 jar.
|
||||
"""
|
||||
return os.path.exists(ANTLR4_JAR)
|
||||
|
||||
|
||||
def find_g4():
|
||||
"""
|
||||
Find all g4 files and return a list of them.
|
||||
The recursive search starts from the directory containing
|
||||
this python file.
|
||||
"""
|
||||
file_path = os.path.realpath(__file__)
|
||||
parent_folder = file_path[0:file_path.rindex('/')+1]
|
||||
res = []
|
||||
for cur, _, filenames in os.walk(parent_folder):
|
||||
cur_files = fnmatch.filter(filenames, "*.g4")
|
||||
res += [cur+'/'+cur_file for cur_file in cur_files]
|
||||
return res
|
||||
|
||||
|
||||
def gen_parser(grammar):
|
||||
"""
|
||||
Generate parser for the input g4 file.
|
||||
"""
|
||||
grammar_folder = grammar[0:grammar.rindex('/')+1]
|
||||
java_home = os.environ['JAVA_HOME']
|
||||
java = java_home + '/bin/java'
|
||||
if not os.path.exists(java):
|
||||
print 'Cannot find java. Check your JAVA_HOME setting.'
|
||||
return
|
||||
|
||||
call([java, '-jar', ANTLR4_JAR,\
|
||||
'-Dlanguage=Swift', grammar, '-visitor',\
|
||||
'-o', grammar_folder + '/gen'])
|
||||
|
||||
|
||||
def swift_test():
|
||||
"""
|
||||
Run unit tests.
|
||||
"""
|
||||
call(['swift', 'test'])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if not jar_exists():
|
||||
print 'Run "mvn install" in antlr4 project root' + \
|
||||
'first or check mvn settings'
|
||||
exit()
|
||||
|
||||
_ = [gen_parser(f) for f in find_g4()]
|
||||
swift_test()
|
Loading…
Reference in New Issue