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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090 | -----------------------------------------------------------------------
-- el-expressions-parsers -- Parser for Expression Language
-- Copyright (C) 2009, 2010, 2011, 2012, 2013, 2018, 2019, 2021, 2022 Stephane Carrez
-- Written by Stephane Carrez (Stephane.Carrez@gmail.com)
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
-----------------------------------------------------------------------
with Ada.Characters.Conversions;
with Util.Texts.Builders;
with EL.Functions;
package body EL.Expressions.Parser is
use Ada.Characters.Conversions;
use EL.Expressions.Nodes;
use EL.Functions;
package Wide_Builder is new Util.Texts.Builders (Element_Type => Wide_Wide_Character,
Input => Wide_Wide_String,
Chunk_Size => 100);
function To_Wide_Wide_String (Source : in Wide_Builder.Builder) return Wide_Wide_String
renames Wide_Builder.To_Array;
use Wide_Builder;
type Token_Type is (T_EOL,
T_LEFT_PARENT,
T_RIGHT_PARENT,
T_LT, T_LE, T_GT, T_GE, T_NE, T_EQ, T_EMPTY,
T_NOT,
T_OR, T_AND, T_LOGICAL_AND,
T_MINUS, T_PLUS, T_MUL, T_DIV, T_MOD, T_DOT,
T_QUESTION, T_COLON, T_COMMA,
T_NUMBER, T_LITERAL, T_NAME,
T_TRUE, T_FALSE, T_NULL,
T_BRACE_END,
T_UNKNOWN);
type Parser (Length : Natural) is limited record
Pos : Natural;
Token_Start : Natural;
Token_End : Natural;
Value : Long_Long_Integer;
Pending_Token : Token_Type := T_EOL;
Mapper : Function_Mapper_Access;
Token : Wide_Builder.Builder (100);
Expr : Wide_Wide_String (1 .. Length);
end record;
function Create_Node (Value : in Wide_Builder.Builder) return ELNode_Access;
procedure Put_Back (P : in out Parser; Token : in Token_Type);
-- Parse a literal or an expression
procedure Parse_EL (P : in out Parser;
Result : out ELNode_Access);
procedure Parse_Choice (P : in out Parser; Result : out ELNode_Access);
procedure Parse_Or (P : in out Parser; Result : out ELNode_Access);
procedure Parse_And (P : in out Parser; Result : out ELNode_Access);
procedure Parse_Equality (P : in out Parser; Result : out ELNode_Access);
procedure Parse_Compare (P : in out Parser; Result : out ELNode_Access);
procedure Parse_Math (P : in out Parser; Result : out ELNode_Access);
procedure Parse_Multiply (P : in out Parser; Result : out ELNode_Access);
procedure Parse_Unary (P : in out Parser; Result : out ELNode_Access);
procedure Parse_Function (P : in out Parser;
Namespace : in String;
Name : in String;
Result : out ELNode_Access);
-- Parse the expression buffer to find the next token.
procedure Peek (P : in out Parser; Token : out Token_Type);
procedure Parse_Number (P : in out Parser;
Result : out Long_Long_Integer);
function Create_Node (Value : in Wide_Builder.Builder) return ELNode_Access is
begin
return Create_Node (To_Wide_Wide_String (Value));
end Create_Node;
-- #{bean.name}
-- #{12 + 23}
-- #{bean.name + bean.name}
-- #{bean.name == 2 ? 'test' : 'foo'}
--
-- expr ::= expr ? expr : expr
-- expr ::= expr <op> expr
-- expr ::= <unary> expr
-- expr ::= ( expr )
-- expr ::= expr ? expr : expr
-- expr ::= name . name
-- expr ::= name
-- expr ::= <number>
-- expr ::= <literal>
-- literal ::= '...' | ".."
-- number ::= [0-9]+
--
-- ------------------------------
-- Parse a literal or an expression
-- ------------------------------
procedure Parse_EL (P : in out Parser;
Result : out ELNode_Access) is
Literal, Node : ELNode_Access;
C : Wide_Wide_Character;
begin
scan :
while P.Pos <= P.Expr'Last loop
C := P.Expr (P.Pos);
if C = '\' then
P.Pos := P.Pos + 1;
exit scan when P.Pos > P.Expr'Last;
C := P.Expr (P.Pos);
Append (P.Token, C);
P.Pos := P.Pos + 1;
elsif C = '#' or else C = '$' then
P.Pos := P.Pos + 1;
if P.Pos > P.Expr'Last then
raise Invalid_Expression with "Missing '{' to start expression";
end if;
C := P.Expr (P.Pos);
if C /= '{' then
raise Invalid_Expression with "Missing '{' to start expression";
end if;
if Length (P.Token) > 0 then
if Literal /= null then
Literal := Create_Node (EL_CONCAT, Literal,
Create_Node (P.Token));
else
Literal := Create_Node (P.Token);
end if;
end if;
P.Pos := P.Pos + 1;
Parse_Choice (P, Node);
if P.Pending_Token /= T_BRACE_END then
raise Invalid_Expression with "Missing '}' to close expression";
end if;
P.Pending_Token := T_EOL;
Clear (P.Token);
if Literal /= null then
Literal := Create_Node (EL_CONCAT, Literal, Node);
else
Literal := Node;
end if;
Node := null;
else
Append (P.Token, C);
P.Pos := P.Pos + 1;
end if;
end loop scan;
if Length (P.Token) > 0 then
Node := Create_Node (P.Token);
if Literal /= null then
Result := Create_Node (EL_CONCAT, Literal, Node);
else
Result := Node;
end if;
else
Result := Literal;
end if;
Node := null;
Literal := null;
exception
when others =>
Delete (Result);
Delete (Literal);
Delete (Node);
raise;
end Parse_EL;
-- ------------------------------
-- Parse a choice expression, then Or.
--
-- choice ::= expr '?' expr ':' choice
--
-- Section 1.11: Conditional Operator - A ? B : C
-- ------------------------------
procedure Parse_Choice (P : in out Parser;
Result : out ELNode_Access) is
Left, Right : ELNode_Access;
Token : Token_Type;
begin
Parse_Or (P, Result);
Peek (P, Token);
if Token /= T_QUESTION then
Put_Back (P, Token);
return;
end if;
Parse_Or (P, Left);
Peek (P, Token);
if Token /= T_COLON then
raise Invalid_Expression with "Missing :";
end if;
Parse_Choice (P, Right);
Result := Create_Node (Result, Left, Right);
exception
when others =>
Delete (Result);
Delete (Left);
Delete (Right);
raise;
end Parse_Choice;
-- ------------------------------
-- Parse a logical 'or' expression, then 'and'
--
-- or-expr ::= and-expr || and-expr
--
-- Section 1.9.1 Binary operator
-- ------------------------------
procedure Parse_Or (P : in out Parser;
Result : out ELNode_Access) is
Token : Token_Type;
Right : ELNode_Access;
begin
Parse_And (P, Result);
loop
Peek (P, Token);
exit when Token /= T_OR;
Parse_And (P, Right);
Result := Create_Node (EL_LOR, Result, Right);
Right := null;
end loop;
Put_Back (P, Token);
exception
when others =>
Delete (Result);
Delete (Right);
raise;
end Parse_Or;
-- ------------------------------
-- Parse a logical 'and' expression, then 'equality'
--
-- and-expr ::= equ-expr && equ-expr
--
-- Section 1.9.1 Binary operator
-- ------------------------------
procedure Parse_And (P : in out Parser;
Result : out ELNode_Access) is
Token : Token_Type;
Right : ELNode_Access;
begin
Parse_Equality (P, Result);
loop
Peek (P, Token);
exit when Token /= T_LOGICAL_AND;
Parse_Equality (P, Right);
Result := Create_Node (EL_LAND, Result, Right);
Right := null;
end loop;
Put_Back (P, Token);
exception
when others =>
Delete (Result);
Delete (Right);
raise;
end Parse_And;
-- ------------------------------
-- Parse an equality '==' 'eq' '!=' 'ne expression, then 'compare'
--
-- equ-expr ::= cmp-expr '==' cmp-expr
-- equ-expr ::= cmp-expr '!=' cmp-expr
--
-- Section 1.8.2 Relational Operators
-- ------------------------------
procedure Parse_Equality (P : in out Parser;
Result : out ELNode_Access) is
Token : Token_Type;
Right : ELNode_Access;
begin
Parse_Compare (P, Result);
loop
Peek (P, Token);
exit when Token /= T_EQ and then Token /= T_NE;
Parse_Equality (P, Right);
if Token = T_EQ then
Result := Create_Node (EL_EQ, Result, Right);
else
Result := Create_Node (EL_NE, Result, Right);
end if;
Right := null;
end loop;
Put_Back (P, Token);
exception
when others =>
Delete (Result);
Delete (Right);
raise;
end Parse_Equality;
-- ------------------------------
-- Parse a comparison operation then Math
-- expr ::= expr '<' expr
-- expr ::= expr '<=' expr
-- expr ::= expr '>' expr
-- expr ::= expr '=' expr
-- expr ::= expr '>=' expr
--
-- Section 1.8.1 Relational Operators
-- ------------------------------
procedure Parse_Compare (P : in out Parser;
Result : out ELNode_Access) is
Right : ELNode_Access;
Token : Token_Type;
begin
Parse_Math (P, Result);
loop
Peek (P, Token);
case Token is
when T_LT =>
Parse_Math (P, Right);
Result := Create_Node (EL_LT, Result, Right);
when T_LE =>
Parse_Math (P, Right);
Result := Create_Node (EL_LE, Result, Right);
when T_GT =>
Parse_Math (P, Right);
Result := Create_Node (EL_GT, Result, Right);
when T_GE =>
Parse_Math (P, Right);
Result := Create_Node (EL_GE, Result, Right);
when others =>
exit;
end case;
Right := null;
end loop;
Put_Back (P, Token);
exception
when others =>
Delete (Result);
Delete (Right);
raise;
end Parse_Compare;
-- ------------------------------
-- Parse a math expression '+' or '-' then Multiply
-- expr ::= factor '+' expr
-- expr ::= factor '-' expr
-- expr ::= factor '&' expr
--
-- Section 1.7.1 Binary operators A (+|-) B
-- ------------------------------
procedure Parse_Math (P : in out Parser;
Result : out ELNode_Access) is
Right : ELNode_Access;
Token : Token_Type;
begin
Parse_Multiply (P, Result);
loop
Peek (P, Token);
case Token is
when T_PLUS =>
Parse_Multiply (P, Right);
Result := Create_Node (EL_ADD, Result, Right);
when T_MINUS =>
Parse_Multiply (P, Right);
Result := Create_Node (EL_SUB, Result, Right);
when T_AND =>
Parse_Multiply (P, Right);
Result := Create_Node (EL_AND, Result, Right);
when others =>
exit;
end case;
Right := null;
end loop;
Put_Back (P, Token);
exception
when others =>
Delete (Result);
Delete (Right);
raise;
end Parse_Math;
-- ------------------------------
-- Parse a multiply '*' '/' '%' then Unary
-- factor ::= term '*' factor
-- factor ::= term '/' factor
-- factor ::= term
--
-- Section 1.7.1 Binary operators A * B
-- Section 1.7.2 Binary operators A / B
-- ------------------------------
procedure Parse_Multiply (P : in out Parser;
Result : out ELNode_Access) is
Token : Token_Type;
Right : ELNode_Access;
begin
Parse_Unary (P, Result);
loop
Peek (P, Token);
case Token is
when T_MUL =>
Parse_Unary (P, Right);
Result := Create_Node (EL_MUL, Result, Right);
when T_DIV =>
Parse_Unary (P, Right);
Result := Create_Node (EL_DIV, Result, Right);
when T_MOD =>
Parse_Unary (P, Right);
Result := Create_Node (EL_MOD, Result, Right);
when others =>
exit;
end case;
Right := null;
end loop;
Put_Back (P, Token);
exception
when others =>
Delete (Result);
Delete (Right);
raise;
end Parse_Multiply;
-- ------------------------------
-- Parse a unary '!' '-' 'not' 'empty' then value
-- unary ::= '(' choice ')'
-- unary ::= ! unary
-- unary ::= not unary
-- unary ::= '-' unary
-- term ::= '(' expr ')'
-- term ::= literal
-- term ::= ['-'] number ['.' number [{'e' | 'E'} number]]
-- term ::= name '.' name
-- number ::= [0-9]+
--
-- Section 1.9.2 Unary not operator
-- Section 1.10 Empty operator
-- Section 1.12 Parentheses
-- Section 1.3 Literals
-- ------------------------------
procedure Parse_Unary (P : in out Parser;
Result : out ELNode_Access) is
Token : Token_Type;
Node : ELNode_Access;
begin
scan_unary :
loop
Peek (P, Token);
case Token is
-- Parenthesis expression
when T_LEFT_PARENT =>
Parse_Choice (P, Result);
Peek (P, Token);
if Token /= T_RIGHT_PARENT then
raise Invalid_Expression with "Missing ')' at end of expression";
end if;
return;
when T_NOT =>
Parse_Unary (P, Node);
Result := Create_Node (EL_NOT, Node);
return;
when T_MINUS =>
Parse_Unary (P, Node);
Result := Create_Node (EL_MINUS, Node);
return;
when T_EMPTY =>
Parse_Unary (P, Node);
Result := Create_Node (EL_EMPTY, Node);
return;
when T_NUMBER =>
Result := Create_Node (P.Value);
return;
--
when T_LITERAL =>
Result := Create_Node (P.Token);
return;
when T_TRUE =>
Result := Create_Node (True);
return;
when T_FALSE =>
Result := Create_Node (False);
return;
when T_NULL =>
Result := Create_Node (False);
return;
when T_NAME =>
-- name
-- name.name.name
-- name[expr]
-- name.name[expr]
-- name(expr,...,expr)
-- name(expr,...,expr).name
-- name(expr,...,expr)[expr]
declare
C : Wide_Wide_Character;
begin
if P.Pos <= P.Expr'Last then
C := P.Expr (P.Pos);
else
C := ' ';
end if;
-- Parse a function call
if C = ':' then
declare
Name : constant String
:= To_String (P.Expr (P.Token_Start .. P.Token_End));
begin
P.Pos := P.Pos + 1;
Peek (P, Token);
if P.Pos <= P.Expr'Last then
C := P.Expr (P.Pos);
else
C := ' ';
end if;
if Token /= T_NAME or else C /= '(' then
raise Invalid_Expression with "Missing function name after ':'";
end if;
Parse_Function (P, Name, To_String (P.Expr (P.Token_Start .. P.Token_End)),
Result);
end;
if P.Pos > P.Expr'Last then
return;
end if;
C := P.Expr (P.Pos);
if C /= '.' and then C /= '[' then
return;
end if;
if C = '.' then
P.Pos := P.Pos + 1;
Peek (P, Token);
if Token /= T_NAME then
raise Invalid_Expression with "Missing name name after '.'";
end if;
end if;
-- Parse a function call
elsif C = '(' then
Parse_Function (P, "", To_String (P.Expr (P.Token_Start .. P.Token_End)),
Result);
if P.Pos > P.Expr'Last then
return;
end if;
C := P.Expr (P.Pos);
if C /= '.' and then C /= '[' then
return;
end if;
if C = '.' then
P.Pos := P.Pos + 1;
Peek (P, Token);
if Token /= T_NAME then
raise Invalid_Expression with "Missing name name after '.'";
end if;
end if;
elsif C /= '.' then
Result := Create_Variable (P.Expr (P.Token_Start .. P.Token_End));
else
Result := null;
end if;
if C = '.' then
if Result /= null then
Result := Create_Value (Variable => Result,
Name => P.Expr (P.Token_Start .. P.Token_End));
if P.Pos > P.Expr'Last then
C := ' ';
else
C := P.Expr (P.Pos);
end if;
else
Result := Create_Variable (P.Expr (P.Token_Start .. P.Token_End));
end if;
-- Parse one or several property extensions
while C = '.' loop
P.Pos := P.Pos + 1;
Peek (P, Token);
exit when Token /= T_NAME;
Result := Create_Value (Variable => Result,
Name => P.Expr (P.Token_Start .. P.Token_End));
if P.Pos <= P.Expr'Last then
C := P.Expr (P.Pos);
else
C := ' ';
end if;
end loop;
end if;
-- Recognize a basic form of array index.
if C = '[' then
P.Pos := P.Pos + 1;
Peek (P, Token);
if Token = T_NAME then
Result := Create_Value (Variable => Result,
Name => P.Expr (P.Token_Start .. P.Token_End));
elsif Token = T_LITERAL then
Result := Create_Value (Variable => Result,
Name => To_Wide_Wide_String (P.Token));
else
raise Invalid_Expression with "Missing string in array index []";
end if;
if P.Pos > P.Expr'Last or else P.Expr (P.Pos) /= ']' then
raise Invalid_Expression with "Missing ']' to close array index";
end if;
P.Pos := P.Pos + 1;
end if;
end;
return;
when others =>
raise Invalid_Expression with "Syntax error in expression";
end case;
end loop scan_unary;
exception
when others =>
Delete (Result);
Delete (Node);
raise;
end Parse_Unary;
-- ------------------------------
-- Put back a token in the buffer.
-- ------------------------------
procedure Put_Back (P : in out Parser; Token : in Token_Type) is
begin
P.Pending_Token := Token;
end Put_Back;
-- ------------------------------
-- Parse the expression buffer to find the next token.
-- ------------------------------
procedure Peek (P : in out Parser; Token : out Token_Type) is
C, C1 : Wide_Wide_Character;
begin
Token := T_UNKNOWN;
-- If a token was put back, return it.
if P.Pending_Token /= T_EOL then
Token := P.Pending_Token;
P.Pending_Token := T_EOL;
return;
end if;
-- Skip white spaces
while P.Pos <= P.Expr'Last loop
C := P.Expr (P.Pos);
exit when C /= ' ';
P.Pos := P.Pos + 1;
end loop;
-- Check for end of string.
if P.Pos > P.Expr'Last then
Token := T_EOL;
return;
end if;
-- See what we have and continue parsing.
P.Pos := P.Pos + 1;
case C is
-- Literal string using single or double quotes
-- Collect up to the end of the string and put
-- the result in the parser token result.
when ''' | '"' =>
Clear (P.Token);
while P.Pos <= P.Expr'Last loop
C1 := P.Expr (P.Pos);
P.Pos := P.Pos + 1;
if C1 = '\' then
C1 := P.Expr (P.Pos);
P.Pos := P.Pos + 1;
elsif C1 = C then
Token := T_LITERAL;
return;
end if;
Append (P.Token, C1);
end loop;
raise Invalid_Expression with "Missing ' or """;
-- Number
when '0' .. '9' =>
P.Pos := P.Pos - 1;
Parse_Number (P, P.Value);
if P.Pos <= P.Expr'Last then
declare
Decimal_Part : Long_Long_Integer := 0;
begin
C := P.Expr (P.Pos);
if C = '.' then
P.Pos := P.Pos + 1;
if P.Pos <= P.Expr'Last then
C := P.Expr (P.Pos);
if C in '0' .. '9' then
Parse_Number (P, Decimal_Part);
end if;
end if;
end if;
end;
end if;
Token := T_NUMBER;
return;
-- Parse a name composed of letters or digits.
when 'a' .. 'z' | 'A' .. 'Z' =>
P.Token_Start := P.Pos - 1;
while P.Pos <= P.Expr'Last loop
C1 := P.Expr (P.Pos);
exit when not (C1 in 'a' .. 'z' or else C1 in 'A' .. 'Z'
or else C1 in '0' .. '9' or else C1 = '_');
P.Pos := P.Pos + 1;
end loop;
P.Token_End := P.Pos - 1;
-- and empty eq false ge gt le lt ne not null true
case C is
when 'a' =>
if P.Expr (P.Token_Start .. P.Token_End) = "and" then
Token := T_LOGICAL_AND;
return;
end if;
when 'd' =>
if P.Expr (P.Token_Start .. P.Token_End) = "div" then
Token := T_DIV;
return;
end if;
when 'e' =>
if P.Expr (P.Token_Start .. P.Token_End) = "eq" then
Token := T_EQ;
return;
elsif P.Expr (P.Token_Start .. P.Token_End) = "empty" then
Token := T_EMPTY;
return;
end if;
when 'f' | 'F' =>
if P.Expr (P.Token_Start .. P.Token_End) = "false" then
Token := T_FALSE;
return;
end if;
when 'g' | 'G' =>
if P.Expr (P.Token_Start .. P.Token_End) = "ge" then
Token := T_GE;
return;
elsif P.Expr (P.Token_Start .. P.Token_End) = "gt" then
Token := T_GT;
return;
end if;
when 'm' | 'M' =>
if P.Expr (P.Token_Start .. P.Token_End) = "mod" then
Token := T_MOD;
return;
end if;
when 'l' | 'L' =>
if P.Expr (P.Token_Start .. P.Token_End) = "le" then
Token := T_LE;
return;
elsif P.Expr (P.Token_Start .. P.Token_End) = "lt" then
Token := T_LT;
return;
end if;
when 'n' | 'N' =>
if P.Expr (P.Token_Start .. P.Token_End) = "not" then
Token := T_NOT;
return;
elsif P.Expr (P.Token_Start .. P.Token_End) = "ne" then
Token := T_NE;
return;
elsif P.Expr (P.Token_Start .. P.Token_End) = "null" then
Token := T_NULL;
return;
end if;
when 'o' =>
if P.Expr (P.Token_Start .. P.Token_End) = "or" then
Token := T_OR;
return;
end if;
when 't' | 'T' =>
if P.Expr (P.Token_Start .. P.Token_End) = "true" then
Token := T_TRUE;
return;
end if;
when others =>
null;
end case;
Token := T_NAME;
return;
when '(' =>
Token := T_LEFT_PARENT;
return;
when ')' =>
Token := T_RIGHT_PARENT;
return;
when '+' =>
Token := T_PLUS;
return;
when '-' =>
Token := T_MINUS;
return;
when '.' =>
Token := T_DOT;
return;
when ',' =>
Token := T_COMMA;
return;
when '*' =>
Token := T_MUL;
return;
when '/' =>
Token := T_DIV;
return;
when '%' =>
Token := T_MOD;
return;
when '?' =>
Token := T_QUESTION;
return;
when ':' =>
Token := T_COLON;
return;
when '!' =>
Token := T_NOT;
if P.Pos <= P.Expr'Last then
C1 := P.Expr (P.Pos);
if C1 = '=' then
P.Pos := P.Pos + 1;
Token := T_NE;
end if;
end if;
return;
when '<' =>
-- Comparison operators < or <=
Token := T_LT;
if P.Pos <= P.Expr'Last then
C1 := P.Expr (P.Pos);
if C1 = '=' then
P.Pos := P.Pos + 1;
Token := T_LE;
end if;
end if;
return;
when '>' =>
-- Comparison operators > or >=
Token := T_GT;
if P.Pos <= P.Expr'Last then
C1 := P.Expr (P.Pos);
if C1 = '=' then
P.Pos := P.Pos + 1;
Token := T_GE;
end if;
end if;
return;
when '&' =>
Token := T_AND;
if P.Pos <= P.Expr'Last then
C1 := P.Expr (P.Pos);
if C1 = '&' then
Token := T_LOGICAL_AND;
P.Pos := P.Pos + 1;
end if;
end if;
return;
when '|' =>
Token := T_UNKNOWN;
if P.Pos <= P.Expr'Last then
C1 := P.Expr (P.Pos);
if C1 = '|' then
Token := T_OR;
P.Pos := P.Pos + 1;
end if;
end if;
return;
when '=' =>
Token := T_UNKNOWN;
if P.Pos <= P.Expr'Last then
C1 := P.Expr (P.Pos);
if C1 = '=' then
Token := T_EQ;
P.Pos := P.Pos + 1;
end if;
end if;
return;
when '}' =>
Token := T_BRACE_END;
return;
when others =>
Token := T_UNKNOWN;
return;
end case;
end Peek;
-- ------------------------------
-- Parse a number
-- ------------------------------
procedure Parse_Number (P : in out Parser;
Result : out Long_Long_Integer) is
Value : Long_Long_Integer := 0;
Num : Long_Long_Integer;
C : Wide_Wide_Character;
begin
while P.Pos <= P.Expr'Last loop
C := P.Expr (P.Pos);
exit when C not in '0' .. '9';
Num := Wide_Wide_Character'Pos (C) - Wide_Wide_Character'Pos ('0');
Value := Value * 10 + Num;
P.Pos := P.Pos + 1;
end loop;
Result := Value;
end Parse_Number;
-- ------------------------------
-- Parse a function call.
-- The function call can have up to 4 arguments.
-- ------------------------------
procedure Parse_Function (P : in out Parser;
Namespace : in String;
Name : in String;
Result : out ELNode_Access) is
Token : Token_Type;
Arg1, Arg2, Arg3, Arg4 : ELNode_Access;
Func : Function_Access;
begin
if P.Mapper = null then
raise Invalid_Expression with "There is no function mapper";
end if;
Func := P.Mapper.Get_Function (Namespace, Name);
-- if Func = null then
-- raise Invalid_Expression with "Function '" & N & "' not found";
-- end if;
-- Extract the first argument.
-- Number of arguments is pre-defined
P.Pos := P.Pos + 1;
Parse_Choice (P, Arg1);
Peek (P, Token);
if Token /= T_COMMA then
if Token /= T_RIGHT_PARENT then
raise Invalid_Expression with "Missing ')' at end of function call";
end if;
Result := Create_Node (Func, Arg1);
return;
end if;
Parse_Choice (P, Arg2);
Peek (P, Token);
if Token /= T_COMMA then
if Token /= T_RIGHT_PARENT then
raise Invalid_Expression with "Missing ')' at end of function call";
end if;
Result := Create_Node (Func, Arg1, Arg2);
return;
end if;
Parse_Choice (P, Arg3);
Peek (P, Token);
if Token /= T_COMMA then
if Token /= T_RIGHT_PARENT then
raise Invalid_Expression with "Missing ')' at end of function call";
end if;
Result := Create_Node (Func, Arg1, Arg2, Arg3);
return;
end if;
Parse_Choice (P, Arg4);
Peek (P, Token);
if Token /= T_RIGHT_PARENT then
raise Invalid_Expression with "Missing ')' at end of function call";
end if;
Result := Create_Node (Func, Arg1, Arg2, Arg3, Arg4);
exception
when others =>
Delete (Result);
Delete (Arg1);
Delete (Arg2);
Delete (Arg3);
Delete (Arg4);
raise;
end Parse_Function;
procedure Parse (Expr : in String;
Context : in ELContext'Class;
Result : out EL.Expressions.Nodes.ELNode_Access) is
P : Parser (Length => Expr'Length);
begin
Result := null;
P.Mapper := Context.Get_Function_Mapper;
P.Expr := To_Wide_Wide_String (Expr);
P.Pos := P.Expr'First;
Parse_EL (P, Result);
if P.Pos <= P.Expr'Last or else P.Pending_Token /= T_EOL then
raise Invalid_Expression with "Syntax error at end of expression";
end if;
exception
when others =>
Delete (Result);
raise;
end Parse;
procedure Parse (Expr : in Wide_Wide_String;
Context : in ELContext'Class;
Result : out EL.Expressions.Nodes.ELNode_Access) is
P : Parser (Length => Expr'Length);
begin
Result := null;
P.Mapper := Context.Get_Function_Mapper;
P.Expr := Expr;
P.Pos := P.Expr'First;
Parse_EL (P, Result);
if P.Pos <= P.Expr'Last or else P.Pending_Token /= T_EOL then
raise Invalid_Expression with "Syntax error at end of expression";
end if;
exception
when others =>
Delete (Result);
raise;
end Parse;
end EL.Expressions.Parser;
|