Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
gaoqiong
yaml-cpp
Commits
b6a0ef20
Commit
b6a0ef20
authored
Jun 30, 2008
by
Jesse Beder
Browse files
Started the parser.
parent
ed6c2947
Changes
12
Show whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
143 additions
and
276 deletions
+143
-276
document.cpp
document.cpp
+0
-33
document.h
document.h
+0
-4
main.cpp
main.cpp
+6
-3
node.cpp
node.cpp
+0
-11
node.h
node.h
+0
-2
parser.cpp
parser.cpp
+16
-124
parser.h
parser.h
+4
-36
reader.cpp
reader.cpp
+21
-0
reader.h
reader.h
+21
-0
sequence.cpp
sequence.cpp
+2
-11
sequence.h
sequence.h
+1
-4
yaml-reader.vcproj
yaml-reader.vcproj
+72
-48
No files found.
document.cpp
View file @
b6a0ef20
#include "document.h"
#include "document.h"
#include "node.h"
#include "node.h"
#include "parser.h"
#include "scanner.h"
#include "exceptions.h"
#include <fstream>
#include <iostream>
#include "token.h"
namespace
YAML
namespace
YAML
{
{
...
@@ -14,11 +7,6 @@ namespace YAML
...
@@ -14,11 +7,6 @@ namespace YAML
{
{
}
}
Document
::
Document
(
const
std
::
string
&
fileName
)
:
m_pRoot
(
0
)
{
Load
(
fileName
);
}
Document
::~
Document
()
Document
::~
Document
()
{
{
Clear
();
Clear
();
...
@@ -29,25 +17,4 @@ namespace YAML
...
@@ -29,25 +17,4 @@ namespace YAML
delete
m_pRoot
;
delete
m_pRoot
;
m_pRoot
=
0
;
m_pRoot
=
0
;
}
}
void
Document
::
Load
(
const
std
::
string
&
fileName
)
{
Clear
();
std
::
ifstream
fin
(
fileName
.
c_str
());
Scanner
scanner
(
fin
);
// scan and output, for now
while
(
1
)
{
Token
*
pToken
=
scanner
.
GetNextToken
();
if
(
!
pToken
)
break
;
std
::
cout
<<
typeid
(
*
pToken
).
name
()
<<
": "
<<
*
pToken
<<
std
::
endl
;
delete
pToken
;
}
getchar
();
// m_pRoot = parser.ReadNextNode();
}
}
}
document.h
View file @
b6a0ef20
#pragma once
#pragma once
#include <string>
namespace
YAML
namespace
YAML
{
{
class
Node
;
class
Node
;
...
@@ -10,11 +8,9 @@ namespace YAML
...
@@ -10,11 +8,9 @@ namespace YAML
{
{
public:
public:
Document
();
Document
();
Document
(
const
std
::
string
&
fileName
);
~
Document
();
~
Document
();
void
Clear
();
void
Clear
();
void
Load
(
const
std
::
string
&
fileName
);
private:
private:
Node
*
m_pRoot
;
Node
*
m_pRoot
;
...
...
main.cpp
View file @
b6a0ef20
#include "
document
.h"
#include "
reader
.h"
#include
"regex.h"
#include
<fstream>
int
main
()
int
main
()
{
{
YAML
::
Document
doc
(
"test.yaml"
);
std
::
ifstream
fin
(
"test.yaml"
);
YAML
::
Reader
reader
(
fin
);
YAML
::
Document
doc
;
reader
.
GetNextDocument
(
doc
);
return
0
;
return
0
;
}
}
\ No newline at end of file
node.cpp
View file @
b6a0ef20
...
@@ -20,15 +20,4 @@ namespace YAML
...
@@ -20,15 +20,4 @@ namespace YAML
delete
m_pContent
;
delete
m_pContent
;
m_pContent
=
0
;
m_pContent
=
0
;
}
}
void
Node
::
Read
(
Parser
*
pParser
,
const
std
::
string
&
token
)
{
Clear
();
if
(
token
==
std
::
string
(
""
)
+
SeqToken
)
{
m_pContent
=
new
Sequence
(
pParser
);
}
else
{
m_pContent
=
new
Scalar
(
token
);
}
}
}
}
node.h
View file @
b6a0ef20
...
@@ -10,7 +10,6 @@ namespace YAML
...
@@ -10,7 +10,6 @@ namespace YAML
const
std
::
string
MapTag
=
"!!map"
;
const
std
::
string
MapTag
=
"!!map"
;
class
Content
;
class
Content
;
class
Parser
;
class
Node
class
Node
{
{
...
@@ -19,7 +18,6 @@ namespace YAML
...
@@ -19,7 +18,6 @@ namespace YAML
~
Node
();
~
Node
();
void
Clear
();
void
Clear
();
void
Read
(
Parser
*
pParser
,
const
std
::
string
&
token
);
private:
private:
std
::
string
m_tag
;
std
::
string
m_tag
;
...
...
parser.cpp
View file @
b6a0ef20
#include "parser.h"
#include "parser.h"
#include "node.h"
#include "node.h"
#include "token.h"
#include <iostream>
namespace
YAML
namespace
YAML
{
{
Parser
::
Parser
(
std
::
istream
&
in
)
:
INPUT
(
in
),
m_ok
(
true
)
Parser
::
Parser
(
std
::
istream
&
in
)
:
m_scanner
(
in
)
{
{
m_state
.
push
(
State
(
C_BLOCK
,
-
1
,
true
));
// eat the stream start token
// TODO: check?
// read header
Token
*
pToken
=
m_scanner
.
GetNextToken
();
std
::
string
token
=
ReadNextToken
();
if
(
token
!=
DocStart
)
m_ok
=
false
;
}
}
Parser
::~
Parser
()
Parser
::~
Parser
()
{
{
}
}
Parser
::
operator
bool
()
const
void
Parser
::
GetNextDocument
(
Document
&
document
)
{
{
return
m_ok
;
// scan and output, for now
}
while
(
1
)
{
Token
*
pToken
=
m_scanner
.
GetNextToken
();
if
(
!
pToken
)
break
;
bool
Parser
::
operator
!
()
const
std
::
cout
<<
typeid
(
*
pToken
).
name
()
<<
": "
<<
*
pToken
<<
std
::
endl
;
{
delete
pToken
;
return
!
m_ok
;
}
}
getchar
();
bool
Parser
::
IsWhitespace
(
char
ch
)
{
return
(
ch
==
' '
||
ch
==
'\n'
||
ch
==
'\r'
||
ch
==
'\t'
);
}
void
Parser
::
Putback
(
const
std
::
string
&
str
)
{
for
(
int
i
=
str
.
size
()
-
1
;
i
>=
0
;
i
--
)
INPUT
.
putback
(
str
[
i
]);
}
// StringWhitespace
// . Strips up to n whitespace characters (or as many
// as there are, if n is -1)
void
Parser
::
StripWhitespace
(
int
n
)
{
while
(
--
n
>=
0
&&
IsWhitespace
(
INPUT
.
peek
()))
INPUT
.
get
();
}
int
Parser
::
GetNumOfSpaces
()
{
// get 'em out
int
n
=
0
;
while
(
INPUT
.
peek
()
==
' '
)
{
INPUT
.
get
();
n
++
;
}
// put 'em back
for
(
int
i
=
0
;
i
<
n
;
i
++
)
INPUT
.
putback
(
' '
);
return
n
;
}
// SeqContinues
// . Returns true if the next item to be read is a continuation of the current sequence.
bool
Parser
::
SeqContinues
()
{
const
State
&
state
=
m_state
.
top
();
if
(
state
.
context
!=
C_BLOCK
)
return
false
;
int
n
=
GetNumOfSpaces
();
return
(
n
==
state
.
indent
);
}
// ReadNextNode
// . Reads and returns the next node from the current input (to be used recursively).
// . The caller is responsible for cleanup!
Node
*
Parser
::
ReadNextNode
()
{
if
(
!
INPUT
)
return
0
;
std
::
string
token
=
ReadNextToken
();
if
(
token
==
DocStart
||
token
==
DocEnd
)
return
0
;
// TODO: putback DocStart?
Node
*
pNode
=
new
Node
;
pNode
->
Read
(
this
,
token
);
return
pNode
;
}
// ReadNextToken
// . Reads:
// . If the first character is non-whitespace, non-special token, then until
// the end of this scalar.
std
::
string
Parser
::
ReadNextToken
()
{
const
State
&
state
=
m_state
.
top
();
if
(
state
.
startingNewLine
)
{
int
n
=
GetNumOfSpaces
();
StripWhitespace
(
n
);
if
(
n
>
state
.
indent
)
{
m_state
.
push
(
State
(
C_BLOCK
,
n
,
true
));
};
while
(
m_state
.
top
().
startingNewLine
&&
n
<
m_state
.
top
().
indent
)
m_state
.
pop
();
}
char
ch
=
INPUT
.
peek
();
if
(
IsWhitespace
(
ch
))
return
""
;
// TODO
if
(
ch
==
SeqToken
)
{
// grab token
INPUT
.
get
();
// is next token whitespace?
if
(
!
IsWhitespace
(
INPUT
.
peek
()))
{
// read entire line
std
::
string
line
;
std
::
getline
(
INPUT
,
line
);
return
ch
+
line
;
}
// if so, strip whitespace and go
StripWhitespace
();
return
std
::
string
(
""
)
+
SeqToken
;
}
// read until end-of-line
std
::
string
line
;
std
::
getline
(
INPUT
,
line
);
return
line
;
}
}
}
}
parser.h
View file @
b6a0ef20
...
@@ -2,54 +2,22 @@
...
@@ -2,54 +2,22 @@
#include <ios>
#include <ios>
#include <string>
#include <string>
#include <stack>
#include "scanner.h"
#include "document.h"
namespace
YAML
namespace
YAML
{
{
class
Node
;
class
Node
;
const
std
::
string
DocStart
=
"---"
;
const
std
::
string
DocEnd
=
"..."
;
const
char
SeqToken
=
'-'
;
enum
CONTEXT
{
C_BLOCK
,
C_FLOW
};
class
Parser
class
Parser
{
{
public:
struct
State
{
State
(
CONTEXT
context_
,
int
indent_
,
bool
startingNewLine_
)
:
context
(
context_
),
indent
(
indent_
),
startingNewLine
(
startingNewLine_
)
{}
CONTEXT
context
;
int
indent
;
bool
startingNewLine
;
};
public:
public:
Parser
(
std
::
istream
&
in
);
Parser
(
std
::
istream
&
in
);
~
Parser
();
~
Parser
();
operator
bool
()
const
;
void
GetNextDocument
(
Document
&
document
);
bool
operator
!
()
const
;
// parse helpers
static
bool
IsWhitespace
(
char
ch
);
void
Putback
(
const
std
::
string
&
str
);
void
StripWhitespace
(
int
n
=
-
1
);
int
GetNumOfSpaces
();
bool
SeqContinues
();
// readers
Node
*
ReadNextNode
();
std
::
string
ReadNextToken
();
private:
private:
bool
m_ok
;
Scanner
m_scanner
;
std
::
istream
&
INPUT
;
std
::
stack
<
State
>
m_state
;
};
};
}
}
reader.cpp
0 → 100644
View file @
b6a0ef20
#include "reader.h"
#include "scanner.h"
#include "parser.h"
namespace
YAML
{
Reader
::
Reader
(
std
::
istream
&
in
)
:
m_pParser
(
0
)
{
m_pParser
=
new
Parser
(
in
);
}
Reader
::~
Reader
()
{
delete
m_pParser
;
}
void
Reader
::
GetNextDocument
(
Document
&
document
)
{
m_pParser
->
GetNextDocument
(
document
);
}
}
reader.h
0 → 100644
View file @
b6a0ef20
#pragma once
#include <ios>
#include "document.h"
namespace
YAML
{
class
Parser
;
class
Reader
{
public:
Reader
(
std
::
istream
&
in
);
~
Reader
();
void
GetNextDocument
(
Document
&
document
);
private:
Parser
*
m_pParser
;
};
}
sequence.cpp
View file @
b6a0ef20
#include "sequence.h"
#include "sequence.h"
#include "node.h"
#include "node.h"
#include "parser.h"
namespace
YAML
namespace
YAML
{
{
Sequence
::
Sequence
(
Parser
*
pParser
)
Sequence
::
Sequence
()
{
{
Read
(
pParser
);
}
}
Sequence
::~
Sequence
()
Sequence
::~
Sequence
()
...
@@ -14,12 +13,4 @@ namespace YAML
...
@@ -14,12 +13,4 @@ namespace YAML
for
(
unsigned
i
=
0
;
i
<
m_data
.
size
();
i
++
)
for
(
unsigned
i
=
0
;
i
<
m_data
.
size
();
i
++
)
delete
m_data
[
i
];
delete
m_data
[
i
];
}
}
void
Sequence
::
Read
(
Parser
*
pParser
)
{
do
{
Node
*
pNode
=
pParser
->
ReadNextNode
();
m_data
.
push_back
(
pNode
);
}
while
(
pParser
->
SeqContinues
());
}
}
}
sequence.h
View file @
b6a0ef20
...
@@ -6,16 +6,13 @@
...
@@ -6,16 +6,13 @@
namespace
YAML
namespace
YAML
{
{
class
Node
;
class
Node
;
class
Parser
;
class
Sequence
:
public
Content
class
Sequence
:
public
Content
{
{
public:
public:
Sequence
(
Parser
*
pParser
);
Sequence
();
virtual
~
Sequence
();
virtual
~
Sequence
();
void
Read
(
Parser
*
pParser
);
protected:
protected:
std
::
vector
<
Node
*>
m_data
;
std
::
vector
<
Node
*>
m_data
;
};
};
...
...
yaml-reader.vcproj
View file @
b6a0ef20
...
@@ -169,10 +169,6 @@
...
@@ -169,10 +169,6 @@
RelativePath=
".\document.cpp"
RelativePath=
".\document.cpp"
>
>
</File>
</File>
<File
RelativePath=
".\exp.cpp"
>
</File>
<File
<File
RelativePath=
".\main.cpp"
RelativePath=
".\main.cpp"
>
>
...
@@ -186,15 +182,26 @@
...
@@ -186,15 +182,26 @@
>
>
</File>
</File>
<File
<File
RelativePath=
".\
pars
er.cpp"
RelativePath=
".\
read
er.cpp"
>
>
</File>
</File>
<File
<File
RelativePath=
".\
regex
.cpp"
RelativePath=
".\
scalar
.cpp"
>
>
</File>
</File>
<File
<File
RelativePath=
".\scalar.cpp"
RelativePath=
".\sequence.cpp"
>
</File>
<Filter
Name=
"Scanner"
>
<File
RelativePath=
".\exp.cpp"
>
</File>
<File
RelativePath=
".\regex.cpp"
>
>
</File>
</File>
<File
<File
...
@@ -210,18 +217,23 @@
...
@@ -210,18 +217,23 @@
>
>
</File>
</File>
<File
<File
RelativePath=
".\s
equence
.cpp"
RelativePath=
".\s
implekey
.cpp"
>
>
</File>
</File>
<File
<File
RelativePath=
".\s
implekey
.cpp"
RelativePath=
".\s
tream
.cpp"
>
>
</File>
</File>
</Filter>
<Filter
Name=
"Parser"
>
<File
<File
RelativePath=
".\
stream
.cpp"
RelativePath=
".\
parser
.cpp"
>
>
</File>
</File>
</Filter>
</Filter>
</Filter>
<Filter
<Filter
Name=
"Header Files"
Name=
"Header Files"
Filter=
"h;hpp;hxx;hm;inl;inc;xsd"
Filter=
"h;hpp;hxx;hm;inl;inc;xsd"
...
@@ -240,50 +252,62 @@
...
@@ -240,50 +252,62 @@
>
>
</File>
</File>
<File
<File
RelativePath=
".\
ex
p.h"
RelativePath=
".\
ma
p.h"
>
>
</File>
</File>
<File
<File
RelativePath=
".\
map
.h"
RelativePath=
".\
node
.h"
>
>
</File>
</File>
<File
<File
RelativePath=
".\
no
de.h"
RelativePath=
".\
rea
de
r
.h"
>
>
</File>
</File>
<File
<File
RelativePath=
".\
parse
r.h"
RelativePath=
".\
scala
r.h"
>
>
</File>
</File>
<File
<File
RelativePath=
".\
regex
.h"
RelativePath=
".\
sequence
.h"
>
>
</File>
</File>
<File
<File
RelativePath=
".\
scalar
.h"
RelativePath=
".\
token
.h"
>
>
</File>
</File>
<Filter
Name=
"Scanner"
>
<File
<File
RelativePath=
".\
scanner
.h"
RelativePath=
".\
exp
.h"
>
>
</File>
</File>
<File
<File
RelativePath=
".\
scanscalar
.h"
RelativePath=
".\
regex
.h"
>
>
</File>
</File>
<File
<File
RelativePath=
".\sequence.h"
RelativePath=
".\scanner.h"
>
</File>
<File
RelativePath=
".\scanscalar.h"
>
>
</File>
</File>
<File
<File
RelativePath=
".\stream.h"
RelativePath=
".\stream.h"
>
>
</File>
</File>
</Filter>
<Filter
Name=
"Parser"
>
<File
<File
RelativePath=
".\
token
.h"
RelativePath=
".\
parser
.h"
>
>
</File>
</File>
</Filter>
</Filter>
</Filter>
<Filter
<Filter
Name=
"Resource Files"
Name=
"Resource Files"
Filter=
"rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav"
Filter=
"rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav"
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment