token.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #include "token.h"
  2. #include <util/string/vector.h>
  3. #include <util/string/printf.h>
  4. namespace NYson {
  5. ////////////////////////////////////////////////////////////////////////////////
  6. ETokenType CharToTokenType(char ch) {
  7. switch (ch) {
  8. case ';':
  9. return ETokenType::Semicolon;
  10. case '=':
  11. return ETokenType::Equals;
  12. case '{':
  13. return ETokenType::LeftBrace;
  14. case '}':
  15. return ETokenType::RightBrace;
  16. case '#':
  17. return ETokenType::Hash;
  18. case '[':
  19. return ETokenType::LeftBracket;
  20. case ']':
  21. return ETokenType::RightBracket;
  22. case '<':
  23. return ETokenType::LeftAngle;
  24. case '>':
  25. return ETokenType::RightAngle;
  26. case '(':
  27. return ETokenType::LeftParenthesis;
  28. case ')':
  29. return ETokenType::RightParenthesis;
  30. case '+':
  31. return ETokenType::Plus;
  32. case ':':
  33. return ETokenType::Colon;
  34. case ',':
  35. return ETokenType::Comma;
  36. default:
  37. return ETokenType::EndOfStream;
  38. }
  39. }
  40. char TokenTypeToChar(ETokenType type) {
  41. switch (type) {
  42. case ETokenType::Semicolon:
  43. return ';';
  44. case ETokenType::Equals:
  45. return '=';
  46. case ETokenType::Hash:
  47. return '#';
  48. case ETokenType::LeftBracket:
  49. return '[';
  50. case ETokenType::RightBracket:
  51. return ']';
  52. case ETokenType::LeftBrace:
  53. return '{';
  54. case ETokenType::RightBrace:
  55. return '}';
  56. case ETokenType::LeftAngle:
  57. return '<';
  58. case ETokenType::RightAngle:
  59. return '>';
  60. case ETokenType::LeftParenthesis:
  61. return '(';
  62. case ETokenType::RightParenthesis:
  63. return ')';
  64. case ETokenType::Plus:
  65. return '+';
  66. case ETokenType::Colon:
  67. return ':';
  68. case ETokenType::Comma:
  69. return ',';
  70. default:
  71. Y_ABORT("unreachable");
  72. }
  73. }
  74. TString TokenTypeToString(ETokenType type) {
  75. return TString(1, TokenTypeToChar(type));
  76. }
  77. ////////////////////////////////////////////////////////////////////////////////
  78. const TToken TToken::EndOfStream;
  79. TToken::TToken()
  80. : Type_(ETokenType::EndOfStream)
  81. , Int64Value(0)
  82. , Uint64Value(0)
  83. , DoubleValue(0.0)
  84. , BooleanValue(false)
  85. {
  86. }
  87. TToken::TToken(ETokenType type)
  88. : Type_(type)
  89. , Int64Value(0)
  90. , Uint64Value(0)
  91. , DoubleValue(0.0)
  92. , BooleanValue(false)
  93. {
  94. switch (type) {
  95. case ETokenType::String:
  96. case ETokenType::Int64:
  97. case ETokenType::Uint64:
  98. case ETokenType::Double:
  99. case ETokenType::Boolean:
  100. Y_ABORT("unreachable");
  101. default:
  102. break;
  103. }
  104. }
  105. TToken::TToken(const TStringBuf& stringValue)
  106. : Type_(ETokenType::String)
  107. , StringValue(stringValue)
  108. , Int64Value(0)
  109. , Uint64Value(0)
  110. , DoubleValue(0.0)
  111. , BooleanValue(false)
  112. {
  113. }
  114. TToken::TToken(i64 int64Value)
  115. : Type_(ETokenType::Int64)
  116. , Int64Value(int64Value)
  117. , Uint64Value(0)
  118. , DoubleValue(0.0)
  119. {
  120. }
  121. TToken::TToken(ui64 uint64Value)
  122. : Type_(ETokenType::Uint64)
  123. , Int64Value(0)
  124. , Uint64Value(uint64Value)
  125. , DoubleValue(0.0)
  126. , BooleanValue(false)
  127. {
  128. }
  129. TToken::TToken(double doubleValue)
  130. : Type_(ETokenType::Double)
  131. , Int64Value(0)
  132. , Uint64Value(0)
  133. , DoubleValue(doubleValue)
  134. , BooleanValue(false)
  135. {
  136. }
  137. TToken::TToken(bool booleanValue)
  138. : Type_(ETokenType::Boolean)
  139. , Int64Value(0)
  140. , DoubleValue(0.0)
  141. , BooleanValue(booleanValue)
  142. {
  143. }
  144. bool TToken::IsEmpty() const {
  145. return Type_ == ETokenType::EndOfStream;
  146. }
  147. const TStringBuf& TToken::GetStringValue() const {
  148. CheckType(ETokenType::String);
  149. return StringValue;
  150. }
  151. i64 TToken::GetInt64Value() const {
  152. CheckType(ETokenType::Int64);
  153. return Int64Value;
  154. }
  155. ui64 TToken::GetUint64Value() const {
  156. CheckType(ETokenType::Uint64);
  157. return Uint64Value;
  158. }
  159. double TToken::GetDoubleValue() const {
  160. CheckType(ETokenType::Double);
  161. return DoubleValue;
  162. }
  163. bool TToken::GetBooleanValue() const {
  164. CheckType(ETokenType::Boolean);
  165. return BooleanValue;
  166. }
  167. void TToken::CheckType(ETokenType expectedType) const {
  168. if (Type_ != expectedType) {
  169. if (Type_ == ETokenType::EndOfStream) {
  170. ythrow TYsonException() << "Unexpected end of stream (ExpectedType: " << TokenTypeToString(expectedType) << ")";
  171. } else {
  172. ythrow TYsonException() << "Unexpected token (Token: '" << ToString(*this)
  173. << "', Type: " << TokenTypeToString(Type_)
  174. << ", ExpectedType: " << TokenTypeToString(expectedType) << ")";
  175. }
  176. }
  177. }
  178. void TToken::Reset() {
  179. Type_ = ETokenType::EndOfStream;
  180. Int64Value = 0;
  181. Uint64Value = 0;
  182. DoubleValue = 0.0;
  183. StringValue = TStringBuf();
  184. BooleanValue = false;
  185. }
  186. TString ToString(const TToken& token) {
  187. switch (token.GetType()) {
  188. case ETokenType::EndOfStream:
  189. return TString();
  190. case ETokenType::String:
  191. return TString(token.GetStringValue());
  192. case ETokenType::Int64:
  193. return ::ToString(token.GetInt64Value());
  194. case ETokenType::Uint64:
  195. return ::ToString(token.GetUint64Value());
  196. case ETokenType::Double:
  197. return ::ToString(token.GetDoubleValue());
  198. case ETokenType::Boolean:
  199. return token.GetBooleanValue() ? "true" : "false";
  200. default:
  201. return TokenTypeToString(token.GetType());
  202. }
  203. }
  204. ////////////////////////////////////////////////////////////////////////////////
  205. } // namespace NYson