in reply to parse lisp style config
I finish a little script, partly achieve my goal, firstly parsed to a nested list, then parsed to pairs or arrays.use '( (a) (b))' to represent list which avoid ambiguity. list is ok, and it keep order. Iterate list first, because it can not get deep value only be chained hash ref.A little example as below.package MyPairList; use strict; use warnings; use feature 'state'; use Data::Dumper; use List::Util qw(all none); use Exporter 'import'; our @EXPORT = qw(pair_list_parse); sub pair_list_parse { my $string = shift; sub get_tokens { my $string = shift; my @tokens = $string =~ /([()]|[^()\s]+)/g; } my @tokens = get_tokens $string; sub parse_list { state @tokens = @_; my @list; while (my $token = shift @tokens) { if ($token eq '(') { push @list, parse_list(); } elsif ($token eq ')') { return \@list; } else { push @list, $token; } } return \@list; } my $list = parse_list @tokens; sub parse_nested_list { my ($list) = @_; my %hash; if (none {ref $_} @$list) { if (@$list == 1) { return $list->[0]; } elsif (@$list == 2) { return { $list->[0] => $list->[1] }; } else { return $list } } elsif (all {ref $_} @$list) { return [ map { parse_nested_list($_) } @$list ]; } else { my ($key, $ref, @remain) = @$list; die 'wrong key ', Dumper $key if ref $key; die 'wrong value ', Dumper $ref unless ref $ref; die 'redundant value', Dumper \@remain if @remain; $hash{$key} = parse_nested_list($ref); } return \%hash; } return parse_nested_list($list) } 1;
parsed to(config( (a A) (b B) ((good) (bad)) (d e f) (a (a1 (a2 A))) ) )
Like perl do, the parse result is normally what people mean$VAR1 = [ { 'config' => [ { 'a' => 'A' }, { 'b' => 'B' }, [ 'good', 'bad' ], [ 'd', 'e', 'f' ], { 'a' => { 'a1' => { 'a2' => 'A' } } } ] } ];
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: parse lisp style config
by choroba (Cardinal) on Nov 28, 2024 at 10:20 UTC | |
by LanX (Saint) on Nov 28, 2024 at 12:04 UTC | |
by vincentaxhe (Scribe) on Nov 28, 2024 at 13:11 UTC | |
by choroba (Cardinal) on Nov 28, 2024 at 14:20 UTC | |
by vincentaxhe (Scribe) on Nov 29, 2024 at 01:41 UTC | |
by ikegami (Patriarch) on Nov 28, 2024 at 15:42 UTC | |
by vincentaxhe (Scribe) on Nov 29, 2024 at 01:38 UTC | |
by ikegami (Patriarch) on Nov 29, 2024 at 05:26 UTC | |
|