parser.cpp
changeset 447 72afe12da1c8
parent 445 0796c5592f00
child 514 497fab7d1404
     1.1 --- a/parser.cpp	Tue Apr 10 13:22:51 2007 +0000
     1.2 +++ b/parser.cpp	Tue Apr 10 13:22:52 2007 +0000
     1.3 @@ -7,26 +7,39 @@
     1.4  
     1.5  Parser::Parser()
     1.6  {
     1.7 -	initCommand();
     1.8 +	initParser();
     1.9  }
    1.10  
    1.11 -void Parser::initCommand()
    1.12 +void Parser::initParser()
    1.13  {
    1.14 +	initAtom();
    1.15 +	current=-1;
    1.16 +}
    1.17 +
    1.18 +void Parser::initAtom()
    1.19 +{
    1.20 +	atom="";
    1.21  	com="";
    1.22  	paramList.clear();
    1.23  	resetError();
    1.24  }
    1.25  
    1.26 -void Parser::parseAtom (const QString &s)
    1.27 +void Parser::parseAtom (QString s)
    1.28  {
    1.29 -	initCommand();
    1.30 -	input=s;
    1.31 +	initAtom();
    1.32 +	atom=s;
    1.33  	QRegExp re;
    1.34  	int pos;
    1.35  
    1.36 +	// Strip WS at beginning
    1.37 +	re.setPattern ("\\w");
    1.38 +	re.setMinimal (true);
    1.39 +	pos=re.search (atom);
    1.40 +	if (pos>=0)
    1.41 +		s=s.right(s.length()-pos);
    1.42 +
    1.43  	// Get command
    1.44  	re.setPattern ("\\b(.*)(\\s|\\()");
    1.45 -	re.setMinimal (true);
    1.46  	pos=re.search (s);
    1.47  	if (pos>=0)
    1.48  		com=re.cap(1);
    1.49 @@ -70,12 +83,17 @@
    1.50  	}	
    1.51  }
    1.52  
    1.53 -QString Parser::command()
    1.54 +QString Parser::getAtom()
    1.55 +{
    1.56 +	return atom;
    1.57 +}
    1.58 +
    1.59 +QString Parser::getCommand()
    1.60  {
    1.61  	return com;
    1.62  }
    1.63  
    1.64 -QStringList Parser::parameters()
    1.65 +QStringList Parser::getParameters()
    1.66  {
    1.67  	return paramList;
    1.68  }
    1.69 @@ -240,9 +258,63 @@
    1.70  
    1.71  void Parser::runScript()
    1.72  {
    1.73 +	current=0;
    1.74  }	
    1.75  
    1.76 -bool Parser::scriptNextAtom()
    1.77 +bool Parser::next()
    1.78  {
    1.79 +	int start=current;
    1.80 +	if (current<0) runScript();
    1.81 +	if (current>=script.length()-1) return false;
    1.82 +
    1.83 +	bool inBracket=false;
    1.84 +	while (true)
    1.85 +	{
    1.86 +		//cout <<"current="<<current<< "   start="<<start<<"  length="<<script.length()<<endl;
    1.87 +
    1.88 +		// Check if we are inside a string
    1.89 +		if (script.at(current)=='"')
    1.90 +		{
    1.91 +			if (inBracket)
    1.92 +				inBracket=false;
    1.93 +			else	
    1.94 +				inBracket=true;
    1.95 +		}
    1.96 +
    1.97 +		// Check if we are in a comment
    1.98 +		if (!inBracket && script.at(current)=='#')
    1.99 +		{
   1.100 +			while (script.at(current)!='\n')
   1.101 +			{
   1.102 +				current++;
   1.103 +				if (current>=script.length()) 
   1.104 +					return false;
   1.105 +			}
   1.106 +			start=current;
   1.107 +		}
   1.108 +
   1.109 +		// Check for end of atom
   1.110 +		if (!inBracket && script.at(current)==';')
   1.111 +		{
   1.112 +			atom=script.mid(start,current-start);
   1.113 +			current++;
   1.114 +			return true;
   1.115 +		}
   1.116 +		
   1.117 +		// Check for end of script
   1.118 +		if (current==script.length() )
   1.119 +		{
   1.120 +			if (inBracket)
   1.121 +			{
   1.122 +				setError (Aborted,"Runaway string");
   1.123 +				return false;
   1.124 +			} else
   1.125 +			{
   1.126 +				atom=script.mid(start);
   1.127 +				return true;
   1.128 +			}
   1.129 +		}
   1.130 +		current++;
   1.131 +	}
   1.132  }	
   1.133