2025-04-05 17:37:13 +03:00
|
|
|
use std::collections::HashMap;
|
2025-04-07 15:40:38 +03:00
|
|
|
use crate::mtgott::charclasses::{is_bad_name, is_digit, is_illegal_name, is_lnspace, is_normal_word_constituent, is_whitespace};
|
|
|
|
|
use std::fmt::{self, Display, Formatter};
|
|
|
|
|
use std::error::Error;
|
2025-04-05 17:37:13 +03:00
|
|
|
|
2025-04-05 23:22:07 +03:00
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
2025-04-05 17:37:13 +03:00
|
|
|
pub enum Expression {
|
|
|
|
|
Root,
|
|
|
|
|
Argument(u64),
|
|
|
|
|
Get(Box<Expression>, Box<Expression>),
|
|
|
|
|
Attribute(Box<Expression>, String),
|
|
|
|
|
Call(Box<Expression>, Vec<Expression>),
|
|
|
|
|
Int(u64),
|
|
|
|
|
None,
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-05 23:22:07 +03:00
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
2025-04-05 17:37:13 +03:00
|
|
|
pub struct IfSubElement {
|
2025-04-05 23:22:07 +03:00
|
|
|
pub branches: Vec<Element>,
|
|
|
|
|
pub conditions: Vec<Expression>,
|
2025-04-05 17:37:13 +03:00
|
|
|
}
|
|
|
|
|
|
2025-04-05 23:22:07 +03:00
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
2025-04-05 17:37:13 +03:00
|
|
|
pub struct ForSubElement {
|
2025-04-05 23:22:07 +03:00
|
|
|
pub iterable: Expression,
|
|
|
|
|
pub core: Element,
|
|
|
|
|
// Either "\n", " " or ""
|
|
|
|
|
pub join: String,
|
2025-04-05 17:37:13 +03:00
|
|
|
}
|
|
|
|
|
|
2025-04-05 23:22:07 +03:00
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
|
|
|
|
pub enum SubElement {
|
2025-04-05 17:37:13 +03:00
|
|
|
Static(String),
|
2025-04-05 23:22:07 +03:00
|
|
|
// ======== Other are dynamic ========
|
2025-04-05 17:37:13 +03:00
|
|
|
If(IfSubElement),
|
2025-04-05 23:22:07 +03:00
|
|
|
// Both for {{}} and {[]}
|
2025-04-05 17:37:13 +03:00
|
|
|
InsertExpr(Expression),
|
|
|
|
|
For(ForSubElement),
|
|
|
|
|
Let(Expression, Element),
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-05 23:22:07 +03:00
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
2025-04-05 17:37:13 +03:00
|
|
|
pub struct Element {
|
2025-04-05 23:22:07 +03:00
|
|
|
pub argc: usize,
|
|
|
|
|
pub sub_elements: Vec<SubElement>,
|
2025-04-05 17:37:13 +03:00
|
|
|
}
|
|
|
|
|
|
2025-04-05 23:22:07 +03:00
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
2025-04-05 17:37:13 +03:00
|
|
|
pub enum Plemege {
|
|
|
|
|
Element(Element),
|
2025-04-05 23:22:07 +03:00
|
|
|
Package(HashMap<String, Plemege>),
|
2025-04-05 17:37:13 +03:00
|
|
|
}
|
|
|
|
|
|
2025-04-05 23:22:07 +03:00
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
2025-04-05 17:37:13 +03:00
|
|
|
pub enum FileParsingErrorKind {
|
|
|
|
|
expected_pack_opening_or_element_opening_or_pack_ending,
|
|
|
|
|
expected_pack_opening_or_element_opening_or_eof,
|
|
|
|
|
unmatched_pack_ending_tag,
|
|
|
|
|
expected_pack_name,
|
|
|
|
|
illegal_pack_name,
|
|
|
|
|
pack_member_name_already_occupied,
|
|
|
|
|
expected_pack_opening_tag_end,
|
|
|
|
|
expected_element_name,
|
2025-04-05 23:22:07 +03:00
|
|
|
incorrect_block_ending_tag_expected_end_element,
|
2025-04-05 17:37:13 +03:00
|
|
|
illegal_element_name,
|
|
|
|
|
expected_argument_name_or_eldef_opening_tag_end,
|
|
|
|
|
illegal_argument_name,
|
|
|
|
|
repeated_argument_name,
|
|
|
|
|
expected_command_name,
|
|
|
|
|
incorrect_block_ending_tag_expected_normal,
|
|
|
|
|
expected_write_tag_end_after_expression,
|
|
|
|
|
expected_roughinsert_tag_end_after_expression,
|
|
|
|
|
illegal_command_name,
|
|
|
|
|
expected_cmd_tag_end,
|
|
|
|
|
expected_variable_name,
|
|
|
|
|
illegal_variable_name,
|
|
|
|
|
expected_assignment_operator,
|
|
|
|
|
expected_cmd_tag_end_after_expression,
|
|
|
|
|
expected_comma_or_colon,
|
|
|
|
|
expected_colon,
|
|
|
|
|
forloop_variable_cant_take_occupied_name,
|
|
|
|
|
incorrect_block_ending_tag_expected_normal_or_lf_gap_nogap_or_forloop,
|
|
|
|
|
incorrect_block_ending_tag_expected_normal_or_endif_or_else_or_else_if,
|
|
|
|
|
incorrect_block_ending_tag_expected_normal_or_endif,
|
|
|
|
|
expected_nonempty_expression,
|
|
|
|
|
expected_closing_round_bracket,
|
|
|
|
|
cant_start_word_immediately_after_digit,
|
|
|
|
|
integer_parsing_error,
|
|
|
|
|
illegal_object_name,
|
|
|
|
|
expected_attribute_name_after_dot,
|
|
|
|
|
illegal_attribute_name,
|
|
|
|
|
expected_closing_square_bracket,
|
|
|
|
|
empty_expression_inside_round_brackets,
|
|
|
|
|
empty_expression_inside_square_brackets,
|
2025-04-07 15:40:38 +03:00
|
|
|
unmatched_element_ending_tag,
|
|
|
|
|
unmatched_magic_block_ending_tag,
|
|
|
|
|
recursion_limit_exceeded,
|
2025-04-05 17:37:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
use FileParsingErrorKind::*;
|
|
|
|
|
|
2025-04-05 23:22:07 +03:00
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
2025-04-05 17:37:13 +03:00
|
|
|
pub struct FileParsingError {
|
2025-04-05 23:22:07 +03:00
|
|
|
pub kind: FileParsingErrorKind,
|
|
|
|
|
pub p1: usize,
|
|
|
|
|
pub p2: usize,
|
2025-04-05 17:37:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl FileParsingError {
|
|
|
|
|
fn new(kind: FileParsingErrorKind, p1: usize, p2: usize) -> Self {
|
|
|
|
|
Self{kind, p1, p2}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-07 15:40:38 +03:00
|
|
|
impl Display for FileParsingErrorKind {
|
|
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
|
|
|
|
write!(f, "{:?}", self)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Display for FileParsingError {
|
|
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
|
|
|
|
write!(f, "FileParsingError: {} at positions {} to {}", self.kind, self.p1, self.p2)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Error for FileParsingError {}
|
|
|
|
|
|
2025-04-05 17:37:13 +03:00
|
|
|
struct Parser<'a> {
|
|
|
|
|
text: &'a str,
|
|
|
|
|
p: usize
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a> Parser<'a> {
|
|
|
|
|
fn here(&self)->Option<char> {
|
|
|
|
|
self.text[self.p..].chars().next()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn is_ahead(&self, substr: &str)->bool {
|
|
|
|
|
self.text[self.p..].starts_with(substr)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn is_char_ahead(&self, ch: char) -> bool {
|
|
|
|
|
match self.here() {
|
|
|
|
|
Some(cha) => cha == ch,
|
|
|
|
|
None => false,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn is_digit_ahead(&self) -> bool {
|
|
|
|
|
match self.here() {
|
|
|
|
|
Some(ch) => is_digit(ch),
|
|
|
|
|
None => false,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn is_word_ahead(&self) -> bool {
|
|
|
|
|
match self.here() {
|
|
|
|
|
Some(ch) => is_normal_word_constituent(ch),
|
|
|
|
|
None => false,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn advance(&mut self) {
|
2025-04-05 23:22:07 +03:00
|
|
|
self.p += self.text[self.p..].chars().next().unwrap().len_utf8();
|
2025-04-05 17:37:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn skip_whitespace(&mut self) {
|
|
|
|
|
loop {
|
|
|
|
|
match self.here() {
|
|
|
|
|
Some(ch ) => if !is_whitespace(ch) {
|
|
|
|
|
break
|
|
|
|
|
} else { self.advance(); }
|
|
|
|
|
None => break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn skip_normal_word(&mut self){
|
|
|
|
|
loop {
|
|
|
|
|
match self.here() {
|
|
|
|
|
Some(ch ) => if !is_normal_word_constituent(ch) {
|
|
|
|
|
break
|
|
|
|
|
} else { self.advance(); }
|
|
|
|
|
None => break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn new_unexpected_char_error(&self, kind: FileParsingErrorKind) -> FileParsingError {
|
|
|
|
|
match self.text[self.p..].char_indices().next() {
|
|
|
|
|
Some((off, _)) => FileParsingError::new(kind, self.p, self.p + off),
|
|
|
|
|
None => FileParsingError::new(kind, self.p, self.p),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn parse_pack_plus_ending(&mut self, top: bool) -> Result<Plemege, FileParsingError> {
|
|
|
|
|
let mut res: HashMap<String, Plemege> = HashMap::new();
|
|
|
|
|
loop {
|
|
|
|
|
self.skip_whitespace();
|
|
|
|
|
if self.p == self.text.len() {
|
|
|
|
|
return if top {
|
2025-04-05 23:22:07 +03:00
|
|
|
Ok(Plemege::Package(res))
|
2025-04-05 17:37:13 +03:00
|
|
|
} else {
|
|
|
|
|
Err(self.new_unexpected_char_error(expected_pack_opening_or_element_opening_or_pack_ending))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if self.is_ahead("{$}") {
|
|
|
|
|
return if top {
|
|
|
|
|
Err(FileParsingError::new(unmatched_pack_ending_tag, self.p, self.p + 3))
|
|
|
|
|
} else {
|
|
|
|
|
self.p += 3;
|
2025-04-05 23:22:07 +03:00
|
|
|
Ok(Plemege::Package(res))
|
2025-04-05 17:37:13 +03:00
|
|
|
};
|
|
|
|
|
} else if self.is_ahead("{$") {
|
|
|
|
|
self.p += 2;
|
|
|
|
|
self.skip_whitespace();
|
|
|
|
|
let p1 = self.p;
|
|
|
|
|
self.skip_normal_word();
|
|
|
|
|
if self.p == p1 {
|
|
|
|
|
return Err(self.new_unexpected_char_error(expected_pack_name))
|
|
|
|
|
}
|
|
|
|
|
let child_name: &str = &self.text[p1..self.p];
|
|
|
|
|
if !is_bad_name(child_name) {
|
|
|
|
|
return Err(FileParsingError::new(illegal_pack_name, p1, self.p))
|
|
|
|
|
}
|
|
|
|
|
if let Some(_) = res.get(child_name) {
|
|
|
|
|
return Err(FileParsingError::new(pack_member_name_already_occupied, p1, self.p))
|
|
|
|
|
}
|
|
|
|
|
self.skip_normal_word();
|
|
|
|
|
if !self.is_ahead("$}") {
|
|
|
|
|
return Err(self.new_unexpected_char_error(expected_pack_opening_tag_end))
|
|
|
|
|
}
|
|
|
|
|
self.p += 2;
|
|
|
|
|
res.insert(String::from(child_name), self.parse_pack_plus_ending(false)?);
|
|
|
|
|
} else if self.is_ahead("{@") {
|
|
|
|
|
self.p += 2;
|
|
|
|
|
self.skip_whitespace();
|
|
|
|
|
let p1 = self.p;
|
|
|
|
|
self.skip_normal_word();
|
|
|
|
|
if p1 == self.p {
|
|
|
|
|
return Err(FileParsingError::new(expected_element_name, p1, self.p))
|
|
|
|
|
}
|
|
|
|
|
let child_name = &self.text[p1..self.p];
|
|
|
|
|
if is_bad_name(child_name) {
|
|
|
|
|
return Err(FileParsingError::new(illegal_element_name, p1, self.p))
|
|
|
|
|
}
|
|
|
|
|
if let Some(_) = res.get(child_name) {
|
|
|
|
|
return Err(FileParsingError::new(pack_member_name_already_occupied, p1, self.p))
|
|
|
|
|
}
|
|
|
|
|
let mut arg_names: Vec<&str> = Vec::new();
|
|
|
|
|
loop {
|
|
|
|
|
self.skip_whitespace();
|
|
|
|
|
if self.is_ahead("@}") {
|
|
|
|
|
self.p += 2;
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
let p1 = self.p;
|
|
|
|
|
self.skip_normal_word();
|
|
|
|
|
if p1 == self.p {
|
|
|
|
|
return Err(FileParsingError::new(expected_argument_name_or_eldef_opening_tag_end, p1, self.p))
|
|
|
|
|
}
|
|
|
|
|
let arg_name: &str = &self.text[p1..self.p];
|
|
|
|
|
if is_bad_name(arg_name) {
|
|
|
|
|
return Err(FileParsingError::new(illegal_argument_name, p1, self.p))
|
|
|
|
|
}
|
|
|
|
|
if arg_names.iter().any(|b: &&str| *b == arg_name) {
|
|
|
|
|
return Err(FileParsingError::new(repeated_argument_name, p1, self.p))
|
|
|
|
|
}
|
|
|
|
|
arg_names.push(arg_name);
|
|
|
|
|
}
|
|
|
|
|
let (child_el, end_cmd): (Element, ReasonOfElementEnd) = self.parse_element_plus_ending_tag(&arg_names)?;
|
2025-04-05 23:22:07 +03:00
|
|
|
if !matches!(end_cmd.cmd, BlockEndingTag::END_ELEMENT) {
|
|
|
|
|
return Err(FileParsingError::new(incorrect_block_ending_tag_expected_end_element, end_cmd.p1, self.p))
|
2025-04-05 17:37:13 +03:00
|
|
|
}
|
|
|
|
|
res.insert(String::from(child_name), Plemege::Element(child_el));
|
|
|
|
|
} else {
|
|
|
|
|
return Err(self.new_unexpected_char_error(if top {
|
|
|
|
|
expected_pack_opening_or_element_opening_or_eof
|
|
|
|
|
} else {
|
|
|
|
|
expected_pack_opening_or_element_opening_or_pack_ending
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-05 23:22:07 +03:00
|
|
|
enum BlockEndingTag {
|
2025-04-07 15:40:38 +03:00
|
|
|
EOF,
|
2025-04-05 23:22:07 +03:00
|
|
|
/* This is {@} */
|
|
|
|
|
END_ELEMENT,
|
|
|
|
|
/* These tags are command tags */
|
|
|
|
|
/* {%} */
|
2025-04-05 17:37:13 +03:00
|
|
|
NORMAL,
|
|
|
|
|
LF,
|
|
|
|
|
GAP,
|
|
|
|
|
NOGAP,
|
|
|
|
|
ENDLOOP,
|
2025-04-05 23:22:07 +03:00
|
|
|
/* Incomplete tag (ony `{% else if` is included */
|
2025-04-05 17:37:13 +03:00
|
|
|
ELSE_IF,
|
|
|
|
|
ELSE,
|
|
|
|
|
ENDIF
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct ReasonOfElementEnd {
|
|
|
|
|
p1: usize,
|
2025-04-05 23:22:07 +03:00
|
|
|
cmd: BlockEndingTag,
|
2025-04-05 17:37:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn fix_whitespaces_in_element(subels: &mut Vec<SubElement>) {
|
|
|
|
|
let n = subels.len();
|
|
|
|
|
if n > 0 {
|
|
|
|
|
match &mut subels[0] {
|
|
|
|
|
SubElement::Static(org) => {
|
|
|
|
|
let mut ta = 0;
|
|
|
|
|
for p in 0..org.len(){
|
|
|
|
|
if !is_whitespace(org.as_bytes()[p] as char) {
|
|
|
|
|
ta = p; break
|
|
|
|
|
}
|
|
|
|
|
if org.as_bytes()[p] == b'\n' {
|
|
|
|
|
ta = p + 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
*org = String::from(&org[ta..]);
|
|
|
|
|
},
|
|
|
|
|
_ => {},
|
|
|
|
|
}
|
|
|
|
|
match &mut subels[n - 1] {
|
|
|
|
|
SubElement::Static(org) => {
|
|
|
|
|
while let Some(ch) = org.chars().last() {
|
|
|
|
|
if is_whitespace(ch) { org.pop(); } else { break }
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
_ => {},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
let mut min_offset = usize::MAX;
|
|
|
|
|
for i in 0..subels.len() {
|
|
|
|
|
match &mut subels[i] {
|
|
|
|
|
SubElement::Static(org) => {
|
|
|
|
|
let mut seen_online = i > 0;
|
|
|
|
|
let mut line_bg: usize = 0;
|
|
|
|
|
for p in 0..org.len() {
|
|
|
|
|
let ch = org.as_bytes()[p] as char;
|
|
|
|
|
if !is_whitespace(ch) {
|
|
|
|
|
if !seen_online {
|
|
|
|
|
seen_online = true;
|
|
|
|
|
min_offset = std::cmp::min(min_offset, p - line_bg)
|
|
|
|
|
}
|
|
|
|
|
} else if ch == '\n' {
|
|
|
|
|
line_bg = p + 1;
|
|
|
|
|
seen_online = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/* This won't cause issues on the .last() because we previously rstripped the last part */
|
|
|
|
|
if !seen_online{
|
|
|
|
|
min_offset = std::cmp::min(min_offset, org.len() - line_bg)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
_ => {},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for i in 0..subels.len() {
|
|
|
|
|
match &mut subels[i] {
|
|
|
|
|
SubElement::Static(org) => {
|
|
|
|
|
let mut res: Vec<u8> = Vec::new();
|
|
|
|
|
let mut should_ignore_gap = i > 0;
|
|
|
|
|
let mut line_bg: usize = 0;
|
|
|
|
|
for p in 0..org.len() {
|
|
|
|
|
let ch = org.as_bytes()[p];
|
|
|
|
|
if ch == b'\n' {
|
|
|
|
|
line_bg = p + 1;
|
|
|
|
|
should_ignore_gap = false;
|
|
|
|
|
/* We handle trailing whitespaces case here */
|
|
|
|
|
while let Some(&ch) = res.last() {
|
|
|
|
|
if is_lnspace(ch as char) { res. pop(); } else { break }
|
|
|
|
|
}
|
|
|
|
|
} else if p - line_bg < min_offset {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
res.push(ch);
|
|
|
|
|
}
|
|
|
|
|
*org = String::from_utf8(res).unwrap();
|
|
|
|
|
},
|
|
|
|
|
_ => {},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a> Parser<'a> {
|
|
|
|
|
/* If BlockEndingCmdTag::ELSE_IF is returned, the ending tag won't be read completely,
|
|
|
|
|
* But in other case it would be read to the end */
|
|
|
|
|
fn parse_element_plus_ending_tag(&mut self, arg_names: &Vec<&str>) -> Result<(Element, ReasonOfElementEnd), FileParsingError> {
|
|
|
|
|
let mut res: Vec<SubElement> = Vec::new();
|
|
|
|
|
let mut tp1 = self.p;
|
|
|
|
|
|
|
|
|
|
let fin_static = |p: &Parser, tp1: usize, res: &mut Vec<SubElement>| {
|
|
|
|
|
if tp1 < p.p {
|
|
|
|
|
res.push(SubElement::Static(String::from(&self.text[tp1..p.p])))
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/* Fixes whitespaces in static sub-elements */
|
|
|
|
|
let finishing_touches = |ree: ReasonOfElementEnd, mut res: Vec<SubElement>| -> Result<(Element, ReasonOfElementEnd), FileParsingError> {
|
|
|
|
|
fix_whitespaces_in_element(&mut res);
|
|
|
|
|
Ok((Element{ argc: arg_names.len(), sub_elements: res }, ree))
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
loop {
|
2025-04-07 15:40:38 +03:00
|
|
|
if self.p == self.text.len() {
|
|
|
|
|
fin_static(self, tp1, &mut res);
|
|
|
|
|
return finishing_touches(ReasonOfElementEnd{p1: self.p, cmd: BlockEndingTag::EOF}, res);
|
|
|
|
|
} else if self.is_ahead("{{") {
|
2025-04-05 17:37:13 +03:00
|
|
|
fin_static(self, tp1, &mut res);
|
|
|
|
|
self.p += 2;
|
|
|
|
|
let expr: Expression = self.parse_expression(arg_names)?;
|
|
|
|
|
if !self.is_ahead("}}") {
|
|
|
|
|
return Err(FileParsingError::new(expected_write_tag_end_after_expression, self.p - 2, self.p));
|
|
|
|
|
}
|
|
|
|
|
self.p += 2;
|
|
|
|
|
if !matches!(expr, Expression::None){
|
|
|
|
|
res.push(SubElement::InsertExpr(
|
|
|
|
|
Expression::Call(
|
|
|
|
|
Box::new(Expression::Attribute(
|
|
|
|
|
Box::new(Expression::Root), String::from("sanitize")
|
|
|
|
|
)),
|
|
|
|
|
vec![expr])
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
tp1 = self.p;
|
|
|
|
|
} else if self.is_ahead("{[") {
|
|
|
|
|
fin_static(self, tp1, &mut res);
|
|
|
|
|
self.p += 2;
|
|
|
|
|
let expr: Expression = self.parse_expression(arg_names)?;
|
|
|
|
|
if !self.is_ahead("]}") {
|
|
|
|
|
return Err(FileParsingError::new(expected_roughinsert_tag_end_after_expression, self.p - 2, self.p))
|
|
|
|
|
}
|
|
|
|
|
self.p += 2;
|
|
|
|
|
if !matches!(expr, Expression::None){
|
|
|
|
|
res.push(SubElement::InsertExpr(expr));
|
|
|
|
|
}
|
|
|
|
|
tp1 = self.p;
|
2025-04-05 23:22:07 +03:00
|
|
|
} else if self.is_ahead("{@}") {
|
|
|
|
|
fin_static(self, tp1, &mut res);
|
|
|
|
|
self.p += 3;
|
|
|
|
|
return finishing_touches(ReasonOfElementEnd{p1: self.p - 3, cmd: BlockEndingTag::END_ELEMENT}, res);
|
2025-04-05 17:37:13 +03:00
|
|
|
} else if self.is_ahead("{%}") {
|
|
|
|
|
fin_static(self, tp1, &mut res);
|
|
|
|
|
self.p += 3;
|
2025-04-05 23:22:07 +03:00
|
|
|
return finishing_touches(ReasonOfElementEnd{p1: self.p - 3, cmd: BlockEndingTag::NORMAL}, res);
|
2025-04-05 17:37:13 +03:00
|
|
|
} else if self.is_ahead("{%") {
|
|
|
|
|
fin_static(self, tp1, &mut res);
|
|
|
|
|
/* Might be needed if this is the ENDING cmd tag */
|
|
|
|
|
let p1 = self.p;
|
|
|
|
|
self.p += 2;
|
|
|
|
|
self.skip_whitespace();
|
|
|
|
|
let pb = self.p;
|
|
|
|
|
self.skip_normal_word();
|
|
|
|
|
if pb == self.p {
|
|
|
|
|
return Err(self.new_unexpected_char_error(expected_command_name))
|
|
|
|
|
}
|
|
|
|
|
let cmd = &self.text[pb..self.p];
|
|
|
|
|
|
|
|
|
|
/* Read space + expect %} and do finishing_touches */
|
2025-04-05 23:22:07 +03:00
|
|
|
let just_one_thing = |pelf: &mut Parser, cmd: BlockEndingTag, res: Vec<SubElement>| -> Result<(Element, ReasonOfElementEnd), FileParsingError> {
|
2025-04-05 17:37:13 +03:00
|
|
|
pelf.skip_whitespace();
|
|
|
|
|
if !pelf.is_ahead("%}") {
|
|
|
|
|
return Err(pelf.new_unexpected_char_error(expected_cmd_tag_end));
|
|
|
|
|
}
|
|
|
|
|
pelf.p += 2;
|
|
|
|
|
finishing_touches(ReasonOfElementEnd{p1, cmd}, res)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
match cmd {
|
2025-04-05 23:22:07 +03:00
|
|
|
"lf" => return just_one_thing(self, BlockEndingTag::LF, res),
|
|
|
|
|
"gap" => return just_one_thing(self, BlockEndingTag::GAP, res),
|
|
|
|
|
"nogap" => return just_one_thing(self, BlockEndingTag::NOGAP, res),
|
2025-04-05 17:37:13 +03:00
|
|
|
"else" => {
|
|
|
|
|
self.skip_whitespace();
|
|
|
|
|
let ps = self.p;
|
|
|
|
|
self.skip_normal_word();
|
|
|
|
|
if ps == self.p {
|
2025-04-05 23:22:07 +03:00
|
|
|
return just_one_thing(self, BlockEndingTag::ELSE, res)
|
2025-04-05 17:37:13 +03:00
|
|
|
} else if &self.text[ps..self.p] != "if" {
|
|
|
|
|
return Err(FileParsingError::new(illegal_command_name, pb, self.p))
|
|
|
|
|
}
|
2025-04-05 23:22:07 +03:00
|
|
|
return finishing_touches(ReasonOfElementEnd{p1, cmd: BlockEndingTag::ELSE_IF}, res);
|
2025-04-05 17:37:13 +03:00
|
|
|
}
|
2025-04-05 23:22:07 +03:00
|
|
|
"endif" => return just_one_thing(self, BlockEndingTag::ENDIF, res),
|
|
|
|
|
"endloop" => return just_one_thing(self, BlockEndingTag::ENDLOOP, res),
|
2025-04-05 17:37:13 +03:00
|
|
|
"for" => res.push(self.parse_let(arg_names)?),
|
|
|
|
|
"if" => res.push(self.parse_if(arg_names)?),
|
|
|
|
|
"let" => res.push(self.parse_let(arg_names)?),
|
|
|
|
|
_ => return Err(FileParsingError::new(illegal_command_name, pb, self.p)),
|
|
|
|
|
}
|
|
|
|
|
tp1 = self.p;
|
|
|
|
|
} else {
|
|
|
|
|
self.advance();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn parse_expression_at_cmd_tag_end(&mut self, arg_names: &Vec<&str>) -> Result<Expression, FileParsingError> {
|
|
|
|
|
let p1 = self.p;
|
|
|
|
|
let expr: Expression = self.parse_expression(arg_names)?;
|
|
|
|
|
if matches!(expr, Expression::None) {
|
|
|
|
|
return Err(FileParsingError::new(expected_nonempty_expression, p1, self.p))
|
|
|
|
|
}
|
|
|
|
|
if !self.is_ahead("%}"){
|
|
|
|
|
return Err(self.new_unexpected_char_error(expected_cmd_tag_end_after_expression));
|
|
|
|
|
}
|
|
|
|
|
Ok(expr)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* It turned out to be so complex I put it in a separate function.
|
|
|
|
|
* It parses expr %} block {% else if expr %} block {% else %} block {%} */
|
|
|
|
|
fn parse_if(&mut self, arg_names: &Vec<&str>) -> Result<SubElement, FileParsingError> {
|
|
|
|
|
let mut conditions: Vec<Expression> = Vec::new();
|
|
|
|
|
let mut blocks: Vec<Element> = Vec::new();
|
|
|
|
|
loop {
|
|
|
|
|
let expr = self.parse_expression_at_cmd_tag_end(arg_names)?;
|
|
|
|
|
let (inner_block, ending_tag) = self.parse_element_plus_ending_tag(arg_names)?;
|
|
|
|
|
conditions.push(expr);
|
|
|
|
|
match ending_tag.cmd {
|
2025-04-05 23:22:07 +03:00
|
|
|
BlockEndingTag::ELSE | BlockEndingTag::NORMAL | BlockEndingTag::ENDIF |
|
|
|
|
|
BlockEndingTag::ELSE_IF => blocks.push(inner_block),
|
2025-04-05 17:37:13 +03:00
|
|
|
_ => return Err(FileParsingError::new(
|
|
|
|
|
incorrect_block_ending_tag_expected_normal_or_endif_or_else_or_else_if, ending_tag.p1, self.p)),
|
|
|
|
|
}
|
2025-04-05 23:22:07 +03:00
|
|
|
if matches!(ending_tag.cmd, BlockEndingTag::ELSE) {
|
2025-04-05 17:37:13 +03:00
|
|
|
let (else_block, the_end) = self.parse_element_plus_ending_tag(arg_names)?;
|
2025-04-05 23:22:07 +03:00
|
|
|
if !matches!(the_end.cmd, BlockEndingTag::NORMAL | BlockEndingTag::ENDIF){
|
2025-04-05 17:37:13 +03:00
|
|
|
return Err(FileParsingError::new(incorrect_block_ending_tag_expected_normal_or_endif, the_end.p1, self.p));
|
|
|
|
|
}
|
|
|
|
|
blocks.push(else_block);
|
|
|
|
|
break
|
2025-04-05 23:22:07 +03:00
|
|
|
} else if matches!(ending_tag.cmd, BlockEndingTag::NORMAL | BlockEndingTag::ENDIF) {
|
2025-04-05 17:37:13 +03:00
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Ok(SubElement::If(IfSubElement{branches: blocks, conditions}))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn parse_let(&mut self, arg_names: &Vec<&str>) -> Result<SubElement, FileParsingError> {
|
|
|
|
|
self.skip_whitespace();
|
|
|
|
|
let p1 = self.p;
|
|
|
|
|
self.skip_normal_word();
|
|
|
|
|
if p1 == self.p {
|
|
|
|
|
// Ironically, these symbols are actually constants
|
|
|
|
|
return Err(FileParsingError::new(expected_variable_name, p1, self.p));
|
|
|
|
|
}
|
|
|
|
|
let new_variable_name = &self.text[p1..self.p];
|
|
|
|
|
if is_bad_name(new_variable_name){
|
|
|
|
|
return Err(FileParsingError::new(illegal_variable_name, p1, self.p));
|
|
|
|
|
}
|
|
|
|
|
self.skip_whitespace();
|
|
|
|
|
if !self.is_char_ahead('=') {
|
|
|
|
|
return Err(self.new_unexpected_char_error(expected_assignment_operator));
|
|
|
|
|
}
|
|
|
|
|
self.p += 1;
|
|
|
|
|
let expr = self.parse_expression_at_cmd_tag_end(arg_names)?;
|
|
|
|
|
let mut arg_names_extended = arg_names.clone();
|
|
|
|
|
arg_names_extended.push(new_variable_name);
|
|
|
|
|
let (inner_block, ending) = self.parse_element_plus_ending_tag(&arg_names_extended)?;
|
2025-04-05 23:22:07 +03:00
|
|
|
if !matches!(ending.cmd, BlockEndingTag::NORMAL) {
|
2025-04-05 17:37:13 +03:00
|
|
|
return Err(FileParsingError::new(incorrect_block_ending_tag_expected_normal, ending.p1, self.p));
|
|
|
|
|
}
|
|
|
|
|
Ok(SubElement::Let(expr, inner_block))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn parse_for_new_variable(&mut self, arg_names_extended: &Vec<&str>) -> Result<&'a str, FileParsingError> {
|
|
|
|
|
self.skip_whitespace();
|
|
|
|
|
let t1 = self.p;
|
|
|
|
|
self.skip_normal_word();
|
|
|
|
|
if t1 == self.p {
|
|
|
|
|
return Err(self.new_unexpected_char_error(expected_variable_name));
|
|
|
|
|
}
|
|
|
|
|
let name = &self.text[t1..self.p];
|
|
|
|
|
if is_illegal_name(name) {
|
|
|
|
|
return Err(FileParsingError::new(illegal_variable_name, t1, self.p));
|
|
|
|
|
}
|
|
|
|
|
if name != "_" && arg_names_extended.iter().find(|&&b| b == name).is_some() {
|
|
|
|
|
return Err(FileParsingError::new(forloop_variable_cant_take_occupied_name, t1, self.p));
|
|
|
|
|
}
|
|
|
|
|
Ok(name)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn parse_for(&mut self, arg_names: &Vec<&str>) -> Result<SubElement, FileParsingError> {
|
|
|
|
|
let mut arg_names_extended = arg_names.clone();
|
|
|
|
|
|
|
|
|
|
let name1 = self.parse_for_new_variable(&arg_names_extended)?;
|
|
|
|
|
arg_names_extended.push(name1);
|
|
|
|
|
|
|
|
|
|
let mut name2 = "";
|
|
|
|
|
self.skip_whitespace();
|
|
|
|
|
if self.is_char_ahead(',') {
|
|
|
|
|
self.p += 1;
|
|
|
|
|
name2 = self.parse_for_new_variable(&arg_names_extended)?;
|
|
|
|
|
}
|
|
|
|
|
arg_names_extended.push(name2);
|
|
|
|
|
|
|
|
|
|
if !self.is_char_ahead(':'){
|
|
|
|
|
return Err(self.new_unexpected_char_error(
|
|
|
|
|
if name2.len() > 0 { expected_colon } else { expected_comma_or_colon }
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
self.p += 1;
|
|
|
|
|
|
|
|
|
|
let expr = self.parse_expression_at_cmd_tag_end(arg_names)?;
|
|
|
|
|
let (inner_block, ending) = self.parse_element_plus_ending_tag(&arg_names_extended)?;
|
|
|
|
|
let separator: String = String::from(match ending.cmd {
|
2025-04-05 23:22:07 +03:00
|
|
|
BlockEndingTag::NOGAP => "",
|
|
|
|
|
BlockEndingTag::GAP => " ",
|
|
|
|
|
BlockEndingTag::NORMAL | BlockEndingTag::LF | BlockEndingTag::ENDLOOP => "\n",
|
2025-04-05 17:37:13 +03:00
|
|
|
_ => return Err(FileParsingError::new(
|
|
|
|
|
incorrect_block_ending_tag_expected_normal_or_lf_gap_nogap_or_forloop, ending.p1, self.p)),
|
|
|
|
|
});
|
|
|
|
|
Ok(SubElement::For(ForSubElement{iterable: expr, core: inner_block, join: separator}))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Checks for ]} }} and %}. May actually return NoneOfThose */
|
|
|
|
|
fn is_tag_end_ahead(&self) -> bool {
|
|
|
|
|
self.is_ahead("]}") || self.is_ahead("}}") || self.is_ahead("%}")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* l1 expression = l2 space l2 space ... space l2 */
|
|
|
|
|
fn parse_expression_l2(&mut self, arg_names: &Vec<&str>) -> Result<Expression, FileParsingError> {
|
|
|
|
|
self.skip_whitespace();
|
|
|
|
|
if self.is_char_ahead('(') {
|
|
|
|
|
let p1 = self.p;
|
|
|
|
|
self.p += 1;
|
|
|
|
|
let expr = self.parse_expression(arg_names)?;
|
|
|
|
|
self.skip_whitespace();
|
|
|
|
|
if !self.is_char_ahead(')') {
|
|
|
|
|
return Err(self.new_unexpected_char_error(expected_closing_round_bracket))
|
|
|
|
|
}
|
|
|
|
|
self.p += 1;
|
|
|
|
|
if matches!(expr, Expression::None){
|
|
|
|
|
return Err(FileParsingError::new(empty_expression_inside_round_brackets, p1, self.p))
|
|
|
|
|
}
|
|
|
|
|
return Ok(expr);
|
|
|
|
|
}
|
|
|
|
|
if self.is_digit_ahead() {
|
|
|
|
|
let p1 = self.p;
|
|
|
|
|
loop {
|
|
|
|
|
if self.is_digit_ahead() {
|
|
|
|
|
self.p += 1;
|
|
|
|
|
} else if self.is_word_ahead() {
|
|
|
|
|
return Err(self.new_unexpected_char_error(cant_start_word_immediately_after_digit))
|
|
|
|
|
} else {
|
|
|
|
|
return match self.text[p1..self.p].parse::<u64>() {
|
|
|
|
|
Ok(v) => Ok(Expression::Int(v)),
|
|
|
|
|
Err(_) => Err(FileParsingError::new(integer_parsing_error, p1, self.p)),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if self.is_word_ahead() {
|
|
|
|
|
let p1 = self.p;
|
|
|
|
|
self.skip_normal_word();
|
|
|
|
|
let toplevel_name = &self.text[p1..self.p];
|
|
|
|
|
if is_bad_name(toplevel_name) {
|
|
|
|
|
return Err(FileParsingError::new(illegal_object_name, p1, self.p));
|
|
|
|
|
}
|
|
|
|
|
let mut bg: Expression = match arg_names.iter().rposition(|&n| n == toplevel_name) {
|
|
|
|
|
Some(i) => Expression::Argument(i as u64),
|
|
|
|
|
None => Expression::Attribute(Box::new(Expression::Root), String::from(toplevel_name))
|
|
|
|
|
};
|
|
|
|
|
loop {
|
|
|
|
|
self.skip_whitespace();
|
|
|
|
|
bg = if self.is_char_ahead('.') {
|
|
|
|
|
self.p += 1;
|
|
|
|
|
self.skip_whitespace();
|
|
|
|
|
let attrp1 = self.p;
|
|
|
|
|
self.skip_normal_word();
|
|
|
|
|
if attrp1 == self.p {
|
|
|
|
|
return Err(self.new_unexpected_char_error(expected_attribute_name_after_dot));
|
|
|
|
|
}
|
|
|
|
|
let attr_name = &self.text[attrp1..self.p];
|
|
|
|
|
if is_bad_name(attr_name) {
|
|
|
|
|
return Err(FileParsingError::new(illegal_attribute_name, attrp1, self.p));
|
|
|
|
|
}
|
|
|
|
|
Expression::Attribute(Box::new(bg), String::from(attr_name))
|
|
|
|
|
} else if self.is_char_ahead('[') {
|
|
|
|
|
let sqbrp1 = self.p;
|
|
|
|
|
self.p += 1;
|
|
|
|
|
let sub_expr = self.parse_expression(arg_names)?;
|
|
|
|
|
self.skip_whitespace();
|
|
|
|
|
if !self.is_char_ahead(']') {
|
|
|
|
|
return Err(self.new_unexpected_char_error(expected_closing_square_bracket))
|
|
|
|
|
}
|
|
|
|
|
self.p += 1;
|
|
|
|
|
if matches!(sub_expr, Expression::None) {
|
|
|
|
|
return Err(FileParsingError::new(empty_expression_inside_square_brackets, sqbrp1, self.p))
|
|
|
|
|
}
|
|
|
|
|
Expression::Get(Box::new(bg), Box::new(sub_expr))
|
|
|
|
|
} else {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return Ok(bg)
|
|
|
|
|
} else {
|
|
|
|
|
return Ok(Expression::None)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn parse_expression(&mut self, arg_names: &Vec<&str>) -> Result<Expression, FileParsingError> {
|
|
|
|
|
let e1: Expression = self.parse_expression_l2(arg_names)?;
|
|
|
|
|
let mut call_args: Vec<Expression> = Vec::new();
|
|
|
|
|
/* It is okay to enter call_args reading loop even when e1 is None.
|
|
|
|
|
If parse_expression_l2 returned None, subsequent call to parse_expression_l2
|
|
|
|
|
is guaranteed to return None. Arg list will be empty and the final `if` will choose
|
|
|
|
|
the second branch, which will return Expression::None */
|
|
|
|
|
loop {
|
|
|
|
|
let arg_expr: Expression = self.parse_expression_l2(arg_names)?;
|
|
|
|
|
if matches!(arg_expr, Expression::None) {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
call_args.push(arg_expr)
|
|
|
|
|
}
|
|
|
|
|
Ok(if call_args.len() > 0 {
|
|
|
|
|
Expression::Call(Box::new(e1), call_args)
|
|
|
|
|
} else {
|
|
|
|
|
e1
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-07 15:40:38 +03:00
|
|
|
/* Parses a file treating it like a package (where other packages and elements could be located) */
|
|
|
|
|
pub fn parse_one_file_packed(text: &str) -> Result<Plemege, FileParsingError> {
|
2025-04-05 17:37:13 +03:00
|
|
|
let mut parser: Parser = Parser{text, p: 0};
|
|
|
|
|
parser.parse_pack_plus_ending(true)
|
|
|
|
|
}
|
2025-04-07 15:40:38 +03:00
|
|
|
|
|
|
|
|
pub fn parse_one_file_simplified(text: &str) -> Result<Plemege, FileParsingError> {
|
|
|
|
|
let mut parser: Parser = Parser{text, p: 0};
|
|
|
|
|
let (el, tt): (Element, ReasonOfElementEnd) = parser.parse_element_plus_ending_tag(&Vec::new())?;
|
|
|
|
|
match tt.cmd {
|
|
|
|
|
BlockEndingTag::EOF => Ok(Plemege::Element(el)),
|
|
|
|
|
BlockEndingTag::END_ELEMENT => Err(FileParsingError::new(unmatched_element_ending_tag, tt.p1, parser.p)),
|
|
|
|
|
_ => Err(FileParsingError::new(unmatched_magic_block_ending_tag, tt.p1, parser.p)),
|
|
|
|
|
}
|
|
|
|
|
}
|