dev-cafe / parselglossy

Generic input parsing library, speaking in tongues.
https://parselglossy.readthedocs.io
MIT License
7 stars 2 forks source link

Multiple definitions of keywords #83

Closed stigrj closed 4 years ago

stigrj commented 4 years ago

Description

Currently the following is allowed

Foo {
  bar = true
}

Foo {
  bar = false
}

and the last definition takes precedence. This should probably not be allowed.

robertodr commented 4 years ago

@bast and I agree that the parser should indeed stop. First step is to provide a test.

bast commented 4 years ago

I poked a bit around in the code and it is asDict() which swallows duplicates and keeps the last insert: https://github.com/dev-cafe/parselglossy/blob/76fbecbf9479fd67b380bedd01ceeb85a7176a23/parselglossy/grammars/lexer.py#L74

What we would like to do is to detect duplicates and stop and duplicates are preserved when replacing asDict() by asList().

But then one needs to recursively traverse a nested list to look for duplicate keys at the same level and this is where I got stuck: since with a nested list I did not know how to distinguish a key from a value (is ['foo', 'bar'] a foo: bar key-value pair or is it a value ['foo', 'bar']?)

One thing we could do is to flatten the list and flatten the dictionary and count the number of elements and if they are different, something was repeated but we would not be able to tell what.

bast commented 4 years ago

I have this counting solution almost ready. PR coming up in not too long.