From ffc2d225884cc809328e19ecc318b4a5b84afe40 Mon Sep 17 00:00:00 2001 From: "Jay Freeman (saurik)" Date: Sun, 20 Dec 2015 04:21:23 -0800 Subject: [PATCH] Allow extern "C" {} and limit semicolon insertion. --- Output.cpp | 4 +++- Parser.ypp.in | 34 +++++++++++++++++++++++++++++++--- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/Output.cpp b/Output.cpp index 6610ff2..d3da0e4 100644 --- a/Output.cpp +++ b/Output.cpp @@ -364,7 +364,8 @@ void CYExpression::Output(CYOutput &out, int precedence, CYFlags flags) const { } void CYExternal::Output(CYOutput &out, CYFlags flags) const { - out << "extern" << abi_ << typed_ << ';'; + out << "extern" << abi_ << typed_; + out.Terminate(); } void CYFatArrow::Output(CYOutput &out, CYFlags flags) const { @@ -638,6 +639,7 @@ void CYLambda::Output(CYOutput &out, CYFlags flags) const { void CYTypeDefinition::Output(CYOutput &out, CYFlags flags) const { out << "typedef" << ' ' << *typed_; + out.Terminate(); } void CYTypeExpression::Output(CYOutput &out, CYFlags flags) const { diff --git a/Parser.ypp.in b/Parser.ypp.in index 88c01bf..e1c2368 100644 --- a/Parser.ypp.in +++ b/Parser.ypp.in @@ -524,6 +524,9 @@ type; }) %type ExpressionOpt %type ExpressionStatement_ %type ExpressionStatement +%type ExternC +%type ExternCStatement +%type ExternCStatementListOpt %type Finally %type ForBinding %type ForDeclaration @@ -613,6 +616,7 @@ type; }) %type TemplateSpans %type ThrowStatement %type TryStatement +%type TypeDefinition %type UnaryExpression_ %type UnaryExpression %type VariableDeclaration @@ -865,6 +869,11 @@ TerminatorSoft | NewLineNot LexOf Terminator ; +TerminatorHard + : ";" + | error { if (yyla.type_get() != yyeof_) CYERR(@error, "required semi-colon"); else CYEOK(); } StrictSemi + ; + Terminator : ";" | error { if (yyla.type_get() != yyeof_ && yyla.type != yytranslate_(token::CloseBrace) && !driver.newline_) CYERR(@error, "required semi-colon"); else CYEOK(); } StrictSemi @@ -1378,7 +1387,7 @@ Block ; StatementList - : StatementListItem[statement] StatementListOpt[next] { $statement->SetNext($next); $$ = $statement; } + : StatementListItem[statement] StatementListOpt[next] { $$ = $statement; CYSetLast($$) = $next; } ; StatementListOpt @@ -2264,8 +2273,12 @@ IdentifierNoOf : "typedef" NewLineOpt { $$ = CYNew CYIdentifier("typedef"); } ; +TypeDefinition + : "typedef" NewLineNot TypedIdentifierYes[typed] TerminatorHard { $$ = CYNew CYTypeDefinition($typed); } + ; + Statement__ - : "typedef" NewLineNot TypedIdentifierYes[typed] Terminator { $$ = CYNew CYTypeDefinition($typed); } + : TypeDefinition[pass] { $$ = $pass; } ; PrimaryExpression @@ -2277,8 +2290,23 @@ IdentifierNoOf : "extern" NewLineOpt { $$ = CYNew CYIdentifier("extern"); } ; +ExternCStatement + : TypedIdentifierYes[typed] TerminatorHard { $$ = CYNew CYExternal(CYNew CYString("C"), $typed); } + | TypeDefinition[pass] { $$ = $pass; } + ; + +ExternCStatementListOpt + : ExternCStatement[statement] ExternCStatementListOpt[next] { $$ = $statement; CYSetLast($$) = $next; } + | { $$ = NULL; } + ; + +ExternC + : "{" ExternCStatementListOpt[pass] "}" { $$ = $pass; } + | ExternCStatement[pass] { $$ = $pass; } + ; + Statement__ - : "extern" NewLineNot StringLiteral[abi] { if (strcmp($abi->Value(), "C") != 0) CYERR(@abi, "unknown extern binding"); } TypedIdentifierYes[typed] Terminator { $$ = CYNew CYExternal($abi, $typed); } + : "extern" NewLineNot StringLiteral[abi] { if (strcmp($abi->Value(), "C") != 0) CYERR(@abi, "unknown extern binding"); } ExternC[pass] { $$ = $pass; } ; /* }}} */ -- 2.45.2