Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions src/nomparser/array.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::fmt::Error;

use internal_macro::test_parser;
use nom::{
branch::alt,
bytes::complete::tag,
Expand All @@ -18,15 +19,23 @@ use crate::{

use super::*;

#[test_parser("[1,2,3]")]
#[test_parser(
"[
1,
2,
x
]"
)]
pub fn array_init(input: Span) -> IResult<Span, Box<NodeEnum>> {
map_res(
tuple((
tag_token(TokenType::LBRACKET),
tag_token_symbol(TokenType::LBRACKET),
separated_list0(
tag_token(TokenType::COMMA),
tag_token_symbol(TokenType::COMMA),
del_newline_or_space!(logic_exp),
),
tag_token(TokenType::RBRACKET),
tag_token_symbol(TokenType::RBRACKET),
)),
|((_, lb), exps, (_, rb))| {
let range = lb.start.to(rb.end);
Expand All @@ -35,15 +44,16 @@ pub fn array_init(input: Span) -> IResult<Span, Box<NodeEnum>> {
)(input)
}

#[test_parser("[123]")]
/// ```ebnf
/// array_element_op = ('[' logic_exp ']') ;
/// ```
pub fn array_element_op(input: Span) -> IResult<Span, (ComplexOp, Vec<Box<NodeEnum>>)> {
delspace(map_res(
tuple((
tag_token(TokenType::LBRACKET),
tag_token_symbol(TokenType::LBRACKET),
opt(logic_exp),
tag_token(TokenType::RBRACKET),
tag_token_symbol(TokenType::RBRACKET),
many0(comment),
)),
|(_, idx, (_, rr), com)| {
Expand Down
4 changes: 4 additions & 0 deletions src/nomparser/comment.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::nomparser::Span;
use crate::{ast::node::comment::CommentNode, ast::range::Range};
use internal_macro::{test_parser, test_parser_error};
use nom::{
branch::alt,
bytes::complete::{tag, take_until},
Expand All @@ -11,6 +12,9 @@ use nom_locate::LocatedSpan;

use super::*;

#[test_parser("//123")]
#[test_parser("/// 123\n")]
#[test_parser_error("/ / 123\n")]
pub fn comment(input: Span) -> IResult<Span, Box<NodeEnum>> {
map_res(
pair(
Expand Down
18 changes: 11 additions & 7 deletions src/nomparser/constval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,16 @@ pub fn number(input: Span) -> IResult<Span, Box<NodeEnum>> {
Ok((re, Box::new(node.into())))
}

#[test_parser("true")]
#[test_parser(" true")]
#[test_parser("false")]
#[test_parser_error("tru")]
#[test_parser_error("fales")]
pub fn bool_const(input: Span) -> IResult<Span, Box<NodeEnum>> {
alt((
map_res(tag_token(TokenType::TRUE), |(_, range)| {
map_res(tag_token_word(TokenType::TRUE), |(_, range)| {
res_enum(BoolConstNode { value: true, range }.into())
}),
map_res(tag_token(TokenType::FALSE), |(_, range)| {
map_res(tag_token_word(TokenType::FALSE), |(_, range)| {
res_enum(
BoolConstNode {
value: false,
Expand All @@ -57,6 +57,10 @@ pub fn bool_const(input: Span) -> IResult<Span, Box<NodeEnum>> {
))(input)
}

#[test_parser("123")]
#[test_parser("12_3")]
#[test_parser("1_2_3")]
#[test_parser_error("1 23")]
fn decimal(input: Span) -> IResult<Span, Span> {
recognize(many1(terminated(one_of("0123456789"), many0(char('_')))))(input)
}
Expand All @@ -70,8 +74,8 @@ fn float(input: Span) -> IResult<Span, Span> {
opt(tuple((
one_of("eE"),
opt(alt((
tag_token(TokenType::PLUS),
tag_token(TokenType::MINUS),
tag_token_symbol(TokenType::PLUS),
tag_token_symbol(TokenType::MINUS),
))),
decimal,
))),
Expand All @@ -81,8 +85,8 @@ fn float(input: Span) -> IResult<Span, Span> {
opt(preceded(char('.'), decimal)),
one_of("eE"),
opt(alt((
tag_token(TokenType::PLUS),
tag_token(TokenType::MINUS),
tag_token_symbol(TokenType::PLUS),
tag_token_symbol(TokenType::MINUS),
))),
decimal,
))), // Case three: 42. and 42.42
Expand Down
39 changes: 28 additions & 11 deletions src/nomparser/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,25 @@ use super::*;
return;
}"#
)]
#[test_parser_error(
"ifa > 1 {
a = 1;
} else {
a = 2;
}"
)]
/// ```ebnf
/// if_statement = "if" logic_exp statement_block ("else" (if_statement | statement_block))? ;
/// ```
pub fn if_statement(input: Span) -> IResult<Span, Box<NodeEnum>> {
map_res(
delspace(tuple((
tag_token(TokenType::IF),
tag_token_word(TokenType::IF),
parse_with_ex(logic_exp, true),
statement_block,
opt(delspace(comment)),
opt(preceded(
tag_token(TokenType::ELSE),
tag_token_word(TokenType::ELSE),
alt((
if_statement,
map_res(statement_block, |n| res_enum(n.into())),
Expand Down Expand Up @@ -98,13 +105,19 @@ pub fn if_statement(input: Span) -> IResult<Span, Box<NodeEnum>> {
}
"
)]
#[test_parser_error(
"whiletrue {
let a = b;
}
"
)]
/// ```ebnf
/// while_statement = "while" logic_exp statement_block ;
/// ```
pub fn while_statement(input: Span) -> IResult<Span, Box<NodeEnum>> {
map_res(
delspace(tuple((
tag_token(TokenType::WHILE),
tag_token_word(TokenType::WHILE),
alt_except(
logic_exp,
"{",
Expand Down Expand Up @@ -159,18 +172,22 @@ pub fn while_statement(input: Span) -> IResult<Span, Box<NodeEnum>> {

}"
)]

#[test_parser_error(
"forlet i = 0; i < 5; i = i + 1{

}"
)]
/// ```enbf
/// for_statement = "for" (assignment | new_variable) ";" logic_exp ";" assignment statement_block;
/// ```
pub fn for_statement(input: Span) -> IResult<Span, Box<NodeEnum>> {
map_res(
delspace(tuple((
tag_token(TokenType::FOR),
tag_token_word(TokenType::FOR),
opt(alt((assignment, new_variable))),
tag_token(TokenType::SEMI),
tag_token_symbol(TokenType::SEMI),
logic_exp,
tag_token(TokenType::SEMI),
tag_token_symbol(TokenType::SEMI),
opt(assignment),
statement_block,
opt(delspace(comment)),
Expand Down Expand Up @@ -204,8 +221,8 @@ pub fn for_statement(input: Span) -> IResult<Span, Box<NodeEnum>> {
pub fn break_statement(input: Span) -> IResult<Span, Box<NodeEnum>> {
map_res(
tuple((
tag_token(TokenType::BREAK),
tag_token(TokenType::SEMI),
tag_token_word(TokenType::BREAK),
tag_token_symbol(TokenType::SEMI),
opt(delspace(comment)),
)),
|(_, _, optcomment)| {
Expand All @@ -228,8 +245,8 @@ pub fn break_statement(input: Span) -> IResult<Span, Box<NodeEnum>> {
pub fn continue_statement(input: Span) -> IResult<Span, Box<NodeEnum>> {
map_res(
tuple((
tag_token(TokenType::CONTINUE),
tag_token(TokenType::SEMI),
tag_token_word(TokenType::CONTINUE),
tag_token_symbol(TokenType::SEMI),
opt(delspace(comment)),
)),
|(_, _, optcomment)| {
Expand Down
15 changes: 9 additions & 6 deletions src/nomparser/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ fn unary_exp(input: Span) -> IResult<Span, Box<NodeEnum>> {
pointer_exp,
map_res(
tuple((
alt((tag_token(TokenType::MINUS), tag_token(TokenType::NOT))),
alt((
tag_token_symbol(TokenType::MINUS),
tag_token_symbol(TokenType::NOT),
)),
pointer_exp,
)),
|((op, op_range), exp)| {
Expand All @@ -83,8 +86,8 @@ pub fn pointer_exp(input: Span) -> IResult<Span, Box<NodeEnum>> {
map_res(
delspace(pair(
many0(alt((
tag_token(TokenType::TAKE_PTR),
tag_token(TokenType::TAKE_VAL),
tag_token_symbol(TokenType::TAKE_PTR),
tag_token_symbol(TokenType::TAKE_VAL),
))),
complex_exp,
)),
Expand Down Expand Up @@ -221,7 +224,7 @@ fn primary_exp(input: Span) -> IResult<Span, Box<NodeEnum>> {
fn take_exp_op(input: Span) -> IResult<Span, (ComplexOp, Vec<Box<NodeEnum>>)> {
delspace(map_res(
preceded(
tag_token(TokenType::DOT),
tag_token_symbol(TokenType::DOT),
pair(opt(identifier), many0(comment)),
),
|(idx, coms)| Ok::<_, Error>((ComplexOp::FieldOp(idx), coms)),
Expand All @@ -236,9 +239,9 @@ fn take_exp_op(input: Span) -> IResult<Span, (ComplexOp, Vec<Box<NodeEnum>>)> {
fn parantheses_exp(input: Span) -> IResult<Span, Box<NodeEnum>> {
map_res(
delimited(
tag_token(TokenType::LPAREN),
tag_token_symbol(TokenType::LPAREN),
parse_with_ex(logic_exp, false),
tag_token(TokenType::RPAREN),
tag_token_symbol(TokenType::RPAREN),
),
|exp| {
res_enum(
Expand Down
19 changes: 10 additions & 9 deletions src/nomparser/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use nom::{
use crate::nomparser::Span;
use crate::{ast::node::function::FuncDefNode, ast::tokens::TokenType};

use internal_macro::test_parser;
use internal_macro::{test_parser, test_parser_error};

use super::*;

Expand Down Expand Up @@ -44,25 +44,26 @@ use super::*;
"
)]
#[test_parser("fn f( \n) int;")]
#[test_parser_error("fnf( \n) int;")]
pub fn function_def(input: Span) -> IResult<Span, Box<TopLevel>> {
map_res(
tuple((
many0(del_newline_or_space!(comment)),
tag_token(TokenType::FN),
tag_token_word(TokenType::FN),
identifier,
opt(generic_type_def),
tag_token(TokenType::LPAREN),
tag_token_symbol(TokenType::LPAREN),
del_newline_or_space!(separated_list0(
tag_token(TokenType::COMMA),
tag_token_symbol(TokenType::COMMA),
del_newline_or_space!(typed_identifier),
)),
tag_token(TokenType::RPAREN),
tag_token_symbol(TokenType::RPAREN),
type_name,
alt((
map_res(statement_block, |b| {
Ok::<_, Error>((Some(b.clone()), b.range))
}),
map_res(tag_token(TokenType::SEMI), |(_, range)| {
map_res(tag_token_symbol(TokenType::SEMI), |(_, range)| {
Ok::<_, Error>((None, range))
}),
)),
Expand Down Expand Up @@ -104,12 +105,12 @@ pub fn call_function_op(input: Span) -> IResult<Span, (ComplexOp, Vec<Box<NodeEn
delspace(map_res(
tuple((
opt(generic_param_def),
tag_token(TokenType::LPAREN),
tag_token_symbol(TokenType::LPAREN),
del_newline_or_space!(separated_list0(
tag_token(TokenType::COMMA),
tag_token_symbol(TokenType::COMMA),
del_newline_or_space!(logic_exp)
)),
tag_token(TokenType::RPAREN),
tag_token_symbol(TokenType::RPAREN),
many0(comment),
)),
|(generic, (_, st), paras, (_, end), com)| {
Expand Down
21 changes: 20 additions & 1 deletion src/nomparser/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,40 @@ use std::fmt::Error;

use crate::nomparser::Span;
use crate::{ast::range::Range, ast::tokens::TokenType};
use nom::character::is_alphanumeric;
use nom::sequence::preceded;
use nom::{
bytes::complete::tag, character::complete::space0, combinator::map_res, error::ParseError,
sequence::delimited, AsChar, IResult, InputTake, InputTakeAtPosition, Parser,
};

use super::*;

pub fn tag_token(token: TokenType) -> impl Fn(Span) -> IResult<Span, (TokenType, Range)> {
pub fn tag_token_symbol(token: TokenType) -> impl Fn(Span) -> IResult<Span, (TokenType, Range)> {
move |input| {
map_res(delspace(tag(token.get_str())), |_out: Span| {
let end = _out.take_split(token.get_str().len()).0;
Ok::<(TokenType, Range), Error>((token, Range::new(_out, end)))
})(input)
}
}

/// 不能直接接 `字母`、`数字` 或 `_`,用于关键字
pub fn tag_token_word(token: TokenType) -> impl Fn(Span) -> IResult<Span, (TokenType, Range)> {
move |input| {
let (s1, s2): (LocatedSpan<&str, bool>, LocatedSpan<&str, bool>) =
preceded(space0, tag(token.get_str()))(input)?;
if s1.starts_with(|c: char| is_alphanumeric(c as u8) || c == '_') {
return Err(nom::Err::Error(nom::error::Error::new(
s2,
nom::error::ErrorKind::Tag,
)));
} else {
let end = s2.take_split(token.get_str().len()).0;
return Ok((s1, (token, Range::new(s2, end))));
}
}
}
pub fn delspace<I, O, E, G>(parser: G) -> impl FnMut(I) -> IResult<I, O, E>
where
G: Parser<I, O, E>,
Expand Down
11 changes: 7 additions & 4 deletions src/nomparser/identifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,12 @@ use super::*;
pub fn extern_identifier(input: Span) -> IResult<Span, Box<NodeEnum>> {
delspace(map_res(
tuple((
separated_list1(tag_token(TokenType::DOUBLE_COLON), delspace(identifier)),
opt(tag_token(TokenType::DOUBLE_COLON)), // 容忍未写完的语句
opt(tag_token(TokenType::COLON)), // 容忍未写完的语句
separated_list1(
tag_token_symbol(TokenType::DOUBLE_COLON),
delspace(identifier),
),
opt(tag_token_symbol(TokenType::DOUBLE_COLON)), // 容忍未写完的语句
opt(tag_token_symbol(TokenType::COLON)), // 容忍未写完的语句
)),
|(mut ns, opt, opt2)| {
let id = ns.pop().unwrap();
Expand Down Expand Up @@ -88,7 +91,7 @@ pub fn typed_identifier(input: Span) -> IResult<Span, Box<TypedIdentifierNode>>
delspace(map_res(
tuple((
identifier,
tag_token(TokenType::COLON),
tag_token_symbol(TokenType::COLON),
opt(type_name),
opt(comment),
)),
Expand Down
Loading