Пример #1
0
        public Regex Parse(string regularExpression)
        {
            var grammar = new RegexGrammar();
            var parseEngine = new ParseEngine(grammar, new ParseEngineOptions(optimizeRightRecursion: true));
            var parseRunner = new ParseRunner(parseEngine, regularExpression);
            while (!parseRunner.EndOfStream())
            {
                if (!parseRunner.Read())
                    throw new Exception(
                        $"Unable to parse regular expression. Error at position {parseRunner.Position}.");
            }
            if (!parseEngine.IsAccepted())
                throw new Exception(
                    $"Error parsing regular expression. Error at position {parseRunner.Position}");

            var parseForest = parseEngine.GetParseForestRootNode();

            var parseTree = new InternalTreeNode(
                    parseForest as IInternalForestNode,
                    new SelectFirstChildDisambiguationAlgorithm());

            var regexVisitor = new RegexVisitor();
            parseTree.Accept(regexVisitor);

            return regexVisitor.Regex;
        }
Пример #2
0
#pragma warning disable CC0091 // Use static method
        public EbnfDefinition Parse(string ebnf)
        {
            var grammar = new EbnfGrammar();
            var parseEngine = new ParseEngine(grammar, new ParseEngineOptions(optimizeRightRecursion: true));
            var parseRunner = new ParseRunner(parseEngine, ebnf);
            while (!parseRunner.EndOfStream())
            {
                if (!parseRunner.Read())
                    throw new Exception(
                        $"Unable to parse Ebnf. Error at position {parseRunner.Position}.");
            }
            if (!parseEngine.IsAccepted())
                throw new Exception(
                    $"Unable to parse Ebnf. Error at position {parseRunner.Position}");

            var parseForest = parseEngine.GetParseForestRootNode();

            var parseTree = new InternalTreeNode(
                    parseForest as IInternalForestNode,
                    new SelectFirstChildDisambiguationAlgorithm());

            var ebnfVisitor = new EbnfVisitor();
            parseTree.Accept(ebnfVisitor);
            return ebnfVisitor.Definition;            
        }
Пример #3
0
        public bool Parse()
        {
            var grammar = new BnfGrammar();
            var parseEngine = new ParseEngine(grammar);
            var parseRunner = new ParseRunner(parseEngine, sampleBnf);

            while (!parseRunner.EndOfStream() && parseRunner.Read()) { }
            return parseRunner.ParseEngine.IsAccepted();
        }
Пример #4
0
 private void FailParseInput(string input)
 {
     var parseRunner = new ParseRunner(_parseEngine, input);
     Assert.IsFalse(parseRunner.ParseEngine.IsAccepted());
 }
Пример #5
0
 private IForestNode ParseInput(string input)
 {
     var parseRunner = new ParseRunner(_parseEngine, input);
     for (int i = 0; i < input.Length; i++)
     {
         Assert.IsTrue(parseRunner.Read(), $"Error found in position {parseRunner.Position}");
     }
     Assert.IsTrue(parseRunner.ParseEngine.IsAccepted());
     return parseRunner.ParseEngine.GetParseForestRootNode();
 }