Пример #1
0
        public static EType Parse(string densityStr)
        {
            if (string.IsNullOrEmpty(densityStr))
            {
                var res = new DensityExpressionResult <FieldType <int, PType>, int, PType> {
                    ErrorString = "Empty expression string!"
                };
                return(res);
            }
            try
            {
                var input  = new AntlrInputStream(densityStr);
                var lexer  = new DensityExpressionGrammarLexer(input);
                var tokens = new CommonTokenStream(lexer);
                var parser = new DensityExpressionGrammarParser(tokens);

                var ctx     = parser.compileUnit();
                var visitor = new DieVisitor();
                var res     = visitor.VisitCompileUnit(ctx);
                return(res);
            } catch (Exception e)
            {
                var res = new DensityExpressionResult <FieldType <int, PType>, int, PType> {
                    ErrorString = e.Message
                };
                return(res);
            }
        }
Пример #2
0
        virtual public DensityExpressionResult <G, M, RF> VisitCompileUnit(DensityExpressionGrammarParser.CompileUnitContext ctx)
        {
            var isDensity     = ctx.density() != null;
            var isProbability = ctx.probability() != null;
            var density       = isDensity
                ? VisitDensity(ctx.density())
                : null;
            var probability = isProbability
                ? VisitProbability(ctx.probability())
                : (RF?)null;
            var errorString = (isDensity || isProbability)
                ? null
                : "No valid Density or Probability!";

            var res = new DensityExpressionResult <G, M, RF>
            {
                Density     = density,
                Probability = probability,
                ErrorString = errorString
            };

            return(res);
        }