Ballerina-lang: Some issues with floating point literal parsing

Created on 10 May 2020  路  8Comments  路  Source: ballerina-platform/ballerina-lang

Case I:

function foo() {
    float f = 5.;
}

Above is parsed as a valid code.

Case II:

function foo() {
    float f = 5.4e;
}

Above gives multiple error messages for a single error. Below are the error messages and the resulting tree.

// Error messages
xxx.bal:0:0:missing digit
xxx.bal:0:0:invalid token '5.4e'
xxx.bal:0:0:missing  MISSING[]

// Resulting tree
function foo() {
    float f =  MISSING[]5.4e;
}

Case III

function foo() {
    float f = 5.e1;
}

Above also get parsed as a valid code.

AreParser PrioritLow TypImprovement

All 8 comments

@SupunS I allowed case I and III intentionally. Test cases for them also added.

floating-point-literal := DecimalFloatingPointNumber
DecimalFloatingPointNumber := DottedDecimalNumber [Exponent] [FloatingPointTypeSuffix]
DottedDecimalNumber := DecimalNumber . Digit*

IIUC , DecimalNumber . Digit* means it is allowed have no digits after dot token right?

I will have a look on case II. :)

@SupunS I allowed case I and III intentionally. Test cases for them also added.

floating-point-literal := DecimalFloatingPointNumber
DecimalFloatingPointNumber := DottedDecimalNumber [Exponent] [FloatingPointTypeSuffix]
DottedDecimalNumber := DecimalNumber . Digit*

IIUC , DecimalNumber . Digit* means it is allowed have no digits after dot token right?

I see. Yes you are correct. I didn't notice that part.

I think for case-III, we can log an error in the lexer, but still return a valid floating-point-literal token, so that the rest of the parser will work normally.. WDYT?

I think for case-III, we can log an error in the lexer, but still return a valid floating-point-literal token, so that the rest of the parser will work normally.. WDYT?

Case III should be fine. I think you are referring to case II here. Yeah we can do that change. +1

Yes, I meant Case-II

Similar to case II, we have another two scenarios where numerical literals are processed as invalid tokens.

  1. When an int or a float starts with a zero. e.g. float f = 02.3
  2. When hex-digit starts with a dot token and there's no at least one hex-digit after that. e.g. float f = 0x.p12

Are we going to allow them to parse as well while logging an error at lexer level?

hmm.. yeah we should do that.

Anyway, I'm lowering the priority of the issue since this doesn't break any code, and only an improvement.

Was this page helpful?
0 / 5 - 0 ratings