예제 #1
0
        private void BenchmarkOpen()
        {
            BenchmarkFileText = string.Empty;
            if (File.Exists(BenchmarkFilePath))
            {
                try
                {
                    BenchmarkFileText = File.ReadAllText(BenchmarkFilePath);
                }
                catch (Exception e)
                {
                    Result = e.Message;
                    return;
                }

                try
                {
                    _docBenchmark = CodeDocument.Load(BenchmarkFileParser, BenchmarkFileText);
                }
                catch (Exception e)
                {
                    Result = "Can't read benchmark file: " + e.Message;
                    return;
                }
            }

            if (_docBenchmark != null && _docBenchmark.AnyNested(c => c.Name == "result1"))
            {
                BenchmarkCompare = new BenchmarkResult(_docBenchmark.Codes().First(c => c.Name == "result1"));
            }
        }
예제 #2
0
        public static Program CreateProgram(CodeDocument doc, Dictionary <string, Function> functions, Dictionary <string, ValueBase> parameters)
        {
            var prog = new Program();

            Scope     externalScope = functions == null ? null : new Scope(null, null, functions);
            Variables variables     = new Variables(null, parameters);

            prog.RootScope = CreateScope(doc.Codes(WordScope).First(), variables, externalScope, DefType.Void, out bool alwaysReturn);

            return(prog);
        }
예제 #3
0
        /// <summary>Apply settings to a linked Grammar.</summary>
        /// <returns></returns>
        private static bool ApplySettingsFromGrammar(Parser parser, CodeDocument doc, ParserStatus status)
        {
            bool ok = true;

            foreach (CodeElement SetterElement in doc.Codes(MetaParser.Setter_____))
            {
                CodeElement elementId = SetterElement.Codes().First();

                Rule rule = parser.Rules.FirstOrDefault(r => r.Name == elementId.Value);
                if (rule == null)
                {
                    status.AddBuildError(() => MessageRes.itc26, elementId, elementId.Value);
                    ok = false;
                    continue;
                }

                foreach (CodeElement assignElement in SetterElement.Codes(MetaParser.Assignment_))
                {
                    List <CodeElement> assignNodes = assignElement.Codes().ToList();
                    CodeElement        propName    = assignNodes[0];
                    string             propValue   = assignNodes.Count > 1 ? assignNodes[1].Value : string.Empty;
                    switch (propName.Value)
                    {
                    case MetaParser.Trust______:
                        rule.Trust = propValue != "false";
                        break;

                    case MetaParser.Collapse___:
                        rule.Collapse = propValue != "false";
                        break;

                    case MetaParser.Comment____:
                        rule.Comment = propValue != "false";
                        break;

                    default:
                        // set the property on rule or sub elements.
                        ok = rule.SetProperty(propName, propValue, status);

                        if (!ok)
                        {
                            status.AddBuildError(() => MessageRes.itc27, propName, elementId.Value, propName.Value);
                        }
                        break;
                    }
                }
            }
            return(ok);
        }
예제 #4
0
        ///
        internal static bool BuildRules(Parser parser, CodeDocument doc, ParserStatus status)
        {
            parser.Rules = new List <Rule>();

            foreach (CodeElement ruleElement in doc.Codes(MetaParser.Rule_______))
            {
                string debug1 = "(" + parser.Name + ")".NL() + ruleElement.ToMarkupProtected("");

                List <CodeElement> ruleElements = ruleElement.Codes().ToList();
                CodeElement        elementId    = ruleElements.First();
                ruleElements.Remove(elementId);
                List <ParserElementBase> elements = BuildExpression(parser, ruleElements, status);
                Rule rule = AddRule(parser, elementId, elements.ToArray());

                string debug2 = debug1 + rule.GetGrammar();
            }

            parser.Rules[0].Comment = true;

            return(ApplySettingsFromGrammar(parser, doc, status) &&
                   InitializeGrammar(parser, parser.Rules, status) &&
                   ValidateGrammar(parser, status));
        }
예제 #5
0
        private void ReadLevels()
        {
            if (_parserLevel == null)
            {
                _parserLevel = new Parser("levels = {int};");
            }

            string tag = Benchmark?.Tag;

            Benchmark           = null;
            Result              = string.Empty;
            PreparedData        = string.Empty;
            _generatedInputFull = string.Empty;

            try
            {
                _docLevel = CodeDocument.Load(_parserLevel, _levels);
            }
            catch (Exception e)
            {
                PreparedData = "Can't read levels: " + e.Message;
                return;
            }

            if (_docLevel.Codes().Count() == 0)
            {
                PreparedData = "No levels found";
                return;
            }

            List <int> levels = new List <int>();

            foreach (var item in _docLevel.Codes())
            {
                levels.Add(int.Parse(item.Value));
            }

            if (levels.Any(i => i < 1))
            {
                PreparedData = "Levels must be 1 or greater";
                return;
            }

            if (levels.Count() > 5)
            {
                PreparedData = "Max 5 levels";
                return;
            }

            string code = string.Empty;

            for (int i = 0; i < levels.Count(); i++)
            {
                code += string.Empty + letters[i] + levels[i];
            }

            Benchmark = new BenchmarkResult(code, tag, levels.Aggregate(1, (mul, j) => mul *= j));
            string grammar = BuildGrammar(Benchmark.GetLevelListFromCode());

            _parserTest = new Parser(grammar);

            if (Benchmark.Combi <= 500)
            {
                PrepareInput(false);
                GeneratedInput = _generatedInputFull;
                CodeDocument doc = CodeDocument.Load(_parserTest, _generatedInputFull);
                GeneratedInputAsMarkup = doc.ToMarkup();
            }
            else
            {
                GeneratedInput         = "Number of combinations > 500";
                GeneratedInputAsMarkup = "Number of combinations > 500";
                PreparedData          += "Input must be prepared:\r\n";
                PreparedData          += "Test grammar:\r\n" + _parserTest.GetGrammar();
            }
        }