📄 lexicalanalyser.java
字号:
lastDelim = true;
if (lastDelim)
{
// minus sign indicates a negative number
if(nextChar == '-')
negative = true; // e.g. -4444
else
negative = false; // e.g. +4444
// +++++-+-+-8
while (inspectNextChar()=='-' || inspectNextChar()=='+')
{
// e.g. +- then change sign
if (inspectNextChar()=='-')
negative = !negative;
advance();
}
// e.g. if other than number return sign (plus/minus)
// e.g. -* -hello +foo +(3+4)
if (numberChars.indexOf(inspectNextChar())==-1 )
{
nextToken=new AddSubOperatorToken(nextChar);
return true;
}
invisibleCode = true;
return true;
}
else
{
// check for increment/decrement ++ or --
if (inspectNextChar() == nextChar)
{
//++ or --
advance();
//ErrorLogger.debugLine("LexAna: Increment/Decrement "+nextChar);
nextToken = new UnaryOperatorToken(nextChar); //, bracketLevel);
}
else
{
//ErrorLogger.debugLine("LexAna: Add/Sub = "+nextChar);
nextToken = new AddSubOperatorToken(nextChar); //, bracketLevel);
}
}
}
else if(nextChar == '/' )
{
foundToken = true;
//token is division "/", comment /* */ or //
if(inspectNextChar() == '*' || inspectNextChar() == '/')
{
return false;
}
else
{
//ErrorLogger.debugLine("LexAna: MulDiv = "+nextChar);
nextToken = new MulDivOperatorToken('/'); // bracketLevel);
}
}
else if(nextChar == '\'')
{
char prevChar = getPreviousChar();
if(prevChar == ']' ||
prevChar == ')' ||
textChars.indexOf(prevChar) > -1)
{
foundToken = true;
//ErrorLogger.debugLine("LexAna: Transpose");
nextToken = new UnaryOperatorToken(nextChar);
}
}
else if(nextChar == '*')
{
foundToken = true;
//ErrorLogger.debugLine("LexAna: MulDiv = "+nextChar);
nextToken = new MulDivOperatorToken('*');
}
else if(nextChar == '\\')
{
foundToken = true;
//ErrorLogger.debugLine("LexAna: left division = "+nextChar);
nextToken = new MulDivOperatorToken('L');
}
else if(nextChar == '^')
{
foundToken = true;
//ErrorLogger.debugLine("LexAna: Power = "+nextChar);
nextToken = new PowerOperatorToken('m');
}
else if (nextChar=='~' && (inspectNextChar() != '='))
{
// e.g. !3 or !(a<3) or ~3 or ~(a<3)
foundToken = true;
//ErrorLogger.debugLine("LexAna: Not = "+nextChar);
nextToken = new UnaryOperatorToken(nextChar);
}
else if((nextChar == '<' ) ||
(nextChar == '>' ) ||
(nextChar == '~' ) ||
(nextChar == '&' ) ||
(nextChar == '|' ) ||
(nextChar == '=' && inspectNextChar() == '=') )
{
foundToken = true;
// < less than '<'
// > greater than '>'
// <= less than or equal 'l'
// >= greater than or equal 'g'
// == equal 'e' (see Assign)
// ~= not equal 'n'
if (inspectNextChar() == '=')
{
advance();
// <=, >=, ~=
if(nextChar == '<')
{
nextChar = 'l';
}
else if(nextChar == '>')
{
nextChar = 'g';
}
else if(nextChar == '~')
{
nextChar = 'n';
}
else if(nextChar == '=')
{
nextChar = 'e';
}
}
else if (nextChar=='&' && (inspectNextChar() == '&'))
{
// && -> and
advance();
nextChar = 'a';
}
else if (nextChar=='|' && (inspectNextChar() == '|'))
{
// || -> or
advance();
nextChar = 'o';
}
//ErrorLogger.debugLine("LexAna: Relation = "+nextChar);
nextToken = new RelationOperatorToken(nextChar); //, bracketLevel);
}
else if(nextChar == '=')
{
foundToken = true;
//token is an assignment operator
//ErrorLogger.debugLine("LexAna: Assign = --");
nextToken = new AssignmentOperatorToken(); //bracketLevel);
}
else if(nextChar == ':')
{
foundToken = true;
//ErrorLogger.debugLine("LexAna: Colon = "+nextChar);
nextToken = new ColonOperatorToken();
}
else if(nextChar =='!')
{
foundToken = true;
// matlab-style "~=" and C-style "!=" implement NOT-EQUAL
if (inspectNextChar() == '=')
{
advance();
//ErrorLogger.debugLine("LexAna: !=");
nextToken = new RelationOperatorToken('n'); //, bracketLevel);
}
else
{
// factorial (e.g. 3!) or
// logical not (e.g. !(2<3) )
//ErrorLogger.debugLine("LexAna: !");
nextToken = new UnaryOperatorToken('!'); //, bracketLevel);
}
}
else if(nextChar == '@')
{
foundToken = true;
//token is a function handle
//ErrorLogger.debugLine("LexAna: function handle");
//token is either a command, function or variable
StringBuffer sBuffer = new StringBuffer();
while((textChars.indexOf(inspectNextChar()) != -1) )
{
sBuffer.append( getNextChar() );
}
String name = sBuffer.toString();
nextToken = new FunctionHandleToken(name);
}
return foundToken;
}
/**Check if the next token is a delimiter
@return true if it is
*/
private boolean handleDelimiter(char nextChar)
{
boolean foundToken = false;
if(delimiterChars.indexOf(nextChar) != -1)
{
//token is a delimiter
if(nextChar == '(')
bracketLevel += BRACKET_PRIORITY;
else if(nextChar == ')')
bracketLevel -= BRACKET_PRIORITY;
//check the bracket ordering is valid
if(bracketLevel < 0)
{
Errors.throwMathLibException(ERR_BRACKET_ORDER);
}
//ErrorLogger.debugLine("LexAna: Delimiter = "+nextChar);
foundToken = true;
nextToken = new DelimiterToken(nextChar);
// remove trailing ' ' \t and \n
//remove multiple spaces and tabs
if ((nextChar == ';') || (nextChar == ','))
{
while (((inspectNextChar() == ' ') ||
(inspectNextChar() == '\t') ) && !EOChars() )
{
advance();
}
// remove return after other delimiter
// e.g. disp(x); \n (\n will be removed)
// e.g. disp(x) \n (\n will not be removed)
if ((inspectNextChar()=='\n'))
advance();
}
}
return foundToken;
}
/**Check if the next token is a dot operator
@return true if it is
*/
private boolean handleDotOperator(char nextChar)
{
boolean foundToken = false;
if(nextChar == '.' )
{
foundToken = true;
char nextNextChar = inspectNextChar();
// something like ".*", "./", ".^", "..." (scalar operations on matrices)
// ".\"
switch(nextNextChar)
{
case '*':
{
nextChar = getNextChar();
//ErrorLogger.debugLine("LexAna: scalar muliplication .*");
nextToken = new MulDivOperatorToken('m');
break;
}
case '/':
{
nextChar = getNextChar();
//ErrorLogger.debugLine("LexAna: scalar division ./");
nextToken = new MulDivOperatorToken('d');
break;
}
case '\\':
{
nextChar = getNextChar();
//ErrorLogger.debugLine("LexAna: scalar left division .\\");
nextToken = new MulDivOperatorToken('l');
break;
}
case '^':
{
nextChar = getNextChar();
//ErrorLogger.debugLine("LexAna: scalar power .^");
nextToken = new PowerOperatorToken('p');
break;
}
case '\'':
{
nextChar = getNextChar();
//ErrorLogger.debugLine("LexAna: nonconjugate transpose .'");
nextToken = new UnaryOperatorToken('t');
break;
}
case '.':
{
// checking for ... \n opr
// .... \r
nextChar = getNextChar();
if( inspectNextChar()=='.')
{
//found ...
invisibleCode = true; // the scanned code needs to be ignored
nextChar = getNextChar();
// scan the next chars until a \r or \n is found
while ((inspectNextChar() != '\r') &&
(inspectNextChar() != '\n') &&
!EOCharsB )
{
getNextChar();
}
getNextChar(); // remove \n or \r
return false;
}
else
{
// found .. this is not allowed
Errors.throwMathLibException("LexAnal: found .. instead of ...");
}
break;
}
default:
{
if (numberChars.indexOf(inspectNextChar()) != -1)
{
return false;
}
else
{
// dot operator
//ErrorLogger.debugLine("LexAna: dot operator");
nextToken = new DotOperatorToken();
}
break;
}
}
}
return foundToken;
}
} // end LexicalAnalyser
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -