1# This is the Pegex grammar for Inline::C
2
3# Note:
4#
5# Use the following environment variables for dev:
6#
7#   export PERL_PEGEX_DEBUG=1
8#   export PERL_PEGEX_AUTO_COMPILE=1
9#
10# To recompile the grammar without AUTO COMPILE, just run:
11#
12#   perl -Ilib -MInline::C::ParsePegex::Grammar=compile
13#
14# And that will put changes to this file into Inline::C::ParsePegex::Grammar.
15
16%grammar inline-c
17%version 0.0.1
18
19# C code is 1 or more 'parts'
20code: part+
21
22# The only parts we care about are function definitions and declarations, but
23# not those inside a comment.
24part: =ALL (
25  | comment
26  | function_definition
27  | function_declaration
28  | anything_else
29)
30
31comment:
32  /- SLASH SLASH [^ BREAK ]* BREAK / |
33  /- SLASH STAR (: [^ STAR ]+ | STAR (! SLASH))* STAR SLASH ([ TAB ]*)? /
34
35# int foo_ () { return -1; }\n
36function_definition:
37  rtype /( identifier )/ -
38  LPAREN arg* % COMMA /- RPAREN - LCURLY -/
39
40function_declaration:
41  rtype /( identifier )/ -
42  LPAREN arg_decl* % COMMA /- RPAREN - SEMI -/
43
44rtype: /- (: rtype1 | rtype2 ) -/
45
46rtype1: / modifier*( type_identifier ) - ( STAR*) /
47
48rtype2: / modifier+ STAR*/
49
50arg: /(: type - ( identifier)|( DOT DOT DOT ))/
51
52arg_decl: /( type WS* identifier*| DOT DOT DOT )/
53
54type: / WS*(: type1 | type2 ) WS* /
55
56type1: / modifier*( type_identifier ) WS*( STAR* )/
57
58type2: / modifier* STAR* /
59
60modifier: /(: (:unsigned|long|extern|const)\b WS* )/
61
62identifier: /(: WORD+ )/
63
64type_identifier: /(: WORD+ )/
65
66anything_else: / ANY* (: EOL | EOS ) /
67