JSONScanner.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #pragma once
  6. #include <wchar.h>
  7. namespace JSON
  8. {
  9. class JSONParser;
  10. // Small scanner for exclusive JSON purpose. The general
  11. // JScript scanner is not appropriate here because of the JSON restricted lexical grammar
  12. // token enums and structures are shared although the token semantics is slightly different.
  13. class JSONScanner
  14. {
  15. public:
  16. JSONScanner();
  17. tokens Scan();
  18. void Init(const wchar_t* input, uint len, Token* pOutToken,
  19. ::Js::ScriptContext* sc, const wchar_t* current, ArenaAllocator* allocator);
  20. void Finalizer();
  21. wchar_t* GetCurrentString(){return currentString;}
  22. uint GetCurrentStringLen(){return currentIndex;}
  23. private:
  24. // Data structure for unescaping strings
  25. struct RangeCharacterPair {
  26. public:
  27. uint m_rangeStart;
  28. uint m_rangeLength;
  29. wchar_t m_char;
  30. RangeCharacterPair() {}
  31. RangeCharacterPair(uint rangeStart, uint rangeLength, wchar_t ch) : m_rangeStart(rangeStart), m_rangeLength(rangeLength), m_char(ch) {}
  32. };
  33. typedef JsUtil::List<RangeCharacterPair, ArenaAllocator> RangeCharacterPairList;
  34. RangeCharacterPairList* currentRangeCharacterPairList;
  35. Js::TempGuestArenaAllocatorObject* allocatorObject;
  36. ArenaAllocator* allocator;
  37. void BuildUnescapedString(bool shouldSkipLastCharacter);
  38. RangeCharacterPairList* GetCurrentRangeCharacterPairList(void);
  39. __inline wchar_t ReadNextChar(void)
  40. {
  41. return *currentChar++;
  42. }
  43. __inline wchar_t PeekNextChar(void)
  44. {
  45. return *currentChar;
  46. }
  47. tokens ScanString();
  48. bool IsJSONNumber();
  49. const wchar_t* inputText;
  50. uint inputLen;
  51. const wchar_t* currentChar;
  52. const wchar_t* pTokenString;
  53. Token* pToken;
  54. ::Js::ScriptContext* scriptContext;
  55. uint currentIndex;
  56. wchar_t* currentString;
  57. __field_ecount(stringBufferLength) wchar_t* stringBuffer;
  58. int stringBufferLength;
  59. friend class JSONParser;
  60. };
  61. } // namespace JSON