-
-
Save xlat/5be8c961d21b6350a2eb to your computer and use it in GitHub Desktop.
my first p6 grammar.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use v6; | |
#~ use Grammar::Debugger; | |
#~ use Grammar::Tracer; | |
grammar Plexus::Expression::Grammar { | |
my @functions = < AND OR IF SPLIT CLOSINGDATE ENDMONTHDATE | |
MIN MAX IN OUT SUM ABS EXISTS EXIST | |
XOR REXIST LONG >; | |
my @macros = < LISTRPT LIST GETBANKINFO HOSTID >; | |
rule TOP { ^ <expression> $ } | |
rule field { | |
|| <report> <dim> <rubric> <column>? | |
|| <dim> <rubric> <column>? | |
|| <rubric> <column>? | |
} | |
rule report { '[' <identifier> <report_modifier>* ']' } | |
rule dim { '[' <identifier> <dim_modifier_list>* '].' } | |
rule rubric { '[' <rubric_identifier> <rubric_modifier>? ']' || '#' } | |
token rubric_modifier { ';^' <-[\]]>+} | |
token rubric_identifier { [ <[A..Z]+[a..z]+[0..9]+[\.*_\-]> || <[#\d+]> ]+ } | |
token column { '.' <[\w_]>+ } | |
token identifier { <[\w.]>+ } | |
token report_modifier { :i ':' <[CMYD]> '=' <[\w\$]>+ } | |
token dim_modifier_list { ':' <dim_modifier_list_item>* % ',' } | |
token dim_modifier_list_item { '^'? <dim_modifier_list_item_element> } | |
token dim_modifier_list_item_element { <variable> | |
|| <macro_call> | |
|| <identifier> | |
} | |
token variable { '$' <identifier> } | |
rule macro_name { :i <@macros> } | |
token macro_call { <macro_name> '(' <macro_param_list>+ % ';' ')' } | |
token macro_param_list { <[\w.,'"*-]>+ } | |
rule function_call { <function_name> '(' <param_list>+ % ';' ')' } | |
rule param_list { <expression> || <identifier> } | |
rule function_name { :i <@functions> } | |
token sign { <[+-]> } | |
token number { [\d+[[\.||\,]\d+]?\%?] } | |
rule string { <simple_quote> || <double_quote> } | |
token simple_quote { \' [\\\'||<-[\']>]*\' } #fixe scite syntaxhighlither' | |
token double_quote { \" [\\\"||<-[\"]>]*\" } #fixe scite syntaxhighlither" | |
rule expression { <bool_expr> } | |
rule bool_expr { <add_expr>+ % ['=='||'!='||'=>'||'=<'||'<>'||'><'||'<='||'>='||'<'||'>'||'='] } | |
rule add_expr { <mul_expr>+ % [\+||\-] } | |
rule mul_expr { <atome>+ % [\*||\/] } #/; | |
rule atome { <sign>? <atome_item> } | |
rule atome_item { | |
|| <.before \$> <variable> | |
|| <.before [\'|\"]><string> | |
|| <.before [\#|\[]><field> | |
|| '(' expression ')' | |
|| <number> | |
|| <function_call> | |
|| <macro_call> | |
} | |
} | |
my @data = split /\n/, chomp q:to<DATA>; | |
[1-*;^1-000-X1-XX1-00000] | |
[11::LIST(CTYGRP;RU,RE;,;0)].[1.222].XX019 | |
[S11:M=$CMONTH][2d].[2-026-X3-EUR-40000].BRX | |
[14C:C=N:Y=2013:M=12][CASRO].[1.1.99].Amount1 | |
[14C:C=N][CASRO].[1.1.99].Amount1 | |
[14C][CASRO:^41,40:02].[1.1.99].Amount1 | |
[14C][CASRO:^41:01,02].[1.1.99].Amount1 | |
[14C][CASRO:41,51:01,02].[1.1.99].Amount1 | |
[14C][CASRO:41:01].[1.1.99].Amount1 | |
[14C][CASRO].[1.1.99].Amount1 | |
[14C][CASRO].[1-#1].Amount1 | |
[14C][CASRO].[1-*].Amount1 | |
[14C][CASRO].#.Amount1 | |
[CASRO].[1.1.99].Amount1 | |
[CASRO].[1-#1].Amount1 | |
[CASRO].#.Amount1 | |
[1.1.99].Amount1 | |
[1-#1].Amount1 | |
#.Amount1 | |
if([1.1.11].1>0;1;[22.4].2=[22.5].2)<SUM([40.*].1) | |
DATA | |
#: | |
for @data -> $expression { | |
if my $parse-tree = try { Plexus::Expression::Grammar.parse( $expression ) } { | |
say "$expression is a valid Plexus expression, parse-tree: " ~ $parse-tree.gist; | |
} else { | |
my $err = $! ?? " : $!\n" ~ $!.backtrace.full !! ''; | |
say "$expression is NOT a valid Plexus expression $err"; | |
exit; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment