Use centrally-defined token sets to recognize the beginning of declarations.

This commit is contained in:
Stephen Heumann 2020-01-18 13:16:44 -06:00
parent 0f3bb11d22
commit dbe330a7b1
3 changed files with 16 additions and 38 deletions

8
CC.pas
View File

@ -109,13 +109,7 @@ NextToken; {get the first token in the program}
while token.kind <> eofsy do begin {compile the program} while token.kind <> eofsy do begin {compile the program}
if doingFunction then if doingFunction then
DoStatement DoStatement
else if (token.kind in [autosy,externsy,registersy,staticsy,typedefsy, else if token.kind in topLevelDeclarationStart then
unsignedsy,signedsy,intsy,longsy,charsy,shortsy,
floatsy,doublesy,compsy,extendedsy,enumsy,
structsy,unionsy,typedef,voidsy,inlinesy,volatilesy,
constsy,ident,asmsy,pascalsy,asmsy,segmentsy,
_Static_assertsy])
then
DoDeclaration(false) DoDeclaration(false)
else begin else begin
Error(26); Error(26);

View File

@ -505,6 +505,7 @@ var
{syntactic classes of tokens} {syntactic classes of tokens}
{---------------------------} {---------------------------}
specifierQualifierListElement: tokenSet; specifierQualifierListElement: tokenSet;
topLevelDeclarationStart: tokenSet;
{---------------------------------------------------------------} {---------------------------------------------------------------}

View File

@ -182,8 +182,9 @@ var
{syntactic classes of tokens} {syntactic classes of tokens}
{---------------------------} {---------------------------}
{ specifierQualifierListElement: tokenSet; (in CCommon)} { specifierQualifierListElement: tokenSet; (in CCommon)}
{ topLevelDeclarationStart: tokenSet; (in CCommon)}
localDeclarationStart: tokenSet;
declarationSpecifiersElement: tokenSet; declarationSpecifiersElement: tokenSet;
declarationStart: tokenSet;
structDeclarationStart: tokenSet; structDeclarationStart: tokenSet;
{-- Parser Utility Procedures ----------------------------------} {-- Parser Utility Procedures ----------------------------------}
@ -610,11 +611,7 @@ var
if c99Scope then PushTable; if c99Scope then PushTable;
Match(lparench,13); {evaluate the start condition} Match(lparench,13); {evaluate the start condition}
if allowMixedDeclarations if allowMixedDeclarations and (token.kind in localDeclarationStart) then begin
and (token.kind in [autosy,registersy,unsignedsy,signedsy,intsy,longsy,
charsy,shortsy,floatsy,doublesy,compsy,extendedsy,enumsy,
structsy,unionsy,typedef,voidsy,volatilesy,constsy,
externsy,staticsy,typedefsy,_Static_assertsy]) then begin
doingForLoopClause1 := true; doingForLoopClause1 := true;
DoDeclaration(false); DoDeclaration(false);
doingForLoopClause1 := false; doingForLoopClause1 := false;
@ -1379,12 +1376,8 @@ var
token.class := reservedSymbol; token.class := reservedSymbol;
end; {else} end; {else}
end; {if} end; {if}
if token.kind in {see if we are doing a prototyped list} {see if we are doing a prototyped list}
[autosy,externsy,registersy,staticsy,typedefsy,unsignedsy,intsy, if token.kind in declarationSpecifiersElement then begin
longsy,charsy,shortsy,floatsy,doublesy,compsy,extendedsy,voidsy,
enumsy,structsy,unionsy,typedef,signedsy,constsy,volatilesy]
then begin
{handle a prototype variable list} {handle a prototype variable list}
numberOfParameters := 0; {don't allow K&R parm declarations} numberOfParameters := 0; {don't allow K&R parm declarations}
luseGlobalPool := useGlobalPool; {use global memory} luseGlobalPool := useGlobalPool; {use global memory}
@ -1396,12 +1389,7 @@ var
with tPtr2^ do begin with tPtr2^ do begin
prototyped := true; {it is prototyped} prototyped := true; {it is prototyped}
repeat {collect the declarations} repeat {collect the declarations}
if (token.kind in [autosy,externsy,registersy,staticsy, if token.kind in declarationSpecifiersElement then begin
typedefsy,unsignedsy,signedsy,intsy,longsy,
charsy,shortsy,floatsy,doublesy,compsy,
extendedsy,enumsy,structsy,unionsy,
typedef,voidsy,volatilesy,constsy])
then begin
lLastParameter := lastParameter; lLastParameter := lastParameter;
DoDeclaration(true); DoDeclaration(true);
lastParameter := lLastParameter; lastParameter := lLastParameter;
@ -3519,11 +3507,7 @@ if isFunction then begin
{declare the parameters} {declare the parameters}
lp := lastParameter; {(save now; it's volatile)} lp := lastParameter; {(save now; it's volatile)}
while not (token.kind in [lbracech,eofsy]) do while not (token.kind in [lbracech,eofsy]) do
if (token.kind in [autosy,externsy,registersy,staticsy,typedefsy, if token.kind in declarationSpecifiersElement then
unsignedsy,signedsy,intsy,longsy,charsy,shortsy,
floatsy,doublesy,compsy,extendedsy,enumsy,
structsy,unionsy,typedef,voidsy,volatilesy,
constsy,ident]) then
DoDeclaration(false) DoDeclaration(false)
else begin else begin
Error(27); Error(27);
@ -3927,11 +3911,7 @@ case statementList^.kind of
EndCompoundStatement; EndCompoundStatement;
end {if} end {if}
else if (statementList^.doingDeclaration or allowMixedDeclarations) else if (statementList^.doingDeclaration or allowMixedDeclarations)
and (token.kind in [autosy,externsy,registersy,staticsy,typedefsy, and (token.kind in localDeclarationStart)
unsignedsy,signedsy,intsy,longsy,charsy,shortsy,
floatsy,doublesy,compsy,extendedsy,enumsy,
structsy,unionsy,typedef,voidsy,volatilesy,
constsy,_Static_assertsy])
then begin then begin
hasStatementNext := false; hasStatementNext := false;
if token.kind <> typedef then if token.kind <> typedef then
@ -4312,13 +4292,16 @@ alignmentSpecifiers := [_Alignassy];
declarationSpecifiersElement := typeSpecifierStart + storageClassSpecifiers declarationSpecifiersElement := typeSpecifierStart + storageClassSpecifiers
+ typeQualifiers + functionSpecifiers + alignmentSpecifiers; + typeQualifiers + functionSpecifiers + alignmentSpecifiers;
declarationStart :=
declarationSpecifiersElement + [_Static_assertsy,segmentsy];
specifierQualifierListElement := specifierQualifierListElement :=
typeSpecifierStart + typeQualifiers + alignmentSpecifiers; typeSpecifierStart + typeQualifiers + alignmentSpecifiers;
structDeclarationStart := specifierQualifierListElement + [_Static_assertsy]; structDeclarationStart := specifierQualifierListElement + [_Static_assertsy];
topLevelDeclarationStart :=
declarationSpecifiersElement + [ident,segmentsy,_Static_assertsy];
localDeclarationStart :=
declarationSpecifiersElement + [_Static_assertsy] - [asmsy];
end; {InitParser} end; {InitParser}