|
What this is
This file is included in the DevDaily.com
"Java Source Code
Warehouse" project. The intent of this project is to help you "Learn
Java by Example" TM.
Other links
The source code
/*
* FortranTokenMarker.java - Fortran token marker
* by Carl Smotricz
* carl@smotricz.com
* www.smotricz.com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package org.gjt.sp.jedit.syntax;
import javax.swing.text.Segment;
/**
* Custom TokenMarker for UNISYS's ASCII FORTRAN 77.
* Characteristics of this dialect are:
* - Fixed column format, with
* - comment character ( 'C'|'c'|'*' ) in column 1,
* - labels (numeric) in column 1-5,
* - continuation character ( any nonblank ) in column 6,
* - logical end of line after column 72.
*
* - Nonstandard block comment character ( '@' ) in any column,
* - Some nonstandard functions:
BITS , BOOL ,
* INDEX , TRMLEN
*
* It should be easy enough to adapt this class for minor variations
* in the dialect so long as the format is the classic fixed column format.
* As this scanner is highly optimized for the fixed column format, it
* is probably not readily adaptable for freeform FORTRAN code.
*/
public class FortranTokenMarker extends TokenMarker
{
// private members
private final static int MAYBE_KEYWORD_FIRST = Token.INTERNAL_FIRST;
private final static int MAYBE_KEYWORD_MORE = 1 + MAYBE_KEYWORD_FIRST;
private final static String S_E_P = "START EDIT PAGE";
private static KeywordMap fortranKeywords;
private KeywordMap keywords;
private int lastOffset;
/**
* Constructor, with a wee bit of initialization.
*/
public FortranTokenMarker()
{
this.keywords = getKeywords();
}
/**
* Implementation of code to mark tokens.
*/
public byte markTokensImpl(byte token, Segment line, int lineIndex)
{
byte lastLineToken = token;
// --- Very quick check for empty line
if (line.count < 1) return lastLineToken; // EXIT METHOD!
char[] array = line.array;
int offset = line.offset;
char c = array[offset];
// --- Very quick check for 'C' comment line
if (c == 'C' || c == 'c' || c == '*')
{
addToken(line.count, Token.COMMENT1);
return lastLineToken; // EXIT METHOD!
}
token = Token.NULL; // context usually ends on line boundary
int lineEnd = offset + line.count;
// --- Check for a label
int limit = Math.min(lineEnd, offset + 5);
int i;
for (i=offset; i 0)
{
byte id = keywords.lookup(line, lastOffset, len);
addToken(len, id);
lastOffset = keywordEnd;
}
}
/**
* Call addToken only if the length of the token is not 0.
*/
private void guardedAddToken(int len, byte token)
{
if (len > 0) addToken(len, token);
}
/**
* Return the keyword map.
* It's lazily initialized on the first call.
*/
public static KeywordMap getKeywords()
{
if (fortranKeywords == null)
{
fortranKeywords = new KeywordMap(false);
// === Commands ===
fortranKeywords.add("CALL", Token.KEYWORD1);
fortranKeywords.add("CLOSE", Token.KEYWORD1);
fortranKeywords.add("CONTINUE", Token.KEYWORD1);
fortranKeywords.add("DO", Token.KEYWORD1);
fortranKeywords.add("ELSE", Token.KEYWORD1);
fortranKeywords.add("ELSEIF", Token.KEYWORD1);
fortranKeywords.add("ENDIF", Token.KEYWORD1);
fortranKeywords.add("GOTO", Token.KEYWORD1);
fortranKeywords.add("GO TO", Token.KEYWORD1);
fortranKeywords.add("IF", Token.KEYWORD1);
fortranKeywords.add("INDEX", Token.KEYWORD1);
fortranKeywords.add("INQUIRE", Token.KEYWORD1);
fortranKeywords.add("OPEN", Token.KEYWORD1);
fortranKeywords.add("PRINT", Token.KEYWORD1);
fortranKeywords.add("READ", Token.KEYWORD1);
fortranKeywords.add("RETURN", Token.KEYWORD1);
fortranKeywords.add("THEN", Token.KEYWORD1);
fortranKeywords.add("WRITE", Token.KEYWORD1);
// === Compiler directives ===
fortranKeywords.add("BLOCK DATA", Token.KEYWORD2);
fortranKeywords.add("COMPILER", Token.KEYWORD2);
fortranKeywords.add("END", Token.KEYWORD2);
fortranKeywords.add("ENTRY", Token.KEYWORD2);
fortranKeywords.add("FUNCTION", Token.KEYWORD2);
fortranKeywords.add("INCLUDE", Token.KEYWORD2);
fortranKeywords.add("SUBROUTINE", Token.KEYWORD2);
// === Data types (etc.) ===
fortranKeywords.add("CHARACTER", Token.KEYWORD3);
fortranKeywords.add("DATA", Token.KEYWORD3);
fortranKeywords.add("DEFINE", Token.KEYWORD3);
fortranKeywords.add("EQUIVALENCE", Token.KEYWORD3);
fortranKeywords.add("IMPLICIT", Token.KEYWORD3);
fortranKeywords.add("INTEGER", Token.KEYWORD3);
fortranKeywords.add("LOGICAL", Token.KEYWORD3);
fortranKeywords.add("PARAMETER", Token.KEYWORD3);
fortranKeywords.add("REAL", Token.KEYWORD3);
// === Operators ===
fortranKeywords.add(".AND.", Token.OPERATOR);
fortranKeywords.add(".EQ.", Token.OPERATOR);
fortranKeywords.add(".NE.", Token.OPERATOR);
fortranKeywords.add(".NOT.", Token.OPERATOR);
fortranKeywords.add(".OR.", Token.OPERATOR);
fortranKeywords.add("+", Token.OPERATOR);
fortranKeywords.add("-", Token.OPERATOR);
fortranKeywords.add("*", Token.OPERATOR);
fortranKeywords.add("**", Token.OPERATOR);
fortranKeywords.add("/", Token.OPERATOR);
// === Literals ===
fortranKeywords.add(".FALSE.", Token.LITERAL2);
fortranKeywords.add(".TRUE.", Token.LITERAL2);
}
return fortranKeywords;
}
}
// End of FortranTokenMarker.java
|