Basic pattern parsing
This commit is contained in:
parent
c163224fb7
commit
dc50dd1545
|
@ -214,13 +214,9 @@ impl Default for ParserMeta {
|
||||||
|
|
||||||
/// Parser for AlexScript code.
|
/// Parser for AlexScript code.
|
||||||
pub fn parser<'a>(m: &'a ParserMeta) -> impl Parser<char, SyntaxTree, Error = Simple<char>> + 'a {
|
pub fn parser<'a>(m: &'a ParserMeta) -> impl Parser<char, SyntaxTree, Error = Simple<char>> + 'a {
|
||||||
// parse_statement(m)
|
parse_statement(m)
|
||||||
// .repeated()
|
|
||||||
// .map(SyntaxTree)
|
|
||||||
// .then_ignore(end())
|
|
||||||
parse_expression(m)
|
|
||||||
.then_ignore(just(";").then(whitespace()))
|
|
||||||
.repeated()
|
.repeated()
|
||||||
|
.map(SyntaxTree)
|
||||||
.then_ignore(end())
|
.then_ignore(end())
|
||||||
.map(|exprs| {
|
.map(|exprs| {
|
||||||
println!("{:#?}", exprs);
|
println!("{:#?}", exprs);
|
||||||
|
@ -288,7 +284,8 @@ fn parse_func_decl<'a>(
|
||||||
m: &'a ParserMeta,
|
m: &'a ParserMeta,
|
||||||
) -> impl Parser<char, ClassMember, Error = Simple<char>> + 'a {
|
) -> impl Parser<char, ClassMember, Error = Simple<char>> + 'a {
|
||||||
keyword("def")
|
keyword("def")
|
||||||
.ignore_then(ident())
|
.then_ignore(whitespace())
|
||||||
|
.ignore_then(ident().then_ignore(whitespace()))
|
||||||
.then(parse_pattern(m).repeated())
|
.then(parse_pattern(m).repeated())
|
||||||
.then(
|
.then(
|
||||||
just('=')
|
just('=')
|
||||||
|
@ -479,13 +476,31 @@ fn parse_literal(_m: &ParserMeta) -> impl Parser<char, Expr, Error = Simple<char
|
||||||
.map(|(l, r)| (l + "." + &r).parse().unwrap())
|
.map(|(l, r)| (l + "." + &r).parse().unwrap())
|
||||||
.map(Literal::Float);
|
.map(Literal::Float);
|
||||||
|
|
||||||
choice((int, float, string)).map(Expr::Literal)
|
choice((int, float, string))
|
||||||
|
.then_ignore(whitespace())
|
||||||
|
.map(Expr::Literal)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_type(_m: &ParserMeta) -> impl Parser<char, Type, Error = Simple<char>> {
|
fn parse_type(_m: &ParserMeta) -> impl Parser<char, Type, Error = Simple<char>> {
|
||||||
todo()
|
todo()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_pattern(_m: &ParserMeta) -> impl Parser<char, Pattern, Error = Simple<char>> {
|
fn parse_pattern(m: &ParserMeta) -> impl Parser<char, Pattern, Error = Simple<char>> {
|
||||||
todo()
|
choice((parse_capture_pattern(m),))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_capture_pattern(_m: &ParserMeta) -> impl Parser<char, Pattern, Error = Simple<char>> {
|
||||||
|
ident()
|
||||||
|
.try_map(|iden: String, span| {
|
||||||
|
if iden.starts_with(|c: char| c.is_lowercase()) {
|
||||||
|
Ok(iden)
|
||||||
|
} else {
|
||||||
|
Err(Simple::custom(
|
||||||
|
span,
|
||||||
|
"capture must start with lowercase letter",
|
||||||
|
))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.then_ignore(whitespace())
|
||||||
|
.map(Pattern::Exact)
|
||||||
}
|
}
|
||||||
|
|
|
@ -194,9 +194,8 @@ pub enum Type {
|
||||||
/// The function being applied. This must be a generic type.
|
/// The function being applied. This must be a generic type.
|
||||||
function: Box<Type>,
|
function: Box<Type>,
|
||||||
|
|
||||||
/// The expression given as an argument to the type. This can be any expression, to allow
|
/// The type given as an argument to the type.
|
||||||
/// const generics; in most cases, though, it should be just a normal type.
|
expression: Box<Type>,
|
||||||
expression: Box<Expr>,
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/// `(a, b)`
|
/// `(a, b)`
|
||||||
|
@ -210,6 +209,9 @@ pub enum Type {
|
||||||
/// statements.
|
/// statements.
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub enum Pattern {
|
pub enum Pattern {
|
||||||
|
/// `a`
|
||||||
|
Capture(String),
|
||||||
|
|
||||||
/// `(a, b)`
|
/// `(a, b)`
|
||||||
Tuple(Vec<Pattern>),
|
Tuple(Vec<Pattern>),
|
||||||
|
|
||||||
|
@ -228,9 +230,6 @@ pub enum Pattern {
|
||||||
/// `Foo { a: x, b: y, ... }`
|
/// `Foo { a: x, b: y, ... }`
|
||||||
Destructure(String, Record),
|
Destructure(String, Record),
|
||||||
|
|
||||||
/// `a`
|
|
||||||
Capture(String),
|
|
||||||
|
|
||||||
/// `_`
|
/// `_`
|
||||||
Ignore,
|
Ignore,
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue