1 /*
2  * Copyright (c) 2009, 2015, 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 /*
27  * This file is available under and governed by the GNU General Public
28  * License version 2 only, as published by the Free Software Foundation.
29  * However, the following notice accompanied the original version of this
30  * file:
31  *
32  * The MIT License
33  *
34  * Copyright (c) 2004-2015 Paul R. Holser, Jr.
35  *
36  * Permission is hereby granted, free of charge, to any person obtaining
37  * a copy of this software and associated documentation files (the
38  * "Software"), to deal in the Software without restriction, including
39  * without limitation the rights to use, copy, modify, merge, publish,
40  * distribute, sublicense, and/or sell copies of the Software, and to
41  * permit persons to whom the Software is furnished to do so, subject to
42  * the following conditions:
43  *
44  * The above copyright notice and this permission notice shall be
45  * included in all copies or substantial portions of the Software.
46  *
47  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
48  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
49  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
50  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
51  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
52  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
53  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
54  */
55 
56 package jdk.internal.joptsimple;
57 
58 import java.util.NoSuchElementException;
59 
60 import static jdk.internal.joptsimple.ParserRules.*;
61 
62 /**
63  * Tokenizes a short option specification string.
64  *
65  * @author <a href="mailto:pholser@alumni.rice.edu">Paul Holser</a>
66  */
67 class OptionSpecTokenizer {
68     private static final char POSIXLY_CORRECT_MARKER = '+';
69     private static final char HELP_MARKER = '*';
70 
71     private String specification;
72     private int index;
73 
OptionSpecTokenizer( String specification )74     OptionSpecTokenizer( String specification ) {
75         if ( specification == null )
76             throw new NullPointerException( "null option specification" );
77 
78         this.specification = specification;
79     }
80 
hasMore()81     boolean hasMore() {
82         return index < specification.length();
83     }
84 
next()85     AbstractOptionSpec<?> next() {
86         if ( !hasMore() )
87             throw new NoSuchElementException();
88 
89 
90         String optionCandidate = String.valueOf( specification.charAt( index ) );
91         index++;
92 
93         AbstractOptionSpec<?> spec;
94         if ( RESERVED_FOR_EXTENSIONS.equals( optionCandidate ) ) {
95             spec = handleReservedForExtensionsToken();
96 
97             if ( spec != null )
98                 return spec;
99         }
100 
101         ensureLegalOption( optionCandidate );
102 
103         if ( hasMore() ) {
104             boolean forHelp = false;
105             if ( specification.charAt( index ) == HELP_MARKER ) {
106                 forHelp = true;
107                 ++index;
108             }
109             spec = hasMore() && specification.charAt( index ) == ':'
110                 ? handleArgumentAcceptingOption( optionCandidate )
111                 : new NoArgumentOptionSpec( optionCandidate );
112             if ( forHelp )
113                 spec.forHelp();
114         } else
115             spec = new NoArgumentOptionSpec( optionCandidate );
116 
117         return spec;
118     }
119 
configure( OptionParser parser )120     void configure( OptionParser parser ) {
121         adjustForPosixlyCorrect( parser );
122 
123         while ( hasMore() )
124             parser.recognize( next() );
125     }
126 
adjustForPosixlyCorrect( OptionParser parser )127     private void adjustForPosixlyCorrect( OptionParser parser ) {
128         if ( POSIXLY_CORRECT_MARKER == specification.charAt( 0 ) ) {
129             parser.posixlyCorrect( true );
130             specification = specification.substring( 1 );
131         }
132     }
133 
handleReservedForExtensionsToken()134     private AbstractOptionSpec<?> handleReservedForExtensionsToken() {
135         if ( !hasMore() )
136             return new NoArgumentOptionSpec( RESERVED_FOR_EXTENSIONS );
137 
138         if ( specification.charAt( index ) == ';' ) {
139             ++index;
140             return new AlternativeLongOptionSpec();
141         }
142 
143         return null;
144     }
145 
handleArgumentAcceptingOption( String candidate )146     private AbstractOptionSpec<?> handleArgumentAcceptingOption( String candidate ) {
147         index++;
148 
149         if ( hasMore() && specification.charAt( index ) == ':' ) {
150             index++;
151             return new OptionalArgumentOptionSpec<String>( candidate );
152         }
153 
154         return new RequiredArgumentOptionSpec<String>( candidate );
155     }
156 }
157