Re: [code][textadept] Lexer/LPeg rules to handle first line and lines obtaining a specific text

From: Mitchell <m.att.foicica.com>
Date: Fri, 7 Jun 2019 08:02:56 -0400 (EDT)

Hi Lukas,

nOn Fri, 7 Jun 2019, Procházka Lukáš Ing. wrote:

> Hello,
>
> suppose having the following text (be it in a [Message Box] or similar
> buffer):
>
> ----
> This is LuaTeX 1.10 (build 1654).
> foo
> bar
> Warning: line 55: cannot locate file.
> baz
> Error: line 160: unrecognized sequence "\bobo" - exiting now.
> Bye.
> ----
>
> I'm trying to define lexer PEG/styling rules so that (see the pic attached):
>
> - First line be styled with "FIRST_LINE" style (here: to have yellow back)
> - Line(s) containing word "Warning" to be styled with "WARNING_STYLE" style
> (here: to have turquoise back),
> and the word "Warning" to be styled with "WARNING_WORD" style (here:
> to have red black);
>
> (Similarly for "Error" line.)
>
> - All backgrounds to be used on the whole line (i.e. to be not stopped by
> last char on the line).
>
> How to define such lexer rules? Or a lexer for inspiration?
>
> (I succeeded with some lexer which recognize tokens and its categories, but
> they don't affect styling of the WHOLE LINE where the token appears.)
>
> Any help would be appreciated.

You can take a look at the diff.lua lexer, which is a line lexer, for inspiration. You can look at the start of a line for 'Warning' and 'Error' tokens.

The FIRST_LINE bit will be tricky since lexers are not inherently stateful. I'd probably have a rule that looks for a '---' line, and then use an `lpeg.P()` function pattern to flag the next line as a "first" line using `lexer.line_from_position()` and `lexer.line_state[line] = 1`. Then I'd have another rule that starts with an `lpeg.P()` function pattern that checks `lexer.line_state[line] == 1`, and if it passes, match the whole line's text. You can read a bit more on lexers with complex state here:

   https://foicica.com/textadept/api.html#lexer.Lexers.with.Complex.State

In order to fill end-of-line backgrounds, use the 'eolfilled' style property:

   https://foicica.com/textadept/api.html#lexer.Styles.and.Styling

I hope this helps.

Cheers,
Mitchell

-- 
You are subscribed to code.att.foicica.com.
To change subscription settings, send an e-mail to code+help.att.foicica.com.
To unsubscribe, send an e-mail to code+unsubscribe.att.foicica.com.
Received on Fri 07 Jun 2019 - 08:02:56 EDT

This archive was generated by hypermail 2.2.0 : Sat 08 Jun 2019 - 06:27:54 EDT