View Javadoc

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