From 2a920aab6b62f2cb192afd55e4a1b8062698fdfd Mon Sep 17 00:00:00 2001 From: Jean Privat Date: Wed, 29 Nov 2017 14:27:40 -0500 Subject: [PATCH] highlight: move hightlightcode inside HighlightVisitor Signed-off-by: Jean Privat --- src/examples/nitlight_as_a_service.nit | 4 +- src/highlight.nit | 90 ++++++++++++++++---------------- src/web/api_light.nit | 2 +- 3 files changed, 48 insertions(+), 48 deletions(-) diff --git a/src/examples/nitlight_as_a_service.nit b/src/examples/nitlight_as_a_service.nit index 103a3c8..cf72574 100644 --- a/src/examples/nitlight_as_a_service.nit +++ b/src/examples/nitlight_as_a_service.nit @@ -24,7 +24,7 @@ import nitcorn::log import template import json::serialization_write -# Nitcorn service to hightlight code +# Nitcorn service to highlight code # # It's a single stand-alone page that has to form to itself. class HighlightAction @@ -38,7 +38,7 @@ class HighlightAction # There is code? Process it var code = http_request.post_args.get_or_null("code") var hlcode = null - if code != null then hlcode = hightlightcode(hl, code) + if code != null then hlcode = hl.highlightcode(code) if http_request.post_args.get_or_null("json") == "true" and hlcode != null then var response = new HttpResponse(200) diff --git a/src/highlight.nit b/src/highlight.nit index da18cce..7b2f718 100644 --- a/src/highlight.nit +++ b/src/highlight.nit @@ -21,51 +21,6 @@ import pipeline import astutil import serialization -# Fully process `content` as a Nit source file. -# -# Set `print_errors = true` to print errors in the code to the console. -fun hightlightcode(hl: HighlightVisitor, content: String, print_errors: nullable Bool): HLCode -do - # Prepare a stand-alone tool context - var tc = new ToolContext - tc.nit_dir = tc.locate_nit_dir # still use the common lib to have core - tc.keep_going = true # no exit, obviously - if print_errors != true then tc.opt_warn.value = -1 # no output - - # Prepare an stand-alone model and model builder. - # Unfortunately, models are enclosing and append-only. - # There is no way (yet?) to have a shared module `core` with - # isolated and throwable user modules. - var model = new Model - var mb = new ModelBuilder(model, tc) - - # Parse the code - var source = new SourceFile.from_string("", content + "\n") - var lexer = new Lexer(source) - var parser = new Parser(lexer) - var tree = parser.parse - - var hlcode = new HLCode(hl, content, source) - - # Check syntax error - var eof = tree.n_eof - if eof isa AError then - mb.error(eof, eof.message) - hl.hightlight_source(source) - return hlcode - end - var amodule = tree.n_base.as(not null) - - # Load the AST as a module in the model - # Then process it - mb.load_rt_module(null, amodule, "") - mb.run_phases - - # Highlight the processed module - hl.enter_visit(amodule) - return hlcode -end - # A standalone highlighted piece of code class HLCode super Serializable @@ -432,6 +387,51 @@ class HighlightVisitor """ end + + # Fully process `content` as a Nit source file. + # + # Set `print_errors = true` to print errors in the code to the console. + fun highlightcode(content: String, print_errors: nullable Bool): HLCode + do + # Prepare a stand-alone tool context + var tc = new ToolContext + tc.nit_dir = tc.locate_nit_dir # still use the common lib to have core + tc.keep_going = true # no exit, obviously + if print_errors != true then tc.opt_warn.value = -1 # no output + + # Prepare an stand-alone model and model builder. + # Unfortunately, models are enclosing and append-only. + # There is no way (yet?) to have a shared module `core` with + # isolated and throwable user modules. + var model = new Model + var mb = new ModelBuilder(model, tc) + + # Parse the code + var source = new SourceFile.from_string("", content + "\n") + var lexer = new Lexer(source) + var parser = new Parser(lexer) + var tree = parser.parse + + var hlcode = new HLCode(self, content, source) + + # Check syntax error + var eof = tree.n_eof + if eof isa AError then + mb.error(eof, eof.message) + highlight_source(source) + return hlcode + end + var amodule = tree.n_base.as(not null) + + # Load the AST as a module in the model + # Then process it + mb.load_rt_module(null, amodule, "") + mb.run_phases + + # Highlight the processed module + highlight_node(amodule) + return hlcode + end end redef class HTMLTag diff --git a/src/web/api_light.nit b/src/web/api_light.nit index 784d683..835589e 100644 --- a/src/web/api_light.nit +++ b/src/web/api_light.nit @@ -31,7 +31,7 @@ class APILight redef fun post(req, res) do var hl = new HighlightVisitor - var hlcode = hightlightcode(hl, req.body) + var hlcode = hl.highlightcode(req.body) res.json(hlcode) end end -- 1.7.9.5