Bonjour,
Encore une question la rédaction de grammaire en Perl6.
Je prend conscience du besoin de définir des mots clés ( ou mots réservés) dans une grammaire. Par contre, je n'ai pas trouvé comment faire. Pourriez-vous m'indiquez comment ?
Pour illustrer mon besoin, prenons le testcase suivant (simplification du parsing d'un ordre SQL, seul la clause from est défini) :
Ce code fonctionne bien et permet de récupérer l'AST :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 use Grammar::Tracer; grammar TEST { rule TEST { <from> <where> } rule from {:i FROM <selectedtable> [',' <selectedtable>]* } rule selectedtable {[<shema_name>'.']?<tablename>['@'<linkname>]? [<alias>]? } token shema_name { <identifier> } regex tablename { <identifier> } token linkname { <identifier> } regex alias { <identifier> } token identifier {:i <ident>+ } rule where {:i WHERE .* } regex comma {',' } } my $fic = q:to/FIN_INI/; FROM table1 ,table2 t2 WHERE t1 = t2(+) FIN_INI say TEST.parse($fic, rule => 'TEST');
TEST
| from
| | selectedtable
| | | shema_name
| | | | identifier
| | | | * MATCH "table1"
| | | * MATCH "table1"
| | | tablename
| | | | identifier
| | | | * MATCH "table1"
| | | * MATCH "table1"
| | | alias
| | | | identifier
| | | | * FAIL
| | | * FAIL
| | * MATCH "table1 "
| | selectedtable
| | | shema_name
| | | | identifier
| | | | * MATCH "table2"
| | | * MATCH "table2"
| | | tablename
| | | | identifier
| | | | * MATCH "table2"
| | | * MATCH "table2"
| | | alias
| | | | identifier
| | | | * MATCH "t2"
| | | * MATCH "t2"
| | * MATCH "table2 t2 "
| * MATCH "FROM table1 ,table2 t2 "
| where
| * MATCH "WHERE t1 = t2(+) \n"
* MATCH "FROM table1 ,table2 t2 WHERE t1 = t2(+) \n"
「FROM table1 ,table2 t2 WHERE t1 = t2(+)
」
from => 「FROM table1 ,table2 t2 」
selectedtable => 「table1 」
tablename => 「table1」
identifier => 「table1」
ident => 「table1」
selectedtable => 「table2 t2 」
tablename => 「table2」
identifier => 「table2」
ident => 「table2」
alias => 「t2」
identifier => 「t2」
ident => 「t2」
where => 「WHERE t1 = t2(+)
」
Par contre, si au lieu de parser la chaine de caractère "FROM table1 ,table2 t2 WHERE t1 = t2(+) ", on parse "FROM table1 ,table2 WHERE t1 = t2(+) " alors cela ne marche plus :
TEST
| from
| | selectedtable
| | | shema_name
| | | | identifier
| | | | * MATCH "table1"
| | | * MATCH "table1"
| | | tablename
| | | | identifier
| | | | * MATCH "table1"
| | | * MATCH "table1"
| | | alias
| | | | identifier
| | | | * FAIL
| | | * FAIL
| | * MATCH "table1 "
| | selectedtable
| | | shema_name
| | | | identifier
| | | | * MATCH "table2"
| | | * MATCH "table2"
| | | tablename
| | | | identifier
| | | | * MATCH "table2"
| | | * MATCH "table2"
| | | alias
| | | | identifier
| | | | * MATCH "WHERE"
| | | * MATCH "WHERE"
| | * MATCH "table2 WHERE "
| * MATCH "FROM table1 ,table2 WHERE "
| where
| * FAIL
* FAIL
Nil
On voit, grace au tracer, que le mot clé WHERE a été consommé en tant qu'alias ...
D'où mon besoin d'arriver à exprimer que WHERE est un mot réservé... A moins qu'une autre solution n'existe ?
Merci beaucoup pour votre aide.
Bien cordialement,
Partager