diff options
author | Wouter van Oortmerssen <wvo@google.com> | 2016-06-15 13:54:17 -0700 |
---|---|---|
committer | Wouter van Oortmerssen <wvo@google.com> | 2016-06-15 14:54:44 -0700 |
commit | d3ac0bc149a9e62feec8e3a00f10ca88491e2254 (patch) | |
tree | ac67d044571884fca4e4eaef63de63b757774fd6 /src | |
parent | afa276288c8db9a983aa8ae134328b1b447bec70 (diff) | |
download | flatbuffers-d3ac0bc149a9e62feec8e3a00f10ca88491e2254.tar.gz flatbuffers-d3ac0bc149a9e62feec8e3a00f10ca88491e2254.tar.bz2 flatbuffers-d3ac0bc149a9e62feec8e3a00f10ca88491e2254.zip |
Added conversion operations that can be used inline in JSON.
e.g.: { myfield: cos(rad(180)) } is equivalent to writing { myfield: -1.0 }
Bug: 29338398
Change-Id: I6fc4ef1fd10bda3ba78cba464414dd071a2f50ca
Tested: on Linux.
Diffstat (limited to 'src')
-rw-r--r-- | src/idl_parser.cpp | 34 |
1 files changed, 32 insertions, 2 deletions
diff --git a/src/idl_parser.cpp b/src/idl_parser.cpp index f5badab1..66e794b3 100644 --- a/src/idl_parser.cpp +++ b/src/idl_parser.cpp @@ -17,6 +17,14 @@ #include <algorithm> #include <list> +#ifdef _WIN32 +#if !defined(_USE_MATH_DEFINES) +#define _USE_MATH_DEFINES // For M_PI. +#endif // !defined(_USE_MATH_DEFINES) +#endif // _WIN32 + +#include <math.h> + #include "flatbuffers/idl.h" #include "flatbuffers/util.h" @@ -1004,8 +1012,30 @@ CheckedError Parser::ParseHash(Value &e, FieldDef* field) { } CheckedError Parser::ParseSingleValue(Value &e) { - // First check if this could be a string/identifier enum value: - if (e.type.base_type != BASE_TYPE_STRING && + // First see if this could be a conversion function: + if (token_ == kTokenIdentifier && *cursor_ == '(') { + auto functionname = attribute_; + NEXT(); + EXPECT('('); + ECHECK(ParseSingleValue(e)); + EXPECT(')'); + #define FLATBUFFERS_FN_DOUBLE(name, op) \ + if (functionname == name) { \ + auto x = strtod(e.constant.c_str(), nullptr); \ + e.constant = NumToString(op); \ + } + FLATBUFFERS_FN_DOUBLE("deg", x / M_PI * 180); + FLATBUFFERS_FN_DOUBLE("rad", x * M_PI / 180); + FLATBUFFERS_FN_DOUBLE("sin", sin(x)); + FLATBUFFERS_FN_DOUBLE("cos", cos(x)); + FLATBUFFERS_FN_DOUBLE("tan", tan(x)); + FLATBUFFERS_FN_DOUBLE("asin", asin(x)); + FLATBUFFERS_FN_DOUBLE("acos", acos(x)); + FLATBUFFERS_FN_DOUBLE("atan", atan(x)); + // TODO(wvo): add more useful conversion functions here. + #undef FLATBUFFERS_FN_DOUBLE + // Then check if this could be a string/identifier enum value: + } else if (e.type.base_type != BASE_TYPE_STRING && e.type.base_type != BASE_TYPE_NONE && (token_ == kTokenIdentifier || token_ == kTokenStringConstant)) { if (IsIdentifierStart(attribute_[0])) { // Enum value. |