use new heap term representation
This commit is contained in:
265
crates/prolog_parser/Cargo.lock
generated
265
crates/prolog_parser/Cargo.lock
generated
@@ -1,265 +0,0 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "arrayvec"
|
||||
version = "0.4.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cd9fd44efafa8690358b7408d253adf110036b88f55672a933f01d616ad9b1b9"
|
||||
dependencies = [
|
||||
"nodrop",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "1.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"
|
||||
|
||||
[[package]]
|
||||
name = "az"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d84e1d907bfc5795a6addb95ef8666141ee73c8f2f5250ff2a46bf4e4f4aec8a"
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "0.1.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
|
||||
|
||||
[[package]]
|
||||
name = "gmp-mpfr-sys"
|
||||
version = "1.4.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a57fdb339d49833021b1fded600ed240ae907e33909d5511a61dff884df7f16e"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lexical"
|
||||
version = "2.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8e0d09e60c187a6d0a3fa418aec8587c6a4ae9de872f6126f2134f319b5ed10d"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"lexical-core",
|
||||
"rustc_version",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lexical-core"
|
||||
version = "0.4.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2304bccb228c4b020f3a4835d247df0a02a7c4686098d4167762cfbbe4c5cb14"
|
||||
dependencies = [
|
||||
"arrayvec",
|
||||
"cfg-if",
|
||||
"rustc_version",
|
||||
"ryu",
|
||||
"static_assertions",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.85"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7ccac4b00700875e6a07c6cde370d44d32fa01c5a65cdd2fca6858c479d28bb3"
|
||||
|
||||
[[package]]
|
||||
name = "nodrop"
|
||||
version = "0.1.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "72ef4a56884ca558e5ddb05a1d1e7e1bfd9a68d9ed024c21704cc98872dae1bb"
|
||||
|
||||
[[package]]
|
||||
name = "num-bigint"
|
||||
version = "0.2.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "090c7f9998ee0ff65aa5b723e4009f7b217707f1fb5ea551329cc4d6231fb304"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"num-integer",
|
||||
"num-traits 0.2.14",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-integer"
|
||||
version = "0.1.44"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"num-traits 0.2.14",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-rational"
|
||||
version = "0.2.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5c000134b5dbf44adc5cb772486d335293351644b801551abe8f75c84cfa4aef"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"num-bigint",
|
||||
"num-integer",
|
||||
"num-traits 0.2.14",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-rug-adapter"
|
||||
version = "0.1.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7470b6acf85abce0771203112db4181d03f7b8a6be49f0e842a78030192f8a58"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"num-bigint",
|
||||
"num-integer",
|
||||
"num-rational",
|
||||
"num-traits 0.2.14",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-traits"
|
||||
version = "0.1.43"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "92e5113e9fd4cc14ded8e499429f396a20f98c772a47cc8622a736e1ec843c31"
|
||||
dependencies = [
|
||||
"num-traits 0.2.14",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-traits"
|
||||
version = "0.2.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ordered-float"
|
||||
version = "0.5.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7eb5259643245d3f292c7a146b2df53bba24d7eab159410e648eb73dc164669d"
|
||||
dependencies = [
|
||||
"num-traits 0.1.43",
|
||||
"unreachable",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "prolog_parser"
|
||||
version = "0.8.68"
|
||||
dependencies = [
|
||||
"lexical",
|
||||
"num-rug-adapter",
|
||||
"ordered-float",
|
||||
"rug",
|
||||
"unicode_reader",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rug"
|
||||
version = "1.11.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e538d00da450a8e48aac7e6322e67b2dc86ec71a1feeac0e3954c4f07f01bc45"
|
||||
dependencies = [
|
||||
"az",
|
||||
"gmp-mpfr-sys",
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustc_version"
|
||||
version = "0.2.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a"
|
||||
dependencies = [
|
||||
"semver",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ryu"
|
||||
version = "1.0.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e"
|
||||
|
||||
[[package]]
|
||||
name = "semver"
|
||||
version = "0.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403"
|
||||
dependencies = [
|
||||
"semver-parser",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "semver-parser"
|
||||
version = "0.7.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3"
|
||||
|
||||
[[package]]
|
||||
name = "smallvec"
|
||||
version = "1.6.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fe0f37c9e8f3c5a4a66ad655a93c74daac4ad00c441533bf5c6e7990bb42604e"
|
||||
|
||||
[[package]]
|
||||
name = "static_assertions"
|
||||
version = "0.3.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7f3eb36b47e512f8f1c9e3d10c2c1965bc992bd9cdb024fa581e2194501c83d3"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-segmentation"
|
||||
version = "1.7.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bb0d2e7be6ae3a5fa87eed5fb451aff96f2573d2694942e40543ae0bbe19c796"
|
||||
|
||||
[[package]]
|
||||
name = "unicode_reader"
|
||||
version = "1.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5b639121690b27acd92c97ed2b52c5e5e8d3d39482e943b4559695cef62f771a"
|
||||
dependencies = [
|
||||
"smallvec",
|
||||
"unicode-segmentation",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unreachable"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "382810877fe448991dfc7f0dd6e3ae5d58088fd0ea5e35189655f84e6814fa56"
|
||||
dependencies = [
|
||||
"void",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "void"
|
||||
version = "1.0.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d"
|
||||
|
||||
[[package]]
|
||||
name = "winapi"
|
||||
version = "0.3.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
|
||||
dependencies = [
|
||||
"winapi-i686-pc-windows-gnu",
|
||||
"winapi-x86_64-pc-windows-gnu",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi-i686-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
||||
|
||||
[[package]]
|
||||
name = "winapi-x86_64-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
||||
@@ -1,782 +0,0 @@
|
||||
use crate::rug::{Integer, Rational};
|
||||
use crate::tabled_rc::*;
|
||||
use ordered_float::*;
|
||||
|
||||
use crate::put_back_n::*;
|
||||
|
||||
use std::cell::Cell;
|
||||
use std::cmp::Ordering;
|
||||
use std::fmt;
|
||||
use std::hash::{Hash, Hasher};
|
||||
use std::io::{Bytes, Error as IOError, Read};
|
||||
use std::ops::Deref;
|
||||
use std::rc::Rc;
|
||||
use std::vec::Vec;
|
||||
|
||||
use indexmap::IndexMap;
|
||||
use unicode_reader::CodePoints;
|
||||
|
||||
pub type Atom = String;
|
||||
|
||||
pub type Var = String;
|
||||
|
||||
pub type Specifier = u32;
|
||||
|
||||
pub const MAX_ARITY: usize = 1023;
|
||||
|
||||
pub const XFX: u32 = 0x0001;
|
||||
pub const XFY: u32 = 0x0002;
|
||||
pub const YFX: u32 = 0x0004;
|
||||
pub const XF: u32 = 0x0010;
|
||||
pub const YF: u32 = 0x0020;
|
||||
pub const FX: u32 = 0x0040;
|
||||
pub const FY: u32 = 0x0080;
|
||||
pub const DELIMITER: u32 = 0x0100;
|
||||
pub const TERM: u32 = 0x1000;
|
||||
pub const LTERM: u32 = 0x3000;
|
||||
|
||||
pub const NEGATIVE_SIGN: u32 = 0x0200;
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! clause_name {
|
||||
($name: expr, $tbl: expr) => {
|
||||
$crate::ast::ClauseName::User($crate::tabled_rc::TabledRc::new($name, $tbl.clone()))
|
||||
};
|
||||
($name: expr) => {
|
||||
$crate::ast::ClauseName::BuiltIn($name)
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! atom {
|
||||
($e:expr, $tbl:expr) => {
|
||||
$crate::ast::Constant::Atom(
|
||||
$crate::ast::ClauseName::User($crate::tabled_rc!($e, $tbl)),
|
||||
None,
|
||||
)
|
||||
};
|
||||
($e:expr) => {
|
||||
$crate::ast::Constant::Atom($crate::clause_name!($e), None)
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! rc_atom {
|
||||
($e:expr) => {
|
||||
Rc::new(String::from($e))
|
||||
};
|
||||
}
|
||||
macro_rules! is_term {
|
||||
($x:expr) => {
|
||||
($x & $crate::ast::TERM) != 0
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! is_lterm {
|
||||
($x:expr) => {
|
||||
($x & $crate::ast::LTERM) != 0
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! is_op {
|
||||
($x:expr) => {
|
||||
$x & ($crate::ast::XF
|
||||
| $crate::ast::YF
|
||||
| $crate::ast::FX
|
||||
| $crate::ast::FY
|
||||
| $crate::ast::XFX
|
||||
| $crate::ast::XFY
|
||||
| $crate::ast::YFX)
|
||||
!= 0
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! is_negate {
|
||||
($x:expr) => {
|
||||
($x & $crate::ast::NEGATIVE_SIGN) != 0
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! is_prefix {
|
||||
($x:expr) => {
|
||||
$x & ($crate::ast::FX | $crate::ast::FY) != 0
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! is_postfix {
|
||||
($x:expr) => {
|
||||
$x & ($crate::ast::XF | $crate::ast::YF) != 0
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! is_infix {
|
||||
($x:expr) => {
|
||||
($x & ($crate::ast::XFX | $crate::ast::XFY | $crate::ast::YFX)) != 0
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! is_xfx {
|
||||
($x:expr) => {
|
||||
($x & $crate::ast::XFX) != 0
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! is_xfy {
|
||||
($x:expr) => {
|
||||
($x & $crate::ast::XFY) != 0
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! is_yfx {
|
||||
($x:expr) => {
|
||||
($x & $crate::ast::YFX) != 0
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! is_yf {
|
||||
($x:expr) => {
|
||||
($x & $crate::ast::YF) != 0
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! is_xf {
|
||||
($x:expr) => {
|
||||
($x & $crate::ast::XF) != 0
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! is_fx {
|
||||
($x:expr) => {
|
||||
($x & $crate::ast::FX) != 0
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! is_fy {
|
||||
($x:expr) => {
|
||||
($x & $crate::ast::FY) != 0
|
||||
};
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub enum RegType {
|
||||
Perm(usize),
|
||||
Temp(usize),
|
||||
}
|
||||
|
||||
impl Default for RegType {
|
||||
fn default() -> Self {
|
||||
RegType::Temp(0)
|
||||
}
|
||||
}
|
||||
|
||||
impl RegType {
|
||||
pub fn reg_num(self) -> usize {
|
||||
match self {
|
||||
RegType::Perm(reg_num) | RegType::Temp(reg_num) => reg_num,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_perm(self) -> bool {
|
||||
matches!(self, RegType::Perm(_))
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for RegType {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
RegType::Perm(val) => write!(f, "Y{}", val),
|
||||
RegType::Temp(val) => write!(f, "X{}", val),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
||||
pub enum VarReg {
|
||||
ArgAndNorm(RegType, usize),
|
||||
Norm(RegType),
|
||||
}
|
||||
|
||||
impl VarReg {
|
||||
pub fn norm(self) -> RegType {
|
||||
match self {
|
||||
VarReg::ArgAndNorm(reg, _) | VarReg::Norm(reg) => reg,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for VarReg {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
VarReg::Norm(RegType::Perm(reg)) => write!(f, "Y{}", reg),
|
||||
VarReg::Norm(RegType::Temp(reg)) => write!(f, "X{}", reg),
|
||||
VarReg::ArgAndNorm(RegType::Perm(reg), arg) => write!(f, "Y{} A{}", reg, arg),
|
||||
VarReg::ArgAndNorm(RegType::Temp(reg), arg) => write!(f, "X{} A{}", reg, arg),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for VarReg {
|
||||
fn default() -> Self {
|
||||
VarReg::Norm(RegType::default())
|
||||
}
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! temp_v {
|
||||
($x:expr) => {
|
||||
$crate::ast::RegType::Temp($x)
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! perm_v {
|
||||
($x:expr) => {
|
||||
$crate::ast::RegType::Perm($x)
|
||||
};
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub enum GenContext {
|
||||
Head,
|
||||
Mid(usize),
|
||||
Last(usize), // Mid & Last: chunk_num
|
||||
}
|
||||
|
||||
impl GenContext {
|
||||
pub fn chunk_num(self) -> usize {
|
||||
match self {
|
||||
GenContext::Head => 0,
|
||||
GenContext::Mid(cn) | GenContext::Last(cn) => cn,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub type OpDirKey = (ClauseName, Fixity);
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct OpDirValue(pub SharedOpDesc);
|
||||
|
||||
impl OpDirValue {
|
||||
pub fn new(spec: Specifier, priority: usize) -> Self {
|
||||
OpDirValue(SharedOpDesc::new(priority, spec))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn shared_op_desc(&self) -> SharedOpDesc {
|
||||
self.0.clone()
|
||||
}
|
||||
}
|
||||
|
||||
// name and fixity -> operator type and precedence.
|
||||
pub type OpDir = IndexMap<OpDirKey, OpDirValue>;
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct MachineFlags {
|
||||
pub double_quotes: DoubleQuotes,
|
||||
}
|
||||
|
||||
impl Default for MachineFlags {
|
||||
fn default() -> Self {
|
||||
MachineFlags {
|
||||
double_quotes: DoubleQuotes::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum DoubleQuotes {
|
||||
Atom,
|
||||
Chars,
|
||||
Codes,
|
||||
}
|
||||
|
||||
impl DoubleQuotes {
|
||||
pub fn is_chars(self) -> bool {
|
||||
matches!(self, DoubleQuotes::Chars)
|
||||
}
|
||||
|
||||
pub fn is_atom(self) -> bool {
|
||||
matches!(self, DoubleQuotes::Atom)
|
||||
}
|
||||
|
||||
pub fn is_codes(self) -> bool {
|
||||
matches!(self, DoubleQuotes::Codes)
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for DoubleQuotes {
|
||||
fn default() -> Self {
|
||||
DoubleQuotes::Chars
|
||||
}
|
||||
}
|
||||
|
||||
pub fn default_op_dir() -> OpDir {
|
||||
let mut op_dir = OpDir::new();
|
||||
|
||||
op_dir.insert((clause_name!(":-"), Fixity::In), OpDirValue::new(XFX, 1200));
|
||||
op_dir.insert((clause_name!(":-"), Fixity::Pre), OpDirValue::new(FX, 1200));
|
||||
op_dir.insert((clause_name!("?-"), Fixity::Pre), OpDirValue::new(FX, 1200));
|
||||
op_dir.insert((clause_name!(","), Fixity::In), OpDirValue::new(XFY, 1000));
|
||||
|
||||
op_dir
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum ArithmeticError {
|
||||
NonEvaluableFunctor(Constant, usize),
|
||||
UninstantiatedVar,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum ParserError {
|
||||
BackQuotedString(usize, usize),
|
||||
UnexpectedChar(char, usize, usize),
|
||||
UnexpectedEOF,
|
||||
IO(IOError),
|
||||
IncompleteReduction(usize, usize),
|
||||
InvalidSingleQuotedCharacter(char),
|
||||
MissingQuote(usize, usize),
|
||||
NonPrologChar(usize, usize),
|
||||
ParseBigInt(usize, usize),
|
||||
Utf8Error(usize, usize),
|
||||
}
|
||||
|
||||
impl ParserError {
|
||||
pub fn line_and_col_num(&self) -> Option<(usize, usize)> {
|
||||
match self {
|
||||
&ParserError::BackQuotedString(line_num, col_num)
|
||||
| &ParserError::UnexpectedChar(_, line_num, col_num)
|
||||
| &ParserError::IncompleteReduction(line_num, col_num)
|
||||
| &ParserError::MissingQuote(line_num, col_num)
|
||||
| &ParserError::NonPrologChar(line_num, col_num)
|
||||
| &ParserError::ParseBigInt(line_num, col_num)
|
||||
| &ParserError::Utf8Error(line_num, col_num) => Some((line_num, col_num)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_str(&self) -> &'static str {
|
||||
match self {
|
||||
ParserError::BackQuotedString(..) => "back_quoted_string",
|
||||
ParserError::UnexpectedChar(..) => "unexpected_char",
|
||||
ParserError::UnexpectedEOF => "unexpected_end_of_file",
|
||||
ParserError::IncompleteReduction(..) => "incomplete_reduction",
|
||||
ParserError::InvalidSingleQuotedCharacter(..) => "invalid_single_quoted_character",
|
||||
ParserError::IO(_) => "input_output_error",
|
||||
ParserError::MissingQuote(..) => "missing_quote",
|
||||
ParserError::NonPrologChar(..) => "non_prolog_character",
|
||||
ParserError::ParseBigInt(..) => "cannot_parse_big_int",
|
||||
ParserError::Utf8Error(..) => "utf8_conversion_error",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<IOError> for ParserError {
|
||||
fn from(err: IOError) -> ParserError {
|
||||
ParserError::IO(err)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&IOError> for ParserError {
|
||||
fn from(error: &IOError) -> ParserError {
|
||||
if error.get_ref().filter(|e| e.is::<BadUtf8Error>()).is_some() {
|
||||
ParserError::Utf8Error(0, 0)
|
||||
} else {
|
||||
ParserError::IO(error.kind().into())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct CompositeOpDir<'a, 'b> {
|
||||
pub primary_op_dir: Option<&'b OpDir>,
|
||||
pub secondary_op_dir: &'a OpDir,
|
||||
}
|
||||
|
||||
impl<'a, 'b> CompositeOpDir<'a, 'b> {
|
||||
#[inline]
|
||||
pub fn new(secondary_op_dir: &'a OpDir, primary_op_dir: Option<&'b OpDir>) -> Self {
|
||||
CompositeOpDir {
|
||||
primary_op_dir,
|
||||
secondary_op_dir,
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn get(&self, name: ClauseName, fixity: Fixity) -> Option<&OpDirValue> {
|
||||
let entry = if let Some(ref primary_op_dir) = &self.primary_op_dir {
|
||||
primary_op_dir.get(&(name.clone(), fixity))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
entry.or_else(move || self.secondary_op_dir.get(&(name, fixity)))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, Eq, Hash, PartialEq, PartialOrd, Ord)]
|
||||
pub enum Fixity {
|
||||
In,
|
||||
Post,
|
||||
Pre,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub struct SharedOpDesc(Rc<Cell<(usize, Specifier)>>);
|
||||
|
||||
impl SharedOpDesc {
|
||||
#[inline]
|
||||
pub fn new(priority: usize, spec: Specifier) -> Self {
|
||||
SharedOpDesc(Rc::new(Cell::new((priority, spec))))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn ptr_eq(lop_desc: &SharedOpDesc, rop_desc: &SharedOpDesc) -> bool {
|
||||
Rc::ptr_eq(&lop_desc.0, &rop_desc.0)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn arity(&self) -> usize {
|
||||
if self.get().1 & (XFX | XFY | YFX) == 0 {
|
||||
1
|
||||
} else {
|
||||
2
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn get(&self) -> (usize, Specifier) {
|
||||
self.0.get()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn set(&self, prec: usize, spec: Specifier) {
|
||||
self.0.set((prec, spec));
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn prec(&self) -> usize {
|
||||
self.0.get().0
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn assoc(&self) -> Specifier {
|
||||
self.0.get().1
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for SharedOpDesc {
|
||||
type Target = Cell<(usize, Specifier)>;
|
||||
|
||||
#[inline]
|
||||
fn deref(&self) -> &Self::Target {
|
||||
self.0.deref()
|
||||
}
|
||||
}
|
||||
|
||||
// this ensures that SharedOpDesc (which is not consistently placed in
|
||||
// every atom!) doesn't affect the value of an atom hash. If
|
||||
// SharedOpDesc values are to be indexed, a BTreeMap or BTreeSet
|
||||
// should be used, obviously.
|
||||
impl Hash for SharedOpDesc {
|
||||
fn hash<H: Hasher>(&self, state: &mut H) {
|
||||
0.hash(state)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub enum Constant {
|
||||
Atom(ClauseName, Option<SharedOpDesc>),
|
||||
Char(char),
|
||||
EmptyList,
|
||||
Fixnum(isize),
|
||||
Integer(Rc<Integer>),
|
||||
Rational(Rc<Rational>),
|
||||
Float(OrderedFloat<f64>),
|
||||
String(Rc<String>),
|
||||
Usize(usize),
|
||||
}
|
||||
|
||||
impl fmt::Display for Constant {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
Constant::Atom(ref atom, _) => {
|
||||
if atom.as_str().chars().any(|c| "`.$'\" ".contains(c)) {
|
||||
write!(f, "'{}'", atom.as_str())
|
||||
} else {
|
||||
write!(f, "{}", atom.as_str())
|
||||
}
|
||||
}
|
||||
Constant::Char(c) => write!(f, "'{}'", *c as u32),
|
||||
Constant::EmptyList => write!(f, "[]"),
|
||||
Constant::Fixnum(n) => write!(f, "{}", n),
|
||||
Constant::Integer(ref n) => write!(f, "{}", n),
|
||||
Constant::Rational(ref n) => write!(f, "{}", n),
|
||||
Constant::Float(ref n) => write!(f, "{}", n),
|
||||
Constant::String(ref s) => write!(f, "\"{}\"", &s),
|
||||
Constant::Usize(integer) => write!(f, "u{}", integer),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Constant {
|
||||
pub fn to_atom(&self) -> Option<ClauseName> {
|
||||
match self {
|
||||
Constant::Atom(a, _) => Some(a.defrock_brackets()),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum ClauseName {
|
||||
BuiltIn(&'static str),
|
||||
User(TabledRc<Atom>),
|
||||
}
|
||||
|
||||
impl fmt::Display for ClauseName {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{}", self.as_str())
|
||||
}
|
||||
}
|
||||
|
||||
impl Hash for ClauseName {
|
||||
fn hash<H: Hasher>(&self, state: &mut H) {
|
||||
(*self.as_str()).hash(state)
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for ClauseName {
|
||||
fn eq(&self, other: &ClauseName) -> bool {
|
||||
*self.as_str() == *other.as_str()
|
||||
}
|
||||
}
|
||||
|
||||
impl Eq for ClauseName {}
|
||||
|
||||
impl Ord for ClauseName {
|
||||
fn cmp(&self, other: &ClauseName) -> Ordering {
|
||||
(*self.as_str()).cmp(other.as_str())
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialOrd for ClauseName {
|
||||
fn partial_cmp(&self, other: &ClauseName) -> Option<Ordering> {
|
||||
Some(self.cmp(other))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a TabledRc<Atom>> for ClauseName {
|
||||
fn from(name: &'a TabledRc<Atom>) -> ClauseName {
|
||||
ClauseName::User(name.clone())
|
||||
}
|
||||
}
|
||||
|
||||
impl ClauseName {
|
||||
#[inline]
|
||||
pub fn owning_module(&self) -> Self {
|
||||
match self {
|
||||
ClauseName::User(ref name) => {
|
||||
let module = name.owning_module();
|
||||
ClauseName::User(TabledRc {
|
||||
atom: module.clone(),
|
||||
table: TabledData::new(module),
|
||||
})
|
||||
}
|
||||
_ => clause_name!("user"),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn to_rc(&self) -> Rc<String> {
|
||||
match self {
|
||||
ClauseName::BuiltIn(s) => Rc::new(s.to_string()),
|
||||
ClauseName::User(ref rc) => rc.inner(),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn with_table(self, atom_tbl: TabledData<Atom>) -> Self {
|
||||
match self {
|
||||
ClauseName::BuiltIn(_) => self,
|
||||
ClauseName::User(mut name) => {
|
||||
name.table = atom_tbl;
|
||||
ClauseName::User(name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn has_table(&self, atom_tbl: &TabledData<Atom>) -> bool {
|
||||
match self {
|
||||
ClauseName::BuiltIn(_) => false,
|
||||
ClauseName::User(ref name) => &name.table == atom_tbl,
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn has_table_of(&self, other: &ClauseName) -> bool {
|
||||
match self {
|
||||
ClauseName::BuiltIn(_) => {
|
||||
matches!(other, ClauseName::BuiltIn(_))
|
||||
}
|
||||
ClauseName::User(ref name) => other.has_table(&name.table),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn as_str(&self) -> &str {
|
||||
match self {
|
||||
ClauseName::BuiltIn(s) => s,
|
||||
ClauseName::User(ref name) => name.as_ref(),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn is_char(&self) -> bool {
|
||||
!self.as_str().is_empty() && self.as_str().chars().nth(1).is_none()
|
||||
}
|
||||
|
||||
pub fn defrock_brackets(&self) -> Self {
|
||||
fn defrock_brackets(s: &str) -> &str {
|
||||
if s.starts_with('(') && s.ends_with(')') {
|
||||
&s[1..s.len() - 1]
|
||||
} else {
|
||||
s
|
||||
}
|
||||
}
|
||||
|
||||
match self {
|
||||
ClauseName::BuiltIn(s) => ClauseName::BuiltIn(defrock_brackets(s)),
|
||||
ClauseName::User(s) => {
|
||||
ClauseName::User(tabled_rc!(defrock_brackets(s.as_str()).to_owned(), s.table))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<str> for ClauseName {
|
||||
#[inline]
|
||||
fn as_ref(&self) -> &str {
|
||||
self.as_str()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Term {
|
||||
AnonVar,
|
||||
Clause(
|
||||
Cell<RegType>,
|
||||
ClauseName,
|
||||
Vec<Box<Term>>,
|
||||
Option<SharedOpDesc>,
|
||||
),
|
||||
Cons(Cell<RegType>, Box<Term>, Box<Term>),
|
||||
Constant(Cell<RegType>, Constant),
|
||||
Var(Cell<VarReg>, Rc<Var>),
|
||||
}
|
||||
|
||||
impl Term {
|
||||
pub fn shared_op_desc(&self) -> Option<SharedOpDesc> {
|
||||
match self {
|
||||
Term::Clause(_, _, _, ref spec) => spec.clone(),
|
||||
Term::Constant(_, Constant::Atom(_, ref spec)) => spec.clone(),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn into_constant(self) -> Option<Constant> {
|
||||
match self {
|
||||
Term::Constant(_, c) => Some(c),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn first_arg(&self) -> Option<&Term> {
|
||||
match self {
|
||||
Term::Clause(_, _, ref terms, _) => terms.first().map(|bt| bt.as_ref()),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_name(&mut self, new_name: ClauseName) {
|
||||
match self {
|
||||
Term::Constant(_, Constant::Atom(ref mut atom, _))
|
||||
| Term::Clause(_, ref mut atom, ..) => {
|
||||
*atom = new_name;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn name(&self) -> Option<ClauseName> {
|
||||
match self {
|
||||
&Term::Constant(_, Constant::Atom(ref atom, _)) | &Term::Clause(_, ref atom, ..) => {
|
||||
Some(atom.clone())
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn arity(&self) -> usize {
|
||||
match self {
|
||||
Term::Clause(_, _, ref child_terms, ..) => child_terms.len(),
|
||||
_ => 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn unfold_by_str_once(term: &mut Term, s: &str) -> Option<(Term, Term)> {
|
||||
if let Term::Clause(_, ref name, ref mut subterms, _) = term {
|
||||
if name.as_str() == s && subterms.len() == 2 {
|
||||
let snd = *subterms.pop().unwrap();
|
||||
let fst = *subterms.pop().unwrap();
|
||||
|
||||
return Some((fst, snd));
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
pub fn unfold_by_str(mut term: Term, s: &str) -> Vec<Term> {
|
||||
let mut terms = vec![];
|
||||
|
||||
while let Some((fst, snd)) = unfold_by_str_once(&mut term, s) {
|
||||
terms.push(fst);
|
||||
term = snd;
|
||||
}
|
||||
|
||||
terms.push(term);
|
||||
terms
|
||||
}
|
||||
|
||||
pub type ParsingStream<R> = PutBackN<CodePoints<Bytes<R>>>;
|
||||
|
||||
use unicode_reader::BadUtf8Error;
|
||||
|
||||
#[inline]
|
||||
pub fn parsing_stream<R: Read>(src: R) -> Result<ParsingStream<R>, ParserError> {
|
||||
let mut stream = put_back_n(CodePoints::from(src.bytes()));
|
||||
match stream.peek() {
|
||||
None => Ok(stream), // empty stream is handled gracefully by Lexer::eof
|
||||
Some(Err(error)) => Err(ParserError::from(error)),
|
||||
Some(Ok(c)) => {
|
||||
if *c == '\u{feff}' {
|
||||
// skip UTF-8 BOM
|
||||
stream.next();
|
||||
}
|
||||
Ok(stream)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,912 +0,0 @@
|
||||
use crate::rug::Integer;
|
||||
use lexical::parse_lossy;
|
||||
use ordered_float::*;
|
||||
|
||||
use crate::ast::*;
|
||||
use crate::tabled_rc::*;
|
||||
|
||||
use std::convert::TryFrom;
|
||||
use std::fmt;
|
||||
use std::io::Read;
|
||||
use std::rc::Rc;
|
||||
|
||||
macro_rules! is_not_eof {
|
||||
($c:expr) => {
|
||||
match $c {
|
||||
Ok(c) => c,
|
||||
Err($crate::ast::ParserError::UnexpectedEOF) => return Ok(true),
|
||||
Err(e) => return Err(e),
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! consume_chars_with {
|
||||
($token:expr, $e:expr) => {
|
||||
loop {
|
||||
match $e {
|
||||
Ok(Some(c)) => $token.push(c),
|
||||
Ok(None) => continue,
|
||||
Err($crate::ast::ParserError::UnexpectedChar(..)) => break,
|
||||
Err(e) => return Err(e),
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum Token {
|
||||
Constant(Constant),
|
||||
Var(Rc<Atom>),
|
||||
Open, // '('
|
||||
OpenCT, // '('
|
||||
Close, // ')'
|
||||
OpenList, // '['
|
||||
CloseList, // ']'
|
||||
OpenCurly, // '{'
|
||||
CloseCurly, // '}'
|
||||
HeadTailSeparator, // '|'
|
||||
Comma, // ','
|
||||
End,
|
||||
}
|
||||
|
||||
impl Token {
|
||||
#[inline]
|
||||
pub(super) fn is_end(&self) -> bool {
|
||||
if let Token::End = self {
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Lexer<'a, R: Read> {
|
||||
pub(crate) atom_tbl: TabledData<Atom>,
|
||||
pub(crate) reader: &'a mut ParsingStream<R>,
|
||||
pub(crate) flags: MachineFlags,
|
||||
pub(crate) line_num: usize,
|
||||
pub(crate) col_num: usize,
|
||||
}
|
||||
|
||||
impl<'a, R: Read + fmt::Debug> fmt::Debug for Lexer<'a, R> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("Lexer")
|
||||
.field("atom_tbl", &self.atom_tbl)
|
||||
.field("reader", &"&'a mut ParsingStream<R>") // Hacky solution.
|
||||
.field("line_num", &self.line_num)
|
||||
.field("col_num", &self.col_num)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, R: Read> Lexer<'a, R> {
|
||||
pub fn new(
|
||||
atom_tbl: TabledData<Atom>,
|
||||
flags: MachineFlags,
|
||||
src: &'a mut ParsingStream<R>,
|
||||
) -> Self {
|
||||
Lexer {
|
||||
atom_tbl,
|
||||
flags,
|
||||
reader: src,
|
||||
line_num: 0,
|
||||
col_num: 0,
|
||||
}
|
||||
}
|
||||
|
||||
fn return_char(&mut self, c: char) {
|
||||
if new_line_char!(c) {
|
||||
self.line_num -= 1;
|
||||
self.col_num = 0;
|
||||
}
|
||||
|
||||
self.reader.put_back(Ok(c));
|
||||
}
|
||||
|
||||
fn skip_char(&mut self) -> Result<char, ParserError> {
|
||||
if let Some(Ok(c)) = self.reader.next() {
|
||||
self.col_num += 1;
|
||||
|
||||
if new_line_char!(c) {
|
||||
self.line_num += 1;
|
||||
self.col_num = 0;
|
||||
}
|
||||
|
||||
Ok(c)
|
||||
} else {
|
||||
Err(ParserError::UnexpectedEOF)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn eof(&mut self) -> Result<bool, ParserError> {
|
||||
if self.reader.peek().is_none() {
|
||||
return Ok(true);
|
||||
}
|
||||
|
||||
let mut c = is_not_eof!(self.lookahead_char());
|
||||
|
||||
while layout_char!(c) {
|
||||
self.skip_char()?;
|
||||
|
||||
if self.reader.peek().is_none() {
|
||||
return Ok(true);
|
||||
}
|
||||
|
||||
c = is_not_eof!(self.lookahead_char());
|
||||
}
|
||||
|
||||
Ok(false)
|
||||
}
|
||||
|
||||
pub fn lookahead_char(&mut self) -> Result<char, ParserError> {
|
||||
match self.reader.peek() {
|
||||
Some(&Ok(c)) => Ok(c),
|
||||
_ => Err(ParserError::UnexpectedEOF),
|
||||
}
|
||||
}
|
||||
|
||||
fn single_line_comment(&mut self) -> Result<(), ParserError> {
|
||||
loop {
|
||||
if self.reader.peek().is_none() || new_line_char!(self.skip_char()?) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn bracketed_comment(&mut self) -> Result<bool, ParserError> {
|
||||
// we have already checked that the current lookahead_char is comment_1_char, just skip it
|
||||
let c = self.skip_char()?;
|
||||
|
||||
if comment_2_char!(self.lookahead_char()?) {
|
||||
self.skip_char()?;
|
||||
|
||||
// Keep reading until we find characters '*' and '/'
|
||||
// Deliberately skip checks for prolog_char to allow comments to contain any characters,
|
||||
// including so-called "extended characters", without having to explicitly add them to a character class.
|
||||
let mut c = self.lookahead_char()?;
|
||||
loop {
|
||||
while !comment_2_char!(c) {
|
||||
self.skip_char()?;
|
||||
c = self.lookahead_char()?;
|
||||
}
|
||||
|
||||
self.skip_char()?;
|
||||
|
||||
c = self.lookahead_char()?;
|
||||
if comment_1_char!(c) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if prolog_char!(c) {
|
||||
self.skip_char()?;
|
||||
Ok(true)
|
||||
} else {
|
||||
Err(ParserError::NonPrologChar(self.line_num, self.col_num))
|
||||
}
|
||||
} else {
|
||||
self.return_char(c);
|
||||
Ok(false)
|
||||
}
|
||||
}
|
||||
|
||||
fn get_back_quoted_char(&mut self) -> Result<char, ParserError> {
|
||||
if back_quote_char!(self.lookahead_char()?) {
|
||||
let c = self.skip_char()?;
|
||||
|
||||
if !back_quote_char!(self.lookahead_char()?) {
|
||||
self.return_char(c);
|
||||
Err(ParserError::UnexpectedChar(c, self.line_num, self.col_num))
|
||||
} else {
|
||||
self.skip_char()
|
||||
}
|
||||
} else if single_quote_char!(self.lookahead_char()?) {
|
||||
self.skip_char()
|
||||
} else {
|
||||
self.get_non_quote_char()
|
||||
}
|
||||
}
|
||||
|
||||
fn get_back_quoted_item(&mut self) -> Result<Option<char>, ParserError> {
|
||||
if backslash_char!(self.lookahead_char()?) {
|
||||
let c = self.skip_char()?;
|
||||
|
||||
if new_line_char!(self.lookahead_char()?) {
|
||||
self.skip_char()?;
|
||||
Ok(None)
|
||||
} else {
|
||||
self.return_char(c);
|
||||
Err(ParserError::UnexpectedChar(c, self.line_num, self.col_num))
|
||||
}
|
||||
} else {
|
||||
self.get_back_quoted_char().map(Some)
|
||||
}
|
||||
}
|
||||
|
||||
fn get_back_quoted_string(&mut self) -> Result<String, ParserError> {
|
||||
let c = self.lookahead_char()?;
|
||||
|
||||
if back_quote_char!(c) {
|
||||
self.skip_char()?;
|
||||
|
||||
let mut token = String::new();
|
||||
consume_chars_with!(token, self.get_back_quoted_item());
|
||||
|
||||
if back_quote_char!(self.lookahead_char()?) {
|
||||
self.skip_char()?;
|
||||
Ok(token)
|
||||
} else {
|
||||
Err(ParserError::MissingQuote(self.line_num, self.col_num))
|
||||
}
|
||||
} else {
|
||||
Err(ParserError::UnexpectedChar(c, self.line_num, self.col_num))
|
||||
}
|
||||
}
|
||||
|
||||
fn get_single_quoted_item(&mut self) -> Result<Option<char>, ParserError> {
|
||||
if backslash_char!(self.lookahead_char()?) {
|
||||
let c = self.skip_char()?;
|
||||
|
||||
if new_line_char!(self.lookahead_char()?) {
|
||||
self.skip_char()?;
|
||||
return Ok(None);
|
||||
} else {
|
||||
self.return_char(c);
|
||||
}
|
||||
}
|
||||
|
||||
self.get_single_quoted_char().map(Some)
|
||||
}
|
||||
|
||||
fn get_single_quoted_char(&mut self) -> Result<char, ParserError> {
|
||||
let c = self.lookahead_char()?;
|
||||
|
||||
if single_quote_char!(c) {
|
||||
self.skip_char()?;
|
||||
|
||||
if !single_quote_char!(self.lookahead_char()?) {
|
||||
self.return_char(c);
|
||||
Err(ParserError::UnexpectedChar(c, self.line_num, self.col_num))
|
||||
} else {
|
||||
self.skip_char()
|
||||
}
|
||||
} else if double_quote_char!(c) || back_quote_char!(c) {
|
||||
self.skip_char()
|
||||
} else {
|
||||
self.get_non_quote_char()
|
||||
}
|
||||
}
|
||||
|
||||
fn get_double_quoted_item(&mut self) -> Result<Option<char>, ParserError> {
|
||||
if backslash_char!(self.lookahead_char()?) {
|
||||
let c = self.skip_char()?;
|
||||
|
||||
if new_line_char!(self.lookahead_char()?) {
|
||||
self.skip_char()?;
|
||||
return Ok(None);
|
||||
} else {
|
||||
self.return_char(c);
|
||||
}
|
||||
}
|
||||
|
||||
self.get_double_quoted_char().map(Some)
|
||||
}
|
||||
|
||||
fn get_double_quoted_char(&mut self) -> Result<char, ParserError> {
|
||||
if double_quote_char!(self.lookahead_char()?) {
|
||||
let c = self.skip_char()?;
|
||||
|
||||
if !double_quote_char!(self.lookahead_char()?) {
|
||||
self.return_char(c);
|
||||
Err(ParserError::UnexpectedChar(c, self.line_num, self.col_num))
|
||||
} else {
|
||||
self.skip_char()
|
||||
}
|
||||
} else if single_quote_char!(self.lookahead_char()?) {
|
||||
self.skip_char()
|
||||
} else if back_quote_char!(self.lookahead_char()?) {
|
||||
self.skip_char()
|
||||
} else {
|
||||
self.get_non_quote_char()
|
||||
}
|
||||
}
|
||||
|
||||
fn get_control_escape_sequence(&mut self) -> Result<char, ParserError> {
|
||||
let escaped = match self.lookahead_char()? {
|
||||
'a' => '\u{07}', // UTF-8 alert
|
||||
'b' => '\u{08}', // UTF-8 backspace
|
||||
'v' => '\u{0b}', // UTF-8 vertical tab
|
||||
'f' => '\u{0c}', // UTF-8 form feed
|
||||
't' => '\t',
|
||||
'n' => '\n',
|
||||
'r' => '\r',
|
||||
c => return Err(ParserError::UnexpectedChar(c, self.line_num, self.col_num)),
|
||||
};
|
||||
|
||||
self.skip_char()?;
|
||||
return Ok(escaped);
|
||||
}
|
||||
|
||||
fn get_octal_escape_sequence(&mut self) -> Result<char, ParserError> {
|
||||
self.escape_sequence_to_char(|c| octal_digit_char!(c), 8)
|
||||
}
|
||||
|
||||
fn get_hexadecimal_escape_sequence(&mut self) -> Result<char, ParserError> {
|
||||
self.skip_char()?;
|
||||
let c = self.lookahead_char()?;
|
||||
|
||||
if hexadecimal_digit_char!(c) {
|
||||
self.escape_sequence_to_char(|c| hexadecimal_digit_char!(c), 16)
|
||||
} else {
|
||||
Err(ParserError::UnexpectedChar(c, self.line_num, self.col_num))
|
||||
}
|
||||
}
|
||||
|
||||
fn escape_sequence_to_char(
|
||||
&mut self,
|
||||
accept_char: impl Fn(char) -> bool,
|
||||
radix: u32,
|
||||
) -> Result<char, ParserError> {
|
||||
let mut c = self.lookahead_char()?;
|
||||
let mut token = String::new();
|
||||
|
||||
loop {
|
||||
token.push(c);
|
||||
|
||||
self.skip_char()?;
|
||||
c = self.lookahead_char()?;
|
||||
|
||||
if !accept_char(c) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if backslash_char!(c) {
|
||||
self.skip_char()?;
|
||||
u32::from_str_radix(&token, radix).map_or_else(
|
||||
|_| Err(ParserError::ParseBigInt(self.line_num, self.col_num)),
|
||||
|n| {
|
||||
char::try_from(n)
|
||||
.map_err(|_| ParserError::Utf8Error(self.line_num, self.col_num))
|
||||
},
|
||||
)
|
||||
} else {
|
||||
// on failure, restore the token characters and backslash.
|
||||
self.reader.put_back_all(token.chars().map(Ok));
|
||||
self.reader.put_back(Ok('\\'));
|
||||
|
||||
Err(ParserError::UnexpectedChar(c, self.line_num, self.col_num))
|
||||
}
|
||||
}
|
||||
|
||||
fn get_non_quote_char(&mut self) -> Result<char, ParserError> {
|
||||
let c = self.lookahead_char()?;
|
||||
|
||||
if graphic_char!(c) || alpha_numeric_char!(c) || solo_char!(c) || space_char!(c) {
|
||||
self.skip_char()
|
||||
} else {
|
||||
if !backslash_char!(c) {
|
||||
return Err(ParserError::UnexpectedChar(c, self.line_num, self.col_num));
|
||||
}
|
||||
|
||||
self.skip_char()?;
|
||||
|
||||
let c = self.lookahead_char()?;
|
||||
|
||||
if meta_char!(c) {
|
||||
self.skip_char()
|
||||
} else if octal_digit_char!(c) {
|
||||
self.get_octal_escape_sequence()
|
||||
} else if symbolic_hexadecimal_char!(c) {
|
||||
self.get_hexadecimal_escape_sequence()
|
||||
} else {
|
||||
self.get_control_escape_sequence()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn char_code_list_token(&mut self) -> Result<String, ParserError> {
|
||||
let mut token = String::new();
|
||||
|
||||
self.skip_char()?;
|
||||
consume_chars_with!(token, self.get_double_quoted_item());
|
||||
|
||||
if double_quote_char!(self.lookahead_char()?) {
|
||||
self.skip_char()?;
|
||||
Ok(token)
|
||||
} else {
|
||||
Err(ParserError::MissingQuote(self.line_num, self.col_num))
|
||||
}
|
||||
}
|
||||
|
||||
fn hexadecimal_constant(&mut self) -> Result<Token, ParserError> {
|
||||
self.skip_char()?;
|
||||
|
||||
if hexadecimal_digit_char!(self.lookahead_char()?) {
|
||||
let mut token = String::new();
|
||||
|
||||
while hexadecimal_digit_char!(self.lookahead_char()?) {
|
||||
token.push(self.skip_char()?);
|
||||
}
|
||||
|
||||
isize::from_str_radix(&token, 16)
|
||||
.map(|n| Token::Constant(Constant::Fixnum(n)))
|
||||
.or_else(|_| {
|
||||
Integer::from_str_radix(&token, 16)
|
||||
.map(|n| Token::Constant(Constant::Integer(Rc::new(n))))
|
||||
.map_err(|_| ParserError::ParseBigInt(self.line_num, self.col_num))
|
||||
})
|
||||
} else {
|
||||
self.return_char('x');
|
||||
Err(ParserError::ParseBigInt(self.line_num, self.col_num))
|
||||
}
|
||||
}
|
||||
|
||||
fn octal_constant(&mut self) -> Result<Token, ParserError> {
|
||||
self.skip_char()?;
|
||||
|
||||
if octal_digit_char!(self.lookahead_char()?) {
|
||||
let mut token = String::new();
|
||||
|
||||
while octal_digit_char!(self.lookahead_char()?) {
|
||||
token.push(self.skip_char()?);
|
||||
}
|
||||
|
||||
isize::from_str_radix(&token, 8)
|
||||
.map(|n| Token::Constant(Constant::Fixnum(n)))
|
||||
.or_else(|_| {
|
||||
Integer::from_str_radix(&token, 8)
|
||||
.map(|n| Token::Constant(Constant::Integer(Rc::new(n))))
|
||||
.map_err(|_| ParserError::ParseBigInt(self.line_num, self.col_num))
|
||||
})
|
||||
} else {
|
||||
self.return_char('o');
|
||||
Err(ParserError::ParseBigInt(self.line_num, self.col_num))
|
||||
}
|
||||
}
|
||||
|
||||
fn binary_constant(&mut self) -> Result<Token, ParserError> {
|
||||
self.skip_char()?;
|
||||
|
||||
if binary_digit_char!(self.lookahead_char()?) {
|
||||
let mut token = String::new();
|
||||
|
||||
while binary_digit_char!(self.lookahead_char()?) {
|
||||
token.push(self.skip_char()?);
|
||||
}
|
||||
|
||||
isize::from_str_radix(&token, 2)
|
||||
.map(|n| Token::Constant(Constant::Fixnum(n)))
|
||||
.or_else(|_| {
|
||||
Integer::from_str_radix(&token, 2)
|
||||
.map(|n| Token::Constant(Constant::Integer(Rc::new(n))))
|
||||
.map_err(|_| ParserError::ParseBigInt(self.line_num, self.col_num))
|
||||
})
|
||||
} else {
|
||||
self.return_char('b');
|
||||
Err(ParserError::ParseBigInt(self.line_num, self.col_num))
|
||||
}
|
||||
}
|
||||
|
||||
fn variable_token(&mut self) -> Result<Token, ParserError> {
|
||||
let mut s = String::new();
|
||||
s.push(self.skip_char()?);
|
||||
|
||||
while alpha_numeric_char!(self.lookahead_char()?) {
|
||||
s.push(self.skip_char()?);
|
||||
}
|
||||
|
||||
Ok(Token::Var(rc_atom!(s)))
|
||||
}
|
||||
|
||||
fn name_token(&mut self, c: char) -> Result<Token, ParserError> {
|
||||
let mut token = String::new();
|
||||
|
||||
if small_letter_char!(c) {
|
||||
token.push(self.skip_char()?);
|
||||
|
||||
while alpha_numeric_char!(self.lookahead_char()?) {
|
||||
token.push(self.skip_char()?);
|
||||
}
|
||||
} else if graphic_token_char!(c) {
|
||||
token.push(self.skip_char()?);
|
||||
|
||||
while graphic_token_char!(self.lookahead_char()?) {
|
||||
token.push(self.skip_char()?);
|
||||
}
|
||||
} else if cut_char!(c) {
|
||||
token.push(self.skip_char()?);
|
||||
} else if semicolon_char!(c) {
|
||||
token.push(self.skip_char()?);
|
||||
} else if single_quote_char!(c) {
|
||||
self.skip_char()?;
|
||||
|
||||
consume_chars_with!(token, self.get_single_quoted_item());
|
||||
|
||||
if single_quote_char!(self.lookahead_char()?) {
|
||||
self.skip_char()?;
|
||||
|
||||
if !token.is_empty() && token.chars().nth(1).is_none() {
|
||||
if let Some(c) = token.chars().next() {
|
||||
return Ok(Token::Constant(Constant::Char(c)));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return Err(ParserError::InvalidSingleQuotedCharacter(
|
||||
self.lookahead_char()?,
|
||||
));
|
||||
}
|
||||
} else {
|
||||
match self.get_back_quoted_string() {
|
||||
Ok(_) => return Err(ParserError::BackQuotedString(self.line_num, self.col_num)),
|
||||
Err(e) => return Err(e),
|
||||
}
|
||||
}
|
||||
|
||||
if token.as_str() == "[]" {
|
||||
Ok(Token::Constant(Constant::EmptyList))
|
||||
} else {
|
||||
Ok(Token::Constant(atom!(token, self.atom_tbl)))
|
||||
}
|
||||
}
|
||||
|
||||
fn vacate_with_float(&mut self, mut token: String) -> Token {
|
||||
self.return_char(token.pop().unwrap());
|
||||
|
||||
let result = OrderedFloat(parse_lossy::<f64, _>(token.as_bytes()).unwrap());
|
||||
Token::Constant(Constant::Float(result))
|
||||
}
|
||||
|
||||
fn skip_underscore_in_number(&mut self) -> Result<char, ParserError> {
|
||||
let mut c = self.lookahead_char()?;
|
||||
|
||||
if c == '_' {
|
||||
self.skip_char()?;
|
||||
self.scan_for_layout()?;
|
||||
c = self.lookahead_char()?;
|
||||
|
||||
if decimal_digit_char!(c) {
|
||||
Ok(c)
|
||||
} else {
|
||||
Err(ParserError::ParseBigInt(self.line_num, self.col_num))
|
||||
}
|
||||
} else {
|
||||
Ok(c)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn number_token(&mut self) -> Result<Token, ParserError> {
|
||||
let mut token = String::new();
|
||||
|
||||
token.push(self.skip_char()?);
|
||||
let mut c = self.skip_underscore_in_number()?;
|
||||
|
||||
while decimal_digit_char!(c) {
|
||||
token.push(c);
|
||||
self.skip_char()?;
|
||||
c = self.skip_underscore_in_number()?;
|
||||
}
|
||||
|
||||
if decimal_point_char!(c) {
|
||||
self.skip_char()?;
|
||||
|
||||
if self.reader.peek().is_none() {
|
||||
self.return_char('.');
|
||||
|
||||
isize::from_str_radix(&token, 10)
|
||||
.map(|n| Token::Constant(Constant::Fixnum(n)))
|
||||
.or_else(|_| {
|
||||
token
|
||||
.parse::<Integer>()
|
||||
.map(|n| Token::Constant(Constant::Integer(Rc::new(n))))
|
||||
.map_err(|_| ParserError::ParseBigInt(self.line_num, self.col_num))
|
||||
})
|
||||
} else if decimal_digit_char!(self.lookahead_char()?) {
|
||||
token.push('.');
|
||||
token.push(self.skip_char()?);
|
||||
|
||||
let mut c = self.lookahead_char()?;
|
||||
|
||||
while decimal_digit_char!(c) {
|
||||
token.push(c);
|
||||
self.skip_char()?;
|
||||
c = self.lookahead_char()?;
|
||||
}
|
||||
|
||||
if exponent_char!(self.lookahead_char()?) {
|
||||
token.push(self.skip_char()?);
|
||||
|
||||
let c = match self.lookahead_char() {
|
||||
Err(_) => return Ok(self.vacate_with_float(token)),
|
||||
Ok(c) => c,
|
||||
};
|
||||
|
||||
if !sign_char!(c) && !decimal_digit_char!(c) {
|
||||
return Ok(self.vacate_with_float(token));
|
||||
}
|
||||
|
||||
if sign_char!(c) {
|
||||
token.push(self.skip_char()?);
|
||||
|
||||
let c = match self.lookahead_char() {
|
||||
Err(_) => {
|
||||
self.return_char(token.pop().unwrap());
|
||||
return Ok(self.vacate_with_float(token));
|
||||
}
|
||||
Ok(c) => c,
|
||||
};
|
||||
|
||||
if !decimal_digit_char!(c) {
|
||||
self.return_char(token.pop().unwrap());
|
||||
return Ok(self.vacate_with_float(token));
|
||||
}
|
||||
}
|
||||
|
||||
if decimal_digit_char!(self.lookahead_char()?) {
|
||||
token.push(self.skip_char()?);
|
||||
|
||||
while decimal_digit_char!(self.lookahead_char()?) {
|
||||
token.push(self.skip_char()?);
|
||||
}
|
||||
|
||||
let n = OrderedFloat(parse_lossy::<f64, _>(token.as_bytes()).unwrap());
|
||||
Ok(Token::Constant(Constant::Float(n)))
|
||||
} else {
|
||||
return Ok(self.vacate_with_float(token));
|
||||
}
|
||||
} else {
|
||||
let n = OrderedFloat(parse_lossy::<f64, _>(token.as_bytes()).unwrap());
|
||||
Ok(Token::Constant(Constant::Float(n)))
|
||||
}
|
||||
} else {
|
||||
self.return_char('.');
|
||||
|
||||
isize::from_str_radix(&token, 10)
|
||||
.map(|n| Token::Constant(Constant::Fixnum(n)))
|
||||
.or_else(|_| {
|
||||
token
|
||||
.parse::<Integer>()
|
||||
.map(|n| Token::Constant(Constant::Integer(Rc::new(n))))
|
||||
.map_err(|_| ParserError::ParseBigInt(self.line_num, self.col_num))
|
||||
})
|
||||
}
|
||||
} else {
|
||||
if token.starts_with('0') && token.len() == 1 {
|
||||
if c == 'x' {
|
||||
self.hexadecimal_constant().or_else(|e| {
|
||||
if let ParserError::ParseBigInt(..) = e {
|
||||
isize::from_str_radix(&token, 10)
|
||||
.map(|n| Token::Constant(Constant::Fixnum(n)))
|
||||
.or_else(|_| {
|
||||
token
|
||||
.parse::<Integer>()
|
||||
.map(|n| Token::Constant(Constant::Integer(Rc::new(n))))
|
||||
.map_err(|_| {
|
||||
ParserError::ParseBigInt(self.line_num, self.col_num)
|
||||
})
|
||||
})
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
})
|
||||
} else if c == 'o' {
|
||||
self.octal_constant().or_else(|e| {
|
||||
if let ParserError::ParseBigInt(..) = e {
|
||||
isize::from_str_radix(&token, 10)
|
||||
.map(|n| Token::Constant(Constant::Fixnum(n)))
|
||||
.or_else(|_| {
|
||||
token
|
||||
.parse::<Integer>()
|
||||
.map(|n| Token::Constant(Constant::Integer(Rc::new(n))))
|
||||
.map_err(|_| {
|
||||
ParserError::ParseBigInt(self.line_num, self.col_num)
|
||||
})
|
||||
})
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
})
|
||||
} else if c == 'b' {
|
||||
self.binary_constant().or_else(|e| {
|
||||
if let ParserError::ParseBigInt(..) = e {
|
||||
isize::from_str_radix(&token, 10)
|
||||
.map(|n| Token::Constant(Constant::Fixnum(n)))
|
||||
.or_else(|_| {
|
||||
token
|
||||
.parse::<Integer>()
|
||||
.map(|n| Token::Constant(Constant::Integer(Rc::new(n))))
|
||||
.map_err(|_| {
|
||||
ParserError::ParseBigInt(self.line_num, self.col_num)
|
||||
})
|
||||
})
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
})
|
||||
} else if single_quote_char!(c) {
|
||||
self.skip_char()?;
|
||||
|
||||
if backslash_char!(self.lookahead_char()?) {
|
||||
self.skip_char()?;
|
||||
|
||||
if new_line_char!(self.lookahead_char()?) {
|
||||
self.return_char('\\');
|
||||
self.return_char('\'');
|
||||
|
||||
return Ok(Token::Constant(Constant::Fixnum(0)));
|
||||
} else {
|
||||
self.return_char('\\');
|
||||
}
|
||||
}
|
||||
|
||||
self.get_single_quoted_char()
|
||||
.map(|c| Token::Constant(Constant::Fixnum(c as isize)))
|
||||
.or_else(|_| {
|
||||
self.return_char(c);
|
||||
|
||||
isize::from_str_radix(&token, 10)
|
||||
.map(|n| Token::Constant(Constant::Fixnum(n)))
|
||||
.or_else(|_| {
|
||||
token
|
||||
.parse::<Integer>()
|
||||
.map(|n| Token::Constant(Constant::Integer(Rc::new(n))))
|
||||
.map_err(|_| {
|
||||
ParserError::ParseBigInt(self.line_num, self.col_num)
|
||||
})
|
||||
})
|
||||
})
|
||||
} else {
|
||||
isize::from_str_radix(&token, 10)
|
||||
.map(|n| Token::Constant(Constant::Fixnum(n)))
|
||||
.or_else(|_| {
|
||||
token
|
||||
.parse::<Integer>()
|
||||
.map(|n| Token::Constant(Constant::Integer(Rc::new(n))))
|
||||
.map_err(|_| ParserError::ParseBigInt(self.line_num, self.col_num))
|
||||
})
|
||||
}
|
||||
} else {
|
||||
isize::from_str_radix(&token, 10)
|
||||
.map(|n| Token::Constant(Constant::Fixnum(n)))
|
||||
.or_else(|_| {
|
||||
token
|
||||
.parse::<Integer>()
|
||||
.map(|n| Token::Constant(Constant::Integer(Rc::new(n))))
|
||||
.map_err(|_| ParserError::ParseBigInt(self.line_num, self.col_num))
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn scan_for_layout(&mut self) -> Result<bool, ParserError> {
|
||||
let mut layout_inserted = false;
|
||||
let mut more_layout = true;
|
||||
|
||||
loop {
|
||||
let cr = self.lookahead_char();
|
||||
|
||||
match cr {
|
||||
Ok(c) if layout_char!(c) => {
|
||||
self.skip_char()?;
|
||||
layout_inserted = true;
|
||||
}
|
||||
Ok(c) if end_line_comment_char!(c) => {
|
||||
self.single_line_comment()?;
|
||||
layout_inserted = true;
|
||||
}
|
||||
Ok(c) if comment_1_char!(c) => {
|
||||
if self.bracketed_comment()? {
|
||||
layout_inserted = true;
|
||||
} else {
|
||||
more_layout = false;
|
||||
}
|
||||
}
|
||||
_ => more_layout = false,
|
||||
};
|
||||
|
||||
if !more_layout {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(layout_inserted)
|
||||
}
|
||||
|
||||
pub fn next_token(&mut self) -> Result<Token, ParserError> {
|
||||
let layout_inserted = self.scan_for_layout()?;
|
||||
let cr = self.lookahead_char();
|
||||
|
||||
match cr {
|
||||
Ok(c) => {
|
||||
if capital_letter_char!(c) || variable_indicator_char!(c) {
|
||||
return self.variable_token();
|
||||
}
|
||||
|
||||
if c == ',' {
|
||||
self.skip_char()?;
|
||||
return Ok(Token::Comma);
|
||||
}
|
||||
|
||||
if c == ')' {
|
||||
self.skip_char()?;
|
||||
return Ok(Token::Close);
|
||||
}
|
||||
|
||||
if c == '(' {
|
||||
self.skip_char()?;
|
||||
return Ok(if layout_inserted {
|
||||
Token::Open
|
||||
} else {
|
||||
Token::OpenCT
|
||||
});
|
||||
}
|
||||
|
||||
if c == '.' {
|
||||
self.skip_char()?;
|
||||
|
||||
match self.lookahead_char() {
|
||||
Ok(c) if layout_char!(c) || c == '%' => {
|
||||
if new_line_char!(c) {
|
||||
self.skip_char()?;
|
||||
}
|
||||
|
||||
return Ok(Token::End);
|
||||
}
|
||||
Err(ParserError::UnexpectedEOF) => {
|
||||
return Ok(Token::End);
|
||||
}
|
||||
_ => {
|
||||
self.return_char('.');
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
if decimal_digit_char!(c) {
|
||||
return self.number_token();
|
||||
}
|
||||
|
||||
if c == ']' {
|
||||
self.skip_char()?;
|
||||
return Ok(Token::CloseList);
|
||||
}
|
||||
|
||||
if c == '[' {
|
||||
self.skip_char()?;
|
||||
return Ok(Token::OpenList);
|
||||
}
|
||||
|
||||
if c == '|' {
|
||||
self.skip_char()?;
|
||||
return Ok(Token::HeadTailSeparator);
|
||||
}
|
||||
|
||||
if c == '{' {
|
||||
self.skip_char()?;
|
||||
return Ok(Token::OpenCurly);
|
||||
}
|
||||
|
||||
if c == '}' {
|
||||
self.skip_char()?;
|
||||
return Ok(Token::CloseCurly);
|
||||
}
|
||||
|
||||
if c == '"' {
|
||||
let s = self.char_code_list_token()?;
|
||||
|
||||
if let DoubleQuotes::Atom = self.flags.double_quotes {
|
||||
let s = clause_name!(s, self.atom_tbl);
|
||||
return Ok(Token::Constant(Constant::Atom(s, None)));
|
||||
} else {
|
||||
let s = Rc::new(s);
|
||||
return Ok(Token::Constant(Constant::String(s)));
|
||||
}
|
||||
}
|
||||
|
||||
self.name_token(c)
|
||||
}
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
#[cfg(feature = "num-rug-adapter")]
|
||||
use num_rug_adapter as rug;
|
||||
#[cfg(feature = "rug")]
|
||||
use rug;
|
||||
|
||||
#[macro_use]
|
||||
pub mod tabled_rc;
|
||||
#[macro_use]
|
||||
pub mod ast;
|
||||
#[macro_use]
|
||||
pub mod macros;
|
||||
pub mod parser;
|
||||
pub mod put_back_n;
|
||||
|
||||
pub mod lexer;
|
||||
@@ -1,253 +0,0 @@
|
||||
#[macro_export]
|
||||
macro_rules! char_class {
|
||||
($c: expr, [$head:expr]) => ($c == $head);
|
||||
($c: expr, [$head:expr $(, $cs:expr)+]) => ($c == $head || $crate::char_class!($c, [$($cs),*]));
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! alpha_char {
|
||||
($c: expr) => {
|
||||
match $c {
|
||||
'a'..='z' => true,
|
||||
'A'..='Z' => true,
|
||||
'_' => true,
|
||||
'\u{00A0}'..='\u{00BF}' => true,
|
||||
'\u{00C0}'..='\u{00D6}' => true,
|
||||
'\u{00D8}'..='\u{00F6}' => true,
|
||||
'\u{00F8}'..='\u{00FF}' => true,
|
||||
'\u{0100}'..='\u{017F}' => true, // Latin Extended-A
|
||||
'\u{0180}'..='\u{024F}' => true, // Latin Extended-B
|
||||
'\u{0250}'..='\u{02AF}' => true, // IPA Extensions
|
||||
'\u{02B0}'..='\u{02FF}' => true, // Spacing Modifier Letters
|
||||
'\u{0300}'..='\u{036F}' => true, // Combining Diacritical Marks
|
||||
'\u{0370}'..='\u{03FF}' => true, // Greek/Coptic
|
||||
'\u{0400}'..='\u{04FF}' => true, // Cyrillic
|
||||
'\u{0500}'..='\u{052F}' => true, // Cyrillic Supplement
|
||||
'\u{0530}'..='\u{058F}' => true, // Armenian
|
||||
'\u{0590}'..='\u{05FF}' => true, // Hebrew
|
||||
'\u{0600}'..='\u{06FF}' => true, // Arabic
|
||||
'\u{0700}'..='\u{074F}' => true, // Syriac
|
||||
_ => false,
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! alpha_numeric_char {
|
||||
($c: expr) => {
|
||||
$crate::alpha_char!($c) || $crate::decimal_digit_char!($c)
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! backslash_char {
|
||||
($c: expr) => {
|
||||
$c == '\\'
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! back_quote_char {
|
||||
($c: expr) => {
|
||||
$c == '`'
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! binary_digit_char {
|
||||
($c: expr) => {
|
||||
$c >= '0' && $c <= '1'
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! capital_letter_char {
|
||||
($c: expr) => {
|
||||
('A'..='Z').contains(&$c)
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! comment_1_char {
|
||||
($c: expr) => {
|
||||
$c == '/'
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! comment_2_char {
|
||||
($c: expr) => {
|
||||
$c == '*'
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! cut_char {
|
||||
($c: expr) => {
|
||||
$c == '!'
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! decimal_digit_char {
|
||||
($c: expr) => {
|
||||
('0'..='9').contains(&$c)
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! decimal_point_char {
|
||||
($c: expr) => {
|
||||
$c == '.'
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! double_quote_char {
|
||||
($c: expr) => {
|
||||
$c == '"'
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! end_line_comment_char {
|
||||
($c: expr) => {
|
||||
$c == '%'
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! exponent_char {
|
||||
($c: expr) => {
|
||||
$c == 'e' || $c == 'E'
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! graphic_char {
|
||||
($c: expr) => ($crate::char_class!($c, ['#', '$', '&', '*', '+', '-', '.', '/', ':',
|
||||
'<', '=', '>', '?', '@', '^', '~']))
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! graphic_token_char {
|
||||
($c: expr) => {
|
||||
$crate::graphic_char!($c) || $crate::backslash_char!($c)
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! hexadecimal_digit_char {
|
||||
($c: expr) => {
|
||||
('0'..='9').contains(&$c) || ('A'..='F').contains(&$c) || ('a'..='f').contains(&$c)
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! layout_char {
|
||||
($c: expr) => {
|
||||
$crate::char_class!($c, [' ', '\n', '\t', '\u{0B}', '\u{0C}'])
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! meta_char {
|
||||
($c: expr) => {
|
||||
$crate::char_class!($c, ['\\', '\'', '"', '`'])
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! new_line_char {
|
||||
($c: expr) => {
|
||||
$c == '\n'
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! octal_digit_char {
|
||||
($c: expr) => {
|
||||
('0'..='7').contains(&$c)
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! octet_char {
|
||||
($c: expr) => {
|
||||
('\u{0000}'..='\u{00FF}').contains(&$c)
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! prolog_char {
|
||||
($c: expr) => {
|
||||
$crate::graphic_char!($c)
|
||||
|| $crate::alpha_numeric_char!($c)
|
||||
|| $crate::solo_char!($c)
|
||||
|| $crate::layout_char!($c)
|
||||
|| $crate::meta_char!($c)
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! semicolon_char {
|
||||
($c: expr) => {
|
||||
$c == ';'
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! sign_char {
|
||||
($c: expr) => {
|
||||
$c == '-' || $c == '+'
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! single_quote_char {
|
||||
($c: expr) => {
|
||||
$c == '\''
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! small_letter_char {
|
||||
($c: expr) => {
|
||||
('a'..='z').contains(&$c)
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! solo_char {
|
||||
($c: expr) => {
|
||||
$crate::char_class!($c, ['!', '(', ')', ',', ';', '[', ']', '{', '}', '|', '%'])
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! space_char {
|
||||
($c: expr) => {
|
||||
$c == ' '
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! symbolic_control_char {
|
||||
($c: expr) => {
|
||||
$crate::char_class!($c, ['a', 'b', 'f', 'n', 'r', 't', 'v', '0'])
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! symbolic_hexadecimal_char {
|
||||
($c: expr) => {
|
||||
$c == 'x'
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! variable_indicator_char {
|
||||
($c: expr) => {
|
||||
$c == '_'
|
||||
};
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,71 +0,0 @@
|
||||
use std::iter::Peekable;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct PutBackN<I: Iterator> {
|
||||
top: Vec<I::Item>,
|
||||
iter: Peekable<I>,
|
||||
}
|
||||
|
||||
pub fn put_back_n<I>(iterable: I) -> PutBackN<I::IntoIter>
|
||||
where I: IntoIterator
|
||||
{
|
||||
PutBackN {
|
||||
top: Vec::new(),
|
||||
iter: iterable.into_iter().peekable(),
|
||||
}
|
||||
}
|
||||
|
||||
impl<I: Iterator> PutBackN<I> {
|
||||
#[inline]
|
||||
pub(crate)
|
||||
fn put_back(&mut self, item: I::Item) {
|
||||
self.top.push(item);
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn take_buf(&mut self) -> Vec<I::Item> {
|
||||
std::mem::replace(&mut self.top, vec![])
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate)
|
||||
fn peek(&mut self) -> Option<&I::Item> {
|
||||
if self.top.is_empty() {
|
||||
/* This is a kludge for Ctrl-D not being
|
||||
* handled properly if self.iter().peek() isn't called
|
||||
* first. */
|
||||
match self.iter.peek() {
|
||||
Some(_) => {
|
||||
self.iter.next().and_then(move |item| {
|
||||
self.top.push(item);
|
||||
self.top.last()
|
||||
})
|
||||
}
|
||||
None => {
|
||||
None
|
||||
}
|
||||
}
|
||||
} else {
|
||||
self.top.last()
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate)
|
||||
fn put_back_all<DEI: DoubleEndedIterator<Item = I::Item>>(&mut self, iter: DEI) {
|
||||
self.top.extend(iter.rev());
|
||||
}
|
||||
}
|
||||
|
||||
impl<I: Iterator> Iterator for PutBackN<I> {
|
||||
type Item = I::Item;
|
||||
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<I::Item> {
|
||||
if self.top.is_empty() {
|
||||
self.iter.next()
|
||||
} else {
|
||||
self.top.pop()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,154 +0,0 @@
|
||||
use std::cell::{RefCell, RefMut};
|
||||
use std::cmp::Ordering;
|
||||
use std::collections::HashSet;
|
||||
use std::fmt;
|
||||
use std::hash::{Hash, Hasher};
|
||||
use std::ops::Deref;
|
||||
use std::rc::Rc;
|
||||
|
||||
pub struct TabledData<T> {
|
||||
table: Rc<RefCell<HashSet<Rc<T>>>>,
|
||||
pub(crate) module_name: Rc<String>,
|
||||
}
|
||||
|
||||
impl<T: Hash + Eq + fmt::Debug> fmt::Debug for TabledData<T> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("TabledData")
|
||||
.field("table", &self.table)
|
||||
.field("module_name", &self.table)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Clone for TabledData<T> {
|
||||
fn clone(&self) -> Self {
|
||||
TabledData {
|
||||
table: self.table.clone(),
|
||||
module_name: self.module_name.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: PartialEq> PartialEq for TabledData<T> {
|
||||
fn eq(&self, other: &TabledData<T>) -> bool {
|
||||
Rc::ptr_eq(&self.table, &other.table) && self.module_name == other.module_name
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Hash + Eq> TabledData<T> {
|
||||
#[inline]
|
||||
pub fn new(module_name: Rc<String>) -> Self {
|
||||
TabledData {
|
||||
table: Rc::new(RefCell::new(HashSet::new())),
|
||||
module_name,
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn borrow_mut(&self) -> RefMut<HashSet<Rc<T>>> {
|
||||
self.table.borrow_mut()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct TabledRc<T: Hash + Eq> {
|
||||
pub(crate) atom: Rc<T>,
|
||||
pub table: TabledData<T>,
|
||||
}
|
||||
|
||||
impl<T: Hash + Eq + fmt::Debug> fmt::Debug for TabledRc<T> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("TabledRc")
|
||||
.field("atom", &self.atom)
|
||||
.field("table", &self.table)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
// this Clone instance is manually defined to prevent the compiler
|
||||
// from complaining when deriving Clone for StringList.
|
||||
impl<T: Hash + Eq> Clone for TabledRc<T> {
|
||||
fn clone(&self) -> Self {
|
||||
TabledRc {
|
||||
atom: self.atom.clone(),
|
||||
table: self.table.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Ord + Hash + Eq> PartialOrd for TabledRc<T> {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||
Some(self.atom.cmp(&other.atom))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Ord + Hash + Eq> Ord for TabledRc<T> {
|
||||
fn cmp(&self, other: &Self) -> Ordering {
|
||||
self.atom.cmp(&other.atom)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Hash + Eq> PartialEq for TabledRc<T> {
|
||||
fn eq(&self, other: &TabledRc<T>) -> bool {
|
||||
self.atom == other.atom
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Hash + Eq> Eq for TabledRc<T> {}
|
||||
|
||||
impl<T: Hash + Eq> Hash for TabledRc<T> {
|
||||
fn hash<H: Hasher>(&self, state: &mut H) {
|
||||
self.atom.hash(state)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Hash + Eq + ToString> TabledRc<T> {
|
||||
pub fn new(atom: T, table: TabledData<T>) -> Self {
|
||||
let atom = match table.borrow_mut().take(&atom) {
|
||||
Some(atom) => atom,
|
||||
None => Rc::new(atom),
|
||||
};
|
||||
|
||||
table.borrow_mut().insert(atom.clone());
|
||||
|
||||
TabledRc { atom, table }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn inner(&self) -> Rc<T> {
|
||||
self.atom.clone()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn owning_module(&self) -> Rc<String> {
|
||||
self.table.module_name.clone()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Hash + Eq> Drop for TabledRc<T> {
|
||||
fn drop(&mut self) {
|
||||
if Rc::strong_count(&self.atom) == 2 {
|
||||
self.table.borrow_mut().remove(&self.atom);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Hash + Eq> Deref for TabledRc<T> {
|
||||
type Target = T;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&*self.atom
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Hash + Eq + fmt::Display> fmt::Display for TabledRc<T> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{}", &*self.atom)
|
||||
}
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! tabled_rc {
|
||||
($e:expr, $tbl:expr) => {
|
||||
$crate::tabled_rc::TabledRc::new(String::from($e), $tbl.clone())
|
||||
};
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
use prolog_parser::ast::*;
|
||||
use prolog_parser::lexer::{Lexer, Token};
|
||||
use prolog_parser::tabled_rc::TabledData;
|
||||
|
||||
use std::rc::Rc;
|
||||
|
||||
#[test]
|
||||
fn valid_token() {
|
||||
let stream = parsing_stream("valid text".as_bytes());
|
||||
assert!(stream.is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_stream() {
|
||||
let bytes: &[u8] = &[];
|
||||
assert!(parsing_stream(bytes).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn skip_utf8_bom() {
|
||||
let atom_tbl = TabledData::new(Rc::new("my_module".to_string()));
|
||||
let flags = MachineFlags::default();
|
||||
let bytes: &[u8] = &[0xEF, 0xBB, 0xBF, '4' as u8, '\n' as u8];
|
||||
let mut stream = parsing_stream(bytes).expect("valid stream");
|
||||
let mut lexer = Lexer::new(atom_tbl, flags, &mut stream);
|
||||
match lexer.next_token() {
|
||||
Ok(Token::Constant(Constant::Fixnum(4))) => (),
|
||||
_ => assert!(false),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_utf16_bom() {
|
||||
let bytes: &[u8] = &[0xFF, 0xFE, 'a' as u8, '\n' as u8];
|
||||
let stream = parsing_stream(bytes);
|
||||
match stream {
|
||||
Err(ParserError::Utf8Error(0, 0)) => (),
|
||||
_ => assert!(false),
|
||||
}
|
||||
}
|
||||
@@ -1,112 +0,0 @@
|
||||
use prolog_parser::ast::*;
|
||||
use prolog_parser::lexer::{Lexer, Token};
|
||||
use prolog_parser::tabled_rc::TabledData;
|
||||
|
||||
use std::rc::Rc;
|
||||
|
||||
fn read_all_tokens(text: &str) -> Result<Vec<Token>, ParserError> {
|
||||
let atom_tbl = TabledData::new(Rc::new("my_module".to_string()));
|
||||
let flags = MachineFlags::default();
|
||||
let mut stream = parsing_stream(text.as_bytes())?;
|
||||
let mut lexer = Lexer::new(atom_tbl, flags, &mut stream);
|
||||
|
||||
let mut tokens = Vec::new();
|
||||
while !lexer.eof()? {
|
||||
let token = lexer.next_token()?;
|
||||
tokens.push(token);
|
||||
}
|
||||
Ok(tokens)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_multiline_comment() -> Result<(), ParserError> {
|
||||
let tokens = read_all_tokens("/**/ 4\n")?;
|
||||
assert_eq!(tokens, [Token::Constant(Constant::Fixnum(4))]);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn any_char_multiline_comment() -> Result<(), ParserError> {
|
||||
let tokens = read_all_tokens("/* █╗╚═══╝ © */ 4\n")?;
|
||||
assert_eq!(tokens, [Token::Constant(Constant::Fixnum(4))]);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn simple_char() -> Result<(), ParserError> {
|
||||
let tokens = read_all_tokens("'a'\n")?;
|
||||
assert_eq!(tokens, [Token::Constant(Constant::Char('a'))]);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn char_with_meta_seq() -> Result<(), ParserError> {
|
||||
let tokens = read_all_tokens(r#"'\\' '\'' '\"' '\`' "#)?; // use literal string so \ are escaped
|
||||
assert_eq!(
|
||||
tokens,
|
||||
[
|
||||
Token::Constant(Constant::Char('\\')),
|
||||
Token::Constant(Constant::Char('\'')),
|
||||
Token::Constant(Constant::Char('"')),
|
||||
Token::Constant(Constant::Char('`'))
|
||||
]
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn char_with_control_seq() -> Result<(), ParserError> {
|
||||
let tokens = read_all_tokens(r"'\a' '\b' '\r' '\f' '\t' '\n' '\v' ")?;
|
||||
assert_eq!(
|
||||
tokens,
|
||||
[
|
||||
Token::Constant(Constant::Char('\u{07}')),
|
||||
Token::Constant(Constant::Char('\u{08}')),
|
||||
Token::Constant(Constant::Char('\r')),
|
||||
Token::Constant(Constant::Char('\u{0c}')),
|
||||
Token::Constant(Constant::Char('\t')),
|
||||
Token::Constant(Constant::Char('\n')),
|
||||
Token::Constant(Constant::Char('\u{0b}')),
|
||||
]
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn char_with_octseq() -> Result<(), ParserError> {
|
||||
let tokens = read_all_tokens(r"'\60433\' ")?;
|
||||
assert_eq!(tokens, [Token::Constant(Constant::Char('愛'))]); // Japanese character
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn char_with_octseq_0() -> Result<(), ParserError> {
|
||||
let tokens = read_all_tokens(r"'\0\' ")?;
|
||||
assert_eq!(tokens, [Token::Constant(Constant::Char('\u{0000}'))]);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn char_with_hexseq() -> Result<(), ParserError> {
|
||||
let tokens = read_all_tokens(r"'\x2124\' ")?;
|
||||
assert_eq!(tokens, [Token::Constant(Constant::Char('ℤ'))]); // Z math symbol
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn char_with_hexseq_invalid() {
|
||||
assert!(read_all_tokens(r"'\x\' ").is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty() -> Result<(), ParserError> {
|
||||
let tokens = read_all_tokens("")?;
|
||||
assert!(tokens.is_empty());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn comment_then_eof() -> Result<(), ParserError> {
|
||||
assert!(read_all_tokens("% only a comment").is_err());
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user