From 19c83811ee806922a6a6d26b94d759ab8e5080c9 Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 25 May 2020 22:48:42 -0400 Subject: [PATCH] Added --opt to peglint --- README.md | 26 ++++++++++- lint/README.md | 114 +++++++++++++++++++++++++++++++++++++++++++++++- lint/peglint.cc | 5 ++- 3 files changed, 141 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 5fbd213..65a486b 100644 --- a/README.md +++ b/README.md @@ -535,7 +535,7 @@ usage: grammar_file_path [source_file_path] options: --ast: show AST tree - --opt: optimaze all AST nodes except nodes selected with --opt-rules + --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 --source: source text @@ -612,6 +612,30 @@ Number <- < [0-9]+ > - Multitive[Number] (3) ``` +``` +> peglint --ast --opt --opt-rules "Primary" --source "1 + 2 * 3" a.peg ++ Additive/0 + + Multitive/1[Primary] + - Number (1) + + Additive/1[Multitive] + + Primary/1 + - Number (2) + + Multitive/1[Primary] + - Number (3) +``` + +``` +> peglint --ast --opt-only --opt-rules "Primary" --source "1 + 2 * 3" a.peg ++ Additive/0 + + Multitive/1 + - Primary/1[Number] (1) + + Additive/1 + + Multitive/0 + - Primary/1[Number] (2) + + Multitive/1 + - Primary/1[Number] (3) +``` + Sample codes ------------ diff --git a/lint/README.md b/lint/README.md index 7300fe4..ef55c0a 100644 --- a/lint/README.md +++ b/lint/README.md @@ -4,5 +4,117 @@ peglint The lint utility for PEG. ``` -usage: peglint [--ast] [--optimize_ast_nodes|--opt] [--trace] [grammar file path] [source file path] +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 + --source: source text + --trace: show trace messages +``` + +### Build peglint + +``` +> cd lint +> mkdir build +> cd build +> cmake .. +> make +``` + +### Lint grammar + +``` +> cat a.peg +A <- 'hello' ^ 'world' + +> peglint a.peg +a.peg:1:14: syntax error +``` + +``` +> cat a.peg +A <- B + +> peglint a.peg +a.peg:1:6: 'B' is not defined. +``` + +``` +> cat a.peg +A <- B / C +B <- 'b' +C <- A + +> peglint a.peg +a.peg:1:10: 'C' is left recursive. +a.peg:3:6: 'A' is left recursive. +``` + +### Lint source text + +``` +> cat a.peg +Additive <- Multitive '+' Additive / Multitive +Multitive <- Primary '*' Multitive / Primary +Primary <- '(' Additive ')' / Number +Number <- < [0-9]+ > +%whitespace <- [ \t\r\n]* + +> peglint --source "1 + a * 3" a.peg +[commendline]:1:3: syntax error +``` + +``` +> cat a.txt +1 + 2 * 3 + +> peglint --ast a.peg a.txt ++ Additive + + Multitive + + Primary + - Number (1) + + Additive + + Multitive + + Primary + - Number (2) + + Multitive + + Primary + - Number (3) +``` + +``` +> peglint --ast --opt --source "1 + 2 * 3" a.peg ++ Additive + - Multitive[Number] (1) + + Additive[Multitive] + - Primary[Number] (2) + - Multitive[Number] (3) +``` + +``` +> peglint --ast --opt --opt-rules "Primary" --source "1 + 2 * 3" a.peg ++ Additive/0 + + Multitive/1[Primary] + - Number (1) + + Additive/1[Multitive] + + Primary/1 + - Number (2) + + Multitive/1[Primary] + - Number (3) +``` + +``` +> peglint --ast --opt-only --opt-rules "Primary" --source "1 + 2 * 3" a.peg ++ Additive/0 + + Multitive/1 + - Primary/1[Number] (1) + + Additive/1 + + Multitive/0 + - Primary/1[Number] (2) + + Multitive/1 + - Primary/1[Number] (3) ``` diff --git a/lint/peglint.cc b/lint/peglint.cc index f8a77ff..930fd2e 100644 --- a/lint/peglint.cc +++ b/lint/peglint.cc @@ -50,7 +50,7 @@ int main(int argc, const char **argv) { opt_help = true; } else if (string("--ast") == arg) { opt_ast = true; - } else if (string("--opt-all") == arg) { + } else if (string("--opt") == arg || string("--opt-all") == arg) { opt_optimize = true; opt_mode = true; } else if (string("--opt-only") == arg) { @@ -80,7 +80,8 @@ int main(int argc, const char **argv) { << " options:" << endl << " --source: source text" << endl << " --ast: show AST tree" << endl - << " --opt-all: optimaze all AST nodes except nodes selected with " + << " --opt, --opt-all: optimaze all AST nodes except nodes " + "selected with " "--opt-rules" << endl << " --opt-only: optimaze only AST nodes selected with --opt-rules"