hadronized / glsl

GLSL parser for Rust
190 stars 28 forks source link

GLSL parse error: Literals should be float not double by default #134

Closed vron closed 3 years ago

vron commented 3 years ago

Hi,

According to https://www.khronos.org/opengl/wiki/Data_Type_(GLSL)#Struct_constructors and my experience floating point literals should be floats by default and not double as glsl currently parse them as.

Run e.g.:

echo "float a = 7.7;" | glsl-tree

To note that the 7.7 is parsed into the AST as a double constant instead of a float. This creates problems when e.g. using the AST to transpile to other languages since a double constant will be printed instead of a float constant => type errors.

I believe that the fix is as simple as swapping the order of float / double parsing in glsl/src/parsers.rs such that the Float one is first:

pub fn primary_expr(i: &str) -> ParserResult<syntax::Expr> {
  alt((
    parens_expr,
    map(float_lit, syntax::Expr::FloatConst), // swapped with row below
    map(double_lit, syntax::Expr::DoubleConst), // swapped with row below
    map(unsigned_lit, syntax::Expr::UIntConst),
    map(integral_lit, syntax::Expr::IntConst),
    map(bool_lit, syntax::Expr::BoolConst),
    map(identifier, syntax::Expr::Variable),
  ))(i)
}

(Also note that the glsl version og glsl-tree should probably be bumped to the 5 release?)

hadronized commented 3 years ago

Thanks for reporting. That’s indeed a bug. I’ll patch both problems today! :heart:

hadronized commented 3 years ago

The required change was a bit bigger than what was expected, but all in all, the problem is now fixed. I’ll release the patch in glsl-5.0.1.