]> gitweb.factorcode.org Git - factor.git/blob - basis/alien/parser/parser.factor
disable parsing of string c-types
[factor.git] / basis / alien / parser / parser.factor
1 ! Copyright (C) 2008, 2009 Slava Pestov, Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien alien.c-types arrays assocs
4 combinators combinators.short-circuit effects grouping
5 kernel parser sequences splitting words fry locals lexer
6 namespaces summary math vocabs.parser ;
7 IN: alien.parser
8
9 : parse-c-type-name ( name -- word )
10     dup search [ nip ] [ no-word ] if* ;
11
12 : parse-c-type ( string -- array )
13     {
14         { [ dup "void" =            ] [ drop void ] }
15         { [ CHAR: ] over member?    ] [ parse-array-type parse-c-type-name prefix ] }
16         { [ dup search c-type-word? ] [ parse-c-type-name ] }
17         { [ "**" ?tail              ] [ drop void* ] }
18         { [ "*" ?tail               ] [ parse-c-type-name resolve-pointer-type ] }
19         [ parse-c-type-name no-c-type ]
20     } cond ;
21
22 : scan-c-type ( -- c-type )
23     scan dup "{" =
24     [ drop \ } parse-until >array ]
25     [ parse-c-type ] if ; 
26
27 : reset-c-type ( word -- )
28     { "c-type" "pointer-c-type" "callback-effect" "callback-abi" } reset-props ;
29
30 : CREATE-C-TYPE ( -- word )
31     scan current-vocab create dup reset-c-type ;
32
33 : normalize-c-arg ( type name -- type' name' )
34     [ length ]
35     [
36         [ CHAR: * = ] trim-head
37         [ length - CHAR: * <array> append ] keep
38     ] bi
39     [ parse-c-type ] dip ;
40
41 : parse-arglist ( parameters return -- types effect )
42     [
43         2 group [ first2 normalize-c-arg 2array ] map
44         unzip [ "," ?tail drop ] map
45     ]
46     [ [ { } ] [ 1array ] if-void ]
47     bi* <effect> ;
48
49 : function-quot ( return library function types -- quot )
50     '[ _ _ _ _ alien-invoke ] ;
51
52 :: make-function ( return! library function! parameters -- word quot effect )
53     return function normalize-c-arg function! return!
54     function create-in dup reset-generic
55     return library function
56     parameters return parse-arglist [ function-quot ] dip ;
57
58 : parse-arg-tokens ( -- tokens )
59     ";" parse-tokens [ "()" subseq? not ] filter ;
60
61 : (FUNCTION:) ( -- word quot effect )
62     scan "c-library" get scan parse-arg-tokens make-function ;
63
64 : define-function ( return library function parameters -- )
65     make-function define-declared ;
66
67 : callback-quot ( return types abi -- quot )
68     [ [ ] 3curry dip alien-callback ] 3curry ;
69
70 :: make-callback-type ( abi return! type-name! parameters -- word quot effect )
71     return type-name normalize-c-arg type-name! return!
72     type-name current-vocab create :> type-word 
73     type-word [ reset-generic ] [ reset-c-type ] bi
74     void* type-word typedef
75     parameters return parse-arglist :> callback-effect :> types
76     type-word callback-effect "callback-effect" set-word-prop
77     type-word abi "callback-abi" set-word-prop
78     type-word return types abi callback-quot (( quot -- alien )) ;
79
80 : (CALLBACK:) ( abi -- word quot effect )
81     scan scan parse-arg-tokens make-callback-type ;
82
83 PREDICATE: alien-function-word < word
84     def>> {
85         [ length 5 = ]
86         [ last \ alien-invoke eq? ]
87     } 1&& ;
88
89 PREDICATE: alien-callback-type-word < typedef-word
90     "callback-effect" word-prop ;
91