示例#1
0
 public TrlParser()
 {
     _pegFacade = new PegFacade <TokenNames, ParseRuleNames, ITrlParseResult>();
     CreateTokenizer();
     CreateSemanticActions();
     CreateParser();
 }
示例#2
0
 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+"))
     }));
 }
示例#3
0
        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);
        }
示例#4
0
 public Generator(PegFacade <TTokenTypeName, TNonTerminalName, TActionResult> outputPeg)
 {
     _inputPeg  = new PegFacade <TokenNames, RuleName, IParserGeneratorResult>();
     _outputPeg = outputPeg;
     BuildTokenizer();
     BuildSemantics();
     BuildParser();
 }
示例#5
0
        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));
            };
        }
示例#6
0
        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}");
            }
        }
示例#7
0
 public SemanticActionsTests()
 {
     _peg = new PegFacade <TokenNames, ParsingRuleNames, IAstResult>();
 }
 public ParserGeneratorTests()
 {
     _peg = new PegFacade <TokenNames, ParsingRuleNames, string>();
 }