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);
        }
Example #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()));
        }