| 1 | # You may distribute under the terms of either the GNU General Public License | ||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||
| 3 | # | ||
| 4 | # (C) Paul Evans, 2013-2023 -- leonerd@leonerd.org.uk | ||
| 5 | |||
| 6 | use v5.20; | ||
| 7 | use warnings; | ||
| 8 | use Object::Pad 0.807; | ||
| 9 | |||
| 10 | package Tickit::Style::Parser 0.56; | ||
| 11 | class Tickit::Style::Parser :strict(params); | ||
| 12 | |||
| 13 | inherit Parser::MGC; | ||
| 14 | |||
| 15 | # Identifiers can include hyphens | ||
| 16 | use constant pattern_ident => qr/[A-Z0-9_-]+/i; | ||
| 17 | |||
| 18 | # Allow #-style line comments | ||
| 19 | use constant pattern_comment => qr/#.*\n/; | ||
| 20 | |||
| 21 | method parse | ||
| 22 | { | ||
| 23 | $self->sequence_of( \&parse_def ); | ||
| 24 | } | ||
| 25 | |||
| 26 | method token_typename | ||
| 27 | { | ||
| 28 | # Also accept the generic "*" wildcard | ||
| 29 | $self->generic_token( typename => qr/(?:${\pattern_ident}::)*${\pattern_ident}|\*/ ); | ||
| 30 | } | ||
| 31 | |||
| 32 | method parse_def | ||
| 33 | { | ||
| 34 | my $type = $self->token_typename; | ||
| 35 | $self->commit; | ||
| 36 | |||
| 37 | my $class; | ||
| 38 | if( $self->maybe_expect( '.' ) ) { | ||
| 39 | $class = $self->token_ident; | ||
| 40 | } | ||
| 41 | |||
| 42 | my %tags; | ||
| 43 | while( $self->maybe_expect( ':' ) ) { | ||
| 44 | $tags{$self->token_ident}++; | ||
| 45 | } | ||
| 46 | |||
| 47 | my %style; | ||
| 48 | $self->scope_of( | ||
| 49 | '{', | ||
| 50 | sub { $self->sequence_of( sub { | ||
| 51 | $self->any_of( | ||
| 52 | sub { | ||
| 53 | my $delete = $self->maybe_expect( '!' ); | ||
| 54 | my $key = $self->token_ident; | ||
| 55 | $self->commit; | ||
| 56 | |||
| 57 | $key =~ s/-/_/g; | ||
| 58 | |||
| 59 | if( $delete ) { | ||
| 60 | $style{$key} = undef; | ||
| 61 | } | ||
| 62 | else { | ||
| 63 | $self->expect( ':' ); | ||
| 64 | my $value = $self->any_of( | ||
| 65 | $self->can( "token_int" ), | ||
| 66 | $self->can( "token_string" ), | ||
| 67 | \&token_boolean, | ||
| 68 | ); | ||
| 69 | $style{$key} = $value; | ||
| 70 | } | ||
| 71 | |||
| 72 | }, | ||
| 73 | sub { | ||
| 74 | $self->expect( '<' ); $self->commit; | ||
| 75 | my $key = $self->maybe_expect( '>' ) || $self->substring_before( '>' ); | ||
| 76 | $self->expect( '>' ); | ||
| 77 | |||
| 78 | $self->expect( ':' ); | ||
| 79 | |||
| 80 | $style{"<$key>"} = $self->token_ident; | ||
| 81 | } | ||
| 82 | ); | ||
| 83 | $self->expect( ';' ); | ||
| 84 | } ) }, | ||
| 85 | '}' | ||
| 86 | ); | ||
| 87 | |||
| 88 | return Tickit::Style::Parser::_Definition->new( | ||
| 89 | type => $type, | ||
| 90 | class => $class, | ||
| 91 | tags => \%tags, | ||
| 92 | style => \%style, | ||
| 93 | ); | ||
| 94 | } | ||
| 95 | |||
| 96 | method token_boolean | ||
| 97 | { | ||
| 98 | return $self->token_kw(qw( true false )) eq "true"; | ||
| 99 | } | ||
| 100 | |||
| 101 | class # hide from indexer | ||
| 102 | Tickit::Style::Parser::_Definition :strict(params) { | ||
| 103 | |||
| 104 | field $type :reader :param; | ||
| 105 | field $class :reader :param; | ||
| 106 | field $tags :reader :param; | ||
| 107 | field $style :reader :param; | ||
| 108 | } | ||
| 109 | |||
| 110 | 0x55AA; |