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