View Javadoc

1   /*
2    * Copyright (C) 2006 uguu@users.sourceforge.jp, All Rights Reserved.
3    *
4    * Redistribution and use in source and binary forms, with or without
5    * modification, are permitted provided that the following conditions
6    * are met:
7    *
8    *    1. Redistributions of source code must retain the above copyright
9    *       notice, this list of conditions and the following disclaimer.
10   *
11   *    2. Redistributions in binary form must reproduce the above copyright
12   *       notice, this list of conditions and the following disclaimer in the
13   *       documentation and/or other materials provided with the distribution.
14   *
15   *    3. Neither the name of Clarkware Consulting, Inc. nor the names of its
16   *       contributors may be used to endorse or promote products derived
17   *       from this software without prior written permission. For written
18   *       permission, please contact clarkware@clarkware.com.
19   *
20   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
21   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
22   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
23   * CLARKWARE CONSULTING OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24   * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
26   * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28   * NEGLIGENCE OR OTHERWISE) ARISING IN  ANY WAY OUT OF THE USE OF THIS SOFTWARE,
29   * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30   */
31  
32  package jp.sourceforge.rpn_computer;
33  
34  import jp.sourceforge.tokenizer.Token;
35  import jp.sourceforge.tokenizer.TokenInfo;
36  
37  /**
38   * <p>
39   * 構文解析に失敗したことを現す例外です。
40   * </p>
41   * 
42   * @author uguu@users.sourceforge.jp
43   */
44  public final class ParseException extends RuntimeException {
45  
46      private Token       token;
47  
48      private TokenInfo[] tokenInfos;
49  
50      /**
51       * <p>
52       * インスタンスを初期化します。期待したトークンとは違うトークンが出現したことを表すメッセージを構築します。
53       * </p>
54       * 
55       * @param token
56       *            出現したトークン。
57       * @param tokenInfos
58       *            期待したトークンの情報の配列。
59       */
60      public ParseException(Token token, TokenInfo[] tokenInfos) {
61          super(ParseException.buildMessage(token, tokenInfos));
62  
63          this.token = token;
64          this.tokenInfos = tokenInfos;
65      }
66  
67      private static String buildMessage(Token token, TokenInfo[] tokenInfos) {
68          String msg;
69          if (token != null) {
70              msg = token.getLine() + "行目、" + token.getColumn() + "列目でトークン\"" + token.getToken() + "\"が出現しました。正しくは、次のパターンのトークンを期待しています。{";
71          } else {
72              msg = "期待したトークンが出現しませんでした。次のパターンのトークンを期待しています。{";
73          }
74          for (int i = 0; i < tokenInfos.length; i++) {
75              if (i != 0) {
76                  msg += ", ";
77              }
78              msg += tokenInfos[i].getTokenPattern().pattern();
79          }
80          msg += "}";
81          return msg;
82      }
83  
84      /**
85       * <p>
86       * 出現したトークンを返します。
87       * </p>
88       * 
89       * @return 出現したトークン。
90       */
91      public Token getToken() {
92          return this.token;
93      }
94  
95      /**
96       * <p>
97       * 期待したトークンの配列を返します。
98       * </p>
99       * 
100      * @return 期待したトークンの配列。
101      */
102     public TokenInfo[] getTokenInfos() {
103         return this.tokenInfos;
104     }
105 }