usethesource / rascal

The implementation of the Rascal meta-programming language (including interpreter, type checker, parser generator, compiler and JVM based run-time system)
http://www.rascal-mpl.org
Other
406 stars 78 forks source link

confusing explanation of labels and names in Syntax Definitions documentation #1367

Open jurgenvinju opened 4 years ago

jurgenvinju commented 4 years ago

See the question by ThomasH on SO: https://stackoverflow.com/questions/60411725/rascal-what-is-the-difference-between-label-and-name-in-the-syntax-definiti/60412309#60412309 about http://tutor.rascal-mpl.org/Rascal/Declarations/SyntaxDefinition/SyntaxDefinition.html

thron7 commented 4 years ago

For the symbol position labels as you described in your SO answer, for me as a consumer of a parse tree it is interesting which value they have when a different alternative has matched.

So, to pick up on your example, slightly changed: When I have the grammar rule syntax E = E lhs "+" E rhs | E minuend "-" E subtrahend, what would be the value of minuend and subtrahend if the first (addition) rule has matched? Is there a null value in Rascal?

jurgenvinju commented 4 years ago

The .minuend operator for “field selection” or sometimes called projection is statically allowed on any tree of type E, but when it is dynamically applied to a tree that doesn’t have it, you will get a runtime exception. I believe NoSuchField(..).

Rascal has no generic null value at all.

To guard for the exception you can use the has operator and the ? set of operators in the context of if and such, but in general (concrete) pattern matching is a nicer way of avoiding the exception altogether.

Virtually All pattern matching is in some conditional context in rascal (function dispatch, switch, visit, if, for, catch, comprehensions), and this avoids again the need for null.

The difference with pattern matching and field projection to get at a nested value is thus that you can statically depend on the binding with pattern matching, but have to assume a dynamic exception might be thrown with projection+assignment.