public void PredicateMustFail() { // Arrange string testInput = "aaaabbbb"; var tokenizer = peg.Tokenizer(TokenDefinitions.AB); var rules = peg.ParserGenerator.GetParsingRules("Start => &([B]) [A] [B]"); var parser = peg.Parser(ParsingRuleNames.Start, rules); // Act var tokenizationResult = tokenizer.Tokenize(testInput); var parseResult = parser.Parse(tokenizationResult.MatchedRanges); // Assert Assert.False(parseResult.Succeed); }
public void ShouldReturnMarchingSubExpressionPEGOnSemanticAction() { // Arrange string input = "aaa"; var tokenizer = _peg.Tokenizer(TokenDefinitions.AB); string matchedSpec = null; _peg.DefaultSemanticActions.SetDefaultGenericPassthroughAction <GenericPassthroughAst>(); _peg.DefaultSemanticActions.OrderedChoiceAction = (_, subResults, matchedPeg) => { matchedSpec = matchedPeg; return(subResults.First()); }; var rules = _peg.ParserGenerator.GetParsingRules("Start => [A] | [B]"); var parser = _peg.Parser(ParsingRuleNames.Start, rules); // Act var tokens = tokenizer.Tokenize(input); var parseResult = parser.Parse(tokens.MatchedRanges); // Assert Assert.Equal("([A] | [B])", matchedSpec); GenericPassthroughAst results = (GenericPassthroughAst)parseResult.SemanticActionResult; Assert.Equal("Start", results.MatchedPeg); results = (GenericPassthroughAst)results.SubResults.First(); Assert.Equal("[A]", results.MatchedPeg); }
private static Parser <Tokens, RuleNames, double?> DefineParser(PegFacade <Tokens, RuleNames, double?> peg) { var grammer = @"Add => (Add [Plus] Add) | [Number]"; var rules = peg.ParserGenerator.GetParsingRules(grammer); var parser = peg.Parser(RuleNames.Add, rules); // RuleNames.Add is the start symbol return(parser); }
public void ShouldParseEmptyCase() { // Arrange string testInput = string.Empty; var tokenizer = peg.Tokenizer(TokenDefinitions.JustA); var parseRules = peg.ParserGenerator.GetParsingRules("Start => [A]?"); var parser = peg.Parser(ParsingRuleNames.Start, parseRules); // Act var tokenizationResult = tokenizer.Tokenize(testInput); var parseResult = parser.Parse(tokenizationResult.MatchedRanges); // Assert Assert.True(parseResult.Succeed); Assert.Empty(subActionResults); Assert.Equal(new MatchRange(0, 0), matchedTokenRangeAssert.MatchedIndices); Assert.Equal(testInput, parseResult.SemanticActionResult); Assert.Equal(0, parseResult.NextParseStartIndex); }
public void ShouldParseWithSecondExpression() { // Arrange string input = "bbb"; var tokenizer = peg.Tokenizer(TokenDefinitions.AB); peg.DefaultSemanticActions.SetTerminalAction(TokenNames.B, (m, _, matchedPeg) => m.GetMatchedString()); var rules = peg.ParserGenerator.GetParsingRules("Start => [A] | [B]"); var parser = peg.Parser(ParsingRuleNames.Start, rules); // Act var tokens = tokenizer.Tokenize(input); var parseResult = parser.Parse(tokens.MatchedRanges); // Assert Assert.True(parseResult.Succeed); Assert.Equal(input, parseResult.SemanticActionResult); Assert.Equal(new MatchRange(0, 1), matchedTokens.MatchedIndices); Assert.Equal(1, parseResult.NextParseStartIndex); }
public void ShouldParse() { // Arrange string inputString = "aaaa"; var op = peg.Operators; var tokenizer = peg.Tokenizer(TokenDefinitions.JustA); var rules = peg.ParserGenerator.GetParsingRules("Start => NonTerminalA; NonTerminalA => [A];"); var parser = peg.Parser(ParsingRuleNames.Start, rules); // Act var inputTokens = tokenizer.Tokenize(inputString); var parseResult = parser.Parse(inputTokens.MatchedRanges); // Assert Assert.True(parseResult.Succeed); Assert.Equal(new MatchRange(0, 1), parseResult.MatchedTokens.MatchedIndices); Assert.Equal(inputString, parseResult.SemanticActionResult); Assert.Equal(inputString, _subResults.Single()); Assert.Equal(1, parseResult.NextParseStartIndex); }
public void ShouldParseRepitition() { // Arrange var inputString = "aaabbbaaa"; var tokenizer = peg.Tokenizer(TokenDefinitions.AB); var rules = peg.ParserGenerator.GetParsingRules("Start => ([A] | [B])*"); var parser = peg.Parser(ParsingRuleNames.Start, rules); // Act var tokens = tokenizer.Tokenize(inputString); var parseResult = parser.Parse(tokens.MatchedRanges); // Assert Assert.True(parseResult.Succeed); Assert.Equal(3, parseResult.NextParseStartIndex); Assert.Equal(inputString, parseResult.SemanticActionResult); Assert.Equal(new MatchRange(0, 3), matchedTokens.MatchedIndices); var subResultList = subActionResults.ToList(); Assert.Equal(3, subResultList.Count); Assert.Equal("aaa", subResultList[0]); Assert.Equal("bbb", subResultList[1]); Assert.Equal("aaa", subResultList[2]); }
private void CreateParser() { const string grammer = @" Start => (Statement? [SemiColon])+; Statement => RewriteRule | Label? Term; Term => NonACTerm | [Variable] | [Identifier] | [String] | [Number] | TermList; Label => [Identifier] ([Comma] [Identifier])* [Colon]; TermList => [OpenRoundBracket] CommaSeperatedTerms [CloseRoundBracket]; CommaSeperatedTerms => (Term ([Comma] Term)*)?; NonACTerm => [Identifier] ClassMemberMapping? [OpenRoundBracket] CommaSeperatedTerms [CloseRoundBracket]; ClassMemberMapping => [OpenAngleBracket] ([Identifier] ([Comma] [Identifier])*)? [CloseAngleBracket]; RewriteRule => Term [Arrow] Term; "; _parser = _pegFacade.Parser(ParseRuleNames.Start, _pegFacade.ParserGenerator.GetParsingRules(grammer)); }
public void ShouldParseEmptyInputTokens() { // Arrange string inputString = string.Empty; var tokenizer = peg.Tokenizer(TokenDefinitions.AB); var rules = peg.ParserGenerator.GetParsingRules("Start => []"); var parser = peg.Parser(ParsingRuleNames.Start, rules); // Act var tokensResult = tokenizer.Tokenize(inputString); var parseResult = parser.Parse(tokensResult.MatchedRanges); // Assert Assert.Equal(new MatchRange(0, 0), parseResult.MatchedTokens.MatchedIndices); Assert.Empty(subActionResults); Assert.True(parseResult.Succeed); Assert.Equal(inputString, parseResult.SemanticActionResult); Assert.Equal(0, parseResult.NextParseStartIndex); }
public void ShouldParse() { // Arrange var inputString = "aaa"; var tokenizer = peg.Tokenizer(TokenDefinitions.JustA); var rules = peg.ParserGenerator.GetParsingRules("TerminalTest => [A];"); var parser = peg.Parser(ParsingRuleNames.TerminalTest, rules); // Act var tokensResult = tokenizer.Tokenize(inputString); var parseResult = parser.Parse(tokensResult.MatchedRanges); // Assert Assert.Equal(new MatchRange(0, 1), parseResult.MatchedTokens.MatchedIndices); Assert.Empty(subActionResults); Assert.True(parseResult.Succeed); Assert.Equal(inputString, parseResult.SemanticActionResult); Assert.Equal(1, parseResult.NextParseStartIndex); }
public void ShouldParse() { // Arrange var inputString = "aaabbb"; var tokenizer = peg.Tokenizer(TokenDefinitions.AB); var rules = peg.ParserGenerator.GetParsingRules("ConcatenationTest => [A] [B]"); var parser = peg.Parser(ParsingRuleNames.ConcatenationTest, rules); // Act var tokens = tokenizer.Tokenize(inputString); var parseResult = parser.Parse(tokens.MatchedRanges); // Assert Assert.Equal(new MatchRange(0, 2), matchedTokenRangeAssert.MatchedIndices); Assert.Equal(2, subActionResults.Count); Assert.Equal("aaa", subActionResults[0]); Assert.Equal("bbb", subActionResults[1]); Assert.True(parseResult.Succeed); Assert.Equal(inputString, parseResult.SemanticActionResult); Assert.Equal(2, parseResult.NextParseStartIndex); }
private void BuildParser() { var op = _inputPeg.Operators; var start = _inputPeg.Rule(RuleName.Start, op.OneOrMore(op.NonTerminal(RuleName.Rule))); var rule = _inputPeg.Rule(RuleName.Rule, op.Sequence(op.Terminal(TokenNames.Identifier), op.Terminal(TokenNames.Arrow), op.NonTerminal(RuleName.Operator), op.Optional(op.Terminal(TokenNames.SemiColon)))); var @operator = _inputPeg.Rule(RuleName.Operator, // NB: Order matters here op.OrderedChoice(op.NonTerminal(RuleName.Choice), op.NonTerminal(RuleName.Sequence), op.NonTerminal(RuleName.Optional), op.NonTerminal(RuleName.ZeroOrMore), op.NonTerminal(RuleName.OneOrMore), op.NonTerminal(RuleName.Terminal), op.NonTerminal(RuleName.NonTerminal), op.NonTerminal(RuleName.Brackets), op.NonTerminal(RuleName.Empty), op.NonTerminal(RuleName.And), op.NonTerminal(RuleName.Not) )); var and = _inputPeg.Rule(RuleName.And, op.Sequence(op.Terminal(TokenNames.And), op.Terminal(TokenNames.OpenRound), op.NonTerminal(RuleName.Operator), op.Terminal(TokenNames.CloseRound))); var not = _inputPeg.Rule(RuleName.Not, op.Sequence(op.Terminal(TokenNames.Not), op.Terminal(TokenNames.OpenRound), op.NonTerminal(RuleName.Operator), op.Terminal(TokenNames.CloseRound))); var oneOrMore = _inputPeg.Rule(RuleName.OneOrMore, op.Sequence(op.NonTerminal(RuleName.Operator), op.Terminal(TokenNames.OneOrMore))); var zeroOrMore = _inputPeg.Rule(RuleName.ZeroOrMore, op.Sequence(op.NonTerminal(RuleName.Operator), op.Terminal(TokenNames.ZeroOrMore))); var terminal = _inputPeg.Rule(RuleName.Terminal, op.Sequence(op.Terminal(TokenNames.OpenSquare), op.Terminal(TokenNames.Identifier), op.Terminal(TokenNames.CloseSquare))); var emptyString = _inputPeg.Rule(RuleName.Empty, op.Sequence(op.Terminal(TokenNames.OpenSquare), op.Terminal(TokenNames.CloseSquare))); var nonterminal = _inputPeg.Rule(RuleName.NonTerminal, op.Terminal(TokenNames.Identifier)); var optional = _inputPeg.Rule(RuleName.Optional, op.Sequence(op.NonTerminal(RuleName.Operator), op.Terminal(TokenNames.Optional))); var sequence = _inputPeg.Rule(RuleName.Sequence, op.Sequence(op.NonTerminal(RuleName.Operator), op.NonTerminal(RuleName.Operator))); var brackets = _inputPeg.Rule(RuleName.Brackets, op.Sequence(op.Terminal(TokenNames.OpenRound), op.NonTerminal(RuleName.Operator), op.Terminal(TokenNames.CloseRound))); var choice = _inputPeg.Rule(RuleName.Choice, op.Sequence(op.NonTerminal(RuleName.Operator), op.Terminal(TokenNames.Choice), op.NonTerminal(RuleName.Operator))); _parser = _inputPeg.Parser(RuleName.Start, new[] { start, rule, sequence, @operator, terminal, optional, brackets, choice, nonterminal, emptyString, oneOrMore, zeroOrMore, and, not }); }