status_t ExpressionParser::ParseNextCommandArgument(const char** expressionString, char* buffer, size_t bufferSize) { fTokenizer.SetTo(*expressionString); fTokenizer.SetCommandMode(true); if (fTokenizer.NextToken().type == TOKEN_END_OF_LINE) return B_ENTRY_NOT_FOUND; fTokenizer.RewindToken(); char* argv[2]; int argc = 0; if (!_ParseArgument(argc, argv)) return B_BAD_VALUE; strlcpy(buffer, argv[0], bufferSize); const Token& token = fTokenizer.NextToken(); if (token.type == TOKEN_END_OF_LINE) *expressionString = NULL; else *expressionString += token.position; return B_OK; }
uint64 ExpressionParser::EvaluateExpression(const char* expressionString) { fTokenizer.SetTo(expressionString); uint64 value = _ParseExpression(); const Token& token = fTokenizer.NextToken(); if (token.type != TOKEN_END_OF_LINE) parse_exception("parse error", token.position); return value; }
uint64 ExpressionParser::EvaluateCommand(const char* expressionString, int& returnCode) { fTokenizer.SetTo(expressionString); // Allowed are command or assignment. A command always starts with an // identifier, an assignment either with an identifier (variable name) or // a dereferenced address. const Token& token = fTokenizer.NextToken(); uint64 value = 0; while (true) { int32 startPosition = token.position; if (token.type == TOKEN_IDENTIFIER) { fTokenizer.NextToken(); if (token.type & TOKEN_ASSIGN_FLAG) { // an assignment fTokenizer.SetPosition(startPosition); value = _ParseExpression(true); returnCode = 0; } else { // no assignment, so let's assume it's a command fTokenizer.SetPosition(startPosition); fTokenizer.SetCommandMode(true); value = _ParseCommandPipe(returnCode); } } else if (token.type == TOKEN_STAR) { // dereferenced address -- assignment fTokenizer.SetPosition(startPosition); value = _ParseExpression(true); returnCode = 0; } else parse_exception("expected command or assignment", token.position); // might be chained with ";" if (fTokenizer.NextToken().type != TOKEN_SEMICOLON) break; fTokenizer.SetCommandMode(false); fTokenizer.NextToken(); } if (token.type != TOKEN_END_OF_LINE) parse_exception("parse error", token.position); return value; }