private void ParseInput() { if (_parser == null) { Markup = "Grammar is not valid"; Output = ""; return; } // Load input CodeDocument doc = null; try { doc = CodeDocument.Load(_parser, _input); } catch (ParserException e) { Output = e.Message; Markup = "Input is not valid"; return; } // Convert to markup Markup = doc.ToMarkup(); ProcessOutput(doc); }
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")); } }
protected void TheVeryBasics() { string grammar = ""; // Set the grammar in this string string input = ""; // Set the input in this string var parser = new Parser(grammar); // instantiate the parser // parse the input and get output tree CodeDocument doc = CodeDocument.Load(parser, input); var d = doc; }
private void BuildParser() { _parser = null; try { _parser = new Parser(_grammar); GrammarOutput = "Grammar is ok" + "\r\n\r\n" + _parser.GetGrammar(); ParseInput(); } catch (ParserException e) { GrammarOutput = e.Message; } try { CodeDocument doc = CodeDocument.Load(new Parser(string.Empty), _grammar); GrammarMarkup = doc.ToMarkup(); } catch (ParserException e) { GrammarMarkup = e.Message; } }
private void RunTestParse() { // Function for calculating new average Func <float, float, double, int> newAvg = (i, old, nw) => (int)(i == 0 ? nw : old * i / (i + 1) + nw / (i + 1)); if (_parserTest == null) { Result += "Parser not set. Enter levels first\r\n"; } if (string.IsNullOrEmpty(_generatedInputFull)) { Result += "No input. Prepare test first\r\n"; } if (_parserTest == null || string.IsNullOrEmpty(_generatedInputFull)) { return; } Result = "running"; // Prepare query test string letters = "bcdefg"; var levels = Benchmark.GetLevelListFromCode(); List <List <string> > sets = GetIdSets(levels); long sum = 0; Random rnd = new Random(); // Local function to add integer to sum. void AddAllCombinations2(int level, int number, TextElement elem) { if (level < sets.Count() - 1) { string find = sets[level][rnd.Next(levels[level])]; AddAllCombinations2(level + 1, number, elem.Codes().First(e => e.Name == letters.Substring(level, 1) && e.Codes().First().Value == find)); } else { CodeElement el = elem.Codes().First(e => e.Name == letters.Substring(level, 1) && e.Codes().First().Value == sets[level][number]); sum += int.Parse(el.Codes().First(e => e.Name == "int").Value); } } Action parse = () => { // ---------- MEMORY --------------- Process currentProcess = System.Diagnostics.Process.GetCurrentProcess(); long memoryUsage1 = currentProcess.WorkingSet64; // ---------- PARSING --------------- Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); CodeDocument doc = CodeDocument.Load(_parserTest, _generatedInputFull); stopWatch.Stop(); // ---------- MEMORY RESULT --------------- currentProcess = System.Diagnostics.Process.GetCurrentProcess(); long memoryUsage2 = (currentProcess.WorkingSet64 - memoryUsage1) / 1024; if (memoryUsage2 > int.MaxValue) { Result += "Memory use too large.\r\n"; } else { Benchmark.MemParse = newAvg(Benchmark.TestRuns, Benchmark.TimeParse, memoryUsage2); } if (stopWatch.Elapsed.TotalMilliseconds > int.MaxValue) { Result += "Parse time too long.\r\n"; } else { Benchmark.TimeParse = newAvg(Benchmark.TestRuns, Benchmark.TimeParse, stopWatch.Elapsed.TotalMilliseconds); } // ---------- QUERYING --------------- int number = -1; int lastLevel = levels.Last(); stopWatch = new Stopwatch(); stopWatch.Start(); for (long l = 0; l < Benchmark.Combi; l++) { if (++number == lastLevel) { number = 0; } AddAllCombinations2(0, number, doc); } stopWatch.Stop(); long expected = ((lastLevel - 1) * lastLevel) / 2; for (int i = 0; i < levels.Count - 1; i++) { expected *= levels[i]; } if (expected != sum) { throw new Exception("test sum is off"); } if (stopWatch.Elapsed.TotalMilliseconds > int.MaxValue) { Result += "Query time too long.\r\n"; } else { Benchmark.TimeQuery = newAvg(Benchmark.TestRuns, Benchmark.TimeQuery, stopWatch.Elapsed.TotalMilliseconds); } // ---------- OUTPUT --------------- Benchmark.TestRuns++; Result = "run " + Benchmark.TestRuns; if (BenchmarkCompare != null) { Result += "\r\n" + Benchmark.Compare(BenchmarkCompare); } }; Thread thread = new Thread(new ThreadStart(parse)); thread.Start(); }
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(); } }