From 02f428ab67bca09a7aa8037ee77cee076f3ab699 Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 21 Jan 2021 21:16:47 -0500 Subject: [PATCH] Fixed peglit problem and update README --- README.md | 20 +++++++++++++------- lint/README.md | 24 +++++++++++++++++------- lint/peglint.cc | 37 ++++++++++++++++++------------------- 3 files changed, 48 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index cc89dbe..d62b836 100644 --- a/README.md +++ b/README.md @@ -598,9 +598,8 @@ usage: grammar_file_path [source_file_path] options: --source: source text --ast: show AST tree - --opt, --opt-all: optimaze all AST nodes except nodes selected with --opt-rules - --opt-only: optimaze only AST nodes selected with --opt-rules - --opt-rules rules: CSV definition rules to adjust AST optimazation + --opt, --opt-all: optimaze all AST nodes except nodes selected with `no_ast_opt` instruction + --opt-only: optimaze only AST nodes selected with `no_ast_opt` instruction --trace: show trace messages ``` @@ -662,8 +661,17 @@ Number <- < [0-9]+ > - Multitive[Number] (3) ``` +### Adjust AST optimazation with `no_ast_opt` instruction + ``` -> peglint --ast --opt --opt-rules "Primary" --source "1 + 2 * 3" a.peg +> cat a.peg +Additive <- Multitive '+' Additive / Multitive +Multitive <- Primary '*' Multitive / Primary +Primary <- '(' Additive ')' / Number { no_ast_opt } +Number <- < [0-9]+ > +%whitespace <- [ \t\r\n]* + +> peglint --ast --opt --source "1 + 2 * 3" a.peg + Additive/0 + Multitive/1[Primary] - Number (1) @@ -672,10 +680,8 @@ Number <- < [0-9]+ > - Number (2) + Multitive/1[Primary] - Number (3) -``` -``` -> peglint --ast --opt-only --opt-rules "Primary" --source "1 + 2 * 3" a.peg +> peglint --ast --opt-only --source "1 + 2 * 3" a.peg + Additive/0 + Multitive/1 - Primary/1[Number] (1) diff --git a/lint/README.md b/lint/README.md index 7a7ddec..d6da622 100644 --- a/lint/README.md +++ b/lint/README.md @@ -8,9 +8,8 @@ usage: grammar_file_path [source_file_path] options: --ast: show AST tree - --opt, --opt-all: optimaze all AST nodes except nodes selected with --opt-rules - --opt-only: optimaze only AST nodes selected with --opt-rules - --opt-rules rules: comma delimitted definition rules for optimazation + --opt, --opt-all: optimaze all AST nodes except nodes selected with `no_ast_opt` instruction + --opt-only: optimaze only AST nodes selected with `no_ast_opt` instruction --source: source text --trace: show trace messages ``` @@ -68,6 +67,8 @@ Number <- < [0-9]+ > [commendline]:1:3: syntax error ``` +### AST + ``` > cat a.txt 1 + 2 * 3 @@ -86,6 +87,8 @@ Number <- < [0-9]+ > - Number (3) ``` +### AST optimazation + ``` > peglint --ast --opt --source "1 + 2 * 3" a.peg + Additive @@ -95,8 +98,17 @@ Number <- < [0-9]+ > - Multitive[Number] (3) ``` +### Adjust AST optimazation with `no_ast_opt` instruction + ``` -> peglint --ast --opt --opt-rules "Primary" --source "1 + 2 * 3" a.peg +> cat a.peg +Additive <- Multitive '+' Additive / Multitive +Multitive <- Primary '*' Multitive / Primary +Primary <- '(' Additive ')' / Number { no_ast_opt } +Number <- < [0-9]+ > +%whitespace <- [ \t\r\n]* + +> peglint --ast --opt --source "1 + 2 * 3" a.peg + Additive/0 + Multitive/1[Primary] - Number (1) @@ -105,10 +117,8 @@ Number <- < [0-9]+ > - Number (2) + Multitive/1[Primary] - Number (3) -``` -``` -> peglint --ast --opt-only --opt-rules "Primary" --source "1 + 2 * 3" a.peg +> peglint --ast --opt-only --source "1 + 2 * 3" a.peg + Additive/0 + Multitive/1 - Primary/1[Number] (1) diff --git a/lint/peglint.cc b/lint/peglint.cc index b85b6bf..1131207 100644 --- a/lint/peglint.cc +++ b/lint/peglint.cc @@ -1,7 +1,7 @@ // // peglint.cc // -// Copyright (c) 2015 Yuji Hirose. All rights reserved. +// Copyright (c) 2021 Yuji Hirose. All rights reserved. // MIT License // @@ -35,7 +35,7 @@ inline vector split(const string &s, char delim) { int main(int argc, const char **argv) { auto opt_ast = false; auto opt_optimize = false; - vector opt_rules; + auto opt_mode = true; auto opt_help = false; auto opt_source = false; vector source; @@ -49,8 +49,12 @@ int main(int argc, const char **argv) { opt_help = true; } else if (string("--ast") == arg) { opt_ast = true; - } else if (string("--opt") == arg) { + } else if (string("--opt") == arg || string("--opt-all") == arg) { opt_optimize = true; + opt_mode = true; + } else if (string("--opt-only") == arg) { + opt_optimize = true; + opt_mode = false; } else if (string("--source") == arg) { opt_source = true; if (argi < argc) { @@ -65,21 +69,16 @@ int main(int argc, const char **argv) { } if (path_list.empty() || opt_help) { - cerr << "usage: grammar_file_path [source_file_path]" << endl - << endl - << " options:" << endl - << " --source: source text" << endl - << " --ast: show AST tree" << endl - << " --opt, --opt-all: optimaze all AST nodes except nodes " - "selected with " - "--opt-rules" - << endl - << " --opt-only: optimaze only AST nodes selected with --opt-rules" - << endl - << " --opt-rules rules: CSV definition rules to adjust AST " - "optimazation" - << endl - << " --trace: show trace messages" << endl; + cerr << R"(usage: grammar_file_path [source_file_path] + + options: + --source: source text + --ast: show AST tree + --opt, --opt-all: optimaze all AST nodes except nodes selected with `no_ast_opt` instruction + --opt-only: optimaze only AST nodes selected with `no_ast_opt` instruction + --trace: show trace messages +)"; + return 1; } @@ -180,7 +179,7 @@ int main(int argc, const char **argv) { if (ast) { if (opt_optimize) { - ast = parser.optimize_ast(ast); + ast = parser.optimize_ast(ast, opt_mode); } std::cout << peg::ast_to_s(ast); }