Renamed the stream member functions get() and eat().

This commit is contained in:
Jesse Beder 2008-06-30 22:34:10 +00:00
parent 852e5b63e5
commit c1966ba3fc
8 changed files with 143 additions and 73 deletions

View file

@ -39,7 +39,7 @@ namespace YAML
// grab string // grab string
std::string str; std::string str;
for(int i=0;i<codeLength;i++) for(int i=0;i<codeLength;i++)
str += in.GetChar(); str += in.get();
// get the value // get the value
unsigned value = ParseHex(str); unsigned value = ParseHex(str);
@ -67,10 +67,10 @@ namespace YAML
std::string Escape(Stream& in) std::string Escape(Stream& in)
{ {
// eat slash // eat slash
char escape = in.GetChar(); char escape = in.get();
// switch on escape character // switch on escape character
char ch = in.GetChar(); char ch = in.get();
// first do single quote, since it's easier // first do single quote, since it's easier
if(escape == '\'' && ch == '\'') if(escape == '\'' && ch == '\'')

View file

@ -153,13 +153,13 @@ namespace YAML
while(1) { while(1) {
// first eat whitespace // first eat whitespace
while(IsWhitespaceToBeEaten(INPUT.peek())) while(IsWhitespaceToBeEaten(INPUT.peek()))
INPUT.Eat(1); INPUT.eat(1);
// then eat a comment // then eat a comment
if(Exp::Comment.Matches(INPUT)) { if(Exp::Comment.Matches(INPUT)) {
// eat until line break // eat until line break
while(INPUT && !Exp::Break.Matches(INPUT)) while(INPUT && !Exp::Break.Matches(INPUT))
INPUT.Eat(1); INPUT.eat(1);
} }
// if it's NOT a line break, then we're done! // if it's NOT a line break, then we're done!
@ -168,7 +168,7 @@ namespace YAML
// otherwise, let's eat the line break and keep going // otherwise, let's eat the line break and keep going
int n = Exp::Break.Match(INPUT); int n = Exp::Break.Match(INPUT);
INPUT.Eat(n); INPUT.eat(n);
// oh yeah, and let's get rid of that simple key // oh yeah, and let's get rid of that simple key
VerifySimpleKey(); VerifySimpleKey();

View file

@ -5,7 +5,6 @@
#include <queue> #include <queue>
#include <stack> #include <stack>
#include <set> #include <set>
#include "regex.h"
#include "stream.h" #include "stream.h"
namespace YAML namespace YAML

View file

@ -43,7 +43,7 @@ namespace YAML
// escaped newline? (only if we're escaping on slash) // escaped newline? (only if we're escaping on slash)
if(params.escape == '\\' && Exp::EscBreak.Matches(INPUT)) { if(params.escape == '\\' && Exp::EscBreak.Matches(INPUT)) {
int n = Exp::EscBreak.Match(INPUT); int n = Exp::EscBreak.Match(INPUT);
INPUT.Eat(n); INPUT.eat(n);
continue; continue;
} }
@ -54,7 +54,7 @@ namespace YAML
} }
// otherwise, just add the damn character // otherwise, just add the damn character
scalar += INPUT.GetChar(); scalar += INPUT.get();
} }
// eof? if we're looking to eat something, then we throw // eof? if we're looking to eat something, then we throw
@ -72,21 +72,21 @@ namespace YAML
int n = params.end.Match(INPUT); int n = params.end.Match(INPUT);
if(n >= 0) { if(n >= 0) {
if(params.eatEnd) if(params.eatEnd)
INPUT.Eat(n); INPUT.eat(n);
break; break;
} }
// ******************************** // ********************************
// Phase #2: eat line ending // Phase #2: eat line ending
n = Exp::Break.Match(INPUT); n = Exp::Break.Match(INPUT);
INPUT.Eat(n); INPUT.eat(n);
// ******************************** // ********************************
// Phase #3: scan initial spaces // Phase #3: scan initial spaces
// first the required indentation // first the required indentation
while(INPUT.peek() == ' ' && (INPUT.column < params.indent || (params.detectIndent && !foundNonEmptyLine))) while(INPUT.peek() == ' ' && (INPUT.column < params.indent || (params.detectIndent && !foundNonEmptyLine)))
INPUT.Eat(1); INPUT.eat(1);
// update indent if we're auto-detecting // update indent if we're auto-detecting
if(params.detectIndent && !foundNonEmptyLine) if(params.detectIndent && !foundNonEmptyLine)
@ -101,7 +101,7 @@ namespace YAML
if(!params.eatLeadingWhitespace) if(!params.eatLeadingWhitespace)
break; break;
INPUT.Eat(1); INPUT.eat(1);
} }
// was this an empty line? // was this an empty line?

View file

@ -23,17 +23,17 @@ namespace YAML
m_simpleKeyAllowed = false; m_simpleKeyAllowed = false;
// eat indicator // eat indicator
INPUT.Eat(1); INPUT.eat(1);
// read name // read name
while(INPUT.peek() != EOF && !Exp::BlankOrBreak.Matches(INPUT)) while(INPUT.peek() != EOF && !Exp::BlankOrBreak.Matches(INPUT))
name += INPUT.GetChar(); name += INPUT.get();
// read parameters // read parameters
while(1) { while(1) {
// first get rid of whitespace // first get rid of whitespace
while(Exp::Blank.Matches(INPUT)) while(Exp::Blank.Matches(INPUT))
INPUT.Eat(1); INPUT.eat(1);
// break on newline or comment // break on newline or comment
if(INPUT.peek() == EOF || Exp::Break.Matches(INPUT) || Exp::Comment.Matches(INPUT)) if(INPUT.peek() == EOF || Exp::Break.Matches(INPUT) || Exp::Comment.Matches(INPUT))
@ -42,7 +42,7 @@ namespace YAML
// now read parameter // now read parameter
std::string param; std::string param;
while(INPUT.peek() != EOF && !Exp::BlankOrBreak.Matches(INPUT)) while(INPUT.peek() != EOF && !Exp::BlankOrBreak.Matches(INPUT))
param += INPUT.GetChar(); param += INPUT.get();
params.push_back(param); params.push_back(param);
} }
@ -61,7 +61,7 @@ namespace YAML
m_simpleKeyAllowed = false; m_simpleKeyAllowed = false;
// eat // eat
INPUT.Eat(3); INPUT.eat(3);
m_tokens.push(new Token(TT_DOC_START)); m_tokens.push(new Token(TT_DOC_START));
} }
@ -73,7 +73,7 @@ namespace YAML
m_simpleKeyAllowed = false; m_simpleKeyAllowed = false;
// eat // eat
INPUT.Eat(3); INPUT.eat(3);
m_tokens.push(new Token(TT_DOC_END)); m_tokens.push(new Token(TT_DOC_END));
} }
@ -86,7 +86,7 @@ namespace YAML
m_simpleKeyAllowed = true; m_simpleKeyAllowed = true;
// eat // eat
char ch = INPUT.GetChar(); char ch = INPUT.get();
TOKEN_TYPE type = (ch == Keys::FlowSeqStart ? TT_FLOW_SEQ_START : TT_FLOW_MAP_START); TOKEN_TYPE type = (ch == Keys::FlowSeqStart ? TT_FLOW_SEQ_START : TT_FLOW_MAP_START);
m_tokens.push(new Token(type)); m_tokens.push(new Token(type));
} }
@ -101,7 +101,7 @@ namespace YAML
m_simpleKeyAllowed = false; m_simpleKeyAllowed = false;
// eat // eat
char ch = INPUT.GetChar(); char ch = INPUT.get();
TOKEN_TYPE type = (ch == Keys::FlowSeqEnd ? TT_FLOW_SEQ_END : TT_FLOW_MAP_END); TOKEN_TYPE type = (ch == Keys::FlowSeqEnd ? TT_FLOW_SEQ_END : TT_FLOW_MAP_END);
m_tokens.push(new Token(type)); m_tokens.push(new Token(type));
} }
@ -112,7 +112,7 @@ namespace YAML
m_simpleKeyAllowed = true; m_simpleKeyAllowed = true;
// eat // eat
INPUT.Eat(1); INPUT.eat(1);
m_tokens.push(new Token(TT_FLOW_ENTRY)); m_tokens.push(new Token(TT_FLOW_ENTRY));
} }
@ -131,7 +131,7 @@ namespace YAML
m_simpleKeyAllowed = true; m_simpleKeyAllowed = true;
// eat // eat
INPUT.Eat(1); INPUT.eat(1);
m_tokens.push(new Token(TT_BLOCK_ENTRY)); m_tokens.push(new Token(TT_BLOCK_ENTRY));
} }
@ -153,7 +153,7 @@ namespace YAML
m_simpleKeyAllowed = false; m_simpleKeyAllowed = false;
// eat // eat
INPUT.Eat(1); INPUT.eat(1);
m_tokens.push(new Token(TT_KEY)); m_tokens.push(new Token(TT_KEY));
} }
@ -181,7 +181,7 @@ namespace YAML
} }
// eat // eat
INPUT.Eat(1); INPUT.eat(1);
m_tokens.push(new Token(TT_VALUE)); m_tokens.push(new Token(TT_VALUE));
} }
@ -197,12 +197,12 @@ namespace YAML
m_simpleKeyAllowed = false; m_simpleKeyAllowed = false;
// eat the indicator // eat the indicator
char indicator = INPUT.GetChar(); char indicator = INPUT.get();
alias = (indicator == Keys::Alias); alias = (indicator == Keys::Alias);
// now eat the content // now eat the content
while(Exp::AlphaNumeric.Matches(INPUT)) while(Exp::AlphaNumeric.Matches(INPUT))
tag += INPUT.GetChar(); tag += INPUT.get();
// we need to have read SOMETHING! // we need to have read SOMETHING!
if(tag.empty()) if(tag.empty())
@ -229,20 +229,20 @@ namespace YAML
m_simpleKeyAllowed = false; m_simpleKeyAllowed = false;
// eat the indicator // eat the indicator
INPUT.Eat(1); INPUT.eat(1);
// read the handle // read the handle
while(INPUT.peek() != EOF && INPUT.peek() != Keys::Tag && !Exp::BlankOrBreak.Matches(INPUT)) while(INPUT.peek() != EOF && INPUT.peek() != Keys::Tag && !Exp::BlankOrBreak.Matches(INPUT))
handle += INPUT.GetChar(); handle += INPUT.get();
// is there a suffix? // is there a suffix?
if(INPUT.peek() == Keys::Tag) { if(INPUT.peek() == Keys::Tag) {
// eat the indicator // eat the indicator
INPUT.Eat(1); INPUT.eat(1);
// then read it // then read it
while(INPUT.peek() != EOF && !Exp::BlankOrBreak.Matches(INPUT)) while(INPUT.peek() != EOF && !Exp::BlankOrBreak.Matches(INPUT))
suffix += INPUT.GetChar(); suffix += INPUT.get();
} }
Token *pToken = new Token(TT_TAG); Token *pToken = new Token(TT_TAG);
@ -293,7 +293,7 @@ namespace YAML
std::string scalar; std::string scalar;
// eat single or double quote // eat single or double quote
char quote = INPUT.GetChar(); char quote = INPUT.get();
bool single = (quote == '\''); bool single = (quote == '\'');
// setup the scanning parameters // setup the scanning parameters
@ -333,13 +333,13 @@ namespace YAML
params.detectIndent = true; params.detectIndent = true;
// eat block indicator ('|' or '>') // eat block indicator ('|' or '>')
char indicator = INPUT.GetChar(); char indicator = INPUT.get();
params.fold = (indicator == Keys::FoldedScalar); params.fold = (indicator == Keys::FoldedScalar);
// eat chomping/indentation indicators // eat chomping/indentation indicators
int n = Exp::Chomp.Match(INPUT); int n = Exp::Chomp.Match(INPUT);
for(int i=0;i<n;i++) { for(int i=0;i<n;i++) {
char ch = INPUT.GetChar(); char ch = INPUT.get();
if(ch == '+') if(ch == '+')
params.chomp = KEEP; params.chomp = KEEP;
else if(ch == '-') else if(ch == '-')
@ -355,12 +355,12 @@ namespace YAML
// now eat whitespace // now eat whitespace
while(Exp::Blank.Matches(INPUT)) while(Exp::Blank.Matches(INPUT))
INPUT.Eat(1); INPUT.eat(1);
// and comments to the end of the line // and comments to the end of the line
if(Exp::Comment.Matches(INPUT)) if(Exp::Comment.Matches(INPUT))
while(INPUT && !Exp::Break.Matches(INPUT)) while(INPUT && !Exp::Break.Matches(INPUT))
INPUT.Eat(1); INPUT.eat(1);
// if it's not a line break, then we ran into a bad character inline // if it's not a line break, then we ran into a bad character inline
if(INPUT && !Exp::Break.Matches(INPUT)) if(INPUT && !Exp::Break.Matches(INPUT))

View file

@ -2,9 +2,9 @@
namespace YAML namespace YAML
{ {
// GetChar // get
// . Extracts a character from the stream and updates our position // . Extracts a character from the stream and updates our position
char Stream::GetChar() char Stream::get()
{ {
char ch = input.get(); char ch = input.get();
column++; column++;
@ -15,21 +15,21 @@ namespace YAML
return ch; return ch;
} }
// GetChar // get
// . Extracts 'n' characters from the stream and updates our position // . Extracts 'n' characters from the stream and updates our position
std::string Stream::GetChar(int n) std::string Stream::get(int n)
{ {
std::string ret; std::string ret;
for(int i=0;i<n;i++) for(int i=0;i<n;i++)
ret += GetChar(); ret += get();
return ret; return ret;
} }
// Eat // eat
// . Eats 'n' characters and updates our position. // . Eats 'n' characters and updates our position.
void Stream::Eat(int n) void Stream::eat(int n)
{ {
for(int i=0;i<n;i++) for(int i=0;i<n;i++)
GetChar(); get();
} }
} }

View file

@ -9,15 +9,15 @@ namespace YAML
{ {
Stream(std::istream& input_): input(input_), line(0), column(0) {} Stream(std::istream& input_): input(input_), line(0), column(0) {}
char peek() { return input.peek(); }
int pos() const { return input.tellg(); } int pos() const { return input.tellg(); }
operator std::istream& () { return input; } operator std::istream& () { return input; }
operator bool() { return input.good(); } operator bool() { return input.good(); }
bool operator !() { return !input; } bool operator !() { return !input; }
char GetChar(); char peek() { return input.peek(); }
std::string GetChar(int n); char get();
void Eat(int n = 1); std::string get(int n);
void eat(int n = 1);
std::istream& input; std::istream& input;
int line, column; int line, column;

121
test.yaml
View file

@ -1,26 +1,97 @@
--- ---
Time: 2001-11-23 15:01:42 -5 model:
User: ed file: data/models/compound.model
Warning: textures: data/materials/compound
This is an error message rooms:
for the log file - name: "Room #1"
--- pos: [0, 0, 0]
Time: 2001-11-23 15:02:31 -5 size: [1000, 1000, 500]
User: ed height: 500
Warning: stairtype: none
A slightly different error display: []
message. pathfinding:
--- tilesize: 50
Date: 2001-11-23 15:03:17 -5 size: [24, 24]
User: ed map: |
Fatal: -----------------------
Unknown variable "bar" -+++++++++++++++++++++-
Stack: -+-------------------+-
- file: TopClass.py -+-------------------+-
line: 23 -+-------------------+-
code: | -+-------------------+-
x = MoreObject("345\n") -+-------------------+-
- file: MoreClass.py -+-------------------+-
line: 58 -+-------------------+-
code: |- -+-------------------+-
foo = bar -+---------------------
-+---------------------
-+---------------------
-+---------------------
-+-------------------+-
-+-------------------+-
-+-------------------+-
-+-------------------+-
-+-------------------+-
-+-------------------+-
-+-------------------+-
-+-------------------+-
-+++++++++++++++++++++-
-----------------------
- name: Doorway
pos: [1000, 400, 0]
size: [50, 200, 500]
height: 500
stairtype: none
display: []
pathfinding:
tilesize: 50
size: [5, 9]
map: |
-----
-+++-
-----
-----
-----
-----
-----
-+++-
-----
- name: "Room #2"
pos: [1050, 0, 0]
size: [1000, 1000, 500]
height: 500
stairtype: none
display: []
pathfinding:
tilesize: 50
size: [24, 24]
map: |
-----------------------
-+++++++++++++++++++++-
-+-------------------+-
-+-------------------+-
-+-------------------+-
-+-------------------+-
-+-------------------+-
-+-------------------+-
-+-------------------+-
-+-------------------+-
---------------------+-
---------------------+-
---------------------+-
---------------------+-
-+-------------------+-
-+-------------------+-
-+-------------------+-
-+-------------------+-
-+-------------------+-
-+-------------------+-
-+-------------------+-
-+-------------------+-
-+++++++++++++++++++++-
-----------------------
exits:
- room1: "Room #1"
room2: "Room #2"
dir: e
pos: [400, 600]