| 1920 | } |
| 1921 | |
| 1922 | static common_peg_parser_variant deserialize_parser_variant(const nlohmann::json & j) { |
| 1923 | if (!j.contains("type") || !j["type"].is_string()) { |
| 1924 | throw std::runtime_error("Parser variant JSON missing or invalid 'type' field"); |
| 1925 | } |
| 1926 | |
| 1927 | std::string type = j["type"]; |
| 1928 | |
| 1929 | if (type == "epsilon") { |
| 1930 | return common_peg_epsilon_parser{}; |
| 1931 | } |
| 1932 | if (type == "start") { |
| 1933 | return common_peg_start_parser{}; |
| 1934 | } |
| 1935 | if (type == "end") { |
| 1936 | return common_peg_end_parser{}; |
| 1937 | } |
| 1938 | if (type == "literal") { |
| 1939 | if (!j.contains("literal") || !j["literal"].is_string()) { |
| 1940 | throw std::runtime_error("literal parser missing or invalid 'literal' field"); |
| 1941 | } |
| 1942 | return common_peg_literal_parser{j["literal"]}; |
| 1943 | } |
| 1944 | if (type == "sequence") { |
| 1945 | if (!j.contains("children") || !j["children"].is_array()) { |
| 1946 | throw std::runtime_error("sequence parser missing or invalid 'children' field"); |
| 1947 | } |
| 1948 | return common_peg_sequence_parser{j["children"].get<std::vector<common_peg_parser_id>>()}; |
| 1949 | } |
| 1950 | if (type == "choice") { |
| 1951 | if (!j.contains("children") || !j["children"].is_array()) { |
| 1952 | throw std::runtime_error("choice parser missing or invalid 'children' field"); |
| 1953 | } |
| 1954 | return common_peg_choice_parser{j["children"].get<std::vector<common_peg_parser_id>>()}; |
| 1955 | } |
| 1956 | if (type == "repetition") { |
| 1957 | if (!j.contains("child") || !j.contains("min_count") || !j.contains("max_count")) { |
| 1958 | throw std::runtime_error("repetition parser missing required fields"); |
| 1959 | } |
| 1960 | return common_peg_repetition_parser{ |
| 1961 | j["child"].get<common_peg_parser_id>(), |
| 1962 | j["min_count"].get<int>(), |
| 1963 | j["max_count"].get<int>() |
| 1964 | }; |
| 1965 | } |
| 1966 | if (type == "and") { |
| 1967 | if (!j.contains("child")) { |
| 1968 | throw std::runtime_error("and parser missing 'child' field"); |
| 1969 | } |
| 1970 | return common_peg_and_parser{j["child"].get<common_peg_parser_id>()}; |
| 1971 | } |
| 1972 | if (type == "not") { |
| 1973 | if (!j.contains("child")) { |
| 1974 | throw std::runtime_error("not parser missing 'child' field"); |
| 1975 | } |
| 1976 | return common_peg_not_parser{j["child"].get<common_peg_parser_id>()}; |
| 1977 | } |
| 1978 | if (type == "any") { |
| 1979 | return common_peg_any_parser{}; |