Java 1.5 Parser - Grammar Rules - Part 8

<- back

Next Chapter Grammar Rules - part 9

Actions for Expressions

Not all rules with actions will be presented because they are quite similar.

Coco/R EBFN Rule
PRODUCTIONS

ConditionalExpression<out IExpression expr>
(. IExpression expr1 = null; .)
=
ConditionalOrExpression<out expr1> {{(. expr = expr1; .)
[QuestonMark
(. TwoOperatorExpression twoOpExpr = new TwoOperatorExpression();
twoOpExpr.setLeftExpression( expr1 ); .)
Expression<out expr1> (. twoOpExpr.setMiddleExpression( expr1 ); .)
Colon ConditionalExpression<out expr1>
(. twoOpExpr.setRightExpression( expr1 ); expr = twoOpExpr; .)
].

ConditionalOrExpression<out IExpression expr>
(. IExpression expr1 = null; OperatorExpression opExpr = null;
OperatorExpression topOpExpr = null;
OperatorExpression lastOpExpr = null; .)
=
ConditionalAndExpression<out expr1> (. expr = expr1; .)
 { (. opExpr = new OperatorExpression(); if ( topOpExpr == null ) {
opExpr.setLeftExpression( expr1 ); topOpExpr = opExpr; } .)
LogicOr (. opExpr.setOperator( OPERATOR.LOGIC_OR ); .)
ConditionalAndExpression<out expr1> (. opExpr.setRightExpression( expr1 );
if ( lastOpExpr != null ) { opExpr.setLeftExpression(
lastOpExpr.getRightExpression() ); lastOpExpr.setRightExpression( opExpr );
} lastOpExpr = opExpr; .)
} (. if ( topOpExpr != null ) { expr = topOpExpr; } .)
.

EqualityExpression<out IExpression expr>
(. IExpression expr1 = null; … .)
=
RelationalExpression<out expr1> {{(. expr = expr1; .)
{ (. opExpr = new OperatorExpression(); if ( … .)
(Equal (. opExpr.setOperator( OPERATOR.EQUAL ); .)
| NotEqual (. opExpr.setOperator( OPERATOR.NOT_EQUAL ); .)
) RelationalExpression<out expr1> (. opExpr.setRightExpression( expr1 ); … .)
} (. if ( … .)
.

UnaryExpression<out IExpression expr>
(. IExpression expr1 = null; PrefixUnaryExpression unExpr = null; .)
=
{ (. if ( unExpr == null ) { unExpr = new PrefixUnaryExpression(); } .)
Increment (. unExpr.addOperator( OPERATOR.INCREMENT ); .)
| Decrement (. unExpr.addOperator( OPERATOR.DECREMENT ); .)
| Plus (. unExpr.addOperator( OPERATOR.PLUS ); .)
| Minus (. unExpr.addOperator( OPERATOR.MINUS ); .)
| Tilde (. unExpr.addOperator( OPERATOR.BITWISE_COMPLIMENT ); .)
| ExclamationMark (. unExpr.addOperator( OPERATOR.LOGIC_NOT ); .)
} UnaryExpressionNotPlusMinus<out expr1> (. if ( unExpr != null ) {
unExpr.setExpression( expr1 ); expr = unExpr; } else { expr = expr1; } .)
.

UnaryExpressionNotPlusMinus<out IExpression expr>
(. expr = null; IExpression expr1 = null; .)
=
(PostfixExpression<out expr1> (. expr = expr1; .)
| CastExpression<out expr1> (. expr = expr1; .)
)
.

PostfixExpression<out IExpression expr>
(. IExpression expr1 = null; expr = null;
PostfixUnaryExpression postfixExpr = null;
QualifiedIdentifier qualifiedIdentifier = null; .)
=
(Primary<out expr1> (. expr = expr1; .)
| QualifiedIdentifier<out qualifiedIdentifier> (. expr = qualifiedIdentifier; .)
) { (. if ( postfixExpr == null ) { postfixExpr = new
PostfixUnaryExpression(); postfixExpr.setExpression( expr ); } .)
Increment (. postfixExpr.addOperator( OPERATOR.INCREMENT ); .)
| Decrement (. postfixExpr.addOperator( OPERATOR.DECREMENT ); .)
} (. if ( postfixExpr != null ) { expr = postfixExpr; } .)
.

CastExpression<out IExpression expr>
(. IExpression expr1 = null; CastExpression caExpr = new CastExpression();
IQualifiedIdentifier qualifiedIdentifier = null; IPrimitiveType
primitiveType = null; .)
=
LeftParenthesis ( (PrimitiveType<out primitiveType>
(. caExpr.setPrimitiveType( primitiveType ); .)
RightParenthesis UnaryExpression<out expr1>
(. caExpr.setExpressionToCast( expr1 ); .)
) |
(QualifiedIdentifier<out qualifiedIdentifier>
(. caExpr.setQualifiedIdentifier( qualifiedIdentifier ); .)
RightParenthesis UnaryExpressionNotPlusMinus<out expr1>
(. caExpr.setExpressionToCast( expr1 ); .)
) ) (. expr = caExpr; .)
.

PrimitiveType<out PrimitiveType primitiveType>
(. primitiveType = new PrimitiveType(); .)
=
Byte (. primitiveType.setType( PRIMITIVE_TYPE.BYTE ); .)
| Short (. primitiveType.setType( PRIMITIVE_TYPE.SHORT ); .)
| Int (. primitiveType.setType( PRIMITIVE_TYPE.INT ); .)
| Long (. primitiveType.setType( PRIMITIVE_TYPE.LONG ); .)
| Char (. primitiveType.setType( PRIMITIVE_TYPE.CHAR ); .)
| Float (. primitiveType.setType( PRIMITIVE_TYPE.FLOAT ); .)
| Double (. primitiveType.setType( PRIMITIVE_TYPE.DOUBLE ); .)
| Boolean (. primitiveType.setType( PRIMITIVE_TYPE.BOOLEAN ); .)
.

Actions for dummy rule Primary

Coco/R EBFN Rule
PRODUCTIONS

Primary<out IExpression expr>
(. expr = null; LiteralExpression literalExpr = null; .)
=
IntigerLiteral (. literalExpr = new LiteralExpression(); literalExpr.setLiteralContent( t.val );
literalExpr.setLiteralType( LITERAL.INTEGER ); expr = literalExpr; .)
.

Unit tests

A few tests are shown here

@Test
public void testParser_expression_TwoOperatorExpression() throws UnsupportedEncodingException {
    System.out.println("testParser_expression_TwoOperatorExpression");
    // Initialize
    String sIdentifier = "Process";
    String sFirst = "x"; List< String > firstList = getIdentifierList( sFirst );
    String sSecond = "y"; List< String > secondList = getIdentifierList( sSecond );
    String sThird = "z"; List< String > thirdList = getIdentifierList( sThird );
    final int nFIRST_ANNOTATION = 0, nFIRST_VALUE_PAIR = 0;
    Parser instance = createParser( "@" + sIdentifier + "(" + sFirst + " ? " + sSecond + " : " + sThird + ")" + " package processor;" );
    // Test
    instance.Parse();
    ISourceFile sourceFile = instance.getSourceFile();
    // Validate
    assertNotNull( sourceFile.getAnnotations().get( nFIRST_ANNOTATION ).getElementValuePairs().get(nFIRST_VALUE_PAIR).getElementValue().getElementValueContent() );
    assertTrue( sourceFile.getAnnotations().get( nFIRST_ANNOTATION ).getElementValuePairs().get(nFIRST_VALUE_PAIR).getElementValue().getElementValueContent() instanceof IExpression );
    assertTrue( sourceFile.getAnnotations().get( nFIRST_ANNOTATION ).getElementValuePairs().get(nFIRST_VALUE_PAIR).getElementValue().getElementValueContent() instanceof TwoOperatorExpression );
    TwoOperatorExpression expression = (TwoOperatorExpression) sourceFile.getAnnotations().get( nFIRST_ANNOTATION ).getElementValuePairs().get(nFIRST_VALUE_PAIR).getElementValue().getElementValueContent();
    assertNotNull( expression.getLeftExpression() );
    assertNotNull( expression.getMiddleExpression() );
    assertNotNull( expression.getRightExpression() );
    assertTrue( expression.getLeftExpression() instanceof IQualifiedIdentifier);
    IQualifiedIdentifier qualifiedIdentifier = (IQualifiedIdentifier) expression.getLeftExpression();
    assertEquals( firstList, qualifiedIdentifier.getIdentifierList() );
    assertTrue( expression.getMiddleExpression() instanceof IQualifiedIdentifier);
    qualifiedIdentifier = (IQualifiedIdentifier) expression.getMiddleExpression();
    assertEquals( secondList, qualifiedIdentifier.getIdentifierList() );
    assertTrue( expression.getRightExpression() instanceof IQualifiedIdentifier);
    qualifiedIdentifier = (IQualifiedIdentifier) expression.getRightExpression();
    assertEquals( thirdList, qualifiedIdentifier.getIdentifierList() );
}

For these unittest to work a few help methods must also be present

private Parser createParser( String sContent ) throws UnsupportedEncodingException {
    InputStream is = new ByteArrayInputStream(sContent.getBytes("UTF-8"));
        Scanner scanner = new Scanner(is);
        Parser instance = new Parser( scanner );
        return instance;
}
 
private List< String > getIdentifierList( String sId ) {
    List< String > identifierList = new ArrayList< String >();
        identifierList.add( sId );
        return identifierList;
}
 
private List< String > getIdentifierList( String sId_1, String sId_2 ) {
    List< String > identifierList = new ArrayList< String >();
        identifierList.add( sId_1 );
        identifierList.add( sId_2 );
        return identifierList;
}
 
private List< String > getIdentifierList( String sId_1, String sId_2, String sId_3 ) {
    List< String > identifierList = new ArrayList< String >();
        identifierList.add( sId_1 );
        identifierList.add( sId_2 );
        identifierList.add( sId_3 );
        return identifierList;
}
 
private List< String > getIdentifierList( String sId_1, String sId_2, String sId_3, String sId_4 ) {
    List< String > identifierList = new ArrayList< String >();
        identifierList.add( sId_1 );
        identifierList.add( sId_2 );
        identifierList.add( sId_3 );
        identifierList.add( sId_4 );
        return identifierList;
}
@Test
public void testParser_expression_OperatorExpression_one_or() throws UnsupportedEncodingException {
    System.out.println("testParser_expression_OperatorExpression_one_or");
    // Initialize
    String sIdentifier = "Process";
    String sFirst = "x"; List< String > firstList = getIdentifierList( sFirst );
    String sSecond = "y"; List< String > secondList = getIdentifierList( sSecond );
    final int nFIRST_ANNOTATION = 0, nFIRST_VALUE_PAIR = 0;
    Parser instance = createParser( "@" + sIdentifier + "(" + sFirst + " || " + sSecond + ")" + " package processor;" );
    // Test
    instance.Parse();
    ISourceFile sourceFile = instance.getSourceFile();
    // Validate
    assertNotNull( sourceFile.getAnnotations().get( nFIRST_ANNOTATION ).getElementValuePairs().get(nFIRST_VALUE_PAIR).getElementValue().getElementValueContent() );
    assertTrue( sourceFile.getAnnotations().get( nFIRST_ANNOTATION ).getElementValuePairs().get(nFIRST_VALUE_PAIR).getElementValue().getElementValueContent() instanceof IExpression );
    assertTrue( sourceFile.getAnnotations().get( nFIRST_ANNOTATION ).getElementValuePairs().get(nFIRST_VALUE_PAIR).getElementValue().getElementValueContent() instanceof OperatorExpression );
    OperatorExpression expression = (OperatorExpression) sourceFile.getAnnotations().get( nFIRST_ANNOTATION ).getElementValuePairs().get(nFIRST_VALUE_PAIR).getElementValue().getElementValueContent();
    assertNotNull( expression.getLeftExpression() );
    assertNotNull( expression.getRightExpression() );
    assertEquals( OPERATOR.LOGIC_OR, expression.getOperator() );
    assertTrue( expression.getLeftExpression() instanceof IQualifiedIdentifier);
    IQualifiedIdentifier qualifiedIdentifier = (IQualifiedIdentifier) expression.getLeftExpression();
    assertEquals( firstList, qualifiedIdentifier.getIdentifierList() );
    assertTrue( expression.getRightExpression() instanceof IQualifiedIdentifier);
    qualifiedIdentifier = (IQualifiedIdentifier) expression.getRightExpression();
    assertEquals( secondList, qualifiedIdentifier.getIdentifierList() );
}
 
@Test
public void testParser_expression_OperatorExpression_and_or_and() throws UnsupportedEncodingException {
    System.out.println("testParser_expression_OperatorExpression_and_or_and");
    // Initialize
    String sIdentifier = "Process";
    String sFirst = "x"; List< String > firstList = getIdentifierList( sFirst );
    String sSecond = "y"; List< String > secondList = getIdentifierList( sSecond );
    String sThird = "z"; List< String > thirdList = getIdentifierList( sThird );
    String sFourth = "q"; List< String > fourthList = getIdentifierList( sFourth );
    final int nFIRST_ANNOTATION = 0, nFIRST_VALUE_PAIR = 0;
    Parser instance = createParser( "@" + sIdentifier + "(" + sFirst + " && " + sSecond + " || " + sThird + " && " + sFourth + ")" + " package processor;" );
    // Test
    instance.Parse();
    ISourceFile sourceFile = instance.getSourceFile();
    OperatorExpression expression = (OperatorExpression) sourceFile.getAnnotations().get( nFIRST_ANNOTATION ).getElementValuePairs().get(nFIRST_VALUE_PAIR).getElementValue().getElementValueContent();
    // Validate
    assertTrue( expression.getLeftExpression() instanceof OperatorExpression);
    assertTrue( expression.getRightExpression() instanceof OperatorExpression);
    assertEquals( OPERATOR.LOGIC_OR, expression.getOperator() );
 
    OperatorExpression opExpr_1 = (OperatorExpression) expression.getLeftExpression();
    assertTrue( opExpr_1.getLeftExpression() instanceof IQualifiedIdentifier);
    assertTrue( opExpr_1.getRightExpression() instanceof IQualifiedIdentifier);
    assertEquals( OPERATOR.LOGIC_AND, opExpr_1.getOperator() );
    IQualifiedIdentifier qualifiedIdentifier_1 = (IQualifiedIdentifier) opExpr_1.getLeftExpression();
    IQualifiedIdentifier qualifiedIdentifier_2 = (IQualifiedIdentifier) opExpr_1.getRightExpression();
    assertEquals( firstList, qualifiedIdentifier_1.getIdentifierList() );
    assertEquals( secondList, qualifiedIdentifier_2.getIdentifierList() );
 
    OperatorExpression opExpr_2 = (OperatorExpression) expression.getRightExpression();
    assertTrue( opExpr_2.getLeftExpression() instanceof IQualifiedIdentifier);
    assertTrue( opExpr_2.getRightExpression() instanceof IQualifiedIdentifier);
    assertEquals( OPERATOR.LOGIC_AND, opExpr_2.getOperator() );
    IQualifiedIdentifier qualifiedIdentifier_3 = (IQualifiedIdentifier) opExpr_2.getLeftExpression();
    IQualifiedIdentifier qualifiedIdentifier_4 = (IQualifiedIdentifier) opExpr_2.getRightExpression();
    assertEquals( thirdList, qualifiedIdentifier_3.getIdentifierList() );
    assertEquals( fourthList, qualifiedIdentifier_4.getIdentifierList() );
}

Next Chapter Grammar Rules - part 9


<- back

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License