/* FormatParser.java */ /* This parser parses Fortran format strings. */ options { STATIC = true; DEBUG_PARSER = false; DEBUG_TOKEN_MANAGER = false; DEBUG_LOOKAHEAD = false; } PARSER_BEGIN(FormatParser) package org.j_paine.formatter; class FormatParser { } PARSER_END(FormatParser) SKIP : { <(" ")+> } TOKEN : { } // An unsigned integer, for repetition factors, field widths, etc. // previously: TOKEN : { } // A string literal inside a format. We haven't implemented // embedded quotes yet. TOKEN : { < A_DESC : "A" | "a" > } TOKEN : { < P_DESC : "P" | "p" > } TOKEN : { < X_DESC : "X" | "x" > } TOKEN : { < I_DESC : "I" | "i" > } TOKEN : { < F_DESC : "F" | "f" > } TOKEN : { < D_DESC : "D" | "d" > } TOKEN : { < E_DESC : "E" | "e" > } TOKEN : { < G_DESC : "G" | "g" > } TOKEN : { < L_DESC : "L" | "l" > } int Integer(): { Token t; } { t= { return (Integer.valueOf(t.image)).intValue(); } } /* I split FormatIOElement into FormatIOElementFloat and * FormatIOElementNonFloat because a floating point edit * descriptor (F, E, D, or G) may follow a P edit descriptor * without a comma. --kgs */ FormatElement FormatIOElementFloat(): { FormatElement fe; int w, d, m; w = d = m = -1; } { // for Iw.m, ignore the .m value /* added D and G edit descriptors, but just use the E implementation. --kgs */ ( w=Integer() "." d=Integer() { fe=new FormatF(w,d); } | w=Integer() "." d=Integer() { fe=new FormatE(w,d); } | w=Integer() "." d=Integer() { fe=new FormatE(w,d); } | w=Integer() "." d=Integer() { fe=new FormatE(w,d); } ) { return fe; } } FormatElement FormatIOElementNonFloat(): { FormatElement fe; int w, d, m; w = d = m = -1; } { // for Iw.m, ignore the .m value /* added L edit descriptor. --kgs */ ( [w=Integer()] { fe=new FormatA(w); } | w=Integer() ["." m=Integer()] { fe=new FormatI(w); } | w=Integer() { fe=new FormatL(w); } ) { return fe; } } // This represents a format element that transfers one // data item. FormatElement FormatNonIOElement(): {} { { return new FormatX(); } } // This represents a format element that doesn't transfer // any data items. FormatElement FormatElement(): { FormatElement fe; } { ( fe=FormatIOElementFloat() | fe=FormatIOElementNonFloat() | fe=FormatNonIOElement() | fe=FormatScale() ) { return fe; } } FormatElement FormatScale(): { FormatElement fe = null; int r=1; } { /* Commas may be omitted between a P edit descriptor and an * immediately following F, E, D, or G edit descriptor (13.5.9). * --kgs */ [ [r=Integer()] (fe=FormatIOElementFloat()) ] { return new FormatP(r, fe); } } FormatSlash FormatSlash(): {} { "/" { return new FormatSlash(); } } // These are a special case. Unlike other format elements, // Fortran permits several slashes to be concatenated without // commas to separate them, and you can't use a repetition // factor on them. FormatString FormatString(): { Token t; String s; } { ( t= ) { s = t.image; s = s.substring(1,s.length()-1); // Remove the quotes. return new FormatString(s); } } // Another special case that can't be repeated, and can be // concatenated to other elements without commas. void OptionalFormatSlashesOrStrings( Format f ): { FormatUniv fs; } { ( (fs=FormatSlash() | fs=FormatString()) { f.addElement(fs); } )* } FormatRepeatedItem FormatRepeatedItem(): { int r=1; FormatUniv fu; } { [ r=Integer() ] ( "(" fu=Format() ")" | fu=FormatElement() ) { /* here we check whether the parsed format element is a P edit * descriptor. in that case, it may have parsed a floating point * edit descriptor along with it (if it followed without a comma) * so return that element here. --kgs */ if(fu instanceof FormatP) { FormatRepeatedItem ritem; ritem = ((FormatP)fu).getRepeatedItem(); if(ritem != null) return ritem; else return new FormatRepeatedItem( r, fu ); } else return new FormatRepeatedItem( r, fu ); } } void FormatGroup( Format f ): { FormatRepeatedItem fri; } { ( OptionalFormatSlashesOrStrings( f ) [ fri = FormatRepeatedItem() { if(fri != null) f.addElement(fri); } OptionalFormatSlashesOrStrings( f ) ] ) } // This rather messy syntax allows us to have slashes and/or // strings either side of a format element or repeated group // without needing to separate them from each other or the element // with commas. // It also means that we can have empty format groups and format // groups that don't transfer any data elements. So for example, // the format ,/, is valid under this grammar. Format Format(): { FormatRepeatedItem fri; Format f = new Format(); } { ( FormatGroup(f) ) ( "," ( FormatGroup(f) ) )* { return f; } }