(Have I chosen an appropriate forum for this question?) Absolutely.
What confuses me is the unbalanced brackets and parentheses. Can you please explain the strange syntax. I had to stare at this a while, looking at the
online version. It turns out that the use of italics is key (!!!), so I'll reproduce the original more exactly:
[
( ]
BracketsOpt . class |
Expression ]
) Note that all brackets on this line are not in italics, which seems to indicate that they are literal bracket chars rather than indicators that an element is optional. (If you look at the top of the page, or other examples,
[foo] indicates zero or one foo, while '['
foo ] indicates [ followed by
foo followed by ']'. Meanwhile, the parens in the above are italicized, indicating they are
not literal. They didn't bother explaining what non-literal parens mean, but I think we can assume they're used for logical grouping. So, the above expression can be interpreted as (using more Java-like notation):
[code]'[' +
( ']' +
BracketsOpt + '.' + class
|
Expression + ']'
) So, using the above expression as part of the rule for
Primary:
Identifier { . Identifier } [ IdentifierSuffix ] we can see that it could represent
java.lang.String (no
IdentifierSuffix)
java.lang.String[].class (no
BracketsOpt)
java.lang.String[][][].class (using
BracketsOpt)
java.lang.String[n + 1] (using
Expression)
Note that
java.lang.String.class
is not covered here; that's handled by a different line of
IdentifierSuffix. The part you ask about is just for handling things with at least one bracket in them.
I want to apply IdentiierSuffix to a cast expression of this form (T)a().b() Let's see - that's an
Expression, more specifically an
Expression1, more specifically an
Expression2, more specifically an
Expression3. Now we see that "(T)" matches '(' +
Type + ')', so the remaining "a().b()" must be another
Expression3. How?
We can see that "a()" matches
Primary:
Identifier { . Identifier }[ IdentifierSuffix] since "a" is an
Identifier, and "()" matches
IdentifierSuffix:
Arguments
Arguments:
( [Expression { , Expression }] )
(No
Expression needed.) So given that "a()" is a
Primary, "a().b()" matches
Expression3:
Primary {Selector} {PostfixOp} if we can make ".b()" match
Selector. Well, we have
Selector:
. Identifier [Arguments] so yes, "b" matches
Identifier, and "()" matches
Argumetns.
And we begin to see why we normally let compilers handle this sort of thing, rather than humans.
<digression>
I see that that while JLS 18 generally uses italics to indicate logical structures and no italics for literals, they use a non-italicized '|' to indicate logical alternation, e.g.
(foo | bar
). So how do they indicate a literal '|'? Looking at
Infixop, they evidently just put the '|' on a single line by itself and expect you to figure that since there's nothing else there, it can't represent alternation; must be literal. :roll: Gee, thanks for consistency.
Also, what about '.'? Is it italicized, or not? Looking at the HTML source I see that frequently they use an italicized '.' when they want a literal. Which makes no sense at all, except that if you don't look at the source, no one can tell if it's italicized or not, so I guess the italics are just there to confuse people who actually look at the HTML source. Let that be a lesson for us.
And lastly, look at this line from
IdentifierSuffix:
. (
class |
this | super
Arguments | new
InnerCreator ) The initial ( is not italicized, while the final one is. Note also that class and this are italicized, but not super. This is a special usage of italics, used to denote the fact that the author is on crack, and should not be trusted.

Well, maybe not the original author(s); maybe it was whoever converted to HTML. (I don't have a print copy of the JLS handy - could someone else check this?) The point is though - the JLS
can contain errors. Though they're pretty rare. And moere annoyingly, there doesn't seem to be any published errata

, though Gilad Bracha has acknowledged that they do know about some errors. (Guess I'll send another e-mail now.) Anyway, don't get too locked into literal interpretation of everything that you overlook the possibility that they've simply screwed up (in some very subtle way, no doubt).
</digression>
Cheers...
[ September 13, 2003: Message edited by: Jim Yingst ]