Skip to content

Commit

Permalink
(#72) Parser: new TODOs, code cleanup of old hacks
Browse files Browse the repository at this point in the history
  • Loading branch information
ForNeVeR committed Feb 15, 2022
1 parent e1cc91d commit 8100762
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 49 deletions.
7 changes: 1 addition & 6 deletions Cesium.Ast/Declarations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,8 @@ public interface IDirectDeclarator
{
IDirectDeclarator? Base { get; }
}
public record IdentifierDirectDeclarator(string? Identifier) : IDirectDeclarator
public record IdentifierDirectDeclarator(string Identifier) : IDirectDeclarator
{
// HACK: This property is only mutable from CParser.TypeDefNameIdentifierHack.
// This requirement is caused by an issue https://github.com/LanguageDev/Yoakke/issues/138
// TODO: Eventually, we should get rid of that.
public string? Identifier { get; set; } = Identifier;

public IDirectDeclarator? Base => null;
}
public record ArrayDirectDeclarator(
Expand Down
57 changes: 14 additions & 43 deletions Cesium.Parser/CParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ private static Expression MakeAssignmentExpression(
// HACK: custom parsing is required here due to the reasons outlined in
// https://github.com/LanguageDev/Yoakke/issues/138
//
// TODO: Wait for fix in Yoakke and get rid of the hack.
//
// declaration: declaration_specifiers init_declarator_list? ';'
[CustomParser("declaration")]
private ParseResult<Declaration> customParseDeclaration(int offset)
Expand Down Expand Up @@ -236,6 +238,7 @@ private static Declaration MakeDeclaration(

// HACK: This is a synthetic set of rules which is absent from the C standard, but required for simplification of
// the implementation of https://github.com/LanguageDev/Yoakke/issues/138
// TODO: Wait for fix in Yoakke and get rid of the hack.
[Rule("declaration_specifiers: declaration_specifier+")]
private static DeclarationSpecifiers MakeDeclarationSpecifiers(IEnumerable<IDeclarationSpecifier> specifiers) =>
specifiers.ToImmutableArray();
Expand Down Expand Up @@ -330,6 +333,8 @@ private static StructDeclarationList MakeStructDeclarationList(
// HACK: custom parsing is required here due to the reasons outlined in
// https://github.com/LanguageDev/Yoakke/issues/138
//
// TODO: Wait for fix in Yoakke and get rid of the hack.
//
// struct_declaration: specifier_qualifier_list struct_declarator_list? ';'
[CustomParser("struct_declaration")]
private ParseResult<StructDeclaration> customParseStructDeclaration(int offset)
Expand Down Expand Up @@ -390,6 +395,8 @@ private static StructDeclaration MakeStructDeclaration(
// HACK: This is a synthetic set of rules which is absent from the C standard, but required for simplification of
// the implementation of https://github.com/LanguageDev/Yoakke/issues/138
//
// TODO: Wait for fix in Yoakke and get rid of the hack.
//
// Actual rules are:
// specifier_qualifier_list: type_specifier specifier_qualifier_list?
// specifier_qualifier_list: type_qualifier specifier_qualifier_list?
Expand Down Expand Up @@ -501,6 +508,8 @@ private static ParameterList MakeParameterList(ParameterList prev, ICToken _, Pa
// HACK: custom parsing is required here due to the reasons outlined in
// https://github.com/LanguageDev/Yoakke/issues/138
//
// TODO: Wait for fix in Yoakke and get rid of the hack.
//
// parameter_declaration: declaration_specifiers declarator
[CustomParser("parameter_declaration")]
private ParseResult<ParameterDeclaration> customParseParameterDeclaration(int offset)
Expand All @@ -519,11 +528,7 @@ private ParseResult<ParameterDeclaration> customParseParameterDeclaration(int of

private static ParameterDeclaration MakeParameterDeclaration(
DeclarationSpecifiers specifiers,
Declarator declarator)
{
(specifiers, declarator) = TypeDefNameIdentifierHack(specifiers, declarator);
return new(specifiers, declarator);
}
Declarator declarator) => new(specifiers, declarator);

[Rule("parameter_declaration: declaration_specifiers abstract_declarator?")]
private static ParameterDeclaration MakeParameterTypeList(
Expand Down Expand Up @@ -679,6 +684,8 @@ private static ExternalDeclaration MakeExternalDeclaration(Declaration declarati
// HACK: custom parsing is required here due to the reasons outlined in
// https://github.com/LanguageDev/Yoakke/issues/138
//
// TODO: Wait for fix in Yoakke and get rid of the hack.
//
// function_definition: declaration_specifiers declarator declaration_list? compound_statement
[CustomParser("function_definition")]
private ParseResult<FunctionDefinition> customParseFunctionDefinition(int offset)
Expand Down Expand Up @@ -709,11 +716,7 @@ private static FunctionDefinition MakeFunctionDefinition(
DeclarationSpecifiers specifiers,
Declarator declarator,
ImmutableArray<Declaration>? declarationList,
CompoundStatement statement)
{
(specifiers, declarator) = TypeDefNameIdentifierHack(specifiers, declarator);
return new(specifiers, declarator, declarationList, statement);
}
CompoundStatement statement) => new(specifiers, declarator, declarationList, statement);

[Rule("declaration_list: declaration")]
private static ImmutableArray<Declaration> MakeDeclarationList(Declaration declaration) =>
Expand Down Expand Up @@ -790,6 +793,7 @@ private static ImmutableArray<Declaration> MakeDeclarationList(
// TODO: 6.10.9 Pragma operator

// HACK: The existence of this method is caused caused by an issue https://github.com/LanguageDev/Yoakke/issues/138
// TODO: Wait for fix in Yoakke and get rid of the hack.
private ParseResult<(DeclarationSpecifiers, Declarator)> CustomParseSpecifiersAndDeclarator(int offset)
{
// HACK: Usually, this would be a call to parseDeclarationSpecifiers(offset). But here, we have to parse them
Expand Down Expand Up @@ -829,37 +833,4 @@ private static ImmutableArray<Declaration> MakeDeclarationList(
offset,
declarator.FurthestError);
}

// HACK: The existence of this method is caused caused by an issue https://github.com/LanguageDev/Yoakke/issues/138
// As no simple workaround exist, we have to do ugly manipulations in parser and AST to support this.
// TODO: Drop this.
private static (DeclarationSpecifiers, Declarator) TypeDefNameIdentifierHack(
DeclarationSpecifiers specifiers,
Declarator declarator)
{
var directDeclarator = declarator.DirectDeclarator;
IdentifierDirectDeclarator? identifierDeclarator = null;
while (directDeclarator != null && identifierDeclarator == null)
{
identifierDeclarator = directDeclarator as IdentifierDirectDeclarator;
directDeclarator = directDeclarator.Base;
}

if (identifierDeclarator is { Identifier: null })
{
var lastSpecifier = specifiers.LastOrDefault();
if (lastSpecifier is NamedTypeSpecifier { TypeDefName: var tn })
{
specifiers = specifiers.RemoveAt(specifiers.Length - 1);
identifierDeclarator.Identifier = tn;
}
}

if (identifierDeclarator is { Identifier: null })
throw new NotSupportedException(
"THIS IS A BUG! It is caused by a hack in parsing for the sake of `typedef_name`." +
$" Please report to the Cesium maintainers: [{string.Join(",", specifiers)}] {declarator}.");

return (specifiers, declarator);
}
}

0 comments on commit 8100762

Please sign in to comment.