Bun venit pe SkullBox!

Bine ai venit, Vizitator. Trebuie să te autentifici sau să îţi creezi un cont.
Ai pierdut sau nu ai primit emailul care conţine codul de activare al contului?

Autentifică-te cu numele de utilizator, parola şi precizează durata sesiunii.
  Pagini: [1]
  Imprimă  
Syntax highlight for Pascal / Delphi [Delphi]  (Vizualizari 368)
DarkByte
*

Deconectat Deconectat

Mesaje: 2441

WWW
Syntax highlight for Pascal / Delphi [Delphi], Mai 11, 2008, 21:50

M-am apucat sa-mi fac un program care sa formateze un text in formatul Pascal / Delphi (syntax highlight) ... mai are nevoie de cateva aditii, dar in mare e ok.

Codul e mai jos.

Cod:
type
  TForm1 = class(TForm)
    ...
    procedure SyntaxHighlight(fis : String);
    ...
    private
      { Private declarations }
    public
      { Public declarations }
  end;
Cod:
procedure TForm1.SyntaxHighlight(fis : String);
var line, i, j, st, P, P1, P2, A1, A2 : integer;
    Source, Idents : TStringList;
    Delimiters : set of char;
    S : String;
    Com : Boolean;
begin
  Source := TStringList.Create;
  Source.LoadFromFile(fis);

  Idents := TStringList.Create;
  Idents.LoadFromFile('idents.txt');

  Delimiters := [#0 .. #255];
  Delimiters := Delimiters - ['0' .. '9', 'A'..'Z', 'a'..'z'];

  Com := False;

  With Source Do
    Begin
      for line := 0 to Count - 1 Do
        Begin

          RichEdit1.Lines.Add(Strings[line]);

          S := LowerCase(Strings[line]);
          St := RichEdit1.SelStart - Length(S) - 2;

          if S = ''
            then Continue;

          if Not Com
            then

          For i := 0 To Idents.Count - 1 do
            Begin
              P := Pos(Idents[i], S);
              If (P > 0) and ((S[P-1] in Delimiters) or (P = 1))
                         and ((S[P + Length(Idents[i])] in Delimiters) or (P + Length(Idents[i]) - 1 = Length(S))) Then
                Begin
                  RichEdit1.SelStart := St + P - 1;
                  RichEdit1.SelLength := Length(Idents[i]);
                  RichEdit1.SelAttributes.Style := [fsBold];
                End;
            End;

          if Not Com
            then
              begin
                P := Pos ('//', S);          {needs polishing}
                A1 := Pos ('{', S);
              end;

            Begin
              p2 := 1;

              if Not Com
                then

              repeat
                p1 := PosEx('''', S, p2);
                p2 := PosEx('''', S, p1 + 1);

                if (p1 > 0) and (p2 = 0)
                  then p2 := Length(S);

                if (p1 > 0) Then
                  Begin
                    RichEdit1.SelStart := St + p1 - 1;
                    If p2 > 0
                      then RichEdit1.SelLength := p2 - p1 + 1
                      else RichEdit1.SelLength := Length(S) - p1 + 1;
                    RichEdit1.SelAttributes.Color := $7F0000;
                    RichEdit1.SelAttributes.Style := [];
                  End
                  Else Break;
                 
                if (P >= p1) and (P <= p2) Then
                  Begin
                    P := 0;
                  End;

                if (A1 >= p1) and (A1 <= p2) Then
                  Begin
                    A1 := 0;
                  End;

                if (p1 = 0) or (p2 = 0)
                  then Break;

                p2 := p2 + 1;
              until False;

              If (P > 0) or (A1 > 0) or Com Then
                Begin
                  if P > 0
                    then
                      begin
                        RichEdit1.SelStart := St + P - 1;
                        RichEdit1.SelLength := Length(S) - P + 1;
                        RichEdit1.SelAttributes.Style := [fsItalic];
                        RichEdit1.SelAttributes.Color := $7F0000;
                      end;

                  if (A1 > 0) or Com
                    then
                      begin
                        Com := True;

                        if A1 = 0
                          then A1 := 1;

                        A2 := Pos ('}', S);

                        If A2 = 0
                          then A2 := Length(S)
                          else Com := False;

                        RichEdit1.SelStart := St + A1 - 1;
                        RichEdit1.SelLength := A2 - A1 + 1;
                        RichEdit1.SelAttributes.Style := [fsItalic];
                        RichEdit1.SelAttributes.Color := $7F0000;

                        A1 := 0;
                      end;
                End;
            End;

        End;
    End;

  RichEdit1.SelStart := 0;
  RichEdit1.SelLength := 0;
  RichEdit1.SetFocus;
end;
Pentru a functiona, in directorul programului e nevoie de fisierul idents.txt, al carui continut e mai jos:
Cod:
absolute
abstract
and
array
as
asm
assembler
at
automated
begin
case
cdecl
class
const
constructor
contains
default
destructor
dispid
dispinterface
div
do
downto
dynamic
else
end
except
export
exports
external
far
file
finalization
finally
for
forward
function
goto
if
implementation
implements
in
index
inherited
initialization
inline
interface
is
label
library
message
mod
name
near
nil
nodefault
not
object
of
on
or
out
overload
override
package
packed
pascal
private
procedure
program
property
protected
public
published
raise
read
readonly
record
register
reintroduce
repeat
requires
resident
resourcestring
safecall
set
shl
shr
stdcall
stored
string
then
threadvar
to
try
type
unit
until
uses
var
virtual
while
with
write
writeonly
xor
In program e nevoie de un TRichEdit denumit RichEdit1 (denumirea standard) pe forma Form1 si mai trebuie doar apelata aceasta procedura cu numele complet al fisierului care trebuie formatat. Aceste lucruri pot fi usor schimbate.

Bineinteles, fisierul idents.txt si codul pot fi modificate pentru a formata texte scrise in alte limbaje. Go for it Smile

Bafta

P.S. un screenshot al unor rezultate:

Memorat

Light travels faster than sound. This is why some people appear intelligent until they speak.

Quidquid latinum dictum sit, altum viditur.

AdyX
*

Deconectat Deconectat

Mesaje: 1062

WWW
Syntax highlight for Pascal / Delphi [Delphi], Mai 12, 2008, 08:56

Frumos, good job! Smile

Am incercat sa fac si eu asa ceva mai demult, dar m-am incurcat la SelStart, SelLenght etc Smile
Memorat
DarkByte
*

Deconectat Deconectat

Mesaje: 2441

WWW
Syntax highlight for Pascal / Delphi [Delphi], Mai 12, 2008, 09:58

Daca ai / ai avut nevoie de ajutor, de ce nu m-ai intrebat ? Cand am refuzat sa te ajut ?

Vorbim pe mess diseara, am sa-ti mai arat cate ceva Smile

Bafta
Memorat

Light travels faster than sound. This is why some people appear intelligent until they speak.

Quidquid latinum dictum sit, altum viditur.

AdyX
*

Deconectat Deconectat

Mesaje: 1062

WWW
Syntax highlight for Pascal / Delphi [Delphi], Mai 12, 2008, 10:12

Cred ca erai intr-o perioada in care nu aveai net, ca nu-mi aduc aminte sa te fi vazut pe mess Smile
Memorat
SkullAds
Ecspert
ReclAmator
* * * * *
Google AdSense

Gen: Bărbat
Mesaje: Multe

Reclama AdSense,
 

 
   


Pagini: [1]
  Imprimă  
 
Schimbă forumul:  

Ethical hacking and programming community
Powered by SMF 1.1.7 | SMF © 2006-2008, Simple Machines LLC
Traducerea în limba română © 2006-2007 www.smf.ro