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: 19 ParseException(const char* message, int32 position) 20 : message(message), 21 position(position) 22 { 23 } 24 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 private: 54 struct Token; 55 class Tokenizer; 56 57 private: 58 MAPM _ParseBinary(); 59 MAPM _ParseSum(); 60 MAPM _ParseProduct(); 61 MAPM _ParsePower(); 62 MAPM _ParseUnary(); 63 void _InitArguments(MAPM values[], 64 int32 argumentCount); 65 MAPM _ParseFunction(const Token& token); 66 MAPM _ParseAtom(); 67 MAPM _ParseFactorial(MAPM value); 68 69 void _EatToken(int32 type); 70 71 Tokenizer* fTokenizer; 72 73 bool fDegreeMode; 74 }; 75 76 #endif // EXPRESSION_PARSER_H 77