private static Babel.Parser.Parser ParseText(string text, bool trace = false)
        {
            Babel.Parser.ErrorHandler handler = new Babel.Parser.ErrorHandler();
            Babel.Lexer.Scanner       scanner = new Babel.Lexer.Scanner();
            Babel.Parser.Parser       parser  = new Babel.Parser.Parser(); // use noarg constructor
            parser.Trace    = trace;
            parser.scanner  = scanner;
            scanner.Handler = handler;
            parser.SetHandler(handler);
            scanner.SetSourceText(text);

            var request = new ParseRequest(true);

            request.Sink = new AuthoringSink(ParseReason.None, 0, 0, Babel.Parser.Parser.MaxErrors);
            parser.MBWInit(request);
            var result = parser.Parse();

            if (handler.Errors)
            {
                throw new Exception(handler.ToString());
            }
            return(parser);
        }
Beispiel #2
0
        /// <summary>
        /// Parses measures from the mdx script text.  If there is an error during parsing an exception is thrown.
        /// </summary>
        /// <param name="daxScript">mdx script text</param>
        /// <returns>Enumerable dax measures</returns>
        public static MeasuresContainer ParseDaxScript(string daxScript)
        {
            var handler = new Babel.Parser.ErrorHandler();
            var scanner = new Babel.Lexer.Scanner();
            var parser  = new Babel.Parser.Parser(); // use noarg constructor

            parser.scanner  = scanner;
            scanner.Handler = handler;
            parser.SetHandler(handler);
            scanner.SetSourceText(daxScript);

            var request = new ParseRequest(true);

            request.Sink = new AuthoringSink(ParseReason.None, 0, 0, Babel.Parser.Parser.MaxErrors);
            parser.MBWInit(request);
            var parseResult = parser.Parse();

            if (handler.Errors)
            {
                throw DaxParsingException.FromHandler(handler, daxScript);
            }

            //Add support measures if measure contains KPIs
            var measures                = parser.AllMeasures;
            var supportMeasures         = new List <DaxMeasure>();
            var containsSupportMeasures = measures.GetSupportingMeasures().Count() != 0;

            if (!containsSupportMeasures)
            {
                foreach (var measure in measures)
                {
                    if (measure.CalcProperty?.KPI == null)
                    {
                        continue;
                    }

                    if (!string.IsNullOrWhiteSpace(measure.CalcProperty.KPI.TargetExpression))
                    {
                        var newMeasure = new DaxMeasure(
                            "_" + measure.Name + " Goal",
                            measure.TableName,
                            measure.CalcProperty.KPI.TargetExpression
                            );
                        supportMeasures.Add(newMeasure);
                    }

                    if (!string.IsNullOrWhiteSpace(measure.CalcProperty.KPI.StatusExpression))
                    {
                        var newMeasure = new DaxMeasure(
                            "_" + measure.Name + " Status",
                            measure.TableName,
                            measure.CalcProperty.KPI.StatusExpression
                            );
                        supportMeasures.Add(newMeasure);
                    }

                    if (!string.IsNullOrWhiteSpace(measure.CalcProperty.KPI.TrendExpression))
                    {
                        var newMeasure = new DaxMeasure(
                            "_" + measure.Name + " Trend",
                            measure.TableName,
                            measure.CalcProperty.KPI.TrendExpression
                            );
                        supportMeasures.Add(newMeasure);
                    }
                }
            }

            return(new MeasuresContainer(measures.Union(supportMeasures).ToList()));
        }
        /// <summary>
        /// Main function of the parsing thread.
        /// This function waits on the queue of the parsing requests and build the parsing tree for
        /// a specific file. The resulting tree is built using LibraryNode objects so that it can
        /// be used inside the class view or object browser.
        /// </summary>
        private void ParseThread()
        {
            const int waitTimeout = 500;
             // Define the array of events this function is interest in.
             WaitHandle[] eventsToWait = new WaitHandle[] { requestPresent, shutDownStarted };
             // Execute the tasks.
             while (true)
             {
            // Wait for a task or a shutdown request.
            int waitResult = WaitHandle.WaitAny(eventsToWait, waitTimeout, false);
            if (1 == waitResult)
            {
               // The shutdown of this component is started, so exit the thread.
               return;
            }
            LibraryTask task = null;
            lock (requests)
            {
               if (0 != requests.Count)
               {
                  task = requests.Dequeue();
               }
               if (0 == requests.Count)
               {
                  requestPresent.Reset();
               }
            }
            if (task == null)
            {
               continue;
            }

            if (task.Text == null)
            {
               if (System.IO.File.Exists(task.FileName))
               {
                  var parser = new Babel.Parser.Parser();
                  handler.Clear();

                  using (var stream = File.OpenRead(task.FileName))
                  {
                     Babel.Lexer.Scanner scanner = new Babel.Lexer.Scanner(stream);

                     handler.SetFileName(task.FileName);

                     scanner.Handler = handler;
                     Debug.Assert(symbolTable != null);
                     parser.SetParsingInfo(task.FileName, symbolTable, handler);
                     parser.scanner = scanner;

                     //parser.MBWInit(req);
                     var retval = parser.Parse();
                     // the parse result is to fill the symbol table

                  }
               }
            }
            else
            {
               var parser = new Babel.Parser.Parser();
               handler.Clear();

               Babel.Lexer.Scanner scanner = new Babel.Lexer.Scanner(); // string interface

               handler.SetFileName(task.FileName);

               scanner.Handler = handler;
               Debug.Assert(symbolTable != null);
               parser.SetParsingInfo(task.Text, symbolTable, handler);
               parser.scanner = scanner;

               scanner.SetSource(task.Text, 0);

               //parser.MBWInit(req);
               parser.Parse();
            }
            //LibraryNode module = new LibraryNode(
            //        System.IO.Path.GetFileName(task.FileName),
            //        LibraryNode.LibraryNodeType.PhysicalContainer);
            //CreateModuleTree(module, module, scope, "", task.ModuleID);
            //if (null != task.ModuleID)
            //{
            //    LibraryNode previousItem = null;
            //    lock (files)
            //    {
            //        if (files.TryGetValue(task.ModuleID, out previousItem))
            //        {
            //            files.Remove(task.ModuleID);
            //        }
            //    }
            //    library.RemoveNode(previousItem);
            //}
            //library.AddNode(module);
            //if (null != task.ModuleID)
            //{
            //    lock (files)
            //    {
            //        files.Add(task.ModuleID, module);
            //    }
            //}
             }
        }