Beispiel #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);
        }
Beispiel #2
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;
        }
Beispiel #3
0
        public Regex Parse(string regularExpression)
        {
            var grammar     = new RegexGrammar();
            var parseEngine = new ParseEngine(grammar, new ParseEngineOptions());
            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 parseTree = parseEngine.GetParseTree();

            var regexVisitor = new RegexVisitor();

            parseTree.Accept(regexVisitor);

            return(regexVisitor.Regex);
        }
Beispiel #4
0
        private static EbnfLexerRuleFactor VisitLexerRuleFactorNode(IInternalTreeNode node)
        {
            for (int c = 0; c < node.Children.Count; c++)
            {
                var child = node.Children[c];
                if (child.NodeType != TreeNodeType.Internal)
                    continue;
                var internalNode = child as IInternalTreeNode;
                var symbolValue = internalNode.Symbol.Value;

                if (EbnfGrammar.Literal == symbolValue)
                    return new EbnfLexerRuleFactorLiteral(
                        VisitLiteralNode(internalNode));

                if (RegexGrammar.Regex == symbolValue)
                {
                    var regexVisitor = new RegexVisitor();
                    internalNode.Accept(regexVisitor);
                    return new EbnfLexerRuleFactorRegex(regexVisitor.Regex);
                }
            }
            throw UnreachableCodeException();
        }
Beispiel #5
0
        private EbnfFactor VisitFactorNode(IInternalTreeNode node)
        {
            for (int c = 0; c < node.Children.Count; c++)
            {
                var child = node.Children[c];
                switch (child.NodeType)
                {
                    case TreeNodeType.Internal:
                        var internalNode = child as IInternalTreeNode;
                        var symbolValue = internalNode.Symbol.Value;

                        if (EbnfGrammar.QualifiedIdentifier == symbolValue)
                            return new EbnfFactorIdentifier(
                                VisitQualifiedIdentifierNode(internalNode));

                        if (EbnfGrammar.Literal == symbolValue)
                            return new EbnfFactorLiteral(
                                VisitLiteralNode(internalNode));

                        if (EbnfGrammar.Repetition == symbolValue)
                            return VisitRepetitionNode(internalNode);

                        if (EbnfGrammar.Optional == symbolValue)
                            return VisitOptionalNode(internalNode);

                        if (EbnfGrammar.Grouping == symbolValue)
                            return VisitGroupingNode(internalNode);

                        if (RegexGrammar.Regex == symbolValue)
                        {
                            var regexVisitor = new RegexVisitor();
                            internalNode.Accept(regexVisitor);
                            return new EbnfFactorRegex(regexVisitor.Regex);
                        }
                        break;

                    case TreeNodeType.Token:
                        break;
                }
            }
            throw UnreachableCodeException();
        }