Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yaml-cpp
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
lvzhengyang
yaml-cpp
Commits
b6a0ef20
Commit
b6a0ef20
authored
Jun 30, 2008
by
Jesse Beder
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Started the parser.
parent
ed6c2947
Show whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
116 additions
and
248 deletions
+116
-248
document.cpp
+0
-33
document.h
+0
-4
main.cpp
+7
-3
node.cpp
+0
-11
node.h
+0
-2
parser.cpp
+16
-124
parser.h
+4
-36
reader.cpp
+21
-0
reader.h
+21
-0
sequence.cpp
+2
-11
sequence.h
+1
-4
yaml-reader.vcproj
+44
-20
No files found.
document.cpp
View file @
b6a0ef20
#include "document.h"
#include "node.h"
#include "parser.h"
#include "scanner.h"
#include "exceptions.h"
#include <fstream>
#include <iostream>
#include "token.h"
namespace
YAML
{
...
...
@@ -14,11 +7,6 @@ namespace YAML
{
}
Document
::
Document
(
const
std
::
string
&
fileName
)
:
m_pRoot
(
0
)
{
Load
(
fileName
);
}
Document
::~
Document
()
{
Clear
();
...
...
@@ -29,25 +17,4 @@ namespace YAML
delete
m_pRoot
;
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
#include <string>
namespace
YAML
{
class
Node
;
...
...
@@ -10,11 +8,9 @@ namespace YAML
{
public
:
Document
();
Document
(
const
std
::
string
&
fileName
);
~
Document
();
void
Clear
();
void
Load
(
const
std
::
string
&
fileName
);
private
:
Node
*
m_pRoot
;
...
...
main.cpp
View file @
b6a0ef20
#include "
document
.h"
#include
"regex.h"
#include "
reader
.h"
#include
<fstream>
int
main
()
{
YAML
::
Document
doc
(
"test.yaml"
);
std
::
ifstream
fin
(
"test.yaml"
);
YAML
::
Reader
reader
(
fin
);
YAML
::
Document
doc
;
reader
.
GetNextDocument
(
doc
);
return
0
;
}
\ No newline at end of file
node.cpp
View file @
b6a0ef20
...
...
@@ -20,15 +20,4 @@ namespace YAML
delete
m_pContent
;
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
const
std
::
string
MapTag
=
"!!map"
;
class
Content
;
class
Parser
;
class
Node
{
...
...
@@ -19,7 +18,6 @@ namespace YAML
~
Node
();
void
Clear
();
void
Read
(
Parser
*
pParser
,
const
std
::
string
&
token
);
private
:
std
::
string
m_tag
;
...
...
parser.cpp
View file @
b6a0ef20
#include "parser.h"
#include "node.h"
#include "token.h"
#include <iostream>
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
));
// read header
std
::
string
token
=
ReadNextToken
();
if
(
token
!=
DocStart
)
m_ok
=
false
;
// eat the stream start token
// TODO: check?
Token
*
pToken
=
m_scanner
.
GetNextToken
();
}
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
{
return
!
m_ok
;
std
::
cout
<<
typeid
(
*
pToken
).
name
()
<<
": "
<<
*
pToken
<<
std
::
endl
;
delete
pToken
;
}
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
;
getchar
();
}
}
parser.h
View file @
b6a0ef20
...
...
@@ -2,54 +2,22 @@
#include <ios>
#include <string>
#include <stack>
#include "scanner.h"
#include "document.h"
namespace
YAML
{
class
Node
;
const
std
::
string
DocStart
=
"---"
;
const
std
::
string
DocEnd
=
"..."
;
const
char
SeqToken
=
'-'
;
enum
CONTEXT
{
C_BLOCK
,
C_FLOW
};
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
:
Parser
(
std
::
istream
&
in
);
~
Parser
();
operator
bool
()
const
;
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
();
void
GetNextDocument
(
Document
&
document
);
private
:
bool
m_ok
;
std
::
istream
&
INPUT
;
std
::
stack
<
State
>
m_state
;
Scanner
m_scanner
;
};
}
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 "node.h"
#include "parser.h"
namespace
YAML
{
Sequence
::
Sequence
(
Parser
*
pParser
)
Sequence
::
Sequence
()
{
Read
(
pParser
);
}
Sequence
::~
Sequence
()
...
...
@@ -14,12 +13,4 @@ namespace YAML
for
(
unsigned
i
=
0
;
i
<
m_data
.
size
();
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 @@
namespace
YAML
{
class
Node
;
class
Parser
;
class
Sequence
:
public
Content
{
public
:
Sequence
(
Parser
*
pParser
);
Sequence
();
virtual
~
Sequence
();
void
Read
(
Parser
*
pParser
);
protected
:
std
::
vector
<
Node
*>
m_data
;
};
...
...
yaml-reader.vcproj
View file @
b6a0ef20
...
...
@@ -170,10 +170,6 @@
>
</File>
<File
RelativePath=
".\exp.cpp"
>
</File>
<File
RelativePath=
".\main.cpp"
>
</File>
...
...
@@ -186,15 +182,26 @@
>
</File>
<File
RelativePath=
".\
pars
er.cpp"
RelativePath=
".\
read
er.cpp"
>
</File>
<File
RelativePath=
".\
regex
.cpp"
RelativePath=
".\
scalar
.cpp"
>
</File>
<File
RelativePath=
".\scalar.cpp"
RelativePath=
".\sequence.cpp"
>
</File>
<Filter
Name=
"Scanner"
>
<File
RelativePath=
".\exp.cpp"
>
</File>
<File
RelativePath=
".\regex.cpp"
>
</File>
<File
...
...
@@ -210,18 +217,23 @@
>
</File>
<File
RelativePath=
".\sequence
.cpp"
RelativePath=
".\simplekey
.cpp"
>
</File>
<File
RelativePath=
".\simplekey
.cpp"
RelativePath=
".\stream
.cpp"
>
</File>
</Filter>
<Filter
Name=
"Parser"
>
<File
RelativePath=
".\stream
.cpp"
RelativePath=
".\parser
.cpp"
>
</File>
</Filter>
</Filter>
<Filter
Name=
"Header Files"
Filter=
"h;hpp;hxx;hm;inl;inc;xsd"
...
...
@@ -240,50 +252,62 @@
>
</File>
<File
RelativePath=
".\
ex
p.h"
RelativePath=
".\
ma
p.h"
>
</File>
<File
RelativePath=
".\
map
.h"
RelativePath=
".\
node
.h"
>
</File>
<File
RelativePath=
".\
node
.h"
RelativePath=
".\
reader
.h"
>
</File>
<File
RelativePath=
".\
parse
r.h"
RelativePath=
".\
scala
r.h"
>
</File>
<File
RelativePath=
".\
regex
.h"
RelativePath=
".\
sequence
.h"
>
</File>
<File
RelativePath=
".\
scalar
.h"
RelativePath=
".\
token
.h"
>
</File>
<Filter
Name=
"Scanner"
>
<File
RelativePath=
".\scanner
.h"
RelativePath=
".\exp
.h"
>
</File>
<File
RelativePath=
".\scanscalar
.h"
RelativePath=
".\regex
.h"
>
</File>
<File
RelativePath=
".\sequence.h"
RelativePath=
".\scanner.h"
>
</File>
<File
RelativePath=
".\scanscalar.h"
>
</File>
<File
RelativePath=
".\stream.h"
>
</File>
</Filter>
<Filter
Name=
"Parser"
>
<File
RelativePath=
".\token
.h"
RelativePath=
".\parser
.h"
>
</File>
</Filter>
</Filter>
<Filter
Name=
"Resource Files"
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