View Javadoc

1   package org.codehaus.groovy.syntax.parser;
2   
3   import org.codehaus.groovy.syntax.Types;
4   import org.codehaus.groovy.syntax.Token;
5   
6   public class UnexpectedTokenException extends ParserException {
7       private Token unexpectedToken;
8       private int[] expectedTypes;
9       private String comment;
10  
11      public UnexpectedTokenException(Token token) {
12          this(token, null, null );
13      }
14      
15      public UnexpectedTokenException(Token token, int expectedType) {
16          this(token, new int[] { expectedType });
17      }
18      
19      public UnexpectedTokenException(Token token, int[] expectedTypes) {
20          this(token, expectedTypes, null );
21      }
22  
23      public UnexpectedTokenException(Token token, int[] expectedTypes, String comment) {
24          super("Unexpected token", token);
25          this.unexpectedToken = token;
26          this.expectedTypes = expectedTypes;
27          this.comment       = comment;
28      }
29  
30      public Token getUnexpectedToken() {
31          return this.unexpectedToken;
32      }
33  
34      public int[] getExpectedTypes() {
35          return this.expectedTypes;
36      }
37  
38      public String getUnexpectedTokenText( ) {
39          String text = null;
40          if( this.unexpectedToken != null )
41          {
42              text = this.unexpectedToken.getText();
43          }
44  
45          if( text == null )
46          {
47              text = "";
48          }
49  
50          return text;
51      }
52  
53      public String getMessage() {
54          StringBuffer message = new StringBuffer();
55  
56          if( expectedTypes != null ) {
57              message.append( "expected " );
58  
59              if (this.expectedTypes.length == 1) {
60                  message.append( Types.getDescription(this.expectedTypes[0]) );
61              }
62              else {
63                  message.append("one of { ");
64      
65                  for (int i = 0; i < expectedTypes.length; ++i) {
66                      message.append( Types.getDescription(this.expectedTypes[i]) );
67      
68                      if ((i + 1) < expectedTypes.length) {
69                          if( expectedTypes.length > 2 ) {
70                              message.append(", ");
71                          }
72                          else {
73                              message.append(" ");
74                          }
75                      }
76  
77                      if ((i + 2) == expectedTypes.length) {
78                          message.append("or ");
79                      }
80                  }
81      
82                  message.append(" }");
83              }
84  
85              message.append( "; found '" );
86          }
87          else {
88              message.append( "could not use '" );
89          }
90  
91          message.append( getUnexpectedTokenText() ).append( "'" );
92          if( unexpectedToken != null ) {
93              message.append(" at " + unexpectedToken.getStartLine() + ":" + unexpectedToken.getStartColumn());
94          }
95          else {
96              message.append(" at unknown location (probably end of file)");
97          }
98  
99          if( comment != null ) {
100             message.append( "; " );
101             message.append( comment );
102         }
103 
104         return message.toString();
105     }
106 }