Skip to content

Commit

Permalink
Merge pull request #202 from fktn-k/feature/200_allow_space_in_unquot…
Browse files Browse the repository at this point in the history
…ed_strings

#200 Allow a space in unquoted strings
  • Loading branch information
fktn-k authored Nov 8, 2023
2 parents 7df5b59 + d85d6e2 commit 6ed3e07
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 36 deletions.
60 changes: 30 additions & 30 deletions docs/mkdocs/docs/tutorials/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,20 @@ See [the CMake Integration section]() for the other ways and modify the implemen

```yaml
novels:
- title: "Robinson Crusoe"
author: "Daniel Defoe"
- title: Robinson Crusoe
author: Daniel Defoe
year: 1678
- title: "Frankenstein"
author: "Jane Austen"
- title: Frankenstein
author: Jane Austen
year: 1818
- title: "Moby-Dick"
author: "Herman Melville"
- title: Moby-Dick
author: Herman Melville
year: 1851
- title: "Brave New World"
author: "Aldous Huxley"
- title: Brave New World
author: Aldous Huxley
year: 1932
- title: "Never Let Me Go"
author: "Kazuo Ishiguro"
- title: Never Let Me Go
author: Kazuo Ishiguro
year: 2005
```
=== "tutorial.cpp"
Expand Down Expand Up @@ -133,24 +133,24 @@ If you run the tutorial executable file, you will see the output like:
```bash
novels:
-
title: "Robinson Crusoe"
author: "Daniel Defoe"
title: Robinson Crusoe
author: Daniel Defoe
year: 1678
-
title: "Frankenstein"
author: "Jane Austen"
title: Frankenstein
author: Jane Austen
year: 1818
-
title: "Moby-Dick"
author: "Herman Melville"
title: Moby-Dick
author: Herman Melville
year: 1851
-
title: "Brave New World"
author: "Aldous Huxley"
title: Brave New World
author: Aldous Huxley
year: 1932
-
title: "Never Let Me Go"
author: "Kazuo Ishiguro"
title: Never Let Me Go
author: Kazuo Ishiguro
year: 2005
```

Expand Down Expand Up @@ -241,20 +241,20 @@ Rebuild and run the application, and you'll see the output like:
```bash
recommends:
-
title: "Robinson Crusoe"
author: "Daniel Defoe"
title: Robinson Crusoe
author: Daniel Defoe
-
title: "Frankenstein"
author: "Jane Austen"
title: Frankenstein
author: Jane Austen
-
title: "Moby-Dick"
author: "Herman Melville"
title: Moby-Dick
author: Herman Melville
-
title: "Brave New World"
author: "Aldous Huxley"
title: Brave New World
author: Aldous Huxley
-
title: "Never Let Me Go"
author: "Kazuo Ishiguro"
title: Never Let Me Go
author: Kazuo Ishiguro
```

### :pill: Integrate with user-defined types
Expand Down
31 changes: 25 additions & 6 deletions include/fkYAML/detail/input/lexical_analyzer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ class lexical_analyzer
case 'F':
case 'f': {
// YAML specifies that only these words represent the boolean value `false`.
// See "10.3.2. Tag Resolution" section in https://yaml.org/spec/1.2.2/
// See "10.3.2 Tag Resolution" section in https://yaml.org/spec/1.2.2/
char_int_type ret = m_input_handler.get_range(5, m_value_buffer);
if (ret == end_of_input)
{
Expand All @@ -247,7 +247,7 @@ class lexical_analyzer
case 'n': {
// YAML specifies that these words and a tilde represent a null value.
// Tildes are already checked above, so no check is needed here.
// See "10.3.2. Tag Resolution" section in https://yaml.org/spec/1.2.2/
// See "10.3.2 Tag Resolution" section in https://yaml.org/spec/1.2.2/
char_int_type ret = m_input_handler.get_range(4, m_value_buffer);
if (ret == end_of_input)
{
Expand All @@ -271,7 +271,7 @@ class lexical_analyzer
case 'T':
case 't': {
// YAML specifies that only these words represent the boolean value `true`.
// See "10.3.2. Tag Resolution" section in https://yaml.org/spec/1.2.2/
// See "10.3.2 Tag Resolution" section in https://yaml.org/spec/1.2.2/
char_int_type ret = m_input_handler.get_range(4, m_value_buffer);
if (ret == end_of_input)
{
Expand Down Expand Up @@ -636,7 +636,7 @@ class lexical_analyzer
case 'o':
// Do not store 'o' since std::strtoull does not support "0o" but "0" as the prefix for octal numbers.
// YAML specifies octal values start with the prefix "0o".
// See "10.3.2. Tag Resolution" section in https://yaml.org/spec/1.2.2/
// See "10.3.2 Tag Resolution" section in https://yaml.org/spec/1.2.2/
return scan_octal_number();
case 'x':
m_value_buffer.push_back(char_traits_type::to_char_type(next));
Expand Down Expand Up @@ -813,7 +813,26 @@ class lexical_analyzer
{
if (!needs_last_double_quote && !needs_last_single_quote)
{
return lexical_token_t::STRING_VALUE;
// Allow a space in an unquoted string only if the space is surrounded by non-space characters.
// See "7.3.3 Plain Style" section in https://yaml.org/spec/1.2.2/
current = m_input_handler.get_next();
switch (current)
{
case ' ':
case '\r':
case '\n':
case '{':
case '}':
case '[':
case ']':
case ',':
case ':':
case '#':
case '\\':
return lexical_token_t::STRING_VALUE;
}
m_input_handler.unget();
current = m_input_handler.get_current();
}
m_value_buffer.push_back(char_traits_type::to_char_type(current));
continue;
Expand Down Expand Up @@ -932,7 +951,7 @@ class lexical_analyzer
}

// Handle escaped characters.
// See "5.7. Escaped Characters" section in https://yaml.org/spec/1.2.2/
// See "5.7 Escaped Characters" section in https://yaml.org/spec/1.2.2/
if (current == '\\')
{
if (!needs_last_double_quote)
Expand Down
1 change: 1 addition & 0 deletions test/unit_test/test_lexical_analyzer_class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ TEST_CASE("LexicalAnalyzerClassTest_ScanStringTokenTest", "[LexicalAnalyzerClass
value_pair_t(std::string(".on"), fkyaml::node::string_type(".on")),
value_pair_t(std::string("foo]"), fkyaml::node::string_type("foo")),
value_pair_t(std::string("foo:bar"), fkyaml::node::string_type("foo:bar")),
value_pair_t(std::string("foo bar"), fkyaml::node::string_type("foo bar")),
value_pair_t(std::string("\"foo bar\""), fkyaml::node::string_type("foo bar")),
value_pair_t(std::string("\"foo:bar\""), fkyaml::node::string_type("foo:bar")),
value_pair_t(std::string("\"foo,bar\""), fkyaml::node::string_type("foo,bar")),
Expand Down

0 comments on commit 6ed3e07

Please # to comment.