<commit_before>! Copyright (C) 2008, 2010 <NAME>, <NAME>.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors alien alien.c-types alien.parser
alien.libraries arrays assocs classes combinators
combinators.short-circuit compiler.units effects grouping
kernel parser sequences splitting words fry locals lexer
namespaces summary math vocabs.parser ;
IN: alien.parser

: parse-c-type-name ( name -- word )
    dup search [ ] [ no-word ] ?if ;

: parse-array-type ( name -- dims c-type )
    "[" split unclip
    [ [ "]" ?tail drop parse-word ] map ] dip ;

: (parse-c-type) ( string -- type )
    {
        { [ dup "void" =         ] [ drop void ] }
        { [ CHAR: ] over member? ] [ parse-array-type parse-c-type-name prefix ] }
        { [ "*" ?tail            ] [ (parse-c-type) <pointer> ] }
        { [ dup search           ] [ parse-c-type-name ] }
        [ dup search [ ] [ no-word ] ?if ]
    } cond ;

: valid-c-type? ( c-type -- ? )
    { [ array? ] [ c-type-word? ] [ pointer? ] [ void? ] } 1|| ;

: parse-c-type ( string -- type )
    (parse-c-type) dup valid-c-type? [ no-c-type ] unless ;

: scan-c-type ( -- c-type )
    scan {
        { [ dup "{" = ] [ drop \ } parse-until >array ] }
        { [ dup "pointer:" = ] [ drop scan-c-type <pointer> ] }
        [ parse-c-type ]
    } cond ; 

: reset-c-type ( word -- )
    dup "struct-size" word-prop
    [ dup [ forget-class ] [ { "struct-size" } reset-props ] bi ] when
    {
        "c-type"
        "pointer-c-type"
        "callback-effect"
        "callback-library"
    } reset-props ;

: CREATE-C-TYPE ( -- word )
    scan current-vocab create {
        [ fake-definition ]
        [ set-word ]
        [ reset-c-type ]<commit_msg>have TYPEDEF:, STRUCT: etc. throw an error if you try to define a c type name ending with asterisk<commit_after>! Copyright (C) 2008, 2010 <NAME>, <NAME>.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors alien alien.c-types alien.parser
alien.libraries arrays assocs classes combinators
combinators.short-circuit compiler.units effects grouping
kernel parser sequences splitting words fry locals lexer
namespaces summary math vocabs.parser ;
IN: alien.parser

: parse-c-type-name ( name -- word )
    dup search [ ] [ no-word ] ?if ;

: parse-array-type ( name -- dims c-type )
    "[" split unclip
    [ [ "]" ?tail drop parse-word ] map ] dip ;

: (parse-c-type) ( string -- type )
    {
        { [ dup "void" =         ] [ drop void ] }
        { [ CHAR: ] over member? ] [ parse-array-type parse-c-type-name prefix ] }
        { [ "*" ?tail            ] [ (parse-c-type) <pointer> ] }
        { [ dup search           ] [ parse-c-type-name ] }
        [ dup search [ ] [ no-word ] ?if ]
    } cond ;

: valid-c-type? ( c-type -- ? )
    { [ array? ] [ c-type-word? ] [ pointer? ] [ void? ] } 1|| ;

: parse-c-type ( string -- type )
    (parse-c-type) dup valid-c-type? [ no-c-type ] unless ;

: scan-c-type ( -- c-type )
    scan {
        { [ dup "{" = ] [ drop \ } parse-until >array ] }
        { [ dup "pointer:" = ] [ drop scan-c-type <pointer> ] }
        [ parse-c-type ]
    } cond ; 

: reset-c-type ( word -- )
    dup "struct-size" word-prop
    [ dup [ forget-class ] [ { "struct-size" } reset-props ] bi ] when
    {
        "c-type"
        "pointer-c-type"
        "callback-effect"
        "callback-library"
    } reset-props ;

ERROR: *-in-c-type-name name ;

: validate-c-type-name ( name -- name )
    dup "*" tail?
    [ *-in-c-type-name ] when ;

: CREATE-C-TYPE ( -- word )
    scan validate-c-type-name current-vocab create {
        [ fake-definition ]
        [ set-word ]
        [ reset-c-type ]
{'fraction_non_alphanumeric': 0.16580592552324, 'fraction_numerical': 0.004892633867898886, 'mean_word_length': 2.00163132137031, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2864326', 'n_tokens_mistral': 1294, 'n_tokens_neox': 1257, 'n_words': 299}