From ef897b418a4487196e1dbc18a97046f8f0aea2e8 Mon Sep 17 00:00:00 2001 From: Robin Haberkorn Date: Tue, 24 Dec 2024 13:29:32 +0300 Subject: introduced true block and EOL comments * The previous convention of !* ... *! are now true block comments, i.e. they are parsed faster, don't spam the goto table and allow embedding of exclamation marks - only "*!" terminates the comment. * It is therefore now forbidden to have goto labels beginning with "*". * Also support "!!" to introduce EOL comments (like C++'s //). This disallows empty labels, but they weren't useful anyway. This is the shortest way to begin a comment. * All comment labels have been converted to true comments, to ensure that syntax highlighting works correctly. EOL comments are used for single line commented-out code, since it's easiest to uncomment - you don't have to jump to the line end. This is a pure convention / coding style. Other people might do it differently. * It's of course still possible to abuse goto labels as comments as TECO did for ages. * In lexing / syntax highlighting, labels and comments are highlighted differently. * When syntax highlighting, a single "!" will first be highlighted as a label since it's not yet unambiguous. Once you type the second character (* or !), the first character is retroactively styled as a comment as well. --- src/lexer.c | 51 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 31 insertions(+), 20 deletions(-) (limited to 'src/lexer.c') diff --git a/src/lexer.c b/src/lexer.c index ff43c1b..c0c7847 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -19,6 +19,8 @@ #include "config.h" #endif +#include + #include #include "sciteco.h" @@ -39,20 +41,12 @@ teco_lexer_getstyle(teco_view_t *view, teco_machine_main_t *machine, * FIXME: What about ^* and ^/? * They are currently highlighted as commands. */ - if (machine->parent.current->keymacro_mask & TECO_KEYMACRO_MASK_START) { - switch (chr) { - case '0'...'9': + if (machine->parent.current->keymacro_mask & TECO_KEYMACRO_MASK_START && + chr <= 0xFF) { + if (g_ascii_isdigit(chr)) style = SCE_SCITECO_NUMBER; - break; - case '+': - case '-': - case '*': - case '/': - case '#': - case '&': + else if (strchr("+-*/#&", chr)) style = SCE_SCITECO_OPERATOR; - break; - } } /* @@ -64,10 +58,7 @@ teco_lexer_getstyle(teco_view_t *view, teco_machine_main_t *machine, /* * Probably a syntax error, so the erroneous symbol * is highlighted and we reset the parser's state machine. - */ - style = SCE_SCITECO_INVALID; - - /* + * * FIXME: Perhaps we should simply reset the state to teco_state_start? */ gsize macro_pc = machine->macro_pc; @@ -75,9 +66,22 @@ teco_lexer_getstyle(teco_view_t *view, teco_machine_main_t *machine, teco_machine_main_init(machine, NULL, FALSE); machine->mode = TECO_MODE_LEXING; machine->macro_pc = macro_pc; - } else if (machine->parent.current->style == SCE_SCITECO_LABEL) { - /* don't highlight the leading `!` as SCE_SCITECO_COMMAND */ - style = SCE_SCITECO_LABEL; + + return SCE_SCITECO_INVALID; + } + + /* + * Don't highlight the leading `!` in comments as SCE_SCITECO_COMMAND. + * True comments also begin with `!`, so make sure they are highlighted + * already from the second character. + * This is then extended back by one character in teco_lexer_step(). + */ + switch (machine->parent.current->style) { + case SCE_SCITECO_COMMENT: + case SCE_SCITECO_LABEL: + return machine->parent.current->style; + default: + break; } return style; @@ -100,7 +104,7 @@ teco_lexer_step(teco_view_t *view, teco_machine_main_t *machine, return; } - gsize old_pc = machine->macro_pc; + gssize old_pc = machine->macro_pc; teco_style_t style = SCE_SCITECO_DEFAULT; @@ -140,6 +144,13 @@ teco_lexer_step(teco_view_t *view, teco_machine_main_t *machine, *cur_col += machine->macro_pc - old_pc; + /* + * True comments begin with `!*` or `!!`, but only the second character gets + * the correct style by default, so we extend it backwards. + */ + if (style == SCE_SCITECO_COMMENT) + old_pc--; + teco_view_ssm(view, SCI_STARTSTYLING, start+old_pc, 0); teco_view_ssm(view, SCI_SETSTYLING, machine->macro_pc-old_pc, style); -- cgit v1.2.3