Fix parsing bugs

1. empty key & value support
2. OnNull handling in test
This commit is contained in:
Yuheng Chen 2015-05-27 23:04:03 +08:00
parent ef020f0f95
commit b6acd869eb
3 changed files with 15 additions and 5 deletions

View file

@ -155,6 +155,7 @@ impl<T: Iterator<Item=char>> Parser<T> {
fn load_node<R: EventReceiver>(&mut self, first_ev: &Event, recv: &mut R)
-> Result<(), ScanError> {
match *first_ev {
Event::Alias => { unimplemented!() },
Event::Scalar(ref v, style) => {
Ok(())
},
@ -164,8 +165,8 @@ impl<T: Iterator<Item=char>> Parser<T> {
Event::MappingStart => {
self.load_mapping(first_ev, recv)
},
// TODO more events
_ => { unreachable!(); }
_ => { println!("UNREACHABLE EVENT: {:?}", first_ev);
unreachable!(); }
}
}
@ -379,7 +380,9 @@ impl<T: Iterator<Item=char>> Parser<T> {
self.skip();
let tok = try!(self.peek());
match tok.1 {
TokenType::KeyToken | TokenType::ValueToken | TokenType::BlockEndToken
TokenType::KeyToken
| TokenType::ValueToken
| TokenType::BlockEndToken
=> {
self.state = State::BlockMappingValue;
// empty scalar
@ -391,6 +394,11 @@ impl<T: Iterator<Item=char>> Parser<T> {
}
}
},
// XXX(chenyh): libyaml failed to parse spec 1.2, ex8.18
TokenType::ValueToken => {
self.state = State::BlockMappingValue;
Ok(Event::empty_scalar())
},
TokenType::BlockEndToken => {
self.pop_state();
self.skip();
@ -411,7 +419,7 @@ impl<T: Iterator<Item=char>> Parser<T> {
match tok.1 {
TokenType::KeyToken | TokenType::ValueToken | TokenType::BlockEndToken
=> {
self.state = State::BlockMappingValue;
self.state = State::BlockMappingKey;
// empty scalar
Ok(Event::empty_scalar())
}

View file

@ -2,6 +2,7 @@
extern crate yaml_rust;
use yaml_rust::parser::{Parser, EventReceiver, Event};
use yaml_rust::scanner::TScalarStyle;
use yaml_rust::yaml::Yaml;
#[derive(Clone, PartialEq, PartialOrd, Debug)]
@ -31,7 +32,7 @@ impl EventReceiver for YamlChecker {
Event::MappingStart => TestEvent::OnMapStart,
Event::MappingEnd => TestEvent::OnMapEnd,
Event::Scalar(ref v, style) => {
if v == "~" {
if v == "~" && style == TScalarStyle::Plain {
TestEvent::OnNull
} else {
TestEvent::OnScalar

View file

@ -320,6 +320,7 @@ const EX8_16 : &'static str =
const EX8_17 : &'static str =
"? explicit key # Empty value\n? |\n block key\n: - one # Explicit compact\n - two # block value\n";
// XXX libyaml failed this test
const EX8_18 : &'static str =
"plain key: in-line value\n: # Both empty\n\"quoted key\":\n- entry\n";