Implemented adjacent key:value pairs when the key is JSON-like

This commit is contained in:
jbeder
2009-10-29 21:05:48 +00:00
parent 72dceba671
commit 011a608b5a
4 changed files with 31 additions and 3 deletions

View File

@@ -43,7 +43,8 @@ namespace YAML
const RegEx Key = RegEx('?'), const RegEx Key = RegEx('?'),
KeyInFlow = RegEx('?') + BlankOrBreak; KeyInFlow = RegEx('?') + BlankOrBreak;
const RegEx Value = RegEx(':') + (BlankOrBreak || RegEx()), const RegEx Value = RegEx(':') + (BlankOrBreak || RegEx()),
ValueInFlow = RegEx(':') + (BlankOrBreak || RegEx(",}", REGEX_OR)); ValueInFlow = RegEx(':') + (BlankOrBreak || RegEx(",}", REGEX_OR)),
ValueInJSONFlow = RegEx(':');
const RegEx Comment = RegEx('#'); const RegEx Comment = RegEx('#');
const RegEx AnchorEnd = RegEx("?:,]}%@`", REGEX_OR) || BlankOrBreak; const RegEx AnchorEnd = RegEx("?:,]}%@`", REGEX_OR) || BlankOrBreak;
const RegEx URI = Word || RegEx("#;/?:@&=+$,_.!~*'()[]", REGEX_OR) || (RegEx('%') + Hex + Hex); const RegEx URI = Word || RegEx("#;/?:@&=+$,_.!~*'()[]", REGEX_OR) || (RegEx('%') + Hex + Hex);

View File

@@ -8,7 +8,7 @@
namespace YAML namespace YAML
{ {
Scanner::Scanner(std::istream& in) Scanner::Scanner(std::istream& in)
: INPUT(in), m_startedStream(false), m_endedStream(false), m_simpleKeyAllowed(false) : INPUT(in), m_startedStream(false), m_endedStream(false), m_simpleKeyAllowed(false), m_canBeJSONFlow(false)
{ {
} }
@@ -142,7 +142,7 @@ namespace YAML
if((InBlockContext() ? Exp::Key : Exp::KeyInFlow).Matches(INPUT)) if((InBlockContext() ? Exp::Key : Exp::KeyInFlow).Matches(INPUT))
return ScanKey(); return ScanKey();
if((InBlockContext() ? Exp::Value : Exp::ValueInFlow).Matches(INPUT)) if(GetValueRegex().Matches(INPUT))
return ScanValue(); return ScanValue();
// alias/anchor // alias/anchor
@@ -226,6 +226,16 @@ namespace YAML
return false; return false;
} }
// GetValueRegex
// . Get the appropriate regex to check if it's a value token
const RegEx& Scanner::GetValueRegex() const
{
if(InBlockContext())
return Exp::Value;
return m_canBeJSONFlow ? Exp::ValueInJSONFlow : Exp::ValueInFlow;
}
// StartStream // StartStream
// . Set the initial conditions for starting a stream. // . Set the initial conditions for starting a stream.
void Scanner::StartStream() void Scanner::StartStream()
@@ -410,3 +420,4 @@ namespace YAML
m_anchors.clear(); m_anchors.clear();
} }
} }

View File

@@ -16,6 +16,7 @@
namespace YAML namespace YAML
{ {
class Node; class Node;
class RegEx;
class Scanner class Scanner
{ {
@@ -78,6 +79,7 @@ namespace YAML
void ThrowParserException(const std::string& msg) const; void ThrowParserException(const std::string& msg) const;
bool IsWhitespaceToBeEaten(char ch); bool IsWhitespaceToBeEaten(char ch);
const RegEx& GetValueRegex() const;
struct SimpleKey { struct SimpleKey {
SimpleKey(const Mark& mark_, int flowLevel_); SimpleKey(const Mark& mark_, int flowLevel_);
@@ -120,6 +122,7 @@ namespace YAML
// state info // state info
bool m_startedStream, m_endedStream; bool m_startedStream, m_endedStream;
bool m_simpleKeyAllowed; bool m_simpleKeyAllowed;
bool m_canBeJSONFlow;
std::stack <SimpleKey> m_simpleKeys; std::stack <SimpleKey> m_simpleKeys;
std::stack <IndentMarker *> m_indents; std::stack <IndentMarker *> m_indents;
std::vector <IndentMarker *> m_indentRefs; // for "garbage collection" std::vector <IndentMarker *> m_indentRefs; // for "garbage collection"

View File

@@ -24,6 +24,7 @@ namespace YAML
PopAllSimpleKeys(); PopAllSimpleKeys();
m_simpleKeyAllowed = false; m_simpleKeyAllowed = false;
m_canBeJSONFlow = false;
// store pos and eat indicator // store pos and eat indicator
Token token(Token::DIRECTIVE, INPUT.mark()); Token token(Token::DIRECTIVE, INPUT.mark());
@@ -60,6 +61,7 @@ namespace YAML
PopAllIndents(); PopAllIndents();
PopAllSimpleKeys(); PopAllSimpleKeys();
m_simpleKeyAllowed = false; m_simpleKeyAllowed = false;
m_canBeJSONFlow = false;
// eat // eat
Mark mark = INPUT.mark(); Mark mark = INPUT.mark();
@@ -73,6 +75,7 @@ namespace YAML
PopAllIndents(); PopAllIndents();
PopAllSimpleKeys(); PopAllSimpleKeys();
m_simpleKeyAllowed = false; m_simpleKeyAllowed = false;
m_canBeJSONFlow = false;
// eat // eat
Mark mark = INPUT.mark(); Mark mark = INPUT.mark();
@@ -86,6 +89,7 @@ namespace YAML
// flows can be simple keys // flows can be simple keys
InsertPotentialSimpleKey(); InsertPotentialSimpleKey();
m_simpleKeyAllowed = true; m_simpleKeyAllowed = true;
m_canBeJSONFlow = false;
// eat // eat
Mark mark = INPUT.mark(); Mark mark = INPUT.mark();
@@ -111,6 +115,7 @@ namespace YAML
} }
m_simpleKeyAllowed = false; m_simpleKeyAllowed = false;
m_canBeJSONFlow = true;
// eat // eat
Mark mark = INPUT.mark(); Mark mark = INPUT.mark();
@@ -138,6 +143,7 @@ namespace YAML
} }
m_simpleKeyAllowed = true; m_simpleKeyAllowed = true;
m_canBeJSONFlow = false;
// eat // eat
Mark mark = INPUT.mark(); Mark mark = INPUT.mark();
@@ -158,6 +164,7 @@ namespace YAML
PushIndentTo(INPUT.column(), IndentMarker::SEQ); PushIndentTo(INPUT.column(), IndentMarker::SEQ);
m_simpleKeyAllowed = true; m_simpleKeyAllowed = true;
m_canBeJSONFlow = false;
// eat // eat
Mark mark = INPUT.mark(); Mark mark = INPUT.mark();
@@ -190,6 +197,7 @@ namespace YAML
{ {
// and check that simple key // and check that simple key
bool isSimpleKey = VerifySimpleKey(); bool isSimpleKey = VerifySimpleKey();
m_canBeJSONFlow = false;
if(isSimpleKey) { if(isSimpleKey) {
// can't follow a simple key with another simple key (dunno why, though - it seems fine) // can't follow a simple key with another simple key (dunno why, though - it seems fine)
@@ -222,6 +230,7 @@ namespace YAML
// insert a potential simple key // insert a potential simple key
InsertPotentialSimpleKey(); InsertPotentialSimpleKey();
m_simpleKeyAllowed = false; m_simpleKeyAllowed = false;
m_canBeJSONFlow = false;
// eat the indicator // eat the indicator
Mark mark = INPUT.mark(); Mark mark = INPUT.mark();
@@ -252,6 +261,7 @@ namespace YAML
// insert a potential simple key // insert a potential simple key
InsertPotentialSimpleKey(); InsertPotentialSimpleKey();
m_simpleKeyAllowed = false; m_simpleKeyAllowed = false;
m_canBeJSONFlow = false;
Token token(Token::TAG, INPUT.mark()); Token token(Token::TAG, INPUT.mark());
@@ -305,6 +315,7 @@ namespace YAML
// can have a simple key only if we ended the scalar by starting a new line // can have a simple key only if we ended the scalar by starting a new line
m_simpleKeyAllowed = params.leadingSpaces; m_simpleKeyAllowed = params.leadingSpaces;
m_canBeJSONFlow = false;
// finally, check and see if we ended on an illegal character // finally, check and see if we ended on an illegal character
//if(Exp::IllegalCharInScalar.Matches(INPUT)) //if(Exp::IllegalCharInScalar.Matches(INPUT))
@@ -347,6 +358,7 @@ namespace YAML
// and scan // and scan
scalar = ScanScalar(INPUT, params); scalar = ScanScalar(INPUT, params);
m_simpleKeyAllowed = false; m_simpleKeyAllowed = false;
m_canBeJSONFlow = true;
Token token(Token::SCALAR, mark); Token token(Token::SCALAR, mark);
token.value = scalar; token.value = scalar;
@@ -413,6 +425,7 @@ namespace YAML
// simple keys always ok after block scalars (since we're gonna start a new line anyways) // simple keys always ok after block scalars (since we're gonna start a new line anyways)
m_simpleKeyAllowed = true; m_simpleKeyAllowed = true;
m_canBeJSONFlow = false;
Token token(Token::SCALAR, mark); Token token(Token::SCALAR, mark);
token.value = scalar; token.value = scalar;