View Javadoc
1   /*
2    * Copyright (c) 2009 QOS.ch All rights reserved.
3    * 
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   * 
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   * 
14   * THE SOFTWARE IS PROVIDED "AS  IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  package ch.qos.cal10n.util;
23  
24  import java.io.Reader;
25  import java.util.List;
26  import java.util.Map;
27  
28  
29  /**
30   * 
31   * @author Ceki Gülcü
32   *
33   */
34  import ch.qos.cal10n.util.Token.TokenType;
35  
36  // Given tokens k,s,v, \, and EOL
37  // ^ denoting the null token
38  // here is the grammar
39  
40  // E = EOL | k s V |  E | ^
41  // V = v EOL | v '\' Vopt
42  // Vopt = EOL V
43  
44  public class Parser {
45  
46    final Reader reader;
47    final Map<String, String> map;
48  
49    List<Token> tokenList;
50    int pointer = 0;
51  
52    Parser(Reader r, Map<String, String> map) {
53      this.reader = r;
54      this.map = map;
55      TokenStream ts = new TokenStream(reader);
56      tokenList = ts.tokenize();
57    }
58  
59    void parseAndPopulate() {
60      E();
61    }
62  
63    private void E() {
64      Token t = getNextToken();
65      if (t == null) {
66        // we are done
67        return;
68      }
69      switch (t.tokenType) {
70      case EOL:
71        break;
72      case KEY:
73        String k = t.getValue();
74        t = getNextToken();
75        if (t.tokenType != TokenType.SEPARATOR) {
76          throw new IllegalStateException("Unexpected token " + t);
77        }
78        StringBuilder buf = new StringBuilder();
79        V(buf);
80        map.put(k, buf.toString());
81        break;
82      }
83      E();
84    }
85  
86    // V = v EOL | v '\' Vopt
87    // Vopt = EOL V
88    private void V(StringBuilder buf) {
89  
90      Token t = getNextToken();
91      if (t.tokenType != TokenType.VALUE) {
92        throw new IllegalStateException("Unexpected token " + t);
93      }
94      buf.append(t.getValue());
95  
96      t = getNextToken();
97      if (t.tokenType == TokenType.EOL) {
98        return;
99      } else if (t.tokenType == TokenType.TRAILING_BACKSLASH) {
100       Vopt(buf);
101 
102     }
103   }
104 
105   private void Vopt(StringBuilder buf) {
106     Token t = getNextToken();
107     if (t.tokenType != TokenType.EOL) {
108       throw new IllegalStateException("Unexpected token " + t);
109     }
110     V(buf);
111   }
112 
113   private Token getNextToken() {
114     if (pointer < tokenList.size()) {
115       return (Token) tokenList.get(pointer++);
116     }
117     return null;
118   }
119 
120 //  private Token getCurentToken() {
121 //    if (pointer < tokenList.size()) {
122 //      return (Token) tokenList.get(pointer);
123 //    }
124 //    return null;
125 //  }
126 //
127 //  private void advanceTokenPointer() {
128 //    pointer++;
129 //  }
130 }