Ejemplo n.º 1
0
            public IInternalTreeNode Parse(string source)
            {
                var grammar = MakeGrammar();

                var parseEngine = new ParseEngine(
                    grammar,
                    new ParseEngineOptions(loggingEnabled: true));
                var parseRunner = new ParseRunner(parseEngine, source);

                while (!parseRunner.EndOfStream())
                {
                    if (!parseRunner.Read())
                    {
                        throw new Exception(
                                  $"Unable to parse expression. Error at line {parseRunner.Line+1}, column {parseRunner.Column+1}.");
                    }

                    Console.WriteLine("-----");
                    Console.ReadKey(true);
                }
                if (!parseEngine.IsAccepted())
                {
                    throw new Exception(
                              $"expression parse not accepted. Error at line {parseRunner.Line+1}, column {parseRunner.Column+1}.");
                }

                return(parseEngine.GetParseTree());
            }
Ejemplo n.º 2
0
        public IEbnfDefinition Parse(string ebnf)
        {
            var grammar     = new EbnfGrammar();
            var parseEngine = new ParseEngine(
                grammar,
                new ParseEngineOptions());
            var parseRunner = new ParseRunner(parseEngine, ebnf);

            while (!parseRunner.EndOfStream())
            {
                if (!parseRunner.Read())
                {
                    throw new Exception(
                              $"Unable to parse Ebnf. Error at line {parseRunner.Line}, column {parseRunner.Column}.");
                }
            }
            if (!parseEngine.IsAccepted())
            {
                throw new Exception(
                          $"Ebnf parse not accepted. Error at line {parseRunner.Line}, column {parseRunner.Column}.");
            }

            var parseTree = parseEngine.GetParseTree();

            var ebnfVisitor = new EbnfVisitor();

            parseTree.Accept(ebnfVisitor);
            return(ebnfVisitor.Definition);
        }
Ejemplo n.º 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);
        }