public TrlParser() { _pegFacade = new PegFacade <TokenNames, ParseRuleNames, ITrlParseResult>(); CreateTokenizer(); CreateSemanticActions(); CreateParser(); }
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+")) })); }
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 Generator(PegFacade <TTokenTypeName, TNonTerminalName, TActionResult> outputPeg) { _inputPeg = new PegFacade <TokenNames, RuleName, IParserGeneratorResult>(); _outputPeg = outputPeg; BuildTokenizer(); BuildSemantics(); BuildParser(); }
private static void DefineSemanticActions(PegFacade <Tokens, RuleNames, double?> peg) { var semanticActions = peg.DefaultSemanticActions; semanticActions.OrderedChoiceAction = (matchedTokens, subresults, matchedPeg) => subresults.First(); semanticActions.SetTerminalAction(Tokens.Number, (matchedTokens, subresults, matchedPeg) => double.Parse(matchedTokens.GetMatchedString())); semanticActions.SetNonTerminalAction(RuleNames.Add, (matchedTokens, subresults, matchedPeg) => subresults.First()); semanticActions.SequenceAction = (matchedTokens, subresults, matchedPeg) => { // "+" will return a sub result of null because if does not have an // action associated with the Plus token via a SetTerminalAction call return(subresults.Where(s => s.HasValue).Sum(s => s.Value)); }; }
static void Main(string[] args) { var peg = new PegFacade <Tokens, RuleNames, double?>(); const string input = "1 + 2 + 3"; // Define tokenizer Tokenizer <Tokens> tokenizer = GetTokenizer(peg); // Define semantic callback functions DefineSemanticActions(peg); // Define PEG rules Parser <Tokens, RuleNames, double?> parser = DefineParser(peg); // Tokenize input var tokenResult = tokenizer.Tokenize(input); if (!tokenResult.Succeed) { Console.WriteLine("Invalid input"); return; } // Remove whitespace var inputNoWhitespace = tokenResult.MatchedRanges .Where(t => t.TokenName != Tokens.Whitespace) .ToList().AsReadOnly(); // Parse var output = parser.Parse(inputNoWhitespace); if (!output.Succeed) { Console.WriteLine("Invalid input"); } else { Console.WriteLine("Parse succeeded"); Console.WriteLine($"Sum = {output.SemanticActionResult}"); } }
public SemanticActionsTests() { _peg = new PegFacade <TokenNames, ParsingRuleNames, IAstResult>(); }
public ParserGeneratorTests() { _peg = new PegFacade <TokenNames, ParsingRuleNames, string>(); }