Skip to content

Commit

Permalink
fix ident parse in prototype, improve K&R parse, and dont visit expr …
Browse files Browse the repository at this point in the history
…in bind (#148)

* fix identifier role in declarator

* fix ident parse in prototype, improve K&R parse, and dont visit expr in bind
  • Loading branch information
ltcmelo authored Oct 13, 2024
1 parent e6fd392 commit b595123
Show file tree
Hide file tree
Showing 20 changed files with 575 additions and 199 deletions.
9 changes: 2 additions & 7 deletions C/SyntaxTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,10 @@ class PSY_C_API SyntaxTree

/**
* \brief The SyntaxCategory enumeration.
*
* The possible categories of syntax that a SyntaxTree
* may represent. If unspecified, it is assumed that
* a SyntaxTree represents a \a translation-unit.
*/
enum class SyntaxCategory : uint8_t
{
UNSPECIFIED = 0,

Any = 0,
Declarations,
Expressions,
Statements,
Expand All @@ -87,7 +82,7 @@ class PSY_C_API SyntaxTree
TextCompleteness textCompleteness,
ParseOptions parseOptions = ParseOptions(),
const std::string& filePath = "",
SyntaxCategory syntaxCategory = SyntaxCategory::UNSPECIFIED);
SyntaxCategory syntaxCategory = SyntaxCategory::Any);

/**
* The path of the file associated to \c this SyntaxTree.
Expand Down
77 changes: 77 additions & 0 deletions C/binder/Binder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,86 @@ SyntaxVisitor::Action Binder::visitCompoundStatement(const CompoundStatementSynt
return Action::Skip;
}

SyntaxVisitor::Action Binder::visitLabeledStatement(const LabeledStatementSyntax *)
{
return Action::Skip;
}

SyntaxVisitor::Action Binder::visitDeclarationStatement(const DeclarationStatementSyntax* node)
{
visit(node->declaration());

return Action::Skip;
}

SyntaxVisitor::Action Binder::visitExpressionStatement(const ExpressionStatementSyntax*)
{
return Action::Skip;
}

SyntaxVisitor::Action Binder::visitIfStatement(const IfStatementSyntax* node)
{
visit(node->statement());
visit(node->elseStatement());
return Action::Skip;
}

SyntaxVisitor::Action Binder::visitSwitchStatement(const SwitchStatementSyntax* node)
{
visit(node->statement());
return Action::Skip;
}

SyntaxVisitor::Action Binder::visitWhileStatement(const WhileStatementSyntax* node)
{
visit(node->statement());
return Action::Skip;
}

SyntaxVisitor::Action Binder::visitDoStatement(const DoStatementSyntax* node)
{
visit(node->statement());
return Action::Skip;
}

SyntaxVisitor::Action Binder::visitForStatement(const ForStatementSyntax* node)
{
visit(node->initializer());
visit(node->statement());
return Action::Skip;
}

SyntaxVisitor::Action Binder::visitGotoStatement(const GotoStatementSyntax *)
{
return Action::Skip;
}

SyntaxVisitor::Action Binder::visitContinueStatement(const ContinueStatementSyntax *)
{
return Action::Skip;
}

SyntaxVisitor::Action Binder::visitBreakStatement(const BreakStatementSyntax *)
{
return Action::Skip;
}

SyntaxVisitor::Action Binder::visitReturnStatement(const ReturnStatementSyntax*)
{
return Action::Skip;
}

SyntaxVisitor::Action Binder::visitExtGNU_AsmStatement(const ExtGNU_AsmStatementSyntax *)
{
return Action::Skip;
}

SyntaxVisitor::Action Binder::visitExtGNU_AsmQualifier(const ExtGNU_AsmQualifierSyntax *)
{
return Action::Skip;
}

SyntaxVisitor::Action Binder::visitExtGNU_AsmOperand(const ExtGNU_AsmOperandSyntax *)
{
return Action::Skip;
}
14 changes: 14 additions & 0 deletions C/binder/Binder.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,21 @@ class PSY_C_INTERNAL_API Binder final : protected SyntaxVisitor
// Statements //
//------------//
virtual Action visitCompoundStatement(const CompoundStatementSyntax*) override;
virtual Action visitLabeledStatement(const LabeledStatementSyntax*) override;
virtual Action visitDeclarationStatement(const DeclarationStatementSyntax*) override;
virtual Action visitExpressionStatement(const ExpressionStatementSyntax*) override;
virtual Action visitIfStatement(const IfStatementSyntax*) override;
virtual Action visitSwitchStatement(const SwitchStatementSyntax*) override;
virtual Action visitWhileStatement(const WhileStatementSyntax*) override;
virtual Action visitDoStatement(const DoStatementSyntax*) override;
virtual Action visitForStatement(const ForStatementSyntax*) override;
virtual Action visitGotoStatement(const GotoStatementSyntax*) override;
virtual Action visitContinueStatement(const ContinueStatementSyntax*) override;
virtual Action visitBreakStatement(const BreakStatementSyntax*) override;
virtual Action visitReturnStatement(const ReturnStatementSyntax*) override;
virtual Action visitExtGNU_AsmStatement(const ExtGNU_AsmStatementSyntax*) override;
virtual Action visitExtGNU_AsmQualifier(const ExtGNU_AsmQualifierSyntax*) override;
virtual Action visitExtGNU_AsmOperand(const ExtGNU_AsmOperandSyntax*) override;
};

template <class SymT, class... SymTArgs>
Expand Down
11 changes: 0 additions & 11 deletions C/parser/DiagnosticsReporter_Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ const std::string Parser::DiagnosticsReporter::ID_of_UnexpectedInitializerOfDecl
const std::string Parser::DiagnosticsReporter::ID_of_UnexpectedStaticOrTypeQualifierInArrayDeclarator = "Parser-304-6.7.6";
const std::string Parser::DiagnosticsReporter::ID_of_UnexpectedPointerInArrayDeclarator = "Parser-305-6.7.6";
const std::string Parser::DiagnosticsReporter::ID_of_ExpectedNamedParameterBeforeEllipsis = "Parser-306-6.7.6.3";
const std::string Parser::DiagnosticsReporter::ID_of_ExpectedTypeSpecifier = "Parser-307-6.7";
const std::string Parser::DiagnosticsReporter::ID_of_UnexpectedCaseLabelOutsideSwitch = "Parser-308-6.8.1-2";
const std::string Parser::DiagnosticsReporter::ID_of_UnexpectedDefaultLabelOutsideSwitch = "Parser-309-6.8.1-2";
const std::string Parser::DiagnosticsReporter::ID_of_UnexpectedContinueOutsideLoop = "Parser-310-6.8.6.2-1";
Expand Down Expand Up @@ -441,16 +440,6 @@ void Parser::DiagnosticsReporter::ExpectedNamedParameterBeforeEllipsis()
DiagnosticCategory::Syntax));
}

void Parser::DiagnosticsReporter::ExpectedTypeSpecifier()
{
diagnoseOrDelayDiagnostic(
DiagnosticDescriptor(ID_of_ExpectedTypeSpecifier,
"[[declaration without type specifier]]",
"missing type specifier, assume `int'",
DiagnosticSeverity::Warning,
DiagnosticCategory::Syntax));
}

void Parser::DiagnosticsReporter::UnexpectedCaseLabelOutsideSwitch()
{
diagnoseOrDelayDiagnostic(
Expand Down
2 changes: 2 additions & 0 deletions C/parser/LanguageExtensions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ LanguageExtensions::LanguageExtensions(MacroTranslations translations)
, BF_all_(~0)
{
/* C */
BF_.extC_KandRStyle_ = false;
BF_.extC_wchar_t_Keyword_ = false;
BF_.extC_char8_t_Keyword_ = false;
BF_.extC_char16_t_Keyword_ = false;
Expand All @@ -63,6 +64,7 @@ const MacroTranslations& LanguageExtensions::translations() const
bool LanguageExtensions::isEnabled_##FLAG() const \
{ return BF_.FLAG##_; }

DEFINE_ENABLE_ISENABLED(extC_KandRStyle)
DEFINE_ENABLE_ISENABLED(extC_wchar_t_Keyword)
DEFINE_ENABLE_ISENABLED(extC_char8_t_Keyword)
DEFINE_ENABLE_ISENABLED(extC_char16_t_Keyword)
Expand Down
12 changes: 12 additions & 0 deletions C/parser/LanguageExtensions.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class PSY_C_API LanguageExtensions final
enum class Ext : std::uint8_t
{
/* C */
C_KandRStyle,
C_wchar_t_Keyword,
C_char8_t_Keyword,
C_char16_t_Keyword,
Expand Down Expand Up @@ -89,6 +90,16 @@ class PSY_C_API LanguageExtensions final
NULLAsBuiltin,
};

//!@{
/**
* Whether to enable K&R style of function definition syntax.
*
* https://gcc.gnu.org/onlinedocs/gcc/Function-Prototypes.html
*/
LanguageExtensions& enable_extC_KandRStyle(bool enable);
bool isEnabled_extC_KandRStyle() const;
//!@}

//!@{
/**
* Whether to enable \c wchar_t as a keyword.
Expand Down Expand Up @@ -282,6 +293,7 @@ class PSY_C_API LanguageExtensions final
struct BitFields
{
/* C */
std::uint64_t extC_KandRStyle_ : 1;
std::uint64_t extC_wchar_t_Keyword_ : 1;
std::uint64_t extC_char8_t_Keyword_ : 1;
std::uint64_t extC_char16_t_Keyword_ : 1;
Expand Down
12 changes: 12 additions & 0 deletions C/parser/ParseOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,23 @@ ParseOptions::ParseOptions(LanguageDialect langDialect,
setAmbiguityMode(AmbiguityMode::DisambiguateAlgorithmicallyAndHeuristically);
}

ParseOptions& ParseOptions::withLanguageDialect(LanguageDialect langDialect)
{
langDialect_ = langDialect_;
return *this;
}

const LanguageDialect& ParseOptions::languageDialect() const
{
return langDialect_;
}

ParseOptions& ParseOptions::withLanguageExtensions(LanguageExtensions langExts)
{
langExts_ = langExts;
return *this;
}

const LanguageExtensions& ParseOptions::languageExtensions() const
{
return langExts_;
Expand Down
10 changes: 10 additions & 0 deletions C/parser/ParseOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,21 @@ class PSY_C_API ParseOptions
LanguageExtensions langExts);
//!@}

//!@{
/**
* The LanguageDialect of \c this ParseOptions.
*/
ParseOptions& withLanguageDialect(LanguageDialect langDialect);
const LanguageDialect& languageDialect() const;
//!@}

//!@{
/**
* The LanguageExtensions of \c this ParseOptions.
*/
ParseOptions& withLanguageExtensions(LanguageExtensions langExts);
const LanguageExtensions& languageExtensions() const;
//!@}

//!@{
/**
Expand All @@ -80,6 +86,9 @@ class PSY_C_API ParseOptions
KeepAll, /**< Keep all comments. */
KeepOnlyDocumentation /**< keep only documentation comments. */
};
//!@}

//!@{
/**
* The CommentMode of \c this ParserOptions.
*/
Expand All @@ -98,6 +107,7 @@ class PSY_C_API ParseOptions
DisambiguateAlgorithmicallyAndHeuristically, /**< Disambiguate ambiguities algorithmically and heristically. */
DisambiguateHeuristically, /**< Disambiguate ambiguities heuristically. */
};

/**
* The AmbiguityMode of \c this ParserOptions.
*/
Expand Down
1 change: 1 addition & 0 deletions C/parser/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ Parser::Parser(SyntaxTree* tree)
, backtracker_(nullptr)
, diagReporter_(this)
, curTkIdx_(1)
, isWithinKandRFuncDef_(false)
, DEPTH_OF_EXPRS_(0)
, DEPTH_OF_STMTS_(0)
{
Expand Down
8 changes: 4 additions & 4 deletions C/parser/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ class PSY_C_INTERNAL_API Parser
void UnexpectedStaticOrTypeQualifiersInArrayDeclarator();
void UnexpectedPointerInArrayDeclarator();
void ExpectedNamedParameterBeforeEllipsis();
void ExpectedTypeSpecifier();
void UnexpectedCaseLabelOutsideSwitch();
void UnexpectedDefaultLabelOutsideSwitch();
void UnexpectedContinueOutsideLoop();
Expand All @@ -179,7 +178,6 @@ class PSY_C_INTERNAL_API Parser
static const std::string ID_of_UnexpectedStaticOrTypeQualifierInArrayDeclarator;
static const std::string ID_of_UnexpectedPointerInArrayDeclarator;
static const std::string ID_of_ExpectedNamedParameterBeforeEllipsis;
static const std::string ID_of_ExpectedTypeSpecifier;
static const std::string ID_of_UnexpectedCaseLabelOutsideSwitch;
static const std::string ID_of_UnexpectedDefaultLabelOutsideSwitch;
static const std::string ID_of_UnexpectedContinueOutsideLoop;
Expand Down Expand Up @@ -232,6 +230,8 @@ class PSY_C_INTERNAL_API Parser
void skipTo(SyntaxKind tkK);
unsigned int curTkIdx_;

bool isWithinKandRFuncDef_;

int DEPTH_OF_EXPRS_;
int DEPTH_OF_STMTS_;

Expand Down Expand Up @@ -329,7 +329,7 @@ class PSY_C_INTERNAL_API Parser
bool parseAlignmentSpecifier_AtFirst(SpecifierSyntax*& spec);
bool parseExtGNU_Typeof_AtFirst(SpecifierSyntax*& spec);
void parseTypedefName_AtFirst(SpecifierSyntax*& spec);
bool parseAtomiceTypeSpecifier_AtFirst(SpecifierSyntax*& spec);
bool parseAtomicTypeSpecifier_AtFirst(SpecifierSyntax*& spec);
template <class TypeDeclT> bool parseTagTypeSpecifier_AtFirst(
DeclarationSyntax*& decl,
SpecifierSyntax*& spec,
Expand All @@ -345,7 +345,7 @@ class PSY_C_INTERNAL_API Parser
bool parseExtGNU_AsmLabel_AtFirst(SpecifierSyntax*& attr);
bool parseExtPSY_QuantifiedTypeSpecifier_AtFirst(SpecifierSyntax*& spec);

IdentifierRole determineIdentifierRole(bool seenType) const;
IdentifierRole guessRoleOfIdentifier() const;

/* Declarators */
bool parseAbstractDeclarator(DeclaratorSyntax*& decltor);
Expand Down
Loading

0 comments on commit b595123

Please sign in to comment.