From cd49c2c78f0b7da6fbf48ddbf27bc8cedb2e41c9 Mon Sep 17 00:00:00 2001 From: greg Date: Wed, 19 Feb 2020 04:50:00 -0800 Subject: [PATCH] Starting to experiment with combine wip --- Cargo.lock | 17 ++++++++++ schala-lang/language/Cargo.toml | 1 + schala-lang/language/src/parser.rs | 54 +++++++++++++++++++----------- 3 files changed, 53 insertions(+), 19 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d4dcd86..81981be 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -87,6 +87,11 @@ name = "byteorder" version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "bytes" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "cc" version = "1.0.45" @@ -124,6 +129,15 @@ dependencies = [ "winconsole 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "combine" +version = "4.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bytes 0.5.4 (registry+https://github.com/rust-lang/crates.io-index)", + "memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "constant_time_eq" version = "0.1.4" @@ -778,6 +792,7 @@ name = "schala-lang" version = "0.1.0" dependencies = [ "colored 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)", + "combine 4.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "derivative 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)", "ena 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", "failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1050,11 +1065,13 @@ dependencies = [ "checksum bitflags 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8a606a02debe2813760609f57a64a2ffd27d9fdf5b2f133eaca0b248dd92cdd2" "checksum blake2b_simd 0.5.8 (registry+https://github.com/rust-lang/crates.io-index)" = "5850aeee1552f495dd0250014cf64b82b7c8879a89d83b33bbdace2cc4f63182" "checksum byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a7c3dd8985a7111efc5c80b44e23ecdd8c007de8ade3b96595387e812b957cf5" +"checksum bytes 0.5.4 (registry+https://github.com/rust-lang/crates.io-index)" = "130aac562c0dd69c56b3b1cc8ffd2e17be31d0b6c25b61c96b76231aa23e39e1" "checksum cc 1.0.45 (registry+https://github.com/rust-lang/crates.io-index)" = "4fc9a35e1f4290eb9e5fc54ba6cf40671ed2a2514c3eeb2b2a908dda2ea5a1be" "checksum cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" "checksum cgmath 0.16.1 (registry+https://github.com/rust-lang/crates.io-index)" = "64a4b57c8f4e3a2e9ac07e0f6abc9c24b6fc9e1b54c3478cfb598f3d0023e51c" "checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" "checksum colored 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6cdb90b60f2927f8d76139c72dbde7e10c3a2bc47c8594c9c7a66529f2687c03" +"checksum combine 4.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c875843236b5e2eb535fd0b696387bfb623f896479b10ed626cf442b836e8032" "checksum constant_time_eq 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "995a44c877f9212528ccc74b21a232f66ad69001e40ede5bcee2ac9ef2657120" "checksum crossbeam-utils 0.6.6 (registry+https://github.com/rust-lang/crates.io-index)" = "04973fa96e96579258a5091af6003abde64af786b860f18622b82e026cca60e6" "checksum derivative 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "942ca430eef7a3806595a6737bc388bf51adb888d3fc0dd1b50f1c170167ee3a" diff --git a/schala-lang/language/Cargo.toml b/schala-lang/language/Cargo.toml index 8f0d88c..86ad3e6 100644 --- a/schala-lang/language/Cargo.toml +++ b/schala-lang/language/Cargo.toml @@ -16,6 +16,7 @@ derivative = "1.0.3" colored = "1.8" radix_trie = "0.1.5" nom = "5.1.0" +combine = "4.0.1" schala-lang-codegen = { path = "../codegen" } schala-repl = { path = "../../schala-repl" } diff --git a/schala-lang/language/src/parser.rs b/schala-lang/language/src/parser.rs index 260ac46..bf0e961 100644 --- a/schala-lang/language/src/parser.rs +++ b/schala-lang/language/src/parser.rs @@ -30,19 +30,20 @@ fn single_alphanumeric_character(text: &str) -> ParseResult { fn identifier(text: &str) -> ParseResult> { use nom::character::complete::char; - map(alt(( + context("Identifier", map(alt(( pair(char('_'), many1(single_alphanumeric_character)), pair(single_alphabetic_character, many0(single_alphanumeric_character)) )), |(first, rest): (char, Vec)| Rc::new(format!("{}{}", first, rest.into_iter().collect::())) - )(text) + ))(text) } const OPERATOR_CHARS: &'static str = "~`!@#$%^&*-+=<>?/|"; fn parse_binop(text: &str) -> ParseResult { - let (text, op): (_, Vec) = context("binop yo", many1(one_of(OPERATOR_CHARS)))(text)?; - let sigil: String = op.into_iter().collect(); - Ok((text, BinOp::from_sigil(&sigil))) + let p = many1(one_of(OPERATOR_CHARS)); + context("Binop", map(p, + |op: Vec| BinOp::from_sigil(&op.into_iter().collect::()) + ))(text) } fn parse_bool_literal(text: &str) -> ParseResult { @@ -50,7 +51,7 @@ fn parse_bool_literal(text: &str) -> ParseResult { value(true, tag("true")), value(false, tag("false")) )); - map(p, ExpressionKind::BoolLiteral)(text) + context("Bool literal", map(p, ExpressionKind::BoolLiteral))(text) } fn parse_number_literal(text: &str) -> ParseResult { @@ -59,11 +60,11 @@ fn parse_number_literal(text: &str) -> ParseResult { map(nom::character::complete::char('_'), |_| None) ))); - let (text, n) = map_res(num_lit, + let (text, n) = context("Number literal", map_res(num_lit, |digits: Vec>| { let num_str: String = digits.into_iter().filter_map(|x| x).collect(); u64::from_str_radix(&num_str, 10) - })(text)?; + }))(text)?; Ok((text, ExpressionKind::NatLiteral(n))) } @@ -91,21 +92,20 @@ fn parse_hex_literal(input: &str) -> ParseResult { fn parse_string_literal(text: &str) -> ParseResult { use nom::character::complete::char; - let (text, string_output) = delimited( - char('"'), take_until("\""), char('"') - )(text)?; - let expr = ExpressionKind::StringLiteral(Rc::new(string_output.to_string())); - Ok((text, expr)) + let p = delimited(char('"'), take_until("\""), char('"')); + context("String literal", map(p, + |s: &str| ExpressionKind::StringLiteral(Rc::new(s.to_string())) + ))(text) } fn literal(input: &str) -> ParseResult { - alt(( - parse_string_literal, + context("Literal", alt(( parse_hex_literal, parse_binary_literal, parse_number_literal, - parse_bool_literal - ))(input) + parse_bool_literal, + parse_string_literal, + )))(input) } fn paren_expr(text: &str) -> ParseResult { @@ -130,7 +130,7 @@ fn identifier_expr(text: &str) -> ParseResult { } fn qualified_identifier_list(text: &str) -> ParseResult>> { - separated_nonempty_list(tag("::"), identifier)(text) + context("Qualified identifier list", separated_nonempty_list(tag("::"), identifier))(text) } fn primary_expr(text: &str) -> ParseResult { @@ -214,7 +214,23 @@ fn expression_kind(text: &str) -> ParseResult { precedence_expr(text) } +mod thing { + use crate::ast::*; + use crate::builtin::Builtin; + use combine::{many1, Parser, sep_by}; + use combine::parser::char::{letter, space}; + + pub fn perform_parsing(input: &str) -> () { + let word = many1(letter()); + let mut parser = sep_by(word, space()); + let result = parser.parse(input); + result + } +} + + pub fn perform_parsing(input: &str) -> Result { - let output = expression_kind(input); +// let output = expression_kind(input) + let output = thing::perform_parsing(input); Ok(format!("{:?}", output)) }