1 /* 2 * Copyright 2006-2009 Haiku, Inc. All Rights Reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Stephan Aßmus <superstippi@gmx.de> 7 * John Scipione <jscipione@gmail.com> 8 * Ingo Weinhold <bonefish@cs.tu-berlin.de> 9 */ 10 #ifndef EXPRESSION_PARSER_H 11 #define EXPRESSION_PARSER_H 12 13 14 #include <String.h> 15 16 17 class ParseException { 18 public: ParseException(const char * message,int32 position)19 ParseException(const char* message, int32 position) 20 : message(message), 21 position(position) 22 { 23 } 24 ParseException(const ParseException & other)25 ParseException(const ParseException& other) 26 : message(other.message), 27 position(other.position) 28 { 29 } 30 31 BString message; 32 int32 position; 33 }; 34 35 struct Function; 36 class MAPM; 37 38 class ExpressionParser { 39 40 public: 41 ExpressionParser(); 42 ~ExpressionParser(); 43 44 bool DegreeMode(); 45 void SetDegreeMode(bool degrees); 46 47 void SetSupportHexInput(bool enabled); 48 49 BString Evaluate(const char* expressionString); 50 int64 EvaluateToInt64(const char* expressionString); 51 double EvaluateToDouble(const char* expressionString); 52 53 status_t SetSeparators(BString decimal, BString group); 54 55 private: 56 struct Token; 57 class Tokenizer; 58 59 private: 60 MAPM _ParseBinary(); 61 MAPM _ParseSum(); 62 MAPM _ParseProduct(); 63 MAPM _ParsePower(); 64 MAPM _ParseUnary(); 65 void _InitArguments(MAPM values[], 66 int32 argumentCount); 67 MAPM _ParseFunction(const Token& token); 68 MAPM _ParseAtom(); 69 MAPM _ParseFactorial(MAPM value); 70 71 void _EatToken(int32 type); 72 73 Tokenizer* fTokenizer; 74 75 bool fDegreeMode; 76 }; 77 78 #endif // EXPRESSION_PARSER_H 79