| 1 | package Sim::Agent::Parser; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | sub new | ||||||||||||||||||||||||||||||||||||||||||
| 7 | 3 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 3 | bless {}, shift; | |||||||||||||||||||||||||||||||||||||||||
| 9 | } | ||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | sub parse_file | ||||||||||||||||||||||||||||||||||||||||||
| 12 | 3 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse_file: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 3 | my ($self, $file) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | 3 | ▶ | open my $fh, '<', $file or die "Cannot open $file"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 3 | local $/; | |||||||||||||||||||||||||||||||||||||||||
| 17 | 3 | my $text = <$fh>; | |||||||||||||||||||||||||||||||||||||||||
| 18 | 3 | close $fh; | |||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 3 | my $tokens = tokenize($text); | |||||||||||||||||||||||||||||||||||||||||
| 21 | 3 | my $ast = parse($tokens); | |||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | 3 | return $ast; | |||||||||||||||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | #################################### | ||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | sub tokenize | ||||||||||||||||||||||||||||||||||||||||||
| 30 | 3 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
tokenize: called CC=17 SCAR=28.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | 3 | my ($text) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | 3 | my @tokens; | |||||||||||||||||||||||||||||||||||||||||
| 35 | 3 | my $i = 0; | |||||||||||||||||||||||||||||||||||||||||
| 36 | 3 | my $len = length($text); | |||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | 3 | ▶ | while ($i < $len) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | { | ||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | 770 | my $char = substr($text, $i, 1); | |||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | # skip whitespace | ||||||||||||||||||||||||||||||||||||||||||
| 44 | 770 | ▶ | if ($char =~ /\s/) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | { | ||||||||||||||||||||||||||||||||||||||||||
| 46 | 394 | $i++; | |||||||||||||||||||||||||||||||||||||||||
| 47 | 394 | next; | |||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | # comment | ||||||||||||||||||||||||||||||||||||||||||
| 51 | 376 | ▶ | if ($char eq ';') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | { | ||||||||||||||||||||||||||||||||||||||||||
| 53 | 1 | $i++; | |||||||||||||||||||||||||||||||||||||||||
| 54 | 1 | ▶ | $i++ while $i < $len && substr($text,$i,1) ne "\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$i < $len and substr($text, $i, 1) ne "\n"
MC/DC50%1/2
$i < $len and substr($text, $i, 1) ne "\n"
$i < $len
substr($text, $i, 1) ne "\n"
Truth table66%2/3
A and B
A= $i < $lenB= substr($text, $i, 1) ne "\n"
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 1 | next; | |||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | # left paren | ||||||||||||||||||||||||||||||||||||||||||
| 59 | 375 | ▶ | if ($char eq '(') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | { | ||||||||||||||||||||||||||||||||||||||||||
| 61 | 100 | push @tokens, { type => 'LPAREN' }; | |||||||||||||||||||||||||||||||||||||||||
| 62 | 100 | $i++; | |||||||||||||||||||||||||||||||||||||||||
| 63 | 100 | next; | |||||||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | # right paren | ||||||||||||||||||||||||||||||||||||||||||
| 67 | 275 | ▶ | if ($char eq ')') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | { | ||||||||||||||||||||||||||||||||||||||||||
| 69 | 100 | push @tokens, { type => 'RPAREN' }; | |||||||||||||||||||||||||||||||||||||||||
| 70 | 100 | $i++; | |||||||||||||||||||||||||||||||||||||||||
| 71 | 100 | next; | |||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | # string | ||||||||||||||||||||||||||||||||||||||||||
| 75 | 175 | ▶ | if ($char eq '"') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | { | ||||||||||||||||||||||||||||||||||||||||||
| 77 | 32 | $i++; | |||||||||||||||||||||||||||||||||||||||||
| 78 | 32 | my $str = ''; | |||||||||||||||||||||||||||||||||||||||||
| 79 | 32 | ▶ | while ($i < $len) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | { | ||||||||||||||||||||||||||||||||||||||||||
| 81 | 667 | my $c = substr($text,$i,1); | |||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | 667 | ▶ | if ($c eq '\\') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | { | ||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | $i++; | |||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | my $next = substr($text,$i,1); | |||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | $str .= $next; | |||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | $i++; | |||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | 667 | ▶ | last if $c eq '"'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | 635 | $str .= $c; | |||||||||||||||||||||||||||||||||||||||||
| 95 | 635 | $i++; | |||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | 32 | ▶ | die "Unterminated string" if $i >= $len; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | 32 | $i++; # skip closing quote | |||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | 32 | push @tokens, { type => 'STRING', value => $str }; | |||||||||||||||||||||||||||||||||||||||||
| 103 | 32 | next; | |||||||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | # symbol | ||||||||||||||||||||||||||||||||||||||||||
| 107 | 143 | my $symbol = ''; | |||||||||||||||||||||||||||||||||||||||||
| 108 | 143 | ▶ | while ($i < $len) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | { | ||||||||||||||||||||||||||||||||||||||||||
| 110 | 1162 | my $c = substr($text,$i,1); | |||||||||||||||||||||||||||||||||||||||||
| 111 | 1162 | ▶ | last if $c =~ /\s/ || $c eq '(' || $c eq ')'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$c =~ /\s/ or $c eq "(" or $c eq ")"
$c =~ /\s/ or $c eq "("
MC/DC66%2/3
$c =~ /\s/ or $c eq "(" or $c eq ")"
$c =~ /\s/
$c eq "("
$c eq ")"
Truth table75%3/4
A or B or C
A= $c =~ /\s/B= $c eq "("C= $c eq ")"
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | 1019 | $symbol .= $c; | |||||||||||||||||||||||||||||||||||||||||
| 113 | 1019 | $i++; | |||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | 143 | push @tokens, { type => 'SYMBOL', value => $symbol }; | |||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | 3 | return \@tokens; | |||||||||||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||
| 123 | sub parse | ||||||||||||||||||||||||||||||||||||||||||
| 124 | 3 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | 3 | my ($tokens) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | 3 | my $pos = 0; | |||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | 3 | my $ast = parse_expr($tokens, \$pos); | |||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | 3 | ▶ | die "Extra tokens after root" if $pos < @$tokens; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | 3 | return $ast; | |||||||||||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | sub parse_expr | ||||||||||||||||||||||||||||||||||||||||||
| 139 | 275 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse_expr: called CC=7 SCAR=19.6
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | 275 | my ($tokens, $pos_ref) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | 275 | ▶ | my $token = $tokens->[$$pos_ref] | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 144 | or die "Unexpected end of input"; | ||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||
| 146 | # list | ||||||||||||||||||||||||||||||||||||||||||
| 147 | 275 | ▶ | if ($token->{type} eq 'LPAREN') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 148 | { | ||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||
| 150 | 100 | $$pos_ref++; | |||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||
| 152 | 100 | my @list; | |||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||
| 154 | 100 | while (1) { | |||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||
| 156 | 372 | ▶ | my $next = $tokens->[$$pos_ref] | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 157 | or die "Unclosed parenthesis"; | ||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||
| 159 | 372 | ▶ | last if $next->{type} eq 'RPAREN'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||
| 161 | 272 | push @list, parse_expr($tokens, $pos_ref); | |||||||||||||||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||
| 164 | 100 | $$pos_ref++; # skip RPAREN | |||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||
| 166 | 100 | return \@list; | |||||||||||||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||
| 169 | # atom | ||||||||||||||||||||||||||||||||||||||||||
| 170 | 175 | ▶ | if ($token->{type} eq 'STRING') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 171 | { | ||||||||||||||||||||||||||||||||||||||||||
| 172 | 32 | $$pos_ref++; | |||||||||||||||||||||||||||||||||||||||||
| 173 | 32 | return $token->{value}; | |||||||||||||||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||
| 176 | 143 | ▶ | if ($token->{type} eq 'SYMBOL') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 177 | { | ||||||||||||||||||||||||||||||||||||||||||
| 178 | 143 | $$pos_ref++; | |||||||||||||||||||||||||||||||||||||||||
| 179 | 143 | return $token->{value}; | |||||||||||||||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||
| 182 | 0 | die "Unexpected token type: $token->{type}"; | |||||||||||||||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||
| 186 | 4 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||
| 189 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||
| 191 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||
| 193 | Sim::Agent::Parser - Tokenizes and parses the S-expression plan into an AST | ||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||
| 195 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||
| 197 | Implements a strict S-expression parser with support for parentheses, symbols, double-quoted strings, whitespace, and semicolon comments. | ||||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||
| 199 | See L<Sim::Agent>. | ||||||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||||||
| 201 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||
| 203 | Gian Luca Brunetti (2026), gianluca.brunetti@gmail.com | ||||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||
| 205 | =head1 LICENSE | ||||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||
| 207 | The GNU General Public License v3.0 | ||||||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||||||
| 209 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||||||