/// <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 <Expression, 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 ?? Grammar.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 PGF.Exceptions.ParseErrorException(); } 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(Expression.FromPtr(exprProb.expr, result_pool), exprProb.prob)); } } } }
/// <summary> /// All functions producing the given category name. /// </summary> /// <param name="catName"></param> /// <returns></returns> public IEnumerable <string> FunctionByCategory(string catName) { using (var str = new Native.NativeString(catName)) { return(GetStringList(new Native.IterFuncCurryName(Native.pgf_iter_functions_by_cat, str.Ptr).IterFunc)); } }
/// <summary> /// Get type from function name. /// </summary> /// <param name="funName"></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)); } }
private T Read <T>(Func <IntPtr, IntPtr, IntPtr, IntPtr> reader, Func <IntPtr, NativeGU.NativeMemoryPool, T> factory, string str) { 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(str)) { var in_ = NativeGU.gu_data_in(strNative.Ptr, strNative.Size, tmp_pool.Ptr); var expr = reader(in_, result_pool.Ptr, exn.Ptr); if (exn.IsRaised || expr == IntPtr.Zero) { throw new PGF.Exceptions.ParseErrorException(); } else { return(factory(expr, result_pool)); } } }