More work
This commit is contained in:
parent
8f0dc6cab8
commit
ce848906c9
@ -5,12 +5,12 @@ use std::str::FromStr;
|
|||||||
|
|
||||||
use nom::IResult;
|
use nom::IResult;
|
||||||
use nom::character::complete::{one_of, space0, alphanumeric0};
|
use nom::character::complete::{one_of, space0, alphanumeric0};
|
||||||
use nom::bytes::complete::{tag, take, take_while, take_until};
|
use nom::bytes::complete::{tag, take, take_while, take_while1, take_until};
|
||||||
use nom::combinator::{map, map_res, value, opt, verify};
|
use nom::combinator::{map, map_res, value, opt, verify};
|
||||||
use nom::multi::{separated_list, separated_nonempty_list, many1, many0};
|
use nom::multi::{separated_list, separated_nonempty_list, many1, many0};
|
||||||
use nom::error::{context, VerboseError};
|
use nom::error::{context, VerboseError};
|
||||||
use nom::branch::alt;
|
use nom::branch::alt;
|
||||||
use nom::sequence::{pair, delimited};
|
use nom::sequence::{pair, delimited, preceded};
|
||||||
|
|
||||||
use crate::ast::*;
|
use crate::ast::*;
|
||||||
use crate::builtin::Builtin;
|
use crate::builtin::Builtin;
|
||||||
@ -40,12 +40,12 @@ fn identifier(text: &str) -> ParseResult<Rc<String>> {
|
|||||||
|
|
||||||
const OPERATOR_CHARS: &'static str = "~`!@#$%^&*-+=<>?/|";
|
const OPERATOR_CHARS: &'static str = "~`!@#$%^&*-+=<>?/|";
|
||||||
fn parse_binop(text: &str) -> ParseResult<BinOp> {
|
fn parse_binop(text: &str) -> ParseResult<BinOp> {
|
||||||
let (text, op): (_, Vec<char>) = context("binop yo", many1(one_of(OPERATOR_CHARS)))(text)?;
|
let (text, op): (_, Vec<char>) = context("Binop", many1(one_of(OPERATOR_CHARS)))(text)?;
|
||||||
let sigil: String = op.into_iter().collect();
|
let sigil: String = op.into_iter().collect();
|
||||||
Ok((text, BinOp::from_sigil(&sigil)))
|
Ok((text, BinOp::from_sigil(&sigil)))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_bool_literal(text: &str) -> ParseResult<ExpressionKind> {
|
fn bool_literal(text: &str) -> ParseResult<ExpressionKind> {
|
||||||
let p = alt((
|
let p = alt((
|
||||||
value(true, tag("true")),
|
value(true, tag("true")),
|
||||||
value(false, tag("false"))
|
value(false, tag("false"))
|
||||||
@ -53,7 +53,7 @@ fn parse_bool_literal(text: &str) -> ParseResult<ExpressionKind> {
|
|||||||
map(p, ExpressionKind::BoolLiteral)(text)
|
map(p, ExpressionKind::BoolLiteral)(text)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_number_literal(text: &str) -> ParseResult<ExpressionKind> {
|
fn number_literal(text: &str) -> ParseResult<ExpressionKind> {
|
||||||
let num_lit = many1(alt((
|
let num_lit = many1(alt((
|
||||||
map(one_of("1234567890"), |s: char| Some(s)),
|
map(one_of("1234567890"), |s: char| Some(s)),
|
||||||
map(nom::character::complete::char('_'), |_| None)
|
map(nom::character::complete::char('_'), |_| None)
|
||||||
@ -69,27 +69,25 @@ fn parse_number_literal(text: &str) -> ParseResult<ExpressionKind> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn parse_binary_literal(input: &str) -> ParseResult<ExpressionKind> {
|
fn binary_literal(input: &str) -> ParseResult<ExpressionKind> {
|
||||||
let (rest, _) = tag("0b")(input)?;
|
let p = preceded(tag("0b"), take_while1(|c: char| c == '0' || c == '1'));
|
||||||
let (rest, n): (&str, u64) = map_res(
|
let (rest, n): (&str, u64) = map_res(
|
||||||
take_while(|c: char| c == '0' || c == '1'),
|
p, |hex_str: &str| u64::from_str_radix(hex_str, 2)
|
||||||
|hex_str: &str| u64::from_str_radix(hex_str, 2)
|
)(input)?;
|
||||||
)(rest)?;
|
|
||||||
let expr = ExpressionKind::NatLiteral(n);
|
let expr = ExpressionKind::NatLiteral(n);
|
||||||
Ok((rest, expr))
|
Ok((rest, expr))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_hex_literal(input: &str) -> ParseResult<ExpressionKind> {
|
fn hex_literal(input: &str) -> ParseResult<ExpressionKind> {
|
||||||
let (rest, _) = tag("0x")(input)?;
|
let p = preceded(tag("0x"), take_while1(|c: char| c.is_digit(16)));
|
||||||
let (rest, n): (&str, u64) = map_res(
|
let (rest, n): (&str, u64) = map_res(
|
||||||
take_while(|c: char| c.is_digit(16)),
|
p, |hex_str: &str| u64::from_str_radix(hex_str, 16)
|
||||||
|hex_str: &str| u64::from_str_radix(hex_str, 16)
|
)(input)?;
|
||||||
)(rest)?;
|
|
||||||
let expr = ExpressionKind::NatLiteral(n);
|
let expr = ExpressionKind::NatLiteral(n);
|
||||||
Ok((rest, expr))
|
Ok((rest, expr))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_string_literal(text: &str) -> ParseResult<ExpressionKind> {
|
fn string_literal(text: &str) -> ParseResult<ExpressionKind> {
|
||||||
use nom::character::complete::char;
|
use nom::character::complete::char;
|
||||||
let (text, string_output) = delimited(
|
let (text, string_output) = delimited(
|
||||||
char('"'), take_until("\""), char('"')
|
char('"'), take_until("\""), char('"')
|
||||||
@ -100,11 +98,11 @@ fn parse_string_literal(text: &str) -> ParseResult<ExpressionKind> {
|
|||||||
|
|
||||||
fn literal(input: &str) -> ParseResult<ExpressionKind> {
|
fn literal(input: &str) -> ParseResult<ExpressionKind> {
|
||||||
alt((
|
alt((
|
||||||
parse_string_literal,
|
string_literal,
|
||||||
parse_hex_literal,
|
hex_literal,
|
||||||
parse_binary_literal,
|
binary_literal,
|
||||||
parse_number_literal,
|
number_literal,
|
||||||
parse_bool_literal
|
bool_literal
|
||||||
))(input)
|
))(input)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -216,10 +214,11 @@ fn expression_kind(text: &str) -> ParseResult<ExpressionKind> {
|
|||||||
|
|
||||||
pub fn perform_parsing(input: &str) -> Result<String, String> {
|
pub fn perform_parsing(input: &str) -> Result<String, String> {
|
||||||
let output = match expression_kind(input) {
|
let output = match expression_kind(input) {
|
||||||
Ok((rest, ast)) => format!("{:?}", ast),
|
Ok((rest, ast)) => format!("{:?} (rest: {})", ast, rest),
|
||||||
Err(nom::Err::Incomplete(needed)) => format!("Incomplete: {:?}" ,needed),
|
Err(nom::Err::Incomplete(needed)) => format!("Incomplete: {:?}" ,needed),
|
||||||
Err(nom::Err::Error(verbose_error) | nom::Err::Failure(verbose_error)) => {
|
Err(nom::Err::Error(verbose_error) | nom::Err::Failure(verbose_error)) => {
|
||||||
nom::error::convert_error(input, verbose_error)
|
format!("Verbose Error: ` {:?} `", verbose_error)
|
||||||
|
//nom::error::convert_error(input, verbose_error)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user