YOUSEFUL / DELPHI / COMPONENTS

TAtlasToken

One token found by the Object Pascal lexer: where it starts, how long it is and what kind it is.

Record Unit Atlas.Syntax · Product Atlas UI

Description

Returned in arrays by AtlasPascalTokens(const Source: string): TArray<TAtlasToken> and by TAtlasSyntaxMemo.Tokens. The tokens cover the text completely and in order: each starts where the previous one ended, and the counts add up to the length of the text. Start is 0-based, so the token's text is Copy(Source, Token.Start + 1, Token.Count). For TAtlasSyntaxMemo.Tokens the positions are those of the editor, where a line break is one character, and can be used as SelStart and SelLength.

Members

MemberKindWhat it does
StartfieldThe 0-based position of the token's first character.
CountfieldThe number of characters in the token; at least 1.
KindfieldWhat the token is; see TAtlasTokenKind.

Fields

Start field

Start, Count: Integer;

The 0-based position of the token's first character.

Count field

Start, Count: Integer;

The number of characters in the token; at least 1.

Kind field

Kind: TAtlasTokenKind;

What the token is; see TAtlasTokenKind.

Examples

Colour Pascal source as HTML

Uses the lexer and the editor's colours outside the editor. PascalToHtml('begin X := 42; end;') returns <pre><span style="color:#2440A6">begin</span> X := <span style="color:#785498">42</span>; <span style="color:#2440A6">end</span>;</pre>.

pascal
uses System.SysUtils, System.NetEncoding, Vcl.Graphics, Atlas.Syntax;

// Object Pascal source as an HTML fragment, in the editor's colours.
function PascalToHtml(const Source: string): string;
var
  Token: TAtlasToken;
  Text: string;
  Color: TColor;
begin
  Result := '<pre>';
  for Token in AtlasPascalTokens(Source) do
  begin
    Text := TNetEncoding.HTML.Encode(Copy(Source, Token.Start + 1, Token.Count));
    if Token.Kind = atkPlain then
      Result := Result + Text
    else
    begin
      Color := ColorToRGB(AtlasTokenColor(Token.Kind));
      Result := Result + Format('<span style="color:#%.2x%.2x%.2x">%s</span>',
        [Color and $FF, (Color shr 8) and $FF, (Color shr 16) and $FF, Text]);
    end;
  end;
  Result := Result + '</pre>';
end;

TAtlasTokenKind · TAtlasSyntaxMemo

Also in Atlas UI

TAtlasKanbanModel · TAtlasKanban · TAtlasSyntaxMemo · TAtlasWebUpdater