Best Gherkin-dotnet code snippet using Gherkin.UnexpectedEOFException.CompositeParserException
Parser.cs
Source:Parser.cs  
...118            } while(!token.IsEOF);119            EndRule(context, RuleType.GherkinDocument);120            if (context.Errors.Count > 0)121            {122                throw new CompositeParserException(context.Errors.ToArray());123            }124            return GetResult(context);125        }126        private void AddError(ParserContext context, ParserException error)127        {128            context.Errors.Add(error);129            if (context.Errors.Count > 10)130                throw new CompositeParserException(context.Errors.ToArray());131        }132        private void HandleAstError(ParserContext context, Action action)133        {134            HandleExternalError(context, () => { action(); return true; });135        }136        private T HandleExternalError<T>(ParserContext context, Func<T> action, T defaultValue = default(T))137        {138            if (StopAtFirstError)139            {140                return action();141            }142            try143            {144                return action();145            }146            catch (CompositeParserException compositeParserException)147            {148                foreach (var error in compositeParserException.Errors)149                    AddError(context, error);150            }151            catch (ParserException error)152            {153                AddError(context, error);154            }155            return defaultValue;156        }157        void Build(ParserContext context, Token token)158        {159            HandleAstError(context, () => this.astBuilder.Build(token));160        }...DeveroomGherkinParser.cs
Source:DeveroomGherkinParser.cs  
...33            catch (ParserException parserException)34            {35                logger.LogVerbose($"ParserErrors: {parserException.Message}");36                gherkinDocument = GetResultOfInvalid();37                if (parserException is CompositeParserException compositeParserException)38                    parserErrors.AddRange(compositeParserException.Errors);39                else40                    parserErrors.Add(parserException);41            }42            catch (Exception e)43            {44                logger.LogException(_monitoringService, e, "Exception during Gherkin parsing");45                gherkinDocument = GetResult();46            }47            return false;48        }49        private DeveroomGherkinDocument GetResultOfInvalid()50        {51            // trying to "finish" open nodes by sending dummy <endrule> messages up to 5 levels of nesting52            for (int i = 0; i < 10; i++)53            {54                var result = GetResult();55                if (result != null)56                    return result;57                try58                {59                    AstBuilder.EndRule(RuleType.None);60                }61                catch (Exception)62                {63                }64            }65            return null;66        }67        public DeveroomGherkinDocument Parse(TextReader featureFileReader, string sourceFilePath)68        {69            var tokenScanner = (ITokenScanner)new HotfixTokenScanner(featureFileReader);70            var tokenMatcher = new TokenMatcher(DialectProvider);71            _astBuilder = new DeveroomGherkinAstBuilder(sourceFilePath, () => tokenMatcher.CurrentDialect);72            var parser = new InternalParser(_astBuilder, AstBuilder.RecordStateForLine, _monitoringService);73            var gherkinDocument = parser.Parse(tokenScanner, tokenMatcher);74            CheckSemanticErrors(gherkinDocument);75            return gherkinDocument;76        }77        class InternalParser : Parser<DeveroomGherkinDocument>78        {79            private readonly Action<int, int> _recordStateForLine;80            private readonly IMonitoringService _monitoringService;81            public InternalParser(IAstBuilder<DeveroomGherkinDocument> astBuilder, Action<int, int> recordStateForLine, IMonitoringService monitoringService)82                : base(astBuilder)83            {84                _recordStateForLine = recordStateForLine;85                _monitoringService = monitoringService;86            }87            public int NullMatchToken(int state, Token token)88            {89                return MatchToken(state, token, new ParserContext()90                {91                    Errors = new List<ParserException>(),92                    TokenMatcher = new AllFalseTokenMatcher(),93                    TokenQueue = new Queue<Token>(),94                    TokenScanner = new NullTokenScanner()95                });96            }97            protected override int MatchToken(int state, Token token, ParserContext context)98            {99                _recordStateForLine?.Invoke(token.Location.Line, state);100                try101                {102                    return base.MatchToken(state, token, context);103                }104                catch (InvalidOperationException ex)105                {106                    _monitoringService.MonitorError(ex);107                    throw;108                }109            }110        }111        public DeveroomGherkinDocument GetResult()112        {113            return _astBuilder.GetResult();114        }115        #region Semantic Errors116        protected virtual void CheckSemanticErrors(DeveroomGherkinDocument specFlowDocument)117        {118            var errors = new List<ParserException>();119            errors.AddRange(((DeveroomGherkinAstBuilder)_astBuilder).Errors);120            if (specFlowDocument?.Feature != null)121            {122                CheckForDuplicateScenarios(specFlowDocument.Feature, errors);123                CheckForDuplicateExamples(specFlowDocument.Feature, errors);124                CheckForMissingExamples(specFlowDocument.Feature, errors);125                CheckForRulesPreSpecFlow31(specFlowDocument.Feature, errors);126            }127            // collect128            if (errors.Count == 1)129                throw errors[0];130            if (errors.Count > 1)131                throw new CompositeParserException(errors.ToArray());132        }133        private void CheckForRulesPreSpecFlow31(Feature feature, List<ParserException> errors)134        {135            //TODO: Show error when Rule keyword is used in SpecFlow v3.0 or earlier136        }137        private void CheckForDuplicateScenarios(Feature feature, List<ParserException> errors)138        {139            // duplicate scenario name140            var duplicatedScenarios = feature.FlattenScenarioDefinitions().GroupBy(sd => sd.Name, sd => sd).Where(g => g.Count() > 1).ToArray();141            errors.AddRange(142                duplicatedScenarios.Select(g =>143                    new SemanticParserException(144                        $"Feature file already contains a scenario with name '{g.Key}'",145                        g.ElementAt(1).Location)));...ParserException.cs
Source:ParserException.cs  
...114        {115        }116    }117    [Serializable]118    public class CompositeParserException : ParserException119    {120        public IEnumerable<ParserException> Errors { get; private set; }121        public CompositeParserException(ParserException[] errors)122            : base(GetMessage(errors))123        {124            if (errors == null) throw new ArgumentNullException("errors");125            Errors = errors;126        }127        private static string GetMessage(ParserException[] errors)128        {129            return "Parser errors:" + Environment.NewLine + string.Join(Environment.NewLine, errors.Select(e => e.Message));130        }131        protected CompositeParserException(SerializationInfo info, StreamingContext context) : base(info, context)132        {133            Errors = (ParserException[])info.GetValue("Errors", typeof (ParserException[]));134        }135        public override void GetObjectData(SerializationInfo info, StreamingContext context)136        {137            base.GetObjectData(info, context);138            info.AddValue("Errors", Errors.ToArray());139        }140    }141}...ParserErrorSerializationTests.cs
Source:ParserErrorSerializationTests.cs  
...94            Assert.IsNull(deserializedException.StateComment);95        }9697        [Test]98        public void CompositeParserExceptionShouldBeSerializable()99        {100            var exception = new CompositeParserException(new ParserException[]101            {102                new AstBuilderException("sample message", new Location(1, 2)), new NoSuchLanguageException("sample message")103            });104105            var deserializedException = SerializeDeserialize(exception);106107            Assert.AreEqual(exception.Message, deserializedException.Message);108109            // the custom details are not serialized (yet?)110            Assert.IsNotNull(deserializedException.Errors);111            Assert.AreEqual(exception.Errors.Count(), deserializedException.Errors.Count());112            Assert.IsInstanceOf<AstBuilderException>(exception.Errors.First());113            Assert.IsInstanceOf<NoSuchLanguageException>(exception.Errors.Last());114        }
...CompositeParserException
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using Gherkin;7{8    {9        static void Main(string[] args)10        {11            {12                var parser = new Parser();13                var feature = parser.Parse("Feature: Test");14            }15            catch (UnexpectedEOFException ex)16            {17                Console.WriteLine(ex.CompositeParserException.Message);18            }19        }20    }21}22using System;23using System.Collections.Generic;24using System.Linq;25using System.Text;26using System.Threading.Tasks;27using Gherkin;28{29    {30        static void Main(string[] args)31        {32            {33                var parser = new Parser();34                var feature = parser.Parse("Feature: Test");35            }36            catch (ParserException ex)37            {38                Console.WriteLine(ex.CompositeParserException.Message);39            }40        }41    }42}43using System;44using System.Collections.Generic;45using System.Linq;46using System.Text;47using System.Threading.Tasks;48using Gherkin;49{50    {51        static void Main(string[] args)52        {53            {54                var parser = new Parser();55                var feature = parser.Parse("Feature: Test");56            }57            catch (ParserException ex)58            {59                Console.WriteLine(ex.CompositeParserException.Message);60            }61        }62    }63}64using System;65using System.Collections.Generic;66using System.Linq;67using System.Text;68using System.Threading.Tasks;69using Gherkin;70{71    {72        static void Main(string[] args)73        {74            {75                var parser = new Parser();76                var feature = parser.Parse("Feature: Test");77            }78            catch (ParserException ex)79            {80                Console.WriteLine(ex.CompositeParserException.Message);81            }82        }83    }84}85using System;86using System.Collections.Generic;CompositeParserException
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using Gherkin;7{8    {9        static void Main(string[] args)10        {11            {12                Parser parser = new Parser();13                parser.Parse("Feature: Testing", "Testing.feature");14            }15            catch (UnexpectedEOFException ex)16            {17                Console.WriteLine(ex.CompositeParserException.Message);18            }19        }20    }21}22using System;23using System.Collections.Generic;24using System.Linq;25using System.Text;26using System.Threading.Tasks;27using Gherkin;28{29    {30        static void Main(string[] args)31        {32            {33                Parser parser = new Parser();34                parser.Parse("Feature: Testing", "Testing.feature");35            }36            catch (UnexpectedEOFException ex)37            {38                Console.WriteLine(ex.CompositeParserException.Message);39                Console.WriteLine("Line Number: " + ex.CompositeParserException.Line);40                Console.WriteLine("Column Number: " + ex.CompositeParserException.Column);41            }42        }43    }44}CompositeParserException
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using Gherkin;7{8    {9        static void Main(string[] args)10        {11            UnexpectedEOFException obj = new UnexpectedEOFException();12            obj.CompositeParserException();13        }14    }15}16using System;17using System.Collections.Generic;18using System.Linq;19using System.Text;20using System.Threading.Tasks;21using Gherkin;22{23    {24        static void Main(string[] args)25        {26            UnexpectedTokenException obj = new UnexpectedTokenException();27            obj.CompositeParserException();28        }29    }30}31using System;32using System.Collections.Generic;33using System.Linq;34using System.Text;35using System.Threading.Tasks;36using Gherkin;37{38    {39        static void Main(string[] args)40        {41            ParserException obj = new ParserException();42            obj.CompositeParserException();43        }44    }45}46using System;47using System.Collections.Generic;48using System.Linq;49using System.Text;50using System.Threading.Tasks;51using Gherkin;52{53    {54        static void Main(string[] args)55        {56            AstBuilder obj = new AstBuilder();57            obj.CompositeParserException();58        }59    }60}61using System;62using System.Collections.Generic;63using System.Linq;64using System.Text;65using System.Threading.Tasks;66using Gherkin;67{68    {69        static void Main(string[] args)70        {71            Parser obj = new Parser();72            obj.CompositeParserException();73        }74    }75}76using System;77using System.Collections.Generic;78using System.Linq;79using System.Text;80using System.Threading.Tasks;81using Gherkin;CompositeParserException
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6{7    {8        static void Main(string[] args)9        {10            {11                var parser = new Parser();12                var feature = parser.Parse("Feature: test13Given test");14            }15            catch (Gherkin.UnexpectedEOFException ex)16            {17                var compositeException = ex.CompositeParserException;18                Console.WriteLine(compositeException.Message);19            }20        }21    }22}CompositeParserException
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using Gherkin;7using System.IO;8using System.Reflection;9{10    {11        static void Main(string[] args)12        {13            {14                string filePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\Test.feature";15                string fileContent = File.ReadAllText(filePath);16                Gherkin.Parser parser = new Gherkin.Parser();17                parser.Parse(fileContent, filePath, 0);18            }19            catch (Gherkin.UnexpectedEOFException ex)20            {21                var compositeParserException = ex.CompositeParserException;22            }23        }24    }25}CompositeParserException
Using AI Code Generation
1using System;2using Gherkin.Ast;3using Gherkin.Parser;4using Gherkin.Parser.Exceptions;5{6    {7        static void Main(string[] args)8        {9";10            GherkinDialectProvider provider = new GherkinDialectProvider();11            Parser<GherkinDocument> parser = new Parser<GherkinDocument>(provider);12            {13                parser.Parse(feature);14            }15            catch (CompositeParserException e)16            {17                foreach (var parserException in e.ParserExceptions)18                {19                    Console.WriteLine(parserException.Message);20                }21            }22            Console.ReadKey();23        }24    }25}CompositeParserException
Using AI Code Generation
1using System;2using Gherkin;3{4    {5        static void Main(string[] args)6        {7Given I have a step";8            var parser = new Parser();9            parser.Parse(featureText);10        }11    }12}13using System;14using Gherkin;15{16    {17        static void Main(string[] args)18        {19Given I have a step";20            var parser = new Parser();21            parser.Parse(featureText);22        }23    }24}25using System;26using Gherkin;27{28    {29        static void Main(string[] args)30        {31Given I have a step";32            var parser = new Parser();33            parser.Parse(featureText);34        }35    }36}37using System;38using Gherkin;39{40    {41        static void Main(string[] args)42        {43Given I have a step";44            var parser = new Parser();45            parser.Parse(featureText);46        }47    }48}49using System;50using Gherkin;51{52    {53        static void Main(string[] args)54        {55Given I have a step";56            var parser = new Parser();57            parser.Parse(featureText);58        }59    }60}61using System;62using Gherkin;63{64    {65        static void Main(stringCompositeParserException
Using AI Code Generation
1using System;2using System.Collections.Generic;3using Gherkin;4using Gherkin.Ast;5{6    {7        static void Main(string[] args)8        {9            {10                var parser = new Parser();11                var feature = parser.Parse(@"Feature: test12            }13            catch (Gherkin.UnexpectedEOFException e)14            {15                foreach(var exception in e.CompositeParserException.ParserExceptions)16                {17                    Console.WriteLine(exception.Message);18                }19            }20        }21    }22}Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
