fix macro hygien

This commit is contained in:
Skgland
2021-02-06 19:39:02 +01:00
parent bc6159c538
commit 64433bd8dd
6 changed files with 176 additions and 107 deletions

View File

@@ -40,20 +40,23 @@ pub const NEGATIVE_SIGN: u32 = 0x0200;
#[macro_export] #[macro_export]
macro_rules! clause_name { macro_rules! clause_name {
($name: expr, $tbl: expr) => { ($name: expr, $tbl: expr) => {
ClauseName::User(TabledRc::new($name, $tbl.clone())) $crate::ast::ClauseName::User($crate::tabled_rc::TabledRc::new($name, $tbl.clone()))
}; };
($name: expr) => { ($name: expr) => {
ClauseName::BuiltIn($name) $crate::ast::ClauseName::BuiltIn($name)
}; };
} }
#[macro_export] #[macro_export]
macro_rules! atom { macro_rules! atom {
($e:expr, $tbl:expr) => { ($e:expr, $tbl:expr) => {
Constant::Atom(ClauseName::User(tabled_rc!($e, $tbl)), None) $crate::ast::Constant::Atom(
$crate::ast::ClauseName::User($crate::tabled_rc!($e, $tbl)),
None,
)
}; };
($e:expr) => { ($e:expr) => {
Constant::Atom(clause_name!($e), None) $crate::ast::Constant::Atom($crate::clause_name!($e), None)
}; };
} }
@@ -65,95 +68,102 @@ macro_rules! rc_atom {
} }
macro_rules! is_term { macro_rules! is_term {
($x:expr) => { ($x:expr) => {
($x & TERM) != 0 ($x & $crate::ast::TERM) != 0
}; };
} }
macro_rules! is_lterm { macro_rules! is_lterm {
($x:expr) => { ($x:expr) => {
($x & LTERM) != 0 ($x & $crate::ast::LTERM) != 0
}; };
} }
macro_rules! is_op { macro_rules! is_op {
($x:expr) => { ($x:expr) => {
$x & (XF | YF | FX | FY | XFX | XFY | YFX) != 0 $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 { macro_rules! is_negate {
($x:expr) => { ($x:expr) => {
($x & NEGATIVE_SIGN) != 0 ($x & $crate::ast::NEGATIVE_SIGN) != 0
}; };
} }
#[macro_export] #[macro_export]
macro_rules! is_prefix { macro_rules! is_prefix {
($x:expr) => { ($x:expr) => {
$x & (FX | FY) != 0 $x & ($crate::ast::FX | $crate::ast::FY) != 0
}; };
} }
#[macro_export] #[macro_export]
macro_rules! is_postfix { macro_rules! is_postfix {
($x:expr) => { ($x:expr) => {
$x & (XF | YF) != 0 $x & ($crate::ast::XF | $crate::ast::YF) != 0
}; };
} }
#[macro_export] #[macro_export]
macro_rules! is_infix { macro_rules! is_infix {
($x:expr) => { ($x:expr) => {
($x & (XFX | XFY | YFX)) != 0 ($x & ($crate::ast::XFX | $crate::ast::XFY | $crate::ast::YFX)) != 0
}; };
} }
#[macro_export] #[macro_export]
macro_rules! is_xfx { macro_rules! is_xfx {
($x:expr) => { ($x:expr) => {
($x & XFX) != 0 ($x & $crate::ast::XFX) != 0
}; };
} }
#[macro_export] #[macro_export]
macro_rules! is_xfy { macro_rules! is_xfy {
($x:expr) => { ($x:expr) => {
($x & XFY) != 0 ($x & $crate::ast::XFY) != 0
}; };
} }
#[macro_export] #[macro_export]
macro_rules! is_yfx { macro_rules! is_yfx {
($x:expr) => { ($x:expr) => {
($x & YFX) != 0 ($x & $crate::ast::YFX) != 0
}; };
} }
#[macro_export] #[macro_export]
macro_rules! is_yf { macro_rules! is_yf {
($x:expr) => { ($x:expr) => {
($x & YF) != 0 ($x & $crate::ast::YF) != 0
}; };
} }
#[macro_export] #[macro_export]
macro_rules! is_xf { macro_rules! is_xf {
($x:expr) => { ($x:expr) => {
($x & XF) != 0 ($x & $crate::ast::XF) != 0
}; };
} }
#[macro_export] #[macro_export]
macro_rules! is_fx { macro_rules! is_fx {
($x:expr) => { ($x:expr) => {
($x & FX) != 0 ($x & $crate::ast::FX) != 0
}; };
} }
#[macro_export] #[macro_export]
macro_rules! is_fy { macro_rules! is_fy {
($x:expr) => { ($x:expr) => {
($x & FY) != 0 ($x & $crate::ast::FY) != 0
}; };
} }
@@ -227,14 +237,14 @@ impl Default for VarReg {
#[macro_export] #[macro_export]
macro_rules! temp_v { macro_rules! temp_v {
($x:expr) => { ($x:expr) => {
RegType::Temp($x) $crate::ast::RegType::Temp($x)
}; };
} }
#[macro_export] #[macro_export]
macro_rules! perm_v { macro_rules! perm_v {
($x:expr) => { ($x:expr) => {
RegType::Perm($x) $crate::ast::RegType::Perm($x)
}; };
} }

View File

@@ -14,7 +14,7 @@ macro_rules! is_not_eof {
($c:expr) => { ($c:expr) => {
match $c { match $c {
Ok(c) => c, Ok(c) => c,
Err(ParserError::UnexpectedEOF) => return Ok(true), Err($crate::ast::ParserError::UnexpectedEOF) => return Ok(true),
Err(e) => return Err(e), Err(e) => return Err(e),
} }
}; };
@@ -26,7 +26,7 @@ macro_rules! consume_chars_with {
match $e { match $e {
Ok(Some(c)) => $token.push(c), Ok(Some(c)) => $token.push(c),
Ok(None) => continue, Ok(None) => continue,
Err(ParserError::UnexpectedChar(..)) => break, Err($crate::ast::ParserError::UnexpectedChar(..)) => break,
Err(e) => return Err(e), Err(e) => return Err(e),
} }
} }

View File

@@ -1,106 +1,138 @@
#[macro_export] #[macro_export]
macro_rules! char_class { macro_rules! char_class {
($c: expr, [$head:expr]) => ($c == $head); ($c: expr, [$head:expr]) => ($c == $head);
($c: expr, [$head:expr $(, $cs:expr)+]) => ($c == $head || char_class!($c, [$($cs),*])); ($c: expr, [$head:expr $(, $cs:expr)+]) => ($c == $head || $crate::char_class!($c, [$($cs),*]));
} }
#[macro_export] #[macro_export]
macro_rules! symbolic_control_char { macro_rules! symbolic_control_char {
($c: expr) => (char_class!($c, ['a', 'b', 'f', 'n', 'r', 't', 'v', '0'])) ($c: expr) => {
$crate::char_class!($c, ['a', 'b', 'f', 'n', 'r', 't', 'v', '0'])
};
} }
#[macro_export] #[macro_export]
macro_rules! space_char { macro_rules! space_char {
($c: expr) => ($c == ' ') ($c: expr) => {
$c == ' '
};
} }
#[macro_export] #[macro_export]
macro_rules! layout_char { macro_rules! layout_char {
($c: expr) => (char_class!($c, [' ', '\n', '\t', '\u{0B}', '\u{0C}'])) ($c: expr) => {
$crate::char_class!($c, [' ', '\n', '\t', '\u{0B}', '\u{0C}'])
};
} }
#[macro_export] #[macro_export]
macro_rules! symbolic_hexadecimal_char { macro_rules! symbolic_hexadecimal_char {
($c: expr) => ($c == 'x') ($c: expr) => {
$c == 'x'
};
} }
#[macro_export] #[macro_export]
macro_rules! octal_digit_char { macro_rules! octal_digit_char {
($c: expr) => ($c >= '0' && $c <= '7') ($c: expr) => {
$c >= '0' && $c <= '7'
};
} }
#[macro_export] #[macro_export]
macro_rules! binary_digit_char { macro_rules! binary_digit_char {
($c: expr) => ($c >= '0' && $c <= '1') ($c: expr) => {
$c >= '0' && $c <= '1'
};
} }
#[macro_export] #[macro_export]
macro_rules! hexadecimal_digit_char { macro_rules! hexadecimal_digit_char {
($c: expr) => ($c >= '0' && $c <= '9' || ($c: expr) => {
$c >= 'A' && $c <= 'F' || $c >= '0' && $c <= '9' || $c >= 'A' && $c <= 'F' || $c >= 'a' && $c <= 'f'
$c >= 'a' && $c <= 'f') };
} }
#[macro_export] #[macro_export]
macro_rules! exponent_char { macro_rules! exponent_char {
($c: expr) => ($c == 'e' || $c == 'E') ($c: expr) => {
$c == 'e' || $c == 'E'
};
} }
#[macro_export] #[macro_export]
macro_rules! sign_char { macro_rules! sign_char {
($c: expr) => ($c == '-' || $c == '+') ($c: expr) => {
$c == '-' || $c == '+'
};
} }
#[macro_export] #[macro_export]
macro_rules! new_line_char { macro_rules! new_line_char {
($c: expr) => ($c == '\n') ($c: expr) => {
$c == '\n'
};
} }
#[macro_export] #[macro_export]
macro_rules! end_line_comment_char { macro_rules! end_line_comment_char {
($c: expr) => ($c == '%') ($c: expr) => {
$c == '%'
};
} }
#[macro_export] #[macro_export]
macro_rules! comment_1_char { macro_rules! comment_1_char {
($c: expr) => ($c == '/') ($c: expr) => {
$c == '/'
};
} }
#[macro_export] #[macro_export]
macro_rules! comment_2_char { macro_rules! comment_2_char {
($c: expr) => ($c == '*') ($c: expr) => {
$c == '*'
};
} }
#[macro_export] #[macro_export]
macro_rules! capital_letter_char { macro_rules! capital_letter_char {
($c: expr) => ($c >= 'A' && $c <= 'Z') ($c: expr) => {
$c >= 'A' && $c <= 'Z'
};
} }
#[macro_export] #[macro_export]
macro_rules! small_letter_char { macro_rules! small_letter_char {
($c: expr) => ($c >= 'a' && $c <= 'z') ($c: expr) => {
$c >= 'a' && $c <= 'z'
};
} }
#[macro_export] #[macro_export]
macro_rules! variable_indicator_char { macro_rules! variable_indicator_char {
($c: expr) => ($c == '_') ($c: expr) => {
$c == '_'
};
} }
#[macro_export] #[macro_export]
macro_rules! graphic_char { macro_rules! graphic_char {
($c: expr) => (char_class!($c, ['#', '$', '&', '*', '+', '-', '.', '/', ':', ($c: expr) => ($crate::char_class!($c, ['#', '$', '&', '*', '+', '-', '.', '/', ':',
'<', '=', '>', '?', '@', '^', '~'])) '<', '=', '>', '?', '@', '^', '~']))
} }
#[macro_export] #[macro_export]
macro_rules! graphic_token_char { macro_rules! graphic_token_char {
($c: expr) => (graphic_char!($c) || backslash_char!($c)) ($c: expr) => {
$crate::graphic_char!($c) || $crate::backslash_char!($c)
};
} }
#[macro_export] #[macro_export]
macro_rules! alpha_char { macro_rules! alpha_char {
($c: expr) => ($c: expr) => {
(match $c { match $c {
'a'..='z' => true, 'a'..='z' => true,
'A'..='Z' => true, 'A'..='Z' => true,
'_' => true, '_' => true,
@@ -120,68 +152,95 @@ macro_rules! alpha_char {
'\u{0590}'..='\u{05FF}' => true, // Hebrew '\u{0590}'..='\u{05FF}' => true, // Hebrew
'\u{0600}'..='\u{06FF}' => true, // Arabic '\u{0600}'..='\u{06FF}' => true, // Arabic
'\u{0700}'..='\u{074F}' => true, // Syriac '\u{0700}'..='\u{074F}' => true, // Syriac
_ => false _ => false,
}) }
};
} }
#[macro_export] #[macro_export]
macro_rules! decimal_digit_char { macro_rules! decimal_digit_char {
($c: expr) => ($c >= '0' && $c <= '9') ($c: expr) => {
$c >= '0' && $c <= '9'
};
} }
#[macro_export] #[macro_export]
macro_rules! decimal_point_char { macro_rules! decimal_point_char {
($c: expr) => ($c == '.') ($c: expr) => {
$c == '.'
};
} }
#[macro_export] #[macro_export]
macro_rules! alpha_numeric_char { macro_rules! alpha_numeric_char {
($c: expr) => (alpha_char!($c) || decimal_digit_char!($c)) ($c: expr) => {
$crate::alpha_char!($c) || $crate::decimal_digit_char!($c)
};
} }
#[macro_export] #[macro_export]
macro_rules! cut_char { macro_rules! cut_char {
($c: expr) => ($c == '!') ($c: expr) => {
$c == '!'
};
} }
#[macro_export] #[macro_export]
macro_rules! semicolon_char { macro_rules! semicolon_char {
($c: expr) => ($c == ';') ($c: expr) => {
$c == ';'
};
} }
#[macro_export] #[macro_export]
macro_rules! backslash_char { macro_rules! backslash_char {
($c: expr) => ($c == '\\') ($c: expr) => {
$c == '\\'
};
} }
#[macro_export] #[macro_export]
macro_rules! single_quote_char { macro_rules! single_quote_char {
($c: expr) => ($c == '\'') ($c: expr) => {
$c == '\''
};
} }
#[macro_export] #[macro_export]
macro_rules! double_quote_char { macro_rules! double_quote_char {
($c: expr) => ($c == '"') ($c: expr) => {
$c == '"'
};
} }
#[macro_export] #[macro_export]
macro_rules! back_quote_char { macro_rules! back_quote_char {
($c: expr) => ($c == '`') ($c: expr) => {
$c == '`'
};
} }
#[macro_export] #[macro_export]
macro_rules! meta_char { macro_rules! meta_char {
($c: expr) => ( char_class!($c, ['\\', '\'', '"', '`']) ) ($c: expr) => {
$crate::char_class!($c, ['\\', '\'', '"', '`'])
};
} }
#[macro_export] #[macro_export]
macro_rules! solo_char { macro_rules! solo_char {
($c: expr) => ( char_class!($c, ['!', '(', ')', ',', ';', '[', ']', ($c: expr) => {
'{', '}', '|', '%']) ) $crate::char_class!($c, ['!', '(', ')', ',', ';', '[', ']', '{', '}', '|', '%'])
};
} }
#[macro_export] #[macro_export]
macro_rules! prolog_char { macro_rules! prolog_char {
($c: expr) => (graphic_char!($c) || alpha_numeric_char!($c) || solo_char!($c) || ($c: expr) => {
layout_char!($c) || meta_char!($c)) $crate::graphic_char!($c)
|| $crate::alpha_numeric_char!($c)
|| $crate::solo_char!($c)
|| $crate::layout_char!($c)
|| $crate::meta_char!($c)
};
} }

View File

@@ -4,11 +4,11 @@ use std::collections::HashSet;
use std::fmt; use std::fmt;
use std::hash::{Hash, Hasher}; use std::hash::{Hash, Hasher};
use std::ops::Deref; use std::ops::Deref;
use std::rc::{Rc}; use std::rc::Rc;
pub struct TabledData<T> { pub struct TabledData<T> {
table: Rc<RefCell<HashSet<Rc<T>>>>, table: Rc<RefCell<HashSet<Rc<T>>>>,
pub(crate) module_name: Rc<String> pub(crate) module_name: Rc<String>,
} }
impl<T: Hash + Eq + fmt::Debug> fmt::Debug for TabledData<T> { impl<T: Hash + Eq + fmt::Debug> fmt::Debug for TabledData<T> {
@@ -22,14 +22,15 @@ impl<T: Hash + Eq + fmt::Debug> fmt::Debug for TabledData<T> {
impl<T> Clone for TabledData<T> { impl<T> Clone for TabledData<T> {
fn clone(&self) -> Self { fn clone(&self) -> Self {
TabledData { table: self.table.clone(), TabledData {
module_name: self.module_name.clone() } table: self.table.clone(),
module_name: self.module_name.clone(),
}
} }
} }
impl<T: PartialEq> PartialEq for TabledData<T> { impl<T: PartialEq> PartialEq for TabledData<T> {
fn eq(&self, other: &TabledData<T>) -> bool fn eq(&self, other: &TabledData<T>) -> bool {
{
Rc::ptr_eq(&self.table, &other.table) && self.module_name == other.module_name Rc::ptr_eq(&self.table, &other.table) && self.module_name == other.module_name
} }
} }
@@ -39,7 +40,7 @@ impl<T: Hash + Eq> TabledData<T> {
pub fn new(module_name: Rc<String>) -> Self { pub fn new(module_name: Rc<String>) -> Self {
TabledData { TabledData {
table: Rc::new(RefCell::new(HashSet::new())), table: Rc::new(RefCell::new(HashSet::new())),
module_name module_name,
} }
} }
@@ -51,7 +52,7 @@ impl<T: Hash + Eq> TabledData<T> {
pub struct TabledRc<T: Hash + Eq> { pub struct TabledRc<T: Hash + Eq> {
pub(crate) atom: Rc<T>, pub(crate) atom: Rc<T>,
pub table: TabledData<T> pub table: TabledData<T>,
} }
impl<T: Hash + Eq + fmt::Debug> fmt::Debug for TabledRc<T> { impl<T: Hash + Eq + fmt::Debug> fmt::Debug for TabledRc<T> {
@@ -67,27 +68,27 @@ impl<T: Hash + Eq + fmt::Debug> fmt::Debug for TabledRc<T> {
// from complaining when deriving Clone for StringList. // from complaining when deriving Clone for StringList.
impl<T: Hash + Eq> Clone for TabledRc<T> { impl<T: Hash + Eq> Clone for TabledRc<T> {
fn clone(&self) -> Self { fn clone(&self) -> Self {
TabledRc { atom: self.atom.clone(), table: self.table.clone() } TabledRc {
atom: self.atom.clone(),
table: self.table.clone(),
}
} }
} }
impl<T: Ord + Hash + Eq> PartialOrd for TabledRc<T> { impl<T: Ord + Hash + Eq> PartialOrd for TabledRc<T> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
{
Some(self.atom.cmp(&other.atom)) Some(self.atom.cmp(&other.atom))
} }
} }
impl<T: Ord + Hash + Eq> Ord for TabledRc<T> { impl<T: Ord + Hash + Eq> Ord for TabledRc<T> {
fn cmp(&self, other: &Self) -> Ordering fn cmp(&self, other: &Self) -> Ordering {
{
self.atom.cmp(&other.atom) self.atom.cmp(&other.atom)
} }
} }
impl<T: Hash + Eq> PartialEq for TabledRc<T> { impl<T: Hash + Eq> PartialEq for TabledRc<T> {
fn eq(&self, other: &TabledRc<T>) -> bool fn eq(&self, other: &TabledRc<T>) -> bool {
{
self.atom == other.atom self.atom == other.atom
} }
} }
@@ -104,7 +105,7 @@ impl<T: Hash + Eq + ToString> TabledRc<T> {
pub fn new(atom: T, table: TabledData<T>) -> Self { pub fn new(atom: T, table: TabledData<T>) -> Self {
let atom = match table.borrow_mut().take(&atom) { let atom = match table.borrow_mut().take(&atom) {
Some(atom) => atom.clone(), Some(atom) => atom.clone(),
None => Rc::new(atom) None => Rc::new(atom),
}; };
table.borrow_mut().insert(atom.clone()); table.borrow_mut().insert(atom.clone());
@@ -147,7 +148,7 @@ impl<T: Hash + Eq + fmt::Display> fmt::Display for TabledRc<T> {
#[macro_export] #[macro_export]
macro_rules! tabled_rc { macro_rules! tabled_rc {
($e:expr, $tbl:expr) => ( ($e:expr, $tbl:expr) => {
TabledRc::new(String::from($e), $tbl.clone()) $crate::tabled_rc::TabledRc::new(String::from($e), $tbl.clone())
) };
} }

View File

@@ -1,9 +1,9 @@
use prolog_parser_rebis::ast::*; use prolog_parser_rebis::ast::*;
use prolog_parser_rebis::{ use prolog_parser_rebis::{
alpha_char, alpha_numeric_char, backslash_char, capital_letter_char, char_class, clause_name, alpha_numeric_char, capital_letter_char, clause_name, cut_char, decimal_digit_char,
cut_char, decimal_digit_char, graphic_char, graphic_token_char, is_fx, is_infix, is_postfix, graphic_token_char, is_fx, is_infix, is_postfix, is_prefix, is_xf, is_xfx, is_xfy, is_yfx,
is_prefix, is_xf, is_xfx, is_xfy, is_yfx, semicolon_char, sign_char, single_quote_char, semicolon_char, sign_char, single_quote_char, small_letter_char, solo_char,
small_letter_char, solo_char, variable_indicator_char, variable_indicator_char,
}; };
use crate::clause_types::*; use crate::clause_types::*;

View File

@@ -1,11 +1,10 @@
use prolog_parser_rebis::ast::*; use prolog_parser_rebis::ast::*;
use prolog_parser_rebis::parser::*; use prolog_parser_rebis::parser::*;
use prolog_parser_rebis::tabled_rc::*;
use prolog_parser_rebis::{ use prolog_parser_rebis::{
alpha_char, alpha_numeric_char, backslash_char, binary_digit_char, char_class, clause_name, alpha_char, binary_digit_char, clause_name, decimal_digit_char, exponent_char, graphic_char,
decimal_digit_char, exponent_char, graphic_char, graphic_token_char, hexadecimal_digit_char, graphic_token_char, hexadecimal_digit_char, layout_char, meta_char, new_line_char,
layout_char, meta_char, new_line_char, octal_digit_char, prolog_char, sign_char, solo_char, octal_digit_char, prolog_char, sign_char, solo_char, symbolic_control_char,
symbolic_control_char, symbolic_hexadecimal_char, temp_v, symbolic_hexadecimal_char, temp_v,
}; };
use lazy_static::lazy_static; use lazy_static::lazy_static;