3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-19 10:29:30 +01:00

CGrammar: Fix struct member declarations, pluralize "pointers to" when applicable

This commit is contained in:
Pragmatic Software 2014-06-17 18:20:13 +00:00
parent c40fd81bb0
commit 77f1005e03
2 changed files with 103 additions and 56 deletions

View File

@ -13,8 +13,8 @@ use warnings;
# These are set automatically by the build/commit script # These are set automatically by the build/commit script
use constant { use constant {
BUILD_NAME => "PBot", BUILD_NAME => "PBot",
BUILD_REVISION => 647, BUILD_REVISION => 648,
BUILD_DATE => "2014-06-16", BUILD_DATE => "2014-06-17",
}; };
1; 1;

View File

@ -626,7 +626,7 @@ declaration:
if(@init_list > 1) { if(@init_list > 1) {
my $last = pop @init_list; my $last = pop @init_list;
$init_declaration_list = join(', ', @init_list) . ' and ' . $last; $init_declaration_list = join(', ', @init_list) . ' and ' . $last;
push $last, @init_list; push @init_list, $last;
} else { } else {
$init_declaration_list = $init_list[0]; $init_declaration_list = $init_list[0];
} }
@ -635,8 +635,10 @@ declaration:
push @defined_types, @init_list; push @defined_types, @init_list;
} else { } else {
my $inits = 0;
while(@init_list) { while(@init_list) {
$return .= "Let "; $inits++;
$return .= "Let " unless $arg{context} eq 'struct member';
my $first_object = shift @init_list; my $first_object = shift @init_list;
my @args = split /\|/, $first_object, 3; my @args = split /\|/, $first_object, 3;
@ -704,60 +706,95 @@ declaration:
splice @init_list, $i--, 1; splice @init_list, $i--, 1;
} }
my $and = @identifiers > 1 ? ' and ' : ''; if($arg{context} eq 'struct member') {
my $comma = ''; if($inits > 1 and not @init_list) {
for(my $i = 0; $i < @identifiers; $i++) { $return .= ' and ';
if($i == @identifiers - 1) { } elsif($inits > 1) {
$return .= "$and$identifiers[$i]"; $return .= ', ';
}
if($first_qualifier) {
if(@identifiers == 1 and $first_qualifier !~ /^(a|an)\s+/) {
$return .= $first_qualifier =~ m/^[aeiouy]/ ? 'an ' : 'a ';
} else {
$first_qualifier =~ s/pointer/pointers/;
}
$return .= "$first_qualifier $item{declaration_specifiers} ";
} else { } else {
$return .= "$comma$identifiers[$i]"; if(@identifiers == 1 and $item{declaration_specifiers} !~ /^(a|an)\s+/) {
$comma = ', '; $return .= $item{declaration_specifiers} =~ m/^[aeiouy]/ ? 'an ' : 'a ';
}
$return .= "$item{declaration_specifiers} ";
} }
}
$return .= ' be '; my $and = @identifiers > 1 ? ' and ' : '';
my $comma = '';
if($first_qualifier) { for(my $i = 0; $i < @identifiers; $i++) {
if(@identifiers == 1 and $first_qualifier !~ /^(a|an)\s+/) { if($i == @identifiers - 1) {
$return .= $first_qualifier =~ m/^[aeiouy]/ ? 'an ' : 'a '; $return .= "$and$identifiers[$i]";
} else {
$return .= "$comma$identifiers[$i]";
$comma = ', ';
}
} }
$return .= "$first_qualifier $item{declaration_specifiers}";
} else { } else {
if(@identifiers == 1 and $item{declaration_specifiers} !~ /^(a|an)\s+/) { my $and = @identifiers > 1 ? ' and ' : '';
$return .= $item{declaration_specifiers} =~ m/^[aeiouy]/ ? 'an ' : 'a '; my $comma = '';
for(my $i = 0; $i < @identifiers; $i++) {
if($i == @identifiers - 1) {
$return .= "$and$identifiers[$i]";
} else {
$return .= "$comma$identifiers[$i]";
$comma = ', ';
}
} }
$return .= "$item{declaration_specifiers}";
}
if(@initializers) { $return .= ' be ';
if(@identifiers > 1) {
$return .= ".\nInitialize ";
@initializers = sort { $a->[1] cmp $b->[1] } @initializers; if($first_qualifier) {
my ($and, $comma); if(@identifiers == 1 and $first_qualifier !~ /^(a|an)\s+/) {
$return .= $first_qualifier =~ m/^[aeiouy]/ ? 'an ' : 'a ';
} else {
$first_qualifier =~ s/pointer/pointers/;
}
$return .= "$first_qualifier $item{declaration_specifiers}";
} else {
if(@identifiers == 1 and $item{declaration_specifiers} !~ /^(a|an)\s+/) {
$return .= $item{declaration_specifiers} =~ m/^[aeiouy]/ ? 'an ' : 'a ';
}
$return .= "$item{declaration_specifiers}";
}
for(my $i = 0; $i < @initializers; $i++) { if(@initializers) {
my ($identifier, $initializer) = @{$initializers[$i]}; if(@identifiers > 1) {
$return .= ".\nInitialize ";
if($i < @initializers - 1 and $initializer eq $initializers[$i + 1]->[1]) { @initializers = sort { $a->[1] cmp $b->[1] } @initializers;
$return .= "$comma$identifier"; my ($and, $comma);
$comma = ', ';
$and = ' and '; for(my $i = 0; $i < @initializers; $i++) {
} else { my ($identifier, $initializer) = @{$initializers[$i]};
$initializer =~ s/^initialized to //;
$return .= "$and$identifier to $initializer"; if($i < @initializers - 1 and $initializer eq $initializers[$i + 1]->[1]) {
if($i < @initializers - 2) { $return .= "$comma$identifier";
$and = $comma = ', '; $comma = ', ';
} else {
$and = ' and '; $and = ' and ';
} else {
$initializer =~ s/^initialized to //;
$return .= "$and$identifier to $initializer";
if($i < @initializers - 2) {
$and = $comma = ', ';
} else {
$and = ' and ';
}
} }
} }
} else {
$return .= " $initializers[0]->[1]";
} }
} else {
$return .= " $initializers[0]->[1]";
} }
$return .= ".\n";
} }
$return .= ".\n";
} }
} }
} }
@ -1272,10 +1309,30 @@ struct_declaration_list:
} }
} }
bitfield_identifier:
identifier ':' constant
{ $return = [$item{identifier}, $item{constant}]; }
struct_declaration: struct_declaration:
specifier_qualifier_list ';' declaration_specifiers <leftop: bitfield_identifier ',' bitfield_identifier> ';'
| comment(?) specifier_qualifier_list struct_declarator_list ';' {
{ $return = join('', @{$item{'comment(?)'}}) . $item{specifier_qualifier_list} . ' ' . $item{struct_declarator_list}; } $return = "$item{declaration_specifiers} ";
my @bitfields = @{$item[-2]};
my $sep = '';
for(my $i = 0; $i < @bitfields; $i++) {
my $plural = $bitfields[$i]->[1] == 1 ? '' : 's';
$return .= "$sep" . "bit-field of $bitfields[$i]->[1] bit$plural $bitfields[$i]->[0]";
if($i == $#bitfields - 1) {
$sep = ' and ';
} else {
$sep = ', ';
}
}
}
| comment(s?) declaration[context => 'struct member'] comment(s?)
{ $return = join('', @{$item[1]}) . $item{declaration} . join('', @{$item[-1]}); }
type_name: type_name:
specifier_qualifier_list abstract_declarator(?) specifier_qualifier_list abstract_declarator(?)
@ -1288,16 +1345,6 @@ specifier_qualifier_list:
$return .= ' ' . join('', @{$item{'specifier_qualifier_list(?)'}}) if @{$item{'specifier_qualifier_list(?)'}}; $return .= ' ' . join('', @{$item{'specifier_qualifier_list(?)'}}) if @{$item{'specifier_qualifier_list(?)'}};
} }
struct_declarator_list:
struct_declarator
| struct_declarator ',' struct_declarator_list
{ $return = $item{struct_declarator} . join('',@{$item{struct_declarator_list}}); }
struct_declarator:
declarator
| declarator(?) ':' constant_expression
{ $return = join('',@{$item{'declarator(?)'}}) . " a bit field $item{constant_expression}"; }
struct_or_union: struct_or_union:
comment(?) ('struct' comment(?) ('struct'
{ $return = 'a structure'; } { $return = 'a structure'; }