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
31f7db5a
Commit
31f7db5a
authored
Jun 26, 2008
by
Jesse Beder
Browse files
More simple scalar scanning.
parent
a3961d04
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
159 additions
and
204 deletions
+159
-204
document.cpp
document.cpp
+2
-1
exceptions.h
exceptions.h
+1
-0
scanner.cpp
scanner.cpp
+149
-197
scanner.h
scanner.h
+6
-5
token.h
token.h
+1
-1
No files found.
document.cpp
View file @
31f7db5a
...
...
@@ -36,8 +36,9 @@ namespace YAML
try
{
scanner
.
Scan
();
}
catch
(
const
UnknownToke
n
&
e
)
{
}
catch
(
const
Exceptio
n
&
e
)
{
}
getchar
();
// if(!scanner)
// return;
...
...
exceptions.h
View file @
31f7db5a
...
...
@@ -11,4 +11,5 @@ namespace YAML
class
IllegalMapKey
:
public
Exception
{};
class
IllegalMapValue
:
public
Exception
{};
class
IllegalScalar
:
public
Exception
{};
class
IllegalTabInScalar
:
public
Exception
{};
}
scanner.cpp
View file @
31f7db5a
#include "scanner.h"
#include "token.h"
#include "exceptions.h"
#include <iostream>
namespace
YAML
{
...
...
@@ -32,30 +33,42 @@ namespace YAML
return
INPUT
.
get
();
}
//
GetLineBreak
// . Eats
with no checking
void
Scanner
::
Eat
L
in
eBreak
(
)
//
Eat
// . Eats
'n' characters and updates our position.
void
Scanner
::
Eat
(
in
t
n
)
{
for
(
int
i
=
0
;
i
<
n
;
i
++
)
{
m_column
++
;
char
ch
=
INPUT
.
get
();
if
(
ch
==
'\n'
)
m_column
=
0
;
INPUT
.
get
();
}
}
//
EatDocumentStart
// .
Eats wi
th n
o check
ing
void
Scanner
::
EatDocumentStart
(
)
//
Peek
// .
Peeks at
th
e
n
ext 'n' characters and returns them in a str
ing
.
std
::
string
Scanner
::
Peek
(
int
n
)
{
INPUT
.
get
();
INPUT
.
get
();
INPUT
.
get
();
std
::
string
ret
;
// extract n - 1 characters, and peek at the nth
for
(
int
i
=
0
;
i
<
n
-
1
;
i
++
)
ret
+=
INPUT
.
get
();
ret
+=
INPUT
.
peek
();
// and put back the n - 1 characters we STOLE
for
(
int
i
=
n
-
2
;
i
>=
0
;
i
--
)
INPUT
.
putback
(
ret
[
i
]);
return
ret
;
}
//
EatDocumentEnd
//
GetLineBreak
// . Eats with no checking
void
Scanner
::
Eat
DocumentEnd
()
void
Scanner
::
Eat
LineBreak
()
{
INPUT
.
get
();
INPUT
.
get
();
INPUT
.
get
();
Eat
(
1
);
m_column
=
0
;
}
// IsWhitespaceToBeEaten
...
...
@@ -65,10 +78,8 @@ namespace YAML
// 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
()
bool
Scanner
::
IsWhitespaceToBeEaten
(
char
ch
)
{
char
ch
=
INPUT
.
peek
();
if
(
ch
==
' '
)
return
true
;
...
...
@@ -79,17 +90,15 @@ namespace YAML
}
// IsLineBreak
bool
Scanner
::
IsLineBreak
()
bool
Scanner
::
IsLineBreak
(
char
ch
)
{
char
ch
=
INPUT
.
peek
();
return
ch
==
'\n'
;
// TODO: More types of line breaks
}
// IsBlank
bool
Scanner
::
IsBlank
()
bool
Scanner
::
IsBlank
(
char
ch
)
{
char
ch
=
INPUT
.
peek
();
return
IsLineBreak
()
||
ch
==
' '
||
ch
==
'\t'
||
ch
==
EOF
;
return
IsLineBreak
(
ch
)
||
ch
==
' '
||
ch
==
'\t'
||
ch
==
EOF
;
}
// IsDocumentStart
...
...
@@ -99,34 +108,8 @@ namespace YAML
if
(
m_column
!=
0
)
return
false
;
// then needs '---'
for
(
int
i
=
0
;
i
<
3
;
i
++
)
{
if
(
INPUT
.
peek
()
!=
'-'
)
{
// first put 'em back
for
(
int
j
=
0
;
j
<
i
;
j
++
)
INPUT
.
putback
(
'-'
);
// and return
return
false
;
}
INPUT
.
get
();
}
// then needs a blank character (or eof)
if
(
!
IsBlank
())
{
// put 'em back
for
(
int
i
=
0
;
i
<
3
;
i
++
)
INPUT
.
putback
(
'-'
);
// and return
return
false
;
}
// finally, put 'em back and go
for
(
int
i
=
0
;
i
<
3
;
i
++
)
INPUT
.
putback
(
'-'
);
return
true
;
std
::
string
next
=
Peek
(
4
);
return
next
[
0
]
==
'-'
&&
next
[
1
]
==
'-'
&&
next
[
2
]
==
'-'
&&
IsBlank
(
next
[
3
]);
}
// IsDocumentEnd
...
...
@@ -136,88 +119,29 @@ namespace YAML
if
(
m_column
!=
0
)
return
false
;
// then needs '...'
for
(
int
i
=
0
;
i
<
3
;
i
++
)
{
if
(
INPUT
.
peek
()
!=
'.'
)
{
// first put 'em back
for
(
int
j
=
0
;
j
<
i
;
j
++
)
INPUT
.
putback
(
'.'
);
// and return
return
false
;
}
INPUT
.
get
();
}
// then needs a blank character (or eof)
if
(
!
IsBlank
())
{
// put 'em back
for
(
int
i
=
0
;
i
<
3
;
i
++
)
INPUT
.
putback
(
'.'
);
// and return
return
false
;
}
// finally, put 'em back and go
for
(
int
i
=
0
;
i
<
3
;
i
++
)
INPUT
.
putback
(
'-'
);
return
true
;
std
::
string
next
=
Peek
(
4
);
return
next
[
0
]
==
'.'
&&
next
[
1
]
==
'.'
&&
next
[
2
]
==
'.'
&&
IsBlank
(
next
[
3
]);
}
// IsBlockEntry
bool
Scanner
::
IsBlockEntry
()
{
if
(
INPUT
.
peek
()
!=
Keys
::
BlockEntry
)
return
false
;
INPUT
.
get
();
// then needs a blank character (or eof)
if
(
!
IsBlank
())
{
INPUT
.
putback
(
Keys
::
BlockEntry
);
return
false
;
}
INPUT
.
putback
(
Keys
::
BlockEntry
);
return
true
;
std
::
string
next
=
Peek
(
2
);
return
next
[
0
]
==
Keys
::
BlockEntry
&&
IsBlank
(
next
[
1
]);
}
// IsKey
bool
Scanner
::
IsKey
()
{
if
(
INPUT
.
peek
()
!=
Keys
::
Key
)
return
false
;
INPUT
.
get
();
// then needs a blank character (or eof), if we're in block context
if
(
m_flowLevel
==
0
&&
!
IsBlank
())
{
INPUT
.
putback
(
Keys
::
BlockEntry
);
return
false
;
}
INPUT
.
putback
(
Keys
::
BlockEntry
);
return
true
;
std
::
string
next
=
Peek
(
2
);
return
next
[
0
]
==
Keys
::
Key
&&
(
IsBlank
(
next
[
1
])
||
m_flowLevel
>
0
);
}
// IsValue
bool
Scanner
::
IsValue
()
{
if
(
INPUT
.
peek
()
!=
Keys
::
Value
)
return
false
;
INPUT
.
get
();
// then needs a blank character (or eof), if we're in block context
if
(
m_flowLevel
==
0
&&
!
IsBlank
())
{
INPUT
.
putback
(
Keys
::
BlockEntry
);
return
false
;
}
INPUT
.
putback
(
Keys
::
BlockEntry
);
return
true
;
std
::
string
next
=
Peek
(
2
);
return
next
[
0
]
==
Keys
::
Value
&&
(
IsBlank
(
next
[
1
])
||
m_flowLevel
>
0
);
}
// IsPlainScalar
...
...
@@ -228,35 +152,26 @@ namespace YAML
// . In the flow context ? : are illegal and - must not be followed with a space.
bool
Scanner
::
IsPlainScalar
()
{
if
(
IsBlank
())
std
::
string
next
=
Peek
(
2
);
if
(
IsBlank
(
next
[
0
]))
return
false
;
// never characters
std
::
string
never
=
",[]{}#&*!|>
\'\"
%@`"
;
for
(
unsigned
i
=
0
;
i
<
never
.
size
();
i
++
)
if
(
INPUT
.
peek
()
==
never
[
i
])
if
(
std
::
string
(
",[]{}#&*!|>
\'\"
%@`"
).
find
(
next
[
0
])
!=
std
::
string
::
npos
)
return
false
;
// specific block/flow characters
if
(
m_flowLevel
==
0
)
{
if
(
INPUT
.
peek
()
==
'-'
||
INPUT
.
peek
()
==
'?'
||
INPUT
.
peek
()
==
':'
)
{
char
ch
=
INPUT
.
get
();
if
(
IsBlank
())
{
INPUT
.
putback
(
ch
);
if
((
next
[
0
]
==
'-'
||
next
[
0
]
==
'?'
||
next
[
0
]
==
':'
)
&&
IsBlank
(
next
[
1
]))
return
false
;
}
}
}
else
{
if
(
INPUT
.
peek
()
==
'?'
||
INPUT
.
peek
()
==
':'
)
if
(
next
[
0
]
==
'?'
||
next
[
0
]
==
':'
)
return
false
;
if
(
INPUT
.
peek
()
==
'-'
)
{
INPUT
.
get
();
if
(
IsBlank
())
{
INPUT
.
putback
(
'-'
);
if
(
next
[
0
]
==
'-'
&&
IsBlank
(
next
[
1
]))
return
false
;
}
}
}
return
true
;
}
...
...
@@ -311,8 +226,8 @@ namespace YAML
m_simpleKeyAllowed
=
false
;
// eat
it
Eat
DocumentStart
();
// eat
Eat
(
3
);
return
pToken
;
}
...
...
@@ -325,8 +240,8 @@ namespace YAML
m_simpleKeyAllowed
=
false
;
// eat
it
Eat
DocumentEnd
();
// eat
Eat
(
3
);
return
pToken
;
}
...
...
@@ -419,7 +334,7 @@ namespace YAML
m_simpleKeyAllowed
=
true
;
// eat
INPUT
.
ge
t
();
Ea
t
(
1
);
return
pToken
;
}
...
...
@@ -443,7 +358,7 @@ namespace YAML
m_simpleKeyAllowed
=
false
;
// eat
INPUT
.
ge
t
();
Ea
t
(
1
);
return
pToken
;
}
...
...
@@ -470,7 +385,7 @@ namespace YAML
m_simpleKeyAllowed
=
false
;
// eat
INPUT
.
ge
t
();
Ea
t
(
1
);
return
pToken
;
}
...
...
@@ -482,7 +397,10 @@ namespace YAML
m_simpleKeyAllowed
=
false
;
// now eat and store the scalar
while
(
1
)
{
std
::
string
scalar
;
bool
leadingBlanks
=
true
;
while
(
INPUT
)
{
// doc start/end tokens
if
(
IsDocumentStart
()
||
IsDocumentEnd
())
break
;
...
...
@@ -492,22 +410,48 @@ namespace YAML
break
;
// first eat non-blanks
while
(
!
IsBlank
())
{
while
(
INPUT
&&
!
IsBlank
(
INPUT
.
peek
()))
{
std
::
string
next
=
Peek
(
2
);
// illegal colon in flow context
if
(
m_flowLevel
>
0
&&
INPUT
.
peek
()
==
':'
)
{
INPUT
.
get
();
if
(
!
IsBlank
())
{
INPUT
.
putback
(
':'
);
if
(
m_flowLevel
>
0
&&
next
[
0
]
==
':'
)
{
if
(
!
IsBlank
(
next
[
1
]))
throw
IllegalScalar
();
}
INPUT
.
putback
(
':'
);
}
// characters that might end the scalar
// TODO: scanner.c line 3434
if
(
next
[
0
]
==
':'
&&
IsBlank
(
next
[
1
]))
break
;
if
(
m_flowLevel
>
0
&&
std
::
string
(
",:?[]{}"
).
find
(
next
[
0
])
!=
std
::
string
::
npos
)
break
;
scalar
+=
GetChar
();
}
// now eat blanks
while
(
IsBlank
(
INPUT
.
peek
())
/* || IsBreak(INPUT.peek()) */
)
{
if
(
IsBlank
(
INPUT
.
peek
()))
{
if
(
leadingBlanks
&&
m_column
<=
m_indents
.
top
())
throw
IllegalTabInScalar
();
// TODO: Store some blanks?
Eat
(
1
);
}
else
{
Eat
(
1
);
}
}
// TODO: join whitespace
// and finally break if we're below the indentation level
if
(
m_flowLevel
==
0
&&
m_column
<=
m_indents
.
top
())
break
;
}
// now modify our token
if
(
leadingBlanks
)
m_simpleKeyAllowed
=
true
;
return
pToken
;
}
...
...
@@ -588,18 +532,18 @@ namespace YAML
{
while
(
1
)
{
// first eat whitespace
while
(
IsWhitespaceToBeEaten
())
INPUT
.
ge
t
();
while
(
IsWhitespaceToBeEaten
(
INPUT
.
peek
()
))
Ea
t
(
1
);
// then eat a comment
if
(
INPUT
.
peek
()
==
Keys
::
Comment
)
{
// eat until line break
while
(
INPUT
&&
!
IsLineBreak
())
INPUT
.
ge
t
();
while
(
INPUT
&&
!
IsLineBreak
(
INPUT
.
peek
()
))
Ea
t
(
1
);
}
// if it's NOT a line break, then we're done!
if
(
!
IsLineBreak
())
if
(
!
IsLineBreak
(
INPUT
.
peek
()
))
break
;
// otherwise, let's eat the line break and keep going
...
...
@@ -651,7 +595,15 @@ namespace YAML
// temporary function for testing
void
Scanner
::
Scan
()
{
while
(
INPUT
)
while
(
INPUT
)
{
ScanNextToken
();
while
(
!
m_tokens
.
empty
())
{
Token
*
pToken
=
m_tokens
.
front
();
m_tokens
.
pop
();
std
::
cout
<<
typeid
(
*
pToken
).
name
()
<<
std
::
endl
;
delete
pToken
;
}
}
}
}
scanner.h
View file @
31f7db5a
...
...
@@ -43,13 +43,14 @@ namespace YAML
private:
char
GetChar
();
void
Eat
(
int
n
=
1
);
std
::
string
Peek
(
int
n
);
void
EatLineBreak
();
void
EatDocumentStart
();
void
EatDocumentEnd
();
bool
IsWhitespaceToBeEaten
();
bool
IsLineBreak
();
bool
IsBlank
();
bool
IsWhitespaceToBeEaten
(
char
ch
);
bool
IsLineBreak
(
char
ch
);
bool
IsBlank
(
char
ch
);
bool
IsDocumentStart
();
bool
IsDocumentEnd
();
bool
IsBlockEntry
();
...
...
token.h
View file @
31f7db5a
...
...
@@ -2,7 +2,7 @@
namespace
YAML
{
class
Token
{};
class
Token
{
public
:
virtual
~
Token
()
{}
};
class
StreamStartToken
:
public
Token
{};
class
StreamEndToken
:
public
Token
{};
...
...
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