Index: src/fortranscanner.l
===================================================================
--- src/fortranscanner.l	(revision 616)
+++ src/fortranscanner.l	(working copy)
@@ -112,6 +112,14 @@
 static ParserInterface *g_thisParser; 
 static const char *     inputString;
 static int		inputPosition;
+static QCString         inputStringPrepass; ///< Input string for prepass of line cont. '&'
+static unsigned int     inputPositionPrepass;
+static int              lineCountPrepass = 0;
+
+#define MAX_INCLUDE_DEPTH 10
+YY_BUFFER_STATE include_stack[MAX_INCLUDE_DEPTH];
+int include_stack_ptr = 0;
+
 static QFile            inputFile;
 static QCString		yyFileName;
 static int		yyLineNr     = 1 ;
@@ -163,6 +171,9 @@
 static QCString getFullName(Entry *e);
 static bool isTypeName(QCString name);
 static void resolveModuleProcedures(QList<Entry> &moduleProcedures, Entry *current_root);
+static int getAmpersandAtTheEnd(const char *buf, int length);
+static void pushBuffer(QCString &buffer);
+static void popBuffer();
 
 //-----------------------------------------------------------------------------
 #undef	YY_INPUT
@@ -226,6 +237,8 @@
 %x      UseOnly
 %x      ModuleProcedure
 
+%x      Prepass
+
  /** comment parsing states */
 %x	DocBlock
 %x	DocBackLine
@@ -235,10 +248,37 @@
 
  /*-----------------------------------------------------------------------------------*/
 
-<*>"&".*"\n"                            { if (YY_START == String) REJECT; // "&" is ignored in strings
-                                          yyLineNr++;}  /* line not finished -> read next line (text after "&" may be 
-                                                           comment and has to be ignored */
+<*>^.*"&"{BS}(!.*)?\n                   { // ampersand is somewhere in line
+                                          //fprintf(stderr, "---%s", yytext);
 
+                                          int index = getAmpersandAtTheEnd(yytext, yyleng);
+                                          if(index<0){ // ampersand is not line continuation
+                                             if(YY_START == Prepass) { // last line in "continuation"
+                                               inputStringPrepass+=(const char*)yytext;
+                                               pushBuffer(inputStringPrepass);
+                                               yy_pop_state();
+                                             } else { // simple line
+                                               REJECT;
+                                             }
+                                          } else { // line with continuation
+                                            inputStringPrepass+=(const char*)yytext;
+                                            lineCountPrepass ++;
+
+                                            // replace & with space and remove the following chars
+                                            int length = inputStringPrepass.length();
+                                            inputStringPrepass[length-yyleng+index] = ' ';
+                                            inputStringPrepass.truncate(length-yyleng+index+1);
+                                            if(YY_START != Prepass)
+					      yy_push_state(Prepass);
+                                          }
+                                        }
+
+<Prepass>^.*\n                          {
+                                          inputStringPrepass+=(const char*)yytext;
+                                          pushBuffer(inputStringPrepass);
+                                          yy_pop_state();
+                                        }
+
  /*------ ignore strings */ 
 <*>"\\\\"                               { /* ignore \\  */}
 <*>"\\\""|\\\'                          { /* ignore \" and \'  */}
@@ -534,7 +574,7 @@
                                         }
 <Variable>"\n"	                        { currentModifiers = SymbolModifiers();
                                           yy_pop_state(); // end variable deklaration list
-                                          yyLineNr++;
+                                          yyLineNr++; yyLineNr+=lineCountPrepass; lineCountPrepass=0;
                                           docBlock.resize(0);
                                         }
 
@@ -587,9 +627,9 @@
 				           //current->type not yet available
                                            QCString arglist= yytext;
 					   //cout << "3=========> got parameterlist " << yytext << endl;
-                                           yyLineNr+= arglist.contains('\n');
-					   static QRegExp re("&[^\n]*\n");
-                                           arglist = arglist.replace(re,"");
+                                           //yyLineNr+= arglist.contains('\n');
+					   //static QRegExp re("&[^\n]*\n");
+                                           //arglist = arglist.replace(re,"");
 					   //cout << "3=========> got parameterlist " << arglist << endl;
 					   current->args = arglist;
 					   current->args = removeRedundantWhiteSpace(current->args);
@@ -599,8 +639,8 @@
 					   BEGIN(SubprogBody);
  				       } 
 <Parameterlist>{NOARGS}                {   
-                                           yyLineNr++;
-					   //cout << "3=========> without parameterlist " <<endl;
+                                           yyLineNr++; yyLineNr+=lineCountPrepass; lineCountPrepass=0;
+					   //printf("3=========> without parameterlist \n");
  					   stringToArgumentList("", current->argList);
                                            addCurrentEntry();
 					   startScope(last_entry);
@@ -659,7 +699,7 @@
   					}
 <DocBlock>"\n"{BS}"!"(">"|"!"+)		{ // comment block (next line is also comment line)
 					  docBlock+="\n"; // \n is necessary for lists
-                                          yyLineNr++;
+                                          yyLineNr++; yyLineNr+=lineCountPrepass; lineCountPrepass=0;
   					}
 <DocBlock>"\n"        			{ // comment block ends at the end of this line
                                           //cout <<"3=========> comment block : "<< docBlock << endl;
@@ -671,19 +711,27 @@
  /*------------------------------------------------------------------------------------------------*/
 
 <*>"\n"                                 {
-                                          yyLineNr++;
+                                          yyLineNr++; yyLineNr+=lineCountPrepass; lineCountPrepass=0;
                                           //if (debugStr.stripWhiteSpace().length() > 0) cout << "ignored text: " << debugStr << " state: " <<YY_START << endl;
                                           debugStr="";
                                         }
 
+
  /*---- error: EOF in wrong state --------------------------------------------------------------------*/
- <SubprogBody,ModuleBody,String,StrIgnore,InterfaceBody><<EOF>> {
-                                          fprintf(stderr,"==== Error: EOF reached in wrong state (end missing)");
-					  scanner_abort();
-					  yyterminate();
+
+<*><<EOF>>                              {
+                                          if ( include_stack_ptr <= 0 ) {
+                                            if (YY_START!=INITIAL && YY_START!=Start) {
+                                              fprintf(stderr,"==== Error: EOF reached in wrong state (end missing)");
+                                              scanner_abort();
+                                            }
+                                            yyterminate();
+                                          } else {
+                                            popBuffer();
+                                          }
                                         }
  <*>.                 	                {
-                                          debugStr+=yytext;
+                                          //debugStr+=yytext;
                                         } // ignore remaining text 
 
  /**********************************************************************************/
@@ -692,6 +740,70 @@
 %%
 //----------------------------------------------------------------------------
 
+static int getAmpersandAtTheEnd(const char *buf, int length)
+{
+  // Avoid ampersands in string and comments
+  int parseState = Start;
+  char quoteSymbol = 0;
+  int ampIndex = -1;
+
+  for(int i=0; i<length && parseState!=Comment; i++)
+  {
+    if(parseState==String)
+    {
+      if(buf[i]=='\\') i++;
+      if(buf[i]==quoteSymbol)
+      {
+        parseState = Start;
+        quoteSymbol = 0;
+      }
+    } else // not in string
+    {
+      switch(buf[i])
+      {
+        case '\'':
+        case '"':
+          parseState = String;
+          quoteSymbol = buf[i];
+          break;
+        case '!':
+          parseState = Comment;
+          break;
+        case ' ':  // ignore whitespace
+        case '\t':
+        case '\n': // this may be at the end of line
+          break;
+        case '&':
+          ampIndex = i;
+          break;
+        default:
+          ampIndex = -1; // invalidate prev ampersand
+      }
+    }
+  }
+  return ampIndex;
+}
+
+static void pushBuffer(QCString& buffer)
+{
+  if ( include_stack_ptr >= MAX_INCLUDE_DEPTH )
+  {
+    fprintf( stderr, "Stack buffers nested too deeply" );
+    exit( 1 );
+  }
+  include_stack[include_stack_ptr++] = YY_CURRENT_BUFFER;
+  yy_switch_to_buffer(yy_scan_string(buffer));
+
+  //fprintf(stderr, "--POP--%s", (const char *)buffer);
+  buffer = NULL;
+}
+
+static void popBuffer() {
+  include_stack_ptr --;
+  yy_delete_buffer( YY_CURRENT_BUFFER );
+  yy_switch_to_buffer( include_stack[include_stack_ptr] );
+}
+
 /** used to copy entry to an interface module procedure */
 static void copyEntry(Entry *dest, Entry *src) 
 {
@@ -1231,6 +1344,8 @@
   defaultProtection = Public;
   inputString = fileBuf;
   inputPosition = 0;
+  inputStringPrepass = NULL;
+  inputPositionPrepass = 0;
 
   //anonCount     = 0;  // don't reset per file
   mtype         = Method;
