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
Hide whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
144 additions
and
276 deletions
+144
-276
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
+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
;
}
bool
Parser
::
operator
!
()
const
{
return
!
m_ok
;
}
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
();
// scan and output, for now
while
(
1
)
{
if
(
state
.
startingNewLine
)
{
Token
*
pToken
=
m_scanner
.
GetNextToken
();
int
n
=
GetNumOfSpaces
();
if
(
!
pToken
)
StripWhitespace
(
n
);
break
;
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
();
std
::
cout
<<
typeid
(
*
pToken
).
name
()
<<
": "
<<
*
pToken
<<
std
::
endl
;
if
(
IsWhitespace
(
ch
))
delete
pToken
;
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
;
}
}
getchar
();
// 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
:
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
(
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
...
@@ -170,10 +170,6 @@
...
@@ -170,10 +170,6 @@
>
>
</File>
</File>
<File
<File
RelativePath=
".\exp.cpp"
>
</File>
<File
RelativePath=
".\main.cpp"
RelativePath=
".\main.cpp"
>
>
</File>
</File>
...
@@ -186,11 +182,7 @@
...
@@ -186,11 +182,7 @@
>
>
</File>
</File>
<File
<File
RelativePath=
".\parser.cpp"
RelativePath=
".\reader.cpp"
>
</File>
<File
RelativePath=
".\regex.cpp"
>
>
</File>
</File>
<File
<File
...
@@ -198,29 +190,49 @@
...
@@ -198,29 +190,49 @@
>
>
</File>
</File>
<File
<File
RelativePath=
".\scanner.cpp"
>
</File>
<File
RelativePath=
".\scanscalar.cpp"
>
</File>
<File
RelativePath=
".\scantoken.cpp"
>
</File>
<File
RelativePath=
".\sequence.cpp"
RelativePath=
".\sequence.cpp"
>
>
</File>
</File>
<Fil
e
<Fil
ter
RelativePath=
".\simplekey.cpp
"
Name=
"Scanner
"
>
>
</File>
<File
<File
RelativePath=
".\exp.cpp"
RelativePath=
".\stream.cpp"
>
</File>
<File
RelativePath=
".\regex.cpp"
>
</File>
<File
RelativePath=
".\scanner.cpp"
>
</File>
<File
RelativePath=
".\scanscalar.cpp"
>
</File>
<File
RelativePath=
".\scantoken.cpp"
>
</File>
<File
RelativePath=
".\simplekey.cpp"
>
</File>
<File
RelativePath=
".\stream.cpp"
>
</File>
</Filter>
<Filter
Name=
"Parser"
>
>
</File>
<File
RelativePath=
".\parser.cpp"
>
</File>
</Filter>
</Filter>
</Filter>
<Filter
<Filter
Name=
"Header Files"
Name=
"Header Files"
...
@@ -240,10 +252,6 @@
...
@@ -240,10 +252,6 @@
>
>
</File>
</File>
<File
<File
RelativePath=
".\exp.h"
>
</File>
<File
RelativePath=
".\map.h"
RelativePath=
".\map.h"
>
>
</File>
</File>
...
@@ -252,11 +260,7 @@
...
@@ -252,11 +260,7 @@
>
>
</File>
</File>
<File
<File
RelativePath=
".\parser.h"
RelativePath=
".\reader.h"
>
</File>
<File
RelativePath=
".\regex.h"
>
>
</File>
</File>
<File
<File
...
@@ -264,25 +268,45 @@
...
@@ -264,25 +268,45 @@
>
>
</File>
</File>
<File
<File
RelativePath=
".\scanner.h"
>
</File>
<File
RelativePath=
".\scanscalar.h"
>
</File>
<File
RelativePath=
".\sequence.h"
RelativePath=
".\sequence.h"
>
>
</File>
</File>
<File
<File
RelativePath=
".\stream.h"
>
</File>
<File
RelativePath=
".\token.h"
RelativePath=
".\token.h"
>
>
</File>
</File>
<Filter
Name=
"Scanner"
>
<File
RelativePath=
".\exp.h"
>
</File>
<File
RelativePath=
".\regex.h"
>
</File>
<File
RelativePath=
".\scanner.h"
>
</File>
<File
RelativePath=
".\scanscalar.h"
>
</File>
<File
RelativePath=
".\stream.h"
>
</File>
</Filter>
<Filter
Name=
"Parser"
>
<File
RelativePath=
".\parser.h"
>
</File>
</Filter>
</Filter>
</Filter>
<Filter
<Filter
Name=
"Resource Files"
Name=
"Resource Files"
...
...
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