diff options
author | Haibo Huang <hhb@google.com> | 2018-08-28 13:51:54 -0700 |
---|---|---|
committer | Haibo Huang <hhb@google.com> | 2018-08-28 21:00:58 +0000 |
commit | b7f974f39180d46da516fb24eb2b1ca67d56089c (patch) | |
tree | 5f5626f8cde04eb9b6492d7dab0a6f2279d52e6d /runtime/CSharp3 | |
parent | bbed35ef4bfde5c3ed2fb5001998b747ce8020a4 (diff) | |
download | antlr-b7f974f39180d46da516fb24eb2b1ca67d56089c.tar.gz |
Update antlr to 3.5.2
1. Add post_update.sh to preserve build.gradle.
2. Add the patches/fix_infinite_recursion.diff to fix infinite
recursion.
3. Run tools/external_updater/updater.sh update antlr
(Should we upgrade to v4?)
Test: m javac-check RUN_ERROR_PRONE=true
Change-Id: Idafdb1cf6cf2310d983be612f36c5e6596a6e23f
Diffstat (limited to 'runtime/CSharp3')
44 files changed, 1008 insertions, 460 deletions
diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/Antlr3.Runtime.Debug.csproj b/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/Antlr3.Runtime.Debug.csproj index 5ca19d8..9c52017 100644 --- a/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/Antlr3.Runtime.Debug.csproj +++ b/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/Antlr3.Runtime.Debug.csproj @@ -12,15 +12,8 @@ <AssemblyName>Antlr3.Runtime.Debug</AssemblyName> <TargetFrameworkVersion>v2.0</TargetFrameworkVersion> <FileAlignment>512</FileAlignment> - <SccProjectName>Perforce Project</SccProjectName> - <SccLocalPath>..\..\..\..\..\..</SccLocalPath> - <SccAuxPath> - </SccAuxPath> - <SccProvider>MSSCCI:Perforce SCM</SccProvider> <SignAssembly>true</SignAssembly> - <AssemblyOriginatorKeyFile>..\..\..\..\..\..\..\keys\antlr\Key.snk</AssemblyOriginatorKeyFile> - <TargetFrameworkProfile> - </TargetFrameworkProfile> + <AssemblyOriginatorKeyFile>..\..\..\..\..\..\..\..\..\keys\antlr\Key.snk</AssemblyOriginatorKeyFile> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <DebugSymbols>true</DebugSymbols> @@ -30,6 +23,7 @@ <DefineConstants>DEBUG;TRACE</DefineConstants> <ErrorReport>prompt</ErrorReport> <WarningLevel>4</WarningLevel> + <DocumentationFile>bin\Debug\Antlr3.Runtime.Debug.xml</DocumentationFile> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> <DebugType>pdbonly</DebugType> @@ -38,6 +32,7 @@ <DefineConstants>TRACE</DefineConstants> <ErrorReport>prompt</ErrorReport> <WarningLevel>4</WarningLevel> + <DocumentationFile>bin\Release\Antlr3.Runtime.Debug.xml</DocumentationFile> </PropertyGroup> <ItemGroup> <Reference Include="System" /> @@ -56,7 +51,7 @@ <Compile Include="JavaExtensions\ExceptionExtensions.cs" /> <Compile Include="Misc\DoubleKeyMap`3.cs" /> <Compile Include="Misc\Stats.cs" /> - <None Include="..\..\..\..\..\..\..\keys\antlr\Key.snk"> + <None Include="..\..\..\..\..\..\..\..\..\keys\antlr\Key.snk"> <Link>Key.snk</Link> </None> <None Include="ParserDebugger.cs" /> diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/ParseTreeBuilder.cs b/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/ParseTreeBuilder.cs index deeaf3b..cb5f678 100644 --- a/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/ParseTreeBuilder.cs +++ b/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/ParseTreeBuilder.cs @@ -58,7 +58,8 @@ namespace Antlr.Runtime.Debug { get { - return callStack.Peek(); + ParseTree[] stack = callStack.ToArray(); + return stack[stack.Length - 1]; } } diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/Properties/AssemblyInfo.cs b/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/Properties/AssemblyInfo.cs index b8c988f..038cddb 100644 --- a/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/Properties/AssemblyInfo.cs +++ b/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/Properties/AssemblyInfo.cs @@ -40,9 +40,9 @@ using System.Runtime.InteropServices; [assembly: AssemblyTitle( "Antlr3.Runtime.Debug" )] [assembly: AssemblyDescription( "" )] [assembly: AssemblyConfiguration( "" )] -[assembly: AssemblyCompany( "Pixel Mine, Inc." )] +[assembly: AssemblyCompany( "Tunnel Vision Laboratories, LLC" )] [assembly: AssemblyProduct( "Antlr3.Runtime.Debug" )] -[assembly: AssemblyCopyright( "Copyright © Pixel Mine 2010" )] +[assembly: AssemblyCopyright( "Copyright © Sam Harwell 2013" )] [assembly: AssemblyTrademark( "" )] [assembly: AssemblyCulture( "" )] [assembly: CLSCompliant( true )] @@ -66,5 +66,5 @@ using System.Runtime.InteropServices; * the Major or Minor version is incremented. * 3. Revision is the Perforce changelist number associated with the release. */ -[assembly: AssemblyVersion("3.3.4.8517")] -[assembly: AssemblyFileVersion("3.3.4.8517")] +[assembly: AssemblyVersion("3.5.0.2")] +[assembly: AssemblyFileVersion("3.5.0.2")] diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime.JavaExtensions/Antlr3.Runtime.JavaExtensions.csproj b/runtime/CSharp3/Sources/Antlr3.Runtime.JavaExtensions/Antlr3.Runtime.JavaExtensions.csproj index 959a9f9..2c90504 100644 --- a/runtime/CSharp3/Sources/Antlr3.Runtime.JavaExtensions/Antlr3.Runtime.JavaExtensions.csproj +++ b/runtime/CSharp3/Sources/Antlr3.Runtime.JavaExtensions/Antlr3.Runtime.JavaExtensions.csproj @@ -12,10 +12,6 @@ <AssemblyName>Antlr3.Runtime.JavaExtensions</AssemblyName> <TargetFrameworkVersion>v3.5</TargetFrameworkVersion> <FileAlignment>512</FileAlignment> - <SccProjectName>SAK</SccProjectName> - <SccLocalPath>SAK</SccLocalPath> - <SccAuxPath>SAK</SccAuxPath> - <SccProvider>SAK</SccProvider> <TargetFrameworkProfile>Client</TargetFrameworkProfile> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> @@ -39,7 +35,7 @@ <SignAssembly>true</SignAssembly> </PropertyGroup> <PropertyGroup> - <AssemblyOriginatorKeyFile>..\..\..\..\..\..\..\keys\antlr\Key.snk</AssemblyOriginatorKeyFile> + <AssemblyOriginatorKeyFile>..\..\..\..\..\..\..\..\..\keys\antlr\Key.snk</AssemblyOriginatorKeyFile> </PropertyGroup> <ItemGroup> <Reference Include="System" /> @@ -80,7 +76,7 @@ </ProjectReference> </ItemGroup> <ItemGroup> - <None Include="..\..\..\..\..\..\..\keys\antlr\Key.snk"> + <None Include="..\..\..\..\..\..\..\..\..\keys\antlr\Key.snk"> <Link>Key.snk</Link> </None> </ItemGroup> diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime.JavaExtensions/Properties/AssemblyInfo.cs b/runtime/CSharp3/Sources/Antlr3.Runtime.JavaExtensions/Properties/AssemblyInfo.cs index 8aa2671..00e66a9 100644 --- a/runtime/CSharp3/Sources/Antlr3.Runtime.JavaExtensions/Properties/AssemblyInfo.cs +++ b/runtime/CSharp3/Sources/Antlr3.Runtime.JavaExtensions/Properties/AssemblyInfo.cs @@ -40,9 +40,9 @@ using System.Runtime.InteropServices; [assembly: AssemblyTitle( "Antlr3.Runtime.JavaExtensions" )] [assembly: AssemblyDescription( "" )] [assembly: AssemblyConfiguration( "" )] -[assembly: AssemblyCompany( "Pixel Mine, Inc." )] +[assembly: AssemblyCompany( "Tunnel Vision Laboratories, LLC" )] [assembly: AssemblyProduct( "Antlr3.Runtime.JavaExtensions" )] -[assembly: AssemblyCopyright( "Copyright © Pixel Mine 2010" )] +[assembly: AssemblyCopyright( "Copyright © Sam Harwell 2013" )] [assembly: AssemblyTrademark( "" )] [assembly: AssemblyCulture( "" )] [assembly: CLSCompliant( true )] @@ -64,7 +64,6 @@ using System.Runtime.InteropServices; * 2. Build is incremented each time the C# port is packaged for release (regardless * of whether it's an incremental or nightly). The value resets to zero whenever * the Major or Minor version is incremented. - * 3. Revision is the Perforce changelist number associated with the release. */ -[assembly: AssemblyVersion("3.3.4.8517")] -[assembly: AssemblyFileVersion("3.3.4.8517")] +[assembly: AssemblyVersion("3.5.0.2")] +[assembly: AssemblyFileVersion("3.5.0.2")] diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime.JavaExtensions/StringExtensions.cs b/runtime/CSharp3/Sources/Antlr3.Runtime.JavaExtensions/StringExtensions.cs index 8432512..837908a 100644 --- a/runtime/CSharp3/Sources/Antlr3.Runtime.JavaExtensions/StringExtensions.cs +++ b/runtime/CSharp3/Sources/Antlr3.Runtime.JavaExtensions/StringExtensions.cs @@ -30,6 +30,7 @@ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#if DEBUG namespace Antlr.Runtime.JavaExtensions { using ObsoleteAttribute = System.ObsoleteAttribute; @@ -38,7 +39,6 @@ namespace Antlr.Runtime.JavaExtensions public static class StringExtensions { -#if DEBUG [Obsolete] public static char charAt( this string str, int index ) { @@ -98,19 +98,19 @@ namespace Antlr.Runtime.JavaExtensions { return str.Replace(oldValue, newValue); } -#endif + [Obsolete] public static string replaceAll( this string str, string regex, string newValue ) { return Regex.Replace( str, regex, newValue ); } + [Obsolete] public static string replaceFirst( this string str, string regex, string replacement ) { return Regex.Replace( str, regex, replacement ); } -#if DEBUG [Obsolete] public static bool startsWith( this string str, string value ) { @@ -146,6 +146,6 @@ namespace Antlr.Runtime.JavaExtensions { return str.Trim(); } -#endif } } +#endif diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime.Test/Antlr3.Runtime.Test.csproj b/runtime/CSharp3/Sources/Antlr3.Runtime.Test/Antlr3.Runtime.Test.csproj index 11206dc..75c4cac 100644 --- a/runtime/CSharp3/Sources/Antlr3.Runtime.Test/Antlr3.Runtime.Test.csproj +++ b/runtime/CSharp3/Sources/Antlr3.Runtime.Test/Antlr3.Runtime.Test.csproj @@ -13,10 +13,6 @@ <TargetFrameworkVersion>v4.0</TargetFrameworkVersion> <FileAlignment>512</FileAlignment> <ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids> - <SccProjectName>SAK</SccProjectName> - <SccLocalPath>SAK</SccLocalPath> - <SccAuxPath>SAK</SccAuxPath> - <SccProvider>SAK</SccProvider> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <DebugSymbols>true</DebugSymbols> @@ -41,14 +37,10 @@ <Reference Include="System.Core"> <RequiredTargetFramework>3.5</RequiredTargetFramework> </Reference> - <Reference Include="vjslib" /> + <Reference Include="System.Numerics" /> </ItemGroup> <ItemGroup> - <Compile Include="Composition\Program.cs" /> - <Compile Include="Properties\AssemblyInfo.cs" /> - </ItemGroup> - <ItemGroup> - <ProjectReference Include="..\..\..\..\..\..\antlrcs\main\Antlr3.StringTemplate\Antlr3.StringTemplate.csproj"> + <ProjectReference Include="..\..\..\..\..\..\Antlr3.StringTemplate\Antlr3.StringTemplate.csproj"> <Project>{B5910BE2-DE21-4AA9-95C1-486F42B9E794}</Project> <Name>Antlr3.StringTemplate</Name> </ProjectReference> @@ -66,11 +58,37 @@ </ProjectReference> </ItemGroup> <ItemGroup> - <Antlr3 Include="SimpleExpression.g3"> - <Generator>MSBuild:Compile</Generator> + <Antlr3 Include="SimpleExpression.g3" /> + <Antlr3 Include="FastSimpleExpression.g3" /> + <None Include="JavaCompat\Expr.g3" /> + <Antlr3 Include="BuildOptions\DebugGrammar.g3"> + <!--<GrammarOptions>-debug</GrammarOptions>--> </Antlr3> + <Antlr3 Include="BuildOptions\DebugTreeGrammar.g3"> + <!--<GrammarOptions>-debug</GrammarOptions>--> + </Antlr3> + <Antlr3 Include="StringTemplateOutput.g3" /> + <Antlr3 Include="TestActionFeatures.g3" /> + <Antlr3 Include="SemanticPredicateReduction.g3" /> + <Antlr3 Include="Composition\Reduce.g3" /> + <Antlr3 Include="Composition\Simplify.g3" /> + <Antlr3 Include="Composition\VecMath.g3" /> + <AntlrAbstractGrammar Include="Composition\VecMath_Lexer.g3"> + <Generator>MSBuild:Compile</Generator> + </AntlrAbstractGrammar> + <AntlrAbstractGrammar Include="Composition\VecMath_Parser.g3"> + <Generator>MSBuild:Compile</Generator> + </AntlrAbstractGrammar> + <Antlr3 Include="PreprocessorLexer.g3" /> + <Antlr3 Include="SynpredTreeParser.g3" /> </ItemGroup> <ItemGroup> + <Compile Include="Composition\Program.cs" /> + <Compile Include="PreprocessorLexer.g3.cs"> + <DependentUpon>PreprocessorLexer.g3</DependentUpon> + </Compile> + <Compile Include="PreprocessorTests.cs" /> + <Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="SemanticPredicateReduction.g3.lexer.cs"> <DependentUpon>SemanticPredicateReduction.g3</DependentUpon> </Compile> @@ -83,40 +101,18 @@ <Compile Include="SimpleExpressionParserHelper.cs"> <DependentUpon>SimpleExpression.g3</DependentUpon> </Compile> - </ItemGroup> - <ItemGroup> - <Antlr3 Include="FastSimpleExpression.g3"> - <Generator>MSBuild:Compile</Generator> - </Antlr3> <Compile Include="FastSimpleExpressionLexerHelper.cs"> <DependentUpon>FastSimpleExpression.g3</DependentUpon> </Compile> <Compile Include="FastSimpleExpressionParserHelper.cs"> <DependentUpon>FastSimpleExpression.g3</DependentUpon> </Compile> - </ItemGroup> - <ItemGroup> - <Antlr3 Include="JavaCompat\Expr.g3"> - <Generator>MSBuild:Compile</Generator> - </Antlr3> - </ItemGroup> - <ItemGroup> - <Antlr3 Include="BuildOptions\DebugGrammar.g3"> - <!--<GrammarOptions>-debug</GrammarOptions>--> - <Generator>MSBuild:Compile</Generator> - </Antlr3> <Compile Include="BuildOptions\DebugGrammarLexerHelper.cs"> <DependentUpon>DebugGrammar.g3</DependentUpon> </Compile> <Compile Include="BuildOptions\DebugGrammarParserHelper.cs"> <DependentUpon>DebugGrammar.g3</DependentUpon> </Compile> - </ItemGroup> - <ItemGroup> - <Antlr3 Include="BuildOptions\DebugTreeGrammar.g3"> - <!--<GrammarOptions>-debug</GrammarOptions>--> - <Generator>MSBuild:Compile</Generator> - </Antlr3> <Compile Include="BuildOptions\DebugTreeGrammarHelper.cs"> <DependentUpon>DebugTreeGrammar.g3</DependentUpon> </Compile> @@ -135,6 +131,9 @@ <Compile Include="StringTemplateOutput.g3.parser.cs"> <DependentUpon>StringTemplateOutput.g3</DependentUpon> </Compile> + <Compile Include="SynpredTreeParser.g3.cs"> + <DependentUpon>SynpredTreeParser.g3</DependentUpon> + </Compile> <Compile Include="TestActionFeatures.g3.lexer.cs"> <DependentUpon>TestActionFeatures.g3</DependentUpon> </Compile> @@ -153,7 +152,6 @@ <ItemGroup> <None Include="BuildOptions\ProfileGrammar.g3"> <!--<GrammarOptions>-profile</GrammarOptions>--> - <Generator>MSBuild:Compile</Generator> </None> <None Include="BuildOptions\ProfileGrammarLexerHelper.cs"> <DependentUpon>ProfileGrammar.g3</DependentUpon> @@ -161,41 +159,24 @@ <None Include="BuildOptions\ProfileGrammarParserHelper.cs"> <DependentUpon>ProfileGrammar.g3</DependentUpon> </None> - <Antlr3 Include="StringTemplateOutput.g3"> - <Generator>MSBuild:Compile</Generator> - </Antlr3> - <Antlr3 Include="TestActionFeatures.g3"> - <Generator>MSBuild:Compile</Generator> - </Antlr3> - <Antlr3 Include="SemanticPredicateReduction.g3"> - <Generator>MSBuild:Compile</Generator> - </Antlr3> - <Antlr3 Include="Composition\Reduce.g3" /> - <Antlr3 Include="Composition\Simplify.g3" /> - <Antlr3 Include="Composition\VecMath.g3" /> - <None Include="Composition\VecMath_Lexer.g3" /> - <None Include="Composition\VecMath_Parser.g3" /> - <None Include="TestExpressionFeatures.g3"> - <Generator>MSBuild:Compile</Generator> - </None> + <None Include="TestExpressionFeatures.g3" /> </ItemGroup> <ItemGroup> <None Include="BuildOptions\ProfileTreeGrammar.g3"> <!--<GrammarOptions>-profile</GrammarOptions>--> - <Generator>MSBuild:Compile</Generator> </None> <None Include="BuildOptions\ProfileTreeGrammarHelper.cs"> <DependentUpon>ProfileTreeGrammar.g3</DependentUpon> </None> </ItemGroup> + <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <PropertyGroup> <!-- Folder containing AntlrBuildTask.dll --> - <AntlrBuildTaskPath>$(SolutionDir)bin\Bootstrap</AntlrBuildTaskPath> + <AntlrBuildTaskPath>$(ProjectDir)..\..\..\..\..\..\bin\Bootstrap</AntlrBuildTaskPath> <!-- Path to the ANTLR Tool itself. --> - <AntlrToolPath>$(SolutionDir)bin\Bootstrap\Antlr3.exe</AntlrToolPath> + <AntlrToolPath>$(ProjectDir)..\..\..\..\..\..\bin\Bootstrap\Antlr3.exe</AntlrToolPath> </PropertyGroup> - <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> - <Import Project="$(ProjectDir)..\..\..\..\..\..\antlrcs\main\bin\Bootstrap\Antlr3.targets" /> + <Import Project="$(ProjectDir)..\..\..\..\..\..\bin\Bootstrap\Antlr3.targets" /> <!-- To modify your build process, add your task inside one of the targets below and uncomment it. Other similar extension points exist, see Microsoft.Common.targets. <Target Name="BeforeBuild"> diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/DebugGrammar.g3 b/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/DebugGrammar.g3 index 36b1884..6edb6d3 100644 --- a/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/DebugGrammar.g3 +++ b/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/DebugGrammar.g3 @@ -1,10 +1,10 @@ /* - * [The "BSD licence"] - * Copyright (c) 2005-2008 Terence Parr + * [The "BSD license"] + * Copyright (c) 2011 Terence Parr * All rights reserved. * * Conversion to C#: - * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * Copyright (c) 2011 Sam Harwell, Pixel Mine, Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -46,6 +46,9 @@ tokens CALL; } +@lexer::namespace{Antlr3.Runtime.Test.BuildOptions} +@parser::namespace{Antlr3.Runtime.Test.BuildOptions} + // START:stat prog: ( stat )* ; diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/DebugGrammarParserHelper.cs b/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/DebugGrammarParserHelper.cs index 95beb20..638100e 100644 --- a/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/DebugGrammarParserHelper.cs +++ b/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/DebugGrammarParserHelper.cs @@ -1,10 +1,10 @@ /* - * [The "BSD licence"] - * Copyright (c) 2005-2008 Terence Parr + * [The "BSD license"] + * Copyright (c) 2011 Terence Parr * All rights reserved. * * Conversion to C#: - * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * Copyright (c) 2011 Sam Harwell, Pixel Mine, Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -30,11 +30,14 @@ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -using System.Collections.Generic; -using Antlr.Runtime.Tree; - -partial class DebugGrammarParser +namespace Antlr3.Runtime.Test.BuildOptions { - /** List of function definitions. Must point at the FUNC nodes. */ - List<CommonTree> functionDefinitions = new List<CommonTree>(); + using System.Collections.Generic; + using Antlr.Runtime.Tree; + + partial class DebugGrammarParser + { + /** List of function definitions. Must point at the FUNC nodes. */ + List<CommonTree> functionDefinitions = new List<CommonTree>(); + } } diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/DebugTreeGrammar.g3 b/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/DebugTreeGrammar.g3 index b16a73e..40ee359 100644 --- a/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/DebugTreeGrammar.g3 +++ b/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/DebugTreeGrammar.g3 @@ -1,10 +1,10 @@ /* - * [The "BSD licence"] - * Copyright (c) 2005-2008 Terence Parr + * [The "BSD license"] + * Copyright (c) 2011 Terence Parr * All rights reserved. * * Conversion to C#: - * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * Copyright (c) 2011 Sam Harwell, Pixel Mine, Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -44,11 +44,13 @@ options { //import java.util.Map; //import java.util.HashMap; -using BigInteger = java.math.BigInteger; +using BigInteger = System.Numerics.BigInteger; using Console = System.Console; } // END:members +@namespace{Antlr3.Runtime.Test.BuildOptions} + // START:rules prog: stat* ; @@ -61,13 +63,13 @@ stat: expr { string result = $expr.value.ToString(); ; expr returns [BigInteger value] - : ^('+' a=expr b=expr) { $value = $a.value.add($b.value); } - | ^('-' a=expr b=expr) { $value = $a.value.subtract($b.value); } - | ^('*' a=expr b=expr) { $value = $a.value.multiply($b.value); } - | ^('/' a=expr b=expr) { $value = $a.value.divide($b.value); } - | ^('%' a=expr b=expr) { $value = $a.value.remainder($b.value); } + : ^('+' a=expr b=expr) { $value = $a.value + $b.value; } + | ^('-' a=expr b=expr) { $value = $a.value - $b.value; } + | ^('*' a=expr b=expr) { $value = $a.value * $b.value; } + | ^('/' a=expr b=expr) { $value = $a.value / $b.value; } + | ^('%' a=expr b=expr) { $value = $a.value \% $b.value; } | ID { $value = getValue($ID.text); } - | INT { $value = new BigInteger($INT.text); } + | INT { $value = BigInteger.Parse($INT.text); } | call { $value = $call.value; } ; diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/DebugTreeGrammarHelper.cs b/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/DebugTreeGrammarHelper.cs index af83214..c139aff 100644 --- a/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/DebugTreeGrammarHelper.cs +++ b/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/DebugTreeGrammarHelper.cs @@ -1,10 +1,10 @@ /* - * [The "BSD licence"] - * Copyright (c) 2005-2008 Terence Parr + * [The "BSD license"] + * Copyright (c) 2011 Terence Parr * All rights reserved. * * Conversion to C#: - * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * Copyright (c) 2011 Sam Harwell, Pixel Mine, Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -30,87 +30,90 @@ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -using System.Collections.Generic; -using Antlr.Runtime.Tree; +namespace Antlr3.Runtime.Test.BuildOptions +{ + using System.Collections.Generic; + using Antlr.Runtime.Tree; -using BigInteger = java.math.BigInteger; -using Console = System.Console; + using BigInteger = System.Numerics.BigInteger; + using Console = System.Console; -partial class DebugTreeGrammar -{ - /** Points to functions tracked by tree builder. */ - private List<CommonTree> functionDefinitions; + partial class DebugTreeGrammar + { + /** Points to functions tracked by tree builder. */ + private List<CommonTree> functionDefinitions; - /** Remember local variables. Currently, this is only the function parameter. - */ - private readonly IDictionary<string, BigInteger> localMemory = new Dictionary<string, BigInteger>(); + /** Remember local variables. Currently, this is only the function parameter. + */ + private readonly IDictionary<string, BigInteger> localMemory = new Dictionary<string, BigInteger>(); - /** Remember global variables set by =. */ - private IDictionary<string, BigInteger> globalMemory = new Dictionary<string, BigInteger>(); + /** Remember global variables set by =. */ + private IDictionary<string, BigInteger> globalMemory = new Dictionary<string, BigInteger>(); - /** Set up an evaluator with a node stream; and a set of function definition ASTs. */ - public DebugTreeGrammar( CommonTreeNodeStream nodes, List<CommonTree> functionDefinitions ) - : this( nodes ) - { - this.functionDefinitions = functionDefinitions; - } + /** Set up an evaluator with a node stream; and a set of function definition ASTs. */ + public DebugTreeGrammar(CommonTreeNodeStream nodes, List<CommonTree> functionDefinitions) + : this(nodes) + { + this.functionDefinitions = functionDefinitions; + } - /** Set up a local evaluator for a nested function call. The evaluator gets the definition - * tree of the function; the set of all defined functions (to find locally called ones); a - * pointer to the global variable memory; and the value of the function parameter to be - * added to the local memory. - */ - private DebugTreeGrammar( CommonTree function, - List<CommonTree> functionDefinitions, - IDictionary<string, BigInteger> globalMemory, - BigInteger paramValue ) - // Expected tree for function: ^(FUNC ID ( INT | ID ) expr) - : this( new CommonTreeNodeStream( function.GetChild( 2 ) ), functionDefinitions ) - { - this.globalMemory = globalMemory; - localMemory[function.GetChild( 1 ).Text] = paramValue; - } + /** Set up a local evaluator for a nested function call. The evaluator gets the definition + * tree of the function; the set of all defined functions (to find locally called ones); a + * pointer to the global variable memory; and the value of the function parameter to be + * added to the local memory. + */ + private DebugTreeGrammar(CommonTree function, + List<CommonTree> functionDefinitions, + IDictionary<string, BigInteger> globalMemory, + BigInteger paramValue) + // Expected tree for function: ^(FUNC ID ( INT | ID ) expr) + : this(new CommonTreeNodeStream(function.GetChild(2)), functionDefinitions) + { + this.globalMemory = globalMemory; + localMemory[function.GetChild(1).Text] = paramValue; + } - /** Find matching function definition for a function name and parameter - * value. The first definition is returned where (a) the name matches - * and (b) the formal parameter agrees if it is defined as constant. - */ - private CommonTree findFunction( string name, BigInteger paramValue ) - { - foreach ( CommonTree f in functionDefinitions ) + /** Find matching function definition for a function name and parameter + * value. The first definition is returned where (a) the name matches + * and (b) the formal parameter agrees if it is defined as constant. + */ + private CommonTree findFunction(string name, BigInteger paramValue) { - // Expected tree for f: ^(FUNC ID (ID | INT) expr) - if ( f.GetChild( 0 ).Text.Equals( name ) ) + foreach (CommonTree f in functionDefinitions) { - // Check whether parameter matches - CommonTree formalPar = (CommonTree)f.GetChild( 1 ); - if ( formalPar.Token.Type == INT - && !new BigInteger( formalPar.Token.Text ).Equals( paramValue ) ) + // Expected tree for f: ^(FUNC ID (ID | INT) expr) + if (f.GetChild(0).Text.Equals(name)) { - // Constant in formalPar list does not match actual value -> no match. - continue; + // Check whether parameter matches + CommonTree formalPar = (CommonTree)f.GetChild(1); + if (formalPar.Token.Type == INT + && !BigInteger.Parse(formalPar.Token.Text).Equals(paramValue)) + { + // Constant in formalPar list does not match actual value -> no match. + continue; + } + // Parameter (value for INT formal arg) as well as fct name agrees! + return f; } - // Parameter (value for INT formal arg) as well as fct name agrees! - return f; } + return null; } - return null; - } - /** Get value of name up call stack. */ - internal BigInteger getValue( string name ) - { - BigInteger value; - if ( localMemory.TryGetValue( name, out value ) && value != null ) - { - return value; - } - if ( globalMemory.TryGetValue( name, out value ) && value != null ) + /** Get value of name up call stack. */ + internal BigInteger getValue(string name) { - return value; + BigInteger value; + if (localMemory.TryGetValue(name, out value) && value != null) + { + return value; + } + if (globalMemory.TryGetValue(name, out value) && value != null) + { + return value; + } + // not found in local memory or global memory + Console.Error.WriteLine("undefined variable " + name); + return BigInteger.Zero; } - // not found in local memory or global memory - Console.Error.WriteLine( "undefined variable " + name ); - return new BigInteger( "0" ); } } diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/ProfileGrammar.g3 b/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/ProfileGrammar.g3 index 5f8de16..9230134 100644 --- a/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/ProfileGrammar.g3 +++ b/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/ProfileGrammar.g3 @@ -1,10 +1,10 @@ /* - * [The "BSD licence"] - * Copyright (c) 2005-2008 Terence Parr + * [The "BSD license"] + * Copyright (c) 2011 Terence Parr * All rights reserved. * * Conversion to C#: - * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * Copyright (c) 2011 Sam Harwell, Pixel Mine, Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -46,6 +46,9 @@ tokens CALL; } +@lexer::namespace{Antlr3.Runtime.Test.BuildOptions} +@parser::namespace{Antlr3.Runtime.Test.BuildOptions} + // START:stat prog: ( stat )* ; diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/ProfileGrammarParserHelper.cs b/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/ProfileGrammarParserHelper.cs index ddd7533..205a088 100644 --- a/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/ProfileGrammarParserHelper.cs +++ b/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/ProfileGrammarParserHelper.cs @@ -1,10 +1,10 @@ /* - * [The "BSD licence"] - * Copyright (c) 2005-2008 Terence Parr + * [The "BSD license"] + * Copyright (c) 2011 Terence Parr * All rights reserved. * * Conversion to C#: - * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * Copyright (c) 2011 Sam Harwell, Pixel Mine, Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -30,11 +30,14 @@ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -using System.Collections.Generic; -using Antlr.Runtime.Tree; - -partial class ProfileGrammarParser +namespace Antlr3.Runtime.Test.BuildOptions { - /** List of function definitions. Must point at the FUNC nodes. */ - List<CommonTree> functionDefinitions = new List<CommonTree>(); + using System.Collections.Generic; + using Antlr.Runtime.Tree; + + partial class ProfileGrammarParser + { + /** List of function definitions. Must point at the FUNC nodes. */ + List<CommonTree> functionDefinitions = new List<CommonTree>(); + } } diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/ProfileTreeGrammar.g3 b/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/ProfileTreeGrammar.g3 index f6786db..5bf107b 100644 --- a/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/ProfileTreeGrammar.g3 +++ b/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/ProfileTreeGrammar.g3 @@ -1,10 +1,10 @@ /* - * [The "BSD licence"] - * Copyright (c) 2005-2008 Terence Parr + * [The "BSD license"] + * Copyright (c) 2011 Terence Parr * All rights reserved. * * Conversion to C#: - * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * Copyright (c) 2011 Sam Harwell, Pixel Mine, Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -49,6 +49,8 @@ using Console = System.Console; } // END:members +@namespace{Antlr3.Runtime.Test.BuildOptions} + // START:rules prog: stat* ; diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/ProfileTreeGrammarHelper.cs b/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/ProfileTreeGrammarHelper.cs index 47cc8a8..06d678a 100644 --- a/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/ProfileTreeGrammarHelper.cs +++ b/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/ProfileTreeGrammarHelper.cs @@ -1,10 +1,10 @@ /* - * [The "BSD licence"] - * Copyright (c) 2005-2008 Terence Parr + * [The "BSD license"] + * Copyright (c) 2011 Terence Parr * All rights reserved. * * Conversion to C#: - * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * Copyright (c) 2011 Sam Harwell, Pixel Mine, Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -30,87 +30,90 @@ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -using System.Collections.Generic; -using Antlr.Runtime.Tree; +namespace Antlr3.Runtime.Test.BuildOptions +{ + using System.Collections.Generic; + using Antlr.Runtime.Tree; -using BigInteger = java.math.BigInteger; -using Console = System.Console; + using BigInteger = java.math.BigInteger; + using Console = System.Console; -partial class ProfileTreeGrammar -{ - /** Points to functions tracked by tree builder. */ - private List<CommonTree> functionDefinitions; + partial class ProfileTreeGrammar + { + /** Points to functions tracked by tree builder. */ + private List<CommonTree> functionDefinitions; - /** Remember local variables. Currently, this is only the function parameter. - */ - private readonly IDictionary<string, BigInteger> localMemory = new Dictionary<string, BigInteger>(); + /** Remember local variables. Currently, this is only the function parameter. + */ + private readonly IDictionary<string, BigInteger> localMemory = new Dictionary<string, BigInteger>(); - /** Remember global variables set by =. */ - private IDictionary<string, BigInteger> globalMemory = new Dictionary<string, BigInteger>(); + /** Remember global variables set by =. */ + private IDictionary<string, BigInteger> globalMemory = new Dictionary<string, BigInteger>(); - /** Set up an evaluator with a node stream; and a set of function definition ASTs. */ - public ProfileTreeGrammar( CommonTreeNodeStream nodes, List<CommonTree> functionDefinitions ) - : this( nodes ) - { - this.functionDefinitions = functionDefinitions; - } + /** Set up an evaluator with a node stream; and a set of function definition ASTs. */ + public ProfileTreeGrammar(CommonTreeNodeStream nodes, List<CommonTree> functionDefinitions) + : this(nodes) + { + this.functionDefinitions = functionDefinitions; + } - /** Set up a local evaluator for a nested function call. The evaluator gets the definition - * tree of the function; the set of all defined functions (to find locally called ones); a - * pointer to the global variable memory; and the value of the function parameter to be - * added to the local memory. - */ - private ProfileTreeGrammar( CommonTree function, - List<CommonTree> functionDefinitions, - IDictionary<string, BigInteger> globalMemory, - BigInteger paramValue ) - // Expected tree for function: ^(FUNC ID ( INT | ID ) expr) - : this( new CommonTreeNodeStream( function.GetChild( 2 ) ), functionDefinitions ) - { - this.globalMemory = globalMemory; - localMemory[function.GetChild( 1 ).Text] = paramValue; - } + /** Set up a local evaluator for a nested function call. The evaluator gets the definition + * tree of the function; the set of all defined functions (to find locally called ones); a + * pointer to the global variable memory; and the value of the function parameter to be + * added to the local memory. + */ + private ProfileTreeGrammar(CommonTree function, + List<CommonTree> functionDefinitions, + IDictionary<string, BigInteger> globalMemory, + BigInteger paramValue) + // Expected tree for function: ^(FUNC ID ( INT | ID ) expr) + : this(new CommonTreeNodeStream(function.GetChild(2)), functionDefinitions) + { + this.globalMemory = globalMemory; + localMemory[function.GetChild(1).Text] = paramValue; + } - /** Find matching function definition for a function name and parameter - * value. The first definition is returned where (a) the name matches - * and (b) the formal parameter agrees if it is defined as constant. - */ - private CommonTree findFunction( string name, BigInteger paramValue ) - { - foreach ( CommonTree f in functionDefinitions ) + /** Find matching function definition for a function name and parameter + * value. The first definition is returned where (a) the name matches + * and (b) the formal parameter agrees if it is defined as constant. + */ + private CommonTree findFunction(string name, BigInteger paramValue) { - // Expected tree for f: ^(FUNC ID (ID | INT) expr) - if ( f.GetChild( 0 ).Text.Equals( name ) ) + foreach (CommonTree f in functionDefinitions) { - // Check whether parameter matches - CommonTree formalPar = (CommonTree)f.GetChild( 1 ); - if ( formalPar.Token.Type == INT - && !new BigInteger( formalPar.Token.Text ).Equals( paramValue ) ) + // Expected tree for f: ^(FUNC ID (ID | INT) expr) + if (f.GetChild(0).Text.Equals(name)) { - // Constant in formalPar list does not match actual value -> no match. - continue; + // Check whether parameter matches + CommonTree formalPar = (CommonTree)f.GetChild(1); + if (formalPar.Token.Type == INT + && !new BigInteger(formalPar.Token.Text).Equals(paramValue)) + { + // Constant in formalPar list does not match actual value -> no match. + continue; + } + // Parameter (value for INT formal arg) as well as fct name agrees! + return f; } - // Parameter (value for INT formal arg) as well as fct name agrees! - return f; } + return null; } - return null; - } - /** Get value of name up call stack. */ - public BigInteger getValue( string name ) - { - BigInteger value; - if ( localMemory.TryGetValue( name, out value ) && value != null ) - { - return value; - } - if ( globalMemory.TryGetValue( name, out value ) && value != null ) + /** Get value of name up call stack. */ + public BigInteger getValue(string name) { - return value; + BigInteger value; + if (localMemory.TryGetValue(name, out value) && value != null) + { + return value; + } + if (globalMemory.TryGetValue(name, out value) && value != null) + { + return value; + } + // not found in local memory or global memory + Console.Error.WriteLine("undefined variable " + name); + return new BigInteger("0"); } - // not found in local memory or global memory - Console.Error.WriteLine( "undefined variable " + name ); - return new BigInteger( "0" ); } } diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime.Test/Composition/Reduce.g3 b/runtime/CSharp3/Sources/Antlr3.Runtime.Test/Composition/Reduce.g3 index 0ed570b..db61e42 100644 --- a/runtime/CSharp3/Sources/Antlr3.Runtime.Test/Composition/Reduce.g3 +++ b/runtime/CSharp3/Sources/Antlr3.Runtime.Test/Composition/Reduce.g3 @@ -2,20 +2,14 @@ options { - tokenVocab=VecMath; - ASTLabelType=CommonTree; - output=AST; - filter=true; - language=CSharp3; + tokenVocab=VecMath; + ASTLabelType=CommonTree; + output=AST; + filter=true; + language=CSharp3; } - -@members -{ - //public override IAstRuleReturnScope Topdown() { return topdown(); } - public override IAstRuleReturnScope Bottomup() { return bottomup(); } -} - +@namespace{Antlr3.Runtime.Test.Composition} /** Rewrite: x+x to be 2*x, 2*x to be x<<1, x<<n<<m to be x<<(n+m) */ bottomup diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime.Test/Composition/Simplify.g3 b/runtime/CSharp3/Sources/Antlr3.Runtime.Test/Composition/Simplify.g3 index fd1fded..f1c2550 100644 --- a/runtime/CSharp3/Sources/Antlr3.Runtime.Test/Composition/Simplify.g3 +++ b/runtime/CSharp3/Sources/Antlr3.Runtime.Test/Composition/Simplify.g3 @@ -9,12 +9,7 @@ options { //rewrite=true; } -@members -{ - public override IAstRuleReturnScope Topdown() { return topdown(); } - public override IAstRuleReturnScope Bottomup() { return bottomup(); } -} - +@namespace{Antlr3.Runtime.Test.Composition} topdown : ^( MULT INT ^(VEC (e+=.)+) ) -> ^(VEC ^(MULT INT $e)+) diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime.Test/FastSimpleExpression.g3 b/runtime/CSharp3/Sources/Antlr3.Runtime.Test/FastSimpleExpression.g3 index 1c51490..b0d5dfd 100644 --- a/runtime/CSharp3/Sources/Antlr3.Runtime.Test/FastSimpleExpression.g3 +++ b/runtime/CSharp3/Sources/Antlr3.Runtime.Test/FastSimpleExpression.g3 @@ -41,6 +41,8 @@ options } @lexer::superClass{Antlr.Runtime.SlimLexer} +@lexer::namespace{Antlr3.Runtime.Test} +@parser::namespace{Antlr3.Runtime.Test} public expression diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime.Test/JavaCompat/Expr.g3 b/runtime/CSharp3/Sources/Antlr3.Runtime.Test/JavaCompat/Expr.g3 index 65e7c5d..78c79cc 100644 --- a/runtime/CSharp3/Sources/Antlr3.Runtime.Test/JavaCompat/Expr.g3 +++ b/runtime/CSharp3/Sources/Antlr3.Runtime.Test/JavaCompat/Expr.g3 @@ -1,10 +1,10 @@ /* - * [The "BSD licence"] - * Copyright (c) 2005-2008 Terence Parr + * [The "BSD license"] + * Copyright (c) 2011 Terence Parr * All rights reserved. * * Conversion to C#: - * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * Copyright (c) 2011 Sam Harwell, Pixel Mine, Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -74,6 +74,9 @@ using Integer = java.lang.Integer; HashMap memory = new HashMap(); } +@lexer::namespace{Antlr3.Runtime.Test.JavaCompat} +@parser::namespace{Antlr3.Runtime.Test.JavaCompat} + prog: stat+ ; stat: expr NEWLINE {JSystem.@out.println($expr.value);} diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime.Test/PreprocessorLexer.g3 b/runtime/CSharp3/Sources/Antlr3.Runtime.Test/PreprocessorLexer.g3 new file mode 100644 index 0000000..29655ef --- /dev/null +++ b/runtime/CSharp3/Sources/Antlr3.Runtime.Test/PreprocessorLexer.g3 @@ -0,0 +1,69 @@ +/* + * [The "BSD license"] + * Copyright (c) 2011 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2011 Sam Harwell, Pixel Mine, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +lexer grammar PreprocessorLexer; + +options +{ + language=CSharp3; +} + +@namespace{Antlr3.Runtime.Test} + +PP_SKIPPED_CHARACTERS + : {false}? => ~(F_NEW_LINE_CHARACTER | F_PP_POUND_SIGN) F_INPUT_CHARACTER* + ; + +DELIMITED_COMMENT + : {true}? => '/*' .* '*/' + ; + +WHITESPACE + : F_WHITESPACE {Skip();} + ; + +fragment F_WHITESPACE + : (' ' | '\t' | '\v' | '\f')+ + ; + +fragment F_NEW_LINE_CHARACTER + : '\r' + | '\n' + ; + +fragment F_PP_POUND_SIGN + : '#' + ; + +fragment F_INPUT_CHARACTER + : ~F_NEW_LINE_CHARACTER + ; diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime.Test/PreprocessorLexer.g3.cs b/runtime/CSharp3/Sources/Antlr3.Runtime.Test/PreprocessorLexer.g3.cs new file mode 100644 index 0000000..e095fbf --- /dev/null +++ b/runtime/CSharp3/Sources/Antlr3.Runtime.Test/PreprocessorLexer.g3.cs @@ -0,0 +1,38 @@ +/* + * [The "BSD license"] + * Copyright (c) 2011 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2011 Sam Harwell, Pixel Mine, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace Antlr3.Runtime.Test +{ + partial class PreprocessorLexer + { + } +} diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime.Test/PreprocessorTests.cs b/runtime/CSharp3/Sources/Antlr3.Runtime.Test/PreprocessorTests.cs new file mode 100644 index 0000000..967f668 --- /dev/null +++ b/runtime/CSharp3/Sources/Antlr3.Runtime.Test/PreprocessorTests.cs @@ -0,0 +1,58 @@ +/* + * [The "BSD license"] + * Copyright (c) 2011 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2011 Sam Harwell, Pixel Mine, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace Antlr3.Runtime.Test +{ + using System.Collections.Generic; + using Antlr.Runtime; + using Microsoft.VisualStudio.TestTools.UnitTesting; + + [TestClass] + public class PreprocessorTests + { + [TestMethod] + public void TestEmptyComment() + { + string inputText = "/**/ "; + var input = new ANTLRStringStream(inputText); + var lexer = new PreprocessorLexer(input); + var tokenStream = new CommonTokenStream(lexer); + tokenStream.Fill(); + + List<IToken> tokens = tokenStream.GetTokens(); + Assert.AreEqual(2, tokens.Count); + Assert.AreEqual(PreprocessorLexer.DELIMITED_COMMENT, tokens[0].Type); + Assert.AreEqual("/**/", tokens[0].Text); + Assert.AreEqual(PreprocessorLexer.EOF, tokens[1].Type); + } + } +} diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime.Test/Properties/AssemblyInfo.cs b/runtime/CSharp3/Sources/Antlr3.Runtime.Test/Properties/AssemblyInfo.cs index fd2190d..1e0fc38 100644 --- a/runtime/CSharp3/Sources/Antlr3.Runtime.Test/Properties/AssemblyInfo.cs +++ b/runtime/CSharp3/Sources/Antlr3.Runtime.Test/Properties/AssemblyInfo.cs @@ -42,7 +42,7 @@ using System.Runtime.InteropServices; [assembly: AssemblyConfiguration( "" )] [assembly: AssemblyCompany( "Pixel Mine, Inc." )] [assembly: AssemblyProduct( "Antlr3.Runtime.Test" )] -[assembly: AssemblyCopyright("Copyright © Sam Harwell 2011")] +[assembly: AssemblyCopyright("Copyright © Sam Harwell 2013")] [assembly: AssemblyTrademark( "" )] [assembly: AssemblyCulture( "" )] [assembly: CLSCompliant( true )] @@ -66,5 +66,5 @@ using System.Runtime.InteropServices; * the Major or Minor version is incremented. * 3. Revision is the Perforce changelist number associated with the release. */ -[assembly: AssemblyVersion("3.3.2.8098")] -[assembly: AssemblyFileVersion("3.3.2.8098")] +[assembly: AssemblyVersion("3.5.0.2")] +[assembly: AssemblyFileVersion("3.5.0.2")] diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime.Test/SynpredTreeParser.g3 b/runtime/CSharp3/Sources/Antlr3.Runtime.Test/SynpredTreeParser.g3 new file mode 100644 index 0000000..6bb7df4 --- /dev/null +++ b/runtime/CSharp3/Sources/Antlr3.Runtime.Test/SynpredTreeParser.g3 @@ -0,0 +1,47 @@ +/* + * [The "BSD license"] + * Copyright (c) 2011 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2011 Sam Harwell, Pixel Mine, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +tree grammar SynpredTreeParser; + +options +{ + language=CSharp3; + ASTLabelType=ITree; + output=AST; +} + +@namespace{Antlr3.Runtime.Test} + +enterInsertOrDeleteData + : ^( INSERT ( (DATA)=>DATA { _value = true; } | { _value = true; } ) .* ) + | ^( DELETE ( (DATA)=>DATA { _value = false; } | { _value = false; } ) .* ) + ; diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime.Test/SynpredTreeParser.g3.cs b/runtime/CSharp3/Sources/Antlr3.Runtime.Test/SynpredTreeParser.g3.cs new file mode 100644 index 0000000..f127aad --- /dev/null +++ b/runtime/CSharp3/Sources/Antlr3.Runtime.Test/SynpredTreeParser.g3.cs @@ -0,0 +1,41 @@ +/* + * [The "BSD license"] + * Copyright (c) 2011 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2011 Sam Harwell, Pixel Mine, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma warning disable 414 // The field 'fieldname' is assigned but its value is never used + +namespace Antlr3.Runtime.Test +{ + partial class SynpredTreeParser + { + private bool _value; + } +} diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime/ANTLRStringStream.cs b/runtime/CSharp3/Sources/Antlr3.Runtime/ANTLRStringStream.cs index 9904680..b3f11b4 100644 --- a/runtime/CSharp3/Sources/Antlr3.Runtime/ANTLRStringStream.cs +++ b/runtime/CSharp3/Sources/Antlr3.Runtime/ANTLRStringStream.cs @@ -241,9 +241,9 @@ namespace Antlr.Runtime { state = markers[markDepth]; } - state.p = p; - state.line = line; - state.charPositionInLine = charPositionInLine; + state.p = Index; + state.line = Line; + state.charPositionInLine = CharPositionInLine; lastMarker = markDepth; return markDepth; } diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime/Antlr3.Runtime.csproj b/runtime/CSharp3/Sources/Antlr3.Runtime/Antlr3.Runtime.csproj index 6de4bf7..5da739f 100644 --- a/runtime/CSharp3/Sources/Antlr3.Runtime/Antlr3.Runtime.csproj +++ b/runtime/CSharp3/Sources/Antlr3.Runtime/Antlr3.Runtime.csproj @@ -12,14 +12,8 @@ <AssemblyName>Antlr3.Runtime</AssemblyName> <TargetFrameworkVersion>v2.0</TargetFrameworkVersion> <FileAlignment>512</FileAlignment> - <SccProjectName>SAK</SccProjectName> - <SccLocalPath>SAK</SccLocalPath> - <SccAuxPath>SAK</SccAuxPath> - <SccProvider>SAK</SccProvider> <SignAssembly>true</SignAssembly> - <AssemblyOriginatorKeyFile>..\..\..\..\..\..\..\keys\antlr\Key.snk</AssemblyOriginatorKeyFile> - <TargetFrameworkProfile> - </TargetFrameworkProfile> + <AssemblyOriginatorKeyFile>..\..\..\..\..\..\..\..\..\keys\antlr\Key.snk</AssemblyOriginatorKeyFile> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <DebugSymbols>true</DebugSymbols> @@ -29,6 +23,7 @@ <DefineConstants>DEBUG;TRACE</DefineConstants> <ErrorReport>prompt</ErrorReport> <WarningLevel>4</WarningLevel> + <DocumentationFile>bin\Debug\Antlr3.Runtime.xml</DocumentationFile> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> <DebugType>pdbonly</DebugType> @@ -37,6 +32,7 @@ <DefineConstants>TRACE</DefineConstants> <ErrorReport>prompt</ErrorReport> <WarningLevel>4</WarningLevel> + <DocumentationFile>bin\Release\Antlr3.Runtime.xml</DocumentationFile> </PropertyGroup> <ItemGroup> <Reference Include="System" /> @@ -105,6 +101,7 @@ <Compile Include="Tree\CommonTreeAdaptor.cs" /> <Compile Include="Tree\CommonTreeNodeStream.cs" /> <Compile Include="Tree\DotTreeGenerator.cs" /> + <Compile Include="Tree\IPositionTrackingStream.cs" /> <Compile Include="Tree\ITree.cs" /> <Compile Include="Tree\ITreeAdaptor.cs" /> <Compile Include="Tree\ITreeNodeStream.cs" /> @@ -131,7 +128,7 @@ <Compile Include="UnwantedTokenException.cs" /> </ItemGroup> <ItemGroup> - <None Include="..\..\..\..\..\..\..\keys\antlr\Key.snk"> + <None Include="..\..\..\..\..\..\..\..\..\keys\antlr\Key.snk"> <Link>Key.snk</Link> </None> </ItemGroup> diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime/BaseRecognizer.cs b/runtime/CSharp3/Sources/Antlr3.Runtime/BaseRecognizer.cs index 5ba18f5..c62a5bf 100644 --- a/runtime/CSharp3/Sources/Antlr3.Runtime/BaseRecognizer.cs +++ b/runtime/CSharp3/Sources/Antlr3.Runtime/BaseRecognizer.cs @@ -1,10 +1,10 @@ /* - * [The "BSD licence"] - * Copyright (c) 2005-2008 Terence Parr + * [The "BSD license"] + * Copyright (c) 2011 Terence Parr * All rights reserved. * * Conversion to C#: - * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * Copyright (c) 2011 Sam Harwell, Pixel Mine, Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -37,15 +37,12 @@ namespace Antlr.Runtime using ArgumentNullException = System.ArgumentNullException; using Array = System.Array; using Conditional = System.Diagnostics.ConditionalAttribute; - using Exception = System.Exception; using IDebugEventListener = Antlr.Runtime.Debug.IDebugEventListener; using MethodBase = System.Reflection.MethodBase; - using NotSupportedException = System.NotSupportedException; using Regex = System.Text.RegularExpressions.Regex; using StackFrame = System.Diagnostics.StackFrame; using StackTrace = System.Diagnostics.StackTrace; using TextWriter = System.IO.TextWriter; - using Type = System.Type; /** <summary> * A generic recognizer that can handle recognizers generated from @@ -97,6 +94,11 @@ namespace Antlr.Runtime set; } + public virtual void SetState(RecognizerSharedState value) + { + this.state = value; + } + protected virtual void InitDFAs() { } diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime/CommonTokenStream.cs b/runtime/CSharp3/Sources/Antlr3.Runtime/CommonTokenStream.cs index a1e4a29..28813cb 100644 --- a/runtime/CSharp3/Sources/Antlr3.Runtime/CommonTokenStream.cs +++ b/runtime/CSharp3/Sources/Antlr3.Runtime/CommonTokenStream.cs @@ -166,6 +166,12 @@ namespace Antlr.Runtime return i; } + public override void Reset() + { + base.Reset(); + _p = SkipOffTokenChannels(0); + } + protected override void Setup() { _p = 0; diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime/DFA.cs b/runtime/CSharp3/Sources/Antlr3.Runtime/DFA.cs index 37e2d06..76c4083 100644 --- a/runtime/CSharp3/Sources/Antlr3.Runtime/DFA.cs +++ b/runtime/CSharp3/Sources/Antlr3.Runtime/DFA.cs @@ -32,6 +32,7 @@ namespace Antlr.Runtime { + using ArgumentNullException = System.ArgumentNullException; using ConditionalAttribute = System.Diagnostics.ConditionalAttribute; using Console = System.Console; using IDebugEventListener = Antlr.Runtime.Debug.IDebugEventListener; @@ -66,16 +67,16 @@ namespace Antlr.Runtime /** <summary>Which recognizer encloses this DFA? Needed to check backtracking</summary> */ protected BaseRecognizer recognizer; - public readonly bool debug = false; + public bool debug = false; public DFA() - : this( new SpecialStateTransitionHandler( SpecialStateTransitionDefault ) ) + : this(SpecialStateTransitionDefault) { } public DFA( SpecialStateTransitionHandler specialStateTransition ) { - this.SpecialStateTransition = specialStateTransition ?? new SpecialStateTransitionHandler( SpecialStateTransitionDefault ); + this.SpecialStateTransition = specialStateTransition ?? SpecialStateTransitionDefault; } public virtual string Description @@ -95,47 +96,44 @@ namespace Antlr.Runtime */ public virtual int Predict( IIntStream input ) { - if ( debug ) - { - Console.Error.WriteLine( "Enter DFA.predict for decision " + decisionNumber ); - } + if (input == null) + throw new ArgumentNullException("input"); + + DfaDebugMessage("Enter DFA.Predict for decision {0}", decisionNumber); + int mark = input.Mark(); // remember where decision started in input int s = 0; // we always start at s0 try { - for ( ; ; ) + while (true) { - if ( debug ) - Console.Error.WriteLine( "DFA " + decisionNumber + " state " + s + " LA(1)=" + (char)input.LA( 1 ) + "(" + input.LA( 1 ) + - "), index=" + input.Index ); + DfaDebugMessage("DFA {0} state {1} LA(1)={2}({3}), index={4}", decisionNumber, s, (char)input.LA(1), input.LA(1), input.Index); + int specialState = special[s]; if ( specialState >= 0 ) { - if ( debug ) - { - Console.Error.WriteLine( "DFA " + decisionNumber + - " state " + s + " is special state " + specialState ); - } + DfaDebugMessage("DFA {0} state {1} is special state {2}", decisionNumber, s, specialState); + s = SpecialStateTransition( this, specialState, input ); - if ( debug ) - { - Console.Error.WriteLine( "DFA " + decisionNumber + - " returns from special state " + specialState + " to " + s ); - } + + DfaDebugMessage("DFA {0} returns from special state {1} to {2}", decisionNumber, specialState, s); + if ( s == -1 ) { NoViableAlt( s, input ); return 0; } + input.Consume(); continue; } + if ( accept[s] >= 1 ) { - if ( debug ) - Console.Error.WriteLine( "accept; predict " + accept[s] + " from state " + s ); + DfaDebugMessage("accept; predict {0} from state {1}", accept[s], s); return accept[s]; } + // look for a normal char transition char c = (char)input.LA( 1 ); // -1 == \uFFFF, all tokens fit in 65000 space if ( c >= min[s] && c <= max[s] ) @@ -148,9 +146,9 @@ namespace Antlr.Runtime // eot[s]>=0 indicates that an EOT edge goes to another // state. if ( eot[s] >= 0 ) - { // EOT Transition to accept state? - if ( debug ) - Console.Error.WriteLine( "EOT transition" ); + { + // EOT Transition to accept state? + DfaDebugMessage("EOT transition"); s = eot[s]; input.Consume(); // TODO: I had this as return accept[eot[s]] @@ -160,40 +158,35 @@ namespace Antlr.Runtime // target? continue; } + NoViableAlt( s, input ); return 0; } + s = snext; input.Consume(); continue; } + if ( eot[s] >= 0 ) - { // EOT Transition? - if ( debug ) - Console.Error.WriteLine( "EOT transition" ); + { + // EOT Transition? + DfaDebugMessage("EOT transition"); s = eot[s]; input.Consume(); continue; } + if ( c == unchecked( (char)TokenTypes.EndOfFile ) && eof[s] >= 0 ) - { // EOF Transition to accept state? - if ( debug ) - Console.Error.WriteLine( "accept via EOF; predict " + accept[eof[s]] + " from " + eof[s] ); + { + // EOF Transition to accept state? + DfaDebugMessage("accept via EOF; predict {0} from {1}", accept[eof[s]], eof[s]); return accept[eof[s]]; } + // not in range and not EOF/EOT, must be invalid symbol - if ( debug ) - { - Console.Error.WriteLine( "min[" + s + "]=" + min[s] ); - Console.Error.WriteLine( "max[" + s + "]=" + max[s] ); - Console.Error.WriteLine( "eot[" + s + "]=" + eot[s] ); - Console.Error.WriteLine( "eof[" + s + "]=" + eof[s] ); - for ( int p = 0; p < transition[s].Length; p++ ) - { - Console.Error.Write( transition[s][p] + " " ); - } - Console.Error.WriteLine(); - } + DfaDebugInvalidSymbol(s); + NoViableAlt( s, input ); return 0; } @@ -204,6 +197,26 @@ namespace Antlr.Runtime } } + [Conditional("DEBUG_DFA")] + private void DfaDebugMessage(string format, params object[] args) + { + Console.Error.WriteLine(format, args); + } + + [Conditional("DEBUG_DFA")] + private void DfaDebugInvalidSymbol(int s) + { + Console.Error.WriteLine("min[{0}]={1}", s, min[s]); + Console.Error.WriteLine("max[{0}]={1}", s, max[s]); + Console.Error.WriteLine("eot[{0}]={1}", s, eot[s]); + Console.Error.WriteLine("eof[{0}]={1}", s, eof[s]); + + for (int p = 0; p < transition[s].Length; p++) + Console.Error.Write(transition[s][p] + " "); + + Console.Error.WriteLine(); + } + protected virtual void NoViableAlt( int s, IIntStream input ) { if ( recognizer.state.backtracking > 0 ) diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime/Lexer.cs b/runtime/CSharp3/Sources/Antlr3.Runtime/Lexer.cs index cf478c6..daf530d 100644 --- a/runtime/CSharp3/Sources/Antlr3.Runtime/Lexer.cs +++ b/runtime/CSharp3/Sources/Antlr3.Runtime/Lexer.cs @@ -137,10 +137,7 @@ namespace Antlr.Runtime state.text = null; if ( input.LA( 1 ) == CharStreamConstants.EndOfFile ) { - IToken eof = new CommonToken((ICharStream)input, CharStreamConstants.EndOfFile, TokenChannels.Default, input.Index, input.Index); - eof.Line = Line; - eof.CharPositionInLine = CharPositionInLine; - return eof; + return GetEndOfFileToken(); } try { @@ -173,6 +170,17 @@ namespace Antlr.Runtime } } + /** Returns the EOF token (default), if you need + * to return a custom token instead override this method. + */ + public virtual IToken GetEndOfFileToken() + { + IToken eof = new CommonToken((ICharStream)input, CharStreamConstants.EndOfFile, TokenChannels.Default, input.Index, input.Index); + eof.Line = Line; + eof.CharPositionInLine = CharPositionInLine; + return eof; + } + /** <summary> * Instruct the lexer to skip creating a token for current lexer rule * and look for another token. nextToken() knows to keep looking when diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime/Misc/FastQueue.cs b/runtime/CSharp3/Sources/Antlr3.Runtime/Misc/FastQueue.cs index 2dc5bfc..af975d7 100644 --- a/runtime/CSharp3/Sources/Antlr3.Runtime/Misc/FastQueue.cs +++ b/runtime/CSharp3/Sources/Antlr3.Runtime/Misc/FastQueue.cs @@ -71,9 +71,9 @@ namespace Antlr.Runtime.Misc } /** <summary> - * Return element i elements ahead of current element. i==0 gets - * current element. This is not an absolute index into the data list - * since p defines the start of the real list. + * Return element {@code i} elements ahead of current element. {@code i==0} + * gets current element. This is not an absolute index into {@link #data} + * since {@code p} defines the start of the real list. * </summary> */ public virtual T this[int i] diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime/Misc/LookaheadStream.cs b/runtime/CSharp3/Sources/Antlr3.Runtime/Misc/LookaheadStream.cs index 24dc0cb..14454a8 100644 --- a/runtime/CSharp3/Sources/Antlr3.Runtime/Misc/LookaheadStream.cs +++ b/runtime/CSharp3/Sources/Antlr3.Runtime/Misc/LookaheadStream.cs @@ -33,13 +33,16 @@ namespace Antlr.Runtime.Misc { using ArgumentException = System.ArgumentException; + using Debug = System.Diagnostics.Debug; using InvalidOperationException = System.InvalidOperationException; + using NotSupportedException = System.NotSupportedException; + using ArgumentOutOfRangeException = System.ArgumentOutOfRangeException; /** <summary> - * A lookahead queue that knows how to mark/release locations - * in the buffer for backtracking purposes. Any markers force the FastQueue - * superclass to keep all tokens until no more markers; then can reset - * to avoid growing a huge buffer. + * A lookahead queue that knows how to mark/release locations in the buffer for + * backtracking purposes. Any markers force the {@link FastQueue} superclass to + * keep all elements until no more markers; then can reset to avoid growing a + * huge buffer. * </summary> */ public abstract class LookaheadStream<T> @@ -47,10 +50,13 @@ namespace Antlr.Runtime.Misc where T : class { /** Absolute token index. It's the index of the symbol about to be - * read via LT(1). Goes from 0 to numtokens. + * read via {@code LT(1)}. Goes from 0 to numtokens. */ private int _currentElementIndex = 0; + /** + * This is the {@code LT(-1)} element for the first element in {@link #data}. + */ private T _previousElement; /** Track object returned by nextElement upon end of stream; @@ -84,9 +90,9 @@ namespace Antlr.Runtime.Misc } } - public override void Clear() + public virtual void Reset() { - base.Clear(); + Clear(); _currentElementIndex = 0; _p = 0; _previousElement = null; @@ -94,14 +100,17 @@ namespace Antlr.Runtime.Misc /** <summary> * Implement nextElement to supply a stream of elements to this - * lookahead buffer. Return eof upon end of the stream we're pulling from. + * lookahead buffer. Return EOF upon end of the stream we're pulling from. * </summary> */ public abstract T NextElement(); public abstract bool IsEndOfFile(T o); - /** <summary>Get and remove first element in queue; override FastQueue.remove()</summary> */ + /** <summary> + * Get and remove first element in queue; override + * {@link FastQueue#remove()}; it's the same, just checks for backtracking. + * </summary> */ public override T Dequeue() { T o = this[0]; @@ -109,6 +118,7 @@ namespace Antlr.Runtime.Misc // have we hit end of buffer and not backtracking? if ( _p == _data.Count && _markDepth == 0 ) { + _previousElement = o; // if so, it's an opportunity to start filling at index 0 again Clear(); // size goes to 0, but retains memory } @@ -119,7 +129,7 @@ namespace Antlr.Runtime.Misc public virtual void Consume() { SyncAhead(1); - _previousElement = Dequeue(); + Dequeue(); _currentElementIndex++; } @@ -201,34 +211,61 @@ namespace Antlr.Runtime.Misc public virtual void Rewind( int marker ) { - Seek( marker ); - Release( marker ); + _markDepth--; + int delta = _p - marker; + _currentElementIndex -= delta; + _p = marker; } public virtual void Rewind() { - Rewind( _lastMarker ); + // rewind but do not release marker + int delta = _p - _lastMarker; + _currentElementIndex -= delta; + _p = _lastMarker; } /** <summary> - * Seek to a 0-indexed position within data buffer. Can't handle - * case where you seek beyond end of existing buffer. Normally used - * to seek backwards in the buffer. Does not force loading of nodes. - * Doesn't see to absolute position in input stream since this stream - * is unbuffered. Seeks only into our moving window of elements. + * Seek to a 0-indexed absolute token index. Normally used to seek backwards + * in the buffer. Does not force loading of nodes. * </summary> + * <remarks> + * To preserve backward compatibility, this method allows seeking past the + * end of the currently buffered data. In this case, the input pointer will + * be moved but the data will only actually be loaded upon the next call to + * {@link #consume} or {@link #LT} for {@code k>0}. + * </remarks> */ public virtual void Seek( int index ) { - _p = index; + if (index < 0) + throw new ArgumentOutOfRangeException("index"); + + int delta = _currentElementIndex - index; + if (_p - delta < 0) + throw new NotSupportedException("can't seek before the beginning of this stream's buffer"); + + _p -= delta; + _currentElementIndex = index; } protected virtual T LB(int k) { - if (k == 1) + Debug.Assert(k > 0); + + int index = _p - k; + if (index == -1) return _previousElement; - throw new ArgumentException("can't look backwards more than one token in this stream"); + // if k>0 then we know index < data.size(). avoid the double-check for + // performance. + if (index >= 0 /*&& index < data.size()*/) + return _data[index]; + + if (index < -1) + throw new NotSupportedException("can't look more than one token before the beginning of this stream's buffer"); + + throw new NotSupportedException("can't look past the end of this stream's buffer using LB(int)"); } } } diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime/NoViableAltException.cs b/runtime/CSharp3/Sources/Antlr3.Runtime/NoViableAltException.cs index 6478c6f..4f37001 100644 --- a/runtime/CSharp3/Sources/Antlr3.Runtime/NoViableAltException.cs +++ b/runtime/CSharp3/Sources/Antlr3.Runtime/NoViableAltException.cs @@ -1,10 +1,10 @@ /* - * [The "BSD licence"] - * Copyright (c) 2005-2008 Terence Parr + * [The "BSD license"] + * Copyright (c) 2011 Terence Parr * All rights reserved. * * Conversion to C#: - * Copyright (c) 2008 Sam Harwell, Pixel Mine, Inc. + * Copyright (c) 2011 Sam Harwell, Tunnel Vision Laboratories, LLC * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -66,7 +66,12 @@ namespace Antlr.Runtime } public NoViableAltException(string grammarDecisionDescription, int decisionNumber, int stateNumber, IIntStream input) - : base(input) + : this(grammarDecisionDescription, decisionNumber, stateNumber, input, 1) + { + } + + public NoViableAltException(string grammarDecisionDescription, int decisionNumber, int stateNumber, IIntStream input, int k) + : base(input, k) { this._grammarDecisionDescription = grammarDecisionDescription; this._decisionNumber = decisionNumber; @@ -74,7 +79,12 @@ namespace Antlr.Runtime } public NoViableAltException(string message, string grammarDecisionDescription, int decisionNumber, int stateNumber, IIntStream input) - : base(message, input) + : this(message, grammarDecisionDescription, decisionNumber, stateNumber, input, 1) + { + } + + public NoViableAltException(string message, string grammarDecisionDescription, int decisionNumber, int stateNumber, IIntStream input, int k) + : base(message, input, k) { this._grammarDecisionDescription = grammarDecisionDescription; this._decisionNumber = decisionNumber; @@ -82,7 +92,12 @@ namespace Antlr.Runtime } public NoViableAltException(string message, string grammarDecisionDescription, int decisionNumber, int stateNumber, IIntStream input, Exception innerException) - : base(message, input, innerException) + : this(message, grammarDecisionDescription, decisionNumber, stateNumber, input, 1, innerException) + { + } + + public NoViableAltException(string message, string grammarDecisionDescription, int decisionNumber, int stateNumber, IIntStream input, int k, Exception innerException) + : base(message, input, k, innerException) { this._grammarDecisionDescription = grammarDecisionDescription; this._decisionNumber = decisionNumber; diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime/Properties/AssemblyInfo.cs b/runtime/CSharp3/Sources/Antlr3.Runtime/Properties/AssemblyInfo.cs index 1489fde..cc3425f 100644 --- a/runtime/CSharp3/Sources/Antlr3.Runtime/Properties/AssemblyInfo.cs +++ b/runtime/CSharp3/Sources/Antlr3.Runtime/Properties/AssemblyInfo.cs @@ -41,9 +41,9 @@ using System.Security; [assembly: AssemblyTitle( "Antlr3.Runtime" )] [assembly: AssemblyDescription( "" )] [assembly: AssemblyConfiguration( "" )] -[assembly: AssemblyCompany( "Pixel Mine, Inc." )] +[assembly: AssemblyCompany( "Tunnel Vision Laboratories, LLC" )] [assembly: AssemblyProduct( "Antlr3.Runtime" )] -[assembly: AssemblyCopyright("Copyright © Sam Harwell 2011")] +[assembly: AssemblyCopyright("Copyright © Sam Harwell 2013")] [assembly: AssemblyTrademark( "" )] [assembly: AssemblyCulture( "" )] [assembly: CLSCompliant( true )] @@ -68,5 +68,5 @@ using System.Security; * the Major or Minor version is incremented. * 3. Revision is the Perforce changelist number associated with the release. */ -[assembly: AssemblyVersion("3.3.4.8517")] -[assembly: AssemblyFileVersion("3.3.4.8517")] +[assembly: AssemblyVersion("3.5.0.2")] +[assembly: AssemblyFileVersion("3.5.0.2")] diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime/RecognitionException.cs b/runtime/CSharp3/Sources/Antlr3.Runtime/RecognitionException.cs index f0c5662..007560d 100644 --- a/runtime/CSharp3/Sources/Antlr3.Runtime/RecognitionException.cs +++ b/runtime/CSharp3/Sources/Antlr3.Runtime/RecognitionException.cs @@ -1,10 +1,10 @@ /* - * [The "BSD licence"] - * Copyright (c) 2005-2008 Terence Parr + * [The "BSD license"] + * Copyright (c) 2011 Terence Parr * All rights reserved. * * Conversion to C#: - * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * Copyright (c) 2011 Sam Harwell, Tunnel Vision Laboratories, LLC * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -34,9 +34,9 @@ namespace Antlr.Runtime { using Antlr.Runtime.Tree; - using ArgumentException = System.ArgumentException; using ArgumentNullException = System.ArgumentNullException; using Exception = System.Exception; + using NotSupportedException = System.NotSupportedException; using SerializationInfo = System.Runtime.Serialization.SerializationInfo; using StreamingContext = System.Runtime.Serialization.StreamingContext; @@ -78,6 +78,11 @@ namespace Antlr.Runtime /** <summary>What input stream did the error occur in?</summary> */ private IIntStream _input; + /// <summary> + /// What was the lookahead index when this exception was thrown? + /// </summary> + private int _k; + /** <summary>What is index of token/char were we looking at when the error occurred?</summary> */ private int _index; @@ -127,8 +132,13 @@ namespace Antlr.Runtime { } - public RecognitionException( IIntStream input ) - : this("A recognition error occurred.", input, null) + public RecognitionException(IIntStream input) + : this("A recognition error occurred.", input, 1, null) + { + } + + public RecognitionException(IIntStream input, int k) + : this("A recognition error occurred.", input, k, null) { } @@ -138,7 +148,12 @@ namespace Antlr.Runtime } public RecognitionException(string message, IIntStream input) - : this(message, input, null) + : this(message, input, 1, null) + { + } + + public RecognitionException(string message, IIntStream input, int k) + : this(message, input, k, null) { } @@ -148,15 +163,21 @@ namespace Antlr.Runtime } public RecognitionException(string message, IIntStream input, Exception innerException) + : this(message, input, 1, innerException) + { + } + + public RecognitionException(string message, IIntStream input, int k, Exception innerException) : base(message, innerException) { this._input = input; + this._k = k; if (input != null) { - this._index = input.Index; + this._index = input.Index + k - 1; if (input is ITokenStream) { - this._token = ((ITokenStream)input).LT(1); + this._token = ((ITokenStream)input).LT(k); this._line = _token.Line; this._charPositionInLine = _token.CharPositionInLine; } @@ -164,20 +185,31 @@ namespace Antlr.Runtime ITreeNodeStream tns = input as ITreeNodeStream; if (tns != null) { - ExtractInformationFromTreeNodeStream(tns); + ExtractInformationFromTreeNodeStream(tns, k); } else { ICharStream charStream = input as ICharStream; if (charStream != null) { - this._c = input.LA(1); - this._line = ((ICharStream)input).Line; - this._charPositionInLine = ((ICharStream)input).CharPositionInLine; + int mark = input.Mark(); + try + { + for (int i = 0; i < k - 1; i++) + input.Consume(); + + this._c = input.LA(1); + this._line = ((ICharStream)input).Line; + this._charPositionInLine = ((ICharStream)input).CharPositionInLine; + } + finally + { + input.Rewind(mark); + } } else { - this._c = input.LA(1); + this._c = input.LA(k); } } } @@ -241,6 +273,14 @@ namespace Antlr.Runtime } } + public int Lookahead + { + get + { + return _k; + } + } + public IToken Token { get @@ -329,6 +369,19 @@ namespace Antlr.Runtime protected virtual void ExtractInformationFromTreeNodeStream(ITreeNodeStream input) { this._node = input.LT(1); + + object positionNode = null; + IPositionTrackingStream positionTrackingStream = input as IPositionTrackingStream; + if (positionTrackingStream != null) + { + positionNode = positionTrackingStream.GetKnownPositionElement(false); + if (positionNode == null) + { + positionNode = positionTrackingStream.GetKnownPositionElement(true); + this._approximateLineInfo = positionNode != null; + } + } + ITokenStreamInformation streamInformation = input as ITokenStreamInformation; if (streamInformation != null) { @@ -345,7 +398,7 @@ namespace Antlr.Runtime else { ITreeAdaptor adaptor = input.TreeAdaptor; - IToken payload = adaptor.GetToken(_node); + IToken payload = adaptor.GetToken(positionNode ?? _node); if (payload != null) { this._token = payload; @@ -365,12 +418,13 @@ namespace Antlr.Runtime this._approximateLineInfo = true; break; } + --i; try { priorNode = input.LT(i); } - catch (ArgumentException) + catch (NotSupportedException) { priorNode = null; } @@ -400,5 +454,21 @@ namespace Antlr.Runtime } } } + + protected virtual void ExtractInformationFromTreeNodeStream(ITreeNodeStream input, int k) + { + int mark = input.Mark(); + try + { + for (int i = 0; i < k - 1; i++) + input.Consume(); + + ExtractInformationFromTreeNodeStream(input); + } + finally + { + input.Rewind(mark); + } + } } } diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/BaseTree.cs b/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/BaseTree.cs index 79f3d97..9327860 100644 --- a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/BaseTree.cs +++ b/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/BaseTree.cs @@ -1,10 +1,10 @@ /* - * [The "BSD licence"] + * [The "BSD license"] * Copyright (c) 2011 Terence Parr * All rights reserved. * * Conversion to C#: - * Copyright (c) 2011 Sam Harwell, Pixel Mine, Inc. + * Copyright (c) 2011 Sam Harwell, Tunnel Vision Laboratories, LLC * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -286,6 +286,30 @@ namespace Antlr.Runtime.Tree t.ChildIndex = i; } + /** Insert child t at child position i (0..n-1) by shifting children + * i+1..n-1 to the right one position. Set parent / indexes properly + * but does NOT collapse nil-rooted t's that come in here like addChild. + */ + public virtual void InsertChild(int i, ITree t) + { + if (i < 0) + throw new ArgumentOutOfRangeException("i"); + if (i > ChildCount) + throw new ArgumentException(); + + if (i == ChildCount) + { + AddChild(t); + return; + } + + Children.Insert(i, t); + + // walk others to increment their child indexes + // set index, parent of this one too + this.FreshenParentAndChildIndexes(i); + } + public virtual object DeleteChild( int i ) { if (i < 0) @@ -428,6 +452,25 @@ namespace Antlr.Runtime.Tree } } + public virtual void FreshenParentAndChildIndexesDeeply() + { + FreshenParentAndChildIndexesDeeply(0); + } + + public virtual void FreshenParentAndChildIndexesDeeply(int offset) + { + int n = ChildCount; + for (int c = offset; c < n; c++) + { + ITree child = GetChild(c); + child.ChildIndex = c; + child.Parent = this; + BaseTree baseTree = child as BaseTree; + if (baseTree != null) + baseTree.FreshenParentAndChildIndexesDeeply(); + } + } + public virtual void SanityCheckParentAndChildIndexes() { SanityCheckParentAndChildIndexes( null, -1 ); diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/CommonTreeNodeStream.cs b/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/CommonTreeNodeStream.cs index 45c46be..f9cb0a7 100644 --- a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/CommonTreeNodeStream.cs +++ b/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/CommonTreeNodeStream.cs @@ -1,10 +1,10 @@ /* - * [The "BSD licence"] - * Copyright (c) 2005-2008 Terence Parr + * [The "BSD license"] + * Copyright (c) 2011 Terence Parr * All rights reserved. * * Conversion to C#: - * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * Copyright (c) 2011 Sam Harwell, Pixel Mine, Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -36,35 +36,45 @@ namespace Antlr.Runtime.Tree using Antlr.Runtime.Misc; using StringBuilder = System.Text.StringBuilder; - using NotSupportedException = System.NotSupportedException; [System.Serializable] - public class CommonTreeNodeStream : LookaheadStream<object>, ITreeNodeStream + public class CommonTreeNodeStream : LookaheadStream<object>, ITreeNodeStream, IPositionTrackingStream { public const int DEFAULT_INITIAL_BUFFER_SIZE = 100; public const int INITIAL_CALL_STACK_SIZE = 10; /** <summary>Pull nodes from which tree?</summary> */ - object _root; + private readonly object _root; /** <summary>If this tree (root) was created from a token stream, track it.</summary> */ protected ITokenStream tokens; /** <summary>What tree adaptor was used to build these trees</summary> */ [System.NonSerialized] - ITreeAdaptor _adaptor; + private ITreeAdaptor _adaptor; /** The tree iterator we are using */ - TreeIterator _it; + private readonly TreeIterator _it; /** <summary>Stack of indexes used for push/pop calls</summary> */ - Stack<int> _calls; + private Stack<int> _calls; /** <summary>Tree (nil A B C) trees like flat A B C streams</summary> */ - bool _hasNilRoot = false; + private bool _hasNilRoot = false; /** <summary>Tracks tree depth. Level=0 means we're at root node level.</summary> */ - int _level = 0; + private int _level = 0; + + /** + * Tracks the last node before the start of {@link #data} which contains + * position information to provide information for error reporting. This is + * tracked in addition to {@link #prevElement} which may or may not contain + * position information. + * + * @see #hasPositionInformation + * @see RecognitionException#extractInformationFromTreeNodeStream + */ + private object _previousLocationElement; public CommonTreeNodeStream( object tree ) : this( new CommonTreeAdaptor(), tree ) @@ -97,6 +107,7 @@ namespace Antlr.Runtime.Tree { return tokens; } + set { tokens = value; @@ -138,12 +149,13 @@ namespace Antlr.Runtime.Tree #endregion - public virtual void Reset() + public override void Reset() { - base.Clear(); + base.Reset(); _it.Reset(); _hasNilRoot = false; _level = 0; + _previousLocationElement = null; if ( _calls != null ) _calls.Clear(); } @@ -181,6 +193,15 @@ namespace Antlr.Runtime.Tree return t; } + public override object Dequeue() + { + object result = base.Dequeue(); + if (_p == 0 && HasPositionInformation(PreviousElement)) + _previousLocationElement = PreviousElement; + + return result; + } + public override bool IsEndOfFile(object o) { return TreeAdaptor.GetType(o) == CharStreamConstants.EndOfFile; @@ -197,9 +218,8 @@ namespace Antlr.Runtime.Tree public virtual void Push( int index ) { if ( _calls == null ) - { _calls = new Stack<int>(); - } + _calls.Push( _p ); // save current index Seek( index ); } @@ -214,6 +234,44 @@ namespace Antlr.Runtime.Tree return ret; } + /** + * Returns an element containing position information. If {@code allowApproximateLocation} is {@code false}, then + * this method will return the {@code LT(1)} element if it contains position information, and otherwise return {@code null}. + * If {@code allowApproximateLocation} is {@code true}, then this method will return the last known element containing position information. + * + * @see #hasPositionInformation + */ + public object GetKnownPositionElement(bool allowApproximateLocation) + { + object node = _data[_p]; + if (HasPositionInformation(node)) + return node; + + if (!allowApproximateLocation) + return null; + + for (int index = _p - 1; index >= 0; index--) + { + node = _data[index]; + if (HasPositionInformation(node)) + return node; + } + + return _previousLocationElement; + } + + public bool HasPositionInformation(object node) + { + IToken token = TreeAdaptor.GetToken(node); + if (token == null) + return false; + + if (token.Line <= 0) + return false; + + return true; + } + #region Tree rewrite interface public virtual void ReplaceChildren( object parent, int startChildIndex, int stopChildIndex, object t ) diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/IPositionTrackingStream.cs b/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/IPositionTrackingStream.cs new file mode 100644 index 0000000..8bc8945 --- /dev/null +++ b/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/IPositionTrackingStream.cs @@ -0,0 +1,59 @@ +/* + [The "BSD license"] + Copyright (c) 2012 Terence Parr + Copyright (c) 2012 Sam Harwell + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the author may not be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace Antlr.Runtime.Tree +{ + /** + * + * @author Sam Harwell + */ + public interface IPositionTrackingStream + { + /** + * Returns an element containing concrete information about the current + * position in the stream. + * + * @param allowApproximateLocation if {@code false}, this method returns + * {@code null} if an element containing exact information about the current + * position is not available + */ + object GetKnownPositionElement(bool allowApproximateLocation); + + /** + * Determines if the specified {@code element} contains concrete position + * information. + * + * @param element the element to check + * @return {@code true} if {@code element} contains concrete position + * information, otherwise {@code false} + */ + bool HasPositionInformation(object element); + + } +} diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/ITreeNodeStream.cs b/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/ITreeNodeStream.cs index b133f39..8f3f30a 100644 --- a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/ITreeNodeStream.cs +++ b/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/ITreeNodeStream.cs @@ -47,18 +47,18 @@ namespace Antlr.Runtime.Tree } /** <summary> - * Get tree node at current input pointer + i ahead where i=1 is next node. - * i<0 indicates nodes in the past. So LT(-1) is previous node, but - * implementations are not required to provide results for k < -1. - * LT(0) is undefined. For i>=n, return null. - * Return null for LT(0) and any index that results in an absolute address - * that is negative. + * Get tree node at current input pointer + {@code k} ahead where + * {@code k==1} is next node. {@code k<0} indicates nodes in the past. So + * {@code LT(-1)} is previous node, but implementations are not required to + * provide results for {@code k < -1}. {@code LT(0)} is undefined. For + * {@code k<=n}, return {@code null}. Return {@code null} for {@code LT(0)} + * and any index that results in an absolute address that is negative. * </summary> * * <remarks> - * This is analogus to the LT() method of the TokenStream, but this - * returns a tree node instead of a token. Makes code gen identical - * for both parser and tree grammars. :) + * This is analogous to {@link TokenStream#LT}, but this returns a tree node + * instead of a {@link Token}. Makes code generation identical for both + * parser and tree grammars. * </remarks> */ object LT( int k ); @@ -74,10 +74,11 @@ namespace Antlr.Runtime.Tree } /** <summary> - * If the tree associated with this stream was created from a TokenStream, - * you can specify it here. Used to do rule $text attribute in tree - * parser. Optional unless you use tree parser rule text attribute - * or output=template and rewrite=true options. + * If the tree associated with this stream was created from a + * {@link TokenStream}, you can specify it here. Used to do rule + * {@code $text} attribute in tree parser. Optional unless you use tree + * parser rule {@code $text} attribute or {@code output=template} and + * {@code rewrite=true} options. * </summary> */ ITokenStream TokenStream @@ -96,11 +97,11 @@ namespace Antlr.Runtime.Tree } /** <summary> - * As we flatten the tree, we use UP, DOWN nodes to represent - * the tree structure. When debugging we need unique nodes - * so we have to instantiate new ones. When doing normal tree - * parsing, it's slow and a waste of memory to create unique - * navigation nodes. Default should be false; + * As we flatten the tree, we use {@link Token#UP}, {@link Token#DOWN} nodes + * to represent the tree structure. When debugging we need unique nodes so + * we have to instantiate new ones. When doing normal tree parsing, it's + * slow and a waste of memory to create unique navigation nodes. Default + * should be {@code false}. * </summary> */ bool UniqueNavigationNodes @@ -110,11 +111,11 @@ namespace Antlr.Runtime.Tree } /** <summary> - * Return the text of all nodes from start to stop, inclusive. - * If the stream does not buffer all the nodes then it can still - * walk recursively from start until stop. You can always return - * null or "" too, but users should not access $ruleLabel.text in - * an action of course in that case. + * Return the text of all nodes from {@code start} to {@code stop}, + * inclusive. If the stream does not buffer all the nodes then it can still + * walk recursively from start until stop. You can always return + * {@code null} or {@code ""} too, but users should not access + * {@code $ruleLabel.text} in an action of course in that case. * </summary> */ string ToString( object start, object stop ); @@ -123,17 +124,18 @@ namespace Antlr.Runtime.Tree #region REWRITING TREES (used by tree parser) /** <summary> - * Replace from start to stop child index of parent with t, which might - * be a list. Number of children may be different - * after this call. The stream is notified because it is walking the - * tree and might need to know you are monkeying with the underlying - * tree. Also, it might be able to modify the node stream to avoid - * restreaming for future phases. + * Replace children of {@code parent} from index {@code startChildIndex} to + * {@code stopChildIndex} with {@code t}, which might be a list. Number of + * children may be different after this call. The stream is notified because + * it is walking the tree and might need to know you are monkeying with the + * underlying tree. Also, it might be able to modify the node stream to + * avoid restreaming for future phases. * </summary> * * <remarks> - * If parent is null, don't do anything; must be at root of overall tree. - * Can't replace whatever points to the parent externally. Do nothing. + * If {@code parent} is {@code null}, don't do anything; must be at root of + * overall tree. Can't replace whatever points to the parent externally. Do + * nothing. * </remarks> */ void ReplaceChildren( object parent, int startChildIndex, int stopChildIndex, object t ); diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/RewriteRuleElementStream.cs b/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/RewriteRuleElementStream.cs index 8e3d5b0..cb76ace 100644 --- a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/RewriteRuleElementStream.cs +++ b/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/RewriteRuleElementStream.cs @@ -71,10 +71,7 @@ namespace Antlr.Runtime.Tree /** <summary>Once a node / subtree has been used in a stream, it must be dup'd * from then on. Streams are reset after subrules so that the streams * can be reused in future subrules. So, reset must set a dirty bit. - * If dirty, then next() always returns a dup. - * - * I wanted to use "naughty bit" here, but couldn't think of a way - * to use "naughty". + * If dirty, then next() always returns a dup.</summary> */ protected bool dirty = false; diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/TreeFilter.cs b/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/TreeFilter.cs index ef7b412..ba44e2d 100644 --- a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/TreeFilter.cs +++ b/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/TreeFilter.cs @@ -58,8 +58,8 @@ namespace Antlr.Runtime.Tree try { // share TreeParser object but not parsing-related state - state = new RecognizerSharedState(); - input = new CommonTreeNodeStream( originalAdaptor, t ); + SetState(new RecognizerSharedState()); + SetTreeNodeStream(new CommonTreeNodeStream(originalAdaptor, t)); ( (CommonTreeNodeStream)input ).TokenStream = originalTokenStream; BacktrackingLevel = 1; whichRule(); diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/TreeParser.cs b/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/TreeParser.cs index 927ee23..f5a1508 100644 --- a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/TreeParser.cs +++ b/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/TreeParser.cs @@ -58,13 +58,13 @@ namespace Antlr.Runtime.Tree public TreeParser( ITreeNodeStream input ) : base() // highlight that we go to super to set state object { - SetTreeNodeStream( input ); + this.input = input; } public TreeParser( ITreeNodeStream input, RecognizerSharedState state ) : base( state ) // share the state object with another parser { - SetTreeNodeStream( input ); + this.input = input; } public override void Reset() diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/TreeRewriter.cs b/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/TreeRewriter.cs index b610c2c..16a38a2 100644 --- a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/TreeRewriter.cs +++ b/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/TreeRewriter.cs @@ -67,8 +67,8 @@ namespace Antlr.Runtime.Tree try { // share TreeParser object but not parsing-related state - state = new RecognizerSharedState(); - input = new CommonTreeNodeStream( originalAdaptor, t ); + SetState(new RecognizerSharedState()); + SetTreeNodeStream(new CommonTreeNodeStream(originalAdaptor, t)); ( (CommonTreeNodeStream)input ).TokenStream = originalTokenStream; BacktrackingLevel = 1; IAstRuleReturnScope r = whichRule(); @@ -119,12 +119,12 @@ namespace Antlr.Runtime.Tree // methods the downup strategy uses to do the up and down rules. // to override, just define tree grammar rule topdown and turn on // filter=true. - public virtual IAstRuleReturnScope Topdown() + protected virtual IAstRuleReturnScope Topdown() { return null; } - public virtual IAstRuleReturnScope Bottomup() + protected virtual IAstRuleReturnScope Bottomup() { return null; } |