-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSynFacilBasic.pas
1665 lines (1626 loc) · 67.3 KB
/
SynFacilBasic.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{ SynFacilBasic
Unidad con rutinas básicas de SynFacilSyn.
Incluye la definición de la clase base: TSynFacilSynBase, que es la clase padre
de TSYnFacilSyn.
Además icnluye la definición del tipo "tFaTokContent" y el procesamiento de
expresiones regulares que son usadas por TSynFacilSyn.
Por Tito Hinostroza 02/12/2014 - Lima Perú
}
unit SynFacilBasic;
{$mode objfpc}{$H+}
interface
uses
SysUtils, Classes, SynEditHighlighter, strutils, Graphics, DOM, LCLIntf,
LCLProc, SynEditHighlighterFoldBase, SynEditTypes;
type
///////// Definiciones para manejo de tokens por contenido ///////////
//Tipo de expresión regular soportada. Las exp. regulares soportadas son
//simples. Solo incluyen literales de cadena o listas.
tFaRegExpType = (
tregTokPos, //Posición de token
tregString, //Literal de cadena: "casa"
tregChars, //Lista de caracteres: [A-Z]
tregChars01, //Lista de caracteres: [A-Z]?
tregChars0_, //Lista de caracteres: [A-Z]*
tregChars1_ //Lista de caracteres: [A-Z]+
);
//Acciones a ejecutar en las comparaciones
tFaActionOnMatch = (
aomNext, //pasa a la siguiente instrucción
aomExit, //termina la exploración
aomMovePar, //Se mueve a una posición específica
aomExitpar //termina la exploración retomando una posición específica.
);
//Estructura para almacenar una instrucción de token por contenido
tFaTokContentInst = record
Chars : array[#0..#255] of ByteBool; //caracteres
Text : string; //cadena válida
tokPos : integer; //Cuando se usa posición del token
expTyp : tFaRegExpType; //tipo de expresión
aMatch : integer; //atributo asignado en caso TRUE
aFail : integer; //atributo asignado en caso TRUE
//Campos para ejecutar instrucciones, cuando No cumple
actionFail : tFaActionOnMatch;
destOnFail : integer; //posición destino
//Campos para ejecutar instrucciones, cuando cumple
actionMatch: tFaActionOnMatch;
destOnMatch: integer; //posición destino
posFin : integer; //para guardar posición
end;
tFaTokContentInstPtr = ^tFaTokContentInst;
ESynFacilSyn = class(Exception); //excepción del resaltador
{ tFaTokContent }
//Estructura para almacenar la descripción de los token por contenido
tFaTokContent = class
TokTyp : integer; //tipo de token por contenido
CaseSensitive: boolean; //Usado para comparación de literales de cadena
Instrucs : array of tFaTokContentInst; //Instrucciones del token por contenido
nInstruc : integer; //Cantidad de instrucciones
procedure Clear;
procedure AddInstruct(exp: string; ifTrue: string = ''; ifFalse: string = '';
atMatch: integer = - 1; atFail: integer = - 1);
procedure AddRegEx(exp: string; Complete: boolean=false);
private
function AddItem(expTyp: tFaRegExpType; ifMatch, ifFail: string): integer;
procedure AddOneInstruct(var exp: string; ifTrue: string; ifFalse: string;
atMatch: integer = -1; atFail: integer = -1);
end;
///////// Definiciones básicas para el resaltador ///////////
//Identifica si un token es el delimitador inicial
TFaTypeDelim =(tdNull, //no es delimitado
tdUniLin, //es delimitador inicial de token delimitado de una línea
tdMulLin, //es delimitador inicial de token delimitado multilínea
tdConten1, //es delimitador inicial de token por contenido 1
tdConten2, //es delimitador inicial de token por contenido 2
tdConten3, //es delimitador inicial de token por contenido 3
tdConten4); //es delimitador inicial de token por contenido 4
//Tipos de coloreado de bloques
TFaColBlock = (cbNull, //sin coloreado
cbLevel, //colorea bloques por nivel
cbBlock); //colorea bloques usando el color definido para cada bloque
TFaProcMetTable = procedure of object; //Tipo de procedimiento para procesar el token de
//acuerdo al caracter inicial.
TFaProcRange = procedure of object; //Procedimiento para procesar en medio de un rango.
TFaSynBlock = class; //definición adelantada
//Descripción de tokens especiales (identificador o símbolo)
TTokSpec = record
txt : string; //palabra clave (puede cambiar la caja y no incluir el primer caracter)
orig : string; //palabra clave tal cual se indica
TokPos: integer; //posición del token dentro de la línea
tTok : integer; //tipo de token
typDel: TFaTypeDelim; {indica si el token especial actual, es en realidad, el
delimitador inicial de un token delimitado o por contenido}
dEnd : string; //delimitador final (en caso de que sea delimitador)
pRange: TFaProcRange; //procedimiento para procesar el token o rango(si es multilinea)
folTok: boolean; //indica si el token delimitado, tiene plegado
chrEsc: char; //Caracter de escape de token delimitado. Si no se usa, contiene #0.
//propiedades para manejo de bloques y plegado de código
openBlk : boolean; //indica si el token es inicio de bloque de plegado
BlksToOpen: array of TFaSynBlock; //lista de referencias a los bloques que abre
closeBlk : boolean; //indica si el token es fin de bloque de plegado
BlksToClose: array of TFaSynBlock; //lista de referencias a los bloques que cierra
OpenSec : boolean; //indica si el token es inicio de sección de bloque
SecsToOpen: array of TFaSynBlock; //lista de bloques de los que es inicio de sección
firstSec : TFaSynBlock; //sección que se debe abrir al abrir el bloque
end;
TEvBlockOnOpen = procedure(blk: TFaSynBlock; var Cancel: boolean) of object;
TArrayTokSpec = array of TTokSpec;
//clase para manejar la definición de bloques de sintaxis
TFaSynBlock = class
name : string; //nombre del bloque
index : integer; //indica su posición dentro de TFaListBlocks
showFold : boolean; //indica si se mostrará la marca de plegado
parentBlk : TFaSynBlock; //bloque padre (donde es válido el bloque)
BackCol : TColor; //color de fondo de un bloque
IsSection : boolean; //indica si es un bloque de tipo sección
UniqSec : boolean; //índica que es sección única
CloseParent : boolean; //indica que debe cerrar al blqoue padre al cerrarse
OnBeforeOpen : TEvBlockOnOpen; //evento de apertura de bloque
OnBeforeClose : TEvBlockOnOpen; //evento de cierre de bloque
end;
TPtrATokEspec = ^TArrayTokSpec; //puntero a tabla
TPtrTokEspec = ^TTokSpec; //puntero a tabla
//Guarda información sobre un atributo de un nodo XML
TFaXMLatrib = record //atributo XML
hay: boolean; //bandera de existencia
val: string; //valor en cadena
n : integer; //valor numérico
bol: boolean; //valor booleando (si aplica)
col: TColor; //valor de color (si aplica)
end;
{ TSynFacilSynBase }
//Clase con métodos básicos para el resaltador
TSynFacilSynBase = class(TSynCustomFoldHighlighter)
protected
fLine : PChar; //Puntero a línea de trabajo
tamLin : integer; //Tamaño de línea actual
fProcTable : array[#0..#255] of TFaProcMetTable; //tabla de métodos
fAtriTable : array[#0..#255] of integer; //tabla de atributos de tokens
posIni : Integer; //índice a inicio de token
posFin : Integer; //índice a siguiente token
fStringLen : Integer; //Tamaño del token actual
fToIdent : PChar; //Puntero a identificador
fTokenID : integer; //Id del token actual
charIni : char; //caracter al que apunta fLine[posFin]
posTok : integer; //para identificar el ordinal del token en una línea
CaseSensitive: boolean; //Para ignorar mayúscula/minúscula
charsIniIden: Set of char; //caracteres iniciales de identificador
lisTmp : TStringList; //lista temporal
fSampleSource: string; //código de muestra
function GetSampleSource: String; override;
protected //identificadores especiales
CharsIdentif: array[#0..#255] of ByteBool; //caracteres válidos para identificadores
tc1, tc2, tc3, tc4: tFaTokContent;
//Tablas para identificadores especiales
mA, mB, mC, mD, mE, mF, mG, mH, mI, mJ,
mK, mL, mM, mN, mO, mP, mQ, mR, mS, mT,
mU, mV, mW, mX, mY, mZ: TArrayTokSpec; //para mayúsculas
mA_,mB_,mC_,mD_,mE_,mF_,mG_,mH_,mI_,mJ_,
mK_,mL_,mM_,mN_,mO_,mP_,mQ_,mR_,mS_,mT_,
mU_,mV_,mW_,mX_,mY_,mZ_: TArrayTokSpec; //para minúsculas
m_, mDol, mArr, mPer, mAmp, mC3 : TArrayTokSpec;
mSym : TArrayTokSpec; //tabla de símbolos especiales
mSym0 : TArrayTokSpec; //tabla temporal para símbolos especiales.
TabMayusc : array[#0..#255] of Char; //Tabla para conversiones rápidas a mayúscula
protected //funciones básicas
function BuscTokEspec(var mat: TArrayTokSpec; cad: string; out n: integer;
TokPos: integer = 0): boolean;
function ToListRegex(list: TFaXMLatrib): string;
function dStartRegex(tStart, tCharsStart: TFaXMLatrib): string;
procedure VerifDelim(delim: string);
procedure ValidAsigDelim(delAct, delNue: TFaTypeDelim; delim: string);
procedure ValidateParamStart(Start: string; var ListElem: TStringList);
function KeyComp(var r: TTokSpec): Boolean;
function CreaBuscTokEspec(var mat: TArrayTokSpec; cad: string; out i: integer;
TokPos: integer = 0): boolean;
//procesamiento de XML
procedure CheckXMLParams(n: TDOMNode; listAtrib: string);
function ReadXMLParam(n: TDOMNode; nomb: string): TFaXMLatrib;
protected //Métodos para tokens por contenido
procedure metTokCont(const tc: tFaTokContent); //inline;
procedure metTokCont1;
procedure metTokCont2;
procedure metTokCont3;
procedure metTokCont4;
protected //Procesamiento de otros elementos
procedure metIdent;
procedure metIdentUTF8;
procedure metNull;
procedure metSpace;
procedure metSymbol;
public //Funciones públicas
procedure DefTokIdentif(dStart, Content: string );
public //Atributos y sus propiedades de acceso
//Atributos predefinidos
tkEol : TSynHighlighterAttributes;
tkSymbol : TSynHighlighterAttributes;
tkSpace : TSynHighlighterAttributes;
tkIdentif : TSynHighlighterAttributes;
tkNumber : TSynHighlighterAttributes;
tkKeyword : TSynHighlighterAttributes;
tkString : TSynHighlighterAttributes;
tkComment : TSynHighlighterAttributes;
//ID para los tokens
tnEol : integer; //id para los tokens salto de línea
tnSymbol : integer; //id para los símbolos
tnSpace : integer; //id para los espacios
tnIdentif : integer; //id para los identificadores
tnNumber : integer; //id para los números
tnKeyword : integer; //id para las palabras claves
tnString : integer; //id para las cadenas
tnComment : integer; //id para los comentarios
{Se crea el contenedor adicional Attrib[], para los atributos, porque aunque ya se
tiene Attribute[] en TSynCustomHighlighter, este está ordenado pro defecto y no
ayuda en ubicar a los attributos por su índice}
Attrib: array of TSynHighlighterAttributes;
function NewTokAttrib(TypeName: string; out TokID: integer
): TSynHighlighterAttributes;
function NewTokType(TypeName: string; out TokAttrib: TSynHighlighterAttributes
): integer;
function NewTokType(TypeName: string): integer;
procedure CreateAttributes; //limpia todos loa atributos
function GetAttribByName(txt: string): TSynHighlighterAttributes;
function GetAttribIDByName(txt: string): integer;
function IsAttributeName(txt: string): boolean;
protected
function ProcXMLattribute(nodo: TDOMNode): boolean;
public //Inicializacoón
constructor Create(AOwner: TComponent); override;
end;
function ExtractRegExp(var exp: string; out str: string; out listChars: string): tFaRegExpType;
function ExtractRegExpN(var exp: string; out RegexTyp: tFaRegExpType ): string;
function ReplaceEscape(str: string): string;
function ColorFromStr(cad: string): TColor;
implementation
const
//Mensajes de error generales
// ERR_START_NO_EMPTY = 'Parámetro "Start" No puede ser nulo';
// ERR_EXP_MUST_BE_BR = 'Expresión debe ser de tipo [lista de caracteres]';
// ERR_TOK_DELIM_NULL = 'Delimitador de token no puede ser nulo';
// ERR_NOT_USE_START = 'No se puede usar "Start" y "CharsStart" simultáneamente.';
// ERR_PAR_START_CHARS = 'Se debe definir el parámetro "Start" o "CharsStart".';
// ERR_TOK_DEL_IDE_ERR = 'Delimitador de token erróneo: %s (debe ser identificador)';
// ERR_IDEN_ALREA_DEL = 'Identificador "%s" ya es delimitador inicial.';
// ERR_INVAL_ATTR_LAB = 'Atributo "%s" no válido para etiqueta <%s>';
// ERR_BAD_PAR_STR_IDEN = 'Parámetro "Start" debe ser de la forma: "[A-Z]", en identificadores';
// ERR_BAD_PAR_CON_IDEN = 'Parámetro "Content" debe ser de la forma: "[A-Z]*", en identificadores';
ERR_START_NO_EMPTY = 'Parameter "Start" can not be null';
ERR_EXP_MUST_BE_BR = 'Expression must be like: [list of chars]';
ERR_TOK_DELIM_NULL = 'Token delimiter can not be null';
ERR_NOT_USE_START = 'Cannot use "Start" and "CharsStart" simultaneously.';
ERR_PAR_START_CHARS = 'It must be defined "Start" or "CharsStart" parameter.';
ERR_TOK_DEL_IDE_ERR = 'Bad Token delimiter: %s (must be identifier)';
ERR_IDEN_ALREA_DEL = 'Identifier "%s" is already a Start delimiter.';
ERR_INVAL_ATTR_LAB = 'Invalid attribute "%s" for label <%s>';
ERR_BAD_PAR_STR_IDEN = 'Parameter "Start" must be like: "[A-Z]", in identifiers';
ERR_BAD_PAR_CON_IDEN = 'Parameter "Content" must be like: "[A-Z]*", in identifiers';
//Mensajes de tokens por contenido
// ERR_EMPTY_INTERVAL = 'Error: Intervalo vacío.';
// ERR_EMPTY_EXPRES = 'Expresión vacía.';
// ERR_EXPECTED_BRACK = 'Se esperaba "]".';
// ERR_UNSUPPOR_EXP_ = 'Expresión no soportada.';
// ERR_INC_ESCAPE_SEQ = 'Secuencia de escape incompleta.';
// ERR_SYN_PAR_IFFAIL_ = 'Error de sintaxis en parámetro "IfFail": ';
// ERR_SYN_PAR_IFMATCH_ = 'Error de sintaxis en parámetro "IfMarch": ';
ERR_EMPTY_INTERVAL = 'Error: Empty Interval.';
ERR_EMPTY_EXPRES = 'Empty expression.';
ERR_EXPECTED_BRACK = 'Expected "]".';
ERR_UNSUPPOR_EXP_ = 'Unsupported expression: ';
ERR_INC_ESCAPE_SEQ = 'Incomplete Escape sequence';
ERR_SYN_PAR_IFFAIL_ = 'Syntax error on Parameter "IfFail": ';
ERR_SYN_PAR_IFMATCH_ = 'Syntax error on Parameter "IfMarch": ';
var
bajos: string[128];
altos: string[128];
function copyEx(txt: string; p: integer): string;
//Versión sobrecargada de copy con 2 parámetros
begin
Result := copy(txt, p, length(txt));
end;
//Funciones para el manejo de expresiones regulares
function ExtractChar(var txt: string; out escaped: boolean; convert: boolean): string;
//Extrae un caracter de una expresión regular. Si el caracter es escapado, devuelve
//TRUE en "escaped"
//Si covert = TRUE, reemplaza el caracter compuesto por uno solo.
var
c: byte;
begin
escaped := false;
Result := ''; //valor por defecto
if txt = '' then exit;
if txt[1] = '\' then begin //caracter escapado
escaped := true;
if length(txt) = 1 then //verificación
raise ESynFacilSyn.Create(ERR_INC_ESCAPE_SEQ);
if txt[2] in ['x','X'] then begin
//caracter en hexadecimal
if length(txt) < 4 then //verificación
raise ESynFacilSyn.Create(ERR_INC_ESCAPE_SEQ);
if convert then begin //toma caracter hexdecimal
c := StrToInt('$'+copy(txt,3,2));
Result := Chr(c);
end else begin //no tranforma
Result := copy(txt, 1,4);
end;
txt := copyEx(txt,5);
end else begin //se supone que es de tipo \A
//secuencia normal de dos caracteres
if convert then begin //hay que convertirlo
Result := txt[2];
end else begin //lo toma tal cual
Result := copy(txt,1,2);
end;
txt := copyEx(txt,3);
end;
end else begin //caracter normal
Result := txt[1];
txt := copyEx(txt,2);
end;
end;
function ExtractChar(var txt: string): char;
//Versión simplificada de ExtractChar(). Extrae un caracter ya convertido. Si no hay
//más caracteres, devuelve #0
var
escaped: boolean;
tmp: String;
begin
if txt = '' then Result := #0
else begin
tmp := ExtractChar(txt, escaped, true);
Result := tmp[1]; //se supone que siempre será de un solo caracter
end;
end;
function ExtractCharN(var txt: string): string;
//Versión simplificada de ExtractChar(). Extrae un caracter sin convertir.
var
escaped: boolean;
begin
Result := ExtractChar(txt, escaped, false);
end;
function ReplaceEscape(str: string): string;
{Reemplaza las secuencias de escape por su caracter real. Las secuencias de
escape recnocidas son:
* Secuencia de 2 caracteres: "\#", donde # es un caracter cualquiera, excepto"x".
Esta secuencia equivale al caracter "#".
* Secuencia de 4 caracteres: "\xHH" o "\XHH", donde "HH" es un número hexadecimnal.
Esta secuencia representa a un caracter ASCII.
Dentro de las expresiones regulares de esta librería, los caracteres: "[", "*", "?",
"*", y "\", tienen significado especial, por eso deben "escaparse".
"\\" -> "\"
"\[" -> "["
"\*" -> "*"
"\?" -> "?"
"\+" -> "+"
"\x$$" -> caracter ASCII $$
}
begin
Result := '';
while str<>'' do
Result += ExtractChar(str);
end;
function EscapeText(str: string): string;
//Comvierte los caracteres que pueden tener significado especial en secuencias de
//escape para que se procesen como caracteres normales.
begin
str := StringReplace(str, '\', '\\',[rfReplaceAll]); //debe hacerse primero
str := StringReplace(str, '[', '\[',[rfReplaceAll]);
str := StringReplace(str, '*', '\*',[rfReplaceAll]);
str := StringReplace(str, '?', '\?',[rfReplaceAll]);
str := StringReplace(str, '+', '\+',[rfReplaceAll]);
Result := str;
end;
function PosChar(ch: char; txt: string): integer;
//Similar a Pos(). Devuelve la posición de un caracter que no este "escapado"
var
f: SizeInt;
begin
f := Pos(ch,txt);
if f=1 then exit(1); //no hay ningún caracter antes.
while (f>0) and (txt[f-1]='\') do begin
f := PosEx(ch, txt, f+1);
end;
Result := f;
end;
function ExtractRegExp(var exp: string; out str: string; out listChars: string): tFaRegExpType;
{Extrae parte de una expresión regular y devuelve el tipo. Esta función se basa en
que toda expresión regular se puede reducir a literales de cadenas o listas (con o
sin cuantificador).
En los casos de listas de caracteres, expande los intervalos de tipo: A..Z, reemplaza
las secuencias de escape y devuelve la lista en "listChars".
En el caso de que sea un literal de cadena, reemplaza las secuencias de escape y
devuelve la cadena en "str".
Soporta todas las formas definidas en "tFaRegExpType".
Si encuentra error, genera una excepción.}
procedure ValidateInterval(var cars: string);
{Valida un conjunto de caracteres, expandiendo los intervalos de tipo "A-Z", y
remplazando las secuencias de escape como: "\[", "\\", "\-", ...
El caracter "-", se considera como indicador de intervalo, a menos que se encuentre
en el primer o ùltimo caracter de la cadena, o esté escapado.
Si hay error genera una excepción.}
var
c, car1, car2: char;
car: string;
tmp: String;
Invert: Boolean;
carsSet: set of char;
begin
//reemplaza intervalos
if cars = '' then
raise ESynFacilSyn.Create(ERR_EMPTY_INTERVAL);
//Verifica si es lista invertida
Invert := false;
if cars[1] = '^' then begin
Invert := true; //marca
cars := copyEx(cars,2); //quita "^"
end;
//Procesa contenido, reemplazando los caracteres escapados.
//Si el primer caracter es "-". lo toma literal, sin asumir error.
car1 := ExtractChar(cars); //Extrae caracter convertido. Se asume que es inicio de intervalo.
tmp := car1; //inicia cadena para acumular.
car := ExtractCharN(cars); //Eextrae siguiente. Sin convertir porque puede ser "\-"
while car<>'' do begin
if car = '-' then begin
//es intervalo
car2 := ExtractChar(cars); //caracter final
if car2 = #0 then begin
//Es intervalo incompleto, podría genera error, pero mejor asumimos que es el caracter "-"
tmp += '-';
break; //sale por que se supone que ya no hay más caracteres
end;
//se tiene un intervalo que hay que reemplazar
for c := Chr(Ord(car1)+1) to car2 do //No se incluye "car1", porque ya se agregó
tmp += c;
end else begin //simplemente acumula
car1 := ExtractChar(car); //Se asume que es inicio de intervalo. No importa perder "car"
tmp += car1; //Es necesario, porque puede estar escapado
end;
car := ExtractCharN(cars); //extrae siguiente
end;
cars := StringReplace(tmp, '%HIGH%', altos,[rfReplaceAll]);
cars := StringReplace(cars, '%ALL%', bajos+altos,[rfReplaceAll]);
//Verifica si debe invertir lista
if Invert then begin
//Convierte a conjunto
carsSet := [];
for c in cars do carsSet += [c];
//Agrega caracteres
cars := '';
for c := #1 to #255 do //no considera #0
if not (c in carsSet) then cars += c;
end;
end;
var
tmp: string;
lastAd: String;
begin
if exp= '' then
raise ESynFacilSyn.Create(ERR_EMPTY_EXPRES);
//Verifica la forma TokPos=1
if UpCase(copy(exp,1,7)) = 'TOKPOS=' then begin
//Caso especial de la forma TokPos=n
str := copy(exp,8,2); //Aquí se devuelve "n"
exp := ''; //ya no quedan caracteres
Result := tregTokPos;
exit;
end;
//Reemplaza secuencias conocidas que equivalen a listas.
if copy(exp,1,2) = '\d' then begin
exp := '[0-9]' + copyEx(exp,3);
end else if copy(exp,1,2) = '\D' then begin
exp := '[^0-9]' + copyEx(exp,3);
end else if copy(exp,1,2) = '\a' then begin
exp := '[A-Za-z]' + copyEx(exp,3);
end else if copy(exp,1,2) = '\w' then begin
exp := '[A-Za-z0-9_]' + copyEx(exp,3);
end else if copy(exp,1,2) = '\W' then begin
exp := '[^A-Za-z0-9_]' + copyEx(exp,3);
end else if copy(exp,1,2) = '\s' then begin
exp := ' ' + copyEx(exp,3);
end else if copy(exp,1,2) = '\S' then begin
exp := '[^ ]' + copyEx(exp,3);
end else if copy(exp,1,2) = '\t' then begin
exp := '\x09' + copyEx(exp,3);
end else if copy(exp,1,1) = '.' then begin
exp := '[\x01-\xFF]' + copyEx(exp,2);
end;
//analiza la secuencia
if (exp[1] = '[') and (length(exp)>1) then begin //Es lista de caracteres
//Captura interior del intervalo.
exp := CopyEx(exp,2);
listChars := '';
tmp := ExtractCharN(exp); //No convierte para no confundir "\]"
while (exp<>'') and (tmp<>']') do begin
listChars += tmp;
tmp := ExtractCharN(exp); //No convierte para no confundir "\]"
end;
if (tmp<>']') then //no se encontró ']'
raise ESynFacilSyn.Create(ERR_EXPECTED_BRACK);
//la norma es tener aquí, el contenido de la lista, pero manteniendo los caracteres escapados
ValidateInterval(listChars); //puede simplificar "listChars". También puede generar excepción
if exp = '' then begin //Lista de tipo "[ ... ]"
Result := tregChars;
end else if exp[1] = '*' then begin //Lista de tipo "[ ... ]* ... "
exp := copyEx(exp,2); //extrae parte procesada
Result := tregChars0_
end else if exp[1] = '?' then begin //Lista de tipo "[ ... ]? ... "
exp := copyEx(exp,2); //extrae parte procesada
Result := tregChars01
end else if exp[1] = '+' then begin //Lista de tipo "[ ... ]+ ... "
exp := copyEx(exp,2); //extrae parte procesada
Result := tregChars1_
end else begin
//No sigue ningún cuantificador, podrías er algún literal
Result := tregChars; //Lista de tipo "[ ... ] ... "
end;
end else if (length(exp)=1) and (exp[1] in ['*','?','+','[']) then begin
//Caso especial, no se usa escape, pero no es lista, ni cuantificador. Se asume
//caracter único
listChars := exp; //'['+exp+']'
exp := ''; //ya no quedan caracteres
Result := tregChars;
exit;
end else begin
//No inicia con lista. Se puede suponer que inicia con literal cadena.
{Pueden ser los casos:
Caso 0) "abc" (solo literal cadena, se extraerá la cadena "abc")
Caso 1) "abc[ ... " (válido, se extraerá la cadena "abc")
Caso 2) "a\[bc[ ... " (válido, se extraerá la cadena "a[bc")
Caso 3) "abc* ... " (válido, pero se debe procesar primero "ab")
Caso 4) "ab\\+ ... " (válido, pero se debe procesar primero "ab")
Caso 5) "a? ... " (válido, pero debe transformarse en lista)
Caso 6) "\[* ... " (válido, pero debe transformarse en lista)
}
str := ''; //para acumular
tmp := ExtractCharN(exp);
lastAd := ''; //solo por seguridad
while tmp<>'' do begin
if tmp = '[' then begin
//Empieza una lista. Caso 1 o 2
exp:= '[' + exp; //devuelve el caracter
str := ReplaceEscape(str);
{ if length(str) = 1 then begin //verifica si tiene un caracter
listChars := str; //'['+str+']'
Result := tregChars; //devuelve como lista de un caracter
exit;
end;}
Result := tregString; //es literal cadena
exit; //sale con lo acumulado en "str"
end else if (tmp = '*') or (tmp = '?') or (tmp = '+') then begin
str := copy(str, 1, length(str)-length(lastAd)); //no considera el último caracter
if str <> '' then begin
//Hay literal cadena, antes de caracter y cuantificador. Caso 3 o 4
exp:= lastAd + tmp + exp; //devuelve el último caracter agregado y el cuantificador
str := ReplaceEscape(str);
if length(str) = 1 then begin //verifica si tiene un caracter
listChars := str; //'['+str+']'
Result := tregChars; //devuelve como lista de un caracter
exit;
end;
Result := tregString; //es literal cadena
exit;
end else begin
//Hay caracter y cuantificador. . Caso 5 o 6
listChars := ReplaceEscape(lastAd); //'['+lastAd+']'
//de "exp" ya se quitó: <caracter><cuantificador>
if tmp = '*' then begin //Lista de tipo "[a]* ... "
Result := tregChars0_
end else if tmp = '?' then begin //Lista de tipo "[a]? ... "
Result := tregChars01
end else if tmp = '+' then begin //Lista de tipo "[a]+ ... "
Result := tregChars1_
end; //no hay otra opción
exit;
end;
end;
str += tmp; //agrega caracter
lastAd := tmp; //guarda el último caracter agregado
tmp := ExtractCharN(exp); //siguiente caracter
end;
//Si llega aquí es porque no encontró cuantificador ni lista (Caso 0)
str := ReplaceEscape(str);
{ if length(str) = 1 then begin //verifica si tiene un caracter
listChars := str; //'['+str+']'
Result := tregChars; //devuelve como lista de un caracter
exit;
end;}
Result := tregString;
end;
end;
function ExtractRegExpN(var exp: string; out RegexTyp: tFaRegExpType): string;
{Extrae parte de una expresión regular y la devuelve como cadena . Actualiza el
tipo de expresión obtenida en "RegexTyp".
No Reemplaza las secuencias de excape ni los intervalos, devuelve el texto tal cual}
var
listChars, str: string;
exp0: String;
tam: Integer;
begin
exp0 := exp; //guarda expresión tal cual
RegexTyp := ExtractRegExp(exp, str, listChars);
tam := length(exp0) - length(exp); //ve diferencia de tamaño
Result := copy(exp0, 1, tam)
end;
function ColorFromStr(cad: string): TColor;
//Convierte una cadena a Color
function EsHexa(txt: string; out num: integer): boolean;
//Convierte un texto en un número entero. Si es numérico devuelve TRUE
var i: integer;
begin
Result := true; //valor por defecto
num := 0; //valor por defecto
for i:=1 to length(txt) do begin
if not (txt[i] in ['0'..'9','a'..'f','A'..'F']) then exit(false); //no era
end;
//todos los dígitos son numéricos
num := StrToInt('$'+txt);
end;
var
r, g, b: integer;
begin
if (cad<>'') and (cad[1] = '#') and (length(cad)=7) then begin
//es código de color. Lo lee de la mejor forma
EsHexa(copy(cad,2,2),r);
EsHexa(copy(cad,4,2),g);
EsHexa(copy(cad,6,2),b);
Result:=RGB(r,g,b);
end else begin //constantes de color
case UpCase(cad) of
'WHITE' : Result :=rgb($FF,$FF,$FF);
'SILVER' : Result :=rgb($C0,$C0,$C0);
'GRAY' : Result :=rgb($80,$80,$80);
'BLACK' : Result :=rgb($00,$00,$00);
'RED' : Result :=rgb($FF,$00,$00);
'MAROON' : Result :=rgb($80,$00,$00);
'YELLOW' : Result :=rgb($FF,$FF,$00);
'OLIVE' : Result :=rgb($80,$80,$00);
'LIME' : Result :=rgb($00,$FF,$00);
'GREEN' : Result :=rgb($00,$80,$00);
'AQUA' : Result :=rgb($00,$FF,$FF);
'TEAL' : Result :=rgb($00,$80,$80);
'BLUE' : Result :=rgb($00,$00,$FF);
'NAVY' : Result :=rgb($00,$00,$80);
'FUCHSIA' : Result :=rgb($FF,$00,$FF);
'PURPLE' : Result :=rgb($80,$00,$80);
'MAGENTA' : Result :=rgb($FF,$00,$FF);
'CYAN' : Result :=rgb($00,$FF,$FF);
'BLUE VIOLET': Result :=rgb($8A,$2B,$E2);
'GOLD' : Result :=rgb($FF,$D7,$00);
'BROWN' : Result :=rgb($A5,$2A,$2A);
'CORAL' : Result :=rgb($FF,$7F,$50);
'VIOLET' : Result :=rgb($EE,$82,$EE);
end;
end;
end;
{ tFaTokContent }
procedure tFaTokContent.Clear;
begin
CaseSensitive := false; //por defecto
nInstruc := 0;
setLength(Instrucs,0);
end;
function tFaTokContent.AddItem(expTyp: tFaRegExpType; ifMatch, ifFail: string): integer;
//Agrega un ítem a la lista Instrucs[]. Devuelve el número de ítems.
//Configura el comportamiento de la instrucción usando "ifMatch".
var
ifMatch0, ifFail0: string;
function extractIns(var txt: string): string;
//Extrae una instrucción (identificador)
var
p: Integer;
begin
txt := trim(txt);
if txt = '' then exit('');
p := 1;
while (p<=length(txt)) and (txt[p] in ['A'..'Z']) do inc(p);
Result := copy(txt,1,p-1);
txt := copyEx(txt, p);
// Result := copy(txt,1,p);
// txt := copyEx(txt, p+1);
end;
function extractPar(var txt: string; errMsg: string): integer;
//Extrae un valor numérico
var
p, p0: Integer;
sign: Integer;
begin
txt := trim(txt);
if txt = '' then exit(0);
if txt[1] = '(' then begin
//caso esperado
p := 2; //explora
if not (txt[2] in ['+','-','0'..'9']) then //validación
raise ESynFacilSyn.Create(errMsg + ifFail0);
sign := 1; //signo por defecto
if txt[2] = '+' then begin
p := 3; //siguiente caracter
sign := 1;
if not (txt[3] in ['0'..'9']) then
raise ESynFacilSyn.Create(errMsg + ifFail0);
end;
if txt[2] = '-' then begin
p := 3; //siguiente caracter
sign := -1;
if not (txt[3] in ['0'..'9']) then
raise ESynFacilSyn.Create(errMsg + ifFail0);
end;
//Aquí se sabe que en txt[p], viene un númaro
p0 := p; //guarda posición de inicio
while (p<=length(txt)) and (txt[p] in ['0'..'9']) do inc(p);
Result := StrToInt(copy(txt,p0,p-p0)) * Sign; //lee como número
if txt[p]<>')' then raise ESynFacilSyn.Create(errMsg + ifFail0);
inc(p);
txt := copyEx(txt, p+1);
end else begin
raise ESynFacilSyn.Create(errMsg + ifFail0);
end;
end;
function HavePar(var txt: string): boolean;
//Verifica si la cadena empieza con "("
begin
Result := false;
txt := trim(txt);
if txt = '' then exit;
if txt[1] = '(' then begin //caso esperado
Result := true;
end;
end;
var
inst: String;
n: Integer;
begin
ifMatch0 := ifMatch; //guarda valor original
ifFail0 := ifFail; //guarda valor original
inc(nInstruc);
n := nInstruc-1; //último índice
setlength(Instrucs, nInstruc);
Instrucs[n].expTyp := expTyp; //tipo
Instrucs[n].actionMatch := aomNext; //valor por defecto
Instrucs[n].actionFail := aomExit; //valor por defecto
Instrucs[n].destOnMatch:=0; //valor por defecto
Instrucs[n].destOnFail:= 0; //valor por defecto
Result := nInstruc;
//Configura comportamiento
if ifMatch<>'' then begin
ifMatch := UpCase(ifMatch);
while ifMatch<>'' do begin
inst := extractIns(ifMatch);
if inst = 'NEXT' then begin //se pide avanzar al siguiente
Instrucs[n].actionMatch := aomNext;
end else if inst = 'EXIT' then begin //se pide salir
if HavePar(ifMatch) then begin //EXIT con parámetro
Instrucs[n].actionMatch := aomExitpar;
Instrucs[n].destOnMatch := n + extractPar(ifMatch, ERR_SYN_PAR_IFMATCH_);
end else begin //EXIT sin parámetros
Instrucs[n].actionMatch := aomExit;
end;
end else if inst = 'MOVE' then begin
Instrucs[n].actionMatch := aomMovePar; //Mover a una posición
Instrucs[n].destOnMatch := n + extractPar(ifMatch, ERR_SYN_PAR_IFMATCH_);
end else begin
raise ESynFacilSyn.Create(ERR_SYN_PAR_IFMATCH_ + ifMatch0);
end;
ifMatch := Trim(ifMatch);
if (ifMatch<>'') and (ifMatch[1] = ';') then //quita delimitador
ifMatch := copyEx(ifMatch,2);
end;
end;
if ifFail<>'' then begin
ifFail := UpCase(ifFail);
while ifFail<>'' do begin
inst := extractIns(ifFail);
if inst = 'NEXT' then begin //se pide avanzar al siguiente
Instrucs[n].actionFail := aomNext;
end else if inst = 'EXIT' then begin //se pide salir
if HavePar(ifFail) then begin //EXIT con parámetro
Instrucs[n].actionFail := aomExitpar;
Instrucs[n].destOnFail := n + extractPar(ifFail, ERR_SYN_PAR_IFFAIL_);
end else begin //EXIT sin parámetros
Instrucs[n].actionFail := aomExit;
end;
end else if inst = 'MOVE' then begin
Instrucs[n].actionFail := aomMovePar; //Mover a una posición
Instrucs[n].destOnFail := n + extractPar(ifFail, ERR_SYN_PAR_IFFAIL_);
end else begin
raise ESynFacilSyn.Create(ERR_SYN_PAR_IFFAIL_ + ifFail0);
end;
ifFail := Trim(ifFail);
if (ifFail<>'') and (ifFail[1] = ';') then //quita delimitador
ifFail := copyEx(ifFail,2);
end;
end;
end;
procedure tFaTokContent.AddOneInstruct(var exp: string; ifTrue: string; ifFalse: string;
atMatch: integer=-1; atFail: integer=-1);
{Agrega una y solo instrucción al token por contenido. Si encuentra más de una
instrucción, genera una excepción. Si se pone ifTrue en blnnco, se asumirá 'next',
si se pone "ifFalse" en blanco, se se asumirá 'exit'.
Este es el punto de entrada único para agregar una instrucción de Regex a
tFaTokContent}
var
list: String;
str: string;
n: Integer;
c: Char;
expr: string;
t: tFaRegExpType;
begin
if exp='' then exit;
//analiza
expr := exp; //guarda, porque se va a trozar
t := ExtractRegExp(exp, str, list);
case t of
tregChars, //Es de tipo lista de caracteres [...]
tregChars01, //Es de tipo lista de caracteres [...]?
tregChars0_, //Es de tipo lista de caracteres [...]*
tregChars1_: //Es de tipo lista de caracteres [...]+
begin
n := AddItem(t, ifTrue, ifFalse)-1; //agrega
Instrucs[n].aMatch:= atMatch;
Instrucs[n].aFail := atFail;
//Configura caracteres de contenido
for c := #0 to #255 do Instrucs[n].Chars[c] := False;
for c in list do Instrucs[n].Chars[c] := True;
end;
tregString: begin //Es de tipo texto literal
n := AddItem(t, ifTrue, ifFalse)-1; //agrega
Instrucs[n].aMatch:= atMatch;
Instrucs[n].aFail := atFail;
//configura cadena
if CaseSensitive then Instrucs[n].Text := str
else Instrucs[n].Text := UpCase(str); //ignora caja
end;
tregTokPos: begin
n := AddItem(t, ifTrue, ifFalse)-1; //agrega
Instrucs[n].aMatch:= atMatch;
Instrucs[n].aFail := atFail;
//configura cadena
Instrucs[n].tokPos:= StrToInt(str); //Orden de token
end;
else
raise ESynFacilSyn.Create(ERR_UNSUPPOR_EXP_ + expr);
end;
end;
procedure tFaTokContent.AddInstruct(exp: string; ifTrue: string=''; ifFalse: string='';
atMatch: integer=-1; atFail: integer=-1);
//Agrega una instrucción para el procesamiento del token por contenido.
//Solo se debe indicar una instrucción, de otra forma se generará un error.
var
expr: String;
begin
expr := exp; //guarda, porque se va a trozar
AddOneInstruct(exp, ifTrue, ifFalse, atMatch, atFail); //si hay error genera excepción
//Si llegó aquí es porque se obtuvo una expresión válida, pero la
//expresión continua.
if exp<>'' then begin
raise ESynFacilSyn.Create(ERR_UNSUPPOR_EXP_ + expr);
end;
end;
procedure tFaTokContent.AddRegEx(exp: string; Complete: boolean = false);
{Agrega una expresión regular (un conjunto de instrucciones sin opciones de control), al
token por contenido. Las expresiones regulares deben ser solo las soportadas.
Ejemplos son: "[0..9]*[\.][0..9]", "[A..Za..z]*"
Las expresiones se evalúan parte por parte. Si un token no coincide completamente con la
expresión regular, se considera al token, solamente hasta el punto en que coincide.
Si se produce algún error se generará una excepción.}
var
dToStart: Integer;
begin
if Complete then begin
//Cuando no coincide completamente, retrocede hasta el demimitador incial
dToStart := 0; //distamcia al inicio
while exp<>'' do begin
AddOneInstruct(exp,'','exit(-'+ IntToStr(dToStart) + ')');
Inc(dToStart);
end;
end else begin
//La coinicidencia puede ser parcial
while exp<>'' do begin
AddOneInstruct(exp,'',''); //en principio, siempre debe coger una expresión
end;
end;
end;
{ TSynFacilSynBase }
function TSynFacilSynBase.GetSampleSource: String;
begin
Result := fSampleSource;
end;
//funciones básicas
function TSynFacilSynBase.BuscTokEspec(var mat: TArrayTokSpec; cad: string;
out n: integer; TokPos: integer = 0): boolean;
//Busca una cadena en una matriz TArrayTokSpec. Si la ubica devuelve el índice en "n".
var i : integer;
begin
Result := false;
if TokPos = 0 then begin //búsqueda normal
for i := 0 to High(mat) do begin
if mat[i].txt = cad then begin
n:= i;
exit(true);
end;
end;
end else begin //búsqueda con TokPos
for i := 0 to High(mat) do begin
if (mat[i].txt = cad) and (TokPos = mat[i].TokPos) then begin
n:= i;
exit(true);
end;
end;
end;
end;
function TSynFacilSynBase.ToListRegex(list: TFaXMLatrib): string;
//Reemplaza el contenido de una lista en foramto XML (p.ej. "A..Z") al formato de
//listas de expresiones regulares; "[A-Z]"
//Los caracteres "..", cambian a "-" y el caracter "-", cambia a "\-"
var
tmp: String;
begin
tmp := StringReplace(list.val, '-', '\-',[rfReplaceAll]);
tmp := StringReplace(tmp, '..', '-',[rfReplaceAll]);
Result := '[' + tmp + ']'; //completa con llaves
end;
function TSynFacilSynBase.dStartRegex(tStart, tCharsStart: TFaXMLatrib): string;
//Lee los parámetros XML "Start" y "CharsStart"; y extrae el delimitador inicial
//a usar en formato de Expresión Regular.
begin
//validaciones
if tStart.hay and tCharsStart.hay then begin
//No es un caso válido que se den los dos parámetros
raise ESynFacilSyn.Create(ERR_NOT_USE_START);
end;
if not tStart.hay and not tCharsStart.hay then begin
//Tampoco es un caso válido que no se de ninguno.
raise ESynFacilSyn.Create(ERR_PAR_START_CHARS);
end;
//Hay uno u otro parámetro definido
if tStart.hay then begin
Result := EscapeText(tStart.val); //protege a los caracteres especiales
end else if tCharsStart.hay then begin
Result := ToListRegex(tCharsStart); //convierte a expresión regular como [a..z]
end;
end;
procedure TSynFacilSynBase.VerifDelim(delim: string);
//Verifica la validez de un delimitador para un token delimitado.
//Si hay error genera una excepción.
var c:char;
tmp: string;
begin
//verifica contenido
if delim = '' then
raise ESynFacilSyn.Create(ERR_TOK_DELIM_NULL);
//verifica si inicia con caracter de identificador.
if delim[1] in charsIniIden then begin
//Empieza como identificador. Hay que verificar que todos los demás caracteres
//sean también de identificador, de otra forma no se podrá reconocer el token.
tmp := copy(delim, 2, length(delim) );
for c in tmp do
if not CharsIdentif[c] then begin
raise ESynFacilSyn.Create(format(ERR_TOK_DEL_IDE_ERR,[delim]));
end;
end;
end;
procedure TSynFacilSynBase.ValidateParamStart(Start: string; var ListElem: TStringList);
{Valida si la expresión del parámetro es de tipo <literal> o [<lista de cars>], de
otra forma generará una excepción.
Si es de tipo <literal>, valida que sea un delimitador válido.
Devuelve en "ListElem" una lista con con los caracteres (En el caso de [<lista de cars>])
o un solo elemento con una cadena (En el caso de <literal>). Por ejemplo:
Si Start = 'cadena', entonces se tendrá: ListElem = [ 'cadena' ]
Si Start = '[1..5]', entonces se tendrá: ListElem = ['0','1','2','3','4','5']
Si encuentra error, genera excepción.}