Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ CPP ] Throw runtime exception on parsing error #288

Merged
merged 1 commit into from
Mar 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 40 additions & 4 deletions source/src/BNFC/Backend/CPP/STL.hs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ makeCppStl opts cf = do
mkfile (name ++ ".y") bison
let header = mkHeaderFile (inPackage opts) cf (allParserCats cf) (allEntryPoints cf) (Map.elems env)
mkfile "Parser.H" header
mkfile "ParserError.H" (printParseErrHeader (inPackage opts) cf)
let (skelH, skelC) = cf2CVisitSkel True (inPackage opts) cf
mkfile "Skeleton.H" skelH
mkfile "Skeleton.C" skelC
Expand All @@ -60,7 +61,35 @@ makeCppStl opts cf = do
Makefile.mkMakefile opts $ makefile name
where name = lang opts


printParseErrHeader :: Maybe String -> CF -> String
printParseErrHeader inPackage cf =
unlines
[
" #pragma once "
, " #include <string>"
, " #include <stdexcept>"
, ""
, nsStart inPackage
, " class parse_error : public std::runtime_error"
, " {"
, " public:"
, " parse_error(int line, std::string str)"
, " : std::runtime_error(str)"
, " , m_line(line) {}"
, " int getLine() {"
, " return m_line;"
, " } "
, " private:"
, " int m_line;"
, " }; "
, nsEnd inPackage
]
where
cat = head $ allEntryPoints cf
dat = identCat $ normCat cat
def = identCat cat
scope = nsScope inPackage

cpptest :: Maybe String -> CF -> String
cpptest inPackage cf =
unlines
Expand All @@ -71,11 +100,13 @@ cpptest inPackage cf =
"/* pretty-print the result. */",
"/* */",
"/****************************************************************************/",
"#include <stdio.h>",
"#include <string.h>",
"#include <cstdio>",
"#include <string>",
"#include <iostream>",
"#include \"Parser.H\"",
"#include \"Printer.H\"",
"#include \"Absyn.H\"",
"#include \"ParserError.H\"",
"",
"void usage() {",
" printf(\"usage: Call with one of the following argument " ++
Expand Down Expand Up @@ -114,7 +145,12 @@ cpptest inPackage cf =
" }",
" } else input = stdin;",
" /* The default entry point is used. For other options see Parser.H */",
" " ++ scope ++ dat ++ " *parse_tree = " ++ scope ++ "p" ++ def ++ "(input);",
" " ++ scope ++ dat ++ " *parse_tree = nullptr;",
" try { ",
" parse_tree = " ++ scope ++ "p" ++ def ++ "(input);",
" } catch( " ++ scope ++ "parse_error &e) {",
" std::cout << \"Parse error on line \" << e.getLine(); ",
" }",
" if (parse_tree)",
" {",
" printf(\"\\nParse Successful!\\n\");",
Expand Down
4 changes: 2 additions & 2 deletions source/src/BNFC/Backend/CPP/STL/CFtoBisonSTL.hs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ header inPackage name cf = unlines
, "#include <stdio.h>"
, "#include <string.h>"
, "#include <algorithm>"
, "#include \"ParserError.H\""
, "#include \"Absyn.H\""
, ""
, "#define YYMAXDEPTH 10000000" -- default maximum stack size is 10000, but right-recursion needs O(n) stack
Expand All @@ -124,8 +125,7 @@ header inPackage name cf = unlines
, "void " ++ ns ++ "yyerror(const char *str)"
, "{"
, " extern char *"++ns++"yytext;"
, " fprintf(stderr,\"error: line %d: %s at %s\\n\", "
, " "++ns++"yy_mylinenumber, str, "++ns++"yytext);"
, " throw "++ns++"::parse_error("++ ns ++ "yy_mylinenumber,str);"
, "}"
, ""
, nsStart inPackage
Expand Down