1 /*
   2  * Copyright (c) 1998, 2020, 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.buildtools.jdwpgen;
  27 
  28 import java.util.*;
  29 import java.io.*;
  30 import java.lang.reflect.InvocationTargetException;
  31 
  32 class Parse {
  33 
  34     final StreamTokenizer izer;
  35     final Map<String, Node> kindMap = new HashMap<String, Node>();
  36 
  37     Parse(Reader reader) {
  38         izer = new StreamTokenizer(new BufferedReader(reader));
  39         izer.resetSyntax();
  40         izer.slashStarComments(true);
  41         izer.slashSlashComments(true);
  42         izer.wordChars((int)'a', (int)'z');
  43         izer.wordChars((int)'A', (int)'Z');
  44         izer.wordChars((int)'0', (int)'9');
  45         izer.wordChars((int)'_', (int)'_');
  46         izer.wordChars((int)'-', (int)'-');
  47         izer.wordChars((int)'.', (int)'.');
  48         izer.whitespaceChars(0, 32);
  49         izer.quoteChar('"');
  50         izer.quoteChar('\'');
  51 
  52         kindMap.put("CommandSet", new CommandSetNode());
  53         kindMap.put("Command", new CommandNode());
  54         kindMap.put("Out", new OutNode());
  55         kindMap.put("Reply", new ReplyNode());
  56         kindMap.put("ErrorSet", new ErrorSetNode());
  57         kindMap.put("Error", new ErrorNode());
  58         kindMap.put("Event", new EventNode());
  59         kindMap.put("Repeat", new RepeatNode());
  60         kindMap.put("Group", new GroupNode());
  61         kindMap.put("Select", new SelectNode());
  62         kindMap.put("Alt", new AltNode());
  63         kindMap.put("ConstantSet", new ConstantSetNode());
  64         kindMap.put("Constant", new ConstantNode());
  65         kindMap.put("int", new IntTypeNode());
  66         kindMap.put("long", new LongTypeNode());
  67         kindMap.put("boolean", new BooleanTypeNode());
  68         kindMap.put("object", new ObjectTypeNode());
  69         kindMap.put("threadObject", new ThreadObjectTypeNode());
  70         kindMap.put("threadGroupObject", new ThreadGroupObjectTypeNode());
  71         kindMap.put("arrayObject", new ArrayObjectTypeNode());
  72         kindMap.put("stringObject", new StringObjectTypeNode());
  73         kindMap.put("classLoaderObject", new ClassLoaderObjectTypeNode());
  74         kindMap.put("classObject", new ClassObjectTypeNode());
  75         kindMap.put("referenceType", new ReferenceTypeNode());
  76         kindMap.put("referenceTypeID", new ReferenceIDTypeNode());
  77         kindMap.put("classType", new ClassTypeNode());
  78         kindMap.put("interfaceType", new InterfaceTypeNode());
  79         kindMap.put("arrayType", new ArrayTypeNode());
  80         kindMap.put("method", new MethodTypeNode());
  81         kindMap.put("field", new FieldTypeNode());
  82         kindMap.put("frame", new FrameTypeNode());
  83         kindMap.put("string", new StringTypeNode());
  84         kindMap.put("moduleID", new ModuleTypeNode());
  85         kindMap.put("value", new ValueTypeNode());
  86         kindMap.put("byte", new ByteTypeNode());
  87         kindMap.put("location", new LocationTypeNode());
  88         kindMap.put("tagged-object", new TaggedObjectTypeNode());
  89         kindMap.put("referenceTypeID", new ReferenceIDTypeNode());
  90         kindMap.put("typed-sequence", new ArrayRegionTypeNode());
  91         kindMap.put("untagged-value", new UntaggedValueTypeNode());
  92     }
  93 
  94     RootNode items() throws IOException {
  95         List<Node> list = new ArrayList<Node>();
  96 
  97         while (izer.nextToken() != StreamTokenizer.TT_EOF) {
  98             izer.pushBack();
  99             list.add(item());
 100         }
 101         RootNode node =  new RootNode();
 102         node.set("Root", list, 1);
 103         return node;
 104     }
 105 
 106     Node item() throws IOException {
 107         switch (izer.nextToken()) {
 108             case StreamTokenizer.TT_EOF:
 109                 error("Unexpect end-of-file");
 110                 return null;
 111 
 112             case StreamTokenizer.TT_WORD: {
 113                 String name = izer.sval;
 114                 if (izer.nextToken() == '=') {
 115                     int ntok = izer.nextToken();
 116                     if (ntok == StreamTokenizer.TT_WORD) {
 117                         return new NameValueNode(name, izer.sval);
 118                     } else if (ntok == '\'') {
 119                         return new NameValueNode(name, izer.sval.charAt(0));
 120                     } else {
 121                         error("Expected value after: " + name + " =");
 122                         return null;
 123                     }
 124                 } else {
 125                     izer.pushBack();
 126                     return new NameNode(name);
 127                 }
 128             }
 129 
 130             case '"':
 131                 return new CommentNode(izer.sval);
 132 
 133             case '(': {
 134                 if (izer.nextToken() == StreamTokenizer.TT_WORD) {
 135                     String kind = izer.sval;
 136                     List<Node> list = new ArrayList<Node>();
 137 
 138                     while (izer.nextToken() != ')') {
 139                         izer.pushBack();
 140                         list.add(item());
 141                     }
 142                     Node proto = kindMap.get(kind);
 143                     if (proto == null) {
 144                         error("Invalid kind: " + kind);
 145                         return null;
 146                     } else {
 147                         try {
 148                             Node node = (Node)proto.getClass().getDeclaredConstructor().newInstance();
 149                             node.set(kind, list, izer.lineno());
 150                             return node;
 151                         } catch (InstantiationException exc) {
 152                             error(exc.toString());
 153                             return null;
 154                         } catch (NoSuchMethodException exc) {
 155                             error(exc.toString());
 156                             return null;
 157                         } catch (InvocationTargetException exc) {
 158                             error(exc.toString());
 159                             return null;
 160                         } catch (IllegalAccessException exc) {
 161                             error(exc.toString());
 162                             return null;
 163                         }
 164                     }
 165                 } else {
 166                     error("Expected kind identifier, got " + izer.ttype +
 167                           " : " + izer.sval);
 168                     return null;
 169                 }
 170             }
 171 
 172             default:
 173                 error("Unexpected character: '" + (char)izer.ttype + "'");
 174                 return null;
 175         }
 176     }
 177 
 178     void error(String errmsg) {
 179         System.err.println(Main.specSource + ":" + izer.lineno() +
 180                            ": " + errmsg);
 181         throw new RuntimeException("Error: " + errmsg);
 182     }
 183 }