diff --git a/src/main.cpp b/src/main.cpp index 0b18ad5..7652fb1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -17,18 +17,18 @@ void dump_tokens(Scanner &scanner) int tok = scanner.lex(&lval); if (tok == Parser::token::TOK_EOF) { - std::cout << Parser::symbol_name(static_cast(tok)); + // std::cout << Parser::symbol_name(static_cast(tok)); break; } - std::cout << Parser::symbol_name(static_cast(tok)); + // std::cout << Parser::symbol_name(static_cast(tok)); if (tok == Parser::token::TOK_IDENTIFIER || tok == Parser::token::TOK_NUMBER || tok == Parser::token::TOK_STRING) { - std::cout << " " << lval.as(); + // std::cout << " " << lval.as(); } - std::cout << "\n"; + // std::cout << "\n"; } } @@ -39,6 +39,8 @@ int main(int argc, char *argv[]) bool token_dump = false; bool ast_dump = false; + bool write_ruby = true; + bool no_parsing = false; for (int i = 1; i < argc; ++i) { @@ -56,10 +58,22 @@ int main(int argc, char *argv[]) { ast_dump = true; } + else if (arg == "--no-output") + { + write_ruby = false; + } + else if (arg == "--no-parsing") + { + no_parsing = true; + } } Scanner scanner(*input_stream); if (token_dump) + { + scanner.print_results = true; + } + if (no_parsing) { dump_tokens(scanner); return 0; @@ -72,9 +86,11 @@ int main(int argc, char *argv[]) if (ast_dump) { std::cout << ast_root->dump(); - return 0; } - RubyGenerator generator; - std::cout << generator.generate(*ast_root); + if (write_ruby) + { + RubyGenerator generator; + std::cout << generator.generate(*ast_root); + } } \ No newline at end of file diff --git a/src/scanner.hpp b/src/scanner.hpp index 70ab7d5..b71ad76 100644 --- a/src/scanner.hpp +++ b/src/scanner.hpp @@ -14,6 +14,7 @@ class Scanner : public yyFlexLexer bool last_was_newline = true; public: + bool print_results = false; IndentStack indent_stack; Scanner(std::istream &in) : yyFlexLexer(&in), yylval(nullptr) {} @@ -24,13 +25,15 @@ public: if (eof_emitted) { - // std::cout << "lex: 0\n"; return 0; } if (indent_stack.popDedent()) { - // std::cout << "lex: dedent\n"; + if (print_results) + { + std::cout << "DEDENT\n"; + } return yy::parser::token::TOK_DEDENT; } @@ -39,18 +42,23 @@ public: if (token == yy::parser::token::TOK_EOF) { - // Emit synthetic NEWLINE if needed if (!last_was_newline) { last_was_newline = true; - // std::cout << "lex: newline\n"; + if (print_results) + { + std::cout << "NEWLINE\n"; + } return yy::parser::token::TOK_NEWLINE; } auto result = indent_stack.closeBlock(); if (result == IndentAction::Dedent) { - // std::cout << "lex: dedent\n"; + if (print_results) + { + std::cout << "DEDENT\n"; + } return yy::parser::token::TOK_DEDENT; } @@ -59,7 +67,24 @@ public: last_was_newline = (token == yy::parser::token::TOK_NEWLINE); - // std::cout << "lex: " << Parser::symbol_name(static_cast(token)) << '\n'; + if (print_results) + { + std::cout << Parser::symbol_name(static_cast(token)); + } + + if (token == Parser::token::TOK_IDENTIFIER || token == Parser::token::TOK_NUMBER || token == Parser::token::TOK_STRING) + { + if (print_results) + { + std::cout << " " << lval->as(); + } + } + + if (print_results) + { + std::cout << "\n"; + } + return token; } diff --git a/test/lexer/01.out b/test/lexer/01.out index fb41d35..71022be 100644 --- a/test/lexer/01.out +++ b/test/lexer/01.out @@ -12,4 +12,4 @@ IDENTIFIER y MINUS IDENTIFIER z NEWLINE -EOF \ No newline at end of file +EOF diff --git a/test/lexer/02.out b/test/lexer/02.out index c4dd2ce..6db4f33 100644 --- a/test/lexer/02.out +++ b/test/lexer/02.out @@ -28,4 +28,4 @@ R_PAREN NEWLINE DEDENT DEDENT -EOF \ No newline at end of file +EOF diff --git a/test/lexer/03.out b/test/lexer/03.out index 3a9fa37..0d5d7a4 100644 --- a/test/lexer/03.out +++ b/test/lexer/03.out @@ -70,4 +70,4 @@ KW_RETURN IDENTIFIER result NEWLINE DEDENT -EOF \ No newline at end of file +EOF diff --git a/test/lexer/04.out b/test/lexer/04.out index 8855e71..1151fdb 100644 --- a/test/lexer/04.out +++ b/test/lexer/04.out @@ -31,4 +31,4 @@ COLON DOT SEMICOLON NEWLINE -EOF \ No newline at end of file +EOF diff --git a/test/lexer/05.out b/test/lexer/05.out index e1dcf04..2bd6e3a 100644 --- a/test/lexer/05.out +++ b/test/lexer/05.out @@ -10,4 +10,4 @@ NEWLINE STRING 'test' STRING "escaped\"quote" NEWLINE -EOF \ No newline at end of file +EOF diff --git a/test/lexer/06.out b/test/lexer/06.out index d62e9e9..d57a716 100644 --- a/test/lexer/06.out +++ b/test/lexer/06.out @@ -48,4 +48,4 @@ COMMA NUMBER 15 R_PAREN NEWLINE -EOF \ No newline at end of file +EOF diff --git a/test/lexer/07.out b/test/lexer/07.out index 8dfccba..bb61e99 100644 --- a/test/lexer/07.out +++ b/test/lexer/07.out @@ -18,4 +18,4 @@ IDENTIFIER very_long_variable_name_123 ASSIGN STRING "test" NEWLINE -EOF \ No newline at end of file +EOF diff --git a/test/lexer/08.out b/test/lexer/08.out index fe81bd9..098abc4 100644 --- a/test/lexer/08.out +++ b/test/lexer/08.out @@ -16,4 +16,4 @@ NEWLINE KW_NOT IDENTIFIER flag NEWLINE -EOF \ No newline at end of file +EOF diff --git a/test/lexer/09.out b/test/lexer/09.out index 071607d..36a776c 100644 --- a/test/lexer/09.out +++ b/test/lexer/09.out @@ -10,4 +10,4 @@ INDENT KW_PASS NEWLINE DEDENT -EOF \ No newline at end of file +EOF diff --git a/test/lexer/10.out b/test/lexer/10.out index 17d5191..f6c1508 100644 --- a/test/lexer/10.out +++ b/test/lexer/10.out @@ -10,4 +10,4 @@ IDENTIFIER e RSHIFT IDENTIFIER f NEWLINE -EOF \ No newline at end of file +EOF diff --git a/test/lexer/11.out b/test/lexer/11.out index ea0e88d..8ebbac7 100644 --- a/test/lexer/11.out +++ b/test/lexer/11.out @@ -38,4 +38,4 @@ IDENTIFIER g RSHIFT_ASSIGN NUMBER 10 NEWLINE -EOF \ No newline at end of file +EOF diff --git a/test/lexer/12.out b/test/lexer/12.out index b6e05be..ef590b8 100644 --- a/test/lexer/12.out +++ b/test/lexer/12.out @@ -16,4 +16,4 @@ L_BRACKET IDENTIFIER index R_BRACKET NEWLINE -EOF \ No newline at end of file +EOF diff --git a/test/lexer/13.out b/test/lexer/13.out index efbdde2..445f335 100644 --- a/test/lexer/13.out +++ b/test/lexer/13.out @@ -38,4 +38,4 @@ COMMA NUMBER 3 R_BRACE NEWLINE -EOF \ No newline at end of file +EOF diff --git a/test/lexer/14.out b/test/lexer/14.out index 948b426..24c6fb6 100644 --- a/test/lexer/14.out +++ b/test/lexer/14.out @@ -4,4 +4,4 @@ STRING 'quote\'test' NEWLINE STRING "unicode ☃ snowman" NEWLINE -EOF \ No newline at end of file +EOF diff --git a/test/lexer/15.out b/test/lexer/15.out index 884951b..4d8a482 100644 --- a/test/lexer/15.out +++ b/test/lexer/15.out @@ -13,4 +13,4 @@ INDENT KW_PASS NEWLINE DEDENT -EOF \ No newline at end of file +EOF diff --git a/test/lexer/16.out b/test/lexer/16.out index 8c4f25a..86910d6 100644 --- a/test/lexer/16.out +++ b/test/lexer/16.out @@ -12,4 +12,4 @@ NUMBER 5 BIT_XOR NUMBER 6 NEWLINE -EOF \ No newline at end of file +EOF diff --git a/test/lexer/17.out b/test/lexer/17.out index ce93f0e..0dd8837 100644 --- a/test/lexer/17.out +++ b/test/lexer/17.out @@ -10,4 +10,4 @@ IDENTIFIER s3 ASSIGN STRING "mixed 'quotes' \"inside\"" NEWLINE -EOF \ No newline at end of file +EOF diff --git a/test/lexer/18.out b/test/lexer/18.out index 7a1502e..45eb69d 100644 --- a/test/lexer/18.out +++ b/test/lexer/18.out @@ -26,4 +26,4 @@ NUMBER 7 R_BRACE R_BRACKET NEWLINE -EOF \ No newline at end of file +EOF diff --git a/test/lexer/19.out b/test/lexer/19.out index 36a87a5..04e1a3b 100644 --- a/test/lexer/19.out +++ b/test/lexer/19.out @@ -167,4 +167,4 @@ COMMA NUMBER 15 R_PAREN NEWLINE -EOF \ No newline at end of file +EOF diff --git a/test/tester.sh b/test/tester.sh index 475658b..6879b2a 100755 --- a/test/tester.sh +++ b/test/tester.sh @@ -6,7 +6,7 @@ for test in lexer/*.in; do name=$(basename "$test" .in) echo "Testing lexer/$name..." - if ! ../build/py2rb --dump-tokens < "$test" | diff - "lexer/${name}.out"; then + if ! ../build/py2rb --dump-tokens --no-output --no-parsing < "$test" | diff - "lexer/${name}.out"; then echo "FAILED: lexer/$name" exit 1 fi @@ -19,7 +19,7 @@ for test in parser/*.in; do name=$(basename "$test" .in) echo "Testing parser/$name..." - if ! ../build/py2rb --dump-ast < "$test" | diff - "parser/${name}.out"; then + if ! ../build/py2rb --dump-ast --no-output < "$test" | diff - "parser/${name}.out"; then echo "FAILED: parser/$name" exit 1 fi