You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Parser.h 4.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #ifndef LEPTON_PARSER_H_
  2. #define LEPTON_PARSER_H_
  3. /* -------------------------------------------------------------------------- *
  4. * Lepton *
  5. * -------------------------------------------------------------------------- *
  6. * This is part of the Lepton expression parser originating from *
  7. * Simbios, the NIH National Center for Physics-Based Simulation of *
  8. * Biological Structures at Stanford, funded under the NIH Roadmap for *
  9. * Medical Research, grant U54 GM072970. See https://simtk.org. *
  10. * *
  11. * Portions copyright (c) 2009 Stanford University and the Authors. *
  12. * Authors: Peter Eastman *
  13. * Contributors: *
  14. * *
  15. * Permission is hereby granted, free of charge, to any person obtaining a *
  16. * copy of this software and associated documentation files (the "Software"), *
  17. * to deal in the Software without restriction, including without limitation *
  18. * the rights to use, copy, modify, merge, publish, distribute, sublicense, *
  19. * and/or sell copies of the Software, and to permit persons to whom the *
  20. * Software is furnished to do so, subject to the following conditions: *
  21. * *
  22. * The above copyright notice and this permission notice shall be included in *
  23. * all copies or substantial portions of the Software. *
  24. * *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
  26. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
  27. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *
  28. * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
  29. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
  30. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE *
  31. * USE OR OTHER DEALINGS IN THE SOFTWARE. *
  32. * -------------------------------------------------------------------------- */
  33. #include "windowsIncludes.h"
  34. #include <map>
  35. #include <string>
  36. #include <vector>
  37. namespace Lepton
  38. {
  39. class CustomFunction;
  40. class ExpressionTreeNode;
  41. class Operation;
  42. class ParsedExpression;
  43. class ParseToken;
  44. /**
  45. * This class provides the main interface for parsing expressions.
  46. */
  47. class LEPTON_EXPORT Parser
  48. {
  49. public:
  50. /**
  51. * Parse a mathematical expression and return a representation of it as an abstract syntax tree.
  52. */
  53. static ParsedExpression parse(const std::string& expression);
  54. /**
  55. * Parse a mathematical expression and return a representation of it as an abstract syntax tree.
  56. *
  57. * @param expression
  58. * @param customFunctions a map specifying user defined functions that may appear in the expression.
  59. * The key are function names, and the values are corresponding CustomFunction objects.
  60. */
  61. static ParsedExpression parse(const std::string& expression, const std::map<std::string, CustomFunction*>& customFunctions);
  62. private:
  63. static std::string trim(const std::string& expression);
  64. static std::vector<ParseToken> tokenize(const std::string& expression);
  65. static ParseToken getNextToken(const std::string& expression, size_t start);
  66. static ExpressionTreeNode parsePrecedence(const std::vector<ParseToken>& tokens, size_t& pos,
  67. const std::map<std::string, CustomFunction*>& customFunctions,
  68. const std::map<std::string, ExpressionTreeNode>& subexpressionDefs, int precedence);
  69. static Operation* getOperatorOperation(const std::string& name);
  70. static Operation* getFunctionOperation(const std::string& name, const std::map<std::string, CustomFunction*>& customFunctions);
  71. };
  72. } // namespace Lepton
  73. #endif /*LEPTON_PARSER_H_*/