📄 lexicalanalyser.java
字号:
} // end loop invisible coee
}
return foundToken; // nothing found
}
/**Check if the next token is a whitespace
@return true if it is
*/
private boolean handleSpace(char nextChar)
{
boolean foundToken = false;
if( (nextChar == ' ' ) ||
//(nextChar == '\n') ||
(nextChar == '\r') ||
(nextChar == '\t') )
{
// whitespaces ' ', '\n' and '\r' are also treated as tokens if they are used
// inside of matrices (e.g.: a=[1 2 \n 3 4] --> a=[1,2;3,4])
if ( parseWhitespaceSwitch)
{
char p = getPreviousChar(); // save previous char
char t = nextChar; // might be ' ', '\r' or '\n'
// remove multiple spaces
while (inspectNextChar() == ' ')
{
char n = getNextChar();
}
// check if '\n' or '\r' are superseeding ' '
if ((inspectNextChar() == '\n') ||
(inspectNextChar() == '\r') )
{
t = getNextChar();
t = '\n';
//ErrorLogger.debugLine("LexAna: whitespace return");
}
// If there is a previous delimiter or trailing delimiter a
// whitespace is just some empty code (e.g. a=[1, 2] or a=[1 ,2] or
// a=[1,2 ] )
// Otherwise the whitespace is a delimiter (e.g. a=[1 2])
char n = inspectNextChar();
if ((p != 0 ) &&
(p != ',') && (p != ';') &&
(p != '\n') && (p != '\r') &&
(n != ',') && (n != ';') && (n != ']') )
{
nextToken = new DelimiterToken(t);
//ErrorLogger.debugLine("LexAna: whitespace delimiter");
}
else
invisibleCode = true;
}
else
{
invisibleCode = true;
}
foundToken = true;
}
return foundToken;
}
/**Check if the next token is a comment
@return true if it is
*/
private boolean handleComment(char nextChar)
{
boolean foundToken = false;
char nextNextChar = inspectNextChar();
if((nextChar == '#' ) ||
(nextChar == '%' ) ||
(nextChar == '/' && nextNextChar == '/'))
{
//comment for the rest of the line
// # this is a comment ...
// % this is a comment ...
// // this is a comment
while((inspectNextChar() != '\n') && (!EOChars()))
{
advance();
}
// remove trailing \n
// e.g. # some comment \n
if (inspectNextChar() == '\n')
advance();
invisibleCode = true;
foundToken = true;
}
else if(nextChar == '/' && nextNextChar == '*')
{
// /* */ style comment
boolean endComment = false;
while(!endComment)
{
if(getNextChar() == '*')
{
if(getNextChar() == '/')
{
endComment = true;
advance();
}
}
else if(EOChars())
endComment = true;
}
invisibleCode = true;
foundToken = true;
}
return foundToken;
}
/**Check if the next token is a number
@return true if it is
*/
private boolean handleNumber(char nextChar)
{
boolean foundToken = false;
// e.g. 50000
// e.g. .6789
if((numberChars.indexOf(nextChar) != -1) ||
(nextChar == '.' && (numberChars.indexOf(inspectNextChar()) != -1)))
{
StringBuffer sBuffer = new StringBuffer();
//token is a number (e.g. 123 or 123.456 or 1234.345e+5)
sBuffer.append(nextChar);
// analyse number left of the point (e.g. >123<.456e+5)
while(numberChars.indexOf(inspectNextChar()) != -1)
{
sBuffer.append(getNextChar());
}
// look for a "." (e.g. 3.4)
if (inspectNextChar() == '.')
{
getNextChar();
if(numberChars.indexOf(inspectNextChar()) > -1)
{
sBuffer.append('.'); // append '.'
// look for numbers on the right side of '.' (e.g. 123.>456<e+5)
while(numberChars.indexOf(inspectNextChar()) != -1)
{
sBuffer.append(getNextChar());
}
}
else
{
backTrack();
}
}
// check for "e" or "E" (e.g. 22.33e5)
if ( (inspectNextChar() == 'e')
|| (inspectNextChar() == 'E') )
{
sBuffer.append(getNextChar()); // append 'e' or 'E'
// check for sign (e.g. 22.33e+5 or 22.33e-5)
if ( (inspectNextChar() == '+')
|| (inspectNextChar() == '-') )
{
sBuffer.append(getNextChar()); // append '+' or '-'
}
// check for exponent
while(numberChars.indexOf(inspectNextChar()) != -1)
{
sBuffer.append(getNextChar());
}
}
// convert buffer to string and check for minus sign
String number = sBuffer.toString();
if (negative)
number = "-" + number;
// real or imaginary number
// check for "i" or "j" to indicate imaginary number
if ( (inspectNextChar() == 'i')
|| (inspectNextChar() == 'j') )
{
// imaginary number
advance();
nextToken = new DoubleNumberToken("0", number);
}
else
{
// real number
nextToken = new DoubleNumberToken(number, null);
}
//ErrorLogger.debugLine("LexAna: number = "+nextToken.toString());
foundToken = true;
}
return foundToken;
}
/**Check if the next token is a string
@return true if it is
*/
private boolean handleString(char nextChar)
{
boolean foundToken = false;
if(nextChar == '\"' || nextChar == '\'')
{
if(nextChar == '\'')
{
char prevChar = getPreviousChar();
// e.g. [1,2]' or variable'
if ( (prevChar == ']')
|| (prevChar == ')')
|| (textChars.indexOf(prevChar) != -1) )
{
return false;
}
}
//token is a string
char endChar = nextChar;
StringBuffer sBuffer = new StringBuffer();
nextChar = getNextChar();
while((nextChar != endChar) && (!EOChars()))
{
sBuffer.append( nextChar );
nextChar = getNextChar();
}
//ErrorLogger.debugLine("LexAna: String = "+sBuffer.toString());
nextToken = new CharToken(sBuffer.toString());
foundToken = true;
}
return foundToken;
}
/**Check if the next token is a text item
@return true if it is */
private boolean handleText(char nextChar)
{
boolean foundToken = false;
if(textChars.indexOf(nextChar) != -1)
{
foundToken = true;
//token is either a command, function or variable
StringBuffer sBuffer = new StringBuffer();
sBuffer.append(nextChar);
while((textChars.indexOf(inspectNextChar()) != -1) )
{
sBuffer.append( getNextChar() );
}
String name = sBuffer.toString();
if( name.equals("i") ||
name.equals("j") )
{
//ErrorLogger.debugLine("LexAna: ImaginaryNumber = "+name);
int value = 1;
if(negative)
value = -1;
nextToken = new DoubleNumberToken(0,value);
}
else if(name.equals("Inf") || name.equals("inf"))
{
// positive and negative infinity
if(negative)
nextToken = new DoubleNumberToken(Double.NEGATIVE_INFINITY);
else
nextToken = new DoubleNumberToken(Double.POSITIVE_INFINITY);
}
else if(name.equals("NaN"))
{
// not a number (e.g. 0/0 or Inf/Inf)
nextToken = new DoubleNumberToken(Double.NaN);
}
else if(delimiterWords.indexOf(" " + name + " ") != -1)
{
nextToken = new DelimiterToken(name);
//ErrorLogger.debugLine("LexAna: found reserved delimiter. word "+name);
}
else if(reservedWords.indexOf(" "+name+" ") != -1)
{
nextToken = new FunctionToken(name);
//ErrorLogger.debugLine("LexAna: found reserved func. word "+name);
}
else if(inspectNextChar() == '(')
{
//text is a function
//ErrorLogger.debugLine("LexAna: Function = "+name);
nextToken = new FunctionToken(name);
}
else
{
nextToken = new VariableToken(name);
//ErrorLogger.debugLine("LexAna: Variable ="+name);
}
}
return foundToken;
}
/**Check if the next token is an operator
@return true if it is */
private boolean handleOperator(char nextChar)
{
boolean foundToken = false;
if ((nextChar == '+') ||
(nextChar == '-') )
{
foundToken = true;
// after ,;([{ a minus sign indicates a negative number
boolean lastDelim = false;
if (lastToken instanceof DelimiterToken)
{
DelimiterToken delimiter = (DelimiterToken)lastToken;
if (delimiter.value==',' ||
delimiter.value==';' ||
delimiter.value==':' ||
delimiter.value=='[' ||
delimiter.value=='{' ||
delimiter.value=='(' )
lastDelim = true;
}
// After an operator token a minus/plus sign indicates
// a negative/positive number (e.g. 3* -4 or 3* +4 or 3+ +4 or 3+ -4)
if (lastToken instanceof BinaryOperatorToken)
lastDelim = true;
// maybe '-' or '+' is first character of character-array
if (lastToken == null)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -