From 98ae1a1499db9801bbd73cef338550eea3d52df7 Mon Sep 17 00:00:00 2001 From: Alexander Luzgarev Date: Thu, 21 Apr 2022 18:12:26 +0200 Subject: [PATCH] Fix infinite loop when parsing parameter list This fixes a bug reported by David Garrison: on encountering the new function argument validation syntax, the parser went into an infinite loop. --- Parser.Tests/MParserShould.cs | 30 ++++++++++++++++++++++++++++++ Parser/Internal/MParserGreen.cs | 4 ++++ 2 files changed, 34 insertions(+) diff --git a/Parser.Tests/MParserShould.cs b/Parser.Tests/MParserShould.cs index 7aa648d..0a47f54 100644 --- a/Parser.Tests/MParserShould.cs +++ b/Parser.Tests/MParserShould.cs @@ -1,5 +1,6 @@ using Xunit; using FluentAssertions; +using System; namespace Parser.Tests { @@ -150,5 +151,34 @@ namespace Parser.Tests rhs.Span.Start.Should().Be(16); rhs.Span.End.Should().Be(17); } + + [Fact] + public void NotHangOnUnknownSyntax() + { + var text = @" +classdef myClass + properties + Channel; + NodeID; + Node; + end + methods + function this = sendData(this, arg1, arg2) + arguments + this (1,1) myClass + arg1 (1,1) double {mustBeNonnegative} + arg2 (1,1) double {mustBeNonnegative} + end + If (arg1 = 0) + this.NodeID = 3; + end + end + function this = getData(this, arg1, arg2) + end +end"; + var sut = GetSut(text); + Func action = sut.Parse; + action.Should().Throw(); + } } } \ No newline at end of file diff --git a/Parser/Internal/MParserGreen.cs b/Parser/Internal/MParserGreen.cs index 32851ba..f9d3480 100644 --- a/Parser/Internal/MParserGreen.cs +++ b/Parser/Internal/MParserGreen.cs @@ -170,6 +170,10 @@ namespace Parser.Internal { var identifierToken = EatToken(TokenKind.IdentifierToken); builder.Add(Factory.IdentifierNameExpressionSyntax(identifierToken)); + if (identifierToken.IsMissing) + { + break; + } } }