| 1 |
|
| package Sidef::Parser; |
| 2 |
|
| |
| 3 |
|
| use utf8; |
| 4 |
|
| use 5.016; |
| 5 |
|
| |
| 6 |
|
| use Sidef::Types::Bool::Bool; |
| 7 |
|
| use List::Util qw(first); |
| 8 |
|
| use Scalar::Util qw(refaddr); |
| 9 |
|
| |
| 10 |
|
| # our $REGMARK; |
| 11 |
|
| |
| 12 |
|
| sub new { |
| 13 |
|
| my (undef, %opts) = @_; |
| 14 |
|
| |
| 15 |
|
| my %options = ( |
| 16 |
|
| line => 1, |
| 17 |
|
| inc => [], |
| 18 |
|
| class => 'main', # a.k.a. namespace |
| 19 |
|
| vars => {'main' => []}, |
| 20 |
|
| ref_vars_refs => {'main' => []}, |
| 21 |
|
| EOT => [], |
| 22 |
|
| |
| 23 |
|
| postfix_ops => { # postfix operators |
| 24 |
|
| '--' => 1, |
| 25 |
|
| '++' => 1, |
| 26 |
|
| '...' => 1, |
| 27 |
|
| '!' => 1, |
| 28 |
|
| '!!' => 1, |
| 29 |
|
| }, |
| 30 |
|
| |
| 31 |
|
| hyper_ops => { |
| 32 |
|
| |
| 33 |
|
| # type => [takes args, method name] |
| 34 |
|
| map => [1, 'map_operator'], |
| 35 |
|
| pam => [1, 'pam_operator'], |
| 36 |
|
| zip => [1, 'zip_operator'], |
| 37 |
|
| wise => [1, 'wise_operator'], |
| 38 |
|
| scalar => [1, 'scalar_operator'], |
| 39 |
|
| rscalar => [1, 'rscalar_operator'], |
| 40 |
|
| cross => [1, 'cross_operator'], |
| 41 |
|
| unroll => [1, 'unroll_operator'], |
| 42 |
|
| reduce => [0, 'reduce_operator'], |
| 43 |
|
| lmap => [0, 'map_operator'], |
| 44 |
|
| }, |
| 45 |
|
| |
| 46 |
|
| static_obj_re => qr{\G |
| 47 |
|
| (?: |
| 48 |
|
| nil\b (?{ state $x = bless({}, 'Sidef::Types::Nil::Nil') }) |
| 49 |
|
| | null\b (?{ state $x = Sidef::Types::Null::Null->new }) |
| 50 |
|
| | true\b (?{ Sidef::Types::Bool::Bool::TRUE }) |
| 51 |
|
| | false\b (?{ Sidef::Types::Bool::Bool::FALSE }) |
| 52 |
|
| | next\b (?{ state $x = bless({}, 'Sidef::Types::Block::Next') }) |
| 53 |
|
| | break\b (?{ state $x = bless({}, 'Sidef::Types::Block::Break') }) |
| 54 |
|
| | Block\b (?{ state $x = bless({}, 'Sidef::DataTypes::Block::Block') }) |
| 55 |
|
| | Backtick\b (?{ state $x = bless({}, 'Sidef::DataTypes::Glob::Backtick') }) |
| 56 |
|
| | ARGF\b (?{ state $x = bless({}, 'Sidef::Meta::Glob::ARGF') }) |
| 57 |
|
| | STDIN\b (?{ state $x = bless({}, 'Sidef::Meta::Glob::STDIN') }) |
| 58 |
|
| | STDOUT\b (?{ state $x = bless({}, 'Sidef::Meta::Glob::STDOUT') }) |
| 59 |
|
| | STDERR\b (?{ state $x = bless({}, 'Sidef::Meta::Glob::STDERR') }) |
| 60 |
|
| | Bool\b (?{ state $x = bless({}, 'Sidef::DataTypes::Bool::Bool') }) |
| 61 |
|
| | FileHandle\b (?{ state $x = bless({}, 'Sidef::DataTypes::Glob::FileHandle') }) |
| 62 |
|
| | DirHandle\b (?{ state $x = bless({}, 'Sidef::DataTypes::Glob::DirHandle') }) |
| 63 |
|
| | SocketHandle\b (?{ state $x = bless({}, 'Sidef::DataTypes::Glob::SocketHandle') }) |
| 64 |
|
| | Dir\b (?{ state $x = bless({}, 'Sidef::DataTypes::Glob::Dir') }) |
| 65 |
|
| | File\b (?{ state $x = bless({}, 'Sidef::DataTypes::Glob::File') }) |
| 66 |
|
| | Arr(?:ay)?+\b (?{ state $x = bless({}, 'Sidef::DataTypes::Array::Array') }) |
| 67 |
|
| | Pair\b (?{ state $x = bless({}, 'Sidef::DataTypes::Array::Pair') }) |
| 68 |
|
| | Vec(?:tor)?\b (?{ state $x = bless({}, 'Sidef::DataTypes::Array::Vector') }) |
| 69 |
|
| | Matrix\b (?{ state $x = bless({}, 'Sidef::DataTypes::Array::Matrix') }) |
| 70 |
|
| | Hash\b (?{ state $x = bless({}, 'Sidef::DataTypes::Hash::Hash') }) |
| 71 |
|
| | Set\b (?{ state $x = bless({}, 'Sidef::DataTypes::Set::Set') }) |
| 72 |
|
| | Bag\b (?{ state $x = bless({}, 'Sidef::DataTypes::Set::Bag') }) |
| 73 |
|
| | Str(?:ing)?+\b (?{ state $x = bless({}, 'Sidef::DataTypes::String::String') }) |
| 74 |
|
| | Num(?:ber)?+\b (?{ state $x = bless({}, 'Sidef::DataTypes::Number::Number') }) |
| 75 |
|
| | Mod\b (?{ state $x = bless({}, 'Sidef::DataTypes::Number::Mod') }) |
| 76 |
|
| | Gauss\b (?{ state $x = bless({}, 'Sidef::DataTypes::Number::Gauss') }) |
| 77 |
|
| | Quadratic\b (?{ state $x = bless({}, 'Sidef::DataTypes::Number::Quadratic') }) |
| 78 |
|
| | Quaternion\b (?{ state $x = bless({}, 'Sidef::DataTypes::Number::Quaternion') }) |
| 79 |
|
| | Poly(?:nomial)?\b (?{ state $x = bless({}, 'Sidef::DataTypes::Number::Polynomial') }) |
| 80 |
|
| | Poly(?:nomial)?Mod\b (?{ state $x = bless({}, 'Sidef::DataTypes::Number::PolynomialMod') }) |
| 81 |
|
| | Frac(?:tion)?\b (?{ state $x = bless({}, 'Sidef::DataTypes::Number::Fraction') }) |
| 82 |
|
| | Inf\b (?{ state $x = Sidef::Types::Number::Number->inf }) |
| 83 |
|
| | NaN\b (?{ state $x = Sidef::Types::Number::Number->nan }) |
| 84 |
|
| | Infi\b (?{ state $x = Sidef::Types::Number::Complex->new(0, Sidef::Types::Number::Number->inf) }) |
| 85 |
|
| | NaNi\b (?{ state $x = Sidef::Types::Number::Complex->new(0, Sidef::Types::Number::Number->nan) }) |
| 86 |
|
| | RangeNum(?:ber)?+\b (?{ state $x = bless({}, 'Sidef::DataTypes::Range::RangeNumber') }) |
| 87 |
|
| | RangeStr(?:ing)?+\b (?{ state $x = bless({}, 'Sidef::DataTypes::Range::RangeString') }) |
| 88 |
|
| | Range\b (?{ state $x = bless({}, 'Sidef::DataTypes::Range::Range') }) |
| 89 |
|
| | Socket\b (?{ state $x = bless({}, 'Sidef::DataTypes::Glob::Socket') }) |
| 90 |
|
| | Pipe\b (?{ state $x = bless({}, 'Sidef::DataTypes::Glob::Pipe') }) |
| 91 |
|
| | Ref\b (?{ state $x = bless({}, 'Sidef::Variable::Ref') }) |
| 92 |
|
| | NamedParam\b (?{ state $x = bless({}, 'Sidef::DataTypes::Variable::NamedParam') }) |
| 93 |
|
| | Lazy\b (?{ state $x = bless({}, 'Sidef::DataTypes::Object::Lazy') }) |
| 94 |
|
| | LazyMethod\b (?{ state $x = bless({}, 'Sidef::DataTypes::Object::LazyMethod') }) |
| 95 |
|
| | Enumerator\b (?{ state $x = bless({}, 'Sidef::DataTypes::Object::Enumerator') }) |
| 96 |
|
| | Complex\b (?{ state $x = bless({}, 'Sidef::DataTypes::Number::Complex') }) |
| 97 |
|
| | Regexp?\b (?{ state $x = bless({}, 'Sidef::DataTypes::Regex::Regex') }) |
| 98 |
|
| | Object\b (?{ state $x = bless({}, 'Sidef::DataTypes::Object::Object') }) |
| 99 |
|
| | Sidef\b (?{ state $x = bless({}, 'Sidef::DataTypes::Sidef::Sidef') }) |
| 100 |
|
| | Sig\b (?{ state $x = bless({}, 'Sidef::Sys::Sig') }) |
| 101 |
|
| | Sys\b (?{ state $x = bless({}, 'Sidef::Sys::Sys') }) |
| 102 |
|
| | Perl\b (?{ state $x = bless({}, 'Sidef::DataTypes::Perl::Perl') }) |
| 103 |
|
| | Math\b (?{ state $x = bless({}, 'Sidef::Math::Math') }) |
| 104 |
|
| | Time\b (?{ state $x = Sidef::Time::Time->new }) |
| 105 |
|
| | Date\b (?{ state $x = Sidef::Time::Date->new }) |
| 106 |
|
| | \$\. (?{ state $x = bless({name => '$.'}, 'Sidef::Variable::Magic') }) |
| 107 |
|
| | \$\? (?{ state $x = bless({name => '$?'}, 'Sidef::Variable::Magic') }) |
| 108 |
|
| | \$\$ (?{ state $x = bless({name => '$$'}, 'Sidef::Variable::Magic') }) |
| 109 |
|
| | \$\^T\b (?{ state $x = bless({name => '$^T'}, 'Sidef::Variable::Magic') }) |
| 110 |
|
| | \$\| (?{ state $x = bless({name => '$|'}, 'Sidef::Variable::Magic') }) |
| 111 |
|
| | \$! (?{ state $x = bless({name => '$!'}, 'Sidef::Variable::Magic') }) |
| 112 |
|
| | \$" (?{ state $x = bless({name => '$"'}, 'Sidef::Variable::Magic') }) |
| 113 |
|
| | \$\\ (?{ state $x = bless({name => '$\\'}, 'Sidef::Variable::Magic') }) |
| 114 |
|
| | \$@ (?{ state $x = bless({name => '$@'}, 'Sidef::Variable::Magic') }) |
| 115 |
|
| | \$% (?{ state $x = bless({name => '$%'}, 'Sidef::Variable::Magic') }) |
| 116 |
|
| | \$~ (?{ state $x = bless({name => '$~'}, 'Sidef::Variable::Magic') }) |
| 117 |
|
| | \$/ (?{ state $x = bless({name => '$/'}, 'Sidef::Variable::Magic') }) |
| 118 |
|
| | \$& (?{ state $x = bless({name => '$&'}, 'Sidef::Variable::Magic') }) |
| 119 |
|
| | \$' (?{ state $x = bless({name => '$\''}, 'Sidef::Variable::Magic') }) |
| 120 |
|
| | \$` (?{ state $x = bless({name => '$`'}, 'Sidef::Variable::Magic') }) |
| 121 |
|
| | \$: (?{ state $x = bless({name => '$:'}, 'Sidef::Variable::Magic') }) |
| 122 |
|
| | \$\] (?{ state $x = bless({name => '$]'}, 'Sidef::Variable::Magic') }) |
| 123 |
|
| | \$\[ (?{ state $x = bless({name => '$['}, 'Sidef::Variable::Magic') }) |
| 124 |
|
| | \$; (?{ state $x = bless({name => '$;'}, 'Sidef::Variable::Magic') }) |
| 125 |
|
| | \$, (?{ state $x = bless({name => '$,'}, 'Sidef::Variable::Magic') }) |
| 126 |
|
| | \$\^O\b (?{ state $x = bless({name => '$^O'}, 'Sidef::Variable::Magic') }) |
| 127 |
|
| | \$\^PERL\b (?{ state $x = bless({name => '$^X', dump => '$^PERL'}, 'Sidef::Variable::Magic') }) |
| 128 |
|
| | (?:\$0|\$\^SIDEF)\b (?{ state $x = bless({name => '$0', dump => '$^SIDEF'}, 'Sidef::Variable::Magic') }) |
| 129 |
|
| | \$\) (?{ state $x = bless({name => '$)'}, 'Sidef::Variable::Magic') }) |
| 130 |
|
| | \$\( (?{ state $x = bless({name => '$('}, 'Sidef::Variable::Magic') }) |
| 131 |
|
| | \$< (?{ state $x = bless({name => '$<'}, 'Sidef::Variable::Magic') }) |
| 132 |
|
| | \$> (?{ state $x = bless({name => '$>'}, 'Sidef::Variable::Magic') }) |
| 133 |
|
| | ∞ (?{ state $x = Sidef::Types::Number::Number->inf }) |
| 134 |
|
| ) (?!::) |
| 135 |
|
| }x, |
| 136 |
|
| prefix_obj_re => qr{\G |
| 137 |
|
| (?: |
| 138 |
|
| if\b (?{ bless({}, 'Sidef::Types::Block::If') }) |
| 139 |
|
| | with\b (?{ bless({}, 'Sidef::Types::Block::With') }) |
| 140 |
|
| | while\b (?{ bless({}, 'Sidef::Types::Block::While') }) |
| 141 |
|
| | foreach\b (?{ bless({}, 'Sidef::Types::Block::ForEach') }) |
| 142 |
|
| | for\b (?{ bless({}, 'Sidef::Types::Block::For') }) |
| 143 |
|
| | return\b (?{ state $x = bless({}, 'Sidef::Types::Block::Return') }) |
| 144 |
|
| #| next\b (?{ bless({}, 'Sidef::Types::Block::Next') }) |
| 145 |
|
| #| break\b (?{ bless({}, 'Sidef::Types::Block::Break') }) |
| 146 |
|
| | read\b (?{ state $x = Sidef::Sys::Sys->new }) |
| 147 |
|
| | goto\b (?{ state $x = bless({}, 'Sidef::Perl::Builtin') }) |
| 148 |
|
| | (?:[*\\]|\+\+|--) (?{ state $x = bless({}, 'Sidef::Variable::Ref') }) |
| 149 |
|
| | (?:>>?|\@\|?|[√+~!\-\^]| |
| 150 |
|
| (?: |
| 151 |
|
| say |
| 152 |
|
| | print |
| 153 |
|
| | defined |
| 154 |
|
| )\b) (?{ state $x = bless({}, 'Sidef::Operator::Unary') }) |
| 155 |
|
| | : (?{ state $x = bless({}, 'Sidef::Meta::PrefixColon') }) |
| 156 |
|
| ) |
| 157 |
|
| }x, |
| 158 |
|
| quote_operators_re => qr{\G |
| 159 |
|
| (?: |
| 160 |
|
| # String |
| 161 |
|
| (?: ['‘‚’] | %q\b. ) (?{ [qw(0 new Sidef::Types::String::String)] }) |
| 162 |
|
| |(?: ["“„”] | %(?:Q\b. | (?!\w). )) (?{ [qw(1 new Sidef::Types::String::String)] }) |
| 163 |
|
| |
| 164 |
|
| # File |
| 165 |
|
| | %f\b. (?{ [qw(0 new Sidef::Types::Glob::File)] }) |
| 166 |
|
| | %F\b. (?{ [qw(1 new Sidef::Types::Glob::File)] }) |
| 167 |
|
| |
| 168 |
|
| # Dir |
| 169 |
|
| | %d\b. (?{ [qw(0 new Sidef::Types::Glob::Dir)] }) |
| 170 |
|
| | %D\b. (?{ [qw(1 new Sidef::Types::Glob::Dir)] }) |
| 171 |
|
| |
| 172 |
|
| # Pipe |
| 173 |
|
| | %p\b. (?{ [qw(0 pipe Sidef::Types::Glob::Pipe)] }) |
| 174 |
|
| | %P\b. (?{ [qw(1 pipe Sidef::Types::Glob::Pipe)] }) |
| 175 |
|
| |
| 176 |
|
| # Backtick |
| 177 |
|
| | %x\b. (?{ [qw(0 new Sidef::Types::Glob::Backtick)] }) |
| 178 |
|
| | (?: %X\b. | ` ) (?{ [qw(1 new Sidef::Types::Glob::Backtick)] }) |
| 179 |
|
| |
| 180 |
|
| # Bytes |
| 181 |
|
| | %b\b. (?{ [qw(0 bytes Sidef::Types::Array::Array)] }) |
| 182 |
|
| | %B\b. (?{ [qw(1 bytes Sidef::Types::Array::Array)] }) |
| 183 |
|
| |
| 184 |
|
| # Chars |
| 185 |
|
| | %c\b. (?{ [qw(0 chars Sidef::Types::Array::Array)] }) |
| 186 |
|
| | %C\b. (?{ [qw(1 chars Sidef::Types::Array::Array)] }) |
| 187 |
|
| |
| 188 |
|
| # Graphemes |
| 189 |
|
| | %g\b. (?{ [qw(0 graphemes Sidef::Types::Array::Array)] }) |
| 190 |
|
| | %G\b. (?{ [qw(1 graphemes Sidef::Types::Array::Array)] }) |
| 191 |
|
| |
| 192 |
|
| # Symbols |
| 193 |
|
| | %[Os]\b. (?{ [qw(0 __NEW__ Sidef::Module::OO)] }) |
| 194 |
|
| | %S\b. (?{ [qw(0 __NEW__ Sidef::Module::Func)] }) |
| 195 |
|
| |
| 196 |
|
| # Arbitrary Perl code |
| 197 |
|
| | %perl\b. (?{ [qw(0 new Sidef::Types::Perl::Perl)] }) |
| 198 |
|
| | %Perl\b. (?{ [qw(1 new Sidef::Types::Perl::Perl)] }) |
| 199 |
|
| ) |
| 200 |
|
| }xs, |
| 201 |
|
| built_in_classes => { |
| 202 |
|
| map { $_ => 1 } |
| 203 |
|
| qw( |
| 204 |
|
| File |
| 205 |
|
| FileHandle |
| 206 |
|
| Dir |
| 207 |
|
| DirHandle |
| 208 |
|
| Arr Array |
| 209 |
|
| Pair |
| 210 |
|
| Vec Vector |
| 211 |
|
| Matrix |
| 212 |
|
| Enumerator |
| 213 |
|
| Hash |
| 214 |
|
| Set |
| 215 |
|
| Bag |
| 216 |
|
| Str String |
| 217 |
|
| Num Number |
| 218 |
|
| Poly Polynomial |
| 219 |
|
| PolyMod PolynomialMod |
| 220 |
|
| Frac Fraction |
| 221 |
|
| Mod |
| 222 |
|
| Gauss |
| 223 |
|
| Quadratic |
| 224 |
|
| Quaternion |
| 225 |
|
| Range |
| 226 |
|
| RangeStr RangeString |
| 227 |
|
| RangeNum RangeNumber |
| 228 |
|
| Complex |
| 229 |
|
| Math |
| 230 |
|
| Pipe |
| 231 |
|
| Ref |
| 232 |
|
| Socket |
| 233 |
|
| SocketHandle |
| 234 |
|
| Bool |
| 235 |
|
| Sys |
| 236 |
|
| Sig |
| 237 |
|
| Regex Regexp |
| 238 |
|
| Time |
| 239 |
|
| Date |
| 240 |
|
| Perl |
| 241 |
|
| Sidef |
| 242 |
|
| Object |
| 243 |
|
| Parser |
| 244 |
|
| Block |
| 245 |
|
| Backtick |
| 246 |
|
| Lazy |
| 247 |
|
| LazyMethod |
| 248 |
|
| NamedParam |
| 249 |
|
| |
| 250 |
|
| true false |
| 251 |
|
| nil null |
| 252 |
|
| ) |
| 253 |
|
| }, |
| 254 |
|
| keywords => { |
| 255 |
|
| map { $_ => 1 } |
| 256 |
|
| qw( |
| 257 |
|
| next |
| 258 |
|
| break |
| 259 |
|
| return |
| 260 |
|
| for foreach |
| 261 |
|
| if elsif else |
| 262 |
|
| with orwith |
| 263 |
|
| while |
| 264 |
|
| given |
| 265 |
|
| with |
| 266 |
|
| continue |
| 267 |
|
| import |
| 268 |
|
| include |
| 269 |
|
| eval |
| 270 |
|
| read |
| 271 |
|
| die |
| 272 |
|
| warn |
| 273 |
|
| |
| 274 |
|
| assert |
| 275 |
|
| assert_eq |
| 276 |
|
| assert_ne |
| 277 |
|
| |
| 278 |
|
| local |
| 279 |
|
| global |
| 280 |
|
| var |
| 281 |
|
| del |
| 282 |
|
| const |
| 283 |
|
| func |
| 284 |
|
| enum |
| 285 |
|
| class |
| 286 |
|
| static |
| 287 |
|
| define |
| 288 |
|
| struct |
| 289 |
|
| subset |
| 290 |
|
| module |
| 291 |
|
| |
| 292 |
|
| DATA |
| 293 |
|
| ARGV |
| 294 |
|
| ARGF |
| 295 |
|
| ENV |
| 296 |
|
| |
| 297 |
|
| STDIN |
| 298 |
|
| STDOUT |
| 299 |
|
| STDERR |
| 300 |
|
| |
| 301 |
|
| __FILE__ |
| 302 |
|
| __LINE__ |
| 303 |
|
| __END__ |
| 304 |
|
| __DATA__ |
| 305 |
|
| __TIME__ |
| 306 |
|
| __DATE__ |
| 307 |
|
| __NAMESPACE__ |
| 308 |
|
| __COMPILED__ |
| 309 |
|
| __OPTIMIZED__ |
| 310 |
|
| ) |
| 311 |
|
| }, |
| 312 |
|
| match_flags_re => qr{[msixpogcaludn]+}, |
| 313 |
|
| var_name_re => qr/[^\W\d]\w*+(?>::[^\W\d]\w*)*/, |
| 314 |
|
| method_name_re => qr/[^\W\d]\w*+!?/, |
| 315 |
|
| var_init_sep_re => qr/\G\h*(?:=>|[=:])\h*/, |
| 316 |
|
| operators_re => do { |
| 317 |
|
| local $" = q{|}; |
| 318 |
|
| |
| 319 |
|
| # Longest prefix first |
| 320 |
|
| my @operators = map { quotemeta } qw( |
| 321 |
|
| |
| 322 |
|
| ||= || |
| 323 |
|
| &&= && |
| 324 |
|
| |
| 325 |
|
| ^.. ..^ |
| 326 |
|
| |
| 327 |
|
| %% ≅ |
| 328 |
|
| ~~ !~ |
| 329 |
|
| <~> |
| 330 |
|
| <=> =~= |
| 331 |
|
| <<= >>= |
| 332 |
|
| << >> |
| 333 |
|
| |>> |> |X> |Z> |
| 334 |
|
| |= | |
| 335 |
|
| &= & |
| 336 |
|
| == =~ |
| 337 |
|
| := = |
| 338 |
|
| <= >= < > |
| 339 |
|
| ++ -- |
| 340 |
|
| += + |
| 341 |
|
| -= - |
| 342 |
|
| //= // |
| 343 |
|
| /= / ÷= ÷ |
| 344 |
|
| **= ** |
| 345 |
|
| %= % |
| 346 |
|
| ^= ^ |
| 347 |
|
| *= * |
| 348 |
|
| ... |
| 349 |
|
| != .. |
| 350 |
|
| \\\\= \\\\ |
| 351 |
|
| !! ! |
| 352 |
|
| : : ⫶ |
| 353 |
|
| « » ~ |
| 354 |
|
| ); |
| 355 |
|
| |
| 356 |
|
| qr{ |
| 357 |
|
| (?(DEFINE) |
| 358 |
|
| (?<ops> |
| 359 |
|
| @operators |
| 360 |
|
| | \p{Block: Mathematical_Operators} |
| 361 |
|
| | \p{Block: Supplemental_Mathematical_Operators} |
| 362 |
|
| ) |
| 363 |
|
| ) |
| 364 |
|
| |
| 365 |
|
| »(?<unroll>[^\W\d]\w*+|(?&ops))« # unroll operator (e.g.: »add« or »+«) |
| 366 |
|
| | >>(?<unroll>[^\W\d]\w*+|(?&ops))<< # unroll operator (e.g.: >>add<< or >>+<<) |
| 367 |
|
| |
| 368 |
|
| | ~X(?<cross>[^\W\d]\w*+|(?&ops)|) # cross operator (e.g.: ~X or ~X+) |
| 369 |
|
| | ~Z(?<zip>[^\W\d]\w*+|(?&ops)|) # zip operator (e.g.: ~Z or ~Z+) |
| 370 |
|
| | ~W(?<wise>[^\W\d]\w*+|(?&ops)|) # wise operator (e.g.: ~W or ~W+) |
| 371 |
|
| | ~S(?<scalar>[^\W\d]\w*+|(?&ops)|) # scalar operator (e.g.: ~S or ~S+) |
| 372 |
|
| | ~RS(?<rscalar>[^\W\d]\w*+|(?&ops)|) # reverse scalar operator (e.g.: ~RS or ~RS/) |
| 373 |
|
| |
| 374 |
|
| | »(?<map>[^\W\d]\w*+|(?&ops))» # mapping operator (e.g.: »add» or »+») |
| 375 |
|
| | >>(?<map>[^\W\d]\w*+|(?&ops))>> # mapping operator (e.g.: >>add>> or >>+>>) |
| 376 |
|
| |
| 377 |
|
| | «(?<pam>[^\W\d]\w*+|(?&ops))« # reverse mapping operator (e.g.: «add« or «+«) |
| 378 |
|
| | <<(?<pam>[^\W\d]\w*+|(?&ops))<< # reverse mapping operator (e.g.: <<add<< or <<+<<) |
| 379 |
|
| |
| 380 |
|
| | »(?<lmap>[^\W\d]\w*+|(?&ops))\(\)» # mapping operator (e.g.: »add()» or »+()») |
| 381 |
|
| | >>(?<lmap>[^\W\d]\w*+|(?&ops))\(\)>> # mapping operator (e.g.: >>add()>> or >>+()>>) |
| 382 |
|
| |
| 383 |
|
| | <<(?<reduce>[^\W\d]\w*+|(?&ops))>> # reduce operator (e.g.: <<add>> or <<+>>) |
| 384 |
|
| | «(?<reduce>[^\W\d]\w*+|(?&ops))» # reduce operator (e.g.: «add» or «+») |
| 385 |
|
| |
| 386 |
|
| | `(?<op>[^\W\d]\w*+!?)` # method-like operator (e.g.: `add` or `add!`) |
| 387 |
|
| | (?<op>(?&ops)) # primitive operator (e.g.: +, -, *, /) |
| 388 |
|
| }x; |
| 389 |
|
| }, |
| 390 |
|
| |
| 391 |
|
| # Reference: https://en.wikipedia.org/wiki/International_variation_in_quotation_marks |
| 392 |
|
| delim_pairs => { |
| 393 |
|
| qw~ |
| 394 |
|
| ( ) [ ] { } < > |
| 395 |
|
| « » » « ‹ › › ‹ |
| 396 |
|
| „ ” “ ” ‘ ’ ‚ ’ |
| 397 |
|
| 〈 〉 ﴾ ﴿ 〈 〉 《 》 |
| 398 |
|
| 「 」 『 』 【 】 〔 〕 |
| 399 |
|
| 〖 〗 〘 〙 〚 〛 ⸨ ⸩ |
| 400 |
|
| ⌈ ⌉ ⌊ ⌋ 〈 〉 ❨ ❩ |
| 401 |
|
| ❪ ❫ ❬ ❭ ❮ ❯ ❰ ❱ |
| 402 |
|
| ❲ ❳ ❴ ❵ ⟅ ⟆ ⟦ ⟧ |
| 403 |
|
| ⟨ ⟩ ⟪ ⟫ ⟬ ⟭ ⟮ ⟯ |
| 404 |
|
| ⦃ ⦄ ⦅ ⦆ ⦇ ⦈ ⦉ ⦊ |
| 405 |
|
| ⦋ ⦌ ⦍ ⦎ ⦏ ⦐ ⦑ ⦒ |
| 406 |
|
| ⦗ ⦘ ⧘ ⧙ ⧚ ⧛ ⧼ ⧽ |
| 407 |
|
| ~ |
| 408 |
|
| }, |
| 409 |
|
| %opts, |
| 410 |
|
| ); |
| 411 |
|
| |
| 412 |
|
| $options{ref_vars} = $options{vars}; |
| 413 |
|
| $options{file_name} //= '-'; |
| 414 |
|
| $options{script_name} //= '-'; |
| 415 |
|
| |
| 416 |
|
| bless \%options, __PACKAGE__; |
| 417 |
|
| } |
| 418 |
|
| |
| 419 |
|
| sub fatal_error { |
| 420 |
|
| my ($self, %opt) = @_; |
| 421 |
|
| |
| 422 |
|
| my $start = rindex($opt{code}, "\n", $opt{pos}) + 1; |
| 423 |
|
| my $point = $opt{pos} - $start; |
| 424 |
|
| my $line = $opt{line} // $self->{line}; |
| 425 |
|
| my $column = $point; |
| 426 |
|
| |
| 427 |
|
| my $error_line = (split(/\R/, substr($opt{code}, $start, $point + 80)))[0]; |
| 428 |
|
| |
| 429 |
|
| if (length($error_line) > 80 && $point > 60) { |
| 430 |
|
| my $from = $point - 40; |
| 431 |
|
| my $rem = $point + 40 - length($error_line); |
| 432 |
|
| |
| 433 |
|
| $from -= $rem; |
| 434 |
|
| $point = 40 + $rem; |
| 435 |
|
| |
| 436 |
|
| $error_line = substr($error_line, $from, 80); |
| 437 |
|
| } |
| 438 |
|
| |
| 439 |
|
| my @lines = ( |
| 440 |
|
| |
| 441 |
|
| "Your code forgot how to 'human.' Please give it some structure!", |
| 442 |
|
| "Were you trying to summon Cthulhu, or is this just a typo?", |
| 443 |
|
| "This line of code is in denial. It doesn't want to cooperate.", |
| 444 |
|
| "Parser has encountered a disturbance in the Force. It appears your code has gone to the dark side.", |
| 445 |
|
| "Code drunk. Fix before it starts ordering random APIs online.", |
| 446 |
|
| "Did you mean to do that? Because your code just broke the Matrix.", |
| 447 |
|
| "Your syntax seems to have taken a coffee break.", |
| 448 |
|
| "Your code is trying to summon Skynet. Abort while you can!", |
| 449 |
|
| "This isn’t a Choose Your Own Adventure. Stick to the script!", |
| 450 |
|
| "Well, that’s not going to work. Try again, friend.", |
| 451 |
|
| |
| 452 |
|
| "If this is what you meant, we need to have a talk.", |
| 453 |
|
| "Uh-oh! Looks like your code slipped on a banana peel.", |
| 454 |
|
| "Your code is having an existential crisis. It doesn’t know who it is anymore.", |
| 455 |
|
| "Something’s wrong… but hey, at least you tried!", |
| 456 |
|
| "The code didn’t like that. In fact, it’s offended.", |
| 457 |
|
| "You’re one typo away from greatness. Unfortunately, this isn’t it.", |
| 458 |
|
| "Well, that’s awkward. Let’s pretend that didn’t happen.", |
| 459 |
|
| "Your code needs more coffee. Or maybe you do.", |
| 460 |
|
| |
| 461 |
|
| "I have no idea what I just read.", |
| 462 |
|
| "Oops! The parser is confused and needs a moment to reboot its brain.", |
| 463 |
|
| "The code is speaking in riddles. Try again in plain text.", |
| 464 |
|
| "Parser ran out of breadcrumbs to follow your logic trail.", |
| 465 |
|
| "It’s like trying to read a book with missing pages.", |
| 466 |
|
| "The code went that way… and then it didn’t.", |
| 467 |
|
| "Parser gave up. It’s currently hiding under the desk.", |
| 468 |
|
| "It’s not you, it’s… okay, it’s definitely you.", |
| 469 |
|
| "Parser needs a map and a flashlight to navigate this code.", |
| 470 |
|
| "Parser has encountered a wild puzzle. It refuses to continue without a cheat sheet.", |
| 471 |
|
| |
| 472 |
|
| "Your code just sent me on a wild goose chase. No geese found.", |
| 473 |
|
| "The parser tried to follow the logic, but it got lost in the spaghetti code.", |
| 474 |
|
| "Your code just opened a wormhole. Destination: unknown.", |
| 475 |
|
| "I’ve parsed Shakespeare, but this? This is next level.", |
| 476 |
|
| "The parser tripped over a missing semicolon and is now in the fetal position.", |
| 477 |
|
| "This code smells… like it’s on fire. Someone grab an extinguisher!", |
| 478 |
|
| "The parser and the code had a staring contest. The parser blinked first.", |
| 479 |
|
| "Parser has lost the plot. Please send a clearer script.", |
| 480 |
|
| "The code just threw a plot twist. Even the parser didn’t see that coming.", |
| 481 |
|
| "It tried to make sense of your code, then rage quit.", |
| 482 |
|
| |
| 483 |
|
| "The code ventured into the unknown… and didn’t leave a map.", |
| 484 |
|
| "The parser tried to keep up, but your code is like a soap opera—too many plot twists.", |
| 485 |
|
| "Your code is speaking Parseltongue. I need an interpreter.", |
| 486 |
|
| "The parser has encountered an enigma wrapped in a mystery, coded in confusion.", |
| 487 |
|
| "The parser felt a great disturbance in the code, as if thousands of bugs cried out in terror.", |
| 488 |
|
| "The code just ghosted me. No clues, no closure, nothing.", |
| 489 |
|
| "Parser tangled in a logic loop. Send help. Or snacks.", |
| 490 |
|
| "The code is playing hide and seek. The parser isn’t winning.", |
| 491 |
|
| "The parser tried reasoning with the code, but it’s ignoring all logic.", |
| 492 |
|
| "Parser has encountered Schrödinger’s code. It’s both broken and not broken, but we won’t know until you fix it.", |
| 493 |
|
| ); |
| 494 |
|
| |
| 495 |
|
| my $error = sprintf("%s\n\nFile : %s\nLine : %s : %s\nError: %s\n\n" . ("~" x 80) . "\n%s\n", |
| 496 |
|
| $lines[rand @lines], |
| 497 |
|
| $self->{file_name} // '-', |
| 498 |
|
| $line, $column, join(', ', grep { defined } $opt{error}, $opt{reason}), $error_line); |
| 499 |
|
| |
| 500 |
|
| $error .= ' ' x ($point) . '^' . "\n" . ('~' x 80) . "\n"; |
| 501 |
|
| |
| 502 |
|
| if (exists($opt{var})) { |
| 503 |
|
| |
| 504 |
|
| my ($name, $class) = $self->get_name_and_class($opt{var}); |
| 505 |
|
| |
| 506 |
|
| my %seen; |
| 507 |
|
| my @names; |
| 508 |
|
| foreach my $var (@{$self->{vars}{$class}}) { |
| 509 |
|
| next if ref $var eq 'ARRAY'; |
| 510 |
|
| if (!$seen{$var->{name}}++) { |
| 511 |
|
| push @names, $var->{name}; |
| 512 |
|
| } |
| 513 |
|
| } |
| 514 |
|
| |
| 515 |
|
| foreach my $var (@{$self->{ref_vars_refs}{$class}}) { |
| 516 |
|
| next if ref $var eq 'ARRAY'; |
| 517 |
|
| if (!$seen{$var->{name}}++) { |
| 518 |
|
| push @names, $var->{name}; |
| 519 |
|
| } |
| 520 |
|
| } |
| 521 |
|
| |
| 522 |
|
| if ($class eq 'main') { |
| 523 |
|
| $class = ''; |
| 524 |
|
| } |
| 525 |
|
| else { |
| 526 |
|
| $class .= '::'; |
| 527 |
|
| } |
| 528 |
|
| |
| 529 |
|
| if (my @candidates = Sidef::best_matches($name, [grep { $_ ne $name } @names])) { |
| 530 |
|
| $error .= ("[?] Did you mean: " . join("\n" . (' ' x 18), map { $class . $_ } sort(@candidates)) . "\n"); |
| 531 |
|
| } |
| 532 |
|
| } |
| 533 |
|
| |
| 534 |
|
| die $error; |
| 535 |
|
| } |
| 536 |
|
| |
| 537 |
|
| sub find_var { |
| 538 |
|
| my ($self, $var_name, $class) = @_; |
| 539 |
|
| |
| 540 |
|
| foreach my $var (@{$self->{vars}{$class}}) { |
| 541 |
|
| next if ref $var eq 'ARRAY'; |
| 542 |
|
| if ($var->{name} eq $var_name) { |
| 543 |
|
| return (wantarray ? ($var, 1) : $var); |
| 544 |
|
| } |
| 545 |
|
| } |
| 546 |
|
| |
| 547 |
|
| foreach my $var (@{$self->{ref_vars_refs}{$class}}) { |
| 548 |
|
| next if ref $var eq 'ARRAY'; |
| 549 |
|
| if ($var->{name} eq $var_name) { |
| 550 |
|
| return (wantarray ? ($var, 0) : $var); |
| 551 |
|
| } |
| 552 |
|
| } |
| 553 |
|
| |
| 554 |
|
| return; |
| 555 |
|
| } |
| 556 |
|
| |
| 557 |
|
| sub check_declarations { |
| 558 |
|
| my ($self, $hash_ref) = @_; |
| 559 |
|
| |
| 560 |
|
| foreach my $class (grep { $_ eq 'main' } keys %{$hash_ref}) { |
| 561 |
|
| |
| 562 |
|
| my $array_ref = $hash_ref->{$class}; |
| 563 |
|
| |
| 564 |
|
| foreach my $variable (@{$array_ref}) { |
| 565 |
|
| if (ref $variable eq 'ARRAY') { |
| 566 |
|
| $self->check_declarations({$class => $variable}); |
| 567 |
|
| } |
| 568 |
|
| elsif ($self->{interactive} or $self->{eval_mode}) { |
| 569 |
|
| ## Everything is OK in interactive mode |
| 570 |
|
| } |
| 571 |
|
| elsif ( $variable->{count} == 0 |
| 572 |
|
| && $variable->{type} ne 'class' |
| 573 |
|
| && $variable->{type} ne 'func' |
| 574 |
|
| && $variable->{type} ne 'method' |
| 575 |
|
| && $variable->{type} ne 'global' |
| 576 |
|
| && $variable->{name} ne 'self' |
| 577 |
|
| && $variable->{name} ne '' |
| 578 |
|
| && $variable->{type} ne 'del' |
| 579 |
|
| && chr(ord $variable->{name}) ne '_') { |
| 580 |
|
| |
| 581 |
|
| warn '[WARNING] ' |
| 582 |
|
| . "$variable->{type} '$variable->{name}' has been declared, but not used again at " |
| 583 |
|
| . "$self->{file_name} line $variable->{line}\n"; |
| 584 |
|
| } |
| 585 |
|
| } |
| 586 |
|
| } |
| 587 |
|
| } |
| 588 |
|
| |
| 589 |
|
| sub get_name_and_class { |
| 590 |
|
| my ($self, $var_name) = @_; |
| 591 |
|
| |
| 592 |
|
| $var_name // return ('', $self->{class}); |
| 593 |
|
| |
| 594 |
|
| my $rindex = rindex($var_name, '::'); |
| 595 |
|
| $rindex != -1 |
| 596 |
|
| ? (substr($var_name, $rindex + 2), substr($var_name, 0, $rindex)) |
| 597 |
|
| : ($var_name, $self->{class}); |
| 598 |
|
| } |
| 599 |
|
| |
| 600 |
|
| sub get_quoted_words { |
| 601 |
|
| my ($self, %opt) = @_; |
| 602 |
|
| |
| 603 |
|
| my $string = $self->get_quoted_string(code => $opt{code}, no_count_line => 1); |
| 604 |
|
| $self->parse_whitespace(code => \$string); |
| 605 |
|
| |
| 606 |
|
| my @words; |
| 607 |
|
| while ($string =~ /\G((?>[^\s\\]+|\\.)++)/gcs) { |
| 608 |
|
| push @words, $1 =~ s{\\#}{#}gr; |
| 609 |
|
| $self->parse_whitespace(code => \$string); |
| 610 |
|
| } |
| 611 |
|
| |
| 612 |
|
| return \@words; |
| 613 |
|
| } |
| 614 |
|
| |
| 615 |
|
| sub get_quoted_string { |
| 616 |
|
| my ($self, %opt) = @_; |
| 617 |
|
| |
| 618 |
|
| local *_ = $opt{code}; |
| 619 |
|
| |
| 620 |
|
| /\G(?=\s)/ && $self->parse_whitespace(code => $opt{code}); |
| 621 |
|
| |
| 622 |
|
| my $delim; |
| 623 |
|
| if (/\G(?=(.))/) { |
| 624 |
|
| $delim = $1; |
| 625 |
|
| if ($delim eq '\\' && /\G\\(.*?)\\/gsc) { |
| 626 |
|
| return $1; |
| 627 |
|
| } |
| 628 |
|
| } |
| 629 |
|
| else { |
| 630 |
|
| $self->fatal_error( |
| 631 |
|
| error => qq{can't find the beginning of a string quote delimiter}, |
| 632 |
|
| code => $_, |
| 633 |
|
| pos => pos($_), |
| 634 |
|
| ); |
| 635 |
|
| } |
| 636 |
|
| |
| 637 |
|
| my $orig_pos = pos($_); |
| 638 |
|
| my $beg_delim = quotemeta $delim; |
| 639 |
|
| my $pair_delim = exists($self->{delim_pairs}{$delim}) ? $self->{delim_pairs}{$delim} : (); |
| 640 |
|
| |
| 641 |
|
| my $string = ''; |
| 642 |
|
| if (defined $pair_delim) { |
| 643 |
|
| |
| 644 |
|
| my $end_delim = quotemeta $pair_delim; |
| 645 |
|
| my $re_delim = $beg_delim . $end_delim; |
| 646 |
|
| |
| 647 |
|
| # if (m{\G(?<main>$beg_delim((?>[^$re_delim\\]+|\\.|(?&main))*+)$end_delim)}sgc) { |
| 648 |
|
| # if (m{\G(?<main>$beg_delim((?>[^\\$re_delim]*+(?>\\.[^\\$re_delim]*|(?&main)){0,}){0,})(?:$end_delim(*ACCEPT:term))?)(*PRUNE)(*FAIL)}sgc) { |
| 649 |
|
| |
| 650 |
|
| if (m{\G(?<main>$beg_delim((?>[^\\$re_delim]*+(?>\\.[^\\$re_delim]*|(?&main)){0,}){0,})$end_delim)}sgc) { |
| 651 |
|
| $string = $2 =~ s/\\([$re_delim])/$1/gr; |
| 652 |
|
| } |
| 653 |
|
| } |
| 654 |
|
| |
| 655 |
|
| # elsif (m{\G$beg_delim([^\\$beg_delim]*+(?>\\.[^\\$beg_delim]*)*)}sgc) { # limited to 2^15-1 escapes |
| 656 |
|
| # elsif (m{\G$beg_delim((?>(?>[^$beg_delim\\]++|\\.){0,}+){0,}+)}sgc) { |
| 657 |
|
| # elsif (m{\G$beg_delim((?>[^\\$beg_delim]*+(?>\\.[^\\$beg_delim]*){0,}){0,})(?:$beg_delim(*ACCEPT:term))?(*PRUNE)(*FAIL)}sgc) { |
| 658 |
|
| |
| 659 |
|
| elsif (m{\G$beg_delim((?>[^\\$beg_delim]*+(?>\\.[^\\$beg_delim]*){0,}){0,})}sgc) { |
| 660 |
|
| $string = $1 =~ s/\\([$beg_delim])/$1/gr; |
| 661 |
|
| } |
| 662 |
|
| |
| 663 |
|
| # $REGMARK eq 'term' |
| 664 |
|
| (defined($pair_delim) ? /\G(?<=\Q$pair_delim\E)/ : /\G$beg_delim/gc) |
| 665 |
|
| || $self->fatal_error( |
| 666 |
|
| error => sprintf(qq{can't find the quoted string terminator <<%s>>}, $pair_delim // $delim), |
| 667 |
|
| code => $_, |
| 668 |
|
| pos => $orig_pos, |
| 669 |
|
| ); |
| 670 |
|
| |
| 671 |
|
| $self->{line} += $string =~ s/\R\K//g if not $opt{no_count_line}; |
| 672 |
|
| return $string; |
| 673 |
|
| } |
| 674 |
|
| |
| 675 |
|
| ## get_method_name() returns the following values: |
| 676 |
|
| # 1st: method/operator (or undef) |
| 677 |
|
| # 2nd: a true value if the operator requires an argument |
| 678 |
|
| # 3rd: type of operator (defined in $self->{hyper_ops}) |
| 679 |
|
| sub get_method_name { |
| 680 |
|
| my ($self, %opt) = @_; |
| 681 |
|
| |
| 682 |
|
| local *_ = $opt{code}; |
| 683 |
|
| |
| 684 |
|
| # Parse whitespace |
| 685 |
|
| $self->parse_whitespace(code => $opt{code}); |
| 686 |
|
| |
| 687 |
|
| # Alpha-numeric method name |
| 688 |
|
| if (/\G((?:SUPER::)*$self->{method_name_re})/goc) { |
| 689 |
|
| return ($1, 0, ''); |
| 690 |
|
| } |
| 691 |
|
| |
| 692 |
|
| # Super-script power |
| 693 |
|
| if (/\G(?=[⁰¹²³⁴⁵⁶⁷⁸⁹])/) { |
| 694 |
|
| return ('**', 1, 'op'); |
| 695 |
|
| } |
| 696 |
|
| |
| 697 |
|
| # Operator-like method name |
| 698 |
|
| if (m{\G$self->{operators_re}}goc) { |
| 699 |
|
| my ($key) = keys(%+); |
| 700 |
|
| return ( |
| 701 |
|
| $+, |
| 702 |
|
| ( |
| 703 |
|
| exists($self->{hyper_ops}{$key}) |
| 704 |
|
| ? $self->{hyper_ops}{$key}[0] |
| 705 |
|
| : not(exists $self->{postfix_ops}{$+}) |
| 706 |
|
| ), |
| 707 |
|
| $key |
| 708 |
|
| ); |
| 709 |
|
| } |
| 710 |
|
| |
| 711 |
|
| # Method name as expression |
| 712 |
|
| my ($obj) = $self->parse_expr(code => $opt{code}); |
| 713 |
|
| return ({self => $obj // return}, 0, ''); |
| 714 |
|
| } |
| 715 |
|
| |
| 716 |
|
| sub parse_delim { |
| 717 |
|
| my ($self, %opt) = @_; |
| 718 |
|
| |
| 719 |
|
| local *_ = $opt{code}; |
| 720 |
|
| |
| 721 |
|
| my @delims = ('|', keys(%{$self->{delim_pairs}})); |
| 722 |
|
| if (exists $opt{ignore_delim}) { |
| 723 |
|
| @delims = grep { not exists $opt{ignore_delim}{$_} } @delims; |
| 724 |
|
| } |
| 725 |
|
| |
| 726 |
|
| my $regex = do { |
| 727 |
|
| local $" = ""; |
| 728 |
|
| qr/\G([@delims])\h*/; |
| 729 |
|
| }; |
| 730 |
|
| |
| 731 |
|
| my $end_delim; |
| 732 |
|
| if (/$regex/gc) { |
| 733 |
|
| $end_delim = $self->{delim_pairs}{$1} // $1; |
| 734 |
|
| $self->parse_whitespace(code => $opt{code}); |
| 735 |
|
| } |
| 736 |
|
| |
| 737 |
|
| return $end_delim; |
| 738 |
|
| } |
| 739 |
|
| |
| 740 |
|
| sub get_init_vars { |
| 741 |
|
| my ($self, %opt) = @_; |
| 742 |
|
| |
| 743 |
|
| local *_ = $opt{code}; |
| 744 |
|
| |
| 745 |
|
| my $end_delim = $self->parse_delim(%opt); |
| 746 |
|
| |
| 747 |
|
| my @vars; |
| 748 |
|
| my %classes; |
| 749 |
|
| |
| 750 |
|
| while ( /\G(?<type>$self->{var_name_re}\h+$self->{var_name_re})\h*/goc |
| 751 |
|
| || /\G([*:]?$self->{var_name_re})\h*/goc |
| 752 |
|
| || (defined($end_delim) && /\G(?=[({])/)) { |
| 753 |
|
| |
| 754 |
|
| my $declaration = $1; |
| 755 |
|
| |
| 756 |
|
| if ($opt{with_vals} && defined($end_delim)) { |
| 757 |
|
| |
| 758 |
|
| # Add the variable into the symbol table |
| 759 |
|
| if (defined $declaration) { |
| 760 |
|
| |
| 761 |
|
| my ($name, $class_name) = $self->get_name_and_class((split(' ', $declaration))[-1]); |
| 762 |
|
| |
| 763 |
|
| undef $classes{$class_name}; |
| 764 |
|
| unshift @{$self->{vars}{$class_name}}, |
| 765 |
|
| { |
| 766 |
|
| obj => '', |
| 767 |
|
| name => $name, |
| 768 |
|
| count => 0, |
| 769 |
|
| type => $opt{type}, |
| 770 |
|
| line => $self->{line}, |
| 771 |
|
| }; |
| 772 |
|
| } |
| 773 |
|
| |
| 774 |
|
| if (/\G<<?\h*/gc) { |
| 775 |
|
| my ($var) = /\G($self->{var_name_re})\h*/goc; |
| 776 |
|
| $var // $self->fatal_error( |
| 777 |
|
| code => $_, |
| 778 |
|
| pos => pos($_), |
| 779 |
|
| error => 'expected a subset name', |
| 780 |
|
| ); |
| 781 |
|
| $declaration .= " < $var "; |
| 782 |
|
| } |
| 783 |
|
| |
| 784 |
|
| if (/\G(?=\{)/) { |
| 785 |
|
| my $pos = pos($_); |
| 786 |
|
| $self->parse_block(code => $opt{code}, topic_var => 1); |
| 787 |
|
| $declaration .= substr($_, $pos, pos($_) - $pos); |
| 788 |
|
| } |
| 789 |
|
| elsif (/\G(?=\()/) { |
| 790 |
|
| my $pos = pos($_); |
| 791 |
|
| $self->parse_arg(code => $opt{code}); |
| 792 |
|
| $declaration .= substr($_, $pos, pos($_) - $pos); |
| 793 |
|
| } |
| 794 |
|
| |
| 795 |
|
| if (/$self->{var_init_sep_re}/goc) { |
| 796 |
|
| my $pos = pos($_); |
| 797 |
|
| $self->parse_obj(code => $opt{code}, multiline => 1); |
| 798 |
|
| $declaration .= '=' . substr($_, $pos, pos($_) - $pos); |
| 799 |
|
| } |
| 800 |
|
| } |
| 801 |
|
| |
| 802 |
|
| push @vars, $declaration; |
| 803 |
|
| |
| 804 |
|
| (defined($end_delim) && (/\G\h*,\h*/gc || /\G\h*(?:#.*)?+(?=\R)/gc)) || last; |
| 805 |
|
| |
| 806 |
|
| $self->parse_whitespace(code => $opt{code}); |
| 807 |
|
| } |
| 808 |
|
| |
| 809 |
|
| # Remove the newly added variables |
| 810 |
|
| foreach my $class_name (keys %classes) { |
| 811 |
|
| for (my $i = 0 ; $i <= $#{$self->{vars}{$class_name}} ; $i++) { |
| 812 |
|
| if (ref($self->{vars}{$class_name}[$i]) eq 'HASH' and not ref($self->{vars}{$class_name}[$i]{obj})) { |
| 813 |
|
| splice(@{$self->{vars}{$class_name}}, $i--, 1); |
| 814 |
|
| } |
| 815 |
|
| } |
| 816 |
|
| } |
| 817 |
|
| |
| 818 |
|
| $self->parse_whitespace(code => $opt{code}); |
| 819 |
|
| |
| 820 |
|
| defined($end_delim) |
| 821 |
|
| && ( |
| 822 |
|
| /\G\h*\Q$end_delim\E/gc |
| 823 |
|
| || $self->fatal_error( |
| 824 |
|
| code => $_, |
| 825 |
|
| pos => pos($_), |
| 826 |
|
| error => "can't find the closing delimiter: `$end_delim`", |
| 827 |
|
| ) |
| 828 |
|
| ); |
| 829 |
|
| |
| 830 |
|
| return \@vars; |
| 831 |
|
| } |
| 832 |
|
| |
| 833 |
|
| sub parse_init_vars { |
| 834 |
|
| my ($self, %opt) = @_; |
| 835 |
|
| |
| 836 |
|
| local *_ = $opt{code}; |
| 837 |
|
| |
| 838 |
|
| my $end_delim = $self->parse_delim(%opt); |
| 839 |
|
| |
| 840 |
|
| my @var_objs; |
| 841 |
|
| while ( /\G(?<type>$self->{var_name_re})\h+($self->{var_name_re})\h*/goc |
| 842 |
|
| || /\G([*:]?)($self->{var_name_re})\h*/goc |
| 843 |
|
| || (defined($end_delim) && /\G(?=[({])/)) { |
| 844 |
|
| my ($attr, $name) = ($1, $2); |
| 845 |
|
| |
| 846 |
|
| my $ref_type; |
| 847 |
|
| if (defined($+{type})) { |
| 848 |
|
| |
| 849 |
|
| my $type = $+{type}; |
| 850 |
|
| my $obj = $self->parse_expr(code => \$type); |
| 851 |
|
| |
| 852 |
|
| if (not defined($obj) or ref($obj) eq 'HASH') { |
| 853 |
|
| $self->fatal_error( |
| 854 |
|
| code => $_, |
| 855 |
|
| pos => pos($_), |
| 856 |
|
| error => "invalid type <<$type>> for variable `$name`", |
| 857 |
|
| reason => "expected a type, such as: Str, Num, File, etc...", |
| 858 |
|
| ); |
| 859 |
|
| } |
| 860 |
|
| |
| 861 |
|
| $ref_type = $obj; |
| 862 |
|
| } |
| 863 |
|
| |
| 864 |
|
| my ($subset); |
| 865 |
|
| if (ref($ref_type) eq 'Sidef::Variable::Subset') { |
| 866 |
|
| $subset = $ref_type; |
| 867 |
|
| undef $ref_type; |
| 868 |
|
| } |
| 869 |
|
| |
| 870 |
|
| my ($var_name, $class_name) = $self->get_name_and_class($name); |
| 871 |
|
| |
| 872 |
|
| if ($opt{type} eq 'del') { |
| 873 |
|
| my $var = $self->find_var($var_name, $class_name); |
| 874 |
|
| |
| 875 |
|
| if (not defined($var)) { |
| 876 |
|
| $self->fatal_error( |
| 877 |
|
| code => $_, |
| 878 |
|
| pos => pos($_) - length($name), |
| 879 |
|
| var => ($class_name . '::' . $var_name), |
| 880 |
|
| error => "attempt to delete non-existent variable `$name`", |
| 881 |
|
| ); |
| 882 |
|
| } |
| 883 |
|
| } |
| 884 |
|
| |
| 885 |
|
| if (exists($self->{keywords}{$var_name}) or exists($self->{built_in_classes}{$var_name})) { |
| 886 |
|
| $self->fatal_error( |
| 887 |
|
| code => $_, |
| 888 |
|
| pos => $-[2], |
| 889 |
|
| error => "`$var_name` is either a keyword or a predefined variable!", |
| 890 |
|
| ); |
| 891 |
|
| } |
| 892 |
|
| |
| 893 |
|
| if (defined($end_delim) and m{\G<<?\h*}gc) { |
| 894 |
|
| my ($subset_name) = /\G($self->{var_name_re})/goc; |
| 895 |
|
| |
| 896 |
|
| $subset_name // $self->fatal_error( |
| 897 |
|
| code => $_, |
| 898 |
|
| pos => pos($_), |
| 899 |
|
| error => "expected the name of the subset", |
| 900 |
|
| ); |
| 901 |
|
| |
| 902 |
|
| my $code = $subset_name; |
| 903 |
|
| my $obj = $self->parse_expr(code => \$code); |
| 904 |
|
| |
| 905 |
|
| (defined($obj) and ref($obj) ne 'HASH') |
| 906 |
|
| || $self->fatal_error( |
| 907 |
|
| code => $_, |
| 908 |
|
| pos => pos($_), |
| 909 |
|
| error => "expected a subset or a type", |
| 910 |
|
| ); |
| 911 |
|
| |
| 912 |
|
| $subset = $obj; |
| 913 |
|
| } |
| 914 |
|
| |
| 915 |
|
| my ($value, $where_expr, $where_block); |
| 916 |
|
| |
| 917 |
|
| if (defined($end_delim)) { |
| 918 |
|
| |
| 919 |
|
| if (/\G\h*(?=\{)/gc) { |
| 920 |
|
| $where_block = $self->parse_block(code => $opt{code}, topic_var => 1); |
| 921 |
|
| } |
| 922 |
|
| elsif (/\G\h*(?=\()/gc) { |
| 923 |
|
| $where_expr = $self->parse_arg(code => $opt{code}); |
| 924 |
|
| } |
| 925 |
|
| |
| 926 |
|
| if (/$self->{var_init_sep_re}/goc) { |
| 927 |
|
| my $obj = $self->parse_obj(code => $opt{code}, multiline => 1); |
| 928 |
|
| $value = ( |
| 929 |
|
| ref($obj) eq 'HASH' |
| 930 |
|
| ? $obj |
| 931 |
|
| : {$self->{class} => [{self => $obj}]} |
| 932 |
|
| ); |
| 933 |
|
| } |
| 934 |
|
| } |
| 935 |
|
| |
| 936 |
|
| #<<< |
| 937 |
|
| my $obj = bless( |
| 938 |
|
| { |
| 939 |
|
| name => $var_name, |
| 940 |
|
| type => $opt{type}, |
| 941 |
|
| (defined($ref_type) ? (ref_type => $ref_type) : ()), |
| 942 |
|
| (defined($subset) ? (subset => $subset) : ()), |
| 943 |
|
| class => $class_name, |
| 944 |
|
| defined($value) ? (value => $value, has_value => 1) : (), |
| 945 |
|
| defined($attr) |
| 946 |
|
| ? ($attr eq '*' ? (array => 1, slurpy => 1) |
| 947 |
|
| : $attr eq ':' ? (hash => 1, slurpy => 1) : ()) |
| 948 |
|
| : (), |
| 949 |
|
| defined($where_block) ? (where_block => $where_block) : (), |
| 950 |
|
| defined($where_expr) ? (where_expr => $where_expr) : (), |
| 951 |
|
| }, |
| 952 |
|
| 'Sidef::Variable::Variable' |
| 953 |
|
| ); |
| 954 |
|
| #>>> |
| 955 |
|
| |
| 956 |
|
| if (exists($opt{callback})) { |
| 957 |
|
| $opt{callback}->($obj); |
| 958 |
|
| } |
| 959 |
|
| |
| 960 |
|
| if (!$opt{private} and $var_name ne '') { |
| 961 |
|
| unshift @{$self->{vars}{$class_name}}, |
| 962 |
|
| { |
| 963 |
|
| obj => $obj, |
| 964 |
|
| name => $var_name, |
| 965 |
|
| count => 0, |
| 966 |
|
| type => $opt{type}, |
| 967 |
|
| line => $self->{line}, |
| 968 |
|
| }; |
| 969 |
|
| } |
| 970 |
|
| |
| 971 |
|
| if ($var_name eq '') { |
| 972 |
|
| $obj->{name} = '__ANON__' . refaddr($obj); |
| 973 |
|
| } |
| 974 |
|
| |
| 975 |
|
| push @var_objs, $obj; |
| 976 |
|
| (defined($end_delim) && (/\G\h*,\h*/gc || /\G\h*(?:#.*)?+(?=\R)/gc)) || last; |
| 977 |
|
| |
| 978 |
|
| if ($opt{params} and $obj->{slurpy}) { |
| 979 |
|
| $self->fatal_error( |
| 980 |
|
| error => "can't declare more parameters after a slurpy parameter", |
| 981 |
|
| code => $_, |
| 982 |
|
| pos => pos($_), |
| 983 |
|
| ); |
| 984 |
|
| } |
| 985 |
|
| |
| 986 |
|
| $self->parse_whitespace(code => $opt{code}); |
| 987 |
|
| } |
| 988 |
|
| |
| 989 |
|
| $self->parse_whitespace(code => $opt{code}) if defined($end_delim); |
| 990 |
|
| |
| 991 |
|
| defined($end_delim) |
| 992 |
|
| && ( |
| 993 |
|
| /\G\h*\Q$end_delim\E/gc |
| 994 |
|
| || $self->fatal_error( |
| 995 |
|
| code => $_, |
| 996 |
|
| pos => pos($_), |
| 997 |
|
| error => "can't find the closing delimiter: `$end_delim`", |
| 998 |
|
| ) |
| 999 |
|
| ); |
| 1000 |
|
| |
| 1001 |
|
| return \@var_objs; |
| 1002 |
|
| } |
| 1003 |
|
| |
| 1004 |
|
| sub parse_whitespace { |
| 1005 |
|
| my ($self, %opt) = @_; |
| 1006 |
|
| |
| 1007 |
|
| my $beg_line = $self->{line}; |
| 1008 |
|
| my $found_space = -1; |
| 1009 |
|
| local *_ = $opt{code}; |
| 1010 |
|
| { |
| 1011 |
|
| ++$found_space; |
| 1012 |
|
| |
| 1013 |
|
| # Whitespace |
| 1014 |
|
| if (/\G(?=\s)/) { |
| 1015 |
|
| |
| 1016 |
|
| # Horizontal space |
| 1017 |
|
| if (/\G\h+/gc) { |
| 1018 |
|
| redo; |
| 1019 |
|
| } |
| 1020 |
|
| |
| 1021 |
|
| # Generic line |
| 1022 |
|
| if (/\G\R/gc) { |
| 1023 |
|
| ++$self->{line}; |
| 1024 |
|
| |
| 1025 |
|
| # Here-document |
| 1026 |
|
| while ($#{$self->{EOT}} != -1) { |
| 1027 |
|
| |
| 1028 |
|
| my $eot = shift @{$self->{EOT}}; |
| 1029 |
|
| my $name = $eot->{name}; |
| 1030 |
|
| my $indent = $eot->{indent}; |
| 1031 |
|
| |
| 1032 |
|
| my $spaces = 0; |
| 1033 |
|
| my $acc = ''; |
| 1034 |
|
| until (/\G$name(?:\R|\z)/gc) { |
| 1035 |
|
| |
| 1036 |
|
| if (/\G(.*)/gc) { |
| 1037 |
|
| $acc .= "$1\n"; |
| 1038 |
|
| } |
| 1039 |
|
| |
| 1040 |
|
| # Indentation is true |
| 1041 |
|
| if ($indent && /\G\R(\h*)$name(?:\R|\z)/gc) { |
| 1042 |
|
| $spaces = length($1); |
| 1043 |
|
| ++$self->{line}; |
| 1044 |
|
| last; |
| 1045 |
|
| } |
| 1046 |
|
| |
| 1047 |
|
| /\G\R/gc |
| 1048 |
|
| ? ++$self->{line} |
| 1049 |
|
| : $self->fatal_error( |
| 1050 |
|
| error => "can't find string terminator <<$name>> anywhere before end-of-file", |
| 1051 |
|
| code => $_, |
| 1052 |
|
| pos => $eot->{pos}, |
| 1053 |
|
| line => $eot->{line}, |
| 1054 |
|
| ); |
| 1055 |
|
| } |
| 1056 |
|
| |
| 1057 |
|
| if ($indent and $spaces > 0) { |
| 1058 |
|
| $acc =~ s/^\h{1,$spaces}//gm; |
| 1059 |
|
| } |
| 1060 |
|
| |
| 1061 |
|
| ++$self->{line}; |
| 1062 |
|
| push @{$eot->{obj}{$self->{class}}}, |
| 1063 |
|
| { |
| 1064 |
|
| self => ( |
| 1065 |
|
| $eot->{type} == 0 |
| 1066 |
|
| ? Sidef::Types::String::String->new($acc) |
| 1067 |
|
| : Sidef::Types::String::String->new($acc)->apply_escapes($self) |
| 1068 |
|
| ) |
| 1069 |
|
| }; |
| 1070 |
|
| } |
| 1071 |
|
| |
| 1072 |
|
| /\G\h+/gc; |
| 1073 |
|
| redo; |
| 1074 |
|
| } |
| 1075 |
|
| |
| 1076 |
|
| # Vertical space |
| 1077 |
|
| if (/\G\v+/gc) { # should not reach here |
| 1078 |
|
| redo; |
| 1079 |
|
| } |
| 1080 |
|
| } |
| 1081 |
|
| |
| 1082 |
|
| # ZERO WIDTH SPACE |
| 1083 |
|
| # https://www.fileformat.info/info/unicode/char/200b/index.htm |
| 1084 |
|
| if (/\G\x{200B}+/gc) { |
| 1085 |
|
| redo; |
| 1086 |
|
| } |
| 1087 |
|
| |
| 1088 |
|
| # Embedded comments (https://docs.raku.org/language/syntax#Multi-line_/_embedded_comments) |
| 1089 |
|
| if (/\G#`(?=[[:punct:]])/gc) { |
| 1090 |
|
| $self->get_quoted_string(code => $opt{code}); |
| 1091 |
|
| redo; |
| 1092 |
|
| } |
| 1093 |
|
| |
| 1094 |
|
| # One-line comment |
| 1095 |
|
| if (/\G#.*/gc) { |
| 1096 |
|
| redo; |
| 1097 |
|
| } |
| 1098 |
|
| |
| 1099 |
|
| # Multi-line C comment |
| 1100 |
|
| if (m{\G/\*}gc) { |
| 1101 |
|
| while (1) { |
| 1102 |
|
| m{\G.*?\*/}gc && last; |
| 1103 |
|
| /\G.+/gc || (/\G\R/gc ? $self->{line}++ : last); |
| 1104 |
|
| } |
| 1105 |
|
| redo; |
| 1106 |
|
| } |
| 1107 |
|
| |
| 1108 |
|
| if ($found_space > 0) { |
| 1109 |
|
| return 1; |
| 1110 |
|
| } |
| 1111 |
|
| |
| 1112 |
|
| return; |
| 1113 |
|
| } |
| 1114 |
|
| } |
| 1115 |
|
| |
| 1116 |
|
| sub parse_expr { |
| 1117 |
|
| my ($self, %opt) = @_; |
| 1118 |
|
| |
| 1119 |
|
| local *_ = $opt{code}; |
| 1120 |
|
| { |
| 1121 |
|
| $self->parse_whitespace(code => $opt{code}); |
| 1122 |
|
| |
| 1123 |
|
| # End of an expression, or end of the script |
| 1124 |
|
| if (/\G;/gc || /\G\z/) { |
| 1125 |
|
| return; |
| 1126 |
|
| } |
| 1127 |
|
| |
| 1128 |
|
| if (/$self->{quote_operators_re}/goc) { |
| 1129 |
|
| my ($double_quoted, $method, $package) = @{$^R}; |
| 1130 |
|
| |
| 1131 |
|
| pos($_) -= 1; |
| 1132 |
|
| my ($string, $pos) = $self->get_quoted_string(code => $opt{code}); |
| 1133 |
|
| |
| 1134 |
|
| # Special case for array-like objects (bytes and chars) |
| 1135 |
|
| my @array_like; |
| 1136 |
|
| if ($method ne 'new' and $method ne '__NEW__') { |
| 1137 |
|
| @array_like = ($package, $method); |
| 1138 |
|
| $package = 'Sidef::Types::String::String'; |
| 1139 |
|
| $method = 'new'; |
| 1140 |
|
| } |
| 1141 |
|
| |
| 1142 |
|
| if ($package eq 'Sidef::Module::Func' or $package eq 'Sidef::Module::OO') { |
| 1143 |
|
| if ($string !~ /^$self->{var_name_re}\z/) { |
| 1144 |
|
| $self->fatal_error( |
| 1145 |
|
| code => $_, |
| 1146 |
|
| pos => (pos($_) - length($string) - 1), |
| 1147 |
|
| error => "invalid symbol declaration", |
| 1148 |
|
| reason => "expected a variable-like name", |
| 1149 |
|
| ); |
| 1150 |
|
| } |
| 1151 |
|
| } |
| 1152 |
|
| |
| 1153 |
|
| my $obj = ( |
| 1154 |
|
| $double_quoted |
| 1155 |
|
| ? do { |
| 1156 |
|
| state $str = Sidef::Types::String::String->new; # load the string module |
| 1157 |
|
| Sidef::Types::String::String::apply_escapes($package->$method($string), $self); |
| 1158 |
|
| } |
| 1159 |
|
| : $package->$method($string =~ s{\\\\}{\\}gr) |
| 1160 |
|
| ); |
| 1161 |
|
| |
| 1162 |
|
| # Special case for backticks and Perl code (add method 'run') |
| 1163 |
|
| if ($package eq 'Sidef::Types::Glob::Backtick' or $package eq 'Sidef::Types::Perl::Perl') { |
| 1164 |
|
| my $struct = |
| 1165 |
|
| $double_quoted && ref($obj) eq 'HASH' |
| 1166 |
|
| ? $obj |
| 1167 |
|
| : { |
| 1168 |
|
| $self->{class} => [ |
| 1169 |
|
| { |
| 1170 |
|
| self => $obj, |
| 1171 |
|
| call => [], |
| 1172 |
|
| } |
| 1173 |
|
| ] |
| 1174 |
|
| }; |
| 1175 |
|
| |
| 1176 |
|
| push @{$struct->{$self->{class}}[-1]{call}}, {method => 'run'}; |
| 1177 |
|
| $obj = $struct; |
| 1178 |
|
| } |
| 1179 |
|
| elsif (@array_like) { |
| 1180 |
|
| if ($double_quoted and ref($obj) eq 'HASH') { |
| 1181 |
|
| push @{$obj->{$self->{class}}[-1]{call}}, {method => $array_like[1]}; |
| 1182 |
|
| } |
| 1183 |
|
| else { |
| 1184 |
|
| my $method = $array_like[1]; |
| 1185 |
|
| $obj = $obj->$method; |
| 1186 |
|
| } |
| 1187 |
|
| } |
| 1188 |
|
| |
| 1189 |
|
| return $obj; |
| 1190 |
|
| } |
| 1191 |
|
| |
| 1192 |
|
| # Object as expression |
| 1193 |
|
| if (/\G(?=\()/) { |
| 1194 |
|
| my $obj = $self->parse_arg(code => $opt{code}); |
| 1195 |
|
| return $obj; |
| 1196 |
|
| } |
| 1197 |
|
| |
| 1198 |
|
| # Block as object |
| 1199 |
|
| if (/\G(?=\{)/) { |
| 1200 |
|
| my $obj = $self->parse_block(code => $opt{code}, topic_var => 1); |
| 1201 |
|
| return $obj; |
| 1202 |
|
| } |
| 1203 |
|
| |
| 1204 |
|
| # Array as object |
| 1205 |
|
| if (/\G(?=\[)/) { |
| 1206 |
|
| |
| 1207 |
|
| my @array; |
| 1208 |
|
| my $obj = $self->parse_array(code => $opt{code}); |
| 1209 |
|
| |
| 1210 |
|
| if (ref($obj->{$self->{class}}) eq 'ARRAY') { |
| 1211 |
|
| push @array, @{$obj->{$self->{class}}}; |
| 1212 |
|
| } |
| 1213 |
|
| |
| 1214 |
|
| return bless(\@array, 'Sidef::Types::Array::HCArray'); |
| 1215 |
|
| } |
| 1216 |
|
| |
| 1217 |
|
| # Bareword followed by a fat comma or preceded by a colon |
| 1218 |
|
| if ( /\G:(\w+)/gc |
| 1219 |
|
| || /\G([^\W\d]\w*+)(?=\h*=>)/gc) { |
| 1220 |
|
| |
| 1221 |
|
| # || /\G([^\W\d]\w*+)(?=\h*=>|:(?![=:]))/gc) { |
| 1222 |
|
| return Sidef::Types::String::String->new($1); |
| 1223 |
|
| } |
| 1224 |
|
| |
| 1225 |
|
| # Bareword followed by a colon becomes a NamedParam with the bareword |
| 1226 |
|
| # on the LHS |
| 1227 |
|
| if (/\G([^\W\d]\w*+):(?![=:])/gc) { |
| 1228 |
|
| my $name = $1; |
| 1229 |
|
| my $obj = $self->parse_obj(code => $opt{code}); |
| 1230 |
|
| return Sidef::Variable::NamedParam->new($name, $obj); |
| 1231 |
|
| } |
| 1232 |
|
| |
| 1233 |
|
| # Declaration of variables (global and lexical) |
| 1234 |
|
| if (/\G(var|global|del)\b\h*/gc) { |
| 1235 |
|
| my $type = $1; |
| 1236 |
|
| my $vars = $self->parse_init_vars(code => $opt{code}, type => $type); |
| 1237 |
|
| my $init_obj = bless({vars => $vars}, 'Sidef::Variable::Init'); |
| 1238 |
|
| |
| 1239 |
|
| if (/\G\h*=\h*/gc) { |
| 1240 |
|
| |
| 1241 |
|
| my $args = $self->parse_obj(code => $opt{code}, multiline => 1); |
| 1242 |
|
| |
| 1243 |
|
| $args // $self->fatal_error( |
| 1244 |
|
| code => $_, |
| 1245 |
|
| pos => pos($_), |
| 1246 |
|
| error => "expected an expression after variable declaration", |
| 1247 |
|
| ); |
| 1248 |
|
| |
| 1249 |
|
| $init_obj->{args} = $args; |
| 1250 |
|
| } |
| 1251 |
|
| |
| 1252 |
|
| #if ($type eq 'del') { |
| 1253 |
|
| # return bless {vars => []}, 'Sidef::Variable::Init'; |
| 1254 |
|
| #} |
| 1255 |
|
| |
| 1256 |
|
| return $init_obj; |
| 1257 |
|
| } |
| 1258 |
|
| |
| 1259 |
|
| # "has" class attributes |
| 1260 |
|
| if (exists($self->{current_class}) and /\Ghas\b\h*/gc) { |
| 1261 |
|
| |
| 1262 |
|
| local $self->{allow_class_variable} = 0; |
| 1263 |
|
| |
| 1264 |
|
| my $vars = $self->parse_init_vars( |
| 1265 |
|
| code => $opt{code}, |
| 1266 |
|
| type => 'has', |
| 1267 |
|
| private => 1, |
| 1268 |
|
| ); |
| 1269 |
|
| |
| 1270 |
|
| foreach my $var (@{$vars}) { |
| 1271 |
|
| my $name = $var->{name}; |
| 1272 |
|
| if (exists($self->{keywords}{$name}) or exists($self->{built_in_classes}{$name})) { |
| 1273 |
|
| $self->fatal_error( |
| 1274 |
|
| code => $_, |
| 1275 |
|
| pos => (pos($_) - length($name)), |
| 1276 |
|
| error => "`$name` is either a keyword or a predefined variable!", |
| 1277 |
|
| ); |
| 1278 |
|
| } |
| 1279 |
|
| } |
| 1280 |
|
| |
| 1281 |
|
| my $args; |
| 1282 |
|
| if (/\G\h*=\h*/gc) { |
| 1283 |
|
| $args = $self->parse_obj(code => $opt{code}, multiline => 1); |
| 1284 |
|
| $args // $self->fatal_error( |
| 1285 |
|
| code => $_, |
| 1286 |
|
| pos => pos($_) - 2, |
| 1287 |
|
| error => qq{expected an expression after "=" in `has` declaration}, |
| 1288 |
|
| ); |
| 1289 |
|
| } |
| 1290 |
|
| |
| 1291 |
|
| my $obj = bless {vars => $vars, defined($args) ? (args => $args) : ()}, 'Sidef::Variable::ClassAttr'; |
| 1292 |
|
| push @{$self->{current_class}{attributes}}, $obj; |
| 1293 |
|
| return $obj; |
| 1294 |
|
| } |
| 1295 |
|
| |
| 1296 |
|
| # Declaration of constants and static variables |
| 1297 |
|
| if (/\G(define|const|static)\b\h*/gc) { |
| 1298 |
|
| my $type = $1; |
| 1299 |
|
| my $line = $self->{line}; |
| 1300 |
|
| |
| 1301 |
|
| my @var_objs; |
| 1302 |
|
| |
| 1303 |
|
| my $callback = sub { |
| 1304 |
|
| my ($v) = @_; |
| 1305 |
|
| |
| 1306 |
|
| my $name = $v->{name}; |
| 1307 |
|
| my $class_name = $v->{class}; |
| 1308 |
|
| |
| 1309 |
|
| my $var = ( |
| 1310 |
|
| $type eq 'define' ? bless($v, 'Sidef::Variable::Define') |
| 1311 |
|
| : $type eq 'static' ? bless($v, 'Sidef::Variable::Static') |
| 1312 |
|
| : $type eq 'const' ? bless($v, 'Sidef::Variable::Const') |
| 1313 |
|
| : die "[PARSER ERROR] Invalid variable type: $type" |
| 1314 |
|
| ); |
| 1315 |
|
| |
| 1316 |
|
| push @var_objs, $var; |
| 1317 |
|
| |
| 1318 |
|
| unshift @{$self->{vars}{$class_name}}, |
| 1319 |
|
| { |
| 1320 |
|
| obj => $var, |
| 1321 |
|
| name => $name, |
| 1322 |
|
| count => 0, |
| 1323 |
|
| type => $type, |
| 1324 |
|
| line => $line, |
| 1325 |
|
| }; |
| 1326 |
|
| }; |
| 1327 |
|
| |
| 1328 |
|
| my $vars = $self->parse_init_vars( |
| 1329 |
|
| code => $opt{code}, |
| 1330 |
|
| type => $type, |
| 1331 |
|
| private => 1, |
| 1332 |
|
| callback => $callback, |
| 1333 |
|
| ); |
| 1334 |
|
| |
| 1335 |
|
| foreach my $var (@var_objs) { |
| 1336 |
|
| my $name = $var->{name}; |
| 1337 |
|
| if (exists($self->{keywords}{$name}) or exists($self->{built_in_classes}{$name})) { |
| 1338 |
|
| $self->fatal_error( |
| 1339 |
|
| code => $_, |
| 1340 |
|
| pos => (pos($_) - length($name)), |
| 1341 |
|
| error => "`$name` is either a keyword or a predefined variable!", |
| 1342 |
|
| ); |
| 1343 |
|
| } |
| 1344 |
|
| } |
| 1345 |
|
| |
| 1346 |
|
| if (@var_objs == 1 and /\G\h*=\h*/gc) { |
| 1347 |
|
| |
| 1348 |
|
| my $var = $var_objs[0]; |
| 1349 |
|
| my $obj = $self->parse_obj(code => $opt{code}, multiline => 1); |
| 1350 |
|
| |
| 1351 |
|
| $obj // $self->fatal_error( |
| 1352 |
|
| code => $_, |
| 1353 |
|
| pos => pos($_) - 2, |
| 1354 |
|
| error => qq{expected an expression after $type "$var->{name}"}, |
| 1355 |
|
| ); |
| 1356 |
|
| |
| 1357 |
|
| $var->{value} = $obj; |
| 1358 |
|
| } |
| 1359 |
|
| |
| 1360 |
|
| my $const_init = bless({vars => \@var_objs, type => $type}, 'Sidef::Variable::ConstInit'); |
| 1361 |
|
| |
| 1362 |
|
| if (/\G\h*=\h*/gc) { |
| 1363 |
|
| $self->fatal_error( |
| 1364 |
|
| code => $_, |
| 1365 |
|
| pos => pos($_) - 2, |
| 1366 |
|
| error => qq{the correct syntax is: `$type(x = ..., y = ...)`}, |
| 1367 |
|
| ); |
| 1368 |
|
| } |
| 1369 |
|
| |
| 1370 |
|
| return $const_init; |
| 1371 |
|
| } |
| 1372 |
|
| |
| 1373 |
|
| # Struct declaration |
| 1374 |
|
| if (/\Gstruct\b\h*/gc) { |
| 1375 |
|
| |
| 1376 |
|
| my ($name, $class_name); |
| 1377 |
|
| if (/\G($self->{var_name_re})\h*/goc) { |
| 1378 |
|
| ($name, $class_name) = $self->get_name_and_class($1); |
| 1379 |
|
| } |
| 1380 |
|
| |
| 1381 |
|
| if (defined($name) and (exists($self->{keywords}{$name}) or exists($self->{built_in_classes}{$name}))) { |
| 1382 |
|
| $self->fatal_error( |
| 1383 |
|
| code => $_, |
| 1384 |
|
| pos => (pos($_) - length($name)), |
| 1385 |
|
| error => "`$name` is either a keyword or a predefined variable!", |
| 1386 |
|
| ); |
| 1387 |
|
| } |
| 1388 |
|
| |
| 1389 |
|
| my $struct = bless( |
| 1390 |
|
| { |
| 1391 |
|
| name => $name, |
| 1392 |
|
| class => $class_name, |
| 1393 |
|
| }, |
| 1394 |
|
| 'Sidef::Variable::Struct' |
| 1395 |
|
| ); |
| 1396 |
|
| |
| 1397 |
|
| if (defined $name) { |
| 1398 |
|
| unshift @{$self->{vars}{$class_name}}, |
| 1399 |
|
| { |
| 1400 |
|
| obj => $struct, |
| 1401 |
|
| name => $name, |
| 1402 |
|
| count => 0, |
| 1403 |
|
| type => 'struct', |
| 1404 |
|
| line => $self->{line}, |
| 1405 |
|
| }; |
| 1406 |
|
| } |
| 1407 |
|
| |
| 1408 |
|
| my $vars = |
| 1409 |
|
| $self->parse_init_vars( |
| 1410 |
|
| code => $opt{code}, |
| 1411 |
|
| type => 'var', |
| 1412 |
|
| private => 1, |
| 1413 |
|
| ); |
| 1414 |
|
| |
| 1415 |
|
| $struct->{vars} = $vars; |
| 1416 |
|
| |
| 1417 |
|
| return $struct; |
| 1418 |
|
| } |
| 1419 |
|
| |
| 1420 |
|
| # Subset declaration |
| 1421 |
|
| if (/\Gsubset\b\h*/gc) { |
| 1422 |
|
| |
| 1423 |
|
| my ($name, $class_name); |
| 1424 |
|
| if (/\G($self->{var_name_re})\h*/goc) { |
| 1425 |
|
| ($name, $class_name) = $self->get_name_and_class($1); |
| 1426 |
|
| } |
| 1427 |
|
| else { |
| 1428 |
|
| $self->fatal_error( |
| 1429 |
|
| code => $_, |
| 1430 |
|
| pos => pos($_), |
| 1431 |
|
| error => "expected a name after the keyword 'subset'", |
| 1432 |
|
| ); |
| 1433 |
|
| } |
| 1434 |
|
| |
| 1435 |
|
| if (exists($self->{keywords}{$name}) or exists($self->{built_in_classes}{$name})) { |
| 1436 |
|
| $self->fatal_error( |
| 1437 |
|
| code => $_, |
| 1438 |
|
| pos => (pos($_) - length($name)), |
| 1439 |
|
| error => "`$name` is either a keyword or a predefined variable!", |
| 1440 |
|
| ); |
| 1441 |
|
| } |
| 1442 |
|
| |
| 1443 |
|
| my $subset = bless({name => $name, class => $class_name}, 'Sidef::Variable::Subset'); |
| 1444 |
|
| |
| 1445 |
|
| unshift @{$self->{vars}{$class_name}}, |
| 1446 |
|
| { |
| 1447 |
|
| obj => $subset, |
| 1448 |
|
| name => $name, |
| 1449 |
|
| count => 0, |
| 1450 |
|
| type => 'subset', |
| 1451 |
|
| line => $self->{line}, |
| 1452 |
|
| }; |
| 1453 |
|
| |
| 1454 |
|
| # Inheritance |
| 1455 |
|
| if (/\G<<?\h*/gc) { |
| 1456 |
|
| { |
| 1457 |
|
| my ($name) = /\G($self->{var_name_re})\h*/goc; |
| 1458 |
|
| |
| 1459 |
|
| $name // $self->fatal_error( |
| 1460 |
|
| code => $_, |
| 1461 |
|
| pos => pos($_), |
| 1462 |
|
| error => "expected a type name for subsetting", |
| 1463 |
|
| ); |
| 1464 |
|
| |
| 1465 |
|
| my $code = $name; |
| 1466 |
|
| my $type = $self->parse_expr(code => \$code); |
| 1467 |
|
| |
| 1468 |
|
| push @{$subset->{inherits}}, $type; |
| 1469 |
|
| |
| 1470 |
|
| /\G,\h*/gc && redo; |
| 1471 |
|
| } |
| 1472 |
|
| } |
| 1473 |
|
| |
| 1474 |
|
| if (/\G(?=\{)/) { |
| 1475 |
|
| my $block = $self->parse_block(code => $opt{code}, topic_var => 1); |
| 1476 |
|
| $subset->{block} = $block; |
| 1477 |
|
| } |
| 1478 |
|
| |
| 1479 |
|
| return $subset; |
| 1480 |
|
| } |
| 1481 |
|
| |
| 1482 |
|
| # Declaration of enums |
| 1483 |
|
| if (/\Genum\b\h*/gc) { |
| 1484 |
|
| my $vars = |
| 1485 |
|
| $self->parse_init_vars( |
| 1486 |
|
| code => $opt{code}, |
| 1487 |
|
| type => 'var', |
| 1488 |
|
| private => 1, |
| 1489 |
|
| ); |
| 1490 |
|
| |
| 1491 |
|
| @{$vars} |
| 1492 |
|
| || $self->fatal_error( |
| 1493 |
|
| code => $_, |
| 1494 |
|
| pos => pos($_), |
| 1495 |
|
| error => q{expected one or more variable names after <enum>}, |
| 1496 |
|
| ); |
| 1497 |
|
| |
| 1498 |
|
| my $value = Sidef::Types::Number::Number::_set_int(-1); |
| 1499 |
|
| |
| 1500 |
|
| foreach my $var (@{$vars}) { |
| 1501 |
|
| my $name = $var->{name}; |
| 1502 |
|
| |
| 1503 |
|
| if (ref $var->{value} eq 'HASH') { |
| 1504 |
|
| $var->{value} = $var->{value}{$self->{class}}[-1]{self}; |
| 1505 |
|
| } |
| 1506 |
|
| |
| 1507 |
|
| $value = |
| 1508 |
|
| $var->{has_value} |
| 1509 |
|
| ? $var->{value} |
| 1510 |
|
| : $value->inc; |
| 1511 |
|
| |
| 1512 |
|
| if (exists($self->{keywords}{$name}) or exists($self->{built_in_classes}{$name})) { |
| 1513 |
|
| $self->fatal_error( |
| 1514 |
|
| code => $_, |
| 1515 |
|
| pos => (pos($_) - length($name)), |
| 1516 |
|
| error => "`$name` is either a keyword or a predefined variable!", |
| 1517 |
|
| ); |
| 1518 |
|
| } |
| 1519 |
|
| |
| 1520 |
|
| unshift @{$self->{vars}{$self->{class}}}, |
| 1521 |
|
| { |
| 1522 |
|
| obj => $value, |
| 1523 |
|
| name => $name, |
| 1524 |
|
| count => 0, |
| 1525 |
|
| type => 'enum', |
| 1526 |
|
| line => $self->{line}, |
| 1527 |
|
| }; |
| 1528 |
|
| } |
| 1529 |
|
| |
| 1530 |
|
| return $value; |
| 1531 |
|
| } |
| 1532 |
|
| |
| 1533 |
|
| # Local variables |
| 1534 |
|
| if (/\Glocal\b\h*/gc) { |
| 1535 |
|
| my $expr = $self->parse_obj(code => $opt{code}); |
| 1536 |
|
| return bless({expr => $expr}, 'Sidef::Variable::Local'); |
| 1537 |
|
| } |
| 1538 |
|
| |
| 1539 |
|
| # Declaration of classes, methods and functions |
| 1540 |
|
| if ( |
| 1541 |
|
| /\G(func|class)\b\h*/gc |
| 1542 |
|
| || /\G(->)\h*/gc |
| 1543 |
|
| || (exists($self->{current_class}) |
| 1544 |
|
| && /\G(method)\b\h*/gc) |
| 1545 |
|
| ) { |
| 1546 |
|
| |
| 1547 |
|
| my $beg_pos = $-[0]; |
| 1548 |
|
| my $type = |
| 1549 |
|
| $1 eq '->' |
| 1550 |
|
| ? exists($self->{current_class}) && !(exists($self->{current_method})) |
| 1551 |
|
| ? 'method' |
| 1552 |
|
| : 'func' |
| 1553 |
|
| : $1; |
| 1554 |
|
| |
| 1555 |
|
| my $name = ''; |
| 1556 |
|
| my $class_name = $self->{class}; |
| 1557 |
|
| my $built_in_obj; |
| 1558 |
|
| |
| 1559 |
|
| if ($type eq 'class' and /\G($self->{var_name_re})\h*/gco) { |
| 1560 |
|
| |
| 1561 |
|
| $name = $1; |
| 1562 |
|
| |
| 1563 |
|
| if (exists($self->{built_in_classes}{$name}) and /\G(?=[{<])/) { |
| 1564 |
|
| |
| 1565 |
|
| my ($obj) = $self->parse_expr(code => \$name); |
| 1566 |
|
| |
| 1567 |
|
| if (defined($obj)) { |
| 1568 |
|
| $name = ''; |
| 1569 |
|
| $built_in_obj = $obj; |
| 1570 |
|
| } |
| 1571 |
|
| } |
| 1572 |
|
| else { |
| 1573 |
|
| ($name, $class_name) = $self->get_name_and_class($1); |
| 1574 |
|
| } |
| 1575 |
|
| } |
| 1576 |
|
| |
| 1577 |
|
| if ($type eq 'method') { |
| 1578 |
|
| $name = ( |
| 1579 |
|
| /\G($self->{method_name_re})\h*/goc ? $1 |
| 1580 |
|
| : /\G($self->{operators_re})\h*/goc ? $+ |
| 1581 |
|
| : '' |
| 1582 |
|
| ); |
| 1583 |
|
| ($name, $class_name) = $self->get_name_and_class($name); |
| 1584 |
|
| } |
| 1585 |
|
| elsif ($type ne 'class') { |
| 1586 |
|
| $name = /\G($self->{var_name_re})\h*/goc ? $1 : ''; |
| 1587 |
|
| ($name, $class_name) = $self->get_name_and_class($name); |
| 1588 |
|
| } |
| 1589 |
|
| |
| 1590 |
|
| if ( $type ne 'method' |
| 1591 |
|
| and $type ne 'class' |
| 1592 |
|
| and (exists($self->{keywords}{$name}) or exists($self->{built_in_classes}{$name}))) { |
| 1593 |
|
| $self->fatal_error( |
| 1594 |
|
| code => $_, |
| 1595 |
|
| pos => $-[0], |
| 1596 |
|
| error => "`$name` is either a keyword or a predefined variable!", |
| 1597 |
|
| ); |
| 1598 |
|
| } |
| 1599 |
|
| |
| 1600 |
|
| my $obj = |
| 1601 |
|
| ($type eq 'func' or $type eq 'method') ? bless({name => $name, type => $type, class => $class_name}, 'Sidef::Variable::Variable') |
| 1602 |
|
| : $type eq 'class' ? bless({name => ($built_in_obj // $name), class => $class_name}, 'Sidef::Variable::ClassInit') |
| 1603 |
|
| : $self->fatal_error( |
| 1604 |
|
| error => "invalid type", |
| 1605 |
|
| reason => "expected a magic thing to happen", |
| 1606 |
|
| code => $_, |
| 1607 |
|
| pos => pos($_), |
| 1608 |
|
| ); |
| 1609 |
|
| |
| 1610 |
|
| if ($name ne '') { |
| 1611 |
|
| my $var = $self->find_var($name, $class_name); |
| 1612 |
|
| |
| 1613 |
|
| if (defined($var) and $var->{type} eq 'class') { |
| 1614 |
|
| $obj->{parent} = $var->{obj}; |
| 1615 |
|
| push @{$obj->{inherit}}, ref($var->{obj}{name}) ? $var->{obj}{name} : $var->{obj}; |
| 1616 |
|
| } |
| 1617 |
|
| } |
| 1618 |
|
| |
| 1619 |
|
| my $has_kids = 0; |
| 1620 |
|
| my $parent; |
| 1621 |
|
| if (($type eq 'method' or $type eq 'func') and $name ne '') { |
| 1622 |
|
| my $var = $self->find_var($name, $class_name); |
| 1623 |
|
| |
| 1624 |
|
| # A function or a method must be declared in the same scope |
| 1625 |
|
| if (defined($var) and $var->{obj}{type} eq $type) { |
| 1626 |
|
| |
| 1627 |
|
| $parent = $var->{obj}; |
| 1628 |
|
| $has_kids = 1; |
| 1629 |
|
| |
| 1630 |
|
| #~ push @{$var->{obj}{value}{kids}}, $obj; |
| 1631 |
|
| $parent->{has_kids} = 1; |
| 1632 |
|
| $obj->{parent} = $parent; |
| 1633 |
|
| } |
| 1634 |
|
| } |
| 1635 |
|
| |
| 1636 |
|
| if (not $has_kids) { |
| 1637 |
|
| unshift @{$self->{vars}{$class_name}}, |
| 1638 |
|
| { |
| 1639 |
|
| obj => $obj, |
| 1640 |
|
| name => $name, |
| 1641 |
|
| count => 0, |
| 1642 |
|
| type => $type, |
| 1643 |
|
| line => $self->{line}, |
| 1644 |
|
| }; |
| 1645 |
|
| } |
| 1646 |
|
| |
| 1647 |
|
| if ($type eq 'class') { |
| 1648 |
|
| my $var_names = |
| 1649 |
|
| $self->parse_init_vars( |
| 1650 |
|
| code => $opt{code}, |
| 1651 |
|
| params => 1, |
| 1652 |
|
| private => 1, |
| 1653 |
|
| type => 'has', |
| 1654 |
|
| ignore_delim => { |
| 1655 |
|
| '{' => 1, |
| 1656 |
|
| '<' => 1, |
| 1657 |
|
| }, |
| 1658 |
|
| ); |
| 1659 |
|
| |
| 1660 |
|
| # Set the class parameters |
| 1661 |
|
| $obj->{vars} = $var_names; |
| 1662 |
|
| |
| 1663 |
|
| # Class inheritance (class Name(...) << Name1, Name2) |
| 1664 |
|
| if (/\G\h*<<?\h*/gc) { |
| 1665 |
|
| while (/\G($self->{var_name_re})\h*/gco) { |
| 1666 |
|
| my ($name, $class_name) = $self->get_name_and_class($1); |
| 1667 |
|
| if (defined(my $class = $self->find_var($name, $class_name))) { |
| 1668 |
|
| if ($class->{type} eq 'class') { |
| 1669 |
|
| |
| 1670 |
|
| # Detect inheritance from the same class |
| 1671 |
|
| if (refaddr($obj) == refaddr($class->{obj})) { |
| 1672 |
|
| $self->fatal_error( |
| 1673 |
|
| error => "Inheriting from the same class is not allowed", |
| 1674 |
|
| code => $_, |
| 1675 |
|
| pos => pos($_) - length($name) - 1, |
| 1676 |
|
| ); |
| 1677 |
|
| } |
| 1678 |
|
| |
| 1679 |
|
| ++$class->{count}; |
| 1680 |
|
| push @{$obj->{inherit}}, $class->{obj}; |
| 1681 |
|
| } |
| 1682 |
|
| else { |
| 1683 |
|
| $self->fatal_error( |
| 1684 |
|
| error => "this is not a class", |
| 1685 |
|
| reason => "expected a class name", |
| 1686 |
|
| code => $_, |
| 1687 |
|
| pos => pos($_) - length($name) - 1, |
| 1688 |
|
| ); |
| 1689 |
|
| } |
| 1690 |
|
| } |
| 1691 |
|
| elsif (exists $self->{built_in_classes}{$name}) { |
| 1692 |
|
| $self->fatal_error( |
| 1693 |
|
| error => "Inheriting from built-in classes is not supported", |
| 1694 |
|
| reason => "`$name` is a built-in class", |
| 1695 |
|
| code => $_, |
| 1696 |
|
| pos => pos($_) - length($name) - 1, |
| 1697 |
|
| ); |
| 1698 |
|
| } |
| 1699 |
|
| else { |
| 1700 |
|
| $self->fatal_error( |
| 1701 |
|
| error => "can't find `$name` class", |
| 1702 |
|
| reason => "expected an existent class name", |
| 1703 |
|
| var => ($class_name . '::' . $name), |
| 1704 |
|
| code => $_, |
| 1705 |
|
| pos => pos($_) - length($name) - 1, |
| 1706 |
|
| ); |
| 1707 |
|
| } |
| 1708 |
|
| |
| 1709 |
|
| /\G,\h*/gc; |
| 1710 |
|
| } |
| 1711 |
|
| } |
| 1712 |
|
| |
| 1713 |
|
| /\G\h*(?=\{)/gc |
| 1714 |
|
| || $self->fatal_error( |
| 1715 |
|
| error => "invalid class declaration", |
| 1716 |
|
| reason => "expected: class $name(...){...}", |
| 1717 |
|
| code => $_, |
| 1718 |
|
| pos => pos($_) |
| 1719 |
|
| ); |
| 1720 |
|
| |
| 1721 |
|
| #~ if (ref($built_in_obj) eq 'Sidef::Variable::ClassInit') { |
| 1722 |
|
| #~ $obj->{name} = $built_in_obj->{name}; |
| 1723 |
|
| #~ } |
| 1724 |
|
| |
| 1725 |
|
| local $self->{class_name} = (defined($built_in_obj) ? ref($built_in_obj) : $obj->{name}); |
| 1726 |
|
| local $self->{current_class} = $built_in_obj // $obj; |
| 1727 |
|
| my $block = $self->parse_block(code => $opt{code}); |
| 1728 |
|
| |
| 1729 |
|
| # Set the block of the class |
| 1730 |
|
| $obj->{block} = $block; |
| 1731 |
|
| } |
| 1732 |
|
| |
| 1733 |
|
| if ($type eq 'func' or $type eq 'method') { |
| 1734 |
|
| |
| 1735 |
|
| my $var_names = do { |
| 1736 |
|
| local $self->{allow_class_variable} = 1 if $type eq 'method'; |
| 1737 |
|
| $self->get_init_vars( |
| 1738 |
|
| code => $opt{code}, |
| 1739 |
|
| with_vals => 1, |
| 1740 |
|
| ignore_delim => { |
| 1741 |
|
| '{' => 1, |
| 1742 |
|
| '-' => 1, |
| 1743 |
|
| } |
| 1744 |
|
| ); |
| 1745 |
|
| }; |
| 1746 |
|
| |
| 1747 |
|
| # Functions and method traits (example: "is cached") |
| 1748 |
|
| if (/\G\h*is\h+(?=\w)/gc) { |
| 1749 |
|
| while (/\G(\w+)/gc) { |
| 1750 |
|
| my $trait = $1; |
| 1751 |
|
| if ($trait eq 'cached') { |
| 1752 |
|
| $obj->{cached} = 1; |
| 1753 |
|
| } |
| 1754 |
|
| |
| 1755 |
|
| #elsif ($type eq 'method' and $trait eq 'exported') { |
| 1756 |
|
| # $obj->{exported} = 1; |
| 1757 |
|
| #} |
| 1758 |
|
| else { |
| 1759 |
|
| $self->fatal_error( |
| 1760 |
|
| error => "Unknown $type trait: $trait", |
| 1761 |
|
| code => $_, |
| 1762 |
|
| pos => pos($_), |
| 1763 |
|
| ); |
| 1764 |
|
| } |
| 1765 |
|
| |
| 1766 |
|
| /\G\h*,\h*/gc || last; |
| 1767 |
|
| } |
| 1768 |
|
| } |
| 1769 |
|
| |
| 1770 |
|
| # Function return type (func name(...) -> Type {...}) |
| 1771 |
|
| if (/\G\h*->\h*/gc) { |
| 1772 |
|
| |
| 1773 |
|
| my @ref; |
| 1774 |
|
| if (/\G\(/gc) { # multiple types |
| 1775 |
|
| while (1) { |
| 1776 |
|
| my ($ref) = $self->parse_expr(code => $opt{code}); |
| 1777 |
|
| push @ref, $ref; |
| 1778 |
|
| |
| 1779 |
|
| /\G\s*\)/gc && last; |
| 1780 |
|
| /\G\s*,\s*/gc |
| 1781 |
|
| || $self->fatal_error( |
| 1782 |
|
| error => "invalid return-type for $type $self->{class_name}<<$name>>", |
| 1783 |
|
| reason => "expected a comma", |
| 1784 |
|
| code => $_, |
| 1785 |
|
| pos => pos($_), |
| 1786 |
|
| ); |
| 1787 |
|
| } |
| 1788 |
|
| } |
| 1789 |
|
| else { # only one type |
| 1790 |
|
| my ($ref) = $self->parse_expr(code => $opt{code}); |
| 1791 |
|
| push @ref, $ref; |
| 1792 |
|
| } |
| 1793 |
|
| |
| 1794 |
|
| foreach my $ref (@ref) { |
| 1795 |
|
| if (ref($ref) eq 'HASH') { |
| 1796 |
|
| $self->fatal_error( |
| 1797 |
|
| error => "invalid return-type for $type $self->{class_name}<<$name>>", |
| 1798 |
|
| reason => "expected a valid type, such as: Str, Num, Arr, etc...", |
| 1799 |
|
| code => $_, |
| 1800 |
|
| pos => pos($_), |
| 1801 |
|
| ); |
| 1802 |
|
| } |
| 1803 |
|
| } |
| 1804 |
|
| |
| 1805 |
|
| $obj->{returns} = \@ref; |
| 1806 |
|
| } |
| 1807 |
|
| |
| 1808 |
|
| /\G\h*\{\h*/gc |
| 1809 |
|
| || $self->fatal_error( |
| 1810 |
|
| error => "invalid `$type` declaration", |
| 1811 |
|
| reason => "expected: $type $name(...){...}", |
| 1812 |
|
| code => $_, |
| 1813 |
|
| pos => pos($_) |
| 1814 |
|
| ); |
| 1815 |
|
| |
| 1816 |
|
| local $self->{$type eq 'func' ? 'current_function' : 'current_method'} = $has_kids ? $parent : $obj; |
| 1817 |
|
| my $args = '|' . join(',', $type eq 'method' ? 'self' : (), @{$var_names}) . ' |'; |
| 1818 |
|
| |
| 1819 |
|
| my $code = '{' . $args . substr($_, pos); |
| 1820 |
|
| my $block = $self->parse_block(code => \$code, with_vars => 1); |
| 1821 |
|
| pos($_) += pos($code) - length($args) - 1; |
| 1822 |
|
| |
| 1823 |
|
| # Set the block of the function/method |
| 1824 |
|
| $obj->{value} = $block; |
| 1825 |
|
| } |
| 1826 |
|
| |
| 1827 |
|
| return $obj; |
| 1828 |
|
| } |
| 1829 |
|
| |
| 1830 |
|
| # "given(expr) {...}" construct |
| 1831 |
|
| if (/\Ggiven\b\h*/gc) { |
| 1832 |
|
| my $expr = ( |
| 1833 |
|
| /\G(?=\()/ |
| 1834 |
|
| ? $self->parse_arg(code => $opt{code}) |
| 1835 |
|
| : $self->parse_obj(code => $opt{code}) |
| 1836 |
|
| ); |
| 1837 |
|
| |
| 1838 |
|
| $expr // $self->fatal_error( |
| 1839 |
|
| error => "invalid declaration of the `given/when` construct", |
| 1840 |
|
| reason => "expected `given(expr) {...}`", |
| 1841 |
|
| code => $_, |
| 1842 |
|
| pos => pos($_), |
| 1843 |
|
| ); |
| 1844 |
|
| |
| 1845 |
|
| my $given_obj = bless({expr => $expr}, 'Sidef::Types::Block::Given'); |
| 1846 |
|
| local $self->{current_given} = $given_obj; |
| 1847 |
|
| my $block = ( |
| 1848 |
|
| /\G\h*(?=\{)/gc |
| 1849 |
|
| ? $self->parse_block(code => $opt{code}, topic_var => 1) |
| 1850 |
|
| : $self->fatal_error( |
| 1851 |
|
| error => "expected a block after `given(expr)`", |
| 1852 |
|
| code => $_, |
| 1853 |
|
| pos => pos($_), |
| 1854 |
|
| ) |
| 1855 |
|
| ); |
| 1856 |
|
| |
| 1857 |
|
| $given_obj->{block} = $block; |
| 1858 |
|
| |
| 1859 |
|
| return $given_obj; |
| 1860 |
|
| } |
| 1861 |
|
| |
| 1862 |
|
| # "when(expr) {...}" construct |
| 1863 |
|
| if (exists($self->{current_given}) && /\Gwhen\b\h*/gc) { |
| 1864 |
|
| my $expr = ( |
| 1865 |
|
| /\G(?=\()/ |
| 1866 |
|
| ? $self->parse_arg(code => $opt{code}) |
| 1867 |
|
| : $self->parse_obj(code => $opt{code}) |
| 1868 |
|
| ); |
| 1869 |
|
| |
| 1870 |
|
| $expr // $self->fatal_error( |
| 1871 |
|
| error => "invalid declaration of the `when` construct", |
| 1872 |
|
| reason => "expected `when(expr) {...}`", |
| 1873 |
|
| code => $_, |
| 1874 |
|
| pos => pos($_), |
| 1875 |
|
| ); |
| 1876 |
|
| |
| 1877 |
|
| my $block = ( |
| 1878 |
|
| /\G\h*(?=\{)/gc |
| 1879 |
|
| ? $self->parse_block(code => $opt{code}, with_vars => 1) |
| 1880 |
|
| : $self->fatal_error( |
| 1881 |
|
| error => "expected a block after `when(expr)`", |
| 1882 |
|
| code => $_, |
| 1883 |
|
| pos => pos($_), |
| 1884 |
|
| ) |
| 1885 |
|
| ); |
| 1886 |
|
| |
| 1887 |
|
| return bless({expr => $expr, block => $block}, 'Sidef::Types::Block::When'); |
| 1888 |
|
| } |
| 1889 |
|
| |
| 1890 |
|
| # "case(expr) {...}" construct |
| 1891 |
|
| if (exists($self->{current_given}) && /\Gcase\b\h*/gc) { |
| 1892 |
|
| my $expr = ( |
| 1893 |
|
| /\G(?=\()/ |
| 1894 |
|
| ? $self->parse_arg(code => $opt{code}) |
| 1895 |
|
| : $self->parse_obj(code => $opt{code}) |
| 1896 |
|
| ); |
| 1897 |
|
| |
| 1898 |
|
| $expr // $self->fatal_error( |
| 1899 |
|
| error => "invalid declaration of the `case` construct", |
| 1900 |
|
| reason => "expected `case(expr) {...}`", |
| 1901 |
|
| code => $_, |
| 1902 |
|
| pos => pos($_), |
| 1903 |
|
| ); |
| 1904 |
|
| |
| 1905 |
|
| my $block = ( |
| 1906 |
|
| /\G\h*(?=\{)/gc |
| 1907 |
|
| ? $self->parse_block(code => $opt{code}, with_vars => 1) |
| 1908 |
|
| : $self->fatal_error( |
| 1909 |
|
| error => "expected a block after `case(expr)`", |
| 1910 |
|
| code => $_, |
| 1911 |
|
| pos => pos($_), |
| 1912 |
|
| ) |
| 1913 |
|
| ); |
| 1914 |
|
| |
| 1915 |
|
| return bless({expr => $expr, block => $block}, 'Sidef::Types::Block::Case'); |
| 1916 |
|
| } |
| 1917 |
|
| |
| 1918 |
|
| # "default {...}" or "else { ... }" construct for `given/when` |
| 1919 |
|
| if (exists($self->{current_given}) && /\G(?:default|else)\h*(?=\{)/gc) { |
| 1920 |
|
| my $block = $self->parse_block(code => $opt{code}); |
| 1921 |
|
| return bless({block => $block}, 'Sidef::Types::Block::Default'); |
| 1922 |
|
| } |
| 1923 |
|
| |
| 1924 |
|
| # `continue` keyword inside a given/when construct |
| 1925 |
|
| if (exists($self->{current_given}) && /\Gcontinue\b/gc) { |
| 1926 |
|
| state $x = bless({}, 'Sidef::Types::Block::Continue'); |
| 1927 |
|
| return $x; |
| 1928 |
|
| } |
| 1929 |
|
| |
| 1930 |
|
| # "do {...}" construct |
| 1931 |
|
| if (/\Gdo\h*(?=\{)/gc) { |
| 1932 |
|
| my $block = $self->parse_block(code => $opt{code}); |
| 1933 |
|
| return bless({block => $block}, 'Sidef::Types::Block::Do'); |
| 1934 |
|
| } |
| 1935 |
|
| |
| 1936 |
|
| # "loop {...}" construct |
| 1937 |
|
| if (/\Gloop\h*(?=\{)/gc) { |
| 1938 |
|
| my $block = $self->parse_block(code => $opt{code}); |
| 1939 |
|
| return bless({block => $block}, 'Sidef::Types::Block::Loop'); |
| 1940 |
|
| } |
| 1941 |
|
| |
| 1942 |
|
| # "try/catch" construct |
| 1943 |
|
| if (/\Gtry\h*(?=\{)/gc) { |
| 1944 |
|
| my $try_block = $self->parse_block(code => $opt{code}); |
| 1945 |
|
| my $obj = bless({try => $try_block}, 'Sidef::Types::Block::Try'); |
| 1946 |
|
| |
| 1947 |
|
| $self->parse_whitespace(code => $opt{code}); |
| 1948 |
|
| |
| 1949 |
|
| if (/\Gcatch\h*(?=\{)/gc) { |
| 1950 |
|
| $obj->{catch} = $self->parse_block(code => $opt{code}, with_vars => 1); |
| 1951 |
|
| } |
| 1952 |
|
| else { |
| 1953 |
|
| $self->backtrack_whitespace(code => $opt{code}); |
| 1954 |
|
| } |
| 1955 |
|
| |
| 1956 |
|
| return $obj; |
| 1957 |
|
| } |
| 1958 |
|
| |
| 1959 |
|
| # "gather/take" construct |
| 1960 |
|
| if (/\Ggather\h*(?=\{)/gc) { |
| 1961 |
|
| my $obj = bless({}, 'Sidef::Types::Block::Gather'); |
| 1962 |
|
| |
| 1963 |
|
| local $self->{current_gather} = $obj; |
| 1964 |
|
| |
| 1965 |
|
| my $block = $self->parse_block(code => $opt{code}); |
| 1966 |
|
| $obj->{block} = $block; |
| 1967 |
|
| |
| 1968 |
|
| return $obj; |
| 1969 |
|
| } |
| 1970 |
|
| |
| 1971 |
|
| if (exists($self->{current_gather}) and /\Gtake\b\h*/gc) { |
| 1972 |
|
| |
| 1973 |
|
| my $obj = ( |
| 1974 |
|
| /\G(?=\()/ |
| 1975 |
|
| ? $self->parse_arg(code => $opt{code}) |
| 1976 |
|
| : $self->parse_obj(code => $opt{code}) |
| 1977 |
|
| ); |
| 1978 |
|
| |
| 1979 |
|
| return bless({expr => $obj, gather => $self->{current_gather}}, 'Sidef::Types::Block::Take'); |
| 1980 |
|
| } |
| 1981 |
|
| |
| 1982 |
|
| # Declaration of a module |
| 1983 |
|
| if (/\Gmodule\b\h*/gc) { |
| 1984 |
|
| my $name = |
| 1985 |
|
| /\G($self->{var_name_re})\h*/goc |
| 1986 |
|
| ? $1 |
| 1987 |
|
| : $self->fatal_error( |
| 1988 |
|
| error => "invalid module declaration", |
| 1989 |
|
| reason => "expected a name", |
| 1990 |
|
| code => $_, |
| 1991 |
|
| pos => pos($_) |
| 1992 |
|
| ); |
| 1993 |
|
| |
| 1994 |
|
| $self->parse_whitespace(code => $opt{code}); |
| 1995 |
|
| |
| 1996 |
|
| if (/\G(?=\{)/) { |
| 1997 |
|
| my $prev_class = $self->{class}; |
| 1998 |
|
| local $self->{class} = $name; |
| 1999 |
|
| my $obj = $self->parse_block(code => $opt{code}, is_module => 1, prev_class => $prev_class); |
| 2000 |
|
| |
| 2001 |
|
| return |
| 2002 |
|
| bless { |
| 2003 |
|
| name => $name, |
| 2004 |
|
| block => $obj |
| 2005 |
|
| }, |
| 2006 |
|
| 'Sidef::Meta::Module'; |
| 2007 |
|
| } |
| 2008 |
|
| else { |
| 2009 |
|
| $self->fatal_error( |
| 2010 |
|
| error => "invalid module declaration", |
| 2011 |
|
| reason => "expected: module $name {...}", |
| 2012 |
|
| code => $_, |
| 2013 |
|
| pos => pos($_) |
| 2014 |
|
| ); |
| 2015 |
|
| } |
| 2016 |
|
| } |
| 2017 |
|
| |
| 2018 |
|
| if (/\Gimport\b\h*/gc) { |
| 2019 |
|
| |
| 2020 |
|
| my $import_pos = pos($_); |
| 2021 |
|
| |
| 2022 |
|
| my $var_names = |
| 2023 |
|
| $self->get_init_vars(code => $opt{code}, |
| 2024 |
|
| with_vals => 0); |
| 2025 |
|
| |
| 2026 |
|
| $self->backtrack_whitespace(code => $opt{code}); |
| 2027 |
|
| |
| 2028 |
|
| @{$var_names} |
| 2029 |
|
| || $self->fatal_error( |
| 2030 |
|
| code => $_, |
| 2031 |
|
| pos => $import_pos, |
| 2032 |
|
| error => "expected a variable-like name for importing!", |
| 2033 |
|
| ); |
| 2034 |
|
| |
| 2035 |
|
| foreach my $var_name (@{$var_names}) { |
| 2036 |
|
| my ($name, $class) = $self->get_name_and_class($var_name); |
| 2037 |
|
| |
| 2038 |
|
| if ($class eq ($self->{class})) { |
| 2039 |
|
| $self->fatal_error( |
| 2040 |
|
| code => $_, |
| 2041 |
|
| pos => $import_pos, |
| 2042 |
|
| error => "can't import '${class}::${name}' into the same namespace", |
| 2043 |
|
| ); |
| 2044 |
|
| } |
| 2045 |
|
| |
| 2046 |
|
| my $var = $self->find_var($name, $class); |
| 2047 |
|
| |
| 2048 |
|
| if (not defined $var) { |
| 2049 |
|
| $self->fatal_error( |
| 2050 |
|
| code => $_, |
| 2051 |
|
| pos => $import_pos, |
| 2052 |
|
| error => "variable '${class}::${name}' does not exists", |
| 2053 |
|
| ); |
| 2054 |
|
| } |
| 2055 |
|
| |
| 2056 |
|
| $var->{count}++; |
| 2057 |
|
| |
| 2058 |
|
| unshift @{$self->{vars}{$self->{class}}}, |
| 2059 |
|
| { |
| 2060 |
|
| obj => $var->{obj}, |
| 2061 |
|
| name => $name, |
| 2062 |
|
| count => 0, |
| 2063 |
|
| type => $var->{type}, |
| 2064 |
|
| line => $self->{line}, |
| 2065 |
|
| }; |
| 2066 |
|
| } |
| 2067 |
|
| |
| 2068 |
|
| return 1; |
| 2069 |
|
| } |
| 2070 |
|
| |
| 2071 |
|
| if (/\Ginclude\b\h*/gc) { |
| 2072 |
|
| |
| 2073 |
|
| my $include_pos = pos($_); |
| 2074 |
|
| |
| 2075 |
|
| state $x = do { |
| 2076 |
|
| require Cwd; |
| 2077 |
|
| require File::Spec; |
| 2078 |
|
| require File::Basename; |
| 2079 |
|
| }; |
| 2080 |
|
| |
| 2081 |
|
| if (@{$self->{inc}} == 0) { |
| 2082 |
|
| push @{$self->{inc}}, split(':', $ENV{SIDEF_INC}) if exists($ENV{SIDEF_INC}); |
| 2083 |
|
| |
| 2084 |
|
| push @{$self->{inc}}, File::Spec->catdir(File::Basename::dirname(Cwd::abs_path($0)), File::Spec->updir, 'share', 'sidef'); |
| 2085 |
|
| |
| 2086 |
|
| if (-f $self->{script_name}) { |
| 2087 |
|
| push @{$self->{inc}}, File::Basename::dirname(Cwd::abs_path($self->{script_name})); |
| 2088 |
|
| } |
| 2089 |
|
| |
| 2090 |
|
| push @{$self->{inc}}, File::Spec->curdir; |
| 2091 |
|
| } |
| 2092 |
|
| |
| 2093 |
|
| my @abs_filenames; |
| 2094 |
|
| if (/\G($self->{var_name_re})/gc) { |
| 2095 |
|
| my $var_name = $1; |
| 2096 |
|
| |
| 2097 |
|
| # The module is defined in the current file -- skip |
| 2098 |
|
| if (exists $self->{ref_vars}{$var_name}) { |
| 2099 |
|
| redo; |
| 2100 |
|
| } |
| 2101 |
|
| |
| 2102 |
|
| # The module was already included -- skip |
| 2103 |
|
| if (exists $Sidef::INCLUDED{$var_name}) { |
| 2104 |
|
| redo; |
| 2105 |
|
| } |
| 2106 |
|
| |
| 2107 |
|
| my @path = split(/::/, $var_name); |
| 2108 |
|
| my $mod_path = File::Spec->catfile(@path[0 .. $#path - 1], $path[-1] . '.sm'); |
| 2109 |
|
| |
| 2110 |
|
| $Sidef::INCLUDED{$var_name} = $mod_path; |
| 2111 |
|
| |
| 2112 |
|
| my ($full_path, $found_module); |
| 2113 |
|
| foreach my $inc_dir (@{$self->{inc}}) { |
| 2114 |
|
| if ( -e ($full_path = File::Spec->catfile($inc_dir, $mod_path)) |
| 2115 |
|
| and -f _ |
| 2116 |
|
| and -r _ ) { |
| 2117 |
|
| $found_module = 1; |
| 2118 |
|
| last; |
| 2119 |
|
| } |
| 2120 |
|
| } |
| 2121 |
|
| |
| 2122 |
|
| $found_module // $self->fatal_error( |
| 2123 |
|
| code => $_, |
| 2124 |
|
| pos => $include_pos, |
| 2125 |
|
| error => "can't find the module '${mod_path}' anywhere in ['" . join("', '", @{$self->{inc}}) . "']", |
| 2126 |
|
| ); |
| 2127 |
|
| |
| 2128 |
|
| push @abs_filenames, [$full_path, $var_name]; |
| 2129 |
|
| } |
| 2130 |
|
| else { |
| 2131 |
|
| |
| 2132 |
|
| my $orig_dir = Cwd::getcwd(); |
| 2133 |
|
| my $orig_file = Cwd::abs_path($self->{file_name}); |
| 2134 |
|
| my $file_dir = File::Basename::dirname($orig_file); |
| 2135 |
|
| |
| 2136 |
|
| my $chdired = 0; |
| 2137 |
|
| if ($orig_dir ne $file_dir) { |
| 2138 |
|
| if (chdir($file_dir)) { |
| 2139 |
|
| $chdired = 1; |
| 2140 |
|
| } |
| 2141 |
|
| } |
| 2142 |
|
| |
| 2143 |
|
| my $expr = do { |
| 2144 |
|
| my ($obj) = $self->parse_expr(code => $opt{code}); |
| 2145 |
|
| $obj; |
| 2146 |
|
| }; |
| 2147 |
|
| |
| 2148 |
|
| my @files = ( |
| 2149 |
|
| ref($expr) eq 'HASH' |
| 2150 |
|
| ? do { |
| 2151 |
|
| map { $_->{self} } |
| 2152 |
|
| map { @{$_->{self}->{$self->{class}}} } |
| 2153 |
|
| map { @{$expr->{$_}} } |
| 2154 |
|
| keys %{$expr}; |
| 2155 |
|
| } |
| 2156 |
|
| : $expr |
| 2157 |
|
| ); |
| 2158 |
|
| |
| 2159 |
|
| push @abs_filenames, map { |
| 2160 |
|
| my $filename = $_; |
| 2161 |
|
| |
| 2162 |
|
| if (index(ref($filename), 'Sidef::') == 0) { |
| 2163 |
|
| $filename = $filename->get_value; |
| 2164 |
|
| } |
| 2165 |
|
| |
| 2166 |
|
| ref($filename) ne '' |
| 2167 |
|
| and $self->fatal_error( |
| 2168 |
|
| code => ${$opt{code}}, |
| 2169 |
|
| pos => $include_pos, |
| 2170 |
|
| error => 'include-error: invalid value of type "' . ref($filename) . '" (expected a string)', |
| 2171 |
|
| ); |
| 2172 |
|
| |
| 2173 |
|
| my @files; |
| 2174 |
|
| foreach my $file (glob($filename)) { |
| 2175 |
|
| |
| 2176 |
|
| my $resolved = 0; |
| 2177 |
|
| foreach my $base ('', @{$self->{inc}}) { |
| 2178 |
|
| my $abs_path = File::Spec->rel2abs($file, $base) // next; |
| 2179 |
|
| |
| 2180 |
|
| if (-f $abs_path) { |
| 2181 |
|
| push @files, $abs_path; |
| 2182 |
|
| $resolved = 1; |
| 2183 |
|
| last; |
| 2184 |
|
| } |
| 2185 |
|
| } |
| 2186 |
|
| |
| 2187 |
|
| if (!$resolved) { |
| 2188 |
|
| $self->fatal_error( |
| 2189 |
|
| code => ${$opt{code}}, |
| 2190 |
|
| pos => $include_pos, |
| 2191 |
|
| error => "include-error: could not resolve path to file `$file`", |
| 2192 |
|
| ); |
| 2193 |
|
| } |
| 2194 |
|
| } |
| 2195 |
|
| |
| 2196 |
|
| foreach my $file (@files) { |
| 2197 |
|
| if (exists $Sidef::INCLUDED{$file}) { |
| 2198 |
|
| $self->fatal_error( |
| 2199 |
|
| code => ${$opt{code}}, |
| 2200 |
|
| pos => $include_pos, |
| 2201 |
|
| error => "include-error: circular inclusion of file `$file`", |
| 2202 |
|
| ); |
| 2203 |
|
| } |
| 2204 |
|
| } |
| 2205 |
|
| |
| 2206 |
|
| map { [$_] } @files |
| 2207 |
|
| } @files; |
| 2208 |
|
| |
| 2209 |
|
| if ($chdired) { chdir($orig_dir) } |
| 2210 |
|
| } |
| 2211 |
|
| |
| 2212 |
|
| my @included; |
| 2213 |
|
| |
| 2214 |
|
| foreach my $pair (@abs_filenames) { |
| 2215 |
|
| |
| 2216 |
|
| my ($full_path, $name) = @{$pair}; |
| 2217 |
|
| |
| 2218 |
|
| open(my $fh, '<:utf8', $full_path) |
| 2219 |
|
| || $self->fatal_error( |
| 2220 |
|
| code => ${$opt{code}}, |
| 2221 |
|
| pos => $include_pos, |
| 2222 |
|
| error => "can't open file `$full_path`: $!" |
| 2223 |
|
| ); |
| 2224 |
|
| |
| 2225 |
|
| my $content = do { local $/; <$fh> }; |
| 2226 |
|
| close $fh; |
| 2227 |
|
| |
| 2228 |
|
| next if $Sidef::INCLUDED{$full_path}; |
| 2229 |
|
| |
| 2230 |
|
| local $self->{class} = $name if defined $name; # new namespace |
| 2231 |
|
| local $self->{line} = 1; |
| 2232 |
|
| local $self->{file_name} = $full_path; |
| 2233 |
|
| local $Sidef::INCLUDED{$full_path} = 1; |
| 2234 |
|
| |
| 2235 |
|
| my $ast = $self->parse_script(code => \$content); |
| 2236 |
|
| |
| 2237 |
|
| push @included, |
| 2238 |
|
| { |
| 2239 |
|
| name => $name, |
| 2240 |
|
| file => $full_path, |
| 2241 |
|
| ast => $ast, |
| 2242 |
|
| }; |
| 2243 |
|
| } |
| 2244 |
|
| |
| 2245 |
|
| return bless({included => \@included}, 'Sidef::Meta::Included'); |
| 2246 |
|
| } |
| 2247 |
|
| |
| 2248 |
|
| # Super-script power |
| 2249 |
|
| if (/\G([⁰¹²³⁴⁵⁶⁷⁸⁹]+)/gc) { |
| 2250 |
|
| my $num = ($1 =~ tr/⁰¹²³⁴⁵⁶⁷⁸⁹/0-9/r); |
| 2251 |
|
| return Sidef::Types::Number::Number::_set_int($num); |
| 2252 |
|
| } |
| 2253 |
|
| |
| 2254 |
|
| # Binary, hexadecimal and octal numbers |
| 2255 |
|
| if (/\G0(b[10_]*|x[0-9A-Fa-f_]*|o[0-7_]*|[0-7_]+)\b/gc) { |
| 2256 |
|
| my $num = $1 =~ tr/_//dr; |
| 2257 |
|
| return |
| 2258 |
|
| Sidef::Types::Number::Number->new( |
| 2259 |
|
| $num =~ /^b/ ? (substr($num, 1) || 0, 2) |
| 2260 |
|
| : $num =~ /^o/ ? (substr($num, 1) || 0, 8) |
| 2261 |
|
| : $num =~ /^x/ ? (substr($num, 1) || 0, 16) |
| 2262 |
|
| : ($num || 0, 8) |
| 2263 |
|
| ); |
| 2264 |
|
| } |
| 2265 |
|
| |
| 2266 |
|
| # Integer or float number |
| 2267 |
|
| if (/\G((?=\.?[0-9])[0-9_]*+(?:\.[0-9_]++)?(?:[Ee](?:[+-]?+[0-9_]+))?)/gc) { |
| 2268 |
|
| my $num = $1 =~ tr/_//dr; |
| 2269 |
|
| |
| 2270 |
|
| if (/\Gi\b/gc) { # imaginary |
| 2271 |
|
| return Sidef::Types::Number::Complex->new(0, $num); |
| 2272 |
|
| } |
| 2273 |
|
| elsif (/\Gf\b/gc) { # floating-point |
| 2274 |
|
| return Sidef::Types::Number::Number::_set_str('float', $num); |
| 2275 |
|
| } |
| 2276 |
|
| |
| 2277 |
|
| return ( |
| 2278 |
|
| $num =~ /^-?[0-9]+\z/ |
| 2279 |
|
| ? Sidef::Types::Number::Number::_set_int($num) |
| 2280 |
|
| : Sidef::Types::Number::Number->new($num) |
| 2281 |
|
| ); |
| 2282 |
|
| } |
| 2283 |
|
| |
| 2284 |
|
| # Prefix `...` |
| 2285 |
|
| if (/\G\.\.\./gc) { |
| 2286 |
|
| return |
| 2287 |
|
| bless( |
| 2288 |
|
| { |
| 2289 |
|
| line => $self->{line}, |
| 2290 |
|
| file => $self->{file_name}, |
| 2291 |
|
| }, |
| 2292 |
|
| 'Sidef::Meta::Unimplemented' |
| 2293 |
|
| ); |
| 2294 |
|
| } |
| 2295 |
|
| |
| 2296 |
|
| # Implicit method call on special variable: "_" |
| 2297 |
|
| if (/\G\./) { |
| 2298 |
|
| |
| 2299 |
|
| if (defined(my $var = $self->find_var('_', $self->{class}))) { |
| 2300 |
|
| $var->{count}++; |
| 2301 |
|
| return $var->{obj}; |
| 2302 |
|
| } |
| 2303 |
|
| |
| 2304 |
|
| $self->fatal_error( |
| 2305 |
|
| code => $_, |
| 2306 |
|
| pos => pos($_), |
| 2307 |
|
| error => q{attempt of using an implicit method call on an inexistent "_" variable}, |
| 2308 |
|
| ); |
| 2309 |
|
| } |
| 2310 |
|
| |
| 2311 |
|
| # Quoted words, numbers, vectors and matrices |
| 2312 |
|
| # %w(...), %i(...), %n(...), %v(...), %m(...), «...», <...> |
| 2313 |
|
| if (/\G%([wWinvm])\b/gc || /\G(?=(«|<(?!<)))/) { |
| 2314 |
|
| my ($type) = $1; |
| 2315 |
|
| my $strings = $self->get_quoted_words(code => $opt{code}); |
| 2316 |
|
| |
| 2317 |
|
| if ($type eq 'w' or $type eq '<') { |
| 2318 |
|
| my @list = map { Sidef::Types::String::String->new(s{\\(?=[\\#\s])}{}gr) } @{$strings}; |
| 2319 |
|
| return Sidef::Types::Array::Array->new(\@list); |
| 2320 |
|
| } |
| 2321 |
|
| |
| 2322 |
|
| if ($type eq 'i') { |
| 2323 |
|
| my @list = map { Sidef::Types::Number::Number->new(s{\\(?=[\\#\s])}{}gr)->int } |
| 2324 |
|
| split(/[\s,]+/, join(' ', @$strings)); |
| 2325 |
|
| return Sidef::Types::Array::Array->new(\@list); |
| 2326 |
|
| } |
| 2327 |
|
| |
| 2328 |
|
| if ($type eq 'n') { |
| 2329 |
|
| my @list = |
| 2330 |
|
| map { Sidef::Types::Number::Number->new(s{\\(?=[\\#\s])}{}gr) } split(/[\s,]+/, join(' ', @$strings)); |
| 2331 |
|
| return Sidef::Types::Array::Array->new(\@list); |
| 2332 |
|
| } |
| 2333 |
|
| |
| 2334 |
|
| if ($type eq 'v') { |
| 2335 |
|
| my @list = |
| 2336 |
|
| map { Sidef::Types::Number::Number->new(s{\\(?=[\\#\s])}{}gr) } split(/[\s,]+/, join(' ', @$strings)); |
| 2337 |
|
| return Sidef::Types::Array::Vector->new(@list); # this must be passed as a list |
| 2338 |
|
| } |
| 2339 |
|
| |
| 2340 |
|
| if ($type eq 'm') { |
| 2341 |
|
| my @matrix; |
| 2342 |
|
| my $data = join(' ', @$strings); |
| 2343 |
|
| foreach my $line (split(/\s*;\s*/, $data)) { |
| 2344 |
|
| my @row = map { Sidef::Types::Number::Number->new(s{\\(?=[\\#\s])}{}gr) } split(/[\s,]+/, $line); |
| 2345 |
|
| push @matrix, Sidef::Types::Array::Array->new(\@row); |
| 2346 |
|
| } |
| 2347 |
|
| return Sidef::Types::Array::Matrix->new(@matrix); |
| 2348 |
|
| } |
| 2349 |
|
| |
| 2350 |
|
| my ($inline_expression, @objs); |
| 2351 |
|
| foreach my $item (@{$strings}) { |
| 2352 |
|
| my $str = Sidef::Types::String::String->new($item)->apply_escapes($self); |
| 2353 |
|
| $inline_expression ||= ref($str) eq 'HASH'; |
| 2354 |
|
| push @objs, $str; |
| 2355 |
|
| } |
| 2356 |
|
| |
| 2357 |
|
| return ( |
| 2358 |
|
| $inline_expression |
| 2359 |
|
| ? bless([map { {self => $_} } @objs], 'Sidef::Types::Array::HCArray') |
| 2360 |
|
| : Sidef::Types::Array::Array->new(\@objs) |
| 2361 |
|
| ); |
| 2362 |
|
| } |
| 2363 |
|
| |
| 2364 |
|
| # Prefix method call (`::name(...)` or `::name ...`) |
| 2365 |
|
| if (/\G::($self->{var_name_re})\h*/goc) { |
| 2366 |
|
| my $name = $1; |
| 2367 |
|
| |
| 2368 |
|
| my $pos = pos($_); |
| 2369 |
|
| my $arg = ( |
| 2370 |
|
| /\G(?=\()/ |
| 2371 |
|
| ? $self->parse_arg(code => $opt{code}) |
| 2372 |
|
| : $self->parse_obj(code => $opt{code}) |
| 2373 |
|
| ); |
| 2374 |
|
| |
| 2375 |
|
| if (not exists($arg->{$self->{class}})) { |
| 2376 |
|
| $self->fatal_error( |
| 2377 |
|
| code => $_, |
| 2378 |
|
| pos => ($pos - length($name)), |
| 2379 |
|
| var => $name, |
| 2380 |
|
| error => "attempt to call method <$name> on an undefined value", |
| 2381 |
|
| ); |
| 2382 |
|
| } |
| 2383 |
|
| |
| 2384 |
|
| return |
| 2385 |
|
| bless { |
| 2386 |
|
| name => $name, |
| 2387 |
|
| expr => $arg, |
| 2388 |
|
| }, |
| 2389 |
|
| 'Sidef::Meta::PrefixMethod'; |
| 2390 |
|
| } |
| 2391 |
|
| |
| 2392 |
|
| if (/($self->{prefix_obj_re})\h*/goc) { |
| 2393 |
|
| return ($^R, 1, $1); |
| 2394 |
|
| } |
| 2395 |
|
| |
| 2396 |
|
| # Assertions |
| 2397 |
|
| if (/\G(assert(?:_(?:eq|ne))?+)\b\h*/gc) { |
| 2398 |
|
| my $action = $1; |
| 2399 |
|
| |
| 2400 |
|
| my $arg = ( |
| 2401 |
|
| /\G(?=\()/ |
| 2402 |
|
| ? $self->parse_arg(code => $opt{code}) |
| 2403 |
|
| : $self->parse_obj(code => $opt{code}) |
| 2404 |
|
| ); |
| 2405 |
|
| |
| 2406 |
|
| return |
| 2407 |
|
| bless( |
| 2408 |
|
| { |
| 2409 |
|
| arg => $arg, |
| 2410 |
|
| act => $action, |
| 2411 |
|
| line => $self->{line}, |
| 2412 |
|
| file => $self->{file_name}, |
| 2413 |
|
| }, |
| 2414 |
|
| 'Sidef::Meta::Assert' |
| 2415 |
|
| ); |
| 2416 |
|
| } |
| 2417 |
|
| |
| 2418 |
|
| # die/warn |
| 2419 |
|
| if (/\G(die|warn)\b\h*/gc) { |
| 2420 |
|
| my $action = $1; |
| 2421 |
|
| |
| 2422 |
|
| my $arg = ( |
| 2423 |
|
| /\G(?=\()/ |
| 2424 |
|
| ? $self->parse_arg(code => $opt{code}) |
| 2425 |
|
| : $self->parse_obj(code => $opt{code}) |
| 2426 |
|
| ); |
| 2427 |
|
| |
| 2428 |
|
| return |
| 2429 |
|
| bless( |
| 2430 |
|
| { |
| 2431 |
|
| arg => $arg, |
| 2432 |
|
| line => $self->{line}, |
| 2433 |
|
| file => $self->{file_name}, |
| 2434 |
|
| }, |
| 2435 |
|
| $action eq 'die' |
| 2436 |
|
| ? "Sidef::Meta::Error" |
| 2437 |
|
| : "Sidef::Meta::Warning" |
| 2438 |
|
| ); |
| 2439 |
|
| } |
| 2440 |
|
| |
| 2441 |
|
| # Eval keyword |
| 2442 |
|
| if (/\Geval\b\h*/gc) { |
| 2443 |
|
| |
| 2444 |
|
| my $obj = ( |
| 2445 |
|
| /\G(?=\()/ |
| 2446 |
|
| ? $self->parse_arg(code => $opt{code}) |
| 2447 |
|
| : $self->parse_obj(code => $opt{code}) |
| 2448 |
|
| ); |
| 2449 |
|
| |
| 2450 |
|
| #<<< |
| 2451 |
|
| return bless( |
| 2452 |
|
| { |
| 2453 |
|
| expr => $obj, |
| 2454 |
|
| parser => Sidef::Object::Object::dclone(scalar {%$self}), |
| 2455 |
|
| |
| 2456 |
|
| #vars => {$self->{class} => [@{$self->{vars}{$self->{class}}}]}, |
| 2457 |
|
| #ref_vars_refs => {$self->{class} => [@{$self->{ref_vars_refs}{$self->{class}}}]}, |
| 2458 |
|
| |
| 2459 |
|
| }, 'Sidef::Eval::Eval'); |
| 2460 |
|
| #>>> |
| 2461 |
|
| } |
| 2462 |
|
| |
| 2463 |
|
| if (/\GParser\b/gc) { |
| 2464 |
|
| return $self; |
| 2465 |
|
| } |
| 2466 |
|
| |
| 2467 |
|
| # Regular expression |
| 2468 |
|
| if (m{\G(?=/)} || /\G%r\b/gc) { |
| 2469 |
|
| my $string = $self->get_quoted_string(code => $opt{code}); |
| 2470 |
|
| return Sidef::Types::Regex::Regex->new($string, /\G($self->{match_flags_re})/goc ? $1 : undef); |
| 2471 |
|
| } |
| 2472 |
|
| |
| 2473 |
|
| # Class variable in form of `Class!var_name` |
| 2474 |
|
| if (/\G($self->{var_name_re})!($self->{var_name_re})/goc) { |
| 2475 |
|
| my ($class_name, $var_name) = ($1, $2); |
| 2476 |
|
| my $class_obj = $self->parse_expr(code => \$class_name); |
| 2477 |
|
| return (bless {class => $class_obj, name => $var_name}, 'Sidef::Variable::ClassVar'); |
| 2478 |
|
| } |
| 2479 |
|
| |
| 2480 |
|
| # Static object (like String or nil) |
| 2481 |
|
| if (/$self->{static_obj_re}/goc) { |
| 2482 |
|
| return $^R; |
| 2483 |
|
| } |
| 2484 |
|
| |
| 2485 |
|
| if (/\G__MAIN__\b/gc) { |
| 2486 |
|
| if (-e $self->{script_name}) { |
| 2487 |
|
| state $x = require Cwd; |
| 2488 |
|
| return Sidef::Types::String::String->new(Cwd::abs_path($self->{script_name})); |
| 2489 |
|
| } |
| 2490 |
|
| return Sidef::Types::String::String->new($self->{script_name}); |
| 2491 |
|
| } |
| 2492 |
|
| |
| 2493 |
|
| if (/\G__FILE__\b/gc) { |
| 2494 |
|
| if (-e $self->{file_name}) { |
| 2495 |
|
| state $x = require Cwd; |
| 2496 |
|
| return Sidef::Types::String::String->new(Cwd::abs_path($self->{file_name})); |
| 2497 |
|
| } |
| 2498 |
|
| return Sidef::Types::String::String->new($self->{file_name}); |
| 2499 |
|
| } |
| 2500 |
|
| |
| 2501 |
|
| if (/\G__DATE__\b/gc) { |
| 2502 |
|
| my (undef, undef, undef, $day, $mon, $year) = localtime; |
| 2503 |
|
| return Sidef::Types::String::String->new(join('-', $year + 1900, map { sprintf "%02d", $_ } $mon + 1, $day)); |
| 2504 |
|
| } |
| 2505 |
|
| |
| 2506 |
|
| if (/\G__TIME__\b/gc) { |
| 2507 |
|
| my ($sec, $min, $hour) = localtime; |
| 2508 |
|
| return Sidef::Types::String::String->new(join(':', map { sprintf "%02d", $_ } $hour, $min, $sec)); |
| 2509 |
|
| } |
| 2510 |
|
| |
| 2511 |
|
| if (/\G__LINE__\b/gc) { |
| 2512 |
|
| return Sidef::Types::Number::Number->new($self->{line}); |
| 2513 |
|
| } |
| 2514 |
|
| |
| 2515 |
|
| if (/\G__COMPILED__\b/gc) { |
| 2516 |
|
| return Sidef::Types::Bool::Bool->new($self->{opt}{R} eq 'Perl' or $self->{opt}{c}); |
| 2517 |
|
| } |
| 2518 |
|
| |
| 2519 |
|
| if (/\G__OPTIMIZED__\b/gc) { |
| 2520 |
|
| return Sidef::Types::Bool::Bool->new(($self->{opt}{O} || 0) > 0); |
| 2521 |
|
| } |
| 2522 |
|
| |
| 2523 |
|
| if (/\G__(?:END|DATA)__\b\h*+\R?/gc) { |
| 2524 |
|
| if (exists $self->{'__DATA__'}) { |
| 2525 |
|
| $self->{'__DATA__'} = substr($_, pos); |
| 2526 |
|
| } |
| 2527 |
|
| pos($_) = length($_); |
| 2528 |
|
| return; |
| 2529 |
|
| } |
| 2530 |
|
| |
| 2531 |
|
| if (/\GDATA\b/gc) { |
| 2532 |
|
| return ( |
| 2533 |
|
| $self->{static_objects}{'__DATA__'} //= do { |
| 2534 |
|
| bless({data => \$self->{'__DATA__'}}, 'Sidef::Meta::Glob::DATA'); |
| 2535 |
|
| } |
| 2536 |
|
| ); |
| 2537 |
|
| } |
| 2538 |
|
| |
| 2539 |
|
| # Beginning of a here-document (<<"EOT", <<'EOT', <<EOT) |
| 2540 |
|
| if (/\G<<(-)?+(?=\S)/gc) { |
| 2541 |
|
| my $indent = $1 ? 1 : 0; |
| 2542 |
|
| my ($name, $type) = (undef, 1); |
| 2543 |
|
| |
| 2544 |
|
| my $pos = pos($_); |
| 2545 |
|
| if (/\G(?=(['"„]))/) { |
| 2546 |
|
| $type = 0 if $1 eq q{'}; |
| 2547 |
|
| my $str = $self->get_quoted_string(code => $opt{code}); |
| 2548 |
|
| $name = $str; |
| 2549 |
|
| } |
| 2550 |
|
| elsif (/\G(\w+)/gc) { |
| 2551 |
|
| $name = $1; |
| 2552 |
|
| } |
| 2553 |
|
| else { |
| 2554 |
|
| $self->fatal_error( |
| 2555 |
|
| error => "invalid 'here-doc' declaration", |
| 2556 |
|
| reason => "expected an alpha-numeric token after '<<'", |
| 2557 |
|
| code => $_, |
| 2558 |
|
| pos => pos($_) |
| 2559 |
|
| ); |
| 2560 |
|
| } |
| 2561 |
|
| |
| 2562 |
|
| my $obj = {$self->{class} => []}; |
| 2563 |
|
| push @{$self->{EOT}}, |
| 2564 |
|
| { |
| 2565 |
|
| name => $name, |
| 2566 |
|
| indent => $indent, |
| 2567 |
|
| type => $type, |
| 2568 |
|
| obj => $obj, |
| 2569 |
|
| pos => $pos, |
| 2570 |
|
| line => $self->{line}, |
| 2571 |
|
| }; |
| 2572 |
|
| |
| 2573 |
|
| return $obj; |
| 2574 |
|
| } |
| 2575 |
|
| |
| 2576 |
|
| if (exists($self->{current_block}) && /\G__BLOCK__\b/gc) { |
| 2577 |
|
| return $self->{current_block}; |
| 2578 |
|
| } |
| 2579 |
|
| |
| 2580 |
|
| if (/\G__NAMESPACE__\b/gc) { |
| 2581 |
|
| return Sidef::Types::String::String->new($self->{class}); |
| 2582 |
|
| } |
| 2583 |
|
| |
| 2584 |
|
| if (exists($self->{current_function})) { |
| 2585 |
|
| /\G__FUNC__\b/gc && return $self->{current_function}; |
| 2586 |
|
| /\G__FUNC_NAME__\b/gc && return Sidef::Types::String::String->new($self->{current_function}{name}); |
| 2587 |
|
| } |
| 2588 |
|
| |
| 2589 |
|
| if (exists($self->{current_class})) { |
| 2590 |
|
| /\G__CLASS__\b/gc && return $self->{current_class}; |
| 2591 |
|
| /\G__CLASS_NAME__\b/gc && return Sidef::Types::String::String->new($self->{class_name}); |
| 2592 |
|
| } |
| 2593 |
|
| |
| 2594 |
|
| if (exists($self->{current_method})) { |
| 2595 |
|
| /\G__METHOD__\b/gc && return $self->{current_method}; |
| 2596 |
|
| /\G__METHOD_NAME__\b/gc && return Sidef::Types::String::String->new($self->{current_method}{name}); |
| 2597 |
|
| } |
| 2598 |
|
| |
| 2599 |
|
| # Variable access |
| 2600 |
|
| if (/\G($self->{var_name_re})/goc) { |
| 2601 |
|
| my $len_var = length($1); |
| 2602 |
|
| my ($name, $class) = $self->get_name_and_class($1); |
| 2603 |
|
| |
| 2604 |
|
| if (defined(my $var = $self->find_var($name, $class))) { |
| 2605 |
|
| |
| 2606 |
|
| if ($var->{type} eq 'del') { |
| 2607 |
|
| $self->fatal_error( |
| 2608 |
|
| code => $_, |
| 2609 |
|
| pos => (pos($_) - length($name)), |
| 2610 |
|
| var => ($class . '::' . $name), |
| 2611 |
|
| error => "attempt to use the deleted variable <$name>", |
| 2612 |
|
| ); |
| 2613 |
|
| } |
| 2614 |
|
| |
| 2615 |
|
| $var->{count}++; |
| 2616 |
|
| return $var->{obj}; |
| 2617 |
|
| } |
| 2618 |
|
| |
| 2619 |
|
| if ($name eq 'ARGV' or $name eq 'ENV') { |
| 2620 |
|
| |
| 2621 |
|
| my $type = 'var'; |
| 2622 |
|
| my $variable = bless({name => $name, type => $type, class => $class}, 'Sidef::Variable::Variable'); |
| 2623 |
|
| |
| 2624 |
|
| unshift @{$self->{vars}{$class}}, |
| 2625 |
|
| { |
| 2626 |
|
| obj => $variable, |
| 2627 |
|
| name => $name, |
| 2628 |
|
| count => 1, |
| 2629 |
|
| type => $type, |
| 2630 |
|
| line => $self->{line}, |
| 2631 |
|
| }; |
| 2632 |
|
| |
| 2633 |
|
| return $variable; |
| 2634 |
|
| } |
| 2635 |
|
| |
| 2636 |
|
| # Class instance variables |
| 2637 |
|
| if ( |
| 2638 |
|
| ref($self->{current_class}) eq 'Sidef::Variable::ClassInit' |
| 2639 |
|
| and defined( |
| 2640 |
|
| my $var = (first { $_->{name} eq $name } (@{$self->{current_class}{vars}}, map { @{$_->{vars}} } @{$self->{current_class}{attributes}})) |
| 2641 |
|
| ) |
| 2642 |
|
| ) { |
| 2643 |
|
| if (exists $self->{current_method}) { |
| 2644 |
|
| if (defined(my $var = $self->find_var('self', $class))) { |
| 2645 |
|
| |
| 2646 |
|
| if ($self->{opt}{k}) { |
| 2647 |
|
| print STDERR "[INFO] `$name` is parsed as `self.$name` at $self->{file_name} line $self->{line}\n"; |
| 2648 |
|
| } |
| 2649 |
|
| |
| 2650 |
|
| $var->{count}++; |
| 2651 |
|
| return |
| 2652 |
|
| scalar { |
| 2653 |
|
| $self->{class} => [ |
| 2654 |
|
| { |
| 2655 |
|
| self => $var->{obj}, |
| 2656 |
|
| ind => [{hash => [$name]}], |
| 2657 |
|
| } |
| 2658 |
|
| ] |
| 2659 |
|
| }; |
| 2660 |
|
| } |
| 2661 |
|
| } |
| 2662 |
|
| elsif (exists $self->{allow_class_variable}) { |
| 2663 |
|
| return $var; |
| 2664 |
|
| } |
| 2665 |
|
| elsif ($var->{type} eq 'has') { |
| 2666 |
|
| $self->fatal_error( |
| 2667 |
|
| error => "class variable <<$var->{name}>> can't be used outside a method", |
| 2668 |
|
| pos => (pos($_) - length($name)), |
| 2669 |
|
| var => $var, |
| 2670 |
|
| code => $_, |
| 2671 |
|
| ); |
| 2672 |
|
| } |
| 2673 |
|
| else { # this should not happen |
| 2674 |
|
| $self->fatal_error( |
| 2675 |
|
| error => "can't use undeclared variable <<$var->{name}>> in this context", |
| 2676 |
|
| pos => (pos($_) - length($name)), |
| 2677 |
|
| var => $var, |
| 2678 |
|
| code => $_, |
| 2679 |
|
| ); |
| 2680 |
|
| } |
| 2681 |
|
| } |
| 2682 |
|
| |
| 2683 |
|
| if (/\G(?=\h*:?=(?![=~>]))/) { |
| 2684 |
|
| |
| 2685 |
|
| if (not $self->{interactive}) { |
| 2686 |
|
| warn "[WARNING] Implicit declaration of global variable `$name`" . " at $self->{file_name} line $self->{line}\n"; |
| 2687 |
|
| } |
| 2688 |
|
| |
| 2689 |
|
| my $code = "global $name"; |
| 2690 |
|
| return $self->parse_expr(code => \$code); |
| 2691 |
|
| } |
| 2692 |
|
| |
| 2693 |
|
| # Method call in functional style (deprecated -- use `::name()` instead) |
| 2694 |
|
| if ($len_var == length($name)) { |
| 2695 |
|
| |
| 2696 |
|
| if ($self->{opt}{k}) { |
| 2697 |
|
| print STDERR "[INFO] `$name` is parsed as a prefix method-call at $self->{file_name} line $self->{line}\n"; |
| 2698 |
|
| } |
| 2699 |
|
| |
| 2700 |
|
| if ($self->{allow_class_variable}) { |
| 2701 |
|
| return $name; |
| 2702 |
|
| } |
| 2703 |
|
| |
| 2704 |
|
| my $pos = pos($_); |
| 2705 |
|
| my $arg = ( |
| 2706 |
|
| /\G\h*+(?=\()/gc |
| 2707 |
|
| ? $self->parse_arg(code => $opt{code}) |
| 2708 |
|
| : $self->fatal_error( |
| 2709 |
|
| code => $_, |
| 2710 |
|
| pos => ($pos - length($name)), |
| 2711 |
|
| var => ($class . '::' . $name), |
| 2712 |
|
| error => "variable <$name> is not declared in the current scope", |
| 2713 |
|
| ) |
| 2714 |
|
| ); |
| 2715 |
|
| |
| 2716 |
|
| if (not exists($arg->{$self->{class}})) { |
| 2717 |
|
| $self->fatal_error( |
| 2718 |
|
| code => $_, |
| 2719 |
|
| pos => ($pos - length($name)), |
| 2720 |
|
| var => $name, |
| 2721 |
|
| error => "attempt to call method <$name> on an undefined value", |
| 2722 |
|
| ); |
| 2723 |
|
| } |
| 2724 |
|
| |
| 2725 |
|
| return |
| 2726 |
|
| bless { |
| 2727 |
|
| name => $name, |
| 2728 |
|
| expr => $arg, |
| 2729 |
|
| }, |
| 2730 |
|
| 'Sidef::Meta::PrefixMethod'; |
| 2731 |
|
| } |
| 2732 |
|
| |
| 2733 |
|
| # Undeclared variable |
| 2734 |
|
| $self->fatal_error( |
| 2735 |
|
| code => $_, |
| 2736 |
|
| pos => (pos($_) - length($name)), |
| 2737 |
|
| var => ($class . '::' . $name), |
| 2738 |
|
| error => "variable <$class\::$name> is not declared in the current scope", |
| 2739 |
|
| ); |
| 2740 |
|
| } |
| 2741 |
|
| |
| 2742 |
|
| # Regex variables ($1, $2, ...) |
| 2743 |
|
| if (/\G\$([0-9]+)\b/gc) { |
| 2744 |
|
| $self->fatal_error( |
| 2745 |
|
| code => $_, |
| 2746 |
|
| pos => (pos($_) - length($1)), |
| 2747 |
|
| error => "regex capture-variables are not supported", |
| 2748 |
|
| ); |
| 2749 |
|
| } |
| 2750 |
|
| |
| 2751 |
|
| /\G\$/gc && redo; |
| 2752 |
|
| |
| 2753 |
|
| #warn "$self->{script_name}:$self->{line}: unexpected char: " . substr($_, pos($_), 1) . "\n"; |
| 2754 |
|
| #return undef, pos($_) + 1; |
| 2755 |
|
| |
| 2756 |
|
| return; |
| 2757 |
|
| } |
| 2758 |
|
| } |
| 2759 |
|
| |
| 2760 |
|
| sub parse_arg { |
| 2761 |
|
| my ($self, %opt) = @_; |
| 2762 |
|
| |
| 2763 |
|
| local *_ = $opt{code}; |
| 2764 |
|
| |
| 2765 |
|
| if (/\G\(/gc) { |
| 2766 |
|
| my $p = pos($_); |
| 2767 |
|
| local $self->{parentheses} = 1; |
| 2768 |
|
| my $obj = $self->parse_script(code => $opt{code}); |
| 2769 |
|
| |
| 2770 |
|
| $self->{parentheses} |
| 2771 |
|
| && $self->fatal_error( |
| 2772 |
|
| code => $_, |
| 2773 |
|
| pos => $p - 1, |
| 2774 |
|
| error => "unbalanced parenthesis", |
| 2775 |
|
| ); |
| 2776 |
|
| |
| 2777 |
|
| return $obj; |
| 2778 |
|
| } |
| 2779 |
|
| |
| 2780 |
|
| return; |
| 2781 |
|
| } |
| 2782 |
|
| |
| 2783 |
|
| sub parse_array { |
| 2784 |
|
| my ($self, %opt) = @_; |
| 2785 |
|
| |
| 2786 |
|
| local *_ = $opt{code}; |
| 2787 |
|
| |
| 2788 |
|
| if (/\G\[/gc) { |
| 2789 |
|
| my $p = pos($_); |
| 2790 |
|
| local $self->{right_brackets} = 1; |
| 2791 |
|
| my $obj = $self->parse_script(code => $opt{code}); |
| 2792 |
|
| |
| 2793 |
|
| $self->{right_brackets} |
| 2794 |
|
| && $self->fatal_error( |
| 2795 |
|
| code => $_, |
| 2796 |
|
| pos => $p - 1, |
| 2797 |
|
| error => "unbalanced right bracket", |
| 2798 |
|
| ); |
| 2799 |
|
| |
| 2800 |
|
| return $obj; |
| 2801 |
|
| } |
| 2802 |
|
| |
| 2803 |
|
| return; |
| 2804 |
|
| } |
| 2805 |
|
| |
| 2806 |
|
| sub parse_lookup { |
| 2807 |
|
| my ($self, %opt) = @_; |
| 2808 |
|
| |
| 2809 |
|
| local *_ = $opt{code}; |
| 2810 |
|
| |
| 2811 |
|
| if (/\G\{/gc) { |
| 2812 |
|
| my $p = pos($_); |
| 2813 |
|
| local $self->{curly_brackets} = 1; |
| 2814 |
|
| my $obj = $self->parse_script(code => $opt{code}); |
| 2815 |
|
| |
| 2816 |
|
| $self->{curly_brackets} |
| 2817 |
|
| && $self->fatal_error( |
| 2818 |
|
| code => $_, |
| 2819 |
|
| pos => $p - 1, |
| 2820 |
|
| error => "unbalanced curly bracket", |
| 2821 |
|
| ); |
| 2822 |
|
| |
| 2823 |
|
| return $obj; |
| 2824 |
|
| } |
| 2825 |
|
| |
| 2826 |
|
| return; |
| 2827 |
|
| } |
| 2828 |
|
| |
| 2829 |
|
| sub parse_block { |
| 2830 |
|
| my ($self, %opt) = @_; |
| 2831 |
|
| |
| 2832 |
|
| local *_ = $opt{code}; |
| 2833 |
|
| if (/\G\{/gc) { |
| 2834 |
|
| |
| 2835 |
|
| my $p = pos($_); |
| 2836 |
|
| local $self->{curly_brackets} = 1; |
| 2837 |
|
| |
| 2838 |
|
| my $class_name = $self->{class}; |
| 2839 |
|
| |
| 2840 |
|
| if ($opt{is_module}) { |
| 2841 |
|
| $class_name = $opt{prev_class}; |
| 2842 |
|
| } |
| 2843 |
|
| |
| 2844 |
|
| my $ref = $self->{vars}{$class_name} //= []; |
| 2845 |
|
| my $count = scalar(@{$self->{vars}{$class_name}}); |
| 2846 |
|
| |
| 2847 |
|
| unshift @{$self->{ref_vars_refs}{$class_name}}, @{$ref}; |
| 2848 |
|
| unshift @{$self->{vars}{$class_name}}, []; |
| 2849 |
|
| |
| 2850 |
|
| $self->{vars}{$class_name} = $self->{vars}{$class_name}[0]; |
| 2851 |
|
| |
| 2852 |
|
| my $block = bless({}, 'Sidef::Types::Block::BlockInit'); |
| 2853 |
|
| |
| 2854 |
|
| # Parse whitespace (if any) |
| 2855 |
|
| $self->parse_whitespace(code => $opt{code}); |
| 2856 |
|
| |
| 2857 |
|
| my $has_vars; |
| 2858 |
|
| my $var_objs = []; |
| 2859 |
|
| |
| 2860 |
|
| if (($opt{topic_var} || $opt{with_vars}) && /\G(?=\|)/) { |
| 2861 |
|
| $has_vars = 1; |
| 2862 |
|
| $var_objs = $self->parse_init_vars( |
| 2863 |
|
| params => 1, |
| 2864 |
|
| code => $opt{code}, |
| 2865 |
|
| type => 'var', |
| 2866 |
|
| ); |
| 2867 |
|
| } |
| 2868 |
|
| |
| 2869 |
|
| # Special '_' variable |
| 2870 |
|
| if ($opt{topic_var} and not $has_vars) { |
| 2871 |
|
| my $code = '_'; |
| 2872 |
|
| $has_vars = 1; |
| 2873 |
|
| $var_objs = $self->parse_init_vars(code => \$code, type => 'var'); |
| 2874 |
|
| } |
| 2875 |
|
| |
| 2876 |
|
| local $self->{current_block} = $block if $has_vars; |
| 2877 |
|
| |
| 2878 |
|
| my $obj = $self->parse_script(code => $opt{code}); |
| 2879 |
|
| |
| 2880 |
|
| $self->{curly_brackets} |
| 2881 |
|
| && $self->fatal_error( |
| 2882 |
|
| code => $_, |
| 2883 |
|
| pos => $p - 1, |
| 2884 |
|
| error => "unbalanced curly bracket", |
| 2885 |
|
| ); |
| 2886 |
|
| |
| 2887 |
|
| #$block->{vars} = [ |
| 2888 |
|
| # map { $_->{obj} } |
| 2889 |
|
| # grep { ref($_) eq 'HASH' and ref($_->{obj}) eq 'Sidef::Variable::Variable' } @{$self->{vars}{$class_name}} |
| 2890 |
|
| #]; |
| 2891 |
|
| |
| 2892 |
|
| if ($has_vars) { |
| 2893 |
|
| $block->{init_vars} = bless({vars => $var_objs}, 'Sidef::Variable::Init'); |
| 2894 |
|
| } |
| 2895 |
|
| |
| 2896 |
|
| $block->{code} = $obj; |
| 2897 |
|
| splice @{$self->{ref_vars_refs}{$class_name}}, 0, $count; |
| 2898 |
|
| $self->{vars}{$class_name} = $ref; |
| 2899 |
|
| |
| 2900 |
|
| return $block; |
| 2901 |
|
| } |
| 2902 |
|
| |
| 2903 |
|
| return; |
| 2904 |
|
| } |
| 2905 |
|
| |
| 2906 |
|
| sub append_method { |
| 2907 |
|
| my ($self, %opt) = @_; |
| 2908 |
|
| |
| 2909 |
|
| # Hyper-operator |
| 2910 |
|
| if (exists $self->{hyper_ops}{$opt{op_type}}) { |
| 2911 |
|
| push @{$opt{array}}, |
| 2912 |
|
| { |
| 2913 |
|
| method => $self->{hyper_ops}{$opt{op_type}}[1], |
| 2914 |
|
| arg => [$opt{method}], |
| 2915 |
|
| }; |
| 2916 |
|
| } |
| 2917 |
|
| |
| 2918 |
|
| # Basic operator/method |
| 2919 |
|
| else { |
| 2920 |
|
| push @{$opt{array}}, {method => $opt{method}}; |
| 2921 |
|
| } |
| 2922 |
|
| |
| 2923 |
|
| # Append the argument (if any) |
| 2924 |
|
| if (exists($opt{arg}) and (%{$opt{arg}} || ($opt{method} =~ /^$self->{operators_re}\z/))) { |
| 2925 |
|
| push @{$opt{array}[-1]{arg}}, $opt{arg}; |
| 2926 |
|
| } |
| 2927 |
|
| } |
| 2928 |
|
| |
| 2929 |
|
| sub parse_methods { |
| 2930 |
|
| my ($self, %opt) = @_; |
| 2931 |
|
| |
| 2932 |
|
| my @methods; |
| 2933 |
|
| local *_ = $opt{code}; |
| 2934 |
|
| my $orig_pos = pos($_); |
| 2935 |
|
| |
| 2936 |
|
| { |
| 2937 |
|
| if ((/\G(?![-=]>)/ && /\G(?=$self->{operators_re})/o) || /\G\./gc) { |
| 2938 |
|
| my ($method, $req_arg, $op_type) = $self->get_method_name(code => $opt{code}); |
| 2939 |
|
| |
| 2940 |
|
| if (defined($method)) { |
| 2941 |
|
| |
| 2942 |
|
| my $has_arg; |
| 2943 |
|
| if (/\G\h*(?=[({])/gc || $req_arg) { |
| 2944 |
|
| my $arg = ( |
| 2945 |
|
| $req_arg ? $self->parse_obj(code => $opt{code}, multiline => 1) |
| 2946 |
|
| : /\G(?=\()/ ? $self->parse_arg(code => $opt{code}) |
| 2947 |
|
| : /\G(?=\{)/ ? $self->parse_block(code => $opt{code}, topic_var => 1) |
| 2948 |
|
| : die "[PARSER ERROR] Something is wrong in the if condition" |
| 2949 |
|
| ); |
| 2950 |
|
| |
| 2951 |
|
| if (defined $arg) { |
| 2952 |
|
| $has_arg = 1; |
| 2953 |
|
| $self->append_method( |
| 2954 |
|
| array => \@methods, |
| 2955 |
|
| method => $method, |
| 2956 |
|
| arg => $arg, |
| 2957 |
|
| op_type => $op_type, |
| 2958 |
|
| ); |
| 2959 |
|
| } |
| 2960 |
|
| else { |
| 2961 |
|
| $self->fatal_error( |
| 2962 |
|
| code => $_, |
| 2963 |
|
| pos => $orig_pos, |
| 2964 |
|
| error => "operator `$method` requires a right-side operand", |
| 2965 |
|
| ); |
| 2966 |
|
| } |
| 2967 |
|
| } |
| 2968 |
|
| |
| 2969 |
|
| $has_arg || do { |
| 2970 |
|
| $self->append_method( |
| 2971 |
|
| array => \@methods, |
| 2972 |
|
| method => $method, |
| 2973 |
|
| op_type => $op_type, |
| 2974 |
|
| ); |
| 2975 |
|
| }; |
| 2976 |
|
| redo; |
| 2977 |
|
| } |
| 2978 |
|
| } |
| 2979 |
|
| } |
| 2980 |
|
| |
| 2981 |
|
| return \@methods; |
| 2982 |
|
| } |
| 2983 |
|
| |
| 2984 |
|
| sub parse_suffixes { |
| 2985 |
|
| my ($self, %opt) = @_; |
| 2986 |
|
| |
| 2987 |
|
| my $struct = $opt{struct}; |
| 2988 |
|
| local *_ = $opt{code}; |
| 2989 |
|
| |
| 2990 |
|
| my $parsed = 0; |
| 2991 |
|
| |
| 2992 |
|
| if (/\G(?=[\{\[])/) { |
| 2993 |
|
| #<<< |
| 2994 |
|
| $struct->{$self->{class}}[-1]{self} = { |
| 2995 |
|
| $self->{class} => [ |
| 2996 |
|
| { |
| 2997 |
|
| self => $struct->{$self->{class}}[-1]{self}, |
| 2998 |
|
| exists($struct->{$self->{class}}[-1]{call}) |
| 2999 |
|
| ? (call => delete $struct->{$self->{class}}[-1]{call}) |
| 3000 |
|
| : (), |
| 3001 |
|
| exists($struct->{$self->{class}}[-1]{ind}) |
| 3002 |
|
| ? (ind => delete $struct->{$self->{class}}[-1]{ind}) |
| 3003 |
|
| : (), |
| 3004 |
|
| } |
| 3005 |
|
| ] |
| 3006 |
|
| }; |
| 3007 |
|
| #>>> |
| 3008 |
|
| } |
| 3009 |
|
| |
| 3010 |
|
| { |
| 3011 |
|
| if (/\G(?=\{)/) { |
| 3012 |
|
| while (/\G(?=\{)/) { |
| 3013 |
|
| my $lookup = $self->parse_lookup(code => $opt{code}); |
| 3014 |
|
| push @{$struct->{$self->{class}}[-1]{ind}}, {hash => $lookup->{$self->{class}}}; |
| 3015 |
|
| } |
| 3016 |
|
| |
| 3017 |
|
| $parsed ||= 1; |
| 3018 |
|
| redo; |
| 3019 |
|
| } |
| 3020 |
|
| |
| 3021 |
|
| if (/\G(?=\[)/) { |
| 3022 |
|
| while (/\G(?=\[)/) { |
| 3023 |
|
| my ($ind) = $self->parse_expr(code => $opt{code}); |
| 3024 |
|
| push @{$struct->{$self->{class}}[-1]{ind}}, {array => $ind}; |
| 3025 |
|
| } |
| 3026 |
|
| |
| 3027 |
|
| $parsed ||= 1; |
| 3028 |
|
| redo; |
| 3029 |
|
| } |
| 3030 |
|
| |
| 3031 |
|
| if (/\G\h*(?=\()/gc) { |
| 3032 |
|
| #<<< |
| 3033 |
|
| $struct->{$self->{class}}[-1]{self} = { |
| 3034 |
|
| $self->{class} => [ |
| 3035 |
|
| { |
| 3036 |
|
| self => $struct->{$self->{class}}[-1]{self}, |
| 3037 |
|
| exists($struct->{$self->{class}}[-1]{call}) |
| 3038 |
|
| ? (call => delete $struct->{$self->{class}}[-1]{call}) |
| 3039 |
|
| : (), |
| 3040 |
|
| exists($struct->{$self->{class}}[-1]{ind}) |
| 3041 |
|
| ? (ind => delete $struct->{$self->{class}}[-1]{ind}) |
| 3042 |
|
| : (), |
| 3043 |
|
| } |
| 3044 |
|
| ] |
| 3045 |
|
| }; |
| 3046 |
|
| #>>> |
| 3047 |
|
| |
| 3048 |
|
| my $arg = $self->parse_arg(code => $opt{code}); |
| 3049 |
|
| |
| 3050 |
|
| push @{$struct->{$self->{class}}[-1]{call}}, |
| 3051 |
|
| { |
| 3052 |
|
| method => 'call', |
| 3053 |
|
| (%{$arg} ? (arg => [$arg]) : ()) |
| 3054 |
|
| }; |
| 3055 |
|
| |
| 3056 |
|
| redo; |
| 3057 |
|
| } |
| 3058 |
|
| } |
| 3059 |
|
| |
| 3060 |
|
| $parsed; |
| 3061 |
|
| } |
| 3062 |
|
| |
| 3063 |
|
| sub backtrack_whitespace { |
| 3064 |
|
| my ($self, %opt) = @_; |
| 3065 |
|
| |
| 3066 |
|
| local *_ = $opt{code}; |
| 3067 |
|
| |
| 3068 |
|
| # Backtrack the removal of whitespace |
| 3069 |
|
| while (1) { |
| 3070 |
|
| my $s = substr($_, pos($_) - 1, 1); |
| 3071 |
|
| |
| 3072 |
|
| if ($s =~ /\R/) { |
| 3073 |
|
| $self->{line} -= 1; |
| 3074 |
|
| pos($_) -= 1; |
| 3075 |
|
| last; |
| 3076 |
|
| } |
| 3077 |
|
| elsif ($s =~ /\h/) { |
| 3078 |
|
| pos($_) -= 1; |
| 3079 |
|
| } |
| 3080 |
|
| else { |
| 3081 |
|
| last; |
| 3082 |
|
| } |
| 3083 |
|
| } |
| 3084 |
|
| } |
| 3085 |
|
| |
| 3086 |
|
| sub parse_obj { |
| 3087 |
|
| my ($self, %opt) = @_; |
| 3088 |
|
| |
| 3089 |
|
| my %struct; |
| 3090 |
|
| local *_ = $opt{code}; |
| 3091 |
|
| |
| 3092 |
|
| if (not($opt{multiline}) and /\G\h*(?=\R)/gc) { |
| 3093 |
|
| $self->fatal_error( |
| 3094 |
|
| code => $_, |
| 3095 |
|
| pos => pos($_) - 1, |
| 3096 |
|
| error => "unexpected end-of-statement", |
| 3097 |
|
| ); |
| 3098 |
|
| } |
| 3099 |
|
| |
| 3100 |
|
| my ($obj, $obj_key, $method) = $self->parse_expr(code => $opt{code}); |
| 3101 |
|
| |
| 3102 |
|
| if (defined $obj) { |
| 3103 |
|
| push @{$struct{$self->{class}}}, {self => $obj}; |
| 3104 |
|
| |
| 3105 |
|
| # for var in array { ... } |
| 3106 |
|
| if (ref($obj) eq 'Sidef::Types::Block::For' and /\G\h*(?=[*:]?$self->{var_name_re})/goc) { |
| 3107 |
|
| |
| 3108 |
|
| my $class_name = $self->{class}; |
| 3109 |
|
| |
| 3110 |
|
| my @loops; |
| 3111 |
|
| { |
| 3112 |
|
| my @vars; |
| 3113 |
|
| while (/\G([*:])?($self->{var_name_re})/gc) { |
| 3114 |
|
| |
| 3115 |
|
| my $type = $1; |
| 3116 |
|
| my $name = $2; |
| 3117 |
|
| push @vars, |
| 3118 |
|
| bless( |
| 3119 |
|
| { |
| 3120 |
|
| name => $name, |
| 3121 |
|
| type => 'var', |
| 3122 |
|
| class => $class_name, |
| 3123 |
|
| ( |
| 3124 |
|
| $type |
| 3125 |
|
| ? ( |
| 3126 |
|
| slurpy => 1, |
| 3127 |
|
| ($type eq '*' ? (array => 1) : (hash => 1)), |
| 3128 |
|
| ) |
| 3129 |
|
| : () |
| 3130 |
|
| ), |
| 3131 |
|
| }, |
| 3132 |
|
| 'Sidef::Variable::Variable' |
| 3133 |
|
| ); |
| 3134 |
|
| |
| 3135 |
|
| unshift @{$self->{vars}{$class_name}}, |
| 3136 |
|
| { |
| 3137 |
|
| obj => $vars[-1], |
| 3138 |
|
| name => $name, |
| 3139 |
|
| count => 1, |
| 3140 |
|
| type => 'var', |
| 3141 |
|
| line => $self->{line}, |
| 3142 |
|
| }; |
| 3143 |
|
| |
| 3144 |
|
| $type && last; |
| 3145 |
|
| /\G\h*,\h*/gc || last; |
| 3146 |
|
| } |
| 3147 |
|
| |
| 3148 |
|
| /\G\h*(?:in|∈|=|)\h*/gc |
| 3149 |
|
| || $self->fatal_error( |
| 3150 |
|
| error => "expected the token <<in>> after variable declaration in for-loop", |
| 3151 |
|
| code => $_, |
| 3152 |
|
| pos => pos($_), |
| 3153 |
|
| ); |
| 3154 |
|
| |
| 3155 |
|
| my $expr = ( |
| 3156 |
|
| /\G(?=\()/ |
| 3157 |
|
| ? $self->parse_arg(code => $opt{code}) |
| 3158 |
|
| : $self->parse_obj(code => $opt{code}) |
| 3159 |
|
| ); |
| 3160 |
|
| |
| 3161 |
|
| push @loops, |
| 3162 |
|
| { |
| 3163 |
|
| vars => \@vars, |
| 3164 |
|
| expr => $expr, |
| 3165 |
|
| }; |
| 3166 |
|
| |
| 3167 |
|
| /\G\h*,\h*/gc && redo; |
| 3168 |
|
| } |
| 3169 |
|
| |
| 3170 |
|
| my $block = ( |
| 3171 |
|
| /\G\h*(?=\{)/gc |
| 3172 |
|
| ? $self->parse_block(code => $opt{code}) |
| 3173 |
|
| : $self->fatal_error( |
| 3174 |
|
| error => "expected a block", |
| 3175 |
|
| code => $_, |
| 3176 |
|
| pos => pos($_), |
| 3177 |
|
| ) |
| 3178 |
|
| ); |
| 3179 |
|
| |
| 3180 |
|
| # Remove the for-loop variables from the outer scope |
| 3181 |
|
| #<<< |
| 3182 |
|
| my %loop_vars = map { |
| 3183 |
|
| map { refaddr($_) => 1 } @{$_->{vars}} |
| 3184 |
|
| } @loops; |
| 3185 |
|
| |
| 3186 |
|
| @{$self->{vars}{$class_name}} = grep { |
| 3187 |
|
| ref($_) ne 'HASH' |
| 3188 |
|
| or not exists $loop_vars{refaddr($_->{obj})} |
| 3189 |
|
| } @{$self->{vars}{$class_name}}; |
| 3190 |
|
| #>>> |
| 3191 |
|
| |
| 3192 |
|
| # Store the info |
| 3193 |
|
| $obj->{block} = $block; |
| 3194 |
|
| $obj->{loops} = \@loops; |
| 3195 |
|
| |
| 3196 |
|
| # Re-bless the $obj into a different class |
| 3197 |
|
| bless $obj, 'Sidef::Types::Block::ForIn'; |
| 3198 |
|
| } |
| 3199 |
|
| elsif ($obj_key) { |
| 3200 |
|
| my $arg = ( |
| 3201 |
|
| /\G(?=\()/ |
| 3202 |
|
| ? $self->parse_arg(code => $opt{code}) |
| 3203 |
|
| : $self->parse_obj(code => $opt{code}) |
| 3204 |
|
| ); |
| 3205 |
|
| |
| 3206 |
|
| if (defined $arg) { |
| 3207 |
|
| my @arg = ($arg); |
| 3208 |
|
| |
| 3209 |
|
| if (ref($obj) eq 'Sidef::Types::Block::For') { |
| 3210 |
|
| |
| 3211 |
|
| if ($#{$arg->{$self->{class}}} == 2) { |
| 3212 |
|
| @arg = ( |
| 3213 |
|
| map { |
| 3214 |
|
| { $self->{class} => [$_] } |
| 3215 |
|
| } @{$arg->{$self->{class}}} |
| 3216 |
|
| ); |
| 3217 |
|
| |
| 3218 |
|
| if (/\G\h*(?=\{)/gc) { |
| 3219 |
|
| my $block = $self->parse_block(code => $opt{code}); |
| 3220 |
|
| |
| 3221 |
|
| $obj->{expr} = \@arg; |
| 3222 |
|
| $obj->{block} = $block; |
| 3223 |
|
| |
| 3224 |
|
| bless $obj, 'Sidef::Types::Block::CFor'; |
| 3225 |
|
| |
| 3226 |
|
| } |
| 3227 |
|
| else { |
| 3228 |
|
| $self->fatal_error( |
| 3229 |
|
| code => $_, |
| 3230 |
|
| pos => pos($_) - 1, |
| 3231 |
|
| error => "invalid declaration of the `for` loop", |
| 3232 |
|
| reason => "expected a block after `for(;;)`", |
| 3233 |
|
| ); |
| 3234 |
|
| } |
| 3235 |
|
| } |
| 3236 |
|
| elsif ($#{$arg->{$self->{class}}} == 0) { |
| 3237 |
|
| |
| 3238 |
|
| if (/\G\h*(?=\{)/gc) { |
| 3239 |
|
| my $block = $self->parse_block(code => $opt{code}, topic_var => 1); |
| 3240 |
|
| |
| 3241 |
|
| $obj->{expr} = $arg; |
| 3242 |
|
| $obj->{block} = $block; |
| 3243 |
|
| |
| 3244 |
|
| bless $obj, 'Sidef::Types::Block::ForEach'; |
| 3245 |
|
| } |
| 3246 |
|
| else { |
| 3247 |
|
| $self->fatal_error( |
| 3248 |
|
| code => $_, |
| 3249 |
|
| pos => pos($_) - 1, |
| 3250 |
|
| error => "invalid declaration of the `for` loop", |
| 3251 |
|
| reason => "expected a block after `for(...)`", |
| 3252 |
|
| ); |
| 3253 |
|
| } |
| 3254 |
|
| } |
| 3255 |
|
| else { |
| 3256 |
|
| $self->fatal_error( |
| 3257 |
|
| code => $_, |
| 3258 |
|
| pos => pos($_) - 1, |
| 3259 |
|
| error => "invalid declaration of the `for` loop: incorrect number of arguments", |
| 3260 |
|
| ); |
| 3261 |
|
| } |
| 3262 |
|
| } |
| 3263 |
|
| elsif (ref($obj) eq 'Sidef::Types::Block::ForEach') { |
| 3264 |
|
| if (/\G\h*(?=\{)/gc) { |
| 3265 |
|
| my $block = $self->parse_block(code => $opt{code}, topic_var => 1); |
| 3266 |
|
| |
| 3267 |
|
| $obj->{expr} = $arg; |
| 3268 |
|
| $obj->{block} = $block; |
| 3269 |
|
| |
| 3270 |
|
| } |
| 3271 |
|
| else { |
| 3272 |
|
| $self->fatal_error( |
| 3273 |
|
| code => $_, |
| 3274 |
|
| pos => pos($_) - 1, |
| 3275 |
|
| error => "invalid declaration of the `foreach` loop", |
| 3276 |
|
| reason => "expected a block after `foreach(...)`", |
| 3277 |
|
| ); |
| 3278 |
|
| } |
| 3279 |
|
| } |
| 3280 |
|
| elsif (ref($obj) eq 'Sidef::Types::Block::If') { |
| 3281 |
|
| |
| 3282 |
|
| if (/\G\h*(?=\{)/gc) { |
| 3283 |
|
| my $block = $self->parse_block(code => $opt{code}, with_vars => 1); |
| 3284 |
|
| push @{$obj->{if}}, {expr => $arg, block => $block}; |
| 3285 |
|
| |
| 3286 |
|
| ELSIF: { |
| 3287 |
|
| |
| 3288 |
|
| $self->parse_whitespace(code => $opt{code}); |
| 3289 |
|
| |
| 3290 |
|
| if (/\Gelsif\h*(?=\()/gc) { |
| 3291 |
|
| my $arg = $self->parse_arg(code => $opt{code}); |
| 3292 |
|
| $self->parse_whitespace(code => $opt{code}); |
| 3293 |
|
| |
| 3294 |
|
| my $block = $self->parse_block(code => $opt{code}, with_vars => 1) // $self->fatal_error( |
| 3295 |
|
| code => $_, |
| 3296 |
|
| pos => pos($_) - 1, |
| 3297 |
|
| error => "invalid declaration of the `if` statement", |
| 3298 |
|
| reason => "expected a block after `elsif(...)`", |
| 3299 |
|
| ); |
| 3300 |
|
| |
| 3301 |
|
| push @{$obj->{if}}, {expr => $arg, block => $block}; |
| 3302 |
|
| redo ELSIF; |
| 3303 |
|
| } |
| 3304 |
|
| } |
| 3305 |
|
| |
| 3306 |
|
| if (/\Gelse\h*(?=\{)/gc) { |
| 3307 |
|
| my $block = $self->parse_block(code => $opt{code}); |
| 3308 |
|
| $obj->{else}{block} = $block; |
| 3309 |
|
| } |
| 3310 |
|
| |
| 3311 |
|
| $self->backtrack_whitespace(code => $opt{code}); |
| 3312 |
|
| } |
| 3313 |
|
| else { |
| 3314 |
|
| $self->fatal_error( |
| 3315 |
|
| code => $_, |
| 3316 |
|
| pos => pos($_) - 1, |
| 3317 |
|
| error => "invalid declaration of the `if` statement", |
| 3318 |
|
| reason => "expected a block after `if(...)`", |
| 3319 |
|
| ); |
| 3320 |
|
| } |
| 3321 |
|
| } |
| 3322 |
|
| elsif (ref($obj) eq 'Sidef::Types::Block::With') { |
| 3323 |
|
| |
| 3324 |
|
| if (/\G\h*(?=\{)/gc) { |
| 3325 |
|
| my $block = $self->parse_block(code => $opt{code}, topic_var => 1); |
| 3326 |
|
| push @{$obj->{with}}, {expr => $arg, block => $block}; |
| 3327 |
|
| |
| 3328 |
|
| ORWITH: { |
| 3329 |
|
| |
| 3330 |
|
| $self->parse_whitespace(code => $opt{code}); |
| 3331 |
|
| |
| 3332 |
|
| if (/\Gorwith\h*(?=\()/gc) { |
| 3333 |
|
| my $arg = $self->parse_arg(code => $opt{code}); |
| 3334 |
|
| $self->parse_whitespace(code => $opt{code}); |
| 3335 |
|
| |
| 3336 |
|
| my $block = $self->parse_block(code => $opt{code}, topic_var => 1) // $self->fatal_error( |
| 3337 |
|
| code => $_, |
| 3338 |
|
| pos => pos($_) - 1, |
| 3339 |
|
| error => "invalid declaration of the `with` statement", |
| 3340 |
|
| reason => "expected a block after `orwith(...)`", |
| 3341 |
|
| ); |
| 3342 |
|
| |
| 3343 |
|
| push @{$obj->{with}}, {expr => $arg, block => $block}; |
| 3344 |
|
| redo ORWITH; |
| 3345 |
|
| } |
| 3346 |
|
| } |
| 3347 |
|
| |
| 3348 |
|
| if (/\Gelse\h*(?=\{)/gc) { |
| 3349 |
|
| my $block = $self->parse_block(code => $opt{code}); |
| 3350 |
|
| $obj->{else}{block} = $block; |
| 3351 |
|
| } |
| 3352 |
|
| |
| 3353 |
|
| $self->backtrack_whitespace(code => $opt{code}); |
| 3354 |
|
| } |
| 3355 |
|
| else { |
| 3356 |
|
| $self->fatal_error( |
| 3357 |
|
| code => $_, |
| 3358 |
|
| pos => pos($_) - 1, |
| 3359 |
|
| error => "invalid declaration of the `with` statement", |
| 3360 |
|
| reason => "expected a block after `with(...)`", |
| 3361 |
|
| ); |
| 3362 |
|
| } |
| 3363 |
|
| } |
| 3364 |
|
| elsif (ref($obj) eq 'Sidef::Types::Block::While') { |
| 3365 |
|
| if (/\G\h*(?=\{)/gc) { |
| 3366 |
|
| my $block = $self->parse_block(code => $opt{code}, with_vars => 1); |
| 3367 |
|
| $obj->{expr} = $arg; |
| 3368 |
|
| $obj->{block} = $block; |
| 3369 |
|
| } |
| 3370 |
|
| else { |
| 3371 |
|
| $self->fatal_error( |
| 3372 |
|
| code => $_, |
| 3373 |
|
| pos => pos($_) - 1, |
| 3374 |
|
| error => "invalid declaration of the `while` statement", |
| 3375 |
|
| reason => "expected a block after `while(...)`", |
| 3376 |
|
| ); |
| 3377 |
|
| } |
| 3378 |
|
| } |
| 3379 |
|
| else { |
| 3380 |
|
| push @{$struct{$self->{class}}[-1]{call}}, {method => $method, arg => \@arg}; |
| 3381 |
|
| } |
| 3382 |
|
| } |
| 3383 |
|
| else { |
| 3384 |
|
| $self->fatal_error( |
| 3385 |
|
| code => $_, |
| 3386 |
|
| error => "expected an argument. Did you mean '$method()' instead?", |
| 3387 |
|
| pos => pos($_) - 1, |
| 3388 |
|
| ); |
| 3389 |
|
| } |
| 3390 |
|
| } |
| 3391 |
|
| |
| 3392 |
|
| { |
| 3393 |
|
| # Method call |
| 3394 |
|
| if (/\G\h*(?=\.\h*(?:$self->{method_name_re}|[(\$]))/ogc) { |
| 3395 |
|
| my $methods = $self->parse_methods(code => $opt{code}); |
| 3396 |
|
| push @{$struct{$self->{class}}[-1]{call}}, @{$methods}; |
| 3397 |
|
| } |
| 3398 |
|
| |
| 3399 |
|
| # Code extended on a newline |
| 3400 |
|
| if (/\G\h*\\(?!\\)/gc) { |
| 3401 |
|
| $self->parse_whitespace(code => $opt{code}); |
| 3402 |
|
| redo; |
| 3403 |
|
| } |
| 3404 |
|
| |
| 3405 |
|
| # Object call |
| 3406 |
|
| if (/\G\h*(?=\()/gc) { |
| 3407 |
|
| my $arg = $self->parse_arg(code => $opt{code}); |
| 3408 |
|
| |
| 3409 |
|
| push @{$struct{$self->{class}}[-1]{call}}, |
| 3410 |
|
| { |
| 3411 |
|
| method => 'call', |
| 3412 |
|
| (%{$arg} ? (arg => [$arg]) : ()) |
| 3413 |
|
| }; |
| 3414 |
|
| |
| 3415 |
|
| redo; |
| 3416 |
|
| } |
| 3417 |
|
| |
| 3418 |
|
| # Do-while construct |
| 3419 |
|
| if (ref($obj) eq 'Sidef::Types::Block::Do' and /\G\h*while\b/gc) { |
| 3420 |
|
| my $arg = $self->parse_obj(code => $opt{code}); |
| 3421 |
|
| push @{$struct{$self->{class}}[-1]{call}}, {keyword => 'while', arg => [$arg]}; |
| 3422 |
|
| } |
| 3423 |
|
| |
| 3424 |
|
| # Parse array and hash fetchers ([...] and {...}) |
| 3425 |
|
| if (/\G\.\h*(?=[\[\{])/gc or 1) { |
| 3426 |
|
| $self->parse_suffixes(code => $opt{code}, struct => \%struct) && redo; |
| 3427 |
|
| } |
| 3428 |
|
| |
| 3429 |
|
| # Tightly-binded operator |
| 3430 |
|
| if ( |
| 3431 |
|
| /\G(?![=-]>)/ # not '=>' or '->' |
| 3432 |
|
| && ( |
| 3433 |
|
| /\G(?=$self->{operators_re})/o # operator |
| 3434 |
|
| || /\G\h*\.\h*(?!\.\.)(?=$self->{operators_re})/gco # dot followed by operator |
| 3435 |
|
| || /\G(?=[⁰¹²³⁴⁵⁶⁷⁸⁹])/ # unicode superscript |
| 3436 |
|
| ) |
| 3437 |
|
| ) { |
| 3438 |
|
| |
| 3439 |
|
| my $orig_pos = pos($_); |
| 3440 |
|
| my ($method, $req_arg, $op_type) = $self->get_method_name(code => $opt{code}); |
| 3441 |
|
| |
| 3442 |
|
| if (defined($method)) { |
| 3443 |
|
| |
| 3444 |
|
| my $has_arg; |
| 3445 |
|
| if ($req_arg) { |
| 3446 |
|
| my $arg = $self->parse_obj(code => $opt{code}, multiline => 1); |
| 3447 |
|
| |
| 3448 |
|
| if (defined $arg) { |
| 3449 |
|
| if (ref $arg ne 'HASH') { |
| 3450 |
|
| $arg = {$self->{class} => [{self => $arg}]}; |
| 3451 |
|
| } |
| 3452 |
|
| |
| 3453 |
|
| my $methods = $self->parse_methods(code => $opt{code}); |
| 3454 |
|
| if (@{$methods}) { |
| 3455 |
|
| push @{$arg->{$self->{class}}[-1]{call}}, @{$methods}; |
| 3456 |
|
| } |
| 3457 |
|
| |
| 3458 |
|
| $has_arg = 1; |
| 3459 |
|
| $self->append_method( |
| 3460 |
|
| array => \@{$struct{$self->{class}}[-1]{call}}, |
| 3461 |
|
| method => $method, |
| 3462 |
|
| arg => $arg, |
| 3463 |
|
| op_type => $op_type, |
| 3464 |
|
| ); |
| 3465 |
|
| } |
| 3466 |
|
| else { |
| 3467 |
|
| $self->fatal_error( |
| 3468 |
|
| code => $_, |
| 3469 |
|
| pos => $orig_pos, |
| 3470 |
|
| error => "operator `$method` requires a right-side operand", |
| 3471 |
|
| ); |
| 3472 |
|
| } |
| 3473 |
|
| } |
| 3474 |
|
| |
| 3475 |
|
| $has_arg || do { |
| 3476 |
|
| $self->append_method( |
| 3477 |
|
| array => \@{$struct{$self->{class}}[-1]{call}}, |
| 3478 |
|
| method => $method, |
| 3479 |
|
| op_type => $op_type, |
| 3480 |
|
| ); |
| 3481 |
|
| }; |
| 3482 |
|
| redo; |
| 3483 |
|
| } |
| 3484 |
|
| } |
| 3485 |
|
| } |
| 3486 |
|
| } |
| 3487 |
|
| else { |
| 3488 |
|
| return; |
| 3489 |
|
| } |
| 3490 |
|
| |
| 3491 |
|
| return \%struct; |
| 3492 |
|
| } |
| 3493 |
|
| |
| 3494 |
|
| sub parse_script { |
| 3495 |
|
| my ($self, %opt) = @_; |
| 3496 |
|
| |
| 3497 |
|
| my %struct; |
| 3498 |
|
| local *_ = $opt{code}; |
| 3499 |
|
| MAIN: { |
| 3500 |
|
| $self->parse_whitespace(code => $opt{code}); |
| 3501 |
|
| |
| 3502 |
|
| if (/\G\@:([^\W\d]\w*+)/gc) { |
| 3503 |
|
| push @{$struct{$self->{class}}}, {self => bless({name => $1}, 'Sidef::Variable::Label')}; |
| 3504 |
|
| redo; |
| 3505 |
|
| } |
| 3506 |
|
| |
| 3507 |
|
| if (/\G(?:[;,]+|=>)/gc) { |
| 3508 |
|
| redo; |
| 3509 |
|
| } |
| 3510 |
|
| |
| 3511 |
|
| my $obj; |
| 3512 |
|
| |
| 3513 |
|
| # Ternary operator |
| 3514 |
|
| if (%struct && /\G\?/gc) { |
| 3515 |
|
| $self->parse_whitespace(code => $opt{code}); |
| 3516 |
|
| |
| 3517 |
|
| my $true = ( |
| 3518 |
|
| /\G(?=\()/ |
| 3519 |
|
| ? $self->parse_arg(code => $opt{code}) |
| 3520 |
|
| : $self->parse_obj(code => $opt{code}) |
| 3521 |
|
| ); |
| 3522 |
|
| |
| 3523 |
|
| $self->parse_whitespace(code => $opt{code}); |
| 3524 |
|
| |
| 3525 |
|
| /\G:/gc |
| 3526 |
|
| || $self->fatal_error( |
| 3527 |
|
| code => $_, |
| 3528 |
|
| pos => pos($_), |
| 3529 |
|
| error => "invalid usage of the ternary operator", |
| 3530 |
|
| reason => "expected ':'", |
| 3531 |
|
| ); |
| 3532 |
|
| |
| 3533 |
|
| $self->parse_whitespace(code => $opt{code}); |
| 3534 |
|
| |
| 3535 |
|
| my $false = ( |
| 3536 |
|
| /\G(?=\()/ |
| 3537 |
|
| ? $self->parse_arg(code => $opt{code}) |
| 3538 |
|
| : $self->parse_obj(code => $opt{code}) |
| 3539 |
|
| ); |
| 3540 |
|
| |
| 3541 |
|
| $obj = bless( |
| 3542 |
|
| { |
| 3543 |
|
| cond => scalar {$self->{class} => [pop @{$struct{$self->{class}}}]}, |
| 3544 |
|
| true => $true, |
| 3545 |
|
| false => $false |
| 3546 |
|
| }, |
| 3547 |
|
| 'Sidef::Types::Bool::Ternary' |
| 3548 |
|
| ); |
| 3549 |
|
| } |
| 3550 |
|
| else { |
| 3551 |
|
| $obj = $self->parse_obj(code => $opt{code}); |
| 3552 |
|
| } |
| 3553 |
|
| |
| 3554 |
|
| if (defined $obj) { |
| 3555 |
|
| push @{$struct{$self->{class}}}, {self => $obj}; |
| 3556 |
|
| |
| 3557 |
|
| { |
| 3558 |
|
| my $pos_before = pos($_); |
| 3559 |
|
| $self->parse_whitespace(code => $opt{code}); |
| 3560 |
|
| |
| 3561 |
|
| # End of expression |
| 3562 |
|
| if (/\G(?:[;,]+|=>)/gc or substr($_, $pos_before, pos($_) - $pos_before) =~ /\R/) { |
| 3563 |
|
| redo MAIN; |
| 3564 |
|
| } |
| 3565 |
|
| |
| 3566 |
|
| # Code extended on a newline |
| 3567 |
|
| if (/\G\\(?!\\)/gc) { |
| 3568 |
|
| $self->parse_whitespace(code => $opt{code}); |
| 3569 |
|
| } |
| 3570 |
|
| |
| 3571 |
|
| my $is_operator = /\G(?!->)/ && /\G(?=($self->{operators_re}))/o; |
| 3572 |
|
| |
| 3573 |
|
| if ($is_operator or /\G(?:->|\.)\h*/gc) { |
| 3574 |
|
| |
| 3575 |
|
| # Implicit end of statement -- redo |
| 3576 |
|
| $self->parse_whitespace(code => $opt{code}); |
| 3577 |
|
| |
| 3578 |
|
| my $methods; |
| 3579 |
|
| if ($is_operator) { |
| 3580 |
|
| $methods = $self->parse_methods(code => $opt{code}); |
| 3581 |
|
| } |
| 3582 |
|
| else { |
| 3583 |
|
| my $code = substr($_, pos); |
| 3584 |
|
| my $dot_op = $code =~ /^\./; |
| 3585 |
|
| if ($dot_op) { $code = ". $code" } |
| 3586 |
|
| else { $code = ".$code" } |
| 3587 |
|
| $methods = $self->parse_methods(code => \$code); |
| 3588 |
|
| pos($_) += pos($code) - ($dot_op ? 2 : 1); |
| 3589 |
|
| } |
| 3590 |
|
| |
| 3591 |
|
| if (@{$methods}) { |
| 3592 |
|
| push @{$struct{$self->{class}}[-1]{call}}, @{$methods}; |
| 3593 |
|
| } |
| 3594 |
|
| else { |
| 3595 |
|
| $self->fatal_error( |
| 3596 |
|
| error => 'incomplete method name', |
| 3597 |
|
| code => $_, |
| 3598 |
|
| pos => pos($_) - 1, |
| 3599 |
|
| ); |
| 3600 |
|
| } |
| 3601 |
|
| |
| 3602 |
|
| $self->parse_suffixes(code => $opt{code}, struct => \%struct); |
| 3603 |
|
| redo; |
| 3604 |
|
| } |
| 3605 |
|
| elsif (/\G(if|while|and|or)\b\h*/gc) { |
| 3606 |
|
| my $keyword = $1; |
| 3607 |
|
| my $obj = $self->parse_obj(code => $opt{code}); |
| 3608 |
|
| push @{$struct{$self->{class}}[-1]{call}}, {keyword => $keyword, arg => [$obj]}; |
| 3609 |
|
| redo; |
| 3610 |
|
| } |
| 3611 |
|
| else { |
| 3612 |
|
| redo MAIN; |
| 3613 |
|
| } |
| 3614 |
|
| } |
| 3615 |
|
| } |
| 3616 |
|
| |
| 3617 |
|
| if (/\G(?:[;,]+|=>)/gc) { |
| 3618 |
|
| redo; |
| 3619 |
|
| } |
| 3620 |
|
| |
| 3621 |
|
| # We are at the end of the script. |
| 3622 |
|
| # We make some checks, and return the \%struct hash ref. |
| 3623 |
|
| if (/\G\z/) { |
| 3624 |
|
| $self->check_declarations($self->{ref_vars}); |
| 3625 |
|
| return \%struct; |
| 3626 |
|
| } |
| 3627 |
|
| |
| 3628 |
|
| if (/\G\]/gc) { |
| 3629 |
|
| |
| 3630 |
|
| if (--$self->{right_brackets} < 0) { |
| 3631 |
|
| $self->fatal_error( |
| 3632 |
|
| error => 'unbalanced right bracket', |
| 3633 |
|
| code => $_, |
| 3634 |
|
| pos => pos($_) - 1, |
| 3635 |
|
| ); |
| 3636 |
|
| } |
| 3637 |
|
| |
| 3638 |
|
| return \%struct; |
| 3639 |
|
| } |
| 3640 |
|
| |
| 3641 |
|
| if (/\G\}/gc) { |
| 3642 |
|
| |
| 3643 |
|
| if (--$self->{curly_brackets} < 0) { |
| 3644 |
|
| $self->fatal_error( |
| 3645 |
|
| error => 'unbalanced curly bracket', |
| 3646 |
|
| code => $_, |
| 3647 |
|
| pos => pos($_) - 1, |
| 3648 |
|
| ); |
| 3649 |
|
| } |
| 3650 |
|
| |
| 3651 |
|
| return \%struct; |
| 3652 |
|
| } |
| 3653 |
|
| |
| 3654 |
|
| # The end of an argument expression |
| 3655 |
|
| if (/\G\)/gc) { |
| 3656 |
|
| |
| 3657 |
|
| if (--$self->{parentheses} < 0) { |
| 3658 |
|
| $self->fatal_error( |
| 3659 |
|
| error => 'unbalanced parenthesis', |
| 3660 |
|
| code => $_, |
| 3661 |
|
| pos => pos($_) - 1, |
| 3662 |
|
| ); |
| 3663 |
|
| } |
| 3664 |
|
| |
| 3665 |
|
| return \%struct; |
| 3666 |
|
| } |
| 3667 |
|
| |
| 3668 |
|
| $self->fatal_error( |
| 3669 |
|
| code => $_, |
| 3670 |
|
| pos => (pos($_)), |
| 3671 |
|
| error => "expected a method", |
| 3672 |
|
| ); |
| 3673 |
|
| |
| 3674 |
|
| pos($_) += 1; |
| 3675 |
|
| redo; |
| 3676 |
|
| } |
| 3677 |
|
| } |
| 3678 |
|
| |
| 3679 |
|
| 1 |