2023-11-17 10:09:44 +01:00
|
|
|
use std::{fmt::Debug, path::PathBuf, sync::Arc};
|
2023-07-28 00:33:15 +02:00
|
|
|
|
2023-11-16 14:50:09 +01:00
|
|
|
use crate::{
|
|
|
|
errors::{CheckError, SourcePos},
|
|
|
|
program::{self, parsed::block::Block},
|
|
|
|
};
|
2023-07-28 00:33:15 +02:00
|
|
|
|
|
|
|
pub mod statements;
|
|
|
|
pub mod types;
|
|
|
|
|
2023-11-24 13:19:38 +01:00
|
|
|
pub fn parse(
|
|
|
|
src: &mut Source,
|
|
|
|
srca: &Arc<Source>,
|
|
|
|
) -> Result<Box<dyn program::parsed::MersStatement>, CheckError> {
|
2023-08-15 19:18:52 +02:00
|
|
|
let pos_in_src = src.get_pos();
|
2023-11-24 13:19:38 +01:00
|
|
|
let statements = statements::parse_multiple(src, srca, "")?;
|
2023-10-19 18:46:15 +02:00
|
|
|
let block = Block {
|
2023-11-24 13:19:38 +01:00
|
|
|
pos_in_src: (pos_in_src, src.get_pos(), srca).into(),
|
2023-10-23 21:48:15 +02:00
|
|
|
statements,
|
2023-10-19 18:46:15 +02:00
|
|
|
};
|
|
|
|
Ok(Box::new(block))
|
2023-07-28 00:33:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Source {
|
2023-11-17 10:09:44 +01:00
|
|
|
src_from: SourceFrom,
|
2023-08-15 19:18:52 +02:00
|
|
|
src_raw_len: usize,
|
2023-10-23 21:48:15 +02:00
|
|
|
src_og: String,
|
2023-07-28 00:33:15 +02:00
|
|
|
src: String,
|
2023-11-16 14:50:09 +01:00
|
|
|
/// (start, content) of each comment, including start/end (//, /* and */), but NOT newline after //
|
2023-08-15 19:18:52 +02:00
|
|
|
comments: Vec<(usize, String)>,
|
2023-07-28 00:33:15 +02:00
|
|
|
i: usize,
|
|
|
|
sections: Vec<SectionMarker>,
|
|
|
|
}
|
2023-11-17 10:09:44 +01:00
|
|
|
impl Clone for Source {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self {
|
|
|
|
src_from: self.src_from.clone(),
|
|
|
|
src_raw_len: self.src_raw_len,
|
|
|
|
src_og: self.src_og.clone(),
|
|
|
|
src: self.src.clone(),
|
|
|
|
comments: self.comments.clone(),
|
|
|
|
i: self.i,
|
|
|
|
sections: vec![],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl Debug for Source {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
write!(f, "Src: {:?}", self.src_from)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub enum SourceFrom {
|
|
|
|
File(PathBuf),
|
|
|
|
Unspecified,
|
|
|
|
}
|
2023-07-28 00:33:15 +02:00
|
|
|
impl Source {
|
2023-11-17 10:09:44 +01:00
|
|
|
pub fn new_from_file(path: PathBuf) -> std::io::Result<Self> {
|
|
|
|
let content = std::fs::read_to_string(&path)?;
|
|
|
|
Ok(Self::new(SourceFrom::File(path), content))
|
|
|
|
}
|
2023-11-21 22:10:58 +01:00
|
|
|
pub fn new_from_string_raw(source: String) -> Self {
|
|
|
|
Self {
|
|
|
|
src_from: SourceFrom::Unspecified,
|
|
|
|
src_raw_len: source.len(),
|
|
|
|
src_og: source.clone(),
|
|
|
|
src: source,
|
|
|
|
comments: vec![],
|
|
|
|
i: 0,
|
|
|
|
sections: vec![],
|
|
|
|
}
|
|
|
|
}
|
2023-11-17 10:09:44 +01:00
|
|
|
pub fn new_from_string(source: String) -> Self {
|
|
|
|
Self::new(SourceFrom::Unspecified, source)
|
|
|
|
}
|
|
|
|
pub fn new(src_from: SourceFrom, source: String) -> Self {
|
2023-08-15 19:18:52 +02:00
|
|
|
let mut src = String::with_capacity(source.len());
|
|
|
|
let mut comment = (0, String::new());
|
|
|
|
let mut comments = Vec::new();
|
|
|
|
let mut chars = source.char_indices().peekable();
|
|
|
|
let mut in_comment = None;
|
|
|
|
loop {
|
|
|
|
if let Some((i, ch)) = chars.next() {
|
|
|
|
match in_comment {
|
|
|
|
Some(false) => {
|
|
|
|
if ch == '\n' {
|
2023-11-16 14:50:09 +01:00
|
|
|
src.push('\n');
|
2023-08-15 19:18:52 +02:00
|
|
|
in_comment = None;
|
|
|
|
comments.push((
|
|
|
|
comment.0,
|
|
|
|
std::mem::replace(&mut comment.1, String::new()),
|
|
|
|
));
|
2023-11-16 14:50:09 +01:00
|
|
|
} else {
|
|
|
|
comment.1.push(ch);
|
2023-08-15 19:18:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(true) => {
|
|
|
|
comment.1.push(ch);
|
|
|
|
if ch == '*' && matches!(chars.peek(), Some((_, '/'))) {
|
|
|
|
chars.next();
|
|
|
|
comment.1.push('/');
|
|
|
|
in_comment = None;
|
|
|
|
comments.push((
|
|
|
|
comment.0,
|
|
|
|
std::mem::replace(&mut comment.1, String::new()),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => match ch {
|
2023-10-19 18:46:15 +02:00
|
|
|
'\\' if matches!(chars.peek(), Some((_, '/'))) => {
|
|
|
|
chars.next();
|
|
|
|
src.push('/');
|
|
|
|
}
|
2023-08-15 19:18:52 +02:00
|
|
|
'/' if matches!(chars.peek(), Some((_, '/'))) => {
|
|
|
|
chars.next();
|
|
|
|
in_comment = Some(false);
|
|
|
|
comment.0 = i;
|
|
|
|
comment.1.push('/');
|
|
|
|
comment.1.push('/');
|
|
|
|
}
|
|
|
|
'/' if matches!(chars.peek(), Some((_, '*'))) => {
|
|
|
|
chars.next();
|
|
|
|
in_comment = Some(true);
|
|
|
|
comment.0 = i;
|
|
|
|
comment.1.push('/');
|
|
|
|
comment.1.push('*');
|
|
|
|
}
|
|
|
|
_ => src.push(ch),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-07-28 00:33:15 +02:00
|
|
|
Self {
|
2023-11-17 10:09:44 +01:00
|
|
|
src_from,
|
2023-08-15 19:18:52 +02:00
|
|
|
src_raw_len: source.len(),
|
2023-10-23 21:48:15 +02:00
|
|
|
src_og: source,
|
2023-07-28 00:33:15 +02:00
|
|
|
src,
|
2023-08-15 19:18:52 +02:00
|
|
|
comments,
|
2023-07-28 00:33:15 +02:00
|
|
|
i: 0,
|
|
|
|
sections: vec![],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-23 21:48:15 +02:00
|
|
|
pub fn src(&self) -> &String {
|
|
|
|
&self.src
|
|
|
|
}
|
|
|
|
pub fn comments(&self) -> &Vec<(usize, String)> {
|
|
|
|
&self.comments
|
|
|
|
}
|
|
|
|
|
2023-07-28 00:33:15 +02:00
|
|
|
pub fn skip_whitespace(&mut self) {
|
|
|
|
if let Some(i) = self.src[self.i..].char_indices().find_map(|(i, ch)| {
|
|
|
|
if !ch.is_whitespace() {
|
|
|
|
Some(i)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}) {
|
|
|
|
self.i += i;
|
|
|
|
} else {
|
|
|
|
self.i = self.src.len();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn end_sections(&mut self) {
|
|
|
|
let pos = self.get_pos();
|
|
|
|
for section in self.sections.iter_mut() {
|
|
|
|
section.checked_end(pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn peek_char(&self) -> Option<char> {
|
|
|
|
self.src[self.i..].chars().next()
|
|
|
|
}
|
|
|
|
pub fn next_char(&mut self) -> Option<char> {
|
|
|
|
self.end_sections();
|
|
|
|
let ch = self.src[self.i..].chars().next()?;
|
|
|
|
self.i += ch.len_utf8();
|
|
|
|
Some(ch)
|
|
|
|
}
|
|
|
|
fn word_splitter(ch: char) -> bool {
|
2023-11-21 22:10:58 +01:00
|
|
|
ch.is_whitespace() || ".,;[](){}/<".contains(ch)
|
2023-07-28 00:33:15 +02:00
|
|
|
}
|
|
|
|
pub fn peek_word(&self) -> &str {
|
|
|
|
self.src[self.i..]
|
|
|
|
.split(Self::word_splitter)
|
|
|
|
.next()
|
|
|
|
.unwrap_or("")
|
|
|
|
}
|
|
|
|
pub fn next_word(&mut self) -> &str {
|
|
|
|
self.end_sections();
|
|
|
|
let word = self.src[self.i..]
|
|
|
|
.split(Self::word_splitter)
|
|
|
|
.next()
|
|
|
|
.unwrap_or("");
|
|
|
|
self.i += word.len();
|
|
|
|
word
|
|
|
|
}
|
|
|
|
pub fn peek_line(&self) -> &str {
|
|
|
|
self.src[self.i..].lines().next().unwrap_or("")
|
|
|
|
}
|
|
|
|
pub fn next_line(&mut self) -> &str {
|
|
|
|
self.end_sections();
|
|
|
|
let line = self.src[self.i..].lines().next().unwrap_or("");
|
|
|
|
self.i += line.len();
|
|
|
|
line
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_pos(&self) -> SourcePos {
|
|
|
|
SourcePos(self.i)
|
|
|
|
}
|
|
|
|
pub fn set_pos(&mut self, new: SourcePos) {
|
|
|
|
self.i = new.0
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a SectionMarker which, when dropped, indicates the end of this section.
|
|
|
|
/// Useful for debugging the parser.
|
|
|
|
pub fn section_begin(&mut self, section: String) -> Arc<String> {
|
|
|
|
#[cfg(debug_assertions)]
|
2023-08-14 17:17:08 +02:00
|
|
|
println!("[mers:parse] Section begin: {}", §ion);
|
2023-07-28 00:33:15 +02:00
|
|
|
let arc = Arc::new(section);
|
|
|
|
self.sections.push(SectionMarker {
|
|
|
|
section: Arc::clone(&arc),
|
|
|
|
start: self.get_pos(),
|
|
|
|
end: None,
|
|
|
|
});
|
|
|
|
arc
|
|
|
|
}
|
|
|
|
pub fn sections(&self) -> &Vec<SectionMarker> {
|
|
|
|
&self.sections
|
|
|
|
}
|
2023-08-15 19:18:52 +02:00
|
|
|
|
2023-10-23 21:48:15 +02:00
|
|
|
pub fn get_line_start(&self, pos: usize) -> usize {
|
|
|
|
self.src[0..pos].rfind("\n").map(|i| i + 1).unwrap_or(0)
|
|
|
|
}
|
|
|
|
pub fn get_line_end(&self, pos: usize) -> usize {
|
|
|
|
// TODO: If the newline is preceded by `\r`s, remove those too since they are part of the newline `\r\n` sequence
|
|
|
|
self.src[pos..]
|
|
|
|
.find("\n")
|
|
|
|
.map(|i| i + pos)
|
|
|
|
.unwrap_or(self.src.len())
|
|
|
|
}
|
|
|
|
|
2023-08-15 19:18:52 +02:00
|
|
|
pub fn format(&self, insertions: &Vec<(usize, bool, String)>) -> String {
|
|
|
|
let mut o = String::with_capacity(self.src_raw_len);
|
|
|
|
let mut insertions = insertions.iter().peekable();
|
|
|
|
let mut comments = self.comments.iter().peekable();
|
|
|
|
for (i, ch) in self.src.char_indices() {
|
|
|
|
let insert = if let Some((index, pre, _)) = insertions.peek() {
|
|
|
|
if *index <= i {
|
|
|
|
Some(*pre)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
if let Some((index, comment)) = comments.peek() {
|
|
|
|
if *index == i {
|
|
|
|
comments.next();
|
|
|
|
o.push_str(comment);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Some(true) = insert {
|
|
|
|
o.push_str(&insertions.next().unwrap().2);
|
|
|
|
}
|
|
|
|
o.push(ch);
|
|
|
|
if let Some(false) = insert {
|
|
|
|
o.push_str(&insertions.next().unwrap().2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
o
|
|
|
|
}
|
2023-11-16 14:50:09 +01:00
|
|
|
|
2023-11-17 10:09:44 +01:00
|
|
|
pub fn src_from(&self) -> &SourceFrom {
|
|
|
|
&self.src_from
|
|
|
|
}
|
|
|
|
|
2023-11-16 14:50:09 +01:00
|
|
|
pub fn pos_in_og(&self, mut pos: usize, inclusive: bool) -> usize {
|
|
|
|
for (start, comment) in &self.comments {
|
|
|
|
if *start < pos || (inclusive && *start == pos) {
|
|
|
|
pos += comment.len();
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pos
|
|
|
|
}
|
|
|
|
pub fn src_og(&self) -> &String {
|
|
|
|
&self.src_og
|
|
|
|
}
|
2023-07-28 00:33:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Source {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
self.end_sections()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returned from Source::begin(), this indicates that parsing of
|
|
|
|
/// a certain section has not yet finished.
|
|
|
|
/// Once this is dropped, a section is considered done.
|
|
|
|
pub struct SectionMarker {
|
|
|
|
section: Arc<String>,
|
|
|
|
start: SourcePos,
|
|
|
|
end: Option<SourcePos>,
|
|
|
|
}
|
|
|
|
impl SectionMarker {
|
|
|
|
pub fn section(&self) -> &str {
|
|
|
|
&self.section
|
|
|
|
}
|
|
|
|
pub fn start(&self) -> &SourcePos {
|
|
|
|
&self.start
|
|
|
|
}
|
|
|
|
pub fn end(&self) -> &Option<SourcePos> {
|
|
|
|
&self.end
|
|
|
|
}
|
|
|
|
/// If this is the only remaining SectionMarker for this section,
|
|
|
|
/// this method sets its `end` property.
|
|
|
|
fn checked_end(&mut self, end: SourcePos) {
|
|
|
|
if self.end.is_none() {
|
|
|
|
if Arc::strong_count(&self.section) == 1 {
|
|
|
|
self.end = Some(end);
|
|
|
|
#[cfg(debug_assertions)]
|
2023-08-14 17:17:08 +02:00
|
|
|
println!("[mers:parse] Section end : {}", &self.section);
|
2023-07-28 00:33:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|