NordicSemiconductor / zcbor

Low footprint C/C++ CBOR library and Python tool providing code generation from CDDL descriptions.
Apache License 2.0
105 stars 34 forks source link

Code generation fails with unhelpful error message #431

Open maxd-nordic opened 1 month ago

maxd-nordic commented 1 month ago

I get this error: https://github.com/NordicSemiconductor/zcbor/blob/a9945b7d2e1203d79976a6f6fc9966cc337488e0/zcbor/zcbor.py#L2702

When generating an encoder for the env-object from the following CDDL:

lwm2m_senml = [1* record]

record = {
    ? bn => tstr,            ; Base Name
    ? bt => int .size 8,     ; Base Time
    ? n => tstr,             ; Name
    ? t => int .size 8,      ; Time
    ? ( vi => int .size 8 // ; Integer Value
        vf => float       // ; Float Value
        vs => tstr        // ; String Value
        vb => bool        // ; Boolean Value
        vd => bstr        // ; Data Value
        vlo => tstr ),       ; Object Link Value
    0*5 key-value-pair       ; To handle unordered maps; length-first ordered map keys
}

; now define the generic versions
key-value-pair = ( int => value )

value = tstr / bstr / int .size 8 / float / bool

n  = 0
t = 6
bn = -2
bt = -3
vi = 2
vf = 2
vs = 3
vd = 8
vb = 4
vlo = "vlo"

degrees = vf
percent = vf
pascals = vf
aqi = vi
timestamp = vi
temperature_rid = n
humidity_rid = n
pressure_rid = n
timestamp_rid = n
iaq_rid = n
env-object = [
    {
        bn => "14205/0/",  ; Custom Environment information object
        temperature_rid => "0",          ; Temperature Resource ID
        degrees => float,       ; temperature in degrees Celsius
    },
    {
        humidity_rid => "1",          ; Humidity Resource ID
        percent => float,       ; Relative humidity in percent
    },
    {
        pressure_rid => "2",          ; Atmospheric pressure Resource ID
        pascals => float,       ; Atmospheric pressure in pascals
    },
    {
        iaq_rid => "10",          ; Air Quality Index Resource ID
        aqi => int .size 4  ; AQI value
    },
    {
        timestamp_rid => "99",          ; Timestamp Resource ID
        timestamp => int .size 8   ; UNIX timestamp in milliseconds
    },
]

I'm using the latest pip release 0.8.1.

Could we at least get a useful error message here?

maxd-nordic commented 1 month ago

An extra indirection makes the code generation succeed:

temperature = {
    n => "0",          ; Temperature Resource ID
    vf => float,       ; temperature in degrees Celsius
}
humidity = {
    n => "1",          ; Humidity Resource ID
    vf => float,       ; Relative humidity in percent
}
pressure = {
    n => "2",          ; Atmospheric pressure Resource ID
    vf => float,       ; Atmospheric pressure in pascals
}
iaq = {
    n => "10",          ; Air Quality Index Resource ID
    vi => int .size 4  ; AQI value
}
timestamp = {
    n => "99",          ; Timestamp Resource ID
    vi => int .size 8   ; UNIX timestamp in milliseconds
}

env-object = [
    temperature,
    humidity,
    pressure,
    iaq,
    timestamp
]