예제 #1
0
파일: Concr.cs 프로젝트: pkolachi/gf-core
        /// <summary>
        /// Get the bracketed (structured) linearization of an expression.
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public Bracket BracketedLinearize(Expr e)
        {
            var tmp_pool = new NativeGU.NativeMemoryPool();
            var exn      = new NativeGU.NativeExceptionContext(tmp_pool);

            var cts = Native.pgf_lzr_concretize(Ptr, e.Ptr, exn.Ptr, tmp_pool.Ptr);

            var ctree = IntPtr.Zero;

            NativeGU.gu_enum_next(cts, ref ctree, tmp_pool.Ptr);

            if (ctree == IntPtr.Zero)
            {
                return(null);
            }

            ctree = Native.pgf_lzr_wrap_linref(ctree, tmp_pool.Ptr);

            var builder = new Bracket.BracketBuilder();

            var mem = Marshal.AllocHGlobal(Marshal.SizeOf <Native.PgfLinFuncs>());

            Marshal.StructureToPtr <Native.PgfLinFuncs>(builder.LinFuncs, mem, false);

            Native.pgf_lzr_linearize(Ptr, ctree, 0, ref mem, tmp_pool.Ptr);

            var b = builder.Build();

            Marshal.FreeHGlobal(mem);

            return(b);
        }
예제 #2
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));
                    }
                }
            }
        }
예제 #3
0
파일: Type.cs 프로젝트: pkolachi/gf-core
        internal static Type FromPtr(IntPtr type, NativeGU.NativeMemoryPool pool)
        {
            var t = new Type();

            t._ptr  = type;
            t._pool = pool;
            return(t);
        }
예제 #4
0
        internal MetaVariableExpr()
        {
            _pool = new NativeGU.NativeMemoryPool();
            IntPtr exprMetaPtr = NativeGU.gu_alloc_variant((byte)PgfExprTag.PGF_EXPR_META,
                                                           (UIntPtr)Marshal.SizeOf <NativePgfExprMeta>(), UIntPtr.Zero, ref _ptr, _pool.Ptr);

            Native.EditStruct <NativePgfExprMeta> (exprMetaPtr, (ref NativePgfExprMeta m) => m.Id = 0);
        }
예제 #5
0
파일: Literal.cs 프로젝트: pkolachi/gf-core
        public LiteralStringExpr(string s) : base()
        {
            _pool = new NativeGU.NativeMemoryPool();

            var    exprTag = (byte)(int)PgfExprTag.PGF_EXPR_LIT;
            IntPtr litPtr  = NativeGU.gu_alloc_variant(exprTag,
                                                       (UIntPtr)Marshal.SizeOf <NativePgfExprLit>(), UIntPtr.Zero, ref _ptr, _pool.Ptr);

            Native.EditStruct <NativePgfExprLit>(litPtr, (ref NativePgfExprLit lit) => {
                MkStringVariant((byte)PgfLiteralTag.PGF_LITERAL_STR, s, ref lit.lit);
            });
        }
예제 #6
0
        internal static Expr FromPtr(IntPtr expr, NativeGU.NativeMemoryPool pool)
        {
            var Tag = (PgfExprTag)NativeGU.gu_variant_open(expr).Tag;

            if (factories.ContainsKey(Tag))
            {
                return(factories[Tag](expr, pool));
            }
            else
            {
                return(new UnsupportedExpr(expr, pool));
            }
        }
예제 #7
0
파일: Concr.cs 프로젝트: pkolachi/gf-core
        /// <summary>
        /// Get all possible linearization for an expression (see <see cref="Linearize(Expression)"/>).
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public IEnumerable <string> LinearizeAll(Expr e)
        {
            var tmp_pool = new NativeGU.NativeMemoryPool();
            var exn      = new NativeGU.NativeExceptionContext(tmp_pool);

            var cts = Native.pgf_lzr_concretize(Ptr, e.Ptr, exn.Ptr, tmp_pool.Ptr);

            if (exn.IsRaised || cts == IntPtr.Zero)
            {
                throw new PGFError("Could not linearize the abstract tree.");
            }

            return(NativeGU.IteratorToIEnumerable(cts, tmp_pool.Ptr).Select(LinearizeCnc));
        }
예제 #8
0
파일: Literal.cs 프로젝트: pkolachi/gf-core
        internal void Initialize <TNative>(PgfLiteralTag litTag, Native.StructAction <TNative> setValue, UIntPtr?size = null)
        {
            _pool = new NativeGU.NativeMemoryPool();

            var    exprTag = (byte)(int)PgfExprTag.PGF_EXPR_LIT;
            IntPtr litPtr  = NativeGU.gu_alloc_variant(exprTag,
                                                       (UIntPtr)Marshal.SizeOf <NativePgfExprLit>(), UIntPtr.Zero, ref _ptr, _pool.Ptr);

            Native.EditStruct <NativePgfExprLit> (litPtr, (ref NativePgfExprLit lit) => {
                IntPtr ilitPtr = NativeGU.gu_alloc_variant((byte)litTag,
                                                           (UIntPtr)Marshal.SizeOf <TNative> (), UIntPtr.Zero, ref lit.lit, _pool.Ptr);

                Native.EditStruct <TNative>(ilitPtr, setValue);
            });
        }
예제 #9
0
파일: PGF.cs 프로젝트: pkolachi/gf-core
        /// <summary>
        /// Returns an enumerable over the set of all expression in
        /// the given category. The expressions are enumerated in decreasing
        /// probability order.
        /// </summary>
        /// <param name="cat">the start category.</param>
        /// <returns></returns>
        public IEnumerable <Expr> GenerateAll(Type cat = null)
        {
            cat = cat ?? StartCat;
            var    tmp_pool    = new NativeGU.NativeMemoryPool();
            var    exn         = new NativeGU.NativeExceptionContext(tmp_pool);
            var    result_pool = new NativeGU.NativeMemoryPool();
            IntPtr ptr         = IntPtr.Zero;
            var    iterator    = Native.pgf_generate_all(this._ptr, cat.Ptr, exn.Ptr, tmp_pool.Ptr, result_pool.Ptr);

            return(NativeGU.IteratorToIEnumerable(iterator, tmp_pool.Ptr).Select(p =>
            {
                var exprProb = Marshal.PtrToStructure <Native.PgfExprProb>(ptr);
                return Expr.FromPtr(exprProb.expr, result_pool);
            }));
        }
예제 #10
0
파일: PGF.cs 프로젝트: pkolachi/gf-core
        /// <summary>
        /// Normalizes an expression to its normal form by using the 'def' rules in the grammar.
        /// </summary>
        /// <param name="expr">the original expression.</param>
        /// <returns>the normalized expression.</returns>
        public Expr Compute(Expr expr)
        {
            var tmp_pool    = new NativeGU.NativeMemoryPool();
            var exn         = new NativeGU.NativeExceptionContext(tmp_pool);
            var result_pool = new NativeGU.NativeMemoryPool();
            var newExpr     = Native.pgf_compute(_ptr, expr.Ptr, exn.Ptr, pool.Ptr, result_pool.Ptr);

            if (exn.IsRaised || newExpr == IntPtr.Zero)
            {
                throw new PGFError("Could not reduce expression.");
            }
            else
            {
                return(Expr.FromPtr(newExpr, result_pool));
            }
        }
예제 #11
0
        public ApplicationExpr(string fname, IEnumerable <Expr> args)
        {
            _pool = new NativeGU.NativeMemoryPool();
            MkStringVariant((byte)PgfExprTag.PGF_EXPR_FUN, fname, ref _ptr);
            foreach (var arg in args)
            {
                var fun     = _ptr;
                var exprApp = NativeGU.gu_alloc_variant((byte)PgfExprTag.PGF_EXPR_APP,
                                                        (UIntPtr)Marshal.SizeOf <PgfExprApp>(), UIntPtr.Zero, ref _ptr, _pool.Ptr);

                Native.EditStruct <PgfExprApp> (exprApp, (ref PgfExprApp app) => {
                    app.Function = fun;
                    app.Argument = arg.Ptr;
                });
            }
        }
예제 #12
0
파일: Native.cs 프로젝트: pkolachi/gf-core
        /*
         * [StructLayout(LayoutKind.Sequential)]
         * public struct PGFClosure
         * {
         *  public GuMapItor fn;
         *  public IntPtr grammar;
         *  public IntPtr obj;
         * }*/

        public static string ReadString(Action <IntPtr, NativeGU.NativeExceptionContext> f)
        {
            var pool   = new NativeGU.NativeMemoryPool();
            var exn    = new NativeGU.NativeExceptionContext(pool);
            var sbuf   = NativeGU.gu_new_string_buf(pool.Ptr);
            var output = NativeGU.gu_string_buf_out(sbuf);

            f(output, exn);
            if (exn.IsRaised)
            {
                throw new Exception();
            }
            var strPtr = NativeGU.gu_string_buf_freeze(sbuf, pool.Ptr);
            var str    = Native.NativeString.StringFromNativeUtf8(strPtr);

            return(str);
        }
예제 #13
0
파일: Native.cs 프로젝트: pkolachi/gf-core
        public static void MapIter(MapIterFunc iter, IntPtr _pgf, Action <string, IntPtr> action)
        {
            var pool = new NativeGU.NativeMemoryPool();
            var exn  = new NativeGU.NativeExceptionContext(pool);
            var f    = new GuMapItor()
            {
                fn = (self, key, value, _err) =>
                {
                    action(Native.NativeString.StringFromNativeUtf8(key), value);
                    if (exn.IsRaised)
                    {
                        throw new Exception();
                    }
                }
            };

            iter(_pgf, ref f, exn.Ptr);
        }
예제 #14
0
파일: Concr.cs 프로젝트: pkolachi/gf-core
        /// <summary>
        /// Linearize expression, i.e. produce a string in the concrete grammar from an expression in the abstract grammar.
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public string Linearize(Expr e)
        {
            var tmp_pool = new NativeGU.NativeMemoryPool();
            var exn      = new NativeGU.NativeExceptionContext(tmp_pool);

            var buf  = NativeGU.gu_new_string_buf(tmp_pool.Ptr);
            var out_ = NativeGU.gu_string_buf_out(buf);

            Native.pgf_linearize(Ptr, e.Ptr, out_, exn.Ptr);
            if (exn.IsRaised)
            {
                throw new PGFError();
            }
            else
            {
                var cstr = NativeGU.gu_string_buf_freeze(buf, tmp_pool.Ptr);
                return(Native.NativeString.StringFromNativeUtf8(cstr));
            }
        }
예제 #15
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));
                }
            }
        }
예제 #16
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));
                }
            }
        }
예제 #17
0
파일: Concr.cs 프로젝트: pkolachi/gf-core
        private string LinearizeCnc(IntPtr cnc)
        {
            var tmp_pool = new NativeGU.NativeMemoryPool();
            var exn      = new NativeGU.NativeExceptionContext(tmp_pool);

            var sbuf = NativeGU.gu_new_string_buf(tmp_pool.Ptr);
            var out_ = NativeGU.gu_string_buf_out(sbuf);

            var wrapped = Native.pgf_lzr_wrap_linref(cnc, tmp_pool.Ptr);

            Native.pgf_lzr_linearize_simple(Ptr, wrapped, 0, out_, exn.Ptr, tmp_pool.Ptr);

            if (exn.IsRaised)
            {
                throw new PGFError("Could not linearize abstract tree.");
            }

            var cstr = NativeGU.gu_string_buf_freeze(sbuf, tmp_pool.Ptr);

            return(Native.NativeString.StringFromNativeUtf8(cstr));
        }
예제 #18
0
파일: Literal.cs 프로젝트: pkolachi/gf-core
        internal new static Expr FromPtr(IntPtr expr, NativeGU.NativeMemoryPool pool)
        {
            var dataPtr    = NativeGU.gu_variant_open(expr).Data; // PgfExprLit*
            var data       = Marshal.PtrToStructure <NativePgfExprLit>(dataPtr);
            var literalTag = (PgfLiteralTag)NativeGU.gu_variant_open(data.lit).Tag;

            switch (literalTag)
            {
            case PgfLiteralTag.PGF_LITERAL_STR:
                return(new LiteralStringExpr(expr, pool));

            case PgfLiteralTag.PGF_LITERAL_INT:
                return(new LiteralIntExpr(expr, pool));

            case PgfLiteralTag.PGF_LITERAL_FLT:
                return(new LiteralFloatExpr(expr, pool));

            default:
                throw new ArgumentException();
            }
        }
예제 #19
0
 internal FunctionExpr(IntPtr expr, NativeGU.NativeMemoryPool pool) : base(expr, pool)
 {
 }
예제 #20
0
 internal ApplicationExpr(IntPtr ptr, NativeGU.NativeMemoryPool pool) : base(ptr, pool)
 {
 }
예제 #21
0
 internal Expr(IntPtr ptr, NativeGU.NativeMemoryPool pool)
 {
     _ptr = ptr; _pool = pool;
 }
예제 #22
0
 internal UnsupportedExpr(IntPtr expr, NativeGU.NativeMemoryPool pool) : base(expr, pool)
 {
 }
예제 #23
0
 internal MetaVariableExpr(IntPtr ptr, NativeGU.NativeMemoryPool pool) : base(ptr, pool)
 {
 }
예제 #24
0
파일: Literal.cs 프로젝트: pkolachi/gf-core
 internal LiteralExpr(IntPtr expr, NativeGU.NativeMemoryPool pool) : base(expr, pool)
 {
 }