From 19673ff01aabe1c293ba57aa9a3c3c3bfe5b0709 Mon Sep 17 00:00:00 2001 From: Jesse Beder Date: Sat, 5 Sep 2009 02:28:11 +0000 Subject: [PATCH] Moved token enums into Token scope --- src/map.cpp | 22 +++++++++---------- src/node.cpp | 16 +++++++------- src/parser.cpp | 6 +++--- src/scanner.cpp | 14 ++++++------ src/scantoken.cpp | 28 ++++++++++++------------ src/sequence.cpp | 16 +++++++------- src/simplekey.cpp | 14 ++++++------ src/token.h | 54 ++++++++++++++++++++++++----------------------- 8 files changed, 86 insertions(+), 84 deletions(-) diff --git a/src/map.cpp b/src/map.cpp index ce24978..8aadec9 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -59,8 +59,8 @@ namespace YAML // split based on start token switch(pScanner->peek().type) { - case TT_BLOCK_MAP_START: ParseBlock(pScanner, state); break; - case TT_FLOW_MAP_START: ParseFlow(pScanner, state); break; + case Token::BLOCK_MAP_START: ParseBlock(pScanner, state); break; + case Token::FLOW_MAP_START: ParseFlow(pScanner, state); break; default: break; } } @@ -75,10 +75,10 @@ namespace YAML throw ParserException(Mark::null(), ErrorMsg::END_OF_MAP); Token token = pScanner->peek(); - if(token.type != TT_KEY && token.type != TT_VALUE && token.type != TT_BLOCK_MAP_END) + if(token.type != Token::KEY && token.type != Token::VALUE && token.type != Token::BLOCK_MAP_END) throw ParserException(token.mark, ErrorMsg::END_OF_MAP); - if(token.type == TT_BLOCK_MAP_END) { + if(token.type == Token::BLOCK_MAP_END) { pScanner->pop(); break; } @@ -86,13 +86,13 @@ namespace YAML std::auto_ptr pKey(new Node), pValue(new Node); // grab key (if non-null) - if(token.type == TT_KEY) { + if(token.type == Token::KEY) { pScanner->pop(); pKey->Parse(pScanner, state); } // now grab value (optional) - if(!pScanner->empty() && pScanner->peek().type == TT_VALUE) { + if(!pScanner->empty() && pScanner->peek().type == Token::VALUE) { pScanner->pop(); pValue->Parse(pScanner, state); } @@ -113,13 +113,13 @@ namespace YAML Token& token = pScanner->peek(); // first check for end - if(token.type == TT_FLOW_MAP_END) { + if(token.type == Token::FLOW_MAP_END) { pScanner->pop(); break; } // now it better be a key - if(token.type != TT_KEY) + if(token.type != Token::KEY) throw ParserException(token.mark, ErrorMsg::END_OF_MAP_FLOW); pScanner->pop(); @@ -130,16 +130,16 @@ namespace YAML pKey->Parse(pScanner, state); // now grab value (optional) - if(!pScanner->empty() && pScanner->peek().type == TT_VALUE) { + if(!pScanner->empty() && pScanner->peek().type == Token::VALUE) { pScanner->pop(); pValue->Parse(pScanner, state); } // now eat the separator (or could be a map end, which we ignore - but if it's neither, then it's a bad node) Token& nextToken = pScanner->peek(); - if(nextToken.type == TT_FLOW_ENTRY) + if(nextToken.type == Token::FLOW_ENTRY) pScanner->pop(); - else if(nextToken.type != TT_FLOW_MAP_END) + else if(nextToken.type != Token::FLOW_MAP_END) throw ParserException(nextToken.mark, ErrorMsg::END_OF_MAP_FLOW); // assign the map with the actual pointers diff --git a/src/node.cpp b/src/node.cpp index 7223bed..c604e43 100644 --- a/src/node.cpp +++ b/src/node.cpp @@ -88,15 +88,15 @@ namespace YAML // now split based on what kind of node we should be switch(pScanner->peek().type) { - case TT_SCALAR: + case Token::SCALAR: m_pContent = new Scalar; break; - case TT_FLOW_SEQ_START: - case TT_BLOCK_SEQ_START: + case Token::FLOW_SEQ_START: + case Token::BLOCK_SEQ_START: m_pContent = new Sequence; break; - case TT_FLOW_MAP_START: - case TT_BLOCK_MAP_START: + case Token::FLOW_MAP_START: + case Token::BLOCK_MAP_START: m_pContent = new Map; break; default: @@ -124,9 +124,9 @@ namespace YAML return; switch(pScanner->peek().type) { - case TT_TAG: ParseTag(pScanner, state); break; - case TT_ANCHOR: ParseAnchor(pScanner, state); break; - case TT_ALIAS: ParseAlias(pScanner, state); break; + case Token::TAG: ParseTag(pScanner, state); break; + case Token::ANCHOR: ParseAnchor(pScanner, state); break; + case Token::ALIAS: ParseAlias(pScanner, state); break; default: return; } } diff --git a/src/parser.cpp b/src/parser.cpp index e3b088b..162364f 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -46,14 +46,14 @@ namespace YAML return; // first eat doc start (optional) - if(m_pScanner->peek().type == TT_DOC_START) + if(m_pScanner->peek().type == Token::DOC_START) m_pScanner->pop(); // now parse our root node document.Parse(m_pScanner, m_state); // and finally eat any doc ends we see - while(!m_pScanner->empty() && m_pScanner->peek().type == TT_DOC_END) + while(!m_pScanner->empty() && m_pScanner->peek().type == Token::DOC_END) m_pScanner->pop(); // clear anchors from the scanner, which are no longer relevant @@ -71,7 +71,7 @@ namespace YAML break; Token& token = m_pScanner->peek(); - if(token.type != TT_DIRECTIVE) + if(token.type != Token::DIRECTIVE) break; // we keep the directives from the last document if none are specified; diff --git a/src/scanner.cpp b/src/scanner.cpp index 407abf2..988ed43 100644 --- a/src/scanner.cpp +++ b/src/scanner.cpp @@ -31,7 +31,7 @@ namespace YAML EnsureTokensInQueue(); if(!m_tokens.empty()) { // Saved anchors shouldn't survive popping the document end marker - if (m_tokens.front().type == TT_DOC_END) { + if (m_tokens.front().type == Token::DOC_END) { ClearAnchors(); } m_tokens.pop(); @@ -60,11 +60,11 @@ namespace YAML Token& token = m_tokens.front(); // if this guy's valid, then we're done - if(token.status == TS_VALID) + if(token.status == Token::VALID) return; // here's where we clean up the impossible tokens - if(token.status == TS_INVALID) { + if(token.status == Token::INVALID) { m_tokens.pop(); continue; } @@ -263,9 +263,9 @@ namespace YAML // now push m_indents.push(indent); if(type == IndentMarker::SEQ) - m_tokens.push(Token(TT_BLOCK_SEQ_START, INPUT.mark())); + m_tokens.push(Token(Token::BLOCK_SEQ_START, INPUT.mark())); else if(type == IndentMarker::MAP) - m_tokens.push(Token(TT_BLOCK_MAP_START, INPUT.mark())); + m_tokens.push(Token(Token::BLOCK_MAP_START, INPUT.mark())); else assert(false); @@ -319,9 +319,9 @@ namespace YAML IndentMarker::INDENT_TYPE type = m_indents.top().type; m_indents.pop(); if(type == IndentMarker::SEQ) - m_tokens.push(Token(TT_BLOCK_SEQ_END, INPUT.mark())); + m_tokens.push(Token(Token::BLOCK_SEQ_END, INPUT.mark())); else if(type == IndentMarker::MAP) - m_tokens.push(Token(TT_BLOCK_MAP_END, INPUT.mark())); + m_tokens.push(Token(Token::BLOCK_MAP_END, INPUT.mark())); } // GetTopIndent diff --git a/src/scantoken.cpp b/src/scantoken.cpp index 9f1fda1..fa853f7 100644 --- a/src/scantoken.cpp +++ b/src/scantoken.cpp @@ -50,7 +50,7 @@ namespace YAML params.push_back(param); } - Token token(TT_DIRECTIVE, mark); + Token token(Token::DIRECTIVE, mark); token.value = name; token.params = params; m_tokens.push(token); @@ -66,7 +66,7 @@ namespace YAML // eat Mark mark = INPUT.mark(); INPUT.eat(3); - m_tokens.push(Token(TT_DOC_START, mark)); + m_tokens.push(Token(Token::DOC_START, mark)); } // DocEnd @@ -79,7 +79,7 @@ namespace YAML // eat Mark mark = INPUT.mark(); INPUT.eat(3); - m_tokens.push(Token(TT_DOC_END, mark)); + m_tokens.push(Token(Token::DOC_END, mark)); } // FlowStart @@ -93,7 +93,7 @@ namespace YAML // eat Mark mark = INPUT.mark(); char ch = INPUT.get(); - TOKEN_TYPE type = (ch == Keys::FlowSeqStart ? TT_FLOW_SEQ_START : TT_FLOW_MAP_START); + Token::TYPE type = (ch == Keys::FlowSeqStart ? Token::FLOW_SEQ_START : Token::FLOW_MAP_START); m_tokens.push(Token(type, mark)); } @@ -109,7 +109,7 @@ namespace YAML // eat Mark mark = INPUT.mark(); char ch = INPUT.get(); - TOKEN_TYPE type = (ch == Keys::FlowSeqEnd ? TT_FLOW_SEQ_END : TT_FLOW_MAP_END); + Token::TYPE type = (ch == Keys::FlowSeqEnd ? Token::FLOW_SEQ_END : Token::FLOW_MAP_END); m_tokens.push(Token(type, mark)); } @@ -121,7 +121,7 @@ namespace YAML // eat Mark mark = INPUT.mark(); INPUT.eat(1); - m_tokens.push(Token(TT_FLOW_ENTRY, mark)); + m_tokens.push(Token(Token::FLOW_ENTRY, mark)); } // BlockEntry @@ -141,7 +141,7 @@ namespace YAML // eat Mark mark = INPUT.mark(); INPUT.eat(1); - m_tokens.push(Token(TT_BLOCK_ENTRY, mark)); + m_tokens.push(Token(Token::BLOCK_ENTRY, mark)); } // Key @@ -164,7 +164,7 @@ namespace YAML // eat Mark mark = INPUT.mark(); INPUT.eat(1); - m_tokens.push(Token(TT_KEY, mark)); + m_tokens.push(Token(Token::KEY, mark)); } // Value @@ -199,7 +199,7 @@ namespace YAML // eat Mark mark = INPUT.mark(); INPUT.eat(1); - m_tokens.push(Token(TT_VALUE, mark)); + m_tokens.push(Token(Token::VALUE, mark)); } // AnchorOrAlias @@ -231,7 +231,7 @@ namespace YAML throw ParserException(INPUT.mark(), alias ? ErrorMsg::CHAR_IN_ALIAS : ErrorMsg::CHAR_IN_ANCHOR); // and we're done - Token token(alias ? TT_ALIAS : TT_ANCHOR, mark); + Token token(alias ? Token::ALIAS : Token::ANCHOR, mark); token.value = name; m_tokens.push(token); } @@ -268,7 +268,7 @@ namespace YAML handle = "!"; } - Token token(TT_TAG, mark); + Token token(Token::TAG, mark); token.value = handle; token.params.push_back(suffix); m_tokens.push(token); @@ -305,7 +305,7 @@ namespace YAML //if(Exp::IllegalCharInScalar.Matches(INPUT)) // throw ParserException(INPUT.mark(), ErrorMsg::CHAR_IN_SCALAR); - Token token(TT_SCALAR, mark); + Token token(Token::SCALAR, mark); token.value = scalar; m_tokens.push(token); } @@ -344,7 +344,7 @@ namespace YAML scalar = ScanScalar(INPUT, params); m_simpleKeyAllowed = false; - Token token(TT_SCALAR, mark); + Token token(Token::SCALAR, mark); token.value = scalar; m_tokens.push(token); } @@ -409,7 +409,7 @@ namespace YAML // simple keys always ok after block scalars (since we're gonna start a new line anyways) m_simpleKeyAllowed = true; - Token token(TT_SCALAR, mark); + Token token(Token::SCALAR, mark); token.value = scalar; m_tokens.push(token); } diff --git a/src/sequence.cpp b/src/sequence.cpp index 31c88e1..ba38e2c 100644 --- a/src/sequence.cpp +++ b/src/sequence.cpp @@ -66,8 +66,8 @@ namespace YAML // split based on start token switch(pScanner->peek().type) { - case TT_BLOCK_SEQ_START: ParseBlock(pScanner, state); break; - case TT_FLOW_SEQ_START: ParseFlow(pScanner, state); break; + case Token::BLOCK_SEQ_START: ParseBlock(pScanner, state); break; + case Token::FLOW_SEQ_START: ParseFlow(pScanner, state); break; default: break; } } @@ -82,11 +82,11 @@ namespace YAML throw ParserException(Mark::null(), ErrorMsg::END_OF_SEQ); Token token = pScanner->peek(); - if(token.type != TT_BLOCK_ENTRY && token.type != TT_BLOCK_SEQ_END) + if(token.type != Token::BLOCK_ENTRY && token.type != Token::BLOCK_SEQ_END) throw ParserException(token.mark, ErrorMsg::END_OF_SEQ); pScanner->pop(); - if(token.type == TT_BLOCK_SEQ_END) + if(token.type == Token::BLOCK_SEQ_END) break; Node *pNode = new Node; @@ -95,7 +95,7 @@ namespace YAML // check for null if(!pScanner->empty()) { const Token& token = pScanner->peek(); - if(token.type == TT_BLOCK_ENTRY || token.type == TT_BLOCK_SEQ_END) + if(token.type == Token::BLOCK_ENTRY || token.type == Token::BLOCK_SEQ_END) continue; } @@ -113,7 +113,7 @@ namespace YAML throw ParserException(Mark::null(), ErrorMsg::END_OF_SEQ_FLOW); // first check for end - if(pScanner->peek().type == TT_FLOW_SEQ_END) { + if(pScanner->peek().type == Token::FLOW_SEQ_END) { pScanner->pop(); break; } @@ -125,9 +125,9 @@ namespace YAML // now eat the separator (or could be a sequence end, which we ignore - but if it's neither, then it's a bad node) Token& token = pScanner->peek(); - if(token.type == TT_FLOW_ENTRY) + if(token.type == Token::FLOW_ENTRY) pScanner->pop(); - else if(token.type != TT_FLOW_SEQ_END) + else if(token.type != Token::FLOW_SEQ_END) throw ParserException(token.mark, ErrorMsg::END_OF_SEQ_FLOW); } } diff --git a/src/simplekey.cpp b/src/simplekey.cpp index 85dc7f8..bdfbda4 100644 --- a/src/simplekey.cpp +++ b/src/simplekey.cpp @@ -14,17 +14,17 @@ namespace YAML void Scanner::SimpleKey::Validate() { if(pMapStart) - pMapStart->status = TS_VALID; + pMapStart->status = Token::VALID; if(pKey) - pKey->status = TS_VALID; + pKey->status = Token::VALID; } void Scanner::SimpleKey::Invalidate() { if(pMapStart) - pMapStart->status = TS_INVALID; + pMapStart->status = Token::INVALID; if(pKey) - pKey->status = TS_INVALID; + pKey->status = Token::INVALID; } // InsertSimpleKey @@ -37,12 +37,12 @@ namespace YAML // first add a map start, if necessary key.pMapStart = PushIndentTo(INPUT.column(), IndentMarker::MAP); if(key.pMapStart) - key.pMapStart->status = TS_UNVERIFIED; + key.pMapStart->status = Token::UNVERIFIED; // then add the (now unverified) key - m_tokens.push(Token(TT_KEY, INPUT.mark())); + m_tokens.push(Token(Token::KEY, INPUT.mark())); key.pKey = &m_tokens.back(); - key.pKey->status = TS_UNVERIFIED; + key.pKey->status = Token::UNVERIFIED; m_simpleKeys.push(key); } diff --git a/src/token.h b/src/token.h index c21524d..8ab82d5 100644 --- a/src/token.h +++ b/src/token.h @@ -11,29 +11,6 @@ namespace YAML { - enum TOKEN_STATUS { TS_VALID, TS_INVALID, TS_UNVERIFIED }; - enum TOKEN_TYPE { - TT_DIRECTIVE, - TT_DOC_START, - TT_DOC_END, - TT_BLOCK_SEQ_START, - TT_BLOCK_MAP_START, - TT_BLOCK_SEQ_END, - TT_BLOCK_MAP_END, - TT_BLOCK_ENTRY, - TT_FLOW_SEQ_START, - TT_FLOW_MAP_START, - TT_FLOW_SEQ_END, - TT_FLOW_MAP_END, - TT_FLOW_ENTRY, - TT_KEY, - TT_VALUE, - TT_ANCHOR, - TT_ALIAS, - TT_TAG, - TT_SCALAR - }; - const std::string TokenNames[] = { "DIRECTIVE", "DOC_START", @@ -57,7 +34,32 @@ namespace YAML }; struct Token { - Token(TOKEN_TYPE type_, const Mark& mark_): status(TS_VALID), type(type_), mark(mark_) {} + // enums + enum STATUS { VALID, INVALID, UNVERIFIED }; + enum TYPE { + DIRECTIVE, + DOC_START, + DOC_END, + BLOCK_SEQ_START, + BLOCK_MAP_START, + BLOCK_SEQ_END, + BLOCK_MAP_END, + BLOCK_ENTRY, + FLOW_SEQ_START, + FLOW_MAP_START, + FLOW_SEQ_END, + FLOW_MAP_END, + FLOW_ENTRY, + KEY, + VALUE, + ANCHOR, + ALIAS, + TAG, + SCALAR + }; + + // data + Token(TYPE type_, const Mark& mark_): status(VALID), type(type_), mark(mark_) {} friend std::ostream& operator << (std::ostream& out, const Token& token) { out << TokenNames[token.type] << std::string(": ") << token.value; @@ -66,8 +68,8 @@ namespace YAML return out; } - TOKEN_STATUS status; - TOKEN_TYPE type; + STATUS status; + TYPE type; Mark mark; std::string value; std::vector params;