| 121 | } |
| 122 | |
| 123 | pub fn lex_python_source(source: &str) -> Result<Vec<PythonToken>> { |
| 124 | let mut process = spawn_python_script_command( |
| 125 | "compat/lex_python.py", |
| 126 | vec!["--stdin", "--output-format", "json"], |
| 127 | default_python_path()?, |
| 128 | )?; |
| 129 | |
| 130 | // Get process stdin and write the input string. |
| 131 | if let Some(mut stdin) = process.stdin.take() { |
| 132 | stdin.write_all(source.as_bytes()).into_diagnostic()?; |
| 133 | } else { |
| 134 | bail!("Failed to open stdin when running `compat/lex_python.py`"); |
| 135 | } |
| 136 | // Get process stdout and parse result. |
| 137 | let output = process.wait_with_output().into_diagnostic()?; |
| 138 | let python_tokens: Vec<PythonToken> = |
| 139 | serde_json::from_str(String::from_utf8_lossy(&output.stdout).as_ref()).into_diagnostic()?; |
| 140 | Ok(python_tokens) |
| 141 | } |
| 142 | |
| 143 | pub fn assert_tokens_eq( |
| 144 | python_tokens: Vec<PythonToken>, |