| 1 | package Inline::C::ParseRecDescent; | ||
| 2 | use strict; | ||
| 3 | use Carp; | ||
| 4 | |||
| 5 | sub register { | ||
| 6 | { | ||
| 7 | extends => [qw(C)], | ||
| 8 | overrides => [qw(get_parser)], | ||
| 9 | } | ||
| 10 | } | ||
| 11 | |||
| 12 | sub get_parser { | ||
| 13 | my $o = shift; | ||
| 14 | Inline::C::_parser_test("Inline::C::ParseRecDescent::get_parser called\n") if $o->{CONFIG}{_TESTING}; | ||
| 15 | eval { require Parse::RecDescent }; | ||
| 16 | croak <<END if $@; | ||
| 17 | This innvocation of Inline requires the Parse::RecDescent module. | ||
| 18 | $@ | ||
| 19 | END | ||
| 20 | $main::RD_HINT++; | ||
| 21 | Parse::RecDescent->new(grammar()) | ||
| 22 | } | ||
| 23 | |||
| 24 | sub grammar { | ||
| 25 | <<'END'; | ||
| 26 | |||
| 27 | code: part(s) | ||
| 28 | { | ||
| 29 | return 1; | ||
| 30 | } | ||
| 31 | |||
| 32 | part: comment | ||
| 33 | | function_definition | ||
| 34 | { | ||
| 35 | my $function = $item[1][0]; | ||
| 36 | $return = 1, last if $thisparser->{data}{done}{$function}++; | ||
| 37 | push @{$thisparser->{data}{functions}}, $function; | ||
| 38 | $thisparser->{data}{function}{$function}{return_type} = | ||
| 39 | $item[1][1]; | ||
| 40 | $thisparser->{data}{function}{$function}{arg_types} = | ||
| 41 | [map {ref $_ ? $_->[0] : '...'} @{$item[1][2]}]; | ||
| 42 | $thisparser->{data}{function}{$function}{arg_names} = | ||
| 43 | [map {ref $_ ? $_->[1] : '...'} @{$item[1][2]}]; | ||
| 44 | } | ||
| 45 | | function_declaration | ||
| 46 | { | ||
| 47 | $return = 1, last unless $thisparser->{data}{AUTOWRAP}; | ||
| 48 | my $function = $item[1][0]; | ||
| 49 | $return = 1, last if $thisparser->{data}{done}{$function}++; | ||
| 50 | my $dummy = 'arg1'; | ||
| 51 | push @{$thisparser->{data}{functions}}, $function; | ||
| 52 | $thisparser->{data}{function}{$function}{return_type} = | ||
| 53 | $item[1][1]; | ||
| 54 | $thisparser->{data}{function}{$function}{arg_types} = | ||
| 55 | [map {ref $_ ? $_->[0] : '...'} @{$item[1][2]}]; | ||
| 56 | $thisparser->{data}{function}{$function}{arg_names} = | ||
| 57 | [map {ref $_ ? ($_->[1] || $dummy++) : '...'} @{$item[1][2]}]; | ||
| 58 | } | ||
| 59 | | anything_else | ||
| 60 | |||
| 61 | comment: | ||
| 62 | m{\s* // [^\n]* \n }x | ||
| 63 | | m{\s* /\* (?:[^*]+|\*(?!/))* \*/ ([ \t]*)? }x | ||
| 64 | |||
| 65 | function_definition: | ||
| 66 | rtype IDENTIFIER '(' <leftop: arg ',' arg>(s?) ')' '{' | ||
| 67 | { | ||
| 68 | [@item[2,1], $item[4]] | ||
| 69 | } | ||
| 70 | |||
| 71 | function_declaration: | ||
| 72 | rtype IDENTIFIER '(' <leftop: arg_decl ',' arg_decl>(s?) ')' ';' | ||
| 73 | { | ||
| 74 | [@item[2,1], $item[4]] | ||
| 75 | } | ||
| 76 | |||
| 77 | rtype: rtype1 | rtype2 | ||
| 78 | |||
| 79 | rtype1: modifier(s?) TYPE star(s?) | ||
| 80 | { | ||
| 81 | $return = $item[2]; | ||
| 82 | $return = join ' ',@{$item[1]},$return | ||
| 83 | if @{$item[1]} and $item[1][0] ne 'extern'; | ||
| 84 | $return .= join '',' ',@{$item[3]} if @{$item[3]}; | ||
| 85 | return undef unless (defined $thisparser->{data}{typeconv} | ||
| 86 | {valid_rtypes}{$return}); | ||
| 87 | } | ||
| 88 | |||
| 89 | rtype2: modifier(s) star(s?) | ||
| 90 | { | ||
| 91 | $return = join ' ',@{$item[1]}; | ||
| 92 | $return .= join '',' ',@{$item[2]} if @{$item[2]}; | ||
| 93 | return undef unless (defined $thisparser->{data}{typeconv} | ||
| 94 | {valid_rtypes}{$return}); | ||
| 95 | } | ||
| 96 | |||
| 97 | arg: type IDENTIFIER {[@item[1,2]]} | ||
| 98 | | '...' | ||
| 99 | |||
| 100 | arg_decl: | ||
| 101 | type IDENTIFIER(s?) {[$item[1], $item[2][0] || '']} | ||
| 102 | | '...' | ||
| 103 | |||
| 104 | type: type1 | type2 | ||
| 105 | |||
| 106 | type1: modifier(s?) TYPE star(s?) | ||
| 107 | { | ||
| 108 | $return = $item[2]; | ||
| 109 | $return = join ' ',@{$item[1]},$return if @{$item[1]}; | ||
| 110 | $return .= join '',' ',@{$item[3]} if @{$item[3]}; | ||
| 111 | return undef unless (defined $thisparser->{data}{typeconv} | ||
| 112 | {valid_types}{$return}); | ||
| 113 | } | ||
| 114 | |||
| 115 | type2: modifier(s) star(s?) | ||
| 116 | { | ||
| 117 | $return = join ' ',@{$item[1]}; | ||
| 118 | $return .= join '',' ',@{$item[2]} if @{$item[2]}; | ||
| 119 | return undef unless (defined $thisparser->{data}{typeconv} | ||
| 120 | {valid_types}{$return}); | ||
| 121 | } | ||
| 122 | |||
| 123 | modifier: | ||
| 124 | 'unsigned' | 'long' | 'extern' | 'const' | ||
| 125 | |||
| 126 | star: '*' | ||
| 127 | |||
| 128 | IDENTIFIER: | ||
| 129 | /\w+/ | ||
| 130 | |||
| 131 | TYPE: /\w+/ | ||
| 132 | |||
| 133 | anything_else: | ||
| 134 | /.*/ | ||
| 135 | |||
| 136 | END | ||
| 137 | } | ||
| 138 | |||
| 139 | my $hack = sub { # Appease -w using Inline::Files | ||
| 140 | print Parse::RecDescent::IN ''; | ||
| 141 | print Parse::RecDescent::IN ''; | ||
| 142 | print Parse::RecDescent::TRACE_FILE ''; | ||
| 143 | print Parse::RecDescent::TRACE_FILE ''; | ||
| 144 | }; | ||
| 145 | |||
| 146 | 1; | ||
| 147 | |||
| 148 | __DATA__ |