Adds comments to entity files

attempt/001
Elijah Voigt 1 year ago
parent 07b5fbce1a
commit f9acf9e2ff

@ -1,2 +1 @@
00/a.entity
00/a.entity

@ -1 +1,3 @@
name "Hello world"
# camera3d
# transform ...

@ -22,6 +22,7 @@ mkShell rec {
vulkan-loader # Rendering
xorg.libX11 xorg.libXcursor xorg.libXi xorg.libXrandr # To use the x11 feature
tmux # Sharing environemnt between editor and terminal
git-bug
];
LD_LIBRARY_PATH = lib.makeLibraryPath buildInputs;

@ -21,6 +21,8 @@ pub(crate) enum Token {
Tag(String),
Str(String),
Num(f32),
Comment(String),
Etc,
}
///
@ -30,32 +32,46 @@ pub(crate) fn tokenize(line: &str) -> Vec<Token> {
let mut l = line;
let mut tokens = Vec::new();
while l.len() > 0 {
debug!("Line: {:?}", l);
if let Ok((rem, (_, s, _))) = tuple((
char::<&str, ()>('"'),
take_until("\""),
char::<&str, ()>('"'),
))(l)
{
debug!("Parsed string {:?}", s);
tokens.push(Token::Str(s.into()));
l = rem;
} else if let Ok((rem, num)) = float::<&str, ()>(l) {
debug!("Parsed float {:?}", num);
tokens.push(Token::Num(num.into()));
l = rem;
} else if let Ok((rem, (_, tag, _))) = tuple((space0, alphanumeric1::<&str, ()>, space0))(l)
{
debug!("Parsed tag {:?}", tag);
tokens.push(Token::Tag(tag.into()));
l = rem;
} else {
debug!("Breaking loop");
break;
// Check for comment
if let Ok((_, (_start, content))) = tuple((char::<&str, ()>('#'), not_line_ending))(line) {
tokens.push(Token::Comment(content.strip_prefix(" ").unwrap().into()));
} else {
// Check for all other token types in a loop
while l.len() > 0 {
if let Ok((rem, (_, s, _))) = tuple((
char::<&str, ()>('"'),
take_until("\""),
char::<&str, ()>('"'),
))(l)
{
debug!("Parsed string {:?}", s);
tokens.push(Token::Str(s.into()));
l = rem;
} else if let Ok((rem, num)) = float::<&str, ()>(l) {
debug!("Parsed float {:?}", num);
tokens.push(Token::Num(num.into()));
l = rem;
} else if let Ok((rem, (_, etc, _))) =
tuple((space0, tag::<&str, &str, ()>("..."), space0))(l)
{
debug!("Parsed etc. {:?}", etc);
tokens.push(Token::Etc);
l = rem;
} else if let Ok((rem, (_, tag, _))) =
tuple((space0, alphanumeric1::<&str, ()>, space0))(l)
{
debug!("Parsed tag {:?}", tag);
tokens.push(Token::Tag(tag.into()));
l = rem;
} else {
debug!("Breaking loop");
break;
}
}
}
debug!("Parsed tokens: {:?}", tokens);
tokens
}

@ -10,8 +10,8 @@ pub(crate) use bevy::{
window::{PrimaryWindow, WindowCloseRequested, WindowRef},
};
pub(crate) use nom::{
bytes::complete::take_until,
character::complete::{char, space0, alphanumeric1},
bytes::complete::{tag, take_until},
character::complete::{alphanumeric1, char, not_line_ending, space0},
number::complete::float,
sequence::tuple,
};

@ -79,25 +79,29 @@ impl SaveEntity {
// Tokenize the line
let tokens = tokenize(line);
// Run line against all parsers
for f in fns {
if let Ok(c) = f(&tokens) {
// Bundle the Type ID with this entry for auditing purposes
let t = c.get_represented_type_info().unwrap().type_id();
entity.components.push(SaveEntityComponent {
type_id: t,
data: c,
});
good = true;
if matches!(tokens[0], Token::Comment(..)) {
debug!("Skipping parsing comment line {:?}", tokens);
} else {
// Run line against all parsers
for f in fns {
if let Ok(c) = f(&tokens) {
// Bundle the Type ID with this entry for auditing purposes
let t = c.get_represented_type_info().unwrap().type_id();
entity.components.push(SaveEntityComponent {
type_id: t,
data: c,
});
good = true;
}
}
}
if !good {
error!(
file = load_context.path().to_str().unwrap(),
line = line,
"failed to parse component",
);
if !good {
error!(
file = load_context.path().to_str().unwrap(),
line = line,
"failed to parse component",
);
}
}
});
// Check for duplicate component types and emit an error

Loading…
Cancel
Save