moar parsing

This commit is contained in:
SimonFJ20 2023-01-21 03:40:08 +01:00
parent df49e28f78
commit 8852ca6fe2
3 changed files with 49 additions and 35 deletions

View File

@ -56,7 +56,8 @@ value ->
object -> "{" object_properties "}" object -> "{" object_properties "}"
object_properties -> (_ object_property (_ "," _ object_property):* _ ",":?):? _ object_properties ->
(_ object_property (_ "," _ object_property):* _ ",":?):? _
object_property -> (Name | String) _ ("=" | ":") _ value object_property -> (Name | String) _ ("=" | ":") _ value

View File

@ -1,17 +1,17 @@
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq)]
pub enum ErrorType { pub enum ErrorType {
UnexpectedToken(char), UnexpectedToken(char),
InvalidConstructor, InvalidConstructor,
} }
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq)]
pub struct Error { pub struct Error {
error: ErrorType, error: ErrorType,
line: isize, line: isize,
col: isize, col: isize,
} }
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq)]
pub enum Token { pub enum Token {
Name(String), Name(String),
Id(String), Id(String),
@ -30,10 +30,10 @@ pub enum Token {
RBrace(String), RBrace(String),
LBracket(String), LBracket(String),
RBracket(String), RBracket(String),
Equal(String), Equal(String), // not implemented
Colon(String), Colon(String), // not implemented
SemiColon(String), SemiColon(String), // not implemented
Comma(String), Comma(String), // not implemented
} }
#[derive(PartialEq)] #[derive(PartialEq)]

View File

@ -41,9 +41,20 @@ impl Parser {
} }
fn parse_element(&mut self) -> Result<Node, ParserError> { fn parse_element(&mut self) -> Result<Node, ParserError> {
let name = match self.current() {
Some(Token::Name(value)) => value.clone(),
_ => panic!("checked by previous predicate"),
};
self.step();
todo!() todo!()
} }
fn parse_singe_line_field(&mut self) -> Result<Node, ParserError> {
match self.current() {
_ => todo!(),
}
}
fn parse_value(&mut self) -> Result<Node, ParserError> { fn parse_value(&mut self) -> Result<Node, ParserError> {
match self.current() { match self.current() {
Some(Token::LBrace(_)) => self.parse_object(), Some(Token::LBrace(_)) => self.parse_object(),
@ -72,26 +83,28 @@ impl Parser {
Ok(Node::Object(values)) Ok(Node::Object(values))
} }
Some(t @ (Token::Name(_) | Token::String(_))) => { Some(t @ (Token::Name(_) | Token::String(_))) => {
// let key = match t { let key = match t {
// Token::Name(v) => v, Token::Name(v) => v.clone(),
// Token::String(v) => &v[1..v.len() - 1].to_string(), Token::String(v) => v[1..v.len() - 1].to_string(),
// _ => panic!("checked by previous predicate"), _ => panic!("checked by previous predicate"),
// }; };
// self.step(); self.step();
// match self.current() { match self.current() {
// Some(Token::Equal(_) | Token::Colon(_)) => {} Some(Token::Equal(_) | Token::Colon(_)) => {}
// _ => return Err("expected ':' or '='".to_string()), _ => return Err("expected ':' or '='".to_string()),
// } }
// self.step(); self.step();
// values[key] = Box::new(self.parse_value()?); values.insert(key, Box::new(self.parse_value()?));
// self.parse_object_tail(values) self.parse_object_tail(values)
todo!()
} }
_ => Err("expected Name, String or '}'".to_string()), _ => Err("expected Name, String or '}'".to_string()),
} }
} }
fn parse_object_tail(&mut self, values: HashMap<String, Box<Node>>) -> Result<Node, String> { fn parse_object_tail(
&mut self,
mut values: HashMap<String, Box<Node>>,
) -> Result<Node, String> {
loop { loop {
match self.current() { match self.current() {
Some(Token::RBrace(_)) => { Some(Token::RBrace(_)) => {
@ -106,18 +119,18 @@ impl Parser {
break Ok(Node::Object(values)); break Ok(Node::Object(values));
} }
Some(t @ (Token::Name(_) | Token::String(_))) => { Some(t @ (Token::Name(_) | Token::String(_))) => {
// let key = match t { let key = match t {
// Token::Name(v) => v, Token::Name(v) => v.clone(),
// Token::String(v) => &v[1..v.len() - 1].to_string(), Token::String(v) => v[1..v.len() - 1].to_string(),
// _ => panic!("unterminated object, checked by previous predicate"), _ => panic!("unterminated object, checked by previous predicate"),
// }; };
// self.step(); self.step();
// match self.current() { match self.current() {
// Some(Token::Equal(_) | Token::Colon(_)) => {} Some(Token::Equal(_) | Token::Colon(_)) => {}
// _ => return Err("expected ':' or '='".to_string()), _ => return Err("expected ':' or '='".to_string()),
// } }
// self.step(); self.step();
// values[key] = Box::new(self.parse_value()?); values.insert(key, Box::new(self.parse_value()?));
todo!() todo!()
} }
_ => { _ => {