示例#1
0
文件: Concr.cs 项目: pkolachi/gf-core
        /// <summary>
        /// Parse given input string in the concrete grammar.
        /// </summary>
        /// <param name="str">Input string to be parsed.</param>
        /// <param name="cat">Category (Type) to parse.</param>
        /// <param name="heuristics">Heuristic (see the GF C runtime docs).</param>
        /// <param name="Callback1">Callback function.</param>
        /// <param name="Callback2">Callback function.</param>
        /// <returns>Enumerates pairs of (abstract grammar) expressions and corresponding probability.</returns>
        public IEnumerable <Tuple <Expr, float> > Parse(string str, Type cat = null, double?heuristics = null,
                                                        Action Callback1     = null, Action Callback2  = null)
        {
            var parse_pool = new NativeGU.NativeMemoryPool();
            var exn        = new NativeGU.NativeExceptionContext(parse_pool);

            cat = cat ?? PGF.StartCat;

            using (var nativeStr = new Native.NativeString(str))
            {
                var result_pool = new NativeGU.NativeMemoryPool();
                var callbackMap = Native.pgf_new_callbacks_map(this.Ptr, parse_pool.Ptr);

                var iterator = Native.pgf_parse_with_heuristics(this.Ptr, cat.Ptr,
                                                                nativeStr.Ptr, heuristics ?? -1, callbackMap,
                                                                exn.Ptr, parse_pool.Ptr, result_pool.Ptr);

                if (iterator == IntPtr.Zero || exn.IsRaised)
                {
                    throw new ParseError();
                }
                else
                {
                    foreach (var ptr in NativeGU.IteratorToIEnumerable(iterator, parse_pool.Ptr))
                    {
                        var exprProb = (Native.PgfExprProb)Marshal.PtrToStructure(ptr, typeof(Native.PgfExprProb));
                        yield return(Tuple.Create(Expr.FromPtr(exprProb.expr, result_pool),
                                                  exprProb.prob));
                    }
                }
            }
        }
示例#2
0
文件: PGF.cs 项目: pkolachi/gf-core
 /// <summary>
 /// Returns a list with all functions with a given return category.
 /// </summary>
 /// <param name="catName">The name of the return category.</param>
 public IEnumerable <string> FunctionsByCat(string catName)
 {
     using (var str = new Native.NativeString(catName))
     {
         return(GetStringList(new Native.IterFuncCurryName(Native.pgf_iter_functions_by_cat, str.Ptr).IterFunc));
     }
 }
示例#3
0
文件: PGF.cs 项目: pkolachi/gf-core
            public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
            {
                result = null;

                uint n_hypos = 0;

                using (var str = new Native.NativeString(binder.Name))
                {
                    var typePtr = Native.pgf_function_type(gr._ptr, str.Ptr);
                    if (typePtr == IntPtr.Zero)
                    {
                        return(false);
                    }
                    n_hypos = NativeGU.SeqLength(Marshal.PtrToStructure <Type.PgfType>(typePtr).hypos);
                }

                if (args.Length != n_hypos)
                {
                    return(false);
                }

                Expr[] exprs = new Expr[args.Length];
                for (var i = 0; i < args.Length; i++)
                {
                    exprs[i] = args[i] as Expr;
                    if (exprs[i] == null)
                    {
                        return(false);
                    }
                }
                result = new ApplicationExpr(binder.Name, exprs);
                return(true);
            }
示例#4
0
文件: PGF.cs 项目: pkolachi/gf-core
 /// <summary>
 /// Returns the type of the function with the given name.
 /// </summary>
 /// <param name="funName">The name of the function.</param>
 /// <returns></returns>
 public Type FunctionType(string funName)
 {
     using (var str = new Native.NativeString(funName))
     {
         var typePtr = Native.pgf_function_type(_ptr, str.Ptr);
         if (typePtr == IntPtr.Zero)
         {
             throw new NullReferenceException();
         }
         return(Type.FromPtr(typePtr, pool));
     }
 }
示例#5
0
文件: PGF.cs 项目: pkolachi/gf-core
            public override bool TryGetMember(GetMemberBinder binder, out object result)
            {
                result = null;

                using (var str = new Native.NativeString(binder.Name))
                {
                    var typePtr = Native.pgf_function_type(gr._ptr, str.Ptr);
                    if (typePtr == IntPtr.Zero)
                    {
                        return(false);
                    }
                }

                result = new ApplicationExpr(binder.Name, new Expr[0]);
                return(true);
            }
示例#6
0
        /// <summary>
        /// Read expression from string.
        /// </summary>
        /// <param name="exprStr"></param>
        /// <returns></returns>
        public static Expr ReadExpr(string exprStr)
        {
            var tmp_pool    = new NativeGU.NativeMemoryPool();
            var exn         = new NativeGU.NativeExceptionContext(tmp_pool);
            var result_pool = new NativeGU.NativeMemoryPool();

            using (var strNative = new Native.NativeString(exprStr))
            {
                var in_  = NativeGU.gu_data_in(strNative.Ptr, strNative.Size, tmp_pool.Ptr);
                var expr = Native.pgf_read_expr(in_, result_pool.Ptr, tmp_pool.Ptr, exn.Ptr);
                if (exn.IsRaised || expr == IntPtr.Zero)
                {
                    throw new PGFError();
                }
                else
                {
                    return(Expr.FromPtr(expr, result_pool));
                }
            }
        }
示例#7
0
文件: Type.cs 项目: pkolachi/gf-core
        /// <summary>
        /// Read type from string.
        /// </summary>
        /// <param name="typeStr"></param>
        /// <returns></returns>
        public static Type ReadType(string typeStr)
        {
            var tmp_pool    = new NativeGU.NativeMemoryPool();
            var exn         = new NativeGU.NativeExceptionContext(tmp_pool);
            var result_pool = new NativeGU.NativeMemoryPool();

            using (var strNative = new Native.NativeString(typeStr))
            {
                var in_ = NativeGU.gu_data_in(strNative.Ptr, strNative.Size, tmp_pool.Ptr);
                var typ = Native.pgf_read_type(in_, result_pool.Ptr, tmp_pool.Ptr, exn.Ptr);
                if (exn.IsRaised || typ == IntPtr.Zero)
                {
                    throw new PGFError();
                }
                else
                {
                    return(Type.FromPtr(typ, result_pool));
                }
            }
        }