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
00/a.entity

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

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

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

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

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

Loading…
Cancel
Save