1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-19 06:55:23 +02:00

Refactoring new parser for C++.

This commit is contained in:
John Camelon 2004-11-19 16:32:22 +00:00
parent 87785b3e67
commit d533e36b40
3 changed files with 434 additions and 566 deletions

View file

@ -14,16 +14,33 @@ import java.util.Collections;
import java.util.List;
import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression;
import org.eclipse.cdt.core.dom.ast.IASTBreakStatement;
import org.eclipse.cdt.core.dom.ast.IASTCaseStatement;
import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement;
import org.eclipse.cdt.core.dom.ast.IASTConditionalExpression;
import org.eclipse.cdt.core.dom.ast.IASTContinueStatement;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTDeclarationStatement;
import org.eclipse.cdt.core.dom.ast.IASTDefaultStatement;
import org.eclipse.cdt.core.dom.ast.IASTDoStatement;
import org.eclipse.cdt.core.dom.ast.IASTEnumerationSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTExpression;
import org.eclipse.cdt.core.dom.ast.IASTExpressionList;
import org.eclipse.cdt.core.dom.ast.IASTExpressionStatement;
import org.eclipse.cdt.core.dom.ast.IASTForStatement;
import org.eclipse.cdt.core.dom.ast.IASTGotoStatement;
import org.eclipse.cdt.core.dom.ast.IASTIfStatement;
import org.eclipse.cdt.core.dom.ast.IASTLabelStatement;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTNullStatement;
import org.eclipse.cdt.core.dom.ast.IASTReturnStatement;
import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.IASTSwitchStatement;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IASTTypeId;
import org.eclipse.cdt.core.dom.ast.IASTUnaryExpression;
import org.eclipse.cdt.core.dom.ast.IASTWhileStatement;
import org.eclipse.cdt.core.dom.ast.IASTEnumerationSpecifier.IASTEnumerator;
import org.eclipse.cdt.core.dom.ast.gnu.IGNUASTCompoundStatementExpression;
import org.eclipse.cdt.core.dom.ast.gnu.IGNUASTTypeIdExpression;
@ -41,6 +58,21 @@ import org.eclipse.cdt.core.parser.ParserMode;
import org.eclipse.cdt.internal.core.parser.ParserProblemFactory;
import org.eclipse.cdt.internal.core.parser.problem.IProblemFactory;
import org.eclipse.cdt.internal.core.parser.token.TokenFactory;
import org.eclipse.cdt.internal.core.parser2.c.CASTBreakStatement;
import org.eclipse.cdt.internal.core.parser2.c.CASTCaseStatement;
import org.eclipse.cdt.internal.core.parser2.c.CASTContinueStatement;
import org.eclipse.cdt.internal.core.parser2.c.CASTDeclarationStatement;
import org.eclipse.cdt.internal.core.parser2.c.CASTDefaultStatement;
import org.eclipse.cdt.internal.core.parser2.c.CASTDoStatement;
import org.eclipse.cdt.internal.core.parser2.c.CASTExpressionStatement;
import org.eclipse.cdt.internal.core.parser2.c.CASTForStatement;
import org.eclipse.cdt.internal.core.parser2.c.CASTGotoStatement;
import org.eclipse.cdt.internal.core.parser2.c.CASTIfStatement;
import org.eclipse.cdt.internal.core.parser2.c.CASTLabelStatement;
import org.eclipse.cdt.internal.core.parser2.c.CASTNullStatement;
import org.eclipse.cdt.internal.core.parser2.c.CASTReturnStatement;
import org.eclipse.cdt.internal.core.parser2.c.CASTSwitchStatement;
import org.eclipse.cdt.internal.core.parser2.c.CASTWhileStatement;
import org.eclipse.cdt.internal.core.parser2.cpp.IProblemRequestor;
/**
@ -583,9 +615,6 @@ public abstract class AbstractGNUSourceCodeParser implements ISourceCodeParser {
protected abstract void translationUnit();
protected abstract IASTStatement statement() throws EndOfFileException,
BacktrackException;
protected abstract IASTTranslationUnit getTranslationUnit();
protected IASTExpression assignmentOperatorExpression(int kind,
@ -1173,4 +1202,370 @@ public abstract class AbstractGNUSourceCodeParser implements ISourceCodeParser {
return !parsePassed;
}
/*
* (non-Javadoc)
*
* @see org.eclipse.cdt.internal.core.parser2.GNUBaseParser#statement(java.lang.Object)
*/
protected IASTStatement statement() throws EndOfFileException,
BacktrackException {
switch (LT(1)) {
// labeled statements
case IToken.t_case:
int startOffset = consume(IToken.t_case).getOffset();
IASTExpression case_exp = constantExpression();
consume(IToken.tCOLON);
cleanupLastToken();
IASTCaseStatement cs = createCaseStatement();
((ASTNode)cs).setOffset( startOffset );
cs.setExpression( case_exp );
case_exp.setParent( cs );
case_exp.setPropertyInParent( IASTCaseStatement.EXPRESSION );
return cs;
case IToken.t_default:
startOffset = consume(IToken.t_default).getOffset();
consume(IToken.tCOLON);
cleanupLastToken();
IASTDefaultStatement df = createDefaultStatement();
((ASTNode)df).setOffset( startOffset );
return df;
// compound statement
case IToken.tLBRACE:
IASTCompoundStatement compound = compoundStatement();
cleanupLastToken();
return compound;
// selection statement
case IToken.t_if:
startOffset = consume(IToken.t_if).getOffset();
consume(IToken.tLPAREN);
IASTExpression if_condition = condition();
consume(IToken.tRPAREN);
IASTStatement then_clause = statement();
IASTStatement else_clause = null;
if (LT(1) == IToken.t_else) {
consume(IToken.t_else);
else_clause = statement();
}
IASTIfStatement if_stmt = createIfStatement();
if_stmt.setCondition( if_condition );
((ASTNode)if_stmt).setOffset( startOffset );
if_condition.setParent( if_stmt );
if_condition.setPropertyInParent( IASTIfStatement.CONDITION );
if_stmt.setThenClause( then_clause );
then_clause.setParent( if_stmt );
then_clause.setPropertyInParent( IASTIfStatement.THEN );
if( else_clause != null )
{
if_stmt.setElseClause( else_clause );
else_clause.setParent( if_stmt );
else_clause.setPropertyInParent( IASTIfStatement.ELSE );
}
cleanupLastToken();
return if_stmt;
case IToken.t_switch:
startOffset = consume( IToken.t_switch ).getOffset();
consume(IToken.tLPAREN);
IASTExpression switch_condition = condition();
consume(IToken.tRPAREN);
IASTStatement switch_body = statement();
cleanupLastToken();
IASTSwitchStatement switch_statement = createSwitchStatement();
((ASTNode)switch_statement).setOffset( startOffset );
switch_statement.setController( switch_condition );
switch_condition.setParent( switch_statement );
switch_condition.setPropertyInParent( IASTSwitchStatement.CONTROLLER );
switch_statement.setBody( switch_body );
switch_body.setParent( switch_statement );
switch_body.setPropertyInParent( IASTSwitchStatement.BODY );
return switch_statement;
//iteration statements
case IToken.t_while:
startOffset = consume(IToken.t_while).getOffset();
consume(IToken.tLPAREN);
IASTExpression while_condition = condition();
consume(IToken.tRPAREN);
IASTStatement while_body = statement();
cleanupLastToken();
IASTWhileStatement while_statement = createWhileStatement();
((ASTNode)while_statement).setOffset( startOffset );
while_statement.setCondition( while_condition );
while_condition.setParent( while_statement );
while_condition.setPropertyInParent( IASTWhileStatement.CONDITION );
while_statement.setBody( while_body );
while_condition.setParent( while_statement );
while_condition.setPropertyInParent( IASTWhileStatement.BODY );
while_body.setParent( while_statement );
return while_statement;
case IToken.t_do:
startOffset = consume(IToken.t_do).getOffset();
IASTStatement do_body = statement();
consume(IToken.t_while);
consume(IToken.tLPAREN);
IASTExpression do_condition = condition();
consume(IToken.tRPAREN);
cleanupLastToken();
IASTDoStatement do_statement = createDoStatement();
((ASTNode)do_statement).setOffset( startOffset );
do_statement.setBody( do_body );
do_body.setParent( do_statement );
do_body.setPropertyInParent( IASTDoStatement.BODY );
do_statement.setCondition( do_condition );
do_condition.setParent( do_statement );
do_condition.setPropertyInParent( IASTDoStatement.CONDITION );
return do_statement;
case IToken.t_for:
startOffset = consume( IToken.t_for ).getOffset();
consume(IToken.tLPAREN);
IASTNode init = forInitStatement();
IASTExpression for_condition = null;
if (LT(1) != IToken.tSEMI)
for_condition = condition();
consume(IToken.tSEMI);
IASTExpression iterationExpression = null;
if (LT(1) != IToken.tRPAREN) {
iterationExpression = expression();
cleanupLastToken();
}
consume(IToken.tRPAREN);
IASTStatement for_body = statement();
cleanupLastToken();
IASTForStatement for_statement = createForStatement();
((ASTNode)for_statement).setOffset( startOffset );
if( init instanceof IASTDeclaration )
{
for_statement.setInit((IASTDeclaration) init);
((IASTDeclaration) init).setParent( for_statement );
((IASTDeclaration) init).setPropertyInParent( IASTForStatement.INITDECLARATION );
}
else if( init instanceof IASTExpression )
{
for_statement.setInit((IASTExpression) init);
((IASTExpression) init).setParent( for_statement );
((IASTExpression) init).setPropertyInParent( IASTForStatement.INITEXPRESSION );
}
if( for_condition != null )
{
for_statement.setCondition( for_condition );
for_condition.setParent( for_statement );
for_condition.setPropertyInParent( IASTForStatement.CONDITION );
}
if( iterationExpression != null )
{
for_statement.setIterationExpression( iterationExpression );
iterationExpression.setParent( for_statement );
iterationExpression.setPropertyInParent( IASTForStatement.ITERATION );
}
for_statement.setBody( for_body );
for_body.setParent( for_statement );
for_body.setPropertyInParent( IASTForStatement.BODY );
return for_statement;
//jump statement
case IToken.t_break:
startOffset = consume(IToken.t_break).getOffset();
consume(IToken.tSEMI);
cleanupLastToken();
IASTBreakStatement break_statement = createBreakStatement();
((ASTNode)break_statement).setOffset( startOffset );
return break_statement;
case IToken.t_continue:
startOffset = consume(IToken.t_continue).getOffset();
consume(IToken.tSEMI);
cleanupLastToken();
IASTContinueStatement continue_statement = createContinueStatement();
((ASTNode)continue_statement).setOffset( startOffset );
return continue_statement;
case IToken.t_return:
startOffset = consume(IToken.t_return).getOffset();
IASTExpression result = null;
if (LT(1) != IToken.tSEMI) {
result = expression();
cleanupLastToken();
}
consume(IToken.tSEMI);
cleanupLastToken();
IASTReturnStatement return_statement = createReturnStatement();
((ASTNode)return_statement).setOffset( startOffset );
if( result != null )
{
return_statement.setReturnValue( result );
result.setParent( return_statement );
result.setPropertyInParent( IASTReturnStatement.RETURNVALUE );
}
return return_statement;
case IToken.t_goto:
startOffset = consume(IToken.t_goto).getOffset();
IToken identifier = consume(IToken.tIDENTIFIER);
consume(IToken.tSEMI);
cleanupLastToken();
IASTName goto_label_name = createName( identifier );
IASTGotoStatement goto_statement = createGoToStatement();
((ASTNode)goto_statement).setOffset( startOffset );
goto_statement.setName( goto_label_name );
goto_label_name.setParent( goto_statement );
goto_label_name.setPropertyInParent( IASTGotoStatement.NAME );
return goto_statement;
case IToken.tSEMI:
startOffset = consume(IToken.tSEMI ).getOffset();
cleanupLastToken();
IASTNullStatement null_statement = createNullStatement();
((ASTNode)null_statement).setOffset( startOffset );
return null_statement;
default:
// can be many things:
// label
if (LT(1) == IToken.tIDENTIFIER && LT(2) == IToken.tCOLON) {
IToken labelName = consume(IToken.tIDENTIFIER);
consume(IToken.tCOLON);
IASTLabelStatement label_statement = createLabelStatement();
((ASTNode)label_statement).setOffset( labelName.getOffset() );
IASTName name = createName( labelName );
label_statement.setName( name );
name.setParent( label_statement );
name.setPropertyInParent( IASTLabelStatement.NAME );
return label_statement;
}
// expressionStatement
// Note: the function style cast ambiguity is handled in
// expression
// Since it only happens when we are in a statement
IToken mark = mark();
try {
IASTExpression expression = expression();
consume(IToken.tSEMI);
IASTExpressionStatement expressionStatement = createExpressionStatement();
expressionStatement.setExpression( expression );
expression.setParent( expressionStatement );
expression.setPropertyInParent( IASTExpressionStatement.EXPFRESSION );
cleanupLastToken();
return expressionStatement;
} catch (BacktrackException b) {
backup(mark);
}
// declarationStatement
IASTDeclaration d = declaration();
IASTDeclarationStatement ds = createDeclarationStatement();
ds.setDeclaration(d);
d.setParent( ds );
d.setPropertyInParent( IASTDeclarationStatement.DECLARATION );
cleanupLastToken();
return ds;
}
}
protected abstract IASTDeclaration declaration() throws BacktrackException, EndOfFileException;
/**
* @return
*/
protected IASTLabelStatement createLabelStatement() {
return new CASTLabelStatement();
}
/**
* @return
*/
protected IASTGotoStatement createGoToStatement() {
return new CASTGotoStatement();
}
/**
* @return
*/
protected IASTReturnStatement createReturnStatement() {
return new CASTReturnStatement();
}
/**
* @return
*/
protected IASTForStatement createForStatement() {
return new CASTForStatement();
}
/**
* @return
*/
protected IASTContinueStatement createContinueStatement() {
return new CASTContinueStatement();
}
/**
* @return
*/
protected IASTDoStatement createDoStatement() {
return new CASTDoStatement();
}
/**
* @return
*/
protected IASTBreakStatement createBreakStatement() {
return new CASTBreakStatement();
}
/**
* @return
*/
protected IASTWhileStatement createWhileStatement() {
return new CASTWhileStatement();
}
/**
* @return
*/
protected IASTNullStatement createNullStatement() {
return new CASTNullStatement();
}
/**
* @return
*/
protected IASTSwitchStatement createSwitchStatement() {
return new CASTSwitchStatement();
}
/**
* @return
*/
protected IASTIfStatement createIfStatement() {
return new CASTIfStatement();
}
/**
* @return
*/
protected IASTDefaultStatement createDefaultStatement() {
return new CASTDefaultStatement();
}
/**
* @return
*/
protected IASTCaseStatement createCaseStatement() {
return new CASTCaseStatement();
}
/**
* @return
*/
protected IASTExpressionStatement createExpressionStatement() {
return new CASTExpressionStatement();
}
/**
* @return
*/
protected IASTDeclarationStatement createDeclarationStatement() {
return new CASTDeclarationStatement();
}
protected abstract IASTNode forInitStatement() throws BacktrackException,
EndOfFileException;
}

View file

@ -1071,365 +1071,6 @@ public class GNUCSourceParser extends AbstractGNUSourceCodeParser {
return new CASTIdExpression();
}
/*
* (non-Javadoc)
*
* @see org.eclipse.cdt.internal.core.parser2.GNUBaseParser#statement(java.lang.Object)
*/
protected IASTStatement statement() throws EndOfFileException,
BacktrackException {
switch (LT(1)) {
// labeled statements
case IToken.t_case:
int startOffset = consume(IToken.t_case).getOffset();
IASTExpression case_exp = constantExpression();
consume(IToken.tCOLON);
cleanupLastToken();
IASTCaseStatement cs = createCaseStatement();
((CASTNode)cs).setOffset( startOffset );
cs.setExpression( case_exp );
case_exp.setParent( cs );
case_exp.setPropertyInParent( IASTCaseStatement.EXPRESSION );
return cs;
case IToken.t_default:
startOffset = consume(IToken.t_default).getOffset();
consume(IToken.tCOLON);
cleanupLastToken();
IASTDefaultStatement df = createDefaultStatement();
((CASTNode)df).setOffset( startOffset );
return df;
// compound statement
case IToken.tLBRACE:
IASTCompoundStatement compound = compoundStatement();
cleanupLastToken();
return compound;
// selection statement
case IToken.t_if:
startOffset = consume(IToken.t_if).getOffset();
consume(IToken.tLPAREN);
IASTExpression if_condition = condition();
consume(IToken.tRPAREN);
IASTStatement then_clause = statement();
IASTStatement else_clause = null;
if (LT(1) == IToken.t_else) {
consume(IToken.t_else);
else_clause = statement();
}
IASTIfStatement if_stmt = createIfStatement();
if_stmt.setCondition( if_condition );
((CASTNode)if_stmt).setOffset( startOffset );
if_condition.setParent( if_stmt );
if_condition.setPropertyInParent( IASTIfStatement.CONDITION );
if_stmt.setThenClause( then_clause );
then_clause.setParent( if_stmt );
then_clause.setPropertyInParent( IASTIfStatement.THEN );
if( else_clause != null )
{
if_stmt.setElseClause( else_clause );
else_clause.setParent( if_stmt );
else_clause.setPropertyInParent( IASTIfStatement.ELSE );
}
cleanupLastToken();
return if_stmt;
case IToken.t_switch:
startOffset = consume( IToken.t_switch ).getOffset();
consume(IToken.tLPAREN);
IASTExpression switch_condition = condition();
consume(IToken.tRPAREN);
IASTStatement switch_body = statement();
cleanupLastToken();
IASTSwitchStatement switch_statement = createSwitchStatement();
((CASTNode)switch_statement).setOffset( startOffset );
switch_statement.setController( switch_condition );
switch_condition.setParent( switch_statement );
switch_condition.setPropertyInParent( IASTSwitchStatement.CONTROLLER );
switch_statement.setBody( switch_body );
switch_body.setParent( switch_statement );
switch_body.setPropertyInParent( IASTSwitchStatement.BODY );
return switch_statement;
//iteration statements
case IToken.t_while:
startOffset = consume(IToken.t_while).getOffset();
consume(IToken.tLPAREN);
IASTExpression while_condition = condition();
consume(IToken.tRPAREN);
IASTStatement while_body = statement();
cleanupLastToken();
IASTWhileStatement while_statement = createWhileStatement();
((CASTNode)while_statement).setOffset( startOffset );
while_statement.setCondition( while_condition );
while_condition.setParent( while_statement );
while_condition.setPropertyInParent( IASTWhileStatement.CONDITION );
while_statement.setBody( while_body );
while_condition.setParent( while_statement );
while_condition.setPropertyInParent( IASTWhileStatement.BODY );
while_body.setParent( while_statement );
return while_statement;
case IToken.t_do:
startOffset = consume(IToken.t_do).getOffset();
IASTStatement do_body = statement();
consume(IToken.t_while);
consume(IToken.tLPAREN);
IASTExpression do_condition = condition();
consume(IToken.tRPAREN);
cleanupLastToken();
IASTDoStatement do_statement = createDoStatement();
((CASTNode)do_statement).setOffset( startOffset );
do_statement.setBody( do_body );
do_body.setParent( do_statement );
do_body.setPropertyInParent( IASTDoStatement.BODY );
do_statement.setCondition( do_condition );
do_condition.setParent( do_statement );
do_condition.setPropertyInParent( IASTDoStatement.CONDITION );
return do_statement;
case IToken.t_for:
startOffset = consume( IToken.t_for ).getOffset();
consume(IToken.tLPAREN);
IASTNode init = forInitStatement();
IASTExpression for_condition = null;
if (LT(1) != IToken.tSEMI)
for_condition = condition();
consume(IToken.tSEMI);
IASTExpression iterationExpression = null;
if (LT(1) != IToken.tRPAREN) {
iterationExpression = expression();
cleanupLastToken();
}
consume(IToken.tRPAREN);
IASTStatement for_body = statement();
cleanupLastToken();
IASTForStatement for_statement = createForStatement();
((CASTNode)for_statement).setOffset( startOffset );
if( init instanceof IASTDeclaration )
{
for_statement.setInit((IASTDeclaration) init);
((IASTDeclaration) init).setParent( for_statement );
((IASTDeclaration) init).setPropertyInParent( IASTForStatement.INITDECLARATION );
}
else if( init instanceof IASTExpression )
{
for_statement.setInit((IASTExpression) init);
((IASTExpression) init).setParent( for_statement );
((IASTExpression) init).setPropertyInParent( IASTForStatement.INITEXPRESSION );
}
if( for_condition != null )
{
for_statement.setCondition( for_condition );
for_condition.setParent( for_statement );
for_condition.setPropertyInParent( IASTForStatement.CONDITION );
}
if( iterationExpression != null )
{
for_statement.setIterationExpression( iterationExpression );
iterationExpression.setParent( for_statement );
iterationExpression.setPropertyInParent( IASTForStatement.ITERATION );
}
for_statement.setBody( for_body );
for_body.setParent( for_statement );
for_body.setPropertyInParent( IASTForStatement.BODY );
return for_statement;
//jump statement
case IToken.t_break:
startOffset = consume(IToken.t_break).getOffset();
consume(IToken.tSEMI);
cleanupLastToken();
IASTBreakStatement break_statement = createBreakStatement();
((CASTNode)break_statement).setOffset( startOffset );
return break_statement;
case IToken.t_continue:
startOffset = consume(IToken.t_continue).getOffset();
consume(IToken.tSEMI);
cleanupLastToken();
IASTContinueStatement continue_statement = createContinueStatement();
((CASTNode)continue_statement).setOffset( startOffset );
return continue_statement;
case IToken.t_return:
startOffset = consume(IToken.t_return).getOffset();
IASTExpression result = null;
if (LT(1) != IToken.tSEMI) {
result = expression();
cleanupLastToken();
}
consume(IToken.tSEMI);
cleanupLastToken();
IASTReturnStatement return_statement = createReturnStatement();
((CASTNode)return_statement).setOffset( startOffset );
if( result != null )
{
return_statement.setReturnValue( result );
result.setParent( return_statement );
result.setPropertyInParent( IASTReturnStatement.RETURNVALUE );
}
return return_statement;
case IToken.t_goto:
startOffset = consume(IToken.t_goto).getOffset();
IToken identifier = consume(IToken.tIDENTIFIER);
consume(IToken.tSEMI);
cleanupLastToken();
IASTName goto_label_name = createName( identifier );
IASTGotoStatement goto_statement = createGoToStatement();
((CASTNode)goto_statement).setOffset( startOffset );
goto_statement.setName( goto_label_name );
goto_label_name.setParent( goto_statement );
goto_label_name.setPropertyInParent( IASTGotoStatement.NAME );
return goto_statement;
case IToken.tSEMI:
startOffset = consume(IToken.tSEMI ).getOffset();
cleanupLastToken();
IASTNullStatement null_statement = createNullStatement();
((CASTNode)null_statement).setOffset( startOffset );
return null_statement;
default:
// can be many things:
// label
if (LT(1) == IToken.tIDENTIFIER && LT(2) == IToken.tCOLON) {
IToken labelName = consume(IToken.tIDENTIFIER);
consume(IToken.tCOLON);
IASTLabelStatement label_statement = createLabelStatement();
((CASTNode)label_statement).setOffset( labelName.getOffset() );
IASTName name = createName( labelName );
label_statement.setName( name );
name.setParent( label_statement );
name.setPropertyInParent( IASTLabelStatement.NAME );
return label_statement;
}
// expressionStatement
// Note: the function style cast ambiguity is handled in
// expression
// Since it only happens when we are in a statement
IToken mark = mark();
try {
IASTExpression expression = expression();
consume(IToken.tSEMI);
IASTExpressionStatement expressionStatement = createExpressionStatement();
expressionStatement.setExpression( expression );
expression.setParent( expressionStatement );
expression.setPropertyInParent( IASTExpressionStatement.EXPFRESSION );
cleanupLastToken();
return expressionStatement;
} catch (BacktrackException b) {
backup(mark);
}
// declarationStatement
IASTDeclaration d = declaration();
IASTDeclarationStatement ds = createDeclarationStatement();
ds.setDeclaration(d);
d.setParent( ds );
d.setPropertyInParent( IASTDeclarationStatement.DECLARATION );
cleanupLastToken();
return ds;
}
}
/**
* @return
*/
protected IASTLabelStatement createLabelStatement() {
return new CASTLabelStatement();
}
/**
* @return
*/
protected IASTGotoStatement createGoToStatement() {
return new CASTGotoStatement();
}
/**
* @return
*/
protected IASTReturnStatement createReturnStatement() {
return new CASTReturnStatement();
}
/**
* @return
*/
protected IASTForStatement createForStatement() {
return new CASTForStatement();
}
/**
* @return
*/
protected IASTContinueStatement createContinueStatement() {
return new CASTContinueStatement();
}
/**
* @return
*/
protected IASTDoStatement createDoStatement() {
return new CASTDoStatement();
}
/**
* @return
*/
protected IASTBreakStatement createBreakStatement() {
return new CASTBreakStatement();
}
/**
* @return
*/
protected IASTWhileStatement createWhileStatement() {
return new CASTWhileStatement();
}
/**
* @return
*/
protected IASTNullStatement createNullStatement() {
return new CASTNullStatement();
}
/**
* @return
*/
protected IASTSwitchStatement createSwitchStatement() {
return new CASTSwitchStatement();
}
/**
* @return
*/
protected IASTIfStatement createIfStatement() {
return new CASTIfStatement();
}
/**
* @return
*/
protected IASTDefaultStatement createDefaultStatement() {
return new CASTDefaultStatement();
}
/**
* @return
*/
protected IASTCaseStatement createCaseStatement() {
return new CASTCaseStatement();
}
/**
* @return
*/
protected IASTExpressionStatement createExpressionStatement() {
return new CASTExpressionStatement();
}
/**
* @return
*/
protected IASTDeclarationStatement createDeclarationStatement() {
return new CASTDeclarationStatement();
}
protected IASTTypeId typeId(boolean skipArrayModifiers)
throws EndOfFileException, BacktrackException {
IToken mark = mark();

View file

@ -16,10 +16,12 @@ import java.util.List;
import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression;
import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement;
import org.eclipse.cdt.core.dom.ast.IASTConditionalExpression;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTEnumerationSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTExpression;
import org.eclipse.cdt.core.dom.ast.IASTExpressionList;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IASTTypeId;
@ -2067,7 +2069,7 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
* @throws BacktrackException
* request for a backtrack
*/
protected Object usingClause(Object scope) throws EndOfFileException,
protected Object usingClause() throws EndOfFileException,
BacktrackException {
IToken firstToken = consume(IToken.t_using);
@ -2167,7 +2169,7 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
* @throws BacktrackException
* request for a backtrack
*/
protected Object linkageSpecification(Object scope)
protected Object linkageSpecification()
throws EndOfFileException, BacktrackException {
IToken firstToken = consume(IToken.t_extern);
if (LT(1) != IToken.tSTRING)
@ -2202,7 +2204,7 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
break linkageDeclarationLoop;
default:
try {
declaration(linkage, null);
declaration();
} catch (BacktrackException bt) {
failParse(bt);
if (checkToken == LA(1).hashCode())
@ -2240,7 +2242,7 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
}
// linkage.enterScope( requestor );
try {
declaration(linkage, null);
declaration();
} finally {
// linkage.exitScope( requestor );
}
@ -2264,7 +2266,7 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
* @throws BacktrackException
* request for a backtrack
*/
protected Object templateDeclaration(Object scope)
protected Object templateDeclaration()
throws EndOfFileException, BacktrackException {
IToken mark = mark();
IToken firstToken = null;
@ -2310,7 +2312,7 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
}
// templateInstantiation.enterScope( requestor );
try {
declaration(templateInstantiation, templateInstantiation);
declaration();
// templateInstantiation.setEndingOffsetAndLineNumber(lastToken.getEndOffset(),
// lastToken.getLineNumber());
} finally {
@ -2342,7 +2344,7 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
}
// templateSpecialization.enterScope(requestor);
try {
declaration(templateSpecialization, templateSpecialization);
declaration();
// templateSpecialization.setEndingOffsetAndLineNumber(
// lastToken.getEndOffset(), lastToken.getLineNumber());
} finally {
@ -2352,7 +2354,7 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
}
try {
List parms = templateParameterList(scope);
List parms = templateParameterList();
IToken gt = consume(IToken.tGT);
Object templateDecl;
try {
@ -2371,7 +2373,7 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
}
// templateDecl.enterScope( requestor );
try {
declaration(templateDecl, templateDecl);
declaration();
// templateDecl.setEndingOffsetAndLineNumber(
// lastToken.getEndOffset(), lastToken.getLineNumber() );
} finally {
@ -2405,15 +2407,13 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
* @throws BacktrackException
* request for a backtrack
*/
protected List templateParameterList(Object scope)
protected List templateParameterList()
throws BacktrackException, EndOfFileException {
// if we have gotten this far then we have a true template-declaration
// iterate through the template parameter list
List returnValue = new ArrayList();
Object parameterScope = null; /* astFactory.createNewCodeBlock( scope ); */
if (parameterScope == null)
parameterScope = scope;
IToken la = LA(1);
int startingOffset = la.getOffset();
@ -2484,7 +2484,7 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
IToken startingToken = lastToken;
consume(IToken.tLT);
List subResult = templateParameterList(parameterScope);
List subResult = templateParameterList();
consume(IToken.tGT);
consume(IToken.t_class);
IToken optionalId = null;
@ -2602,7 +2602,7 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
* @throws BacktrackException
* request a backtrack
*/
protected void declaration(Object scope, Object ownerTemplate)
protected IASTDeclaration declaration()
throws EndOfFileException, BacktrackException {
switch (LT(1)) {
case IToken.t_asm:
@ -2629,40 +2629,40 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
// resultDeclaration.acceptElement(requestor);
break;
case IToken.t_namespace:
namespaceDefinition(scope);
namespaceDefinition();
break;
case IToken.t_using:
usingClause(scope);
usingClause();
break;
case IToken.t_export:
case IToken.t_template:
templateDeclaration(scope);
templateDeclaration();
break;
case IToken.t_extern:
if (LT(2) == IToken.tSTRING) {
linkageSpecification(scope);
linkageSpecification();
break;
}
default:
if (supportExtendedTemplateSyntax
&& (LT(1) == IToken.t_static || LT(1) == IToken.t_inline || LT(1) == IToken.t_extern)
&& LT(2) == IToken.t_template)
templateDeclaration(scope);
templateDeclaration();
else
simpleDeclarationStrategyUnion(scope, ownerTemplate);
simpleDeclarationStrategyUnion();
}
cleanupLastToken();
return null;
}
protected Object simpleDeclarationStrategyUnion(Object scope,
Object ownerTemplate) throws EndOfFileException, BacktrackException {
protected Object simpleDeclarationStrategyUnion() throws EndOfFileException, BacktrackException {
simpleDeclarationMark = mark();
IProblem firstFailure = null;
IProblem secondFailure = null;
try {
return simpleDeclaration(SimpleDeclarationStrategy.TRY_CONSTRUCTOR,
ownerTemplate, false);
false);
// try it first with the original strategy
} catch (BacktrackException bt) {
if (simpleDeclarationMark == null)
@ -2673,8 +2673,7 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
try {
return simpleDeclaration(
SimpleDeclarationStrategy.TRY_FUNCTION, ownerTemplate,
false);
SimpleDeclarationStrategy.TRY_FUNCTION, false);
} catch (BacktrackException bt2) {
if (simpleDeclarationMark == null) {
if (firstFailure != null && (bt2.getProblem() == null))
@ -2688,8 +2687,7 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
try {
return simpleDeclaration(
SimpleDeclarationStrategy.TRY_VARIABLE, ownerTemplate,
false);
SimpleDeclarationStrategy.TRY_VARIABLE, false);
} catch (BacktrackException b3) {
backup(simpleDeclarationMark); //TODO - necessary?
@ -2720,7 +2718,7 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
* request a backtrack
*
*/
protected Object namespaceDefinition(Object scope)
protected Object namespaceDefinition()
throws BacktrackException, EndOfFileException {
IToken first = consume(IToken.t_namespace);
@ -2770,7 +2768,7 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
break namespaceDeclarationLoop;
default:
try {
declaration(namespaceDefinition, null);
declaration();
} catch (BacktrackException bt) {
failParse(bt);
if (checkToken == LA(1).hashCode())
@ -2844,13 +2842,12 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
* @param tryConstructor
* true == take strategy1 (constructor ) : false == take strategy
* 2 ( pointer to function)
*
* @return TODO
* @throws BacktrackException
* request a backtrack
*/
protected Object simpleDeclaration(SimpleDeclarationStrategy strategy,
Object ownerTemplate, boolean fromCatchHandler)
boolean fromCatchHandler)
throws BacktrackException, EndOfFileException {
IToken firstToken = LA(1);
int firstOffset = firstToken.getOffset();
@ -2860,7 +2857,7 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
throwBacktrack(firstToken.getOffset(), firstToken.getEndOffset(),
firstToken.getLineNumber(), firstToken.getFilename());
DeclarationWrapper sdw = new DeclarationWrapper(null, firstToken
.getOffset(), firstToken.getLineNumber(), ownerTemplate, fn);
.getOffset(), firstToken.getLineNumber(), null, fn);
firstToken = null; // necessary for scalability
declSpecifierSeq(sdw, false,
@ -4091,7 +4088,7 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
break memberDeclarationLoop;
default:
try {
declaration(astClassSpecifier, null);
declaration();
} catch (BacktrackException bt) {
if (checkToken == LA(1).hashCode())
failParseWithErrorHandling();
@ -4220,173 +4217,6 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
}
}
/**
* Parses a statement.
*
* @throws BacktrackException
* request a backtrack
*/
protected IASTStatement statement() throws EndOfFileException,
BacktrackException {
switch (LT(1)) {
case IToken.t_case:
consume(IToken.t_case);
constantExpression();
cleanupLastToken();
consume(IToken.tCOLON);
statement();
cleanupLastToken();
return null;
case IToken.t_default:
consume(IToken.t_default);
consume(IToken.tCOLON);
statement();
cleanupLastToken();
return null;
case IToken.tLBRACE:
compoundStatement();
cleanupLastToken();
return null;
case IToken.t_if:
consume(IToken.t_if);
consume(IToken.tLPAREN);
condition();
consume(IToken.tRPAREN);
if (LT(1) != IToken.tLBRACE)
statement();
else
statement();
if (LT(1) == IToken.t_else) {
consume(IToken.t_else);
if (LT(1) == IToken.t_if) {
//an else if, return and get the rest of the else if as
// the next statement instead of recursing
cleanupLastToken();
return null;
} else if (LT(1) != IToken.tLBRACE)
statement();
else
statement();
}
cleanupLastToken();
return null;
case IToken.t_switch:
consume();
consume(IToken.tLPAREN);
condition();
consume(IToken.tRPAREN);
statement();
cleanupLastToken();
return null;
case IToken.t_while:
consume(IToken.t_while);
consume(IToken.tLPAREN);
condition();
consume(IToken.tRPAREN);
if (LT(1) != IToken.tLBRACE)
statement();
else
statement();
cleanupLastToken();
return null;
case IToken.t_do:
consume(IToken.t_do);
if (LT(1) != IToken.tLBRACE)
statement();
else
statement();
consume(IToken.t_while);
consume(IToken.tLPAREN);
condition();
consume(IToken.tRPAREN);
cleanupLastToken();
return null;
case IToken.t_for:
consume();
consume(IToken.tLPAREN);
forInitStatement();
if (LT(1) != IToken.tSEMI)
condition();
consume(IToken.tSEMI);
if (LT(1) != IToken.tRPAREN) {
expression();
cleanupLastToken();
}
consume(IToken.tRPAREN);
statement();
cleanupLastToken();
return null;
case IToken.t_break:
consume();
consume(IToken.tSEMI);
cleanupLastToken();
return null;
case IToken.t_continue:
consume();
consume(IToken.tSEMI);
cleanupLastToken();
return null;
case IToken.t_return:
consume();
if (LT(1) != IToken.tSEMI) {
expression();
cleanupLastToken();
}
consume(IToken.tSEMI);
cleanupLastToken();
return null;
case IToken.t_goto:
consume();
consume(IToken.tIDENTIFIER);
consume(IToken.tSEMI);
cleanupLastToken();
return null;
case IToken.t_try:
consume();
compoundStatement();
catchHandlerSequence();
cleanupLastToken();
return null;
case IToken.tSEMI:
consume();
cleanupLastToken();
return null;
default:
// can be many things:
// label
if (LT(1) == IToken.tIDENTIFIER
&& LT(2) == IToken.tCOLON) {
consume(IToken.tIDENTIFIER);
consume(IToken.tCOLON);
statement();
cleanupLastToken();
return null;
}
// expressionStatement
// Note: the function style cast ambiguity is handled in
// expression
// Since it only happens when we are in a statement
IToken mark = mark();
Object expressionStatement = null;
try {
expressionStatement = expression();
consume(IToken.tSEMI);
cleanupLastToken();
return null;
} catch (BacktrackException b) {
backup(mark);
// if (expressionStatement != null)
// expressionStatement.freeReferences();
}
// declarationStatement
declaration(null, null);
return null;
}
}
protected void catchHandlerSequence()
throws EndOfFileException, BacktrackException {
@ -4403,7 +4233,7 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
consume(IToken.tELLIPSIS);
else
simpleDeclaration(SimpleDeclarationStrategy.TRY_VARIABLE,
null, true);
true);
consume(IToken.tRPAREN);
catchBlockCompoundStatement();
@ -4432,21 +4262,23 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
/**
* @throws BacktrackException
*/
protected void forInitStatement() throws BacktrackException,
protected IASTNode forInitStatement() throws BacktrackException,
EndOfFileException {
IToken mark = mark();
try {
expression();
consume(IToken.tSEMI);
// e.acceptElement(requestor);
return null;
} catch (BacktrackException bt) {
backup(mark);
try {
simpleDeclarationStrategyUnion(null, null);
return (IASTNode) simpleDeclarationStrategyUnion();
} catch (BacktrackException b) {
failParse(b);
throwBacktrack(b);
return null;
}
}
@ -4471,7 +4303,7 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
while (true) {
try {
int checkOffset = LA(1).hashCode();
declaration(translationUnit, null);
declaration();
if (LA(1).hashCode() == checkOffset)
failParseWithErrorHandling();
} catch (EndOfFileException e) {