forked from Orchid/orchid
Updated everything and moved to hard tab indentation
This commit is contained in:
@@ -4,12 +4,12 @@ version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
itertools = "0.13.0"
|
||||
itertools = "0.14.0"
|
||||
never = "0.1.0"
|
||||
once_cell = "1.19.0"
|
||||
once_cell = "1.20.2"
|
||||
orchid-api = { version = "0.1.0", path = "../orchid-api" }
|
||||
orchid-api-derive = { version = "0.1.0", path = "../orchid-api-derive" }
|
||||
orchid-api-traits = { version = "0.1.0", path = "../orchid-api-traits" }
|
||||
orchid-base = { version = "0.1.0", path = "../orchid-base" }
|
||||
orchid-extension = { version = "0.1.0", path = "../orchid-extension" }
|
||||
ordered-float = "4.2.1"
|
||||
ordered-float = "4.6.0"
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use orchid_api_derive::Coding;
|
||||
use orchid_base::error::OrcRes;
|
||||
use orchid_extension::atom::{AtomFactory, MethodSet, Atomic, AtomicFeatures, ToAtom, TypAtom};
|
||||
use orchid_extension::atom::{AtomFactory, Atomic, AtomicFeatures, MethodSet, ToAtom, TypAtom};
|
||||
use orchid_extension::atom_thin::{ThinAtom, ThinVariant};
|
||||
use orchid_extension::conv::TryFromExpr;
|
||||
use orchid_extension::expr::Expr;
|
||||
@@ -9,49 +9,47 @@ use ordered_float::NotNan;
|
||||
#[derive(Clone, Debug, Coding)]
|
||||
pub struct Int(pub i64);
|
||||
impl Atomic for Int {
|
||||
type Variant = ThinVariant;
|
||||
type Data = Self;
|
||||
fn reg_reqs() -> MethodSet<Self> {
|
||||
MethodSet::new()
|
||||
}
|
||||
type Variant = ThinVariant;
|
||||
type Data = Self;
|
||||
fn reg_reqs() -> MethodSet<Self> { MethodSet::new() }
|
||||
}
|
||||
impl ThinAtom for Int {}
|
||||
impl TryFromExpr for Int {
|
||||
fn try_from_expr(expr: Expr) -> OrcRes<Self> {
|
||||
TypAtom::<Int>::try_from_expr(expr).map(|t| t.value)
|
||||
}
|
||||
fn try_from_expr(expr: Expr) -> OrcRes<Self> {
|
||||
TypAtom::<Int>::try_from_expr(expr).map(|t| t.value)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Coding)]
|
||||
pub struct Float(pub NotNan<f64>);
|
||||
impl Atomic for Float {
|
||||
type Variant = ThinVariant;
|
||||
type Data = Self;
|
||||
fn reg_reqs() -> MethodSet<Self> { MethodSet::new() }
|
||||
type Variant = ThinVariant;
|
||||
type Data = Self;
|
||||
fn reg_reqs() -> MethodSet<Self> { MethodSet::new() }
|
||||
}
|
||||
impl ThinAtom for Float {}
|
||||
impl TryFromExpr for Float {
|
||||
fn try_from_expr(expr: Expr) -> OrcRes<Self> {
|
||||
TypAtom::<Float>::try_from_expr(expr).map(|t| t.value)
|
||||
}
|
||||
fn try_from_expr(expr: Expr) -> OrcRes<Self> {
|
||||
TypAtom::<Float>::try_from_expr(expr).map(|t| t.value)
|
||||
}
|
||||
}
|
||||
|
||||
pub enum Numeric {
|
||||
Int(i64),
|
||||
Float(NotNan<f64>),
|
||||
Int(i64),
|
||||
Float(NotNan<f64>),
|
||||
}
|
||||
impl TryFromExpr for Numeric {
|
||||
fn try_from_expr(expr: Expr) -> OrcRes<Self> {
|
||||
Int::try_from_expr(expr.clone())
|
||||
.map(|t| Numeric::Int(t.0))
|
||||
.or_else(|e| Float::try_from_expr(expr).map(|t| Numeric::Float(t.0)).map_err(|e2| e + e2))
|
||||
}
|
||||
fn try_from_expr(expr: Expr) -> OrcRes<Self> {
|
||||
Int::try_from_expr(expr.clone())
|
||||
.map(|t| Numeric::Int(t.0))
|
||||
.or_else(|e| Float::try_from_expr(expr).map(|t| Numeric::Float(t.0)).map_err(|e2| e + e2))
|
||||
}
|
||||
}
|
||||
impl ToAtom for Numeric {
|
||||
fn to_atom_factory(self) -> AtomFactory {
|
||||
match self {
|
||||
Self::Float(f) => Float(f).factory(),
|
||||
Self::Int(i) => Int(i).factory(),
|
||||
}
|
||||
}
|
||||
fn to_atom_factory(self) -> AtomFactory {
|
||||
match self {
|
||||
Self::Float(f) => Float(f).factory(),
|
||||
Self::Int(i) => Int(i).factory(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use std::ops::RangeInclusive;
|
||||
|
||||
use orchid_base::error::OrcRes;
|
||||
use orchid_base::number::{num_to_err, parse_num, Numeric};
|
||||
use orchid_base::number::{Numeric, num_to_err, parse_num};
|
||||
use orchid_extension::atom::AtomicFeatures;
|
||||
use orchid_extension::lexer::{LexContext, Lexer};
|
||||
use orchid_extension::tree::{GenTok, GenTokTree};
|
||||
@@ -12,16 +12,16 @@ use super::num_atom::{Float, Int};
|
||||
#[derive(Default)]
|
||||
pub struct NumLexer;
|
||||
impl Lexer for NumLexer {
|
||||
const CHAR_FILTER: &'static [RangeInclusive<char>] = &['0'..='9'];
|
||||
fn lex<'a>(all: &'a str, ctx: &'a LexContext<'a>) -> OrcRes<(&'a str, GenTokTree<'a>)> {
|
||||
let ends_at = all.find(|c: char| !c.is_ascii_hexdigit() && !"xX._pP".contains(c));
|
||||
let (chars, tail) = all.split_at(ends_at.unwrap_or(all.len()));
|
||||
let fac = match parse_num(chars) {
|
||||
Ok(Numeric::Float(f)) => Float(f).factory(),
|
||||
Ok(Numeric::Uint(uint)) => Int(uint.try_into().unwrap()).factory(),
|
||||
Ok(Numeric::Decimal(dec)) => Float(NotNan::new(dec.try_into().unwrap()).unwrap()).factory(),
|
||||
Err(e) => return Err(num_to_err(e, ctx.pos(all)).into()),
|
||||
};
|
||||
Ok((tail, GenTok::X(fac).at(ctx.pos(all)..ctx.pos(tail))))
|
||||
}
|
||||
const CHAR_FILTER: &'static [RangeInclusive<char>] = &['0'..='9'];
|
||||
fn lex<'a>(all: &'a str, ctx: &'a LexContext<'a>) -> OrcRes<(&'a str, GenTokTree<'a>)> {
|
||||
let ends_at = all.find(|c: char| !c.is_ascii_hexdigit() && !"xX._pP".contains(c));
|
||||
let (chars, tail) = all.split_at(ends_at.unwrap_or(all.len()));
|
||||
let fac = match parse_num(chars) {
|
||||
Ok(Numeric::Float(f)) => Float(f).factory(),
|
||||
Ok(Numeric::Uint(uint)) => Int(uint.try_into().unwrap()).factory(),
|
||||
Ok(Numeric::Decimal(dec)) => Float(NotNan::new(dec.try_into().unwrap()).unwrap()).factory(),
|
||||
Err(e) => return Err(num_to_err(e, ctx.pos(all)).into()),
|
||||
};
|
||||
Ok((tail, GenTok::X(fac).at(ctx.pos(all)..ctx.pos(tail))))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,40 +7,40 @@ use orchid_extension::entrypoint::ExtReq;
|
||||
use orchid_extension::fs::DeclFs;
|
||||
use orchid_extension::system::{System, SystemCard};
|
||||
use orchid_extension::system_ctor::SystemCtor;
|
||||
use orchid_extension::tree::{comments, fun, module, root_mod, MemKind};
|
||||
use orchid_extension::tree::{MemKind, comments, fun, module, root_mod};
|
||||
|
||||
use crate::OrcString;
|
||||
use crate::number::num_atom::{Float, Int};
|
||||
use crate::string::str_atom::{IntStrAtom, StrAtom};
|
||||
use crate::string::str_lexer::StringLexer;
|
||||
use crate::OrcString;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct StdSystem;
|
||||
impl SystemCtor for StdSystem {
|
||||
type Deps = ();
|
||||
type Instance = Self;
|
||||
const NAME: &'static str = "orchid::std";
|
||||
const VERSION: f64 = 0.00_01;
|
||||
fn inst() -> Option<Self::Instance> { Some(StdSystem) }
|
||||
type Deps = ();
|
||||
type Instance = Self;
|
||||
const NAME: &'static str = "orchid::std";
|
||||
const VERSION: f64 = 0.00_01;
|
||||
fn inst() -> Option<Self::Instance> { Some(StdSystem) }
|
||||
}
|
||||
impl SystemCard for StdSystem {
|
||||
type Ctor = Self;
|
||||
type Req = Never;
|
||||
fn atoms() -> impl IntoIterator<Item = Option<Box<dyn AtomDynfo>>> {
|
||||
[Some(Int::dynfo()), Some(Float::dynfo()), Some(StrAtom::dynfo()), Some(IntStrAtom::dynfo())]
|
||||
}
|
||||
type Ctor = Self;
|
||||
type Req = Never;
|
||||
fn atoms() -> impl IntoIterator<Item = Option<Box<dyn AtomDynfo>>> {
|
||||
[Some(Int::dynfo()), Some(Float::dynfo()), Some(StrAtom::dynfo()), Some(IntStrAtom::dynfo())]
|
||||
}
|
||||
}
|
||||
impl System for StdSystem {
|
||||
fn request(_: ExtReq, req: Self::Req) -> orchid_base::reqnot::Receipt { match req {} }
|
||||
fn lexers() -> Vec<orchid_extension::lexer::LexerObj> { vec![&StringLexer] }
|
||||
fn parsers() -> Vec<orchid_extension::parser::ParserObj> { vec![] }
|
||||
fn vfs() -> DeclFs { DeclFs::Mod(&[]) }
|
||||
fn env() -> Vec<(Tok<String>, MemKind)> {
|
||||
vec![root_mod("std", [], [module(true, "string", [], [comments(
|
||||
["Concatenate two strings"],
|
||||
fun(true, "concat", |left: OrcString, right: OrcString| {
|
||||
StrAtom::new(Arc::new(left.get_string().to_string() + &right.get_string()))
|
||||
}),
|
||||
)])])]
|
||||
}
|
||||
fn request(_: ExtReq, req: Self::Req) -> orchid_base::reqnot::Receipt { match req {} }
|
||||
fn lexers() -> Vec<orchid_extension::lexer::LexerObj> { vec![&StringLexer] }
|
||||
fn parsers() -> Vec<orchid_extension::parser::ParserObj> { vec![] }
|
||||
fn vfs() -> DeclFs { DeclFs::Mod(&[]) }
|
||||
fn env() -> Vec<(Tok<String>, MemKind)> {
|
||||
vec![root_mod("std", [], [module(true, "string", [], [comments(
|
||||
["Concatenate two strings"],
|
||||
fun(true, "concat", |left: OrcString, right: OrcString| {
|
||||
StrAtom::new(Arc::new(left.get_string().to_string() + &right.get_string()))
|
||||
}),
|
||||
)])])]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,9 +5,9 @@ use std::sync::Arc;
|
||||
|
||||
use orchid_api_derive::Coding;
|
||||
use orchid_api_traits::{Encode, Request};
|
||||
use orchid_base::error::{mk_errv, OrcRes};
|
||||
use orchid_base::error::{OrcRes, mk_errv};
|
||||
use orchid_base::intern;
|
||||
use orchid_base::interner::{intern, Tok};
|
||||
use orchid_base::interner::{Tok, intern};
|
||||
use orchid_extension::atom::{AtomMethod, Atomic, MethodSet, Supports, TypAtom};
|
||||
use orchid_extension::atom_owned::{DeserializeCtx, OwnedAtom, OwnedVariant};
|
||||
use orchid_extension::conv::TryFromExpr;
|
||||
@@ -17,83 +17,83 @@ use orchid_extension::system::SysCtx;
|
||||
#[derive(Copy, Clone, Coding)]
|
||||
pub struct StringGetVal;
|
||||
impl Request for StringGetVal {
|
||||
type Response = Arc<String>;
|
||||
type Response = Arc<String>;
|
||||
}
|
||||
impl AtomMethod for StringGetVal {
|
||||
const NAME: &str = "std::string_get_val";
|
||||
const NAME: &str = "std::string_get_val";
|
||||
}
|
||||
impl Supports<StringGetVal> for StrAtom {
|
||||
fn handle(&self, _: SysCtx, _: StringGetVal) -> <StringGetVal as Request>::Response {
|
||||
self.0.clone()
|
||||
}
|
||||
fn handle(&self, _: SysCtx, _: StringGetVal) -> <StringGetVal as Request>::Response {
|
||||
self.0.clone()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct StrAtom(Arc<String>);
|
||||
impl Atomic for StrAtom {
|
||||
type Variant = OwnedVariant;
|
||||
type Data = ();
|
||||
fn reg_reqs() -> MethodSet<Self> { MethodSet::new().handle::<StringGetVal>() }
|
||||
type Variant = OwnedVariant;
|
||||
type Data = ();
|
||||
fn reg_reqs() -> MethodSet<Self> { MethodSet::new().handle::<StringGetVal>() }
|
||||
}
|
||||
impl StrAtom {
|
||||
pub fn new(str: Arc<String>) -> Self { Self(str) }
|
||||
pub fn value(&self) -> Arc<String> { self.0.clone() }
|
||||
pub fn new(str: Arc<String>) -> Self { Self(str) }
|
||||
pub fn value(&self) -> Arc<String> { self.0.clone() }
|
||||
}
|
||||
impl Deref for StrAtom {
|
||||
type Target = str;
|
||||
fn deref(&self) -> &Self::Target { &self.0 }
|
||||
type Target = str;
|
||||
fn deref(&self) -> &Self::Target { &self.0 }
|
||||
}
|
||||
impl OwnedAtom for StrAtom {
|
||||
type Refs = ();
|
||||
fn val(&self) -> Cow<'_, Self::Data> { Cow::Owned(()) }
|
||||
fn serialize(&self, _: SysCtx, sink: &mut (impl io::Write + ?Sized)) -> Self::Refs {
|
||||
self.deref().encode(sink)
|
||||
}
|
||||
fn deserialize(mut ctx: impl DeserializeCtx, _: Self::Refs) -> Self {
|
||||
Self::new(Arc::new(ctx.read::<String>()))
|
||||
}
|
||||
type Refs = ();
|
||||
fn val(&self) -> Cow<'_, Self::Data> { Cow::Owned(()) }
|
||||
fn serialize(&self, _: SysCtx, sink: &mut (impl io::Write + ?Sized)) -> Self::Refs {
|
||||
self.deref().encode(sink)
|
||||
}
|
||||
fn deserialize(mut ctx: impl DeserializeCtx, _: Self::Refs) -> Self {
|
||||
Self::new(Arc::new(ctx.read::<String>()))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct IntStrAtom(Tok<String>);
|
||||
impl Atomic for IntStrAtom {
|
||||
type Variant = OwnedVariant;
|
||||
type Data = orchid_api::TStr;
|
||||
fn reg_reqs() -> MethodSet<Self> { MethodSet::new() }
|
||||
type Variant = OwnedVariant;
|
||||
type Data = orchid_api::TStr;
|
||||
fn reg_reqs() -> MethodSet<Self> { MethodSet::new() }
|
||||
}
|
||||
impl From<Tok<String>> for IntStrAtom {
|
||||
fn from(value: Tok<String>) -> Self { Self(value) }
|
||||
fn from(value: Tok<String>) -> Self { Self(value) }
|
||||
}
|
||||
impl OwnedAtom for IntStrAtom {
|
||||
type Refs = ();
|
||||
fn val(&self) -> Cow<'_, Self::Data> { Cow::Owned(self.0.to_api()) }
|
||||
fn print(&self, _ctx: SysCtx) -> String { format!("{:?}i", *self.0) }
|
||||
fn serialize(&self, _: SysCtx, write: &mut (impl io::Write + ?Sized)) { self.0.encode(write) }
|
||||
fn deserialize(ctx: impl DeserializeCtx, _: ()) -> Self { Self(intern(&ctx.decode::<String>())) }
|
||||
type Refs = ();
|
||||
fn val(&self) -> Cow<'_, Self::Data> { Cow::Owned(self.0.to_api()) }
|
||||
fn print(&self, _ctx: SysCtx) -> String { format!("{:?}i", *self.0) }
|
||||
fn serialize(&self, _: SysCtx, write: &mut (impl io::Write + ?Sized)) { self.0.encode(write) }
|
||||
fn deserialize(ctx: impl DeserializeCtx, _: ()) -> Self { Self(intern(&ctx.decode::<String>())) }
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum OrcString<'a> {
|
||||
Val(TypAtom<'a, StrAtom>),
|
||||
Int(TypAtom<'a, IntStrAtom>),
|
||||
Val(TypAtom<'a, StrAtom>),
|
||||
Int(TypAtom<'a, IntStrAtom>),
|
||||
}
|
||||
impl OrcString<'_> {
|
||||
pub fn get_string(&self) -> Arc<String> {
|
||||
match &self {
|
||||
Self::Int(tok) => Tok::from_api(tok.value).arc(),
|
||||
Self::Val(atom) => atom.request(StringGetVal),
|
||||
}
|
||||
}
|
||||
pub fn get_string(&self) -> Arc<String> {
|
||||
match &self {
|
||||
Self::Int(tok) => Tok::from_api(tok.value).arc(),
|
||||
Self::Val(atom) => atom.request(StringGetVal),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFromExpr for OrcString<'static> {
|
||||
fn try_from_expr(expr: Expr) -> OrcRes<OrcString<'static>> {
|
||||
if let Ok(v) = TypAtom::<StrAtom>::try_from_expr(expr.clone()) {
|
||||
return Ok(OrcString::Val(v));
|
||||
}
|
||||
match TypAtom::<IntStrAtom>::try_from_expr(expr) {
|
||||
Ok(t) => Ok(OrcString::Int(t)),
|
||||
Err(e) => Err(mk_errv(intern!(str: "A string was expected"), "", e.pos_iter())),
|
||||
}
|
||||
}
|
||||
fn try_from_expr(expr: Expr) -> OrcRes<OrcString<'static>> {
|
||||
if let Ok(v) = TypAtom::<StrAtom>::try_from_expr(expr.clone()) {
|
||||
return Ok(OrcString::Val(v));
|
||||
}
|
||||
match TypAtom::<IntStrAtom>::try_from_expr(expr) {
|
||||
Ok(t) => Ok(OrcString::Int(t)),
|
||||
Err(e) => Err(mk_errv(intern!(str: "A string was expected"), "", e.pos_iter())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
use itertools::Itertools;
|
||||
use orchid_base::error::{mk_err, mk_errv, OrcErr, OrcRes};
|
||||
use orchid_base::error::{OrcErr, OrcRes, mk_err, mk_errv};
|
||||
use orchid_base::interner::intern;
|
||||
use orchid_base::location::Pos;
|
||||
use orchid_base::tree::{vname_tv, wrap_tokv};
|
||||
use orchid_base::{intern, vname};
|
||||
use orchid_extension::atom::AtomicFeatures;
|
||||
use orchid_extension::lexer::{err_not_applicable, LexContext, Lexer};
|
||||
use orchid_extension::lexer::{LexContext, Lexer, err_not_applicable};
|
||||
use orchid_extension::tree::{GenTok, GenTokTree};
|
||||
|
||||
use super::str_atom::IntStrAtom;
|
||||
@@ -13,126 +13,126 @@ use super::str_atom::IntStrAtom;
|
||||
/// Reasons why [parse_string] might fail. See [StringError]
|
||||
#[derive(Clone)]
|
||||
enum StringErrorKind {
|
||||
/// A unicode escape sequence wasn't followed by 4 hex digits
|
||||
NotHex,
|
||||
/// A unicode escape sequence contained an unassigned code point
|
||||
BadCodePoint,
|
||||
/// An unrecognized escape sequence was found
|
||||
BadEscSeq,
|
||||
/// A unicode escape sequence wasn't followed by 4 hex digits
|
||||
NotHex,
|
||||
/// A unicode escape sequence contained an unassigned code point
|
||||
BadCodePoint,
|
||||
/// An unrecognized escape sequence was found
|
||||
BadEscSeq,
|
||||
}
|
||||
|
||||
/// Error produced by [parse_string]
|
||||
#[derive(Clone)]
|
||||
struct StringError {
|
||||
/// Character where the error occured
|
||||
pos: u32,
|
||||
/// Reason for the error
|
||||
kind: StringErrorKind,
|
||||
/// Character where the error occured
|
||||
pos: u32,
|
||||
/// Reason for the error
|
||||
kind: StringErrorKind,
|
||||
}
|
||||
|
||||
impl StringError {
|
||||
/// Convert into project error for reporting
|
||||
pub fn into_proj(self, pos: u32) -> OrcErr {
|
||||
let start = pos + self.pos;
|
||||
mk_err(
|
||||
intern!(str: "Failed to parse string"),
|
||||
match self.kind {
|
||||
StringErrorKind::NotHex => "Expected a hex digit",
|
||||
StringErrorKind::BadCodePoint => "The specified number is not a Unicode code point",
|
||||
StringErrorKind::BadEscSeq => "Unrecognized escape sequence",
|
||||
},
|
||||
[Pos::Range(start..start + 1).into()],
|
||||
)
|
||||
}
|
||||
/// Convert into project error for reporting
|
||||
pub fn into_proj(self, pos: u32) -> OrcErr {
|
||||
let start = pos + self.pos;
|
||||
mk_err(
|
||||
intern!(str: "Failed to parse string"),
|
||||
match self.kind {
|
||||
StringErrorKind::NotHex => "Expected a hex digit",
|
||||
StringErrorKind::BadCodePoint => "The specified number is not a Unicode code point",
|
||||
StringErrorKind::BadEscSeq => "Unrecognized escape sequence",
|
||||
},
|
||||
[Pos::Range(start..start + 1).into()],
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// Process escape sequences in a string literal
|
||||
fn parse_string(str: &str) -> Result<String, StringError> {
|
||||
let mut target = String::new();
|
||||
let mut iter = str.char_indices().map(|(i, c)| (i as u32, c));
|
||||
while let Some((_, c)) = iter.next() {
|
||||
if c != '\\' {
|
||||
target.push(c);
|
||||
continue;
|
||||
}
|
||||
let (mut pos, code) = iter.next().expect("lexer would have continued");
|
||||
let next = match code {
|
||||
c @ ('\\' | '"' | '$') => c,
|
||||
'b' => '\x08',
|
||||
'f' => '\x0f',
|
||||
'n' => '\n',
|
||||
'r' => '\r',
|
||||
't' => '\t',
|
||||
'\n' => 'skipws: loop {
|
||||
match iter.next() {
|
||||
None => return Ok(target),
|
||||
Some((_, c)) =>
|
||||
if !c.is_whitespace() {
|
||||
break 'skipws c;
|
||||
},
|
||||
}
|
||||
},
|
||||
'u' => {
|
||||
let acc = ((0..4).rev())
|
||||
.map(|radical| {
|
||||
let (j, c) = (iter.next()).ok_or(StringError { pos, kind: StringErrorKind::NotHex })?;
|
||||
pos = j;
|
||||
let b = u32::from_str_radix(&String::from(c), 16)
|
||||
.map_err(|_| StringError { pos, kind: StringErrorKind::NotHex })?;
|
||||
Ok(16u32.pow(radical) + b)
|
||||
})
|
||||
.fold_ok(0, u32::wrapping_add)?;
|
||||
char::from_u32(acc).ok_or(StringError { pos, kind: StringErrorKind::BadCodePoint })?
|
||||
},
|
||||
_ => return Err(StringError { pos, kind: StringErrorKind::BadEscSeq }),
|
||||
};
|
||||
target.push(next);
|
||||
}
|
||||
Ok(target)
|
||||
let mut target = String::new();
|
||||
let mut iter = str.char_indices().map(|(i, c)| (i as u32, c));
|
||||
while let Some((_, c)) = iter.next() {
|
||||
if c != '\\' {
|
||||
target.push(c);
|
||||
continue;
|
||||
}
|
||||
let (mut pos, code) = iter.next().expect("lexer would have continued");
|
||||
let next = match code {
|
||||
c @ ('\\' | '"' | '$') => c,
|
||||
'b' => '\x08',
|
||||
'f' => '\x0f',
|
||||
'n' => '\n',
|
||||
'r' => '\r',
|
||||
't' => '\t',
|
||||
'\n' => 'skipws: loop {
|
||||
match iter.next() {
|
||||
None => return Ok(target),
|
||||
Some((_, c)) =>
|
||||
if !c.is_whitespace() {
|
||||
break 'skipws c;
|
||||
},
|
||||
}
|
||||
},
|
||||
'u' => {
|
||||
let acc = ((0..4).rev())
|
||||
.map(|radical| {
|
||||
let (j, c) = (iter.next()).ok_or(StringError { pos, kind: StringErrorKind::NotHex })?;
|
||||
pos = j;
|
||||
let b = u32::from_str_radix(&String::from(c), 16)
|
||||
.map_err(|_| StringError { pos, kind: StringErrorKind::NotHex })?;
|
||||
Ok(16u32.pow(radical) + b)
|
||||
})
|
||||
.fold_ok(0, u32::wrapping_add)?;
|
||||
char::from_u32(acc).ok_or(StringError { pos, kind: StringErrorKind::BadCodePoint })?
|
||||
},
|
||||
_ => return Err(StringError { pos, kind: StringErrorKind::BadEscSeq }),
|
||||
};
|
||||
target.push(next);
|
||||
}
|
||||
Ok(target)
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct StringLexer;
|
||||
impl Lexer for StringLexer {
|
||||
const CHAR_FILTER: &'static [std::ops::RangeInclusive<char>] = &['"'..='"'];
|
||||
fn lex<'a>(all: &'a str, ctx: &'a LexContext<'a>) -> OrcRes<(&'a str, GenTokTree<'a>)> {
|
||||
let mut tail = all.strip_prefix('"').ok_or_else(err_not_applicable)?;
|
||||
let mut ret = GenTok::X(IntStrAtom::from(intern!(str: "")).factory()).at(ctx.tok_ran(0, all));
|
||||
let mut cur = String::new();
|
||||
let mut errors = vec![];
|
||||
let str_to_gen = |str: &mut String, tail: &str, err: &mut Vec<OrcErr>| {
|
||||
let str_val = parse_string(&str.split_off(0))
|
||||
.inspect_err(|e| err.push(e.clone().into_proj(ctx.pos(tail) - str.len() as u32)))
|
||||
.unwrap_or_default();
|
||||
GenTok::X(IntStrAtom::from(intern(&*str_val)).factory())
|
||||
.at(ctx.tok_ran(str.len() as u32, tail))
|
||||
};
|
||||
let add_frag = |prev: GenTokTree<'a>, new: GenTokTree<'a>| {
|
||||
wrap_tokv(vname_tv(&vname!(std::string::concat), new.range.end).chain([prev, new]))
|
||||
};
|
||||
loop {
|
||||
if let Some(rest) = tail.strip_prefix('"') {
|
||||
return Ok((rest, add_frag(ret, str_to_gen(&mut cur, tail, &mut errors))));
|
||||
} else if let Some(rest) = tail.strip_prefix('$') {
|
||||
ret = add_frag(ret, str_to_gen(&mut cur, tail, &mut errors));
|
||||
let (new_tail, tree) = ctx.recurse(rest)?;
|
||||
tail = new_tail;
|
||||
ret = add_frag(ret, tree);
|
||||
} else if tail.starts_with('\\') {
|
||||
// parse_string will deal with it, we just have to skip the next char
|
||||
tail = &tail[2..];
|
||||
} else {
|
||||
let mut ch = tail.chars();
|
||||
if let Some(c) = ch.next() {
|
||||
cur.push(c);
|
||||
tail = ch.as_str();
|
||||
} else {
|
||||
let range = ctx.pos(all)..ctx.pos("");
|
||||
return Err(mk_errv(intern!(str: "No string end"), "String never terminated with \"", [
|
||||
Pos::Range(range.clone()).into(),
|
||||
]));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
const CHAR_FILTER: &'static [std::ops::RangeInclusive<char>] = &['"'..='"'];
|
||||
fn lex<'a>(all: &'a str, ctx: &'a LexContext<'a>) -> OrcRes<(&'a str, GenTokTree<'a>)> {
|
||||
let mut tail = all.strip_prefix('"').ok_or_else(err_not_applicable)?;
|
||||
let mut ret = GenTok::X(IntStrAtom::from(intern!(str: "")).factory()).at(ctx.tok_ran(0, all));
|
||||
let mut cur = String::new();
|
||||
let mut errors = vec![];
|
||||
let str_to_gen = |str: &mut String, tail: &str, err: &mut Vec<OrcErr>| {
|
||||
let str_val = parse_string(&str.split_off(0))
|
||||
.inspect_err(|e| err.push(e.clone().into_proj(ctx.pos(tail) - str.len() as u32)))
|
||||
.unwrap_or_default();
|
||||
GenTok::X(IntStrAtom::from(intern(&*str_val)).factory())
|
||||
.at(ctx.tok_ran(str.len() as u32, tail))
|
||||
};
|
||||
let add_frag = |prev: GenTokTree<'a>, new: GenTokTree<'a>| {
|
||||
wrap_tokv(vname_tv(&vname!(std::string::concat), new.range.end).chain([prev, new]))
|
||||
};
|
||||
loop {
|
||||
if let Some(rest) = tail.strip_prefix('"') {
|
||||
return Ok((rest, add_frag(ret, str_to_gen(&mut cur, tail, &mut errors))));
|
||||
} else if let Some(rest) = tail.strip_prefix('$') {
|
||||
ret = add_frag(ret, str_to_gen(&mut cur, tail, &mut errors));
|
||||
let (new_tail, tree) = ctx.recurse(rest)?;
|
||||
tail = new_tail;
|
||||
ret = add_frag(ret, tree);
|
||||
} else if tail.starts_with('\\') {
|
||||
// parse_string will deal with it, we just have to skip the next char
|
||||
tail = &tail[2..];
|
||||
} else {
|
||||
let mut ch = tail.chars();
|
||||
if let Some(c) = ch.next() {
|
||||
cur.push(c);
|
||||
tail = ch.as_str();
|
||||
} else {
|
||||
let range = ctx.pos(all)..ctx.pos("");
|
||||
return Err(mk_errv(intern!(str: "No string end"), "String never terminated with \"", [
|
||||
Pos::Range(range.clone()).into(),
|
||||
]));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user