Skip to content

Commit

Permalink
Merge pull request #1130 from guwirth/cpp17/selection-statement
Browse files Browse the repository at this point in the history
Cpp17/selection statement
  • Loading branch information
guwirth authored May 13, 2017
2 parents 36e44c2 + 4d05b4e commit afb0424
Show file tree
Hide file tree
Showing 9 changed files with 2,935 additions and 23 deletions.
File renamed without changes.
1,257 changes: 1,257 additions & 0 deletions cxx-squid/dox/2015-07-24_cpp14_grammar.txt

Large diffs are not rendered by default.

1,292 changes: 1,292 additions & 0 deletions cxx-squid/dox/2016-11-28_cpp17_grammar.txt

Large diffs are not rendered by default.

Binary file added cxx-squid/dox/2016-11-28_cpp17_n4618.pdf
Binary file not shown.
262 changes: 262 additions & 0 deletions cxx-squid/dox/grammar-diff.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,262 @@
namespace-name:
identifier
namespace-alias

pp-number:
digit
. digit
pp-number digit
pp-number identifier-nondigit
pp-number � digit
pp-number � nondigit
pp-number e sign
pp-number E sign
pp-number p sign
pp-number P sign
pp-number .

hexadecimal-literal:
hexadecimal-prefix hexadecimal-digit-sequence

hexadecimal-prefix: one of
0x 0X

hexadecimal-digit-sequence:
hexadecimal-digit
hexadecimal-digit-sequence �opt hexadecimal-digit

floating-literal:
decimal-floating-literal
hexadecimal-floating-literal

decimal-floating-literal:
fractional-constant exponent-partopt floating-suffixopt
digit-sequence exponent-part floating-suffixopt

hexadecimal-floating-literal:
hexadecimal-prefix hexadecimal-fractional-constant binary-exponent-part floating-suffixopt
hexadecimal-prefix hexadecimal-digit-sequence binary-exponent-part floating-suffixopt

hexadecimal-fractional-constant:
hexadecimal-digit-sequenceopt . hexadecimal-digit-sequence
hexadecimal-digit-sequence .

binary-exponent-part:
p signopt digit-sequence
P signopt digit-sequence

user-defined-floating-literal:
fractional-constant exponent-partopt ud-suffix
digit-sequence exponent-part ud-suffix
hexadecimal-prefix hexadecimal-fractional-constant binary-exponent-part ud-suffix
hexadecimal-prefix hexadecimal-digit-sequence binary-exponent-part ud-suffix

simple-capture:
identifier
& identifier
this
* this

lambda-declarator:
( parameter-declaration-clause ) decl-specifier-seqopt
noexcept-specifieropt attribute-specifier-seqopt trailing-return-typeopt

postfix-expression:
primary-expression
postfix-expression [ expr-or-braced-init-list ]
postfix-expression ( expression-listopt )
simple-type-specifier ( expression-listopt )
typename-specifier ( expression-listopt )
simple-type-specifier braced-init-list
typename-specifier braced-init-list
postfix-expression . templateopt id-expression
postfix-expression -> templateopt id-expression
postfix-expression . pseudo-destructor-name
postfix-expression -> pseudo-destructor-name
postfix-expression ++
postfix-expression --
dynamic_cast < type-id > ( expression )
static_cast < type-id > ( expression )
reinterpret_cast < type-id > ( expression )
const_cast < type-id > ( expression )
typeid ( expression )
typeid ( type-id )

labeled-statement:
attribute-specifier-seqopt identifier : statement
attribute-specifier-seqoptcase constant-expression : statement
attribute-specifier-seqoptdefault : statement

iteration-statement:
while ( condition ) statement
do statement while ( expression ) ;
for ( init-statement conditionopt ; expressionopt ) statement
for ( for-range-declaration : for-range-initializer ) statement

for-range-declaration:
attribute-specifier-seqopt decl-specifier-seq declarator
attribute-specifier-seqopt decl-specifier-seq ref-qualifieropt [ identifier-list ]

for-range-initializer:
expr-or-braced-init-list

jump-statement:
break ;
continue ;
return expr-or-braced-init-listopt ;
goto identifier ;

declaration:
block-declaration
nodeclspec-function-declaration
function-definition
template-declaration
deduction-guide
explicit-instantiation
explicit-specialization
linkage-specification
namespace-definition
empty-declaration
attribute-declaration

nodeclspec-function-declaration:
attribute-specifier-seqopt declarator ;

alias-declaration:
using identifier attribute-specifier-seqopt = defining-type-id ;

simple-declaration:
decl-specifier-seq init-declarator-listopt ;
attribute-specifier-seq decl-specifier-seq init-declarator-list ;
attribute-specifier-seqopt decl-specifier-seq ref-qualifieropt [ identifier-list ] initializer ;

decl-specifier:
storage-class-specifier
defining-type-specifier
function-specifier
friend
typedef
constexpr
inline

function-specifier:
virtual
explicit

type-specifier:
simple-type-specifier
elaborated-type-specifier
typename-specifier
cv-qualifier

defining-type-specifier:
type-specifier
class-specifier
enum-specifier

defining-type-specifier-seq:
defining-type-specifier attribute-specifier-seqopt
defining-type-specifier defining-type-specifier-seq

simple-type-specifier:
nested-name-specifieropt type-name
nested-name-specifier template simple-template-id
nested-name-specifieropt template-name
char
char16_t
char32_t
wchar_t
bool
short
int
long
signed
unsigned
float
double
void
auto
decltype-specifier

enum-head:
enum-key attribute-specifier-seqopt enum-head-nameopt enum-baseopt

enum-head-name:
nested-name-specifieropt identifier

opaque-enum-declaration:
enum-key attribute-specifier-seqopt nested-name-specifieropt identifier enum-baseopt ;

using-declaration:
using using-declarator-list ;

using-declarator-list:
using-declarator ...opt
using-declarator-list , using-declarator ...opt

using-declarator:
typenameopt nested-name-specifier unqualified-id

attribute-specifier:
[ [ attribute-using-prefixopt attribute-list ] ]
alignment-specifier

alignment-specifier:
alignas ( type-id ...opt )
alignas ( constant-expression ...opt )
attribute-using-prefix :
using attribute-namespace :

attribute-argument-clause:
( balanced-token-seqopt )

balanced-token-seq:
balanced-token
balanced-token-seq balanced-token

balanced-token:
( balanced-token-seqopt )
[ balanced-token-seqopt ]
{ balanced-token-seqopt }
any token other than a parenthesis, a bracket, or a brace

parameters-and-qualifiers:
( parameter-declaration-clause ) cv-qualifier-seqopt
ref-qualifieropt noexcept-specifieropt attribute-specifier-seqopt

trailing-return-type:
-> type-id

defining-type-id:
defining-type-specifier-seq abstract-declaratoropt

expr-or-braced-init-list:
expression
braced-init-list

member-declaration:
attribute-specifier-seqopt decl-specifier-seqopt member-declarator-listopt ;
function-definition
using-declaration
static_assert-declaration
template-declaration
deduction-guide
alias-declaration
empty-declaration

deduction-guide:
explicitopt template-name ( parameter-declaration-clause ) -> simple-template-id ;

noexcept-specifier:
noexcept ( constant-expression )
noexcept
throw ( )

group-part:
if-section
control-line
text-line
# conditionally-supported-directive

conditionally-supported-directive:
pp-tokens new-line
36 changes: 18 additions & 18 deletions cxx-squid/src/main/java/org/sonar/cxx/parser/CxxGrammarImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public enum CxxGrammarImpl implements GrammarRuleKey {
condition,
selectionStatement,
iterationStatement,
forInitStatement,
initStatement,
forRangeDeclaration,
forRangeInitializer,
jumpStatement,
Expand Down Expand Up @@ -700,6 +700,20 @@ private static void statements(LexerfulGrammarBuilder b) {

b.rule(emptyStatement).is(";"); // todo: not C++

b.rule(initStatement).is(
b.firstOf(
expressionStatement, // C++
simpleDeclaration // C++
)
);

b.rule(condition).is( // todo C++17
b.firstOf(
b.sequence(b.optional(attributeSpecifierSeq), conditionDeclSpecifierSeq, declarator, b.firstOf(b.sequence("=", initializerClause), bracedInitList)), // C++
expression // C++ todo: wrong order
)
);

b.rule(labeledStatement).is(
b.firstOf(
b.sequence(b.optional(attributeSpecifierSeq), IDENTIFIER, ":", statement), // C++
Expand All @@ -717,15 +731,8 @@ private static void statements(LexerfulGrammarBuilder b) {

b.rule(selectionStatement).is(
b.firstOf(
b.sequence(CxxKeyword.IF, "(", condition, ")", statement, b.optional(CxxKeyword.ELSE, statement)), // C++
b.sequence(CxxKeyword.SWITCH, "(", condition, ")", statement)
)
);

b.rule(condition).is(
b.firstOf(
b.sequence(b.optional(attributeSpecifierSeq), conditionDeclSpecifierSeq, declarator, b.firstOf(b.sequence("=", initializerClause), bracedInitList)), // C++
expression // C++ todo: wrong order
b.sequence(CxxKeyword.IF, b.optional(CxxKeyword.CONSTEXPR), "(", b.optional(initStatement), condition, ")", statement, b.optional(CxxKeyword.ELSE, statement)), // C++
b.sequence(CxxKeyword.SWITCH, "(", b.optional(initStatement), condition, ")", statement)
)
);

Expand All @@ -741,19 +748,12 @@ private static void statements(LexerfulGrammarBuilder b) {
b.firstOf(
b.sequence(CxxKeyword.WHILE, "(", condition, ")", statement), // C++
b.sequence(CxxKeyword.DO, statement, CxxKeyword.WHILE, "(", expression, ")", ";"), // C++
b.sequence(CxxKeyword.FOR, "(", forInitStatement, b.optional(condition), ";", b.optional(expression), ")", statement), // C++
b.sequence(CxxKeyword.FOR, "(", initStatement, b.optional(condition), ";", b.optional(expression), ")", statement), // C++
b.sequence(CxxKeyword.FOR, "(", forRangeDeclaration, ":", forRangeInitializer, ")", statement), // C++
b.sequence(CxxKeyword.FOR, "each", "(", forRangeDeclaration, "in", forRangeInitializer, ")", statement) // ???
)
);

b.rule(forInitStatement).is(
b.firstOf(
expressionStatement, // C++
simpleDeclaration // C++
)
);

b.rule(forRangeDeclaration).is(
b.optional(attributeSpecifierSeq), forRangeDeclSpecifierSeq, declarator // C++
);
Expand Down
20 changes: 15 additions & 5 deletions cxx-squid/src/test/java/org/sonar/cxx/parser/StatementTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,20 @@ public void selectionStatement() {

mockRule(CxxGrammarImpl.statement);
mockRule(CxxGrammarImpl.condition);
mockRule(CxxGrammarImpl.initStatement);

assertThat(p).matches("if ( condition ) statement");
assertThat(p).matches("if constexpr ( condition ) statement");
assertThat(p).matches("if ( initStatement condition ) statement");
assertThat(p).matches("if constexpr ( initStatement condition ) statement");

assertThat(p).matches("if ( condition ) statement else statement");
assertThat(p).matches("if constexpr ( condition ) statement else statement");
assertThat(p).matches("if ( initStatement condition ) statement else statement");
assertThat(p).matches("if constexpr ( initStatement condition ) statement else statement");

assertThat(p).matches("switch ( condition ) statement");
assertThat(p).matches("switch ( initStatement condition ) statement");
}

@Test
Expand Down Expand Up @@ -168,15 +178,15 @@ public void iterationStatement() {
mockRule(CxxGrammarImpl.condition);
mockRule(CxxGrammarImpl.statement);
mockRule(CxxGrammarImpl.expression);
mockRule(CxxGrammarImpl.forInitStatement);
mockRule(CxxGrammarImpl.initStatement);
mockRule(CxxGrammarImpl.forRangeDeclaration);
mockRule(CxxGrammarImpl.forRangeInitializer);

assertThat(p).matches("while ( condition ) statement");
assertThat(p).matches("do statement while ( expression ) ;");
assertThat(p).matches("for ( forInitStatement ; ) statement");
assertThat(p).matches("for ( forInitStatement condition ; ) statement");
assertThat(p).matches("for ( forInitStatement condition ; expression ) statement");
assertThat(p).matches("for ( initStatement ; ) statement");
assertThat(p).matches("for ( initStatement condition ; ) statement");
assertThat(p).matches("for ( initStatement condition ; expression ) statement");
assertThat(p).matches("for ( forRangeDeclaration : forRangeInitializer ) statement");
}

Expand Down Expand Up @@ -218,7 +228,7 @@ public void iterationStatement_reallife() {

@Test
public void forInitStatement_reallife() {
p.setRootRule(g.rule(CxxGrammarImpl.forInitStatement));
p.setRootRule(g.rule(CxxGrammarImpl.initStatement));
assertThat(p).matches("int i=1;");
}

Expand Down
Loading

0 comments on commit afb0424

Please sign in to comment.