001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.cnd.api.utils;
043:
044: import java.io.FileNotFoundException;
045: import java.util.ArrayList;
046:
047: /**
048: * Parse a Fortran file. Return complete Fortran statements, taking into consideration
049: * things like the source form, line length, continuation lines, and comments. This
050: * class works in conjunction with FortranParse to parse fortran files for mod and use
051: * statements.
052: */
053: public class FortranParser {
054:
055: private FortranReader in;
056:
057: private boolean verbose = false;
058:
059: /** Contstructor for this semi-parsing class */
060: public FortranParser(String file, String options, boolean verbose,
061: boolean verboseReader) {
062:
063: this .verbose = verbose;
064:
065: try {
066: in = new FortranReader(file, options, verboseReader);
067: } catch (FileNotFoundException ex) {
068: in = null;
069: }
070: }
071:
072: /** Contstructor for this semi-parsing class */
073: public FortranParser(String file, String options) {
074: this (file, options, false, false);
075: }
076:
077: /**
078: * This is a semi-parser. Kind of. It gets complete Fortran statements (stripped of
079: * comments and labels) and checks for 2 specific keywords, MODULE and USE. It
080: * creates a list containing each MODULE or USE name preceded by an 'M' or 'U'.
081: */
082: public ArrayList parser() {
083: ArrayList list = new ArrayList();
084: String line;
085: String arg;
086:
087: if (in == null) {
088: return null;
089: }
090:
091: try {
092: while ((line = in.getStatement()) != null) {
093: if (line.length() >= 15
094: && line.substring(0, 15).equalsIgnoreCase(
095: "moduleprocedure")) { // NOI18N
096: // ignore this keyword
097: } else if (line.length() >= 6
098: && line.substring(0, 6).equalsIgnoreCase(
099: "module")) { // NOI18N
100: arg = getName(line.substring(6));
101: list.add("M" + arg); // NOI18N
102: } else if (line.length() >= 3
103: && line.substring(0, 3).equalsIgnoreCase("use")) { // NOI18N
104: arg = getName(line.substring(3));
105: list.add("U" + arg); // NOI18N
106: }
107: }
108: } catch (UnexpectedEOFException ex) {
109: if (verbose) {
110: System.err.println("Error: Unexpected EOF"); // NOI18N
111: ex.printStackTrace();
112: }
113: return null;
114: }
115:
116: return list;
117: }
118:
119: /** Get the name following a MODULE or USE keyword */
120: private String getName(String arg) {
121: StringBuffer buf = new StringBuffer();
122: String arg2 = arg.trim();
123:
124: for (int i = 0; i < arg2.length(); i++) {
125: char c = arg2.charAt(i);
126:
127: if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
128: || (c >= '0' && c <= '9') || c == '_') {
129: buf.append(c);
130: } else {
131: break;
132: }
133: }
134: return buf.toString();
135: }
136:
137: /** This main method is used for testing the class */
138: public static void main(String[] args) {
139: FortranParser parser = null;
140: boolean verbose = false;
141: String file = null;
142:
143: for (int i = 0; i < args.length; i++) {
144: if (args[i].equals("-v")) { // NOI18N
145: verbose = true;
146: } else {
147: file = args[i];
148: }
149: }
150:
151: parser = new FortranParser(file, null);
152: parser.verbose = verbose;
153:
154: ArrayList list = parser.parser();
155:
156: System.out.println("FortranParser: list has " + list.size()
157: + " elements");// NOI18N
158: for (int i = 0; i < list.size(); i++) {
159: String stmnt = list.get(i).toString();
160: if (stmnt.charAt(0) == 'M') {
161: System.out.println("\tModule " + stmnt.substring(1)); // NOI18N
162: } else {
163: System.out.println("\tUse " + stmnt.substring(1)); // NOI18N
164: }
165: }
166: }
167: }
|