View Javadoc

1   ////////////////////////////////////////////////////////////////////////////////
2   // MillScript: an Open Spice interpreter and batch website creation tool
3   // Copyright (C) 2001-2004 Open World Ltd
4   // Copyright (C) 2005 Kevin Rogers
5   //
6   // This file is part of MillScript.
7   //
8   // MillScript is free software; you can redistribute it and/or modify it under
9   // the terms of the GNU General Public License as published by the Free
10  // Software Foundation; either version 2 of the License, or (at your option)
11  // any later version.
12  //
13  // MillScript is distributed in the hope that it will be useful, but WITHOUT
14  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
16  // more details.
17  //
18  // You should have received a copy of the GNU General Public License along with
19  // MillScript; if not, write to the Free Software Foundation, Inc., 59 Temple
20  // Place, Suite 330, Boston, MA  02111-1307  USA
21  ////////////////////////////////////////////////////////////////////////////////
22  package org.millscript.millscript.syntax;
23  
24  import org.millscript.millscript.alert.Alerts;
25  
26  /**
27   * This class provides support for parsing MillScript package names. As such it
28   * is a convenience class, to reduce the work required to implement syntax that
29   * must parse package names.
30   */
31  public abstract class PkgReaderSyntax extends PrefixSyntax {
32  
33      /**
34       * Parse a package name using the specified parser. The package name is made
35       * up of names separated by periods, any other symbols are an error.
36       *
37       * @param   parser  Parser to parse from, i.e. the source.
38       * @return  a string containing the full package name
39       */
40      public String readPackageName( final Parser parser ) {
41          // Read the first part of the package name
42          final StringBuffer s = new StringBuffer( parser.readSymbol() );
43          // Now try to read a period
44          while ( parser.tryRead( "." ) ) {
45              // ok, we've got a long package name, so append the period and read
46              // the next part of the package name
47              s.append( "." );
48              s.append( parser.readSymbol() );
49          }
50          // We've finished reading the package name, if the next symbol is
51          // anything other than a semi-colon or equals, generate a mishap.
52          if ( !parser.peekRead( ";" ) && !parser.peekRead( "=" ) ) {
53              throw(
54                  Alerts.parse(
55                      "Unexpected component in package name",
56                      null
57                  ).culprit( "component", parser.getErrorString() ).mishap()
58              );
59          }
60          // Return the package name
61          return s.toString();
62      }
63  
64      /**
65       * Returns the nickname for the specified package. The nickname is that part
66       * of the package name following the last period, or the whole name if there
67       * are no periods.
68       *
69       * @param   s   a string containing a package name
70       * @return  a string containing the nickname for the specified package name
71       */
72      public String nickname( final String s ) {
73          // find the index of the last period
74          int n = s.lastIndexOf( '.' );
75          // If there is no period, the nickname is the same as the supplied
76          // package name
77          if ( n == -1 ) {
78              return s;
79          }
80          // Otherwise we return the part of the specified package name following
81          // the last period.
82          return s.substring( n + 1 );
83      }
84  
85  }