org.ledyba.sora.parser.FortranParser.java Source code

Java tutorial

Introduction

Here is the source code for org.ledyba.sora.parser.FortranParser.java

Source

// $ANTLR 3.2 Sep 23, 2009 12:02:23 FortranParser.g 2009-10-15 15:39:31

/**
 * Copyright (c) 2005, 2006 Los Alamos National Security, LLC.  This
 * material was produced under U.S. Government contract DE-
 * AC52-06NA25396 for Los Alamos National Laboratory (LANL), which is
 * operated by the Los Alamos National Security, LLC (LANS) for the
 * U.S. Department of Energy. The U.S. Government has rights to use,
 * reproduce, and distribute this software. NEITHER THE GOVERNMENT NOR
 * LANS MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR ASSUMES ANY
 * LIABILITY FOR THE USE OF THIS SOFTWARE. If software is modified to
 * produce derivative works, such modified software should be clearly
 * marked, so as not to confuse it with the version available from
 * LANL.
 *
 * Additionally, this program and the accompanying materials are made
 * available under the terms of the Eclipse Public License v1.0 which
 * accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */

/**
 *
 * @author Craig E Rasmussen, Christopher D. Rickett, Bryan Rasmussen
 */

package org.ledyba.sora.parser;

import org.antlr.v4.runtime.Parser;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.TokenStream;

public abstract class FortranParser extends Parser {

    protected FortranParser(TokenStream input) {
        super(input);
    }

    public boolean isTPrefixSpec(int token) {
        if (token == FortranLexer.T_ELEMENTAL || token == FortranLexer.T_IMPURE || token == FortranLexer.T_MODULE
                || token == FortranLexer.T_PURE || token == FortranLexer.T_RECURSIVE) {
            return true;
        }
        return false;
    }

    /**
     * Check for include and end of file.  T_INCLUDE is not in the grammar
     * so this method must be called after every statement (and initially
     * at the beginning of program unit file).
     */
    public void checkForInclude() {

        // consume bare T_EOS
        while (_input.LA(1) == FortranLexer.T_EOS) {
            _input.consume();
        }

        if (_input.LA(1) == FortranLexer.T_INCLUDE) {
            String files[];
            _input.consume(); // consume T_INCLUDE

            // get include filename from token stream
            files = _input.LT(1).getText().split(":");
            _input.consume(); // consume T_INCLUDE_NAME

            // check for empty include file (no statements)
            if (_input.LA(1) == FortranLexer.T_EOF) {
                Token tk = _input.LT(1);
                _input.consume();

                files = tk.getText().split(":");
            }

            // include acts like a statement so need to see if another include follows
            checkForInclude();
        }

        else if (_input.LA(1) == FortranLexer.T_EOF) {
            Token tk = _input.LT(1);
            String[] files = tk.getText().split(":");
            _input.consume();
            // unwind T_EOFs for include files containing includes
            checkForInclude();
        }

    }

} // end FortranParser