This commit is contained in:
Greg Shuflin 2021-11-17 12:45:55 -08:00
parent f1ffeb155a
commit d37be75478
2 changed files with 111 additions and 111 deletions

View File

@ -1,17 +1,20 @@
use std::{cell::RefCell, rc::Rc};
use nom::{
Err,
branch::alt,
bytes::complete::{take_till, tag},
character::complete::{alpha1, alphanumeric0, not_line_ending,none_of, char, one_of, space0, space1, multispace0, line_ending},
combinator::{opt, peek, not, value, map, recognize},
error::{context, VerboseError, ParseError},
multi::{fold_many1, many1, many0, separated_list1, separated_list0},
sequence::{pair, tuple, preceded},
IResult, Parser,
bytes::complete::{tag, take_till},
character::complete::{
alpha1, alphanumeric0, char, line_ending, multispace0, none_of, not_line_ending, one_of, space0,
space1,
},
combinator::{map, not, opt, peek, recognize, value},
error::{context, ParseError, VerboseError},
multi::{fold_many1, many0, many1, separated_list0, separated_list1},
sequence::{pair, preceded, tuple},
Err, IResult, Parser,
};
use nom_locate::{position, LocatedSpan};
use std::rc::Rc;
use std::cell::RefCell;
use crate::identifier::{Id, IdStore};
type StoreRef = Rc<RefCell<IdStore<ASTItem>>>;
@ -29,65 +32,50 @@ fn fresh_id(span: &Span) -> Id<ASTItem> {
table_handle.fresh()
}
fn tok<'a, O>(input_parser: impl Parser<Span<'a>, O, VerboseError<Span<'a>>>) -> impl FnMut(Span<'a>)
-> IResult<Span<'a>, O, VerboseError<Span<'a>>> {
context("tok",
map(tuple((ws0, input_parser)), |(_, output)|
output))
fn tok<'a, O>(
input_parser: impl Parser<Span<'a>, O, VerboseError<Span<'a>>>,
) -> impl FnMut(Span<'a>) -> IResult<Span<'a>, O, VerboseError<Span<'a>>> {
context("tok", map(tuple((ws0, input_parser)), |(_, output)| output))
}
fn kw<'a>(keyword_str: &'static str) -> impl FnMut(Span<'a>) -> ParseResult<()> {
context("keyword",
tok(value((), tag(keyword_str))))
context("keyword", tok(value((), tag(keyword_str))))
}
// whitespace does consume at least one piece of whitespace - use ws0 for maybe none
fn whitespace(input: Span) -> ParseResult<()> {
context("whitespace",
alt((
block_comment,
line_comment,
value((), space1),
)))(input)
context("whitespace", alt((block_comment, line_comment, value((), space1))))(input)
}
fn ws0(input: Span) -> ParseResult<()> {
context("WS0",
value((), many0(whitespace)))(input)
context("WS0", value((), many0(whitespace)))(input)
}
fn line_comment(input: Span) -> ParseResult<()> {
value((),
tuple((tag("//"), not_line_ending)),
)(input)
value((), tuple((tag("//"), not_line_ending)))(input)
}
fn block_comment(input: Span) -> ParseResult<()> {
context("Block-comment",
value((),
context(
"Block-comment",
value(
(),
tuple((
tag("/*"),
many0(alt((
value((), none_of("*/")),
value((), none_of("/*")),
block_comment,
))),
tag("*/")
))))(input)
}
fn statement_delimiter(input: Span) -> ParseResult<()> {
tok(alt((
value((), line_ending),
value((), char(';'))
))
many0(alt((value((), none_of("*/")), value((), none_of("/*")), block_comment))),
tag("*/"),
)),
),
)(input)
}
fn statement_delimiter(input: Span) -> ParseResult<()> {
tok(alt((value((), line_ending), value((), char(';')))))(input)
}
fn block(input: Span) -> ParseResult<Block> {
context("block",
context(
"block",
map(
tuple((
tok(char('{')),
@ -95,26 +83,29 @@ fn block(input: Span) -> ParseResult<Block> {
separated_list0(statement_delimiter, statement),
many0(statement_delimiter),
tok(char('}')),
)), |(_, _, items, _, _)| items.into()))(input)
)),
|(_, _, items, _, _)| items.into(),
),
)(input)
}
fn statement(input: Span) -> ParseResult<Statement> {
let (input, pos) = position(input)?;
let pos: usize = pos.location_offset();
let id = fresh_id(&input);
context("Parsing-statement",
context(
"Parsing-statement",
map(expression, move |expr| Statement {
id,
location: pos.into(),
kind: StatementKind::Expression(expr),
}))(input)
}),
)(input)
}
fn expression(input: Span) -> ParseResult<Expression> {
let id = fresh_id(&input);
map(pair(expression_kind, opt(type_anno)), move |(kind, maybe_anno)| {
Expression::new(id, kind)
})(input)
map(pair(expression_kind, opt(type_anno)), move |(kind, maybe_anno)| Expression::new(id, kind))(input)
}
fn type_anno(input: Span) -> ParseResult<TypeIdentifier> {
@ -140,13 +131,7 @@ pub fn expression_kind(input: Span) -> ParseResult<ExpressionKind> {
}
fn primary_expr(input: Span) -> ParseResult<ExpressionKind> {
context("primary-expr",
alt((
number_literal,
bool_literal,
identifier_expr,
)))(input)
context("primary-expr", alt((number_literal, bool_literal, identifier_expr)))(input)
}
fn identifier_expr(input: Span) -> ParseResult<ExpressionKind> {
@ -155,27 +140,23 @@ fn identifier_expr(input: Span) -> ParseResult<ExpressionKind> {
fn qualified_identifier(input: Span) -> ParseResult<QualifiedName> {
let id = fresh_id(&input);
tok(
map(
separated_list1(tag("::"), map(identifier, |x| rc_string(x.fragment()))),
move |items| QualifiedName { id, components: items }
))(input)
tok(map(separated_list1(tag("::"), map(identifier, |x| rc_string(x.fragment()))), move |items| {
QualifiedName { id, components: items }
}))(input)
}
fn identifier(input: Span) -> ParseResult<Span> {
recognize(
tuple((
alt((tag("_"), alpha1)),
alphanumeric0,
)))(input)
recognize(tuple((alt((tag("_"), alpha1)), alphanumeric0)))(input)
}
fn bool_literal(input: Span) -> ParseResult<ExpressionKind> {
context("bool-literal",
context(
"bool-literal",
alt((
map(kw("true"), |_| ExpressionKind::BoolLiteral(true)),
map(kw("false"), |_| ExpressionKind::BoolLiteral(false)),
)))(input)
)),
)(input)
}
fn number_literal(input: Span) -> ParseResult<ExpressionKind> {
@ -282,14 +263,11 @@ mod test {
}
macro_rules! span {
($func:expr, $input:expr) => {
{
($func:expr, $input:expr) => {{
let id_store: IdStore<ASTItem> = IdStore::new();
let span = Span::new_extra($input,
Rc::new(RefCell::new(id_store)));
let span = Span::new_extra($input, Rc::new(RefCell::new(id_store)));
$func(span).map(|(span, x)| (*span.fragment(), x))
}
};
}};
}
#[test]
@ -313,7 +291,10 @@ mod test {
}
assert_eq!(span!(expression_kind, " /*gay*/ true").unwrap().1, ExpressionKind::BoolLiteral(true));
assert_eq!(span!(expression_kind, " /*yolo*/ barnaby").unwrap().1, ExpressionKind::Value(qn!(barnaby)));
assert_eq!(
span!(expression_kind, " /*yolo*/ barnaby").unwrap().1,
ExpressionKind::Value(qn!(barnaby))
);
}
#[test]
@ -328,10 +309,12 @@ mod test {
}"#;
let parsed = span!(block, source).map_err(|err| match err {
Err::Error(err) | Err::Failure(err) => {
let err = VerboseError { errors: err.errors.into_iter().map(|(sp, kind)| (*sp.fragment(), kind)).collect() };
let err = VerboseError {
errors: err.errors.into_iter().map(|(sp, kind)| (*sp.fragment(), kind)).collect(),
};
nom::error::convert_error(source, err)
},
_ => panic!()
}
_ => panic!(),
});
if let Err(err) = parsed {
@ -339,16 +322,35 @@ mod test {
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());
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()
);
}
}

View File

@ -1352,5 +1352,3 @@ fn backtick_operators() {
assert_eq!(output, vec![digit!("1"), op!("plus"), digit!("2")]);
}
*/