From be7dd4af9a993b1c46ae8e2a475988471568e055 Mon Sep 17 00:00:00 2001 From: Jean Privat Date: Wed, 29 Nov 2017 13:48:57 -0500 Subject: [PATCH] nitlight: add --txt for text-based highlighting Signed-off-by: Jean Privat --- src/highlight.nit | 46 +++++++++++++++++++++++++++++ src/nitlight.nit | 26 ++++++++++++++++- tests/nitlight.args | 1 + tests/sav/nitlight_args4.res | 66 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 tests/sav/nitlight_args4.res diff --git a/src/highlight.nit b/src/highlight.nit index dc761f5..d2411fb 100644 --- a/src/highlight.nit +++ b/src/highlight.nit @@ -17,6 +17,7 @@ module highlight import frontend import astutil +import console # Visitor used to produce a HTML tree based on a AST on a `Source` class AbstractHighlightVisitor @@ -109,3 +110,48 @@ class AbstractHighlightVisitor # Low-level highlighting between 2 tokens protected fun do_highlight(first_token: Token, last_token: nullable Token) is abstract end + +# Text-based highlighter that use ANSI escape sequence for colors +class AnsiHighlightVisitor + super AbstractHighlightVisitor + + # The produced highlighting + var result = new Template + + redef fun do_highlight(f, l) + do + var c + c = f + while c != null do + if c != f then result.add(c.blank_before) + result.add c.ansi_colored + + if c == l then + c = null + else + c = c.next_token + end + end + end +end + +redef class Token + # Return the ANSI colored text + fun ansi_colored: String do return text +end + +redef class TComment + redef fun ansi_colored do return super.blue +end + +redef class TokenKeyword + redef fun ansi_colored do return super.yellow +end + +redef class TClassid + redef fun ansi_colored do return super.green +end + +redef class TokenLiteral + redef fun ansi_colored do return super.red +end diff --git a/src/nitlight.nit b/src/nitlight.nit index e536f04..c76665e 100644 --- a/src/nitlight.nit +++ b/src/nitlight.nit @@ -94,7 +94,8 @@ var opt_last_line = new OptionInt("End the source file at this line (default: to var opt_dir = new OptionString("Output html files in a specific directory (required if more than one module)", "-d", "--dir") var opt_full = new OptionBool("Process also imported modules", "--full") var opt_ast = new OptionBool("Generate specific HTML elements for each Node of the AST", "--ast") -toolcontext.option_context.add_option(opt_fragment, opt_line_id_prefix, opt_first_line, opt_last_line, opt_dir, opt_full) +var opt_txt = new OptionBool("Generate text with ANSI coloring escape sequences", "--txt") +toolcontext.option_context.add_option(opt_fragment, opt_line_id_prefix, opt_first_line, opt_last_line, opt_dir, opt_full, opt_ast, opt_txt) toolcontext.tooldescription = "Usage: nitlight [OPTION]... ...\nGenerates HTML of highlited code from Nit source files." toolcontext.process_options(args) @@ -116,6 +117,29 @@ else if mmodules.length > 1 then return end +if opt_txt.value then + for mm in mmodules do + var v = new AnsiHighlightVisitor + v.include_loose_tokens = true + v.include_whole_lines = true + + if opt_first_line.value != 0 then v.first_line = opt_first_line.value + if opt_last_line.value != 0 then v.last_line = opt_last_line.value + var m = modelbuilder.mmodule2node(mm) + assert m != null + + v.highlight_node(m) + var page = v.result + + if dir != null then + page.write_to_file("{dir}/{mm.c_name}.txt") + else + page.write_to(stdout) + end + end + return +end + for mm in mmodules do if dir != null then toolcontext.info("write {dir}/{mm.c_name}.html", 1) diff --git a/tests/nitlight.args b/tests/nitlight.args index 0e7de6a..4f02479 100644 --- a/tests/nitlight.args +++ b/tests/nitlight.args @@ -1,3 +1,4 @@ -f base_simple3.nit base_simple3.nit -f --line-id-prefix XYZ --first-line 38 --last-line 46 base_simple3.nit +--txt base_simple3.nit diff --git a/tests/sav/nitlight_args4.res b/tests/sav/nitlight_args4.res new file mode 100644 index 0000000..9104b4b --- /dev/null +++ b/tests/sav/nitlight_args4.res @@ -0,0 +1,66 @@ +# This file is part of NIT ( http://www.nitlanguage.org ). +# +# Copyright 2006-2008 Jean Privat +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import end + +interface Object +end + +enum Bool +end + +enum Int + fun output is intern +end + +class A + init do 5.output + fun run do 6.output +end + +class B + var val: Int + init(v: Int) + do + 7.output + self.val = v + end + fun run do val.output +end + +class C + var val1: Int + var val2: Int = 10 +end + +fun foo do 2.output +fun bar(i: Int) do i.output +fun baz: Int do return 4 + +1.output +foo +bar(3) +baz.output + +var a = new A +a.run + +var b = new B(8) +b.run + +var c = new C(9) +c.val1.output +c.val2.output -- 1.7.9.5