2021-11-16 20:23:27 -08:00
|
|
|
use nom::{
|
|
|
|
Err,
|
|
|
|
branch::alt,
|
2021-11-17 01:54:35 -08:00
|
|
|
bytes::complete::{take_till, tag},
|
|
|
|
character::complete::{alpha1, alphanumeric0, not_line_ending,none_of, char, one_of, space0, space1, multispace0, line_ending},
|
2021-11-17 03:40:43 -08:00
|
|
|
combinator::{opt, peek, not, value, map, recognize},
|
2021-11-16 20:23:27 -08:00
|
|
|
error::{context, VerboseError, ParseError},
|
2021-11-17 01:54:35 -08:00
|
|
|
multi::{fold_many1, many1, many0, separated_list1, separated_list0},
|
2021-11-17 03:40:43 -08:00
|
|
|
sequence::{pair, tuple, preceded},
|
2021-11-16 20:23:27 -08:00
|
|
|
IResult, Parser,
|
|
|
|
};
|
2021-11-17 01:54:35 -08:00
|
|
|
use std::rc::Rc;
|
2021-11-16 20:23:27 -08:00
|
|
|
|
|
|
|
type ParseResult<'a, O> = IResult<&'a str, O, VerboseError<&'a str>>;
|
|
|
|
|
|
|
|
use crate::ast::*;
|
|
|
|
|
2021-11-17 01:54:35 -08:00
|
|
|
fn rc_string(s: &str) -> Rc<String> {
|
|
|
|
Rc::new(s.to_string())
|
2021-11-16 20:23:27 -08:00
|
|
|
}
|
|
|
|
|
2021-11-17 01:54:35 -08:00
|
|
|
fn tok<'a, O>(input_parser: impl Parser<&'a str, O, VerboseError<&'a str>>) -> impl FnMut(&'a str)
|
|
|
|
-> IResult<&'a str, O, VerboseError<&'a str>> {
|
|
|
|
|
|
|
|
context("tok",
|
|
|
|
map(tuple((ws0, input_parser)), |(_, output)|
|
|
|
|
output))
|
|
|
|
}
|
2021-11-16 20:23:27 -08:00
|
|
|
|
2021-11-17 01:54:35 -08:00
|
|
|
fn kw<'a>(keyword_str: &'static str) -> impl FnMut(&'a str) -> ParseResult<()> {
|
|
|
|
context("keyword",
|
|
|
|
tok(value((), tag(keyword_str))))
|
2021-11-16 20:23:27 -08:00
|
|
|
}
|
|
|
|
|
2021-11-17 01:54:35 -08:00
|
|
|
|
2021-11-16 20:23:27 -08:00
|
|
|
// whitespace does consume at least one piece of whitespace - use ws0 for maybe none
|
|
|
|
fn whitespace(input: &str) -> ParseResult<()> {
|
|
|
|
context("whitespace",
|
|
|
|
alt((
|
|
|
|
block_comment,
|
2021-11-17 01:54:35 -08:00
|
|
|
line_comment,
|
|
|
|
value((), space1),
|
2021-11-16 20:23:27 -08:00
|
|
|
)))(input)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn ws0(input: &str) -> ParseResult<()> {
|
|
|
|
context("WS0",
|
|
|
|
value((), many0(whitespace)))(input)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn line_comment(input: &str) -> ParseResult<()> {
|
|
|
|
value((),
|
2021-11-17 01:54:35 -08:00
|
|
|
tuple((tag("//"), not_line_ending)),
|
2021-11-16 20:23:27 -08:00
|
|
|
)(input)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn block_comment(input: &str) -> ParseResult<()> {
|
2021-11-17 01:54:35 -08:00
|
|
|
context("Block-comment",
|
2021-11-16 20:23:27 -08:00
|
|
|
value((),
|
|
|
|
tuple((
|
|
|
|
tag("/*"),
|
|
|
|
many0(alt((
|
2021-11-17 01:54:35 -08:00
|
|
|
value((), none_of("*/")),
|
|
|
|
value((), none_of("/*")),
|
2021-11-16 20:23:27 -08:00
|
|
|
block_comment,
|
|
|
|
))),
|
|
|
|
tag("*/")
|
2021-11-17 01:54:35 -08:00
|
|
|
))))(input)
|
2021-11-16 20:23:27 -08:00
|
|
|
}
|
|
|
|
|
2021-11-17 01:54:35 -08:00
|
|
|
fn statement_delimiter(input: &str) -> ParseResult<()> {
|
|
|
|
tok(alt((
|
|
|
|
value((), line_ending),
|
|
|
|
value((), char(';'))
|
|
|
|
))
|
|
|
|
)(input)
|
2021-11-16 20:23:27 -08:00
|
|
|
}
|
|
|
|
|
2021-11-17 01:54:35 -08:00
|
|
|
fn block(input: &str) -> ParseResult<Block> {
|
|
|
|
context("block",
|
|
|
|
map(
|
|
|
|
tuple((
|
|
|
|
tok(char('{')),
|
|
|
|
many0(statement_delimiter),
|
|
|
|
separated_list0(statement_delimiter, statement),
|
|
|
|
many0(statement_delimiter),
|
|
|
|
tok(char('}')),
|
|
|
|
)), |(_, _, items, _, _)| items.into()))(input)
|
2021-11-16 20:23:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn statement(input: &str) -> ParseResult<Statement> {
|
|
|
|
context("Parsing-statement",
|
2021-11-17 03:40:43 -08:00
|
|
|
map(expression, |expr| Statement {
|
2021-11-16 20:23:27 -08:00
|
|
|
id: Default::default(),
|
|
|
|
location: Default::default(),
|
2021-11-17 03:40:43 -08:00
|
|
|
kind: StatementKind::Expression(expr),
|
2021-11-16 20:23:27 -08:00
|
|
|
}))(input)
|
|
|
|
}
|
|
|
|
|
2021-11-17 03:40:43 -08:00
|
|
|
fn expression(input: &str) -> ParseResult<Expression> {
|
|
|
|
map(pair(expression_kind, opt(type_anno)), |(kind, maybe_anno)| {
|
|
|
|
Expression::new(Default::default(), kind)
|
|
|
|
})(input)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn type_anno(input: &str) -> ParseResult<TypeIdentifier> {
|
|
|
|
preceded(kw(":"), type_identifier)(input)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn type_identifier(input: &str) -> ParseResult<TypeIdentifier> {
|
|
|
|
/*
|
|
|
|
alt((
|
|
|
|
tuple((kw("("), separated_list0(kw(","), type_identifier), kw(")"))),
|
|
|
|
type_singleton_name
|
|
|
|
))(input)
|
|
|
|
*/
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn type_singleton_name(input: &str) -> ParseResult<TypeSingletonName> {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
2021-11-16 20:23:27 -08:00
|
|
|
pub fn expression_kind(input: &str) -> ParseResult<ExpressionKind> {
|
2021-11-17 01:54:35 -08:00
|
|
|
context("expression-kind", primary_expr)(input)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn primary_expr(input: &str) -> ParseResult<ExpressionKind> {
|
|
|
|
|
|
|
|
context("primary-expr",
|
2021-11-16 20:23:27 -08:00
|
|
|
alt((
|
|
|
|
number_literal,
|
|
|
|
bool_literal,
|
2021-11-17 01:54:35 -08:00
|
|
|
identifier_expr,
|
|
|
|
)))(input)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
fn identifier_expr(input: &str) -> ParseResult<ExpressionKind> {
|
|
|
|
context("identifier-expr", map(qualified_identifier, ExpressionKind::Value))(input)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn qualified_identifier(input: &str) -> ParseResult<QualifiedName> {
|
|
|
|
tok(
|
|
|
|
map(
|
|
|
|
separated_list1(tag("::"), map(identifier, rc_string)),
|
|
|
|
|items| QualifiedName { id: Default::default(), components: items }
|
|
|
|
))(input)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn identifier(input: &str) -> ParseResult<&str> {
|
|
|
|
recognize(
|
|
|
|
tuple((
|
|
|
|
alt((tag("_"), alpha1)),
|
|
|
|
alphanumeric0,
|
2021-11-16 20:23:27 -08:00
|
|
|
)))(input)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bool_literal(input: &str) -> ParseResult<ExpressionKind> {
|
2021-11-17 01:54:35 -08:00
|
|
|
context("bool-literal",
|
2021-11-16 20:23:27 -08:00
|
|
|
alt((
|
2021-11-17 01:54:35 -08:00
|
|
|
map(kw("true"), |_| ExpressionKind::BoolLiteral(true)),
|
|
|
|
map(kw("false"), |_| ExpressionKind::BoolLiteral(false)),
|
|
|
|
)))(input)
|
2021-11-16 20:23:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn number_literal(input: &str) -> ParseResult<ExpressionKind> {
|
|
|
|
map(alt((tok(hex_literal), tok(bin_literal), tok(dec_literal))), ExpressionKind::NatLiteral)(input)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn dec_literal(input: &str) -> ParseResult<u64> {
|
|
|
|
map(digits(digit_group_dec), |chars: Vec<char>| {
|
|
|
|
let s: String = chars.into_iter().collect();
|
|
|
|
s.parse().unwrap()
|
|
|
|
})(input)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn hex_literal(input: &str) -> ParseResult<u64> {
|
|
|
|
map(preceded(alt((tag("0x"), tag("0X"))), digits(digit_group_hex)), |chars: Vec<char>| {
|
|
|
|
let s: String = chars.into_iter().collect();
|
|
|
|
parse_hex(&s).unwrap()
|
|
|
|
})(input)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bin_literal(input: &str) -> ParseResult<u64> {
|
|
|
|
map(preceded(alt((tag("0b"), tag("0B"))), digits(digit_group_bin)), |chars: Vec<char>| {
|
|
|
|
let s: String = chars.into_iter().collect();
|
|
|
|
parse_binary(&s).unwrap()
|
|
|
|
})(input)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn digits<'a, E: ParseError<&'a str>>(
|
|
|
|
digit_type: impl Parser<&'a str, Vec<char>, E>,
|
|
|
|
) -> impl FnMut(&'a str) -> IResult<&'a str, Vec<char>, E> {
|
|
|
|
map(separated_list1(many1(char('_')), digit_type), |items: Vec<Vec<char>>| {
|
|
|
|
items.into_iter().flatten().collect()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn digit_group_dec(input: &str) -> ParseResult<Vec<char>> {
|
|
|
|
many1(one_of("0123456789"))(input)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn digit_group_hex(input: &str) -> ParseResult<Vec<char>> {
|
|
|
|
many1(one_of("0123456789abcdefABCDEF"))(input)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn digit_group_bin(input: &str) -> ParseResult<Vec<char>> {
|
|
|
|
many1(one_of("01"))(input)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_binary(digits: &str) -> Result<u64, &'static str> {
|
|
|
|
let mut result: u64 = 0;
|
|
|
|
let mut multiplier = 1;
|
|
|
|
for d in digits.chars().rev() {
|
|
|
|
match d {
|
|
|
|
'1' => result += multiplier,
|
|
|
|
'0' => (),
|
|
|
|
'_' => continue,
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
multiplier = match multiplier.checked_mul(2) {
|
|
|
|
Some(m) => m,
|
|
|
|
None => return Err("Binary expression will overflow"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_hex(digits: &str) -> Result<u64, &'static str> {
|
|
|
|
let mut result: u64 = 0;
|
|
|
|
let mut multiplier: u64 = 1;
|
|
|
|
for d in digits.chars().rev() {
|
|
|
|
if d == '_' {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
match d.to_digit(16) {
|
|
|
|
Some(n) => result += n as u64 * multiplier,
|
|
|
|
None => return Err("Internal parser error: invalid hex digit"),
|
|
|
|
}
|
|
|
|
multiplier = match multiplier.checked_mul(16) {
|
|
|
|
Some(m) => m,
|
|
|
|
None => return Err("Hexadecimal expression will overflow"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use pretty_assertions::assert_eq;
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
2021-11-17 01:54:35 -08:00
|
|
|
fn rc(s: &str) -> Rc<String> {
|
|
|
|
Rc::new(s.to_owned())
|
|
|
|
}
|
|
|
|
macro_rules! qn {
|
|
|
|
( $( $component:ident),* ) => {
|
|
|
|
{
|
|
|
|
let mut components = vec![];
|
|
|
|
$(
|
|
|
|
components.push(rc(stringify!($component)));
|
|
|
|
)*
|
|
|
|
QualifiedName { components, id: Default::default() }
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-11-16 20:23:27 -08:00
|
|
|
#[test]
|
|
|
|
fn combinator_test1() {
|
|
|
|
assert_eq!(digits(digit_group_dec)("342").unwrap().1, vec!['3', '4', '2']);
|
|
|
|
assert_eq!(bin_literal("0b1111qsdf"), Ok(("qsdf", 15)));
|
|
|
|
}
|
|
|
|
|
2021-11-17 01:54:35 -08:00
|
|
|
#[test]
|
|
|
|
fn combinator_test_ws0() {
|
|
|
|
assert_eq!(block_comment("/*yolo*/").unwrap(), ("", ()));
|
|
|
|
assert_eq!(block_comment("/*yolo*/ jumpy /*nah*/").unwrap(), (" jumpy /*nah*/", ()));
|
|
|
|
assert_eq!(ws0("/* yolo */ ").unwrap(), ("", ()));
|
|
|
|
assert_eq!(ws0("/* /* no */ yolo */ ").unwrap(), ("", ()));
|
|
|
|
}
|
|
|
|
|
2021-11-16 20:23:27 -08:00
|
|
|
#[test]
|
|
|
|
fn combinator_test2() {
|
2021-11-17 01:54:35 -08:00
|
|
|
for s in [" 15", " 0b1111", " 1_5_", "0XF__", "0Xf"].iter() {
|
2021-11-16 20:23:27 -08:00
|
|
|
assert_eq!(expression_kind(s).unwrap().1, ExpressionKind::NatLiteral(15));
|
|
|
|
}
|
2021-11-17 01:54:35 -08:00
|
|
|
|
|
|
|
assert_eq!(expression_kind(" /*gay*/ true").unwrap().1, ExpressionKind::BoolLiteral(true));
|
|
|
|
assert_eq!(expression_kind(" /*yolo*/ barnaby").unwrap().1, ExpressionKind::Value(qn!(barnaby)));
|
2021-11-16 20:23:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn combinator_test3() {
|
2021-11-17 01:54:35 -08:00
|
|
|
let source = "{}";
|
|
|
|
assert_eq!(block(source).unwrap().1, vec![].into());
|
2021-11-16 20:23:27 -08:00
|
|
|
let source = r#"{
|
|
|
|
|
2021-11-17 01:54:35 -08:00
|
|
|
//hella
|
|
|
|
4_5 //bog
|
|
|
|
11; /*chutney*/0xf
|
2021-11-16 20:23:27 -08:00
|
|
|
}"#;
|
|
|
|
let parsed = block(source).map_err(|err| match err {
|
|
|
|
Err::Error(err) | Err::Failure(err) => nom::error::convert_error(source, err),
|
|
|
|
_ => panic!()
|
|
|
|
});
|
|
|
|
|
|
|
|
if let Err(err) = parsed {
|
|
|
|
println!("{}", err);
|
|
|
|
panic!("parse error desu!");
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(parsed.unwrap().1, vec![
|
|
|
|
Statement { id: Default::default(), location:
|
|
|
|
Default::default(), kind: StatementKind::Expression(Expression::new(Default::default(),
|
|
|
|
ExpressionKind::NatLiteral(45))) },
|
|
|
|
Statement { id: Default::default(), location:
|
|
|
|
Default::default(), kind: StatementKind::Expression(Expression::new(Default::default(),
|
|
|
|
ExpressionKind::NatLiteral(11))) },
|
|
|
|
Statement { id: Default::default(), location:
|
|
|
|
Default::default(), kind: StatementKind::Expression(Expression::new(Default::default(),
|
|
|
|
ExpressionKind::NatLiteral(15))) },
|
|
|
|
].into());
|
|
|
|
}
|
|
|
|
}
|