Commit 94cfe6c5 authored by Arpad Lovassy's avatar Arpad Lovassy
Browse files

Bug 571311 - CFG file macro multiplication


Signed-off-by: Arpad Lovassy's avatarzlovarp <arpad.lovassy@semcon.com>
parent 21c383d0
...@@ -614,11 +614,12 @@ BITSTRINGMATCH: '\'' FR_BINDIGITMATCH* '\'' 'B'; ...@@ -614,11 +614,12 @@ BITSTRINGMATCH: '\'' FR_BINDIGITMATCH* '\'' 'B';
HEXSTRINGMATCH: '\'' FR_HEXDIGITMATCH* '\'' 'H'; HEXSTRINGMATCH: '\'' FR_HEXDIGITMATCH* '\'' 'H';
OCTETSTRINGMATCH: '\'' FR_OCTDIGITMATCH* '\'' 'O'; OCTETSTRINGMATCH: '\'' FR_OCTDIGITMATCH* '\'' 'O';
COMMA: ','; COMMA: ',';
STAR5: '*' -> type(STAR);
FSTRING: FSTRING:
( '\\"' // \" is handled separately in the structured definitions ( '\\"' // \" is handled separately in the structured definitions
| '\\' . // Handle escaped characters | '\\' . // Handle escaped characters
| ~[{}"\\$\n\r\t #/,:=]+ // Anything except {,},'"',\,$,#,/,',',:,= and whitespace | ~[{}"\\$\n\r\t #/,:=*]+ // Anything except {,},'"',\,$,#,/,',',:,=,* and whitespace
// comparing to titan.core [,:=] is excluded, but pr_SimpleValue accepts ASSIGNMENTCHAR, COLON and COMMA // comparing to titan.core [,:=*] is excluded, but pr_SimpleValue accepts ASSIGNMENTCHAR, COLON and COMMA, * is a multiplication operator
| '/' | '/'
); );
......
...@@ -44,6 +44,7 @@ import java.util.regex.Pattern; ...@@ -44,6 +44,7 @@ import java.util.regex.Pattern;
@members{ @members{
// format strings for error messages if definition (macro or environment variable) cannot be resolved // format strings for error messages if definition (macro or environment variable) cannot be resolved
// %s : definition // %s : definition
private static final String DEFINITION_NOT_FOUND = "Could not resolve definition: %s";
private static final String DEFINITION_NOT_FOUND_STRING = "Could not resolve definition: %s using \"\" as a replacement."; private static final String DEFINITION_NOT_FOUND_STRING = "Could not resolve definition: %s using \"\" as a replacement.";
private static final String DEFINITION_NOT_FOUND_BSTR = "Could not resolve definition: %s using ''B as a replacement."; private static final String DEFINITION_NOT_FOUND_BSTR = "Could not resolve definition: %s using ''B as a replacement.";
private static final String DEFINITION_NOT_FOUND_HSTR = "Could not resolve definition: %s using ''H as a replacement."; private static final String DEFINITION_NOT_FOUND_HSTR = "Could not resolve definition: %s using ''H as a replacement.";
...@@ -303,6 +304,27 @@ import java.util.regex.Pattern; ...@@ -303,6 +304,27 @@ import java.util.regex.Pattern;
return value; return value;
} }
/**
* Gets the macro value string of a macro (general, with or without type)
* @param aMacroToken the macro token
* @param aErrorFormatStr format strings for error messages if definition (macro or environment variable) cannot be resolved
* %s : definition
* @return the macro value string
* or "" if macro is invalid. In this case an error marker is also created
*/
private String getAnyMacroValue( final Token aMacroToken, String aErrorFormatStr ) {
String definition = getMacroName( aMacroToken.getText() );
if (definition == null) {
definition = getTypedMacroName( aMacroToken.getText() );
}
final String errorMsg = String.format( DEFINITION_NOT_FOUND, definition );
final String value = getDefinitionValue( definition );
if ( value == null ) {
reportError( errorMsg, aMacroToken, aMacroToken );
}
return value;
}
/** /**
* Creates a wrapper TerminalNode around the Token to use it as ParseTree * Creates a wrapper TerminalNode around the Token to use it as ParseTree
* @param aToken token to wrap * @param aToken token to wrap
...@@ -1158,7 +1180,9 @@ pr_MacroAssignment returns [ DefineSectionHandler.Definition definition ] ...@@ -1158,7 +1180,9 @@ pr_MacroAssignment returns [ DefineSectionHandler.Definition definition ]
}: }:
( col = TTCN3IDENTIFIER { name = $col.getText(); } ( col = TTCN3IDENTIFIER { name = $col.getText(); }
ASSIGNMENTCHAR ASSIGNMENTCHAR
endCol = pr_MacroRhs { value = $endCol.text; } ( endCol = pr_MacroRhs { value = $endCol.text; }
| mul = pr_MacroMultiplication { value = $mul.value; }
)
) )
{ if(name != null && value != null) { { if(name != null && value != null) {
addDefinition( name, value, $col ); addDefinition( name, value, $col );
...@@ -1177,6 +1201,38 @@ pr_MacroRhs: ...@@ -1177,6 +1201,38 @@ pr_MacroRhs:
ENDCHAR ENDCHAR
; ;
pr_MacroMultiplication returns [ String value ]
@init {
$value = null;
double num = 0;
}:
( v1 = pr_MacroAssignmentValue
{ final String defValue = $v1.text.charAt(0) == '$' ? getAnyMacroValue( $v1.start, DEFINITION_NOT_FOUND ) : $v1.text;
if ( defValue != null ) {
try {
num = Double.parseDouble( defValue );
} catch (Exception e) {
reportError( "First operand of multiplication is not a number: "+defValue, $v1.start, $v1.stop );
}
}
}
( STAR
v2 = pr_MacroAssignmentValue
{ final String defValue2 = $v2.text.charAt(0) == '$' ? getAnyMacroValue( $v2.start, DEFINITION_NOT_FOUND ) : $v2.text;
if ( defValue2 != null ) {
try {
num *= Double.parseDouble( defValue2 );
} catch (Exception e) {
reportError( "Second operand of multiplication is not a number: "+defValue2, $v2.start, $v2.stop );
}
}
}
)+
)
{ $value = ""+num;
}
;
pr_StructuredValue: pr_StructuredValue:
( pr_SimpleValue ( pr_SimpleValue
| BEGINCHAR | BEGINCHAR
......
...@@ -327,11 +327,12 @@ BITSTRINGMATCH: '\'' FR_BINDIGITMATCH* '\'' 'B'; ...@@ -327,11 +327,12 @@ BITSTRINGMATCH: '\'' FR_BINDIGITMATCH* '\'' 'B';
HEXSTRINGMATCH: '\'' FR_HEXDIGITMATCH* '\'' 'H'; HEXSTRINGMATCH: '\'' FR_HEXDIGITMATCH* '\'' 'H';
OCTETSTRINGMATCH: '\'' FR_OCTDIGITMATCH* '\'' 'O'; OCTETSTRINGMATCH: '\'' FR_OCTDIGITMATCH* '\'' 'O';
COMMA: ','; COMMA: ',';
STAR5: '*' -> type(STAR);
FSTRING: FSTRING:
( '\\"' // \" is handled separately in the structured definitions ( '\\"' // \" is handled separately in the structured definitions
| '\\' . // Handle escaped characters | '\\' . // Handle escaped characters
| ~[{}"\\$\n\r\t #/,:=]+ // Anything except {,},'"',\,$,#,/,',',:,= and whitespace | ~[{}"\\$\n\r\t #/,:=*]+ // Anything except {,},'"',\,$,#,/,',',:,=,* and whitespace
// comparing to titan.core [,:=] is excluded, but pr_SimpleValue accepts ASSIGNMENTCHAR, COLON and COMMA // comparing to titan.core [,:=*] is excluded, but pr_SimpleValue accepts ASSIGNMENTCHAR, COLON and COMMA, * is a multiplication operator
| '/' | '/'
); );
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment