3 ms·
> So it's not ambiguous. It is. you can parse something given how it's indented. Doesn't mean that the resulting code is actually correct (from the logic point
by dmitriid 4y ago
> So it's not ambiguous.
It is. you can parse something given how it's indented. Doesn't mean that the resulting code is actually correct (from the logic point of view). And you cannot properly format it because the logic is ambiguous.
Both of these are valid code. One (or both of them) are incorrect
if x > 0:
function1()
function2()
function3()
if x > 0:
function1()
function2()
function3()
No formatter will be able to guess which one is correct. It will do a best effort approximation. But good luck figuring out how to format a YAML file:
db:
mysqldatabase:
hostname: localhost
port: 3012
username: root
password: root
Oh oops. Valid file. Can be parsed. Invalid indentation
> IDEA (by now) does very stupid things while trying hard to be smart.
This is beside the point.
- still_grokking 4y agoif x > 0: function1() function2() function3() if x > 0: function1() function2() function3() Where's the ambiguity? Please show two or more possible ways to parse this code, as I still don't see it. I see in each case only one way to parse the examples. An ambiguity in parsing is something like that (in e.g. C++): `a<b>(c)`. You could read it as an boolean expression `(a < b) > c` or as a call to a templated function `a<TemplateParam>(someType param)`. But there is only one way to read the above code, imho.
- davidgrenier 4y agoI don't understand your first example. They are both correct, but have different semantic meaning. A parser doesn't have to try to find incorrectness there, it simply has to infer the semantic meaning of both, which is... the second one calls function3() irrespective of x. Both programs should be expressible in any language hence I don't see why you are placing the onus on the compiler to tell you one is incorrect.
- dmitriid 4y ago> They are both correct, but have different semantic meaning. Exactly > it simply has to infer the semantic meaning of both, which is How exactly do you want the parser for the formatter to "infer semantic meaning"? It has no knowledge of the human requirements for this code. To the parser both are valid > Both programs should be expressible in any language hence I don't see why you are placing the onus on the compiler to tell you one is incorrect. Are we talking about "expressible in any language"? No. Here's what I wrote: "Formatters will no save you from the many, many, many cases where indentation is simply ambiguous" in the context of formatters.