/src/hermes/include/hermes/SourceMap/SourceMapParser.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) Meta Platforms, Inc. and affiliates. |
3 | | * |
4 | | * This source code is licensed under the MIT license found in the |
5 | | * LICENSE file in the root directory of this source tree. |
6 | | */ |
7 | | |
8 | | #ifndef HERMES_SUPPORT_SOURCEMAPPARSER_H |
9 | | #define HERMES_SUPPORT_SOURCEMAPPARSER_H |
10 | | |
11 | | #include "hermes/SourceMap/SourceMapGenerator.h" |
12 | | |
13 | | #include "llvh/Support/MemoryBuffer.h" |
14 | | |
15 | | namespace hermes { |
16 | | |
17 | | /// JavaScript version 3 source map parser. |
18 | | /// See https://sourcemaps.info/spec.html for the spec that this class |
19 | | /// parses. |
20 | | class SourceMapParser { |
21 | | public: |
22 | | /// Parse input \p sourceMap and return parsed SourceMap. |
23 | | /// On failure if malformed, prints an error message and returns nullptr. |
24 | | static std::unique_ptr<SourceMap> parse( |
25 | | llvh::MemoryBufferRef sourceMap, |
26 | | SourceErrorManager &sm); |
27 | | |
28 | | /// Parse input \p sourceMapContent and return parsed SourceMap. |
29 | | /// Set the filename of the map file to "<source map>". |
30 | | /// On failure if malformed, prints an error message and returns nullptr. |
31 | | static std::unique_ptr<SourceMap> parse( |
32 | | llvh::StringRef sourceMapContent, |
33 | 0 | SourceErrorManager &sm) { |
34 | 0 | return parse(llvh::MemoryBufferRef(sourceMapContent, "<source map>"), sm); |
35 | 0 | } |
36 | | |
37 | | private: |
38 | | SourceMapParser() = delete; |
39 | | SourceMapParser(SourceMapParser &) = delete; |
40 | | SourceMapParser(SourceMapParser &&) = delete; |
41 | | SourceMapParser &operator=(SourceMapParser &) = delete; |
42 | | SourceMapParser &operator=(SourceMapParser &&) = delete; |
43 | | |
44 | | /// Delta encoding state. |
45 | | struct State { |
46 | | int32_t generatedColumn = 0; |
47 | | int32_t sourceIndex = 0; |
48 | | int32_t representedLine = 0; |
49 | | int32_t representedColumn = 0; |
50 | | int32_t nameIndex = 0; |
51 | | }; |
52 | | |
53 | | /// Parse "mappings" section from \p sourceMappings. The parsed line mappings |
54 | | /// are returned in \p lines. |
55 | | static bool parseMappings( |
56 | | llvh::StringRef sourceMappings, |
57 | | std::vector<SourceMap::SegmentList> &lines); |
58 | | |
59 | | /// Parse single segment in mapping. |
60 | | static llvh::Optional<SourceMap::Segment> |
61 | | parseSegment(const State &state, const char *&pCur, const char *pSegEnd); |
62 | | }; |
63 | | |
64 | | } // namespace hermes |
65 | | |
66 | | #endif // HERMES_SUPPORT_SOURCEMAPPARSER_H |