qwertie / ecsharp

Home of LoycCore, the LES language of Loyc trees, the Enhanced C# parser, the LeMP macro preprocessor, and the LLLPG parser generator.
http://ecsharp.net
Other
172 stars 25 forks source link

LES3: `a .b` should be a valid expression #108

Closed qwertie closed 4 years ago

qwertie commented 4 years ago

in v27.0, 0xF .Foo is considered an invalid expression because it appears the expression 0xF is followed by the keyword token .Foo. This can be made valid by adding a space after . (0xF . Foo). Note that 0xF.Foo is not what you want; it would be parsed as a floating-point hex custom literal equivalent to _oo"0xF.F"

In LES3 you can't write a C#/Java-style fluent expression like

Use("cool thing")
    .With("other thing");

This is parsed as two statements because of the newline, so I wasn't really concerned that .With is also parsed as a keyword. But now that I'm planning to split LES3 into LESC and LESP, this code should be valid in LESC, so .With should not be treated as a keyword.

There appear to be two solutions.

  1. Treat .With as a keyword in the lexer (that's the status quo) and then reinterpret it as if it were two tokens in the parser.
  2. Treat .With as two tokens in the lexer.

On closer inspection, option 1 doesn't work because the parser always sees .With as a unit, but can't treat it as a unit in case of input like a .b!c, which must be parsed like a.(b!c).

However, option 2 tends to cause user-visible changes to token trees. In the latest build, (' .foo bar) means (#foo, bar), but if there is no keyword token, the natural output becomes (`'.`, foo, bar) instead. I'm inclined to think that this is acceptable. There will also be a slightly lower efficiency of keyword parsing; aside from the extra token, .foo x will first cause a symbol to be created for foo and then the parser will create a second symbol for #foo, on top of the string concat "#"+"foo" that was already happening.

qwertie commented 4 years ago

Change merged today for v28.0