Guest User

Untitled

a guest
Oct 21st, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.35 KB | None | 0 0
  1. error: no viable conversion from returned value of type
  2. '(lambda at ./html_parser.hpp:53:9)' to function return type 'Parser<char>' (aka
  3. 'std::__1::function<std::__1::optional<std::__1::pair<char, std::__1::basic_string<char> > >
  4. (std::__1::basic_string<char>)>')
  5. return [=](std::string& input) -> ParserResult<char> {
  6. ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  7. /home/acid/tools/include/c++/v1/functional:1627:5: note: candidate constructor not viable: no known conversion
  8. from '(lambda at ./html_parser.hpp:53:9)' to 'std::nullptr_t' (aka 'nullptr_t') for 1st argument
  9. function(nullptr_t) _NOEXCEPT : __f_(0) {}
  10. ^
  11. /home/acid/tools/include/c++/v1/functional:1628:5: note: candidate constructor not viable: no known conversion
  12. from '(lambda at ./html_parser.hpp:53:9)' to 'const
  13. std::__1::function<std::__1::optional<std::__1::pair<char, std::__1::basic_string<char> > >
  14. (std::__1::basic_string<char>)> &' for 1st argument
  15. function(const function&);
  16. ^
  17. /home/acid/tools/include/c++/v1/functional:1629:5: note: candidate constructor not viable: no known conversion
  18. from '(lambda at ./html_parser.hpp:53:9)' to 'std::__1::function<std::__1::optional<std::__1::pair<char,
  19. std::__1::basic_string<char> > > (std::__1::basic_string<char>)> &&' for 1st argument
  20. function(function&&) _NOEXCEPT;
  21. ^
  22. /home/acid/tools/include/c++/v1/functional:1631:5: note: candidate template ignored: requirement
  23. '__callable<(lambda at ./html_parser.hpp:53:9)>::value' was not satisfied [with _Fp =
  24. (lambda at ./html_parser.hpp:53:9)]
  25. function(_Fp);
  26. ^
  27. 1 error generated.
  28.  
  29. #pragma once
  30.  
  31. #include <string>
  32. #include <utility>
  33. #include <functional>
  34. #include <optional>
  35.  
  36. namespace dragon {
  37. namespace html {
  38. namespace parser {
  39. template <typename ParserOutput, typename ParserInput = std::string>
  40. using ParserResult = std::optional<std::pair<ParserOutput, ParserInput>>;
  41.  
  42. template<typename ParserOutput, typename ParserInput = std::string>
  43. using Parser = std::function<ParserResult<ParserOutput, ParserInput>(ParserInput)>;
  44.  
  45. template <typename ParserOutput, typename ParserInput = std::string>
  46. auto parse(Parser<ParserOutput, ParserInput> p, ParserInput i) -> ParserResult<ParserOutput, ParserInput>{
  47. return p(i);
  48. }
  49.  
  50. // few parser combinators.
  51.  
  52. // thenP combinator: applies the first parser, if it succeeds apply the second to the rest of
  53. // the input left over by the first parser.
  54. // currently just fails and returns empty!! does not provide any debugging info/msg
  55. // as to why the parsing failed.
  56. template<typename FirstParser, typename SecondParser>
  57. auto thenP(FirstParser f, SecondParser s) {
  58. return [=](std::string input) -> decltype(parse(s, std::string())) {
  59. auto fv = parse(f, input);
  60.  
  61. if (fv) {
  62. auto fvv = *fv;
  63. return parse(s, fvv.second);
  64. }
  65. else {
  66. return {};
  67. }
  68. };
  69. }
  70.  
  71. template<typename FirstParser, typename SecondParser>
  72. auto choiceP(FirstParser f, SecondParser s) {
  73. return [=](std::string input) {
  74. auto fv = parse(f, input);
  75. if (!fv) return parse(s, input);
  76. return fv;
  77. };
  78. }
  79.  
  80. auto charP(char match) -> Parser<char> {
  81. return [=](std::string& input) -> ParserResult<char> {
  82. if ((input.empty() == false) && (input[0] == match)) {
  83. return std::make_pair(input[0], input.substr(1));
  84. }
  85. return {};
  86. };
  87. }
  88. }
  89. }
  90. }
  91.  
  92. int main()
  93. {
  94. auto less = Parser::parser::charP('<');
  95. auto greater = Parser::parser::charP('>');
  96.  
  97. auto lag = Parser::parser::thenP(less, greater);
  98. auto log = Parser::parser::choiceP(less, greater);
  99.  
  100. auto lagv = lag("<>");
  101. auto logv = log("|>");
  102.  
  103. return 0;
  104. }
  105.  
  106. auto charP(char match) -> Parser<char> {
  107. return [=](std::string& input) -> ParserResult<char> { ... };
  108. }
  109.  
  110. std::function<void(int)> f = [](int& ){};
Add Comment
Please, Sign In to add comment