public void OneOfReportsCorrectError() { var names = new[] { "one", "two" }; TextParser <TextSpan> p = Parse.OneOf(names.Select(Span.EqualTo).ToArray()); AssertParser.SucceedsWith(p.Select(t => t.ToStringValue()), "one", "one"); AssertParser.SucceedsWith(p.Select(t => t.ToStringValue()), "two", "two"); AssertParser.FailsWithMessage(p.Select(t => t.ToStringValue()), "four", "Syntax error (line 1, column 1): unexpected `f`, expected `one` or `two`."); }
public void Parse_Unary_Not_Symbol_Success(string input, string expectedSymbol) { AssertParser.SucceedsWith(Parsers.LogicalUnary, input, actualLogical => { Assert.Equal(LogicalOperatorKind.Not, actualLogical.OperatorKind); Symbol rightSymbol = Assert.IsType <Symbol>(actualLogical.Right); Assert.Equal(expectedSymbol, rightSymbol.Name); }); }
public void Parse_FunctionCall_Success(string input, string expectedFunctionName) { AssertParser.SucceedsWith(Parsers.Evaluation, input, actualEvaluation => { Assert.Equal(1, actualEvaluation.Children.Count); FunctionCall actualFunctionCall = Assert.IsType <FunctionCall>(actualEvaluation.Children[0]); Assert.Equal(expectedFunctionName, actualFunctionCall.Name); }); }
public void ErrorMessageFromLastPartialItemPropagates() { var atom = Token.EqualTo(SExpressionToken.Atom); var number = Token.EqualTo(SExpressionToken.Number); var alternating = number.Then(n => atom).Many().AtEnd(); AssertParser.FailsWithMessage(alternating, "123 abc 123 123", new SExpressionTokenizer(), "Syntax error (line 1, column 13): unexpected number `123`, expected atom."); }
public void Parse_Symbol_Success(string input, string expectedSymbolName) { AssertParser.SucceedsWith(Parsers.Evaluation, input, actualEvaluation => { Assert.Equal(1, actualEvaluation.Children.Count); Symbol actualSymbol = Assert.IsType <Symbol>(actualEvaluation.Children[0]); Assert.Equal(expectedSymbolName, actualSymbol.Name); }); }
public void Sequence2TokenSucceeds() { // Issue - explicit tuple argument types are needed here; see: // https://github.com/dotnet/csharplang/issues/258 // Keeping this instance as an example, but using the "cleaner" .Item1, .Item2 syntax below. var p = Parse.Sequence(Token.EqualTo('1'), Token.EqualTo('w')) .Select(((Token <char> a, Token <char> b)t) => new [] { t.a, t.b }); AssertParser.SucceedsWithAll(p, "1w"); }
public void ErrorMessageFromIncompleteItemPropagates() { var atom = Token.EqualTo(SExpressionToken.Atom); var number = Token.EqualTo(SExpressionToken.Number); var alternating = number.Then(n => atom).AtEnd(); AssertParser.FailsWithMessage(alternating, "123", new SExpressionTokenizer(), "Syntax error: unexpected end of input, expected atom."); }
public void Parse_Logical_Success(string input, ExpressionKind expectedRootExpressionKind) { AssertParser.SucceedsWith(Parsers.Root, input, actualRoot => { Assert.Equal(1, actualRoot.Children.Count); ExpressionNode actualExpression = actualRoot.Children[0]; Assert.Equal(expectedRootExpressionKind, actualExpression.Kind); }); }
public void ErrorMessagesFromAppliedCharacterParsersPropagate() { var number = Token.EqualTo(SExpressionToken.Number) .Apply(t => Character.EqualTo('1').Then(_ => Character.EqualTo('x'))); var numbers = number.AtEnd(); AssertParser.FailsWithMessage(numbers, "123", new SExpressionTokenizer(), "Syntax error (line 1, column 2): invalid number, unexpected `2`, expected `x`."); }
public void TokenChainFailWithMultiTokenOperator() { // Addition is represented with operator '++' // If we only have one '+', ensure we get error var nPlusPlusN = Parse.Chain( Token.EqualTo('+').IgnoreThen(Token.EqualTo('+')), Token.EqualTo('1').Value(1), (opr, val1, val2) => val1 + val2); AssertParser.FailsAt(nPlusPlusN, "1+1", 2); }
public void Parse_StaticMethod_SingleArgument_String_Success(string input, ExpressionKind expectedArgumentKind) { AssertParser.SucceedsWith(Parsers.FunctionCalls.StaticMethod, input, actualFunctionCall => { Assert.Equal(FunctionKind.StaticMethod, actualFunctionCall.FunctionKind); Assert.Collection(actualFunctionCall.Arguments, actualArgument => { Assert.Equal(expectedArgumentKind, actualArgument.Kind); }); }); }
public void SuccessWithLongChains() { const int chainLength = 5000; string input = string.Join("+", Enumerable.Repeat("1", chainLength)); var chainParser = Parse.Chain( Character.EqualTo('+'), Numerics.IntegerInt32, (opr, val1, val2) => val1 + val2); AssertParser.SucceedsWith(chainParser, input, chainLength); }
public void SuccessLeftAssociativeChain() { const string input = "A.1.2.3"; var seed = Character.EqualTo('A').Select(i => System.Collections.Immutable.ImmutableList.Create <int>()); var chainParser = seed.Chain( Character.EqualTo('.'), Numerics.IntegerInt32, (o, r, i) => r.Add(i)); AssertParser.SucceedsWith(chainParser, input, System.Collections.Immutable.ImmutableList.Create(1, 2, 3)); }
public void ChainFailWithMultiTokenOperator() { // Addition is represented with operator '++' // If we only have one '+', ensure we get error var nPlusPlusN = Parse.Chain( Character.EqualTo('+').IgnoreThen(Character.EqualTo('+')), Numerics.IntegerInt32, (opr, val1, val2) => val1 + val2); AssertParser.FailsAt(nPlusPlusN, "1+1", 2); }
public void Parse_Unqualified_Success(string input, string expectedMetadataName) { AssertParser.SucceedsWith(Parsers.ItemMetadata, input, actualItemMetadata => { actualItemMetadata.PostParse( new TextPositions(input) ); Assert.Equal(expectedMetadataName, actualItemMetadata.Name); }); }
public void TokenSuccessWithLongChains() { const int chainLength = 5000; string input = string.Join("+", Enumerable.Repeat("1", chainLength)); var chainParser = Parse.Chain( Token.EqualTo('+'), Token.EqualTo('1').Value(1), (opr, val1, val2) => val1 + val2); AssertParser.SucceedsWith(chainParser, input, chainLength); }
public void Parse_Eval_Symbol_Success(string input, string expectedSymbolName) { AssertParser.SucceedsWith(Parsers.QuotedString, input, actualQuotedString => { Evaluate evaluation = actualQuotedString.Evaluations.FirstOrDefault(); Assert.NotNull(evaluation); Symbol symbol = evaluation.Children.OfType <Symbol>().FirstOrDefault(); Assert.NotNull(symbol); Assert.Equal(expectedSymbolName, symbol.Name); }); }
public void FindDeepestNode_Success(string input, int absolutePosition, ExpressionKind expectedExpressionKind, ExpressionKind?parentExpressionKind) { AssertParser.SucceedsWith(Parsers.Root, input, actualExpression => { actualExpression.PostParse( new TextPositions(input) ); ExpressionNode actualNodeAtPosition = actualExpression.FindDeepestNodeAt(absolutePosition); Assert.NotNull(actualNodeAtPosition); Assert.Equal(expectedExpressionKind, actualNodeAtPosition.Kind); Assert.Equal(parentExpressionKind, actualNodeAtPosition.Parent?.Kind); }); }
public void Parse_Symbols_Success(string input, ComparisonKind expectedComparisonKind, string expectedLeftSymbol, string expectedRightSymbol) { AssertParser.SucceedsWith(Parsers.Comparison, input, actualComparison => { Assert.Equal(expectedComparisonKind, actualComparison.ComparisonKind); Assert.NotNull(actualComparison.Left); Symbol left = Assert.IsType <Symbol>(actualComparison.Left); Assert.Equal(left.Name, expectedLeftSymbol); Assert.NotNull(actualComparison.Right); Symbol right = Assert.IsType <Symbol>(actualComparison.Right); Assert.Equal(right.Name, expectedRightSymbol); }); }
public void ParseCompare_QuotedStrings_Success(string input, ComparisonKind expectedComparisonKind, string expectedLeftContent, string expectedRightContent) { AssertParser.SucceedsWith(Parsers.Comparison, input, actualComparison => { Assert.Equal(expectedComparisonKind, actualComparison.ComparisonKind); Assert.NotNull(actualComparison.Left); QuotedString left = Assert.IsType <QuotedString>(actualComparison.Left); Assert.Equal(expectedLeftContent, left.StringContent); Assert.NotNull(actualComparison.Right); QuotedString right = Assert.IsType <QuotedString>(actualComparison.Right); Assert.Equal(expectedRightContent, right.StringContent); }); }
public void CanParseSeceralLines() { AssertParser.SucceedsWith(AssemblerParser.Assembler, @" ;multiline sample label: mov a , b;", lines => CollectionAssert.AreEqual( new[] { new AssemblerLine(null, null, null, "multiline sample"), new AssemblerLine("label", null, null, null), new AssemblerLine(null, "mov", new[] { "a", "b" }, "") }, lines)); }
public void SimpleListEquivalentToStringSplit(string input) { AssertParser.SucceedsWith(Parsers.SimpleLists.List, input, actualList => { DumpList(actualList, input); string[] expectedValues = input.Split(';'); Assert.Collection(actualList.Items, HasListItems(expectedValues, (expectedValue, actualItem) => { Assert.Equal(ExpressionKind.SimpleListItem, actualItem.Kind); SimpleListItem actuaListItem = Assert.IsType <SimpleListItem>(actualItem); Assert.Equal(expectedValue, actuaListItem.Value); })); }); }
public void Parse_Unary_Not_Comparison_QuotedString_Success(string input, ComparisonKind expectedComparisonKind, string expectedLeftString, string expectedRightString) { AssertParser.SucceedsWith(Parsers.Logical, input, actualLogical => { Assert.Equal(LogicalOperatorKind.Not, actualLogical.OperatorKind); Compare comparison = Assert.IsType <Compare>(actualLogical.Right); Assert.Equal(expectedComparisonKind, comparison.ComparisonKind); QuotedString leftString = Assert.IsType <QuotedString>(comparison.Left); Assert.Equal(expectedLeftString, leftString.StringContent); QuotedString rightString = Assert.IsType <QuotedString>(comparison.Right); Assert.Equal(expectedRightString, rightString.StringContent); }); }
public void Parse_Comparison_Symbols_Success(string input, ComparisonKind expectedComparisonKind, string expectedLeftSymbol, string expectedRightSymbol) { AssertParser.SucceedsWith(Parsers.Root, input, actualRoot => { Assert.Equal(1, actualRoot.Children.Count); ExpressionNode actualExpression = actualRoot.Children[0]; Compare actualComparison = Assert.IsType <Compare>(actualExpression); Assert.Equal(expectedComparisonKind, actualComparison.ComparisonKind); Assert.NotNull(actualComparison.Left); Symbol left = Assert.IsType <Symbol>(actualComparison.Left); Assert.Equal(left.Name, expectedLeftSymbol); Assert.NotNull(actualComparison.Right); Symbol right = Assert.IsType <Symbol>(actualComparison.Right); Assert.Equal(right.Name, expectedRightSymbol); }); }
public void Parse_QuotedStrings_Success(string input, ComparisonKind expectedComparisonKind, string expectedLeftContent, string expectedRightContent) { AssertParser.SucceedsWith(Parsers.Root, input, actualRoot => { Assert.Equal(1, actualRoot.Children.Count); ExpressionNode actualExpression = actualRoot.Children[0]; Compare actualComparison = Assert.IsType <Compare>(actualExpression); Assert.Equal(expectedComparisonKind, actualComparison.ComparisonKind); Assert.NotNull(actualComparison.Left); QuotedString left = Assert.IsType <QuotedString>(actualComparison.Left); Assert.Equal(expectedLeftContent, left.StringContent); Assert.NotNull(actualComparison.Right); QuotedString right = Assert.IsType <QuotedString>(actualComparison.Right); Assert.Equal(expectedRightContent, right.StringContent); }); }
public void DroppedClosingParenthesisProducesMeaningfulError(Tokenizer <ArithmeticExpressionToken> tokenizer) { AssertParser.FailsWithMessage(ArithmeticExpressionParser.Lambda, "1 + (2 * 3", tokenizer, "Syntax error: unexpected end of input, expected `)`."); }
public void ThenFailsIfFirstParserFails() { AssertParser.Fails(Character.EqualTo('a').Then(_ => Character.EqualTo('b')), "cb"); }
public void MissingOperatorProducesMeaningfulError(Tokenizer <ArithmeticExpressionToken> tokenizer) { AssertParser.FailsWithMessage(ArithmeticExpressionParser.Lambda, "1 3", tokenizer, "Syntax error (line 1, column 3): unexpected number `3`."); }
public void MissingOperandProducesMeaningfulError(Tokenizer <ArithmeticExpressionToken> tokenizer) { AssertParser.FailsWithMessage(ArithmeticExpressionParser.Lambda, "1 + * 3", tokenizer, "Syntax error (line 1, column 5): unexpected operator `*`, expected expression."); }
public void FailedParsingProducesMessage() { AssertParser.FailsWithMessage(Character.EqualTo('a').Named("hello"), "b", "Syntax error (line 1, column 1): unexpected `b`, expected hello."); }