コード例 #1
0
        public static List <Options> FromFile(string path)
        {
            using var file = File.OpenText(path);
            var text = file.ReadToEnd();

            file.Close();

            var grammar = DSLGrammar.Build();
            var ast     = grammar.Parse(text);

            if (ast == null)
            {
                throw new LoadFromFileException(grammar, grammar.Goal, text);
            }

            var cstBuilder = new CstBuilderVisitor();

            cstBuilder.Apply(ast);
            var cst = (CstDsl)cstBuilder.GetResult();

            var optionsBuilder = new DslStateMachineOptionsBuilderVisitor();

            cst.Visit(optionsBuilder);

            var options = (Dictionary <string, Options>)optionsBuilder.GetResult();

            return(options.Values.ToList());
        }
コード例 #2
0
        static void Main(string[] args)
        {
            Console.WriteLine("DSL Parser Lib\n");
            Console.WriteLine();

            var dslParser = DSLGrammar.Build();

            Console.WriteLine(dslParser.PrintGrammar() + "\n\n");
            // return;

            using var dfaExplanation = File.OpenText("../../../Examples/future_example_min.fa");
            var dfaText = dfaExplanation.ReadToEnd();
            var ast     = dslParser.Parse(dfaText);

            // var ast = dslParser.Parse("Determine(Nfa1, Dfa1) Minimize(Dfa1, MinDfa1) Function SomeFunc2 (env, int a, bool flag) SomeFunc(Dfa1, 4, 3.14, a, Dfa2) End Function ", DSLGrammar.CodeArea);

            Console.WriteLine("Save astOutput.dot into ../../../Examples/astOutput.dot");
            File.Delete("../../../Examples/astOutput.dot");
            using var astFileOutput = File.OpenWrite("../../../Examples/astOutput.dot");
            var astOutputBytes = Encoding.UTF8.GetBytes(ast?.Dot(false) ?? "[NULL PARSE RESULT]");

            astFileOutput.Write(astOutputBytes);
            // return;

            var cstBuilder = new CstBuilderVisitor();

            cstBuilder.Apply(ast);
            var cstDsl = (CstDsl)cstBuilder.GetResult();

            Console.WriteLine(cstDsl?.Dot() ?? "[NULL]");
        }
コード例 #3
0
        static void Main(string[] args)
        {
            while (args.Any())
            {
                var arg = args[0];
                args = args.Where((_, i) => i != 0).ToArray();

                if (arg.StartsWith("-"))
                {
                    switch (arg)
                    {
                    case "-r":
                    case "--regex":
                        _regexpForParsing = args[0];
                        args = args.Where((_, i) => i != 0).ToArray();

                        break;

                    case "-o":
                    case "--out":
                        var path = args[0];
                        args = args.Where((_, i) => i != 0).ToArray();

                        if (Uri.IsWellFormedUriString(path, UriKind.RelativeOrAbsolute) &&
                            Directory.Exists(path))
                        {
                            _outputPath = path;
                        }
                        break;

                    case "-M":
                    case "--minimize":
                        _minimize = true;
                        break;

                    case "-D":
                    case "--determine":
                        _determ   = true;
                        _dverbose = false;
                        break;

                    case "-D:verbose":
                    case "--determine:verbose":
                        _determ   = true;
                        _dverbose = true;
                        break;


                    case "--table":
                        _printTable = true;
                        break;

                    case "--dot":
                        _printDot = true;
                        break;

                    case "--asset":
                    case "--asset:true":
                        throw new NotImplementedException();
                        // Assets.Add(new Asset {Text = args[0], ExpectedResult = true});
                        // args = args.Where((_, i) => i != 0).ToArray();
                        break;

                    case "--asset:false":
                        throw new NotImplementedException();
                        // Assets.Add(new Asset {Text = args[0], ExpectedResult = false});
                        // args = args.Where((_, i) => i != 0).ToArray();
                        break;

                    default:
                        break;
                    }
                }
                else
                {
                    _optionsFilePath = arg;
                }
            }

            try
            {
                _optionsFilePath = args[0];
            } catch (IndexOutOfRangeException) {}

            try
            {
                if (string.IsNullOrEmpty(_regexpForParsing))
                {
                    Console.WriteLine($"[Info] Used options file is {_optionsFilePath}");
                    using var file = File.OpenText(_optionsFilePath);
                    var text = file.ReadToEnd();
                    file.Close();

                    var grammar = DSLGrammar.Build();
                    var ast     = grammar.Parse(text);

                    if (ast == null)
                    {
                        throw new LoadFromFileException(grammar, grammar.Goal, text);
                    }

                    var cstBuilder = new CstBuilderVisitor();
                    cstBuilder.Apply(ast);
                    var cst = (CstDsl)cstBuilder.GetResult();

                    var optionsBuilder = new DslStateMachineOptionsBuilderVisitor();
                    cst.Visit(optionsBuilder);

                    var options = (Dictionary <string, SMOptions>)optionsBuilder.GetResult();

                    foreach (var(machineName, machineOptions) in options)
                    {
                        Machines.Add(machineName, Machine.GetFromOptions(machineOptions));
                    }

                    foreach (var entity in cst.CodeEntities)
                    {
                        if (entity is CstFunctionCall functionCall)
                        {
                            BuiltIn[functionCall.FunctionName.Name].Execute(functionCall.Args.ToList());
                        }
                    }
                }

                else
                {
                    Console.WriteLine($"[Info] Generating state machine with regex \"{_regexpForParsing}\"");

                    var regexpParser = RegexpGrammar.GetParser();
                    var parseRes     = regexpParser.Parse(_regexpForParsing);

                    // Console.WriteLine(parseRes.Dot()); return;

                    var cstBuilder = new CSTBuilderVisitor();
                    cstBuilder.Apply(parseRes);
                    var cst = cstBuilder.GetResult();

                    // Console.WriteLine(cst.Dot()); return;

                    var stateMachineBuilder = new RegexStateMachineBuilderVisitor();
                    cst.Visit(stateMachineBuilder);

                    Machines.Add("DEFAULT_MACHINE_REGEX", stateMachineBuilder.GetResult());

                    // Console.WriteLine(stateMachine.ToOptions().ToDot()); return;
                }

                //     Console.WriteLine($"[Info] Type: {stateMachine.Type}, MachineId: {stateMachine.Id}");
                //     Console.WriteLine();
                //
                //     PrintTable(stateMachine);
                //     PrintDot(stateMachine);
                //     SaveMachineIntoFile(stateMachine, "NonDeterm");
                //
                //     if (_determ && stateMachine.Type != MachineType.Determined)
                //     {
                //
                //         Console.WriteLine();
                //         Console.WriteLine(new string('=', 60));
                //         Console.WriteLine("[Action] Determining...");
                //
                //         stateMachine = stateMachine.Determine(_dverbose).RenameToNormalNames("s");
                //         if (_dverbose)
                //         {
                //             Console.WriteLine(new string('=', 60));
                //             Console.WriteLine("[Info] State machine was determined");
                //         }
                //         Console.WriteLine($"[Info] New id: {stateMachine.Id}");
                //         Console.WriteLine();
                //
                //         PrintTable(stateMachine);
                //         PrintDot(stateMachine);
                //
                //         SaveMachineIntoFile(stateMachine, "Determ");
                //     }
                //
                //     if (_minimize)
                //     {
                //         Console.WriteLine();
                //         Console.WriteLine(new string('=', 60));
                //         Console.WriteLine("[Action] Minimizing...");
                //
                //         stateMachine = stateMachine.Minimize().RenameToNormalNames("m");
                //         Console.WriteLine($"[Info] New id: {stateMachine.Id}");
                //         Console.WriteLine();
                //
                //         PrintTable(stateMachine);
                //         PrintDot(stateMachine);
                //         SaveMachineIntoFile(stateMachine, "MinDeterm");
                //     }
                //
                //     TestAssets(Assets, stateMachine);
                //
            }

            catch (FileNotFoundException)
            {
                Console.Error.WriteLine("[Error] Can't load configuration file");
            }

            catch (LoadFromFileException ex)
            {
                Console.Error.WriteLine("[Error] " + ex.Message);
            }

            catch (CoreSMException e)
            {
                Console.Error.WriteLine($"Some errors happened when machine was running:\n {e.Message}");
            }
        }