ExpressionPrimitive.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AzCore/std/string/regex.h>
  9. #include <AzCore/Casting/numeric_cast.h>
  10. #include <AzFramework/StringFunc/StringFunc.h>
  11. #include <ExpressionEngine/ExpressionPrimitive.h>
  12. #include <ExpressionEngine/InternalTypes.h>
  13. namespace ExpressionEvaluation
  14. {
  15. ////////////////////
  16. // PrimitiveParser
  17. ////////////////////
  18. void PrimitiveParser::EvaluateToken(const ElementInformation& parseResult, ExpressionResultStack& evaluationStack) const
  19. {
  20. evaluationStack.emplace(parseResult.m_extraStore);
  21. }
  22. ///////////////////////////
  23. // NumericPrimitiveParser
  24. ///////////////////////////
  25. NumericPrimitiveParser::NumericPrimitiveParser()
  26. : m_regex(R"(^(0|([1-9][0-9]*))(\.[0-9]+)?)")
  27. {
  28. }
  29. ExpressionParserId NumericPrimitiveParser::GetParserId() const
  30. {
  31. return Interfaces::NumericPrimitives;
  32. }
  33. NumericPrimitiveParser::ParseResult NumericPrimitiveParser::ParseElement(const AZStd::string& inputText, size_t offset) const
  34. {
  35. AZStd::smatch match;
  36. ParseResult result;
  37. if (AZStd::regex_search(&inputText.at(offset), match, m_regex))
  38. {
  39. AZStd::string matchedCharacters = match[0].str();
  40. result.m_charactersConsumed = static_cast<int>(matchedCharacters.length());
  41. double numericValue = AzFramework::StringFunc::ToDouble(matchedCharacters.c_str());
  42. result.m_element = Primitive::GetPrimitiveElement(numericValue);
  43. }
  44. return result;
  45. }
  46. ///////////////////////////
  47. // BooleanPrimitiveParser
  48. ///////////////////////////
  49. BooleanPrimitiveParser::BooleanPrimitiveParser()
  50. : m_regex(R"((true|false))", AZStd::regex::ECMAScript | AZStd::regex::icase)
  51. {
  52. }
  53. ExpressionParserId BooleanPrimitiveParser::GetParserId() const
  54. {
  55. return Interfaces::BooleanPrimitives;
  56. }
  57. BooleanPrimitiveParser::ParseResult BooleanPrimitiveParser::ParseElement(const AZStd::string& inputText, size_t offset) const
  58. {
  59. AZStd::smatch match;
  60. ParseResult result;
  61. if (AZStd::regex_search(&inputText.at(offset), match, m_regex))
  62. {
  63. AZStd::string matchedCharacters = match[0].str();
  64. AZStd::to_lower(matchedCharacters.begin(), matchedCharacters.end());
  65. result.m_charactersConsumed = matchedCharacters.length();
  66. bool booleanValue = AzFramework::StringFunc::ToBool(matchedCharacters.c_str());
  67. result.m_element = Primitive::GetPrimitiveElement(booleanValue);
  68. }
  69. return result;
  70. }
  71. }