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 Tokenizer <Tokens> GetTokenizer(PegFacade <Tokens, RuleNames, double?> peg) { return(peg.Tokenizer(new[] { peg.Token(Tokens.Plus, new Regex(@"\+")), peg.Token(Tokens.Number, new Regex(@"[-+]?[0-9]+\.?[0-9]*")), peg.Token(Tokens.Whitespace, new Regex(@"\s+")) })); }
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]); }
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); }
private void BuildTokenizer() { _tokenizer = _inputPeg.Tokenizer(new[] { _inputPeg.Token(TokenNames.Identifier, new Regex(@"[a-zA-Z_][a-zA-Z\d_]*")), _inputPeg.Token(TokenNames.OpenSquare, new Regex(@"\[")), _inputPeg.Token(TokenNames.CloseSquare, new Regex(@"\]")), _inputPeg.Token(TokenNames.OpenRound, new Regex(@"\(")), _inputPeg.Token(TokenNames.CloseRound, new Regex(@"\)")), _inputPeg.Token(TokenNames.WhiteSpace, new Regex(@"\s+")), _inputPeg.Token(TokenNames.And, new Regex(@"\&")), _inputPeg.Token(TokenNames.Not, new Regex(@"\!")), _inputPeg.Token(TokenNames.OneOrMore, new Regex(@"\+")), _inputPeg.Token(TokenNames.ZeroOrMore, new Regex(@"\*")), _inputPeg.Token(TokenNames.Optional, new Regex(@"\?")), _inputPeg.Token(TokenNames.Choice, new Regex(@"\|")), _inputPeg.Token(TokenNames.Arrow, new Regex(@"\=\>")), _inputPeg.Token(TokenNames.SemiColon, new Regex(@";")) }); }
private void CreateTokenizer() { // NB: This list of token definitions is prioritized _tokenizer = _pegFacade.Tokenizer(new[] { _pegFacade.Token(TokenNames.Comment, new Regex(@"//.*\n")), _pegFacade.Token(TokenNames.String, new Regex(@"""(?:(?:\\\"")|[^\""])*?\""", RegexOptions.Compiled)), // \" is used to escape quote characters _pegFacade.Token(TokenNames.Variable, new Regex(@"\:[_a-zA-Z]\w*(?:\.[_a-zA-Z]\w*)*", RegexOptions.Compiled)), _pegFacade.Token(TokenNames.Identifier, new Regex(@"[_a-zA-Z]\w*(?:\.[_a-zA-Z]\w*)*", RegexOptions.Compiled)), _pegFacade.Token(TokenNames.Whitespace, new Regex(@"\s+", RegexOptions.Compiled)), _pegFacade.Token(TokenNames.SemiColon, new Regex(@";", RegexOptions.Compiled)), _pegFacade.Token(TokenNames.Number, new Regex(@"[+-]?\d+\.?\d*", RegexOptions.Compiled)), _pegFacade.Token(TokenNames.Comma, new Regex(@",", RegexOptions.Compiled)), _pegFacade.Token(TokenNames.Colon, new Regex(@":", RegexOptions.Compiled)), _pegFacade.Token(TokenNames.OpenRoundBracket, new Regex(@"\(", RegexOptions.Compiled)), _pegFacade.Token(TokenNames.CloseRoundBracket, new Regex(@"\)", RegexOptions.Compiled)), _pegFacade.Token(TokenNames.Arrow, new Regex(@"\=\>", RegexOptions.Compiled)), _pegFacade.Token(TokenNames.OpenAngleBracket, new Regex(@"\<", RegexOptions.Compiled)), _pegFacade.Token(TokenNames.CloseAngleBracket, new Regex(@"\>", RegexOptions.Compiled)) }); }
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); }