MCPcopy Create free account
hub / github.com/Glyphack/enderpy / parse_dict

Method parse_dict

parser/src/parser/parser.rs:1871–1922  ·  view source on GitHub ↗

https://docs.python.org/3/reference/expressions.html#dictionary-displays

(
        &mut self,
        node: Node,
        first_key: Option<Expression>,
        first_value: Expression,
    )

Source from the content-addressed store, hash-verified

1869
1870 // https://docs.python.org/3/reference/expressions.html#dictionary-displays
1871 fn parse_dict(
1872 &mut self,
1873 node: Node,
1874 first_key: Option<Expression>,
1875 first_value: Expression,
1876 ) -> Result<Expression, ParsingError> {
1877 if self.at(Kind::For) || self.at(Kind::Async) && matches!(self.peek_kind(), Ok(Kind::For)) {
1878 let Some(key) = first_key else {
1879 panic!("cannot use ** in dict comprehension");
1880 };
1881
1882 // make sure the first key is some
1883 let generators = self.parse_comp_for()?;
1884 self.expect(Kind::RightBracket)?;
1885 Ok(Expression::DictComp(Box::new(DictComp {
1886 node: self.finish_node(node),
1887 key,
1888 value: first_value,
1889 generators,
1890 })))
1891 } else {
1892 // we already consumed the first pair
1893 // so if there are more pairs we need to consume the comma
1894 if !self.at(Kind::RightBracket) {
1895 self.expect(Kind::Comma)?;
1896 self.consume_whitespace_and_newline();
1897 }
1898 let mut keys = match first_key {
1899 Some(k) => vec![k],
1900 None => vec![],
1901 };
1902 let mut values = vec![first_value];
1903 while !self.eat(Kind::RightBracket) {
1904 let (key, value) = self.parse_double_starred_kv_pair()?;
1905
1906 if let Some(k) = key {
1907 keys.push(k)
1908 }
1909
1910 values.push(value);
1911 if !self.at(Kind::RightBracket) {
1912 self.expect(Kind::Comma)?;
1913 self.consume_whitespace_and_newline();
1914 }
1915 }
1916 Ok(Expression::Dict(Box::new(Dict {
1917 node: self.finish_node(node),
1918 keys,
1919 values,
1920 })))
1921 }
1922 }
1923
1924 fn parse_double_starred_kv_pair(
1925 &mut self,

Callers 1

parse_dict_or_setMethod · 0.80

Calls 9

DictCompClass · 0.85
DictClass · 0.85
atMethod · 0.80
parse_comp_forMethod · 0.80
expectMethod · 0.80
finish_nodeMethod · 0.80
eatMethod · 0.80

Tested by

no test coverage detected