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; }
#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; }