Search code examples
pharo

Message precedence with parenthesis and how to check for it?


In the AST I'm looking at RBMessageNode and I'd like to check if that node as precedence that differs from the standard. Noticed startWithParenthesis and stopWithParenthesis but they don't seem to produce the results I expected.

aNode startWithParenthesis ifTrue: [ ... do whatever ... ].

Is there a way to do this?


Solution

  • What's interesting here is the equivalence between the original condition

    • detect nodes with a precedence that differs from the standard

    and the one the parser needs to conform to the Smalltalk syntax

    • the need for parenthesis.

    As a good practice, when one finds these kinds of coincidences it is good to make them explicit by adding a second selector that conveys the other meaning. Given that in this case these are methods for testing, we have two options:

    negative testing:

    subvertsPrecedence
      ^self needsParenthesis
    

    positive testing:

    hasStandardPrecedence
      ^self needsParenthesis not
    

    We should implemente the one that better express our intention. And if we decide to implement the two of them, it is better to re-writte the second as

    positive testing:

    hasStandardPrecedence
      ^self subvertsPrecedence not
    

    in order to make it clearer the relationship with the other.