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
ed6c2947
Commit
ed6c2947
authored
Jun 30, 2008
by
Jesse Beder
Browse files
Added a peek token command (for the parser to use).
parent
07d4cac4
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
108 additions
and
112 deletions
+108
-112
document.cpp
document.cpp
+12
-3
exceptions.h
exceptions.h
+18
-17
scanner.cpp
scanner.cpp
+77
-82
scanner.h
scanner.h
+1
-1
stream.cpp
stream.cpp
+0
-8
stream.h
stream.h
+0
-1
No files found.
document.cpp
View file @
ed6c2947
...
...
@@ -5,6 +5,9 @@
#include "exceptions.h"
#include <fstream>
#include <iostream>
#include "token.h"
namespace
YAML
{
Document
::
Document
()
:
m_pRoot
(
0
)
...
...
@@ -34,10 +37,16 @@ namespace YAML
std
::
ifstream
fin
(
fileName
.
c_str
());
Scanner
scanner
(
fin
);
scanner
.
Scan
();
// 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
();
// if(!scanner)
// return;
// m_pRoot = parser.ReadNextNode();
}
...
...
exceptions.h
View file @
ed6c2947
...
...
@@ -5,33 +5,34 @@
namespace
YAML
{
class
Exception
:
public
std
::
exception
{};
class
ScannerException
:
public
Exception
{};
class
UnknownToken
:
public
Exception
{};
class
IllegalBlockEntry
:
public
Exception
{};
class
IllegalMapKey
:
public
Exception
{};
class
IllegalMapValue
:
public
Exception
{};
class
IllegalScalar
:
public
Exception
{};
class
IllegalTabInIndentation
:
public
Exception
{};
class
IllegalFlowEnd
:
public
Exception
{};
class
IllegalDocIndicator
:
public
Exception
{};
class
IllegalEOF
:
public
Exception
{};
class
RequiredSimpleKeyNotFound
:
public
Exception
{};
class
ZeroIndentationInBlockScalar
:
public
Exception
{};
class
UnexpectedCharacterInBlockScalar
:
public
Exception
{};
class
AnchorNotFound
:
public
Exception
{};
class
IllegalCharacterInAnchor
:
public
Exception
{};
class
UnknownToken
:
public
Scanner
Exception
{};
class
IllegalBlockEntry
:
public
Scanner
Exception
{};
class
IllegalMapKey
:
public
Scanner
Exception
{};
class
IllegalMapValue
:
public
Scanner
Exception
{};
class
IllegalScalar
:
public
Scanner
Exception
{};
class
IllegalTabInIndentation
:
public
Scanner
Exception
{};
class
IllegalFlowEnd
:
public
Scanner
Exception
{};
class
IllegalDocIndicator
:
public
Scanner
Exception
{};
class
IllegalEOF
:
public
Scanner
Exception
{};
class
RequiredSimpleKeyNotFound
:
public
Scanner
Exception
{};
class
ZeroIndentationInBlockScalar
:
public
Scanner
Exception
{};
class
UnexpectedCharacterInBlockScalar
:
public
Scanner
Exception
{};
class
AnchorNotFound
:
public
Scanner
Exception
{};
class
IllegalCharacterInAnchor
:
public
Scanner
Exception
{};
class
UnknownEscapeSequence
:
public
Exception
{
class
UnknownEscapeSequence
:
public
Scanner
Exception
{
public:
UnknownEscapeSequence
(
char
ch_
)
:
ch
(
ch_
)
{}
char
ch
;
};
class
NonHexNumber
:
public
Exception
{
class
NonHexNumber
:
public
Scanner
Exception
{
public:
NonHexNumber
(
char
ch_
)
:
ch
(
ch_
)
{}
char
ch
;
};
class
InvalidUnicode
:
public
Exception
{
class
InvalidUnicode
:
public
Scanner
Exception
{
public:
InvalidUnicode
(
unsigned
value_
)
:
value
(
value_
)
{}
unsigned
value
;
...
...
scanner.cpp
View file @
ed6c2947
...
...
@@ -2,7 +2,6 @@
#include "token.h"
#include "exceptions.h"
#include "exp.h"
#include <iostream>
namespace
YAML
{
...
...
@@ -23,39 +22,51 @@ namespace YAML
delete
*
it
;
}
///////////////////////////////////////////////////////////////////////
// Misc. helpers
// GetNextToken
// . Removes and returns the next token on the queue.
Token
*
Scanner
::
GetNextToken
()
{
Token
*
pToken
=
PeekNextToken
();
if
(
!
m_tokens
.
empty
())
m_tokens
.
pop
();
return
pToken
;
}
// IsWhitespaceToBeEaten
// . We can eat whitespace if:
// 1. It's a space
// 2. It's a tab, and we're either:
// a. In the flow context
// b. In the block context but not where a simple key could be allowed
// (i.e., not at the beginning of a line, or following '-', '?', or ':')
bool
Scanner
::
IsWhitespaceToBeEaten
(
char
ch
)
// PeekNextToken
// . Returns (but does not remove) the next token on the queue, and scans if only we need to.
Token
*
Scanner
::
PeekNextToken
()
{
if
(
ch
==
' '
)
return
true
;
while
(
1
)
{
Token
*
pToken
=
0
;
if
(
ch
==
'\t'
&&
(
m_flowLevel
>=
0
||
!
m_simpleKeyAllowed
))
return
true
;
// is there a token in the queue?
if
(
!
m_tokens
.
empty
())
pToken
=
m_tokens
.
front
();
return
false
;
}
// (here's where we clean up the impossible tokens)
if
(
pToken
&&
pToken
->
status
==
TS_INVALID
)
{
m_tokens
.
pop
();
delete
pToken
;
continue
;
}
// ScanAndEnqueue
// . Scans the token, then pushes it in the queue.
// . Note: we also use a set of "limbo tokens", i.e., tokens
// that haven't yet been pushed. This way, if ScanToken()
// throws an exception, we'll be keeping track of 'pToken'
// somewhere, and it will be automatically cleaned up when
// the Scanner destructs.
template
<
typename
T
>
void
Scanner
::
ScanAndEnqueue
(
T
*
pToken
)
{
m_limboTokens
.
insert
(
pToken
);
m_tokens
.
push
(
ScanToken
(
pToken
));
m_limboTokens
.
erase
(
pToken
);
// on unverified tokens, we just have to wait
if
(
pToken
&&
pToken
->
status
==
TS_UNVERIFIED
)
pToken
=
0
;
// then that's what we want
if
(
pToken
)
return
pToken
;
// no token? maybe we've actually finished
if
(
m_endedStream
)
break
;
// no? then scan...
ScanNextToken
();
}
return
0
;
}
// ScanNextToken
...
...
@@ -166,7 +177,8 @@ namespace YAML
break
;
// otherwise, let's eat the line break and keep going
INPUT
.
EatLineBreak
();
int
n
=
Exp
::
Break
.
Match
(
INPUT
);
INPUT
.
Eat
(
n
);
// oh yeah, and let's get rid of that simple key
VerifySimpleKey
();
...
...
@@ -177,6 +189,41 @@ namespace YAML
}
}
///////////////////////////////////////////////////////////////////////
// Misc. helpers
// IsWhitespaceToBeEaten
// . We can eat whitespace if:
// 1. It's a space
// 2. It's a tab, and we're either:
// a. In the flow context
// b. In the block context but not where a simple key could be allowed
// (i.e., not at the beginning of a line, or following '-', '?', or ':')
bool
Scanner
::
IsWhitespaceToBeEaten
(
char
ch
)
{
if
(
ch
==
' '
)
return
true
;
if
(
ch
==
'\t'
&&
(
m_flowLevel
>=
0
||
!
m_simpleKeyAllowed
))
return
true
;
return
false
;
}
// ScanAndEnqueue
// . Scans the token, then pushes it in the queue.
// . Note: we also use a set of "limbo tokens", i.e., tokens
// that haven't yet been pushed. This way, if ScanToken()
// throws an exception, we'll be keeping track of 'pToken'
// somewhere, and it will be automatically cleaned up when
// the Scanner destructs.
template
<
typename
T
>
void
Scanner
::
ScanAndEnqueue
(
T
*
pToken
)
{
m_limboTokens
.
insert
(
pToken
);
m_tokens
.
push
(
ScanToken
(
pToken
));
m_limboTokens
.
erase
(
pToken
);
}
// PushIndentTo
// . Pushes an indentation onto the stack, and enqueues the
// proper token (sequence start or mapping start).
...
...
@@ -216,56 +263,4 @@ namespace YAML
m_tokens
.
push
(
new
BlockEndToken
);
}
}
// GetNextToken
// . Returns the next token on the queue, and scans if only we need to.
Token
*
Scanner
::
GetNextToken
()
{
while
(
1
)
{
Token
*
pToken
=
0
;
// is there a token in the queue?
if
(
!
m_tokens
.
empty
())
pToken
=
m_tokens
.
front
();
// (here's where we clean up the impossible tokens)
if
(
pToken
&&
pToken
->
status
==
TS_INVALID
)
{
m_tokens
.
pop
();
delete
pToken
;
continue
;
}
// on unverified tokens, we just have to wait
if
(
pToken
&&
pToken
->
status
==
TS_UNVERIFIED
)
pToken
=
0
;
// then that's what we want
if
(
pToken
)
{
m_tokens
.
pop
();
return
pToken
;
}
// no token? maybe we've actually finished
if
(
m_endedStream
)
break
;
// no? then scan...
ScanNextToken
();
}
return
0
;
}
// temporary function for testing
void
Scanner
::
Scan
()
{
while
(
1
)
{
Token
*
pToken
=
GetNextToken
();
if
(
!
pToken
)
break
;
std
::
cout
<<
typeid
(
*
pToken
).
name
()
<<
": "
<<
*
pToken
<<
std
::
endl
;
delete
pToken
;
}
}
}
scanner.h
View file @
ed6c2947
...
...
@@ -19,7 +19,7 @@ namespace YAML
~
Scanner
();
Token
*
GetNextToken
();
void
Sca
n
();
Token
*
PeekNextToke
n
();
private:
// scanning
...
...
stream.cpp
View file @
ed6c2947
...
...
@@ -32,12 +32,4 @@ namespace YAML
for
(
int
i
=
0
;
i
<
n
;
i
++
)
GetChar
();
}
// GetLineBreak
// . Eats with no checking
void
Stream
::
EatLineBreak
()
{
Eat
(
1
);
column
=
0
;
}
}
stream.h
View file @
ed6c2947
...
...
@@ -18,7 +18,6 @@ namespace YAML
char
GetChar
();
std
::
string
GetChar
(
int
n
);
void
Eat
(
int
n
=
1
);
void
EatLineBreak
();
std
::
istream
&
input
;
int
line
,
column
;
...
...
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