parser.rs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. use std::{collections::HashMap, io, io::Write, process, str::Chars};
  2. use itertools::Itertools;
  3. use termion::{color, style};
  4. use crate::{
  5. lexer::{Token, TokenType},
  6. types::{Constant, Type, Witness},
  7. };
  8. pub type Ast = HashMap<String, HashMap<String, HashMap<String, (Token, Token)>>>;
  9. pub type UnparsedConstants = HashMap<String, (Token, Token)>;
  10. pub type Constants = Vec<Constant>;
  11. pub type UnparsedWitnesses = HashMap<String, (Token, Token)>;
  12. pub type Witnesses = Vec<Witness>;
  13. pub struct Parser {
  14. file: String,
  15. lines: Vec<String>,
  16. tokens: Vec<Token>,
  17. }
  18. impl Parser {
  19. pub fn new(filename: &str, source: Chars, tokens: Vec<Token>) -> Self {
  20. // For nice error reporting, we'll load everything into a string
  21. // vector so we have references to lines.
  22. let lines = source.as_str().lines().map(|x| x.to_string()).collect();
  23. Parser { file: filename.to_string(), lines, tokens }
  24. }
  25. pub fn parse(self) -> (Constants, Witnesses, Ast) {
  26. // We use these to keep state when iterating
  27. let mut declaring_constant = false;
  28. let mut declaring_contract = false;
  29. let mut declaring_circuit = false;
  30. let mut constant_tokens = vec![];
  31. let mut contract_tokens = vec![];
  32. let mut circuit_tokens = vec![];
  33. let mut ast = HashMap::new();
  34. let mut namespace = String::new();
  35. let mut ast_inner = HashMap::new();
  36. let mut namespace_found = false; // Nasty
  37. let mut iter = self.tokens.iter();
  38. while let Some(t) = iter.next() {
  39. // Start by declaring a section
  40. if !declaring_constant && !declaring_contract && !declaring_circuit {
  41. if t.token_type != TokenType::Symbol {
  42. // TODO: Revisit
  43. // TODO: Visit this again when we are allowing imports
  44. unimplemented!();
  45. }
  46. // The sections we must be declaring in our source code
  47. match t.token.as_str() {
  48. "constant" => {
  49. declaring_constant = true;
  50. // Eat all the tokens within the `constant` section
  51. for inner in iter.by_ref() {
  52. constant_tokens.push(inner.clone());
  53. if inner.token_type == TokenType::RightBrace {
  54. break
  55. }
  56. }
  57. }
  58. "contract" => {
  59. declaring_contract = true;
  60. // Eat all the tokens within the `contract` section
  61. for inner in iter.by_ref() {
  62. contract_tokens.push(inner.clone());
  63. if inner.token_type == TokenType::RightBrace {
  64. break
  65. }
  66. }
  67. }
  68. "circuit" => {
  69. declaring_circuit = true;
  70. // Eat all the tokens within the `circuit` section
  71. for inner in iter.by_ref() {
  72. circuit_tokens.push(inner.clone());
  73. if inner.token_type == TokenType::RightBrace {
  74. break
  75. }
  76. }
  77. }
  78. x => self.error(format!("Unknown `{}` proof section", x), t.line, t.column),
  79. }
  80. }
  81. // We shouldn't be reaching these states
  82. if declaring_constant && (declaring_contract || declaring_circuit) {
  83. unreachable!()
  84. }
  85. if declaring_contract && (declaring_constant || declaring_circuit) {
  86. unreachable!()
  87. }
  88. if declaring_circuit && (declaring_constant || declaring_contract) {
  89. unreachable!()
  90. }
  91. // Now go through the token vectors and work it through
  92. if declaring_constant {
  93. self.check_section_structure("constant", constant_tokens.clone());
  94. // TODO: Do we need this?
  95. if namespace_found && namespace != constant_tokens[0].token {
  96. self.error(
  97. format!(
  98. "Found `{}` namespace. Expected `{}`.",
  99. constant_tokens[0].token, namespace
  100. ),
  101. constant_tokens[0].line,
  102. constant_tokens[0].column,
  103. );
  104. } else {
  105. namespace = constant_tokens[0].token.clone();
  106. namespace_found = true;
  107. }
  108. let constants_cloned = constant_tokens.clone();
  109. let mut constants_map = HashMap::new();
  110. // This is everything between the braces: { .. }
  111. let mut constants_inner = constants_cloned[2..constant_tokens.len() - 1].iter();
  112. while let Some((typ, name, comma)) = constants_inner.next_tuple() {
  113. if comma.token_type != TokenType::Comma {
  114. self.error(
  115. "Separator is not a comma".to_string(),
  116. comma.line,
  117. comma.column,
  118. );
  119. }
  120. if constants_map.contains_key(name.token.as_str()) {
  121. self.error(
  122. format!(
  123. "Section `constant` already contains the token `{}`.",
  124. &name.token
  125. ),
  126. name.line,
  127. name.column,
  128. );
  129. }
  130. constants_map.insert(name.token.clone(), (name.clone(), typ.clone()));
  131. }
  132. ast_inner.insert("constant".to_string(), constants_map);
  133. declaring_constant = false;
  134. }
  135. if declaring_contract {
  136. self.check_section_structure("contract", contract_tokens.clone());
  137. // TODO: Do we need this?
  138. if namespace_found && namespace != contract_tokens[0].token {
  139. self.error(
  140. format!(
  141. "Found `{}` namespace. Expected `{}`.",
  142. contract_tokens[0].token, namespace
  143. ),
  144. contract_tokens[0].line,
  145. contract_tokens[0].column,
  146. );
  147. } else {
  148. namespace = contract_tokens[0].token.clone();
  149. namespace_found = true;
  150. }
  151. let contract_cloned = contract_tokens.clone();
  152. let mut contract_map = HashMap::new();
  153. // This is everything between the braces: { .. }
  154. let mut contract_inner = contract_cloned[2..contract_tokens.len() - 1].iter();
  155. while let Some((typ, name, comma)) = contract_inner.next_tuple() {
  156. if comma.token_type != TokenType::Comma {
  157. self.error(
  158. "Separator is not a comma".to_string(),
  159. comma.line,
  160. comma.column,
  161. );
  162. }
  163. if contract_map.contains_key(name.token.as_str()) {
  164. self.error(
  165. format!(
  166. "Section `contract` already contains the token `{}`.",
  167. &name.token
  168. ),
  169. name.line,
  170. name.column,
  171. );
  172. }
  173. contract_map.insert(name.token.clone(), (name.clone(), typ.clone()));
  174. }
  175. ast_inner.insert("contract".to_string(), contract_map);
  176. declaring_contract = false;
  177. }
  178. if declaring_circuit {
  179. declaring_circuit = false;
  180. }
  181. }
  182. ast.insert(namespace.clone(), ast_inner);
  183. self.verify_initial_ast(&ast);
  184. // Clean up the `constant` section
  185. let (constants, err) =
  186. Parser::parse_ast_constants(ast.get(&namespace).unwrap().get("constant").unwrap());
  187. if let Some(err_msg) = err {
  188. // TODO: Return problematic token from parse_ast_constants()
  189. self.error(err_msg, 1, 1);
  190. }
  191. // Clean up the `contract section
  192. let (contract, err) =
  193. Parser::parse_ast_contract(ast.get(&namespace).unwrap().get("contract").unwrap());
  194. if let Some(err_msg) = err {
  195. // TODO: Return problematic token from parse_ast_contract()
  196. self.error(err_msg, 1, 1);
  197. }
  198. // Return
  199. (constants, contract, HashMap::new())
  200. }
  201. fn verify_initial_ast(&self, ast: &Ast) {
  202. // Verify that there are all 3 sections
  203. for v in ast.values() {
  204. if !v.contains_key("constant") {
  205. self.error("Missing `constant` section in the source.".to_string(), 1, 1);
  206. }
  207. if !v.contains_key("contract") {
  208. self.error("Missing `contract` section in the source.".to_string(), 1, 1);
  209. }
  210. /*
  211. if !v.contains_key("circuit") {
  212. self.error("Missing `circuit` section in the source.".to_string(), 1, 1);
  213. }
  214. */
  215. }
  216. }
  217. fn check_section_structure(&self, section: &str, tokens: Vec<Token>) {
  218. if tokens[0].token_type != TokenType::String {
  219. self.error(
  220. format!("{} section declaration must start with a naming string.", section),
  221. tokens[0].line,
  222. tokens[0].column,
  223. );
  224. }
  225. if tokens[1].token_type != TokenType::LeftBrace {
  226. self.error(
  227. format!(
  228. "{} section opening is not correct. Must be opened with a left brace `{{`",
  229. section
  230. ),
  231. tokens[0].line,
  232. tokens[0].column,
  233. );
  234. }
  235. if tokens[tokens.len() - 1].token_type != TokenType::RightBrace {
  236. self.error(
  237. format!(
  238. "{} section closing is not correct. Must be closed with a right brace `}}`",
  239. section
  240. ),
  241. tokens[0].line,
  242. tokens[0].column,
  243. );
  244. }
  245. if tokens[2..tokens.len() - 1].len() % 3 != 0 {
  246. self.error(
  247. format!(
  248. "Invalid number of elements in `{}` section. Must be pairs of `type:name` separated with a comma `,`",
  249. section
  250. ),
  251. tokens[0].line,
  252. tokens[0].column,
  253. );
  254. }
  255. }
  256. fn parse_ast_constants(ast: &UnparsedConstants) -> (Constants, Option<String>) {
  257. let mut ret = vec![];
  258. for (k, v) in ast {
  259. if &v.0.token != k {
  260. return (vec![], Some("Constant name doesn't match token".to_string()))
  261. }
  262. if v.0.token_type != TokenType::Symbol {
  263. return (vec![], Some("Constant name is not a symbol".to_string()))
  264. }
  265. if v.1.token_type != TokenType::Symbol {
  266. return (vec![], Some("Constant type is not a symbol".to_string()))
  267. }
  268. match v.1.token.as_str() {
  269. "EcFixedPoint" => {
  270. ret.push(Constant {
  271. name: k.to_string(),
  272. typ: Type::EcFixedPoint,
  273. line: v.0.line,
  274. column: v.0.column,
  275. });
  276. }
  277. x => {
  278. let err_msg = format!("`{}` is an illegal constant type", x);
  279. return (vec![], Some(err_msg))
  280. }
  281. }
  282. }
  283. (ret, None)
  284. }
  285. fn parse_ast_contract(ast: &UnparsedWitnesses) -> (Witnesses, Option<String>) {
  286. let mut ret = vec![];
  287. for (k, v) in ast {
  288. if &v.0.token != k {
  289. return (vec![], Some("Contract input name doesn't match token".to_string()))
  290. }
  291. if v.0.token_type != TokenType::Symbol {
  292. return (vec![], Some("Contract input name is not a symbol".to_string()))
  293. }
  294. if v.1.token_type != TokenType::Symbol {
  295. return (vec![], Some("Contract input type is not a symbol".to_string()))
  296. }
  297. match v.1.token.as_str() {
  298. "Base" => {
  299. ret.push(Witness {
  300. name: k.to_string(),
  301. typ: Type::Base,
  302. line: v.0.line,
  303. column: v.0.column,
  304. });
  305. }
  306. "Scalar" => {
  307. ret.push(Witness {
  308. name: k.to_string(),
  309. typ: Type::Scalar,
  310. line: v.0.line,
  311. column: v.0.column,
  312. });
  313. }
  314. "MerklePath" => {
  315. ret.push(Witness {
  316. name: k.to_string(),
  317. typ: Type::MerklePath,
  318. line: v.0.line,
  319. column: v.0.column,
  320. });
  321. }
  322. x => {
  323. let err_msg = format!("`{}` is an illegal witness type", x);
  324. return (vec![], Some(err_msg))
  325. }
  326. }
  327. }
  328. (ret, None)
  329. }
  330. fn error(&self, msg: String, ln: usize, col: usize) {
  331. let err_msg = format!("{} (line {}, column {})", msg, ln, col);
  332. let dbg_msg = format!("{}:{}:{}: {}", self.file, ln, col, self.lines[ln - 1]);
  333. let pad = dbg_msg.split(": ").next().unwrap().len() + col + 2;
  334. let caret = format!("{:width$}^", "", width = pad);
  335. let msg = format!("{}\n{}\n{}\n", err_msg, dbg_msg, caret);
  336. Parser::abort(&msg);
  337. }
  338. fn abort(msg: &str) {
  339. let stderr = io::stderr();
  340. let mut handle = stderr.lock();
  341. write!(
  342. handle,
  343. "{}{}Parser error:{} {}",
  344. style::Bold,
  345. color::Fg(color::Red),
  346. style::Reset,
  347. msg,
  348. )
  349. .unwrap();
  350. handle.flush().unwrap();
  351. process::exit(1);
  352. }
  353. }