49
49
import static org .intellij .grammar .generator .NameShortener .getRawClassName ;
50
50
import static org .intellij .grammar .generator .ParserGeneratorUtil .*;
51
51
import static org .intellij .grammar .generator .RuleGraphHelper .Cardinality .*;
52
- import static org .intellij .grammar .generator .RuleGraphHelper .getSynonymTargetOrSelf ;
53
- import static org .intellij .grammar .generator .RuleGraphHelper .hasPsiClass ;
52
+ import static org .intellij .grammar .generator .RuleGraphHelper .*;
54
53
import static org .intellij .grammar .psi .BnfTypes .*;
55
54
56
55
@@ -2036,6 +2035,7 @@ private JBIterable<?> resolveUserPsiPathMethods(BnfRule startRule,
2036
2035
String item = indexStart > -1 ? pathElement .substring (0 , indexStart ).trim () : pathElement .trim ();
2037
2036
2038
2037
if (item .isEmpty ()) return item ;
2038
+ if (targetRule [0 ] == null ) return null ;
2039
2039
RuleMethodsHelper .MethodInfo targetInfo = myRulesMethodsHelper .getMethodInfo (targetRule [0 ], item );
2040
2040
targetRule [0 ] = targetInfo == null ? null : targetInfo .rule ;
2041
2041
return targetInfo ;
@@ -2057,18 +2057,44 @@ private void generateUserPsiAccessors(BnfRule startRule, RuleMethodsHelper.Metho
2057
2057
boolean last = i == splitPath .length - 1 ;
2058
2058
int indexStart = pathElement .indexOf ('[' );
2059
2059
int indexEnd = indexStart > 0 ? pathElement .lastIndexOf (']' ) : -1 ;
2060
+ String base = (indexStart > -1 ? pathElement .substring (0 , indexStart ) : pathElement ).trim ();
2060
2061
String index = indexEnd > -1 ? pathElement .substring (indexStart + 1 , indexEnd ).trim () : null ;
2061
- if ( "first" . equals ( index )) index = "0" ;
2062
+
2062
2063
2063
2064
RuleMethodsHelper .MethodInfo targetInfo = (RuleMethodsHelper .MethodInfo )m ;
2064
- if (targetInfo == null ||
2065
- index != null && !targetInfo .cardinality .many () ||
2066
- i > 0 && StringUtil .isEmpty (targetInfo .name ) && targetInfo .rule == null ) {
2065
+ String error ;
2066
+ if (indexStart > 0 && (indexEnd == -1 || StringUtil .isNotEmpty (pathElement .substring (indexEnd + 1 )))) {
2067
+ error = "'<name>[<index>]' expected, got '" + pathElement + "'" ;
2068
+ }
2069
+ else if (targetInfo == null ) {
2070
+ Collection <String > available = targetRule == null ? null : myRulesMethodsHelper .getMethodNames (targetRule );
2071
+ if (targetRule == null ) {
2072
+ error = "'" + base + "' not found in '" + splitPath [i - 1 ] + "' (not a rule)" ;
2073
+ }
2074
+ else if (available == null || available .isEmpty ()) {
2075
+ error = "'" + base + "' not found in '" + targetRule .getName () + "' (available: nothing)" ;
2076
+ }
2077
+ else {
2078
+ error = "'" + base + "' not found in '" + targetRule .getName () + "' (available: " + String .join (", " , available ) + ")" ;
2079
+ }
2080
+ }
2081
+ else if (index != null && !targetInfo .cardinality .many ()) {
2082
+ error = "'[" + index + "]' unexpected after '" + base + getCardinalityText (targetInfo .cardinality ) + "'" ;
2083
+ }
2084
+ else if (!last && index == null && targetInfo .cardinality .many ()) {
2085
+ error = "'[<index>]' required after '" + base + getCardinalityText (targetInfo .cardinality ) + "'" ;
2086
+ }
2087
+ else if (i > 0 && StringUtil .isEmpty (targetInfo .name )) {
2088
+ error = "'" + base + "' accessor suppressed in '" + splitPath [i - 1 ] + "'" ;
2089
+ }
2090
+ else {
2091
+ error = null ;
2092
+ }
2093
+ if (error != null ) {
2067
2094
if (intf ) { // warn only once
2068
- addWarning (startRule .getProject (), "incorrect item '" + pathElement + "' in '" + startRule .getName () + "' method " +
2069
- methodInfo .name + "=\" " + methodInfo .path + "\" " );
2095
+ addWarning (startRule .getProject (), "%s#%s(\" %s\" ): %s" , startRule .getName (), methodInfo .name , methodInfo .path , error );
2070
2096
}
2071
- return ; // missing rule, unknown or wrong cardinality
2097
+ return ;
2072
2098
}
2073
2099
2074
2100
boolean many = targetInfo .cardinality .many ();
@@ -2105,6 +2131,8 @@ private void generateUserPsiAccessors(BnfRule startRule, RuleMethodsHelper.Metho
2105
2131
2106
2132
// list item
2107
2133
if (index != null ) {
2134
+ if ("first" .equals (index )) index = "0" ;
2135
+
2108
2136
context += "." ;
2109
2137
boolean isLast = index .equals ("last" );
2110
2138
if (isLast ) index = context + "size() - 1" ;
0 commit comments