Skip to content

Commit

Permalink
Merge pull request #2390 from sanssecours/😴
Browse files Browse the repository at this point in the history
YAwn: Fix Comment Handling & Improve Error Messages
  • Loading branch information
markus2330 committed Feb 10, 2019
2 parents 7d6b724 + e8c4046 commit 8ef7b9b
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 9 deletions.
5 changes: 5 additions & 0 deletions doc/news/_preparation_next_release.md
Expand Up @@ -118,6 +118,11 @@ Simply add `check/path/user <user>` and `check/path/mode <modes>` as metadata
and be assured that you can safely set a path value to the key. A more detailed explanation can be found
[here](/src/plugins/path/README.md) *(Michael Zronek)*

### YAwn

- The plugin now handles comments at the end of a file properly. *(René Schwaiger)*
- We improved the syntax error messages of the plugin. *(René Schwaiger)*
- We fixed a memory leak that occurred, if a YAML file contained syntax errors. *(René Schwaiger)*

### YAy PEG

Expand Down
29 changes: 29 additions & 0 deletions src/plugins/yawn/README.md
Expand Up @@ -69,6 +69,35 @@ kdb rm -r user/tests/yawn
sudo kdb umount user/tests/yawn
```

### Error Messages

```sh
# Mount plugin
sudo kdb mount config.yaml user/tests/yawn yawn

# Manually add some data
printf -- ' - Brutus\n' > `kdb file user/tests/yawn`
# Add element with incorrect indentation
printf -- '- Burst' >> `kdb file user/tests/yawn`

# Try to retrieve data
kdb ls user/tests/yawn
# RET: 5
# STDERR-REGEX: Reason: .*/config.yaml:2:1: Syntax error on token number 5: “<Token, SEQUENCE_START, SEQUENCE START, 2:1–2:1>”

# Fix syntax error
printf -- ' - Brutus\n' > `kdb file user/tests/yawn`
printf -- ' - Burst' >> `kdb file user/tests/yawn`
kdb ls user/tests/yawn
#> user/tests/yawn
#> user/tests/yawn/#0
#> user/tests/yawn/#1

# Undo modifications
kdb rm -r user/tests/yawn
sudo kdb umount user/tests/yawn
```

## Limitations

The plugin has the same limitations as [YAMBi ](../yambi/).
12 changes: 6 additions & 6 deletions src/plugins/yawn/convert.cpp
Expand Up @@ -23,10 +23,6 @@
#include "listener.hpp"
#include "walk.hpp"

#ifdef ENABLE_ASAN
#include <sanitizer/lsan_interface.h>
#endif

using std::cerr;
using std::cout;
using std::endl;
Expand Down Expand Up @@ -157,7 +153,7 @@ int handleErrors (int const ambiguousOutput, ErrorListener const & errorListener

if (errorListener.getNumberOfErrors () > 0)
{
ELEKTRA_SET_ERROR (ELEKTRA_ERROR_PARSE, error.getKey (), errorListener.getErrorMessage ().c_str ());
ELEKTRA_SET_ERROR (ELEKTRA_ERROR_PARSE, error.getKey (), (filename + ":" + errorListener.getErrorMessage ()).c_str ());
return -1;
}
return 0;
Expand Down Expand Up @@ -205,7 +201,11 @@ int addToKeySet (CppKeySet & keySet, CppKey & parent, string const & filename)

parser.parse (nextToken, syntaxError, nullptr, nullptr, &root, &ambiguousOutput);

if (handleErrors (ambiguousOutput, errorListener, filename, grammar, parent) < 0) return -1;
if (handleErrors (ambiguousOutput, errorListener, filename, grammar, parent) < 0)
{
yaep::free_tree (root, nullptr, nullptr);
return -1;
}

Listener listener{ parent };
walk (listener, root);
Expand Down
5 changes: 3 additions & 2 deletions src/plugins/yawn/error_listener.cpp
Expand Up @@ -47,8 +47,9 @@ void ErrorListener::syntaxError (int errorTokenNumber, void * errorTokenData, in
void * recoveredTokenData __attribute__ ((unused)))
{
errors++;
message = "Syntax error on token number " + to_string (errorTokenNumber) + ": “" +
to_string (**static_cast<unique_ptr<Token> *> (errorTokenData)) + "\n";
auto token = **static_cast<unique_ptr<Token> *> (errorTokenData);
message = to_string (token.getStart ().line) + ":" + to_string (token.getStart ().column) + ": Syntax error on token number " +
to_string (errorTokenNumber) + ": “" + to_string (token) + "\n";
if (ignoredToken > 0)
{
message += "Ignoring " + to_string (recoveredToken - ignoredToken) + " tokens starting with token number " +
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/yawn/lexer.cpp
Expand Up @@ -405,7 +405,7 @@ void Lexer::scanComment ()
{
ELEKTRA_LOG_DEBUG ("Scan comment");
size_t start = input.index ();
while (input.LA (1) != '\n')
while (input.LA (1) != '\n' && input.LA (1) != 0)
{
forward ();
}
Expand Down

0 comments on commit 8ef7b9b

Please sign in to comment.