1 /* |
|
2 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 */ |
|
23 |
|
24 package shapegen; |
|
25 |
|
26 import java.util.ArrayList; |
|
27 import java.util.HashMap; |
|
28 import java.util.List; |
|
29 import java.util.Map; |
|
30 import java.io.IOException; |
|
31 import java.io.StringReader; |
|
32 |
|
33 import static java.lang.Character.isLetter; |
|
34 import static java.lang.Character.isUpperCase; |
|
35 import static java.lang.Character.isWhitespace; |
|
36 |
|
37 /** |
|
38 * Parse a type template definition string |
|
39 * |
|
40 * input :: classDef |
|
41 * classDef :: letter [ ( classDef* ) ] |
|
42 * |
|
43 * @author Robert Field |
|
44 */ |
|
45 public class TTParser extends StringReader { |
|
46 |
|
47 private Map<Character, TTNode> letterMap = new HashMap<>(); |
|
48 private char ch; |
|
49 |
|
50 private final String def; |
|
51 |
|
52 public TTParser(String s) { |
|
53 super(s); |
|
54 this.def = s; |
|
55 } |
|
56 |
|
57 private void advance() throws IOException { |
|
58 do { |
|
59 ch = (char)read(); |
|
60 } while (isWhitespace(ch)); |
|
61 } |
|
62 |
|
63 public TTNode parse() { |
|
64 try { |
|
65 advance(); |
|
66 return classDef(); |
|
67 } catch (IOException t) { |
|
68 throw new RuntimeException(t); |
|
69 } |
|
70 } |
|
71 |
|
72 private TTNode classDef() throws IOException { |
|
73 if (!isLetter(ch)) { |
|
74 if (ch == (char)-1) { |
|
75 throw new IOException("Unexpected end of type template in " + def); |
|
76 } else { |
|
77 throw new IOException("Unexpected character in type template: " + (Character)ch + " in " + def); |
|
78 } |
|
79 } |
|
80 char nodeCh = ch; |
|
81 TTNode node = letterMap.get(nodeCh); |
|
82 boolean canBeClass = isUpperCase(nodeCh); |
|
83 advance(); |
|
84 if (node == null) { |
|
85 List<TTNode> subtypes = new ArrayList<>(); |
|
86 if (ch == '(') { |
|
87 advance(); |
|
88 while (ch != ')') { |
|
89 subtypes.add(classDef()); |
|
90 } |
|
91 advance(); |
|
92 } |
|
93 node = new TTNode(subtypes, canBeClass); |
|
94 letterMap.put(nodeCh, node); |
|
95 } |
|
96 return node; |
|
97 } |
|
98 } |
|