diff --git a/Cargo.lock b/Cargo.lock index 2ae1a50..a0c8439 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -73,6 +73,12 @@ version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +[[package]] +name = "cc" +version = "1.0.72" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22a9137b95ea06864e018375b72adfb7db6e6f68cfc8df5a04d00288050485ee" + [[package]] name = "cfg-if" version = "1.0.0" @@ -88,6 +94,17 @@ dependencies = [ "ahash", ] +[[package]] +name = "clipboard-win" +version = "4.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3db8340083d28acb43451166543b98c838299b7e0863621be53a338adceea0ed" +dependencies = [ + "error-code", + "str-buf", + "winapi", +] + [[package]] name = "const-random" version = "0.1.13" @@ -116,6 +133,23 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7" +[[package]] +name = "debug2" +version = "0.1.0" +dependencies = [ + "debug2-derive", +] + +[[package]] +name = "debug2-derive" +version = "0.1.0" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "synstructure", +] + [[package]] name = "diff" version = "0.1.12" @@ -158,6 +192,33 @@ dependencies = [ "log", ] +[[package]] +name = "endian-type" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c34f04666d835ff5d62e058c3995147c06f42fe86ff053337632bca83e42702d" + +[[package]] +name = "error-code" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5115567ac25674e0043e472be13d14e537f37ea8aa4bdc4aef0c89add1db1ff" +dependencies = [ + "libc", + "str-buf", +] + +[[package]] +name = "fd-lock" +version = "3.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a16910e685088843d53132b04e0f10a571fdb193224fc589685b3ba1ce4cb03d" +dependencies = [ + "cfg-if", + "libc", + "windows-sys", +] + [[package]] name = "fixedbitset" version = "0.2.0" @@ -186,9 +247,12 @@ name = "handball" version = "0.1.0" dependencies = [ "chumsky", + "debug2", "lalrpop", "lalrpop-util", "logos", + "rustyline", + "rustyline-derive", ] [[package]] @@ -326,12 +390,43 @@ version = "2.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" +[[package]] +name = "memoffset" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5aa361d4faea93603064a027415f07bd8e1d5c88c9fbf68bf56a285428fd79ce" +dependencies = [ + "autocfg", +] + [[package]] name = "new_debug_unreachable" version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e4a24736216ec316047a1fc4252e27dabb04218aa4a3f37c6e7ddbf1f9782b54" +[[package]] +name = "nibble_vec" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77a5d83df9f36fe23f0c3648c6bbb8b0298bb5f1939c8f2704431371f4b84d43" +dependencies = [ + "smallvec", +] + +[[package]] +name = "nix" +version = "0.23.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f305c2c2e4c39a82f7bf0bf65fb557f9070ce06781d4f2454295cc34b1c43188" +dependencies = [ + "bitflags", + "cc", + "cfg-if", + "libc", + "memoffset", +] + [[package]] name = "parking_lot" version = "0.11.2" @@ -412,6 +507,16 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "radix_trie" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c069c179fcdc6a2fe24d8d18305cf085fdbd4f922c041943e203685d6a1c58fd" +dependencies = [ + "endian-type", + "nibble_vec", +] + [[package]] name = "redox_syscall" version = "0.2.10" @@ -454,6 +559,40 @@ version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "61b3909d758bb75c79f23d4736fac9433868679d3ad2ea7a61e3c25cfda9a088" +[[package]] +name = "rustyline" +version = "9.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c38cfbd0a4d7df7aab7cf53732d5d43449d0300358fd15cd4e8c8468a956aca" +dependencies = [ + "bitflags", + "cfg-if", + "clipboard-win", + "dirs-next", + "fd-lock", + "libc", + "log", + "memchr", + "nix", + "radix_trie", + "scopeguard", + "smallvec", + "unicode-segmentation", + "unicode-width", + "utf8parse", + "winapi", +] + +[[package]] +name = "rustyline-derive" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb35a55ab810b5c0fe31606fe9b47d1354e4dc519bec0a102655f78ea2b38057" +dependencies = [ + "quote", + "syn", +] + [[package]] name = "scopeguard" version = "1.1.0" @@ -472,6 +611,12 @@ version = "1.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1ecab6c735a6bb4139c0caafd0cc3635748bbb3acf4550e8138122099251f309" +[[package]] +name = "str-buf" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d44a3643b4ff9caf57abcee9c2c621d6c03d9135e0d8b589bd9afb5992cb176a" + [[package]] name = "string_cache" version = "0.8.2" @@ -496,6 +641,18 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "synstructure" +version = "0.12.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f36bdaa60a83aca3921b5259d5400cbf5e90fc51931376a9bd4a0eb79aa7210f" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "unicode-xid", +] + [[package]] name = "term" version = "0.7.0" @@ -516,6 +673,18 @@ dependencies = [ "crunchy", ] +[[package]] +name = "unicode-segmentation" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8895849a949e7845e06bd6dc1aa51731a103c42707010a5b591c0038fb73385b" + +[[package]] +name = "unicode-width" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973" + [[package]] name = "unicode-xid" version = "0.2.2" @@ -528,6 +697,12 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b4ae116fef2b7fea257ed6440d3cfcff7f190865f170cdad00bb6465bf18ecba" +[[package]] +name = "utf8parse" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "936e4b492acfd135421d8dca4b1aa80a7bfc26e702ef3af710e0752684df5372" + [[package]] name = "wasi" version = "0.10.2+wasi-snapshot-preview1" @@ -555,3 +730,46 @@ name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows-sys" +version = "0.28.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82ca39602d5cbfa692c4b67e3bcbb2751477355141c1ed434c94da4186836ff6" +dependencies = [ + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_msvc" +version = "0.28.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "52695a41e536859d5308cc613b4a022261a274390b25bd29dfff4bf08505f3c2" + +[[package]] +name = "windows_i686_gnu" +version = "0.28.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f54725ac23affef038fecb177de6c9bf065787c2f432f79e3c373da92f3e1d8a" + +[[package]] +name = "windows_i686_msvc" +version = "0.28.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51d5158a43cc43623c0729d1ad6647e62fa384a3d135fd15108d37c683461f64" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.28.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc31f409f565611535130cfe7ee8e6655d3fa99c1c61013981e491921b5ce954" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.28.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f2b8c7cbd3bfdddd9ab98769f9746a7fad1bca236554cd032b78d768bc0e89f" diff --git a/Cargo.toml b/Cargo.toml index a14c188..cc636b5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,8 +7,11 @@ edition = "2021" [dependencies] chumsky = "0.6.0" -lalrpop-util = "0.19.6" +lalrpop-util = { version = "0.19.6", features = ["lexer"] } logos = "0.12.0" +debug2 = {path="../debug2"} +rustyline = "9.1.1" +rustyline-derive = "0.6.0" [build-dependencies] lalrpop = "0.19.6" diff --git a/src/grammar.lalrpop b/src/grammar.lalrpop index ce98100..b44909a 100644 --- a/src/grammar.lalrpop +++ b/src/grammar.lalrpop @@ -1,33 +1,12 @@ -use crate::{Token, Tree, Val}; - +use crate::Tree; grammar<'s>(); -pub(crate) Trees = Tree*; - -Tree: Tree<'s> = { - "(" ")" => Tree::Branch(<>), - Val => Tree::Leaf(<>) - +pub Tree: Tree = { + "(" "+" ")" => Tree::Add(<>), + "(" "-" ")" => Tree::Sub(<>), + "(" "*" ")" => Tree::Mul(<>), + "(" "/" ")" => Tree::Div(<>), + r"[0-9]+(\.[0-9]+)?" => Tree::Val(<>.parse().unwrap()), } - -Val: Val<'s> = { - "@int" => Val::Int(<>), - "@float" => Val::Float(<>), - "@id" => Val::Ident(<>), -} - - -extern { - type Location = usize; - type Error = crate::LexError; - - enum Token<'s> { - "(" => Token::Lparen, - ")" => Token::Rparen, - "@id" => Token::Ident(<&'s str>), - "@int" => Token::Int(), - "@float" => Token::Float(), - } -} \ No newline at end of file diff --git a/src/grammar.rs b/src/grammar.rs index 4d9370f..0675b2f 100644 --- a/src/grammar.rs +++ b/src/grammar.rs @@ -1,6 +1,6 @@ // auto-generated: "lalrpop 0.19.6" -// sha3: b4e129326576b92bdcb244ce79862d1fd8d25830fd43a283867e772251d1f229 -use crate::{Token, Tree, Val}; +// sha3: df1d4c73d92562a3473f57d776bdf582379e4cd2914b23c96b97e851e4c77 +use crate::Tree; #[allow(unused_extern_crates)] extern crate lalrpop_util as __lalrpop_util; #[allow(unused_imports)] @@ -9,95 +9,124 @@ extern crate core; extern crate alloc; #[cfg_attr(rustfmt, rustfmt_skip)] -mod __parse__Trees { +mod __parse__Tree { #![allow(non_snake_case, non_camel_case_types, unused_mut, unused_variables, unused_imports, unused_parens)] - use crate::{Token, Tree, Val}; + use crate::Tree; #[allow(unused_extern_crates)] extern crate lalrpop_util as __lalrpop_util; #[allow(unused_imports)] use self::__lalrpop_util::state_machine as __state_machine; extern crate core; extern crate alloc; - use super::__ToTriple; + use self::__lalrpop_util::lexer::Token; #[allow(dead_code)] - pub(crate) enum __Symbol<'s> + pub(crate) enum __Symbol<'input> { - Variant0(Token<'s>), - Variant1(f64), - Variant2(&'s str), - Variant3(i64), - Variant4(Tree<'s>), - Variant5(alloc::vec::Vec>), - Variant6(Val<'s>), + Variant0(&'input str), + Variant1(Tree), + Variant2(alloc::vec::Vec), } const __ACTION: &[i8] = &[ // State 0 - 3, 0, 7, 8, 9, + 11, 0, 0, 0, 0, 0, 12, // State 1 - 3, -8, 7, 8, 9, + 11, 14, 0, 0, 0, 0, 12, // State 2 - 3, -7, 7, 8, 9, + 11, 15, 0, 0, 0, 0, 12, // State 3 - -5, -5, -5, -5, -5, + 11, 0, 0, 0, 0, 0, 12, // State 4 - 0, 0, 0, 0, 0, + 11, 0, 0, 0, 0, 0, 12, // State 5 - -2, -2, -2, -2, -2, + 11, 17, 0, 0, 0, 0, 12, // State 6 - -10, -10, -10, -10, -10, + 11, 18, 0, 0, 0, 0, 12, // State 7 - -11, -11, -11, -11, -11, + 11, 19, 0, 0, 0, 0, 12, // State 8 - -9, -9, -9, -9, -9, + 11, 20, 0, 0, 0, 0, 12, // State 9 - -6, -6, -6, -6, -6, + 0, 0, 0, 0, 0, 0, 0, // State 10 - 0, 12, 0, 0, 0, + 0, 0, 2, 3, 4, 5, 0, // State 11 - -1, -1, -1, -1, -1, + -7, -7, 0, 0, 0, 0, -7, + // State 12 + -10, -10, 0, 0, 0, 0, -10, + // State 13 + -4, -4, 0, 0, 0, 0, -4, + // State 14 + -1, -1, 0, 0, 0, 0, -1, + // State 15 + -11, -11, 0, 0, 0, 0, -11, + // State 16 + -5, -5, 0, 0, 0, 0, -5, + // State 17 + -2, -2, 0, 0, 0, 0, -2, + // State 18 + -3, -3, 0, 0, 0, 0, -3, + // State 19 + -6, -6, 0, 0, 0, 0, -6, ]; fn __action(state: i8, integer: usize) -> i8 { - __ACTION[(state as usize) * 5 + integer] + __ACTION[(state as usize) * 7 + integer] } const __EOF_ACTION: &[i8] = &[ // State 0 - -7, + 0, // State 1 - -8, + 0, // State 2 0, // State 3 - -5, + 0, // State 4 - -12, + 0, // State 5 - -2, + 0, // State 6 - -10, + 0, // State 7 - -11, + 0, // State 8 - -9, + 0, // State 9 - -6, + -12, // State 10 0, // State 11 + -7, + // State 12 + 0, + // State 13 + -4, + // State 14 -1, + // State 15 + 0, + // State 16 + -5, + // State 17 + -2, + // State 18 + -3, + // State 19 + -6, ]; fn __goto(state: i8, nt: usize) -> i8 { match nt { 0 => match state { - 1 => 9, - _ => 3, + 1..=4 => 12, + 5..=8 => 15, + _ => 9, }, - 2 => 1, - 3 => match state { - 2 => 10, - _ => 4, + 2 => match state { + 2 => 6, + 3 => 7, + 4 => 8, + _ => 5, }, - 4 => 5, _ => 0, } } @@ -105,9 +134,11 @@ mod __parse__Trees { const __TERMINAL: &[&str] = &[ r###""(""###, r###"")""###, - r###""@float""###, - r###""@id""###, - r###""@int""###, + r###""*""###, + r###""+""###, + r###""-""###, + r###""/""###, + r###"r#"[0-9]+(\\.[0-9]+)?"#"###, ]; __TERMINAL.iter().enumerate().filter_map(|(index, terminal)| { let next_state = __action(__state, index); @@ -118,20 +149,21 @@ mod __parse__Trees { } }).collect() } - pub(crate) struct __StateMachine<'s> + pub(crate) struct __StateMachine<'input, 's> where { - __phantom: core::marker::PhantomData<(&'s ())>, + input: &'input str, + __phantom: core::marker::PhantomData<(&'input (), &'s ())>, } - impl<'s> __state_machine::ParserDefinition for __StateMachine<'s> + impl<'input, 's> __state_machine::ParserDefinition for __StateMachine<'input, 's> where { type Location = usize; - type Error = crate::LexError; - type Token = Token<'s>; + type Error = &'static str; + type Token = Token<'input>; type TokenIndex = usize; - type Symbol = __Symbol<'s>; - type Success = alloc::vec::Vec>; + type Symbol = __Symbol<'input>; + type Success = Tree; type StateIndex = i8; type Action = i8; type ReduceIndex = i8; @@ -149,7 +181,7 @@ mod __parse__Trees { #[inline] fn token_to_index(&self, token: &Self::Token) -> Option { - __token_to_integer(token, core::marker::PhantomData::<(&())>) + __token_to_integer(token, core::marker::PhantomData::<(&(), &())>) } #[inline] @@ -159,7 +191,7 @@ mod __parse__Trees { #[inline] fn error_action(&self, state: i8) -> i8 { - __action(state, 5 - 1) + __action(state, 7 - 1) } #[inline] @@ -173,7 +205,7 @@ mod __parse__Trees { } fn token_to_symbol(&self, token_index: usize, token: Self::Token) -> Self::Symbol { - __token_to_symbol(token_index, token, core::marker::PhantomData::<(&())>) + __token_to_symbol(token_index, token, core::marker::PhantomData::<(&(), &())>) } fn expected_tokens(&self, state: i8) -> alloc::vec::Vec { @@ -201,11 +233,12 @@ mod __parse__Trees { symbols: &mut alloc::vec::Vec<__state_machine::SymbolTriple>, ) -> Option<__state_machine::ParseResult> { __reduce( + self.input, action, start_location, states, symbols, - core::marker::PhantomData::<(&())>, + core::marker::PhantomData::<(&(), &())>, ) } @@ -214,127 +247,126 @@ mod __parse__Trees { } } fn __token_to_integer< + 'input, 's, >( - __token: &Token<'s>, - _: core::marker::PhantomData<(&'s ())>, + __token: &Token<'input>, + _: core::marker::PhantomData<(&'input (), &'s ())>, ) -> Option { match *__token { - Token::Lparen if true => Some(0), - Token::Rparen if true => Some(1), - Token::Float(_) if true => Some(2), - Token::Ident(_) if true => Some(3), - Token::Int(_) if true => Some(4), + Token(1, _) if true => Some(0), + Token(2, _) if true => Some(1), + Token(3, _) if true => Some(2), + Token(4, _) if true => Some(3), + Token(5, _) if true => Some(4), + Token(6, _) if true => Some(5), + Token(0, _) if true => Some(6), _ => None, } } fn __token_to_symbol< + 'input, 's, >( __token_index: usize, - __token: Token<'s>, - _: core::marker::PhantomData<(&'s ())>, - ) -> __Symbol<'s> + __token: Token<'input>, + _: core::marker::PhantomData<(&'input (), &'s ())>, + ) -> __Symbol<'input> { match __token_index { - 0 | 1 => __Symbol::Variant0(__token), - 2 => match __token { - Token::Float(__tok0) if true => __Symbol::Variant1(__tok0), - _ => unreachable!(), - }, - 3 => match __token { - Token::Ident(__tok0) if true => __Symbol::Variant2(__tok0), - _ => unreachable!(), - }, - 4 => match __token { - Token::Int(__tok0) if true => __Symbol::Variant3(__tok0), + 0 | 1 | 2 | 3 | 4 | 5 | 6 => match __token { + Token(1, __tok0) | Token(2, __tok0) | Token(3, __tok0) | Token(4, __tok0) | Token(5, __tok0) | Token(6, __tok0) | Token(0, __tok0) if true => __Symbol::Variant0(__tok0), _ => unreachable!(), }, _ => unreachable!(), } } - pub(crate) struct TreesParser { + pub struct TreeParser { + builder: __lalrpop_util::lexer::MatcherBuilder, _priv: (), } - impl TreesParser { - pub(crate) fn new() -> TreesParser { - TreesParser { + impl TreeParser { + pub fn new() -> TreeParser { + let __builder = super::__intern_token::new_builder(); + TreeParser { + builder: __builder, _priv: (), } } #[allow(dead_code)] - pub(crate) fn parse< + pub fn parse< + 'input, 's, - __TOKEN: __ToTriple<'s, >, - __TOKENS: IntoIterator, >( &self, - __tokens0: __TOKENS, - ) -> Result>, __lalrpop_util::ParseError, crate::LexError>> + input: &'input str, + ) -> Result, &'static str>> { - let __tokens = __tokens0.into_iter(); - let mut __tokens = __tokens.map(|t| __ToTriple::to_triple(t)); + let mut __tokens = self.builder.matcher(input); __state_machine::Parser::drive( __StateMachine { - __phantom: core::marker::PhantomData::<(&())>, + input, + __phantom: core::marker::PhantomData::<(&(), &())>, }, __tokens, ) } } pub(crate) fn __reduce< + 'input, 's, >( + input: &'input str, __action: i8, __lookahead_start: Option<&usize>, __states: &mut alloc::vec::Vec, - __symbols: &mut alloc::vec::Vec<(usize,__Symbol<'s>,usize)>, - _: core::marker::PhantomData<(&'s ())>, - ) -> Option>,__lalrpop_util::ParseError, crate::LexError>>> + __symbols: &mut alloc::vec::Vec<(usize,__Symbol<'input>,usize)>, + _: core::marker::PhantomData<(&'input (), &'s ())>, + ) -> Option, &'static str>>> { let (__pop_states, __nonterminal) = match __action { 0 => { - __reduce0(__lookahead_start, __symbols, core::marker::PhantomData::<(&())>) + __reduce0(input, __lookahead_start, __symbols, core::marker::PhantomData::<(&(), &())>) } 1 => { - __reduce1(__lookahead_start, __symbols, core::marker::PhantomData::<(&())>) + __reduce1(input, __lookahead_start, __symbols, core::marker::PhantomData::<(&(), &())>) } 2 => { - __reduce2(__lookahead_start, __symbols, core::marker::PhantomData::<(&())>) + __reduce2(input, __lookahead_start, __symbols, core::marker::PhantomData::<(&(), &())>) } 3 => { - __reduce3(__lookahead_start, __symbols, core::marker::PhantomData::<(&())>) + __reduce3(input, __lookahead_start, __symbols, core::marker::PhantomData::<(&(), &())>) } 4 => { - __reduce4(__lookahead_start, __symbols, core::marker::PhantomData::<(&())>) + __reduce4(input, __lookahead_start, __symbols, core::marker::PhantomData::<(&(), &())>) } 5 => { - __reduce5(__lookahead_start, __symbols, core::marker::PhantomData::<(&())>) + __reduce5(input, __lookahead_start, __symbols, core::marker::PhantomData::<(&(), &())>) } 6 => { - __reduce6(__lookahead_start, __symbols, core::marker::PhantomData::<(&())>) + __reduce6(input, __lookahead_start, __symbols, core::marker::PhantomData::<(&(), &())>) } 7 => { - __reduce7(__lookahead_start, __symbols, core::marker::PhantomData::<(&())>) + __reduce7(input, __lookahead_start, __symbols, core::marker::PhantomData::<(&(), &())>) } 8 => { - __reduce8(__lookahead_start, __symbols, core::marker::PhantomData::<(&())>) + __reduce8(input, __lookahead_start, __symbols, core::marker::PhantomData::<(&(), &())>) } 9 => { - __reduce9(__lookahead_start, __symbols, core::marker::PhantomData::<(&())>) + __reduce9(input, __lookahead_start, __symbols, core::marker::PhantomData::<(&(), &())>) } 10 => { - __reduce10(__lookahead_start, __symbols, core::marker::PhantomData::<(&())>) + __reduce10(input, __lookahead_start, __symbols, core::marker::PhantomData::<(&(), &())>) } 11 => { - // __Trees = Trees => ActionFn(0); - let __sym0 = __pop_Variant5(__symbols); + // __Tree = Tree => ActionFn(0); + let __sym0 = __pop_Variant1(__symbols); let __start = __sym0.0.clone(); let __end = __sym0.2.clone(); - let __nt = super::__action0::<>(__sym0); + let __nt = super::__action0::<>(input, __sym0); return Some(Ok(__nt)); } _ => panic!("invalid action code {}", __action) @@ -350,415 +382,545 @@ mod __parse__Trees { fn __symbol_type_mismatch() -> ! { panic!("symbol type mismatch") } - fn __pop_Variant0< - 's, - >( - __symbols: &mut alloc::vec::Vec<(usize,__Symbol<'s>,usize)> - ) -> (usize, Token<'s>, usize) - { - match __symbols.pop() { - Some((__l, __Symbol::Variant0(__v), __r)) => (__l, __v, __r), - _ => __symbol_type_mismatch() - } - } - fn __pop_Variant4< - 's, - >( - __symbols: &mut alloc::vec::Vec<(usize,__Symbol<'s>,usize)> - ) -> (usize, Tree<'s>, usize) - { - match __symbols.pop() { - Some((__l, __Symbol::Variant4(__v), __r)) => (__l, __v, __r), - _ => __symbol_type_mismatch() - } - } - fn __pop_Variant6< - 's, - >( - __symbols: &mut alloc::vec::Vec<(usize,__Symbol<'s>,usize)> - ) -> (usize, Val<'s>, usize) - { - match __symbols.pop() { - Some((__l, __Symbol::Variant6(__v), __r)) => (__l, __v, __r), - _ => __symbol_type_mismatch() - } - } - fn __pop_Variant5< - 's, - >( - __symbols: &mut alloc::vec::Vec<(usize,__Symbol<'s>,usize)> - ) -> (usize, alloc::vec::Vec>, usize) - { - match __symbols.pop() { - Some((__l, __Symbol::Variant5(__v), __r)) => (__l, __v, __r), - _ => __symbol_type_mismatch() - } - } fn __pop_Variant1< - 's, + 'input, >( - __symbols: &mut alloc::vec::Vec<(usize,__Symbol<'s>,usize)> - ) -> (usize, f64, usize) + __symbols: &mut alloc::vec::Vec<(usize,__Symbol<'input>,usize)> + ) -> (usize, Tree, usize) { match __symbols.pop() { Some((__l, __Symbol::Variant1(__v), __r)) => (__l, __v, __r), _ => __symbol_type_mismatch() } } - fn __pop_Variant3< - 's, - >( - __symbols: &mut alloc::vec::Vec<(usize,__Symbol<'s>,usize)> - ) -> (usize, i64, usize) - { - match __symbols.pop() { - Some((__l, __Symbol::Variant3(__v), __r)) => (__l, __v, __r), - _ => __symbol_type_mismatch() - } - } fn __pop_Variant2< - 's, + 'input, >( - __symbols: &mut alloc::vec::Vec<(usize,__Symbol<'s>,usize)> - ) -> (usize, &'s str, usize) + __symbols: &mut alloc::vec::Vec<(usize,__Symbol<'input>,usize)> + ) -> (usize, alloc::vec::Vec, usize) { match __symbols.pop() { Some((__l, __Symbol::Variant2(__v), __r)) => (__l, __v, __r), _ => __symbol_type_mismatch() } } + fn __pop_Variant0< + 'input, + >( + __symbols: &mut alloc::vec::Vec<(usize,__Symbol<'input>,usize)> + ) -> (usize, &'input str, usize) + { + match __symbols.pop() { + Some((__l, __Symbol::Variant0(__v), __r)) => (__l, __v, __r), + _ => __symbol_type_mismatch() + } + } pub(crate) fn __reduce0< + 'input, 's, >( + input: &'input str, __lookahead_start: Option<&usize>, - __symbols: &mut alloc::vec::Vec<(usize,__Symbol<'s>,usize)>, - _: core::marker::PhantomData<(&'s ())>, + __symbols: &mut alloc::vec::Vec<(usize,__Symbol<'input>,usize)>, + _: core::marker::PhantomData<(&'input (), &'s ())>, ) -> (usize, usize) { - // Tree = "(", Trees, ")" => ActionFn(2); + // Tree = "(", "+", ")" => ActionFn(10); assert!(__symbols.len() >= 3); let __sym2 = __pop_Variant0(__symbols); - let __sym1 = __pop_Variant5(__symbols); + let __sym1 = __pop_Variant0(__symbols); let __sym0 = __pop_Variant0(__symbols); let __start = __sym0.0.clone(); let __end = __sym2.2.clone(); - let __nt = super::__action2::<>(__sym0, __sym1, __sym2); - __symbols.push((__start, __Symbol::Variant4(__nt), __end)); + let __nt = super::__action10::<>(input, __sym0, __sym1, __sym2); + __symbols.push((__start, __Symbol::Variant1(__nt), __end)); (3, 0) } pub(crate) fn __reduce1< + 'input, 's, >( + input: &'input str, __lookahead_start: Option<&usize>, - __symbols: &mut alloc::vec::Vec<(usize,__Symbol<'s>,usize)>, - _: core::marker::PhantomData<(&'s ())>, + __symbols: &mut alloc::vec::Vec<(usize,__Symbol<'input>,usize)>, + _: core::marker::PhantomData<(&'input (), &'s ())>, ) -> (usize, usize) { - // Tree = Val => ActionFn(3); - let __sym0 = __pop_Variant6(__symbols); + // Tree = "(", "+", Tree+, ")" => ActionFn(11); + assert!(__symbols.len() >= 4); + let __sym3 = __pop_Variant0(__symbols); + let __sym2 = __pop_Variant2(__symbols); + let __sym1 = __pop_Variant0(__symbols); + let __sym0 = __pop_Variant0(__symbols); let __start = __sym0.0.clone(); - let __end = __sym0.2.clone(); - let __nt = super::__action3::<>(__sym0); - __symbols.push((__start, __Symbol::Variant4(__nt), __end)); - (1, 0) + let __end = __sym3.2.clone(); + let __nt = super::__action11::<>(input, __sym0, __sym1, __sym2, __sym3); + __symbols.push((__start, __Symbol::Variant1(__nt), __end)); + (4, 0) } pub(crate) fn __reduce2< + 'input, 's, >( + input: &'input str, __lookahead_start: Option<&usize>, - __symbols: &mut alloc::vec::Vec<(usize,__Symbol<'s>,usize)>, - _: core::marker::PhantomData<(&'s ())>, + __symbols: &mut alloc::vec::Vec<(usize,__Symbol<'input>,usize)>, + _: core::marker::PhantomData<(&'input (), &'s ())>, ) -> (usize, usize) { - // Tree* = => ActionFn(7); - let __start = __lookahead_start.cloned().or_else(|| __symbols.last().map(|s| s.2.clone())).unwrap_or_default(); - let __end = __start.clone(); - let __nt = super::__action7::<>(&__start, &__end); - __symbols.push((__start, __Symbol::Variant5(__nt), __end)); - (0, 1) + // Tree = "(", "-", Tree+, ")" => ActionFn(2); + assert!(__symbols.len() >= 4); + let __sym3 = __pop_Variant0(__symbols); + let __sym2 = __pop_Variant2(__symbols); + let __sym1 = __pop_Variant0(__symbols); + let __sym0 = __pop_Variant0(__symbols); + let __start = __sym0.0.clone(); + let __end = __sym3.2.clone(); + let __nt = super::__action2::<>(input, __sym0, __sym1, __sym2, __sym3); + __symbols.push((__start, __Symbol::Variant1(__nt), __end)); + (4, 0) } pub(crate) fn __reduce3< + 'input, 's, >( + input: &'input str, __lookahead_start: Option<&usize>, - __symbols: &mut alloc::vec::Vec<(usize,__Symbol<'s>,usize)>, - _: core::marker::PhantomData<(&'s ())>, + __symbols: &mut alloc::vec::Vec<(usize,__Symbol<'input>,usize)>, + _: core::marker::PhantomData<(&'input (), &'s ())>, ) -> (usize, usize) { - // Tree* = Tree+ => ActionFn(8); - let __sym0 = __pop_Variant5(__symbols); + // Tree = "(", "*", ")" => ActionFn(12); + assert!(__symbols.len() >= 3); + let __sym2 = __pop_Variant0(__symbols); + let __sym1 = __pop_Variant0(__symbols); + let __sym0 = __pop_Variant0(__symbols); let __start = __sym0.0.clone(); - let __end = __sym0.2.clone(); - let __nt = super::__action8::<>(__sym0); - __symbols.push((__start, __Symbol::Variant5(__nt), __end)); - (1, 1) + let __end = __sym2.2.clone(); + let __nt = super::__action12::<>(input, __sym0, __sym1, __sym2); + __symbols.push((__start, __Symbol::Variant1(__nt), __end)); + (3, 0) } pub(crate) fn __reduce4< + 'input, 's, >( + input: &'input str, __lookahead_start: Option<&usize>, - __symbols: &mut alloc::vec::Vec<(usize,__Symbol<'s>,usize)>, - _: core::marker::PhantomData<(&'s ())>, + __symbols: &mut alloc::vec::Vec<(usize,__Symbol<'input>,usize)>, + _: core::marker::PhantomData<(&'input (), &'s ())>, ) -> (usize, usize) { - // Tree+ = Tree => ActionFn(9); - let __sym0 = __pop_Variant4(__symbols); + // Tree = "(", "*", Tree+, ")" => ActionFn(13); + assert!(__symbols.len() >= 4); + let __sym3 = __pop_Variant0(__symbols); + let __sym2 = __pop_Variant2(__symbols); + let __sym1 = __pop_Variant0(__symbols); + let __sym0 = __pop_Variant0(__symbols); let __start = __sym0.0.clone(); - let __end = __sym0.2.clone(); - let __nt = super::__action9::<>(__sym0); - __symbols.push((__start, __Symbol::Variant5(__nt), __end)); - (1, 2) + let __end = __sym3.2.clone(); + let __nt = super::__action13::<>(input, __sym0, __sym1, __sym2, __sym3); + __symbols.push((__start, __Symbol::Variant1(__nt), __end)); + (4, 0) } pub(crate) fn __reduce5< + 'input, 's, >( + input: &'input str, __lookahead_start: Option<&usize>, - __symbols: &mut alloc::vec::Vec<(usize,__Symbol<'s>,usize)>, - _: core::marker::PhantomData<(&'s ())>, + __symbols: &mut alloc::vec::Vec<(usize,__Symbol<'input>,usize)>, + _: core::marker::PhantomData<(&'input (), &'s ())>, ) -> (usize, usize) { - // Tree+ = Tree+, Tree => ActionFn(10); - assert!(__symbols.len() >= 2); - let __sym1 = __pop_Variant4(__symbols); - let __sym0 = __pop_Variant5(__symbols); + // Tree = "(", "/", Tree+, ")" => ActionFn(4); + assert!(__symbols.len() >= 4); + let __sym3 = __pop_Variant0(__symbols); + let __sym2 = __pop_Variant2(__symbols); + let __sym1 = __pop_Variant0(__symbols); + let __sym0 = __pop_Variant0(__symbols); let __start = __sym0.0.clone(); - let __end = __sym1.2.clone(); - let __nt = super::__action10::<>(__sym0, __sym1); - __symbols.push((__start, __Symbol::Variant5(__nt), __end)); - (2, 2) + let __end = __sym3.2.clone(); + let __nt = super::__action4::<>(input, __sym0, __sym1, __sym2, __sym3); + __symbols.push((__start, __Symbol::Variant1(__nt), __end)); + (4, 0) } pub(crate) fn __reduce6< + 'input, 's, >( + input: &'input str, __lookahead_start: Option<&usize>, - __symbols: &mut alloc::vec::Vec<(usize,__Symbol<'s>,usize)>, - _: core::marker::PhantomData<(&'s ())>, + __symbols: &mut alloc::vec::Vec<(usize,__Symbol<'input>,usize)>, + _: core::marker::PhantomData<(&'input (), &'s ())>, ) -> (usize, usize) { - // Trees = => ActionFn(11); - let __start = __lookahead_start.cloned().or_else(|| __symbols.last().map(|s| s.2.clone())).unwrap_or_default(); - let __end = __start.clone(); - let __nt = super::__action11::<>(&__start, &__end); - __symbols.push((__start, __Symbol::Variant5(__nt), __end)); - (0, 3) + // Tree = r#"[0-9]+(\\.[0-9]+)?"# => ActionFn(5); + let __sym0 = __pop_Variant0(__symbols); + let __start = __sym0.0.clone(); + let __end = __sym0.2.clone(); + let __nt = super::__action5::<>(input, __sym0); + __symbols.push((__start, __Symbol::Variant1(__nt), __end)); + (1, 0) } pub(crate) fn __reduce7< + 'input, 's, >( + input: &'input str, __lookahead_start: Option<&usize>, - __symbols: &mut alloc::vec::Vec<(usize,__Symbol<'s>,usize)>, - _: core::marker::PhantomData<(&'s ())>, + __symbols: &mut alloc::vec::Vec<(usize,__Symbol<'input>,usize)>, + _: core::marker::PhantomData<(&'input (), &'s ())>, ) -> (usize, usize) { - // Trees = Tree+ => ActionFn(12); - let __sym0 = __pop_Variant5(__symbols); - let __start = __sym0.0.clone(); - let __end = __sym0.2.clone(); - let __nt = super::__action12::<>(__sym0); - __symbols.push((__start, __Symbol::Variant5(__nt), __end)); - (1, 3) + // Tree* = => ActionFn(8); + let __start = __lookahead_start.cloned().or_else(|| __symbols.last().map(|s| s.2.clone())).unwrap_or_default(); + let __end = __start.clone(); + let __nt = super::__action8::<>(input, &__start, &__end); + __symbols.push((__start, __Symbol::Variant2(__nt), __end)); + (0, 1) } pub(crate) fn __reduce8< + 'input, 's, >( + input: &'input str, __lookahead_start: Option<&usize>, - __symbols: &mut alloc::vec::Vec<(usize,__Symbol<'s>,usize)>, - _: core::marker::PhantomData<(&'s ())>, + __symbols: &mut alloc::vec::Vec<(usize,__Symbol<'input>,usize)>, + _: core::marker::PhantomData<(&'input (), &'s ())>, ) -> (usize, usize) { - // Val = "@int" => ActionFn(4); - let __sym0 = __pop_Variant3(__symbols); - let __start = __sym0.0.clone(); - let __end = __sym0.2.clone(); - let __nt = super::__action4::<>(__sym0); - __symbols.push((__start, __Symbol::Variant6(__nt), __end)); - (1, 4) - } - pub(crate) fn __reduce9< - 's, - >( - __lookahead_start: Option<&usize>, - __symbols: &mut alloc::vec::Vec<(usize,__Symbol<'s>,usize)>, - _: core::marker::PhantomData<(&'s ())>, - ) -> (usize, usize) - { - // Val = "@float" => ActionFn(5); - let __sym0 = __pop_Variant1(__symbols); - let __start = __sym0.0.clone(); - let __end = __sym0.2.clone(); - let __nt = super::__action5::<>(__sym0); - __symbols.push((__start, __Symbol::Variant6(__nt), __end)); - (1, 4) - } - pub(crate) fn __reduce10< - 's, - >( - __lookahead_start: Option<&usize>, - __symbols: &mut alloc::vec::Vec<(usize,__Symbol<'s>,usize)>, - _: core::marker::PhantomData<(&'s ())>, - ) -> (usize, usize) - { - // Val = "@id" => ActionFn(6); + // Tree* = Tree+ => ActionFn(9); let __sym0 = __pop_Variant2(__symbols); let __start = __sym0.0.clone(); let __end = __sym0.2.clone(); - let __nt = super::__action6::<>(__sym0); - __symbols.push((__start, __Symbol::Variant6(__nt), __end)); - (1, 4) + let __nt = super::__action9::<>(input, __sym0); + __symbols.push((__start, __Symbol::Variant2(__nt), __end)); + (1, 1) + } + pub(crate) fn __reduce9< + 'input, + 's, + >( + input: &'input str, + __lookahead_start: Option<&usize>, + __symbols: &mut alloc::vec::Vec<(usize,__Symbol<'input>,usize)>, + _: core::marker::PhantomData<(&'input (), &'s ())>, + ) -> (usize, usize) + { + // Tree+ = Tree => ActionFn(6); + let __sym0 = __pop_Variant1(__symbols); + let __start = __sym0.0.clone(); + let __end = __sym0.2.clone(); + let __nt = super::__action6::<>(input, __sym0); + __symbols.push((__start, __Symbol::Variant2(__nt), __end)); + (1, 2) + } + pub(crate) fn __reduce10< + 'input, + 's, + >( + input: &'input str, + __lookahead_start: Option<&usize>, + __symbols: &mut alloc::vec::Vec<(usize,__Symbol<'input>,usize)>, + _: core::marker::PhantomData<(&'input (), &'s ())>, + ) -> (usize, usize) + { + // Tree+ = Tree+, Tree => ActionFn(7); + assert!(__symbols.len() >= 2); + let __sym1 = __pop_Variant1(__symbols); + let __sym0 = __pop_Variant2(__symbols); + let __start = __sym0.0.clone(); + let __end = __sym1.2.clone(); + let __nt = super::__action7::<>(input, __sym0, __sym1); + __symbols.push((__start, __Symbol::Variant2(__nt), __end)); + (2, 2) } } -pub(crate) use self::__parse__Trees::TreesParser; +pub use self::__parse__Tree::TreeParser; +#[cfg_attr(rustfmt, rustfmt_skip)] +mod __intern_token { + #![allow(unused_imports)] + use crate::Tree; + #[allow(unused_extern_crates)] + extern crate lalrpop_util as __lalrpop_util; + #[allow(unused_imports)] + use self::__lalrpop_util::state_machine as __state_machine; + extern crate core; + extern crate alloc; + pub fn new_builder() -> __lalrpop_util::lexer::MatcherBuilder { + let __strs: &[(&str, bool)] = &[ + ("^([0-9]+(\\.[0-9]+)?)", false), + ("^(\\()", false), + ("^(\\))", false), + ("^(\\*)", false), + ("^(\\+)", false), + ("^(\\-)", false), + ("^(/)", false), + (r"^(\s*)", true), + ]; + __lalrpop_util::lexer::MatcherBuilder::new(__strs.iter().copied()).unwrap() + } +} +pub(crate) use self::__lalrpop_util::lexer::Token; +#[allow(unused_variables)] fn __action0< + 'input, 's, >( - (_, __0, _): (usize, alloc::vec::Vec>, usize), -) -> alloc::vec::Vec> + input: &'input str, + (_, __0, _): (usize, Tree, usize), +) -> Tree { __0 } +#[allow(unused_variables)] fn __action1< + 'input, 's, >( - (_, __0, _): (usize, alloc::vec::Vec>, usize), -) -> alloc::vec::Vec> + input: &'input str, + (_, _, _): (usize, &'input str, usize), + (_, _, _): (usize, &'input str, usize), + (_, __0, _): (usize, alloc::vec::Vec, usize), + (_, _, _): (usize, &'input str, usize), +) -> Tree { - __0 + Tree::Add(__0) } +#[allow(unused_variables)] fn __action2< + 'input, 's, >( - (_, _, _): (usize, Token<'s>, usize), - (_, __0, _): (usize, alloc::vec::Vec>, usize), - (_, _, _): (usize, Token<'s>, usize), -) -> Tree<'s> + input: &'input str, + (_, _, _): (usize, &'input str, usize), + (_, _, _): (usize, &'input str, usize), + (_, __0, _): (usize, alloc::vec::Vec, usize), + (_, _, _): (usize, &'input str, usize), +) -> Tree { - Tree::Branch(__0) + Tree::Sub(__0) } +#[allow(unused_variables)] fn __action3< + 'input, 's, >( - (_, __0, _): (usize, Val<'s>, usize), -) -> Tree<'s> + input: &'input str, + (_, _, _): (usize, &'input str, usize), + (_, _, _): (usize, &'input str, usize), + (_, __0, _): (usize, alloc::vec::Vec, usize), + (_, _, _): (usize, &'input str, usize), +) -> Tree { - Tree::Leaf(__0) + Tree::Mul(__0) } +#[allow(unused_variables)] fn __action4< + 'input, 's, >( - (_, __0, _): (usize, i64, usize), -) -> Val<'s> + input: &'input str, + (_, _, _): (usize, &'input str, usize), + (_, _, _): (usize, &'input str, usize), + (_, __0, _): (usize, alloc::vec::Vec, usize), + (_, _, _): (usize, &'input str, usize), +) -> Tree { - Val::Int(__0) + Tree::Div(__0) } +#[allow(unused_variables)] fn __action5< + 'input, 's, >( - (_, __0, _): (usize, f64, usize), -) -> Val<'s> + input: &'input str, + (_, __0, _): (usize, &'input str, usize), +) -> Tree { - Val::Float(__0) + Tree::Val(__0.parse().unwrap()) } +#[allow(unused_variables)] fn __action6< + 'input, 's, >( - (_, __0, _): (usize, &'s str, usize), -) -> Val<'s> -{ - Val::Ident(__0) -} - -fn __action7< - 's, ->( - __lookbehind: &usize, - __lookahead: &usize, -) -> alloc::vec::Vec> -{ - alloc::vec![] -} - -fn __action8< - 's, ->( - (_, v, _): (usize, alloc::vec::Vec>, usize), -) -> alloc::vec::Vec> -{ - v -} - -fn __action9< - 's, ->( - (_, __0, _): (usize, Tree<'s>, usize), -) -> alloc::vec::Vec> + input: &'input str, + (_, __0, _): (usize, Tree, usize), +) -> alloc::vec::Vec { alloc::vec![__0] } -fn __action10< +#[allow(unused_variables)] +fn __action7< + 'input, 's, >( - (_, v, _): (usize, alloc::vec::Vec>, usize), - (_, e, _): (usize, Tree<'s>, usize), -) -> alloc::vec::Vec> + input: &'input str, + (_, v, _): (usize, alloc::vec::Vec, usize), + (_, e, _): (usize, Tree, usize), +) -> alloc::vec::Vec { { let mut v = v; v.push(e); v } } -fn __action11< +#[allow(unused_variables)] +fn __action8< + 'input, 's, >( + input: &'input str, __lookbehind: &usize, __lookahead: &usize, -) -> alloc::vec::Vec> +) -> alloc::vec::Vec { - let __start0 = __lookbehind.clone(); - let __end0 = __lookahead.clone(); - let __temp0 = __action7( + alloc::vec![] +} + +#[allow(unused_variables)] +fn __action9< + 'input, + 's, +>( + input: &'input str, + (_, v, _): (usize, alloc::vec::Vec, usize), +) -> alloc::vec::Vec +{ + v +} + +#[allow(unused_variables)] +fn __action10< + 'input, + 's, +>( + input: &'input str, + __0: (usize, &'input str, usize), + __1: (usize, &'input str, usize), + __2: (usize, &'input str, usize), +) -> Tree +{ + let __start0 = __1.2.clone(); + let __end0 = __2.0.clone(); + let __temp0 = __action8( + input, &__start0, &__end0, ); let __temp0 = (__start0, __temp0, __end0); __action1( + input, + __0, + __1, __temp0, + __2, ) } -fn __action12< +#[allow(unused_variables)] +fn __action11< + 'input, 's, >( - __0: (usize, alloc::vec::Vec>, usize), -) -> alloc::vec::Vec> + input: &'input str, + __0: (usize, &'input str, usize), + __1: (usize, &'input str, usize), + __2: (usize, alloc::vec::Vec, usize), + __3: (usize, &'input str, usize), +) -> Tree { - let __start0 = __0.0.clone(); - let __end0 = __0.2.clone(); - let __temp0 = __action8( - __0, + let __start0 = __2.0.clone(); + let __end0 = __2.2.clone(); + let __temp0 = __action9( + input, + __2, ); let __temp0 = (__start0, __temp0, __end0); __action1( + input, + __0, + __1, __temp0, + __3, ) } -pub trait __ToTriple<'s, > { - fn to_triple(value: Self) -> Result<(usize,Token<'s>,usize), __lalrpop_util::ParseError, crate::LexError>>; +#[allow(unused_variables)] +fn __action12< + 'input, + 's, +>( + input: &'input str, + __0: (usize, &'input str, usize), + __1: (usize, &'input str, usize), + __2: (usize, &'input str, usize), +) -> Tree +{ + let __start0 = __1.2.clone(); + let __end0 = __2.0.clone(); + let __temp0 = __action8( + input, + &__start0, + &__end0, + ); + let __temp0 = (__start0, __temp0, __end0); + __action3( + input, + __0, + __1, + __temp0, + __2, + ) } -impl<'s, > __ToTriple<'s, > for (usize, Token<'s>, usize) { - fn to_triple(value: Self) -> Result<(usize,Token<'s>,usize), __lalrpop_util::ParseError, crate::LexError>> { +#[allow(unused_variables)] +fn __action13< + 'input, + 's, +>( + input: &'input str, + __0: (usize, &'input str, usize), + __1: (usize, &'input str, usize), + __2: (usize, alloc::vec::Vec, usize), + __3: (usize, &'input str, usize), +) -> Tree +{ + let __start0 = __2.0.clone(); + let __end0 = __2.2.clone(); + let __temp0 = __action9( + input, + __2, + ); + let __temp0 = (__start0, __temp0, __end0); + __action3( + input, + __0, + __1, + __temp0, + __3, + ) +} + +pub trait __ToTriple<'input, 's, > { + fn to_triple(value: Self) -> Result<(usize,Token<'input>,usize), __lalrpop_util::ParseError, &'static str>>; +} + +impl<'input, 's, > __ToTriple<'input, 's, > for (usize, Token<'input>, usize) { + fn to_triple(value: Self) -> Result<(usize,Token<'input>,usize), __lalrpop_util::ParseError, &'static str>> { Ok(value) } } -impl<'s, > __ToTriple<'s, > for Result<(usize, Token<'s>, usize), crate::LexError> { - fn to_triple(value: Self) -> Result<(usize,Token<'s>,usize), __lalrpop_util::ParseError, crate::LexError>> { +impl<'input, 's, > __ToTriple<'input, 's, > for Result<(usize, Token<'input>, usize), &'static str> { + fn to_triple(value: Self) -> Result<(usize,Token<'input>,usize), __lalrpop_util::ParseError, &'static str>> { match value { Ok(v) => Ok(v), Err(error) => Err(__lalrpop_util::ParseError::User { error }), diff --git a/src/main.rs b/src/main.rs index 02a19c4..97db77e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,130 +1,63 @@ -use logos::Logos; - mod grammar; -#[derive(Logos, Debug, PartialEq)] -enum LogosToken { - #[regex(r"[0-9]+", priority = 2)] - Int, +use debug2::dbg; - #[regex(r"[0-9]+(\\.[0-9]+)?")] - Float, - - #[regex(r"[A-Za-z0-9!$%&*+\-./:<=>?@^_~]+")] - Ident, - - #[error] - #[regex(r"[ \t\n\f]+", logos::skip)] - Error, - - #[token(r"(")] - Lparen, - - #[token(r")")] - Rparen, -} -#[derive(Debug)] -pub struct LexError; - -type Spanned = Result<(Loc, Tok, Loc), Error>; - -struct Lexer<'a>(logos::Lexer<'a, LogosToken>); - -impl<'a> Lexer<'a> { - fn new(src: &'a str) -> Self { - Self(LogosToken::lexer(src)) - } +#[derive(Debug, debug2::Debug, PartialEq)] +pub enum Tree { + Val(f64), + Add(Vec), + Sub(Vec), + Mul(Vec), + Div(Vec), } -#[derive(Clone, Debug)] -pub enum Token<'s> { - Int(i64), - Float(f64), - Ident(&'s str), - Lparen, - Rparen, +use rustyline::validate::{ + MatchingBracketValidator, ValidationContext, ValidationResult, Validator, +}; +use rustyline::{Editor, Result}; +use rustyline_derive::{Completer, Helper, Highlighter, Hinter}; + +#[derive(Completer, Helper, Highlighter, Hinter)] +struct InputValidator { + brackets: MatchingBracketValidator, } -impl<'a> Iterator for Lexer<'a> { - type Item = Spanned, usize, LexError>; - - fn next(&mut self) -> Option { - let kind = self.0.next()?; - let span = self.0.span(); - let text = self.0.slice(); - - let t = match kind { - LogosToken::Int => Token::Int(text.parse().unwrap()), - LogosToken::Float => Token::Float(text.parse().unwrap()), - LogosToken::Ident => Token::Ident(text), - LogosToken::Lparen => Token::Lparen, - LogosToken::Rparen => Token::Rparen, - LogosToken::Error => return Some(Err(LexError)), - }; - - Some(Ok((span.start, t, span.end))) - } -} - -#[derive(Clone, Debug)] -pub enum Tree<'s> { - Leaf(Val<'s>), - Branch(Vec>), -} - -#[derive(Clone, Debug)] -pub enum Val<'s> { - Int(i64), - Float(f64), - Ident(&'s str), -} - -enum Func {} - -enum RVal { - Func(Func), - Num(f64), -} - -enum Op { - Push(RVal), - Call(u8), - Print, -} - -fn comp_to(t: &Tree) - -fn eval(t: &Tree<'_>) -> f64 { - match t { - Tree::Leaf(Val::Ident(_)) => panic!("Need number got {:?}", t), - Tree::Leaf(Val::Float(f)) => *f, - Tree::Leaf(Val::Int(i)) => *i as f64, - Tree::Branch(v) => { - if let Tree::Leaf(Val::Ident(f)) = v[0] { - match f { - "min" => v[1..].iter().map(eval).reduce(f64::min).unwrap(), - "max" => v[1..].iter().map(eval).reduce(f64::max).unwrap(), - "+" => v[1..].iter().map(eval).sum(), - "*" => v[1..].iter().map(eval).product(), - "/" => eval(&v[1]) / v[2..].iter().map(eval).product::(), - "-" => eval(&v[1]) - v[2..].iter().map(eval).sum::(), - _ => panic!("Unknown func {:?}", f), - } - } else { - panic!("Not a function") - } - } +impl Validator for InputValidator { + fn validate(&self, ctx: &mut ValidationContext) -> Result { + self.brackets.validate(ctx) } } fn main() { - let src = std::fs::read_to_string(std::env::args().nth(1).unwrap()).unwrap(); + // let src = std::fs::read_to_string(std::env::args().nth(1).unwrap()).unwrap(); - let tree = grammar::TreesParser::new().parse(Lexer::new(&src)); + // let tree = grammar::TreeParser::new().parse(&src); - let t = dbg!(tree).unwrap(); + // let t = dbg!(tree.unwrap()); - dbg!(eval(&t[0])); + // println!("{}", eval(&t)); + + let mut rl = Editor::new(); + rl.set_helper(Some(InputValidator { + brackets: MatchingBracketValidator::new(), + })); + while let Ok(line) = rl.readline("> ") { + rl.add_history_entry(&line); + + let tree = grammar::TreeParser::new().parse(&line).unwrap(); + dbg!(&tree); + println!("< {}", eval(&tree)) + } +} + +fn eval(t: &Tree) -> f64 { + match t { + Tree::Val(v) => *v, + Tree::Add(vs) => vs.iter().map(eval).sum(), + Tree::Mul(vs) => vs.iter().map(eval).product(), + Tree::Sub(vs) => eval(&vs[0]) - vs[1..].iter().map(eval).sum::(), + Tree::Div(vs) => eval(&vs[0]) / vs[1..].iter().map(eval).product::(), + } } #[cfg(test)] @@ -132,15 +65,42 @@ mod tests { use super::*; #[test] - fn math_exprs() { - fn test(s: &str, v: f64) { - let tree = grammar::TreesParser::new().parse(Lexer::new(s)).unwrap(); - assert_eq!(tree.len(), 1); - assert_eq!(eval(&tree[0]), v); - } + fn simple_math_space() { + let t = grammar::TreeParser::new() + .parse("( + 1 2 ( / 2 3 4 5) )") + .unwrap(); + assert_eq!( + t, + Tree::Add(vec![ + Tree::Val(1.0), + Tree::Val(2.0), + Tree::Div(vec![ + Tree::Val(2.0), + Tree::Val(3.0), + Tree::Val(4.0), + Tree::Val(5.0), + ]) + ]) + ); + } - test("1", 1.0); - test("(+ 1 2)", 3.0); - test("(+ 1 2 (/ 2 3 4 5))", 3.033333333333333); + #[test] + fn simple_math_dence() { + let t = grammar::TreeParser::new() + .parse("(+ 1 2 (/ 2 3 4 5))") + .unwrap(); + assert_eq!( + t, + Tree::Add(vec![ + Tree::Val(1.0), + Tree::Val(2.0), + Tree::Div(vec![ + Tree::Val(2.0), + Tree::Val(3.0), + Tree::Val(4.0), + Tree::Val(5.0), + ]) + ]) + ); } } diff --git a/test/pass/math.scm b/test/pass/math.scm index 593de37..f3250a9 100644 --- a/test/pass/math.scm +++ b/test/pass/math.scm @@ -1 +1 @@ -(+ 1 2 (/ 2 3 4 5)) \ No newline at end of file +( + 1 2 ( / 2 3 4 5) ) \ No newline at end of file